From 2b80ab425b266e1b6f61a74b8ee1f04b6b6213bf Mon Sep 17 00:00:00 2001 From: Robert Xiao Date: Fri, 16 Jun 2023 14:47:03 -0700 Subject: [PATCH] Return new UC_ERR_OVERFLOW instead of UC_ERR_NOMEM when reg buffer is too small --- include/unicorn/unicorn.h | 21 +++++++++++++-------- qemu/unicorn_common.h | 2 +- uc.c | 2 ++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/include/unicorn/unicorn.h b/include/unicorn/unicorn.h index dbe8d027..3814b8e0 100644 --- a/include/unicorn/unicorn.h +++ b/include/unicorn/unicorn.h @@ -192,6 +192,7 @@ typedef enum uc_err { UC_ERR_HOOK_EXIST, // hook for this event already existed UC_ERR_RESOURCE, // Insufficient resource: uc_emu_start() UC_ERR_EXCEPTION, // Unhandled CPU exception + UC_ERR_OVERFLOW, // Provided buffer is not large enough: uc_reg_*2() } uc_err; /* @@ -807,7 +808,7 @@ uc_err uc_reg_read(uc_engine *uc, int regid, void *value); @size: size of value being written; on return, size of value written @return UC_ERR_OK on success; UC_ERR_ARG if register number or value is - invalid; UC_ERR_NOMEM if value is not large enough. + invalid; UC_ERR_OVERFLOW if value is not large enough for the register. */ UNICORN_EXPORT uc_err uc_reg_write2(uc_engine *uc, int regid, const void *value, size_t *size); @@ -821,7 +822,7 @@ uc_err uc_reg_write2(uc_engine *uc, int regid, const void *value, size_t *size); @size: size of value buffer; on return, size of value read @return UC_ERR_OK on success; UC_ERR_ARG if register number or value is - invalid; UC_ERR_NOMEM if value is not large enough. + invalid; UC_ERR_OVERFLOW if value is not large enough to hold the register. */ UNICORN_EXPORT uc_err uc_reg_read2(uc_engine *uc, int regid, void *value, size_t *size); @@ -865,7 +866,8 @@ uc_err uc_reg_read_batch(uc_engine *uc, int *regs, void **vals, int count); @count: length of *regs, *vals and *sizes @return UC_ERR_OK on success; UC_ERR_ARG if some register number or value is - invalid; UC_ERR_NOMEM if some value is not large enough. + invalid; UC_ERR_OVERFLOW if some value is not large enough for the + corresponding register. */ UNICORN_EXPORT uc_err uc_reg_write_batch2(uc_engine *uc, int *regs, const void *const *vals, @@ -882,7 +884,8 @@ uc_err uc_reg_write_batch2(uc_engine *uc, int *regs, const void *const *vals, @count: length of *regs, *vals and *sizes @return UC_ERR_OK on success; UC_ERR_ARG if some register number or value is - invalid; UC_ERR_NOMEM if some value is not large enough. + invalid; UC_ERR_OVERFLOW if some value is not large enough to hold the + corresponding register. */ UNICORN_EXPORT uc_err uc_reg_read_batch2(uc_engine *uc, int *regs, void *const *vals, @@ -1218,7 +1221,7 @@ uc_err uc_context_reg_read(uc_context *ctx, int regid, void *value); @size: size of value being written; on return, size of value written @return UC_ERR_OK on success; UC_ERR_ARG if register number or value is - invalid; UC_ERR_NOMEM if value is not large enough. + invalid; UC_ERR_OVERFLOW if value is not large enough for the register. */ UNICORN_EXPORT uc_err uc_context_reg_write2(uc_context *ctx, int regid, const void *value, @@ -1233,7 +1236,7 @@ uc_err uc_context_reg_write2(uc_context *ctx, int regid, const void *value, @size: size of value buffer; on return, size of value read @return UC_ERR_OK on success; UC_ERR_ARG if register number or value is - invalid; UC_ERR_NOMEM if value is not large enough. + invalid; UC_ERR_OVERFLOW if value is not large enough to hold the register. */ UNICORN_EXPORT uc_err uc_context_reg_read2(uc_context *ctx, int regid, void *value, @@ -1279,7 +1282,8 @@ uc_err uc_context_reg_read_batch(uc_context *ctx, int *regs, void **vals, @count: length of *regs, *vals and *sizes @return UC_ERR_OK on success; UC_ERR_ARG if some register number or value is - invalid; UC_ERR_NOMEM if some value is not large enough. + invalid; UC_ERR_OVERFLOW if some value is not large enough for the + corresponding register. */ UNICORN_EXPORT uc_err uc_context_reg_write_batch2(uc_context *ctx, int *regs, @@ -1297,7 +1301,8 @@ uc_err uc_context_reg_write_batch2(uc_context *ctx, int *regs, @count: length of *regs, *vals and *sizes @return UC_ERR_OK on success; UC_ERR_ARG if some register number or value is - invalid; UC_ERR_NOMEM if some value is not large enough. + invalid; UC_ERR_OVERFLOW if some value is not large enough to hold the + corresponding register. */ UNICORN_EXPORT uc_err uc_context_reg_read_batch2(uc_context *ctx, int *regs, void *const *vals, diff --git a/qemu/unicorn_common.h b/qemu/unicorn_common.h index 035fe061..1a622fe7 100644 --- a/qemu/unicorn_common.h +++ b/qemu/unicorn_common.h @@ -131,7 +131,7 @@ static inline void uc_common_init(struct uc_struct* uc) #define CHECK_REG_TYPE(type) do { \ if (unlikely(*size < sizeof(type))) { \ - return UC_ERR_NOMEM; \ + return UC_ERR_OVERFLOW; \ } \ *size = sizeof(type); \ ret = UC_ERR_OK; \ diff --git a/uc.c b/uc.c index 0c7a8f32..b92dddc1 100644 --- a/uc.c +++ b/uc.c @@ -148,6 +148,8 @@ const char *uc_strerror(uc_err code) return "Insufficient resource (UC_ERR_RESOURCE)"; case UC_ERR_EXCEPTION: return "Unhandled CPU exception (UC_ERR_EXCEPTION)"; + case UC_ERR_OVERFLOW: + return "Provided buffer is too small (UC_ERR_OVERFLOW)"; } }