Skip to content

Commit

Permalink
Return new UC_ERR_OVERFLOW instead of UC_ERR_NOMEM when reg buffer is…
Browse files Browse the repository at this point in the history
… too small
  • Loading branch information
nneonneo committed Jun 16, 2023
1 parent b041345 commit 2b80ab4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
21 changes: 13 additions & 8 deletions include/unicorn/unicorn.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion qemu/unicorn_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; \
Expand Down
2 changes: 2 additions & 0 deletions uc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
}
}

Expand Down

0 comments on commit 2b80ab4

Please sign in to comment.