From ea54204952412ac1883160b4dc90a5c18deafb70 Mon Sep 17 00:00:00 2001 From: Andrew Dutcher Date: Mon, 10 Oct 2016 14:04:51 -0700 Subject: [PATCH] Tweak some names in a few places, encapsulate the uc_context struct to hide it from users for some reason --- include/uc_priv.h | 9 +++++++++ include/unicorn/unicorn.h | 11 +++-------- tests/unit/test_x86.c | 12 ++++++------ uc.c | 31 +++++++++++++++++-------------- 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/include/uc_priv.h b/include/uc_priv.h index e96236ea..aeeac888 100644 --- a/include/uc_priv.h +++ b/include/uc_priv.h @@ -245,6 +245,15 @@ struct uc_struct { uint64_t next_pc; // save next PC for some special cases }; +// Metadata stub for the variable-size cpu context used with uc_context_*() +struct uc_context { + uc_arch arch; + uc_mode mode; + size_t size; + bool used; + char data[0]; +}; + // check if this address is mapped in (via uc_mem_map()) MemoryRegion *memory_mapping(struct uc_struct* uc, uint64_t address); diff --git a/include/unicorn/unicorn.h b/include/unicorn/unicorn.h index c3115a0f..d531c44c 100644 --- a/include/unicorn/unicorn.h +++ b/include/unicorn/unicorn.h @@ -273,14 +273,9 @@ typedef enum uc_query_type { UC_QUERY_PAGE_SIZE, } uc_query_type; -// Metadata stub for the variable-size cpu context used with uc_context_*() -typedef struct uc_context { - uc_arch arch; - uc_mode mode; - size_t size; - bool used; - char data[0]; -} uc_context; +// Opaque storage for CPU context, used with uc_context_*() +struct uc_context; +typedef struct uc_context uc_context; /* Return combined API version & major and minor version numbers. diff --git a/tests/unit/test_x86.c b/tests/unit/test_x86.c index 9d821ec3..8218389c 100644 --- a/tests/unit/test_x86.c +++ b/tests/unit/test_x86.c @@ -742,7 +742,7 @@ static void test_x86_16(void **state) static void test_i386_reg_save(void **state) { uc_engine *uc; - uc_context *saved_regs; + uc_context *saved_context; static const uint64_t address = 0; static const uint8_t code[] = { @@ -766,10 +766,10 @@ static void test_i386_reg_save(void **state) uc_assert_success(uc_emu_start(uc, address, address+1, 0, 0)); // grab a buffer to use for state saving - uc_assert_success(uc_context_alloc(uc, &saved_regs)); + uc_assert_success(uc_context_alloc(uc, &saved_context)); // save the state - uc_assert_success(uc_context_save(uc, saved_regs)); + uc_assert_success(uc_context_save(uc, saved_context)); // step one instruction uc_assert_success(uc_emu_start(uc, address, address+1, 0, 0)); @@ -779,7 +779,7 @@ static void test_i386_reg_save(void **state) assert_int_equal(eax, 3); // restore the state - uc_context_restore(uc, saved_regs); + uc_context_restore(uc, saved_context); // check that eax == 2 uc_assert_success(uc_reg_read(uc, UC_X86_REG_EAX, &eax)); @@ -793,14 +793,14 @@ static void test_i386_reg_save(void **state) assert_int_equal(eax, 3); // restore the state - uc_context_restore(uc, saved_regs); + uc_context_restore(uc, saved_context); // check that eax == 2 uc_assert_success(uc_reg_read(uc, UC_X86_REG_EAX, &eax)); assert_int_equal(eax, 2); // clean up; - uc_context_free(saved_regs); + uc_context_free(saved_context); uc_assert_success(uc_close(uc)); } /******************************************************************************/ diff --git a/uc.c b/uc.c index f0ad92ee..7980e96f 100644 --- a/uc.c +++ b/uc.c @@ -1156,8 +1156,8 @@ uc_err uc_query(uc_engine *uc, uc_query_type type, size_t *result) return UC_ERR_OK; } -size_t cpu_regs_size(uc_arch arch, uc_mode mode); -size_t cpu_regs_size(uc_arch arch, uc_mode mode) +size_t cpu_context_size(uc_arch arch, uc_mode mode); +size_t cpu_context_size(uc_arch arch, uc_mode mode) { // each of these constants is defined by offsetof(CPUXYZState, tlb_table) // tbl_table is the first entry in the CPU_COMMON macro, so it marks the end @@ -1176,13 +1176,14 @@ size_t cpu_regs_size(uc_arch arch, uc_mode mode) UNICORN_EXPORT uc_err uc_context_alloc(uc_engine *uc, uc_context **context) { - size_t size = cpu_regs_size(uc->arch, uc->mode); - *context = malloc(size + sizeof(uc_context)); - if (*context) { - (*context)->size = size; - (*context)->arch = uc->arch; - (*context)->mode = uc->mode; - (*context)->used = false; + struct uc_context **_context = context; + size_t size = cpu_context_size(uc->arch, uc->mode); + *_context = malloc(size + sizeof(uc_context)); + if (*_context) { + (*_context)->size = size; + (*_context)->arch = uc->arch; + (*_context)->mode = uc->mode; + (*_context)->used = false; return UC_ERR_OK; } else { return UC_ERR_NOMEM; @@ -1199,11 +1200,12 @@ uc_err uc_context_free(uc_context *context) UNICORN_EXPORT uc_err uc_context_save(uc_engine *uc, uc_context *context) { - if (context->arch != uc->arch || context->mode != uc->mode) { + struct uc_context *_context = context; + if (_context->arch != uc->arch || _context->mode != uc->mode) { return UC_ERR_ARG; } else { - memcpy(context->data, uc->cpu->env_ptr, context->size); - context->used = true; + memcpy(_context->data, uc->cpu->env_ptr, _context->size); + _context->used = true; return UC_ERR_OK; } } @@ -1211,10 +1213,11 @@ uc_err uc_context_save(uc_engine *uc, uc_context *context) UNICORN_EXPORT uc_err uc_context_restore(uc_engine *uc, uc_context *context) { - if (context->arch != uc->arch || context->mode != uc->mode || !context->used) { + struct uc_context *_context = context; + if (_context->arch != uc->arch || _context->mode != uc->mode || !_context->used) { return UC_ERR_ARG; } else { - memcpy(uc->cpu->env_ptr, context->data, context->size); + memcpy(uc->cpu->env_ptr, _context->data, _context->size); return UC_ERR_OK; } }