From b9a10152f149871a9e1ea8116fe0833df7c23291 Mon Sep 17 00:00:00 2001 From: Hoang-Vu Dang Date: Mon, 11 Jul 2016 10:13:13 -0500 Subject: [PATCH] memleak: code_gen_buffer using g_free for non-linux --- qemu/aarch64.h | 1 + qemu/arm.h | 1 + qemu/header_gen.py | 1 + qemu/m68k.h | 1 + qemu/mips.h | 1 + qemu/mips64.h | 1 + qemu/mips64el.h | 1 + qemu/mipsel.h | 1 + qemu/sparc.h | 1 + qemu/sparc64.h | 1 + qemu/translate-all.c | 22 ++++++++++++++++++++++ qemu/unicorn_common.h | 5 ++--- qemu/x86_64.h | 1 + 13 files changed, 35 insertions(+), 3 deletions(-) diff --git a/qemu/aarch64.h b/qemu/aarch64.h index 09352a6a..54be2c19 100644 --- a/qemu/aarch64.h +++ b/qemu/aarch64.h @@ -14,6 +14,7 @@ #define memory_map_ptr memory_map_ptr_aarch64 #define memory_unmap memory_unmap_aarch64 #define memory_free memory_free_aarch64 +#define free_code_gen_buffer free_code_gen_buffer_aarch64 #define helper_raise_exception helper_raise_exception_aarch64 #define tcg_enabled tcg_enabled_aarch64 #define tcg_exec_init tcg_exec_init_aarch64 diff --git a/qemu/arm.h b/qemu/arm.h index 6b955375..87d1b19e 100644 --- a/qemu/arm.h +++ b/qemu/arm.h @@ -14,6 +14,7 @@ #define memory_map_ptr memory_map_ptr_arm #define memory_unmap memory_unmap_arm #define memory_free memory_free_arm +#define free_code_gen_buffer free_code_gen_buffer_arm #define helper_raise_exception helper_raise_exception_arm #define tcg_enabled tcg_enabled_arm #define tcg_exec_init tcg_exec_init_arm diff --git a/qemu/header_gen.py b/qemu/header_gen.py index b98b0acd..983fdecb 100644 --- a/qemu/header_gen.py +++ b/qemu/header_gen.py @@ -20,6 +20,7 @@ symbols = ( 'memory_map_ptr', 'memory_unmap', 'memory_free', + 'free_code_gen_buffer', 'helper_raise_exception', 'tcg_enabled', 'tcg_exec_init', diff --git a/qemu/m68k.h b/qemu/m68k.h index bb37dedb..755e85ee 100644 --- a/qemu/m68k.h +++ b/qemu/m68k.h @@ -14,6 +14,7 @@ #define memory_map_ptr memory_map_ptr_m68k #define memory_unmap memory_unmap_m68k #define memory_free memory_free_m68k +#define free_code_gen_buffer free_code_gen_buffer_m68k #define helper_raise_exception helper_raise_exception_m68k #define tcg_enabled tcg_enabled_m68k #define tcg_exec_init tcg_exec_init_m68k diff --git a/qemu/mips.h b/qemu/mips.h index f88ee66a..fd2ad22f 100644 --- a/qemu/mips.h +++ b/qemu/mips.h @@ -14,6 +14,7 @@ #define memory_map_ptr memory_map_ptr_mips #define memory_unmap memory_unmap_mips #define memory_free memory_free_mips +#define free_code_gen_buffer free_code_gen_buffer_mips #define helper_raise_exception helper_raise_exception_mips #define tcg_enabled tcg_enabled_mips #define tcg_exec_init tcg_exec_init_mips diff --git a/qemu/mips64.h b/qemu/mips64.h index 2422ea67..4c47a987 100644 --- a/qemu/mips64.h +++ b/qemu/mips64.h @@ -14,6 +14,7 @@ #define memory_map_ptr memory_map_ptr_mips64 #define memory_unmap memory_unmap_mips64 #define memory_free memory_free_mips64 +#define free_code_gen_buffer free_code_gen_buffer_mips64 #define helper_raise_exception helper_raise_exception_mips64 #define tcg_enabled tcg_enabled_mips64 #define tcg_exec_init tcg_exec_init_mips64 diff --git a/qemu/mips64el.h b/qemu/mips64el.h index 9ca8d02b..71a19f6a 100644 --- a/qemu/mips64el.h +++ b/qemu/mips64el.h @@ -14,6 +14,7 @@ #define memory_map_ptr memory_map_ptr_mips64el #define memory_unmap memory_unmap_mips64el #define memory_free memory_free_mips64el +#define free_code_gen_buffer free_code_gen_buffer_mips64el #define helper_raise_exception helper_raise_exception_mips64el #define tcg_enabled tcg_enabled_mips64el #define tcg_exec_init tcg_exec_init_mips64el diff --git a/qemu/mipsel.h b/qemu/mipsel.h index d097143b..7ce0bd75 100644 --- a/qemu/mipsel.h +++ b/qemu/mipsel.h @@ -14,6 +14,7 @@ #define memory_map_ptr memory_map_ptr_mipsel #define memory_unmap memory_unmap_mipsel #define memory_free memory_free_mipsel +#define free_code_gen_buffer free_code_gen_buffer_mipsel #define helper_raise_exception helper_raise_exception_mipsel #define tcg_enabled tcg_enabled_mipsel #define tcg_exec_init tcg_exec_init_mipsel diff --git a/qemu/sparc.h b/qemu/sparc.h index 32f5512d..5139c3ea 100644 --- a/qemu/sparc.h +++ b/qemu/sparc.h @@ -14,6 +14,7 @@ #define memory_map_ptr memory_map_ptr_sparc #define memory_unmap memory_unmap_sparc #define memory_free memory_free_sparc +#define free_code_gen_buffer free_code_gen_buffer_sparc #define helper_raise_exception helper_raise_exception_sparc #define tcg_enabled tcg_enabled_sparc #define tcg_exec_init tcg_exec_init_sparc diff --git a/qemu/sparc64.h b/qemu/sparc64.h index d42eb1fd..a95aaab6 100644 --- a/qemu/sparc64.h +++ b/qemu/sparc64.h @@ -14,6 +14,7 @@ #define memory_map_ptr memory_map_ptr_sparc64 #define memory_unmap memory_unmap_sparc64 #define memory_free memory_free_sparc64 +#define free_code_gen_buffer free_code_gen_buffer_sparc64 #define helper_raise_exception helper_raise_exception_sparc64 #define tcg_enabled tcg_enabled_sparc64 #define tcg_exec_init tcg_exec_init_sparc64 diff --git a/qemu/translate-all.c b/qemu/translate-all.c index 31d4f47f..a476c0f7 100644 --- a/qemu/translate-all.c +++ b/qemu/translate-all.c @@ -126,6 +126,9 @@ static void tb_link_page(struct uc_struct *uc, TranslationBlock *tb, tb_page_addr_t phys_pc, tb_page_addr_t phys_page2); static TranslationBlock *tb_find_pc(struct uc_struct *uc, uintptr_t tc_ptr); +// Unicorn: for cleaning up memory later. +void free_code_gen_buffer(struct uc_struct *uc); + static void cpu_gen_init(struct uc_struct *uc) { uc->tcg_ctx = g_malloc(sizeof(TCGContext)); @@ -563,6 +566,11 @@ static inline void *split_cross_256mb(struct uc_struct *uc, void *buf1, size_t s static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE] __attribute__((aligned(CODE_GEN_ALIGN))); +void free_code_gen_buffer(struct uc_struct *uc) +{ + // Do nothing, we use a static buffer. +} + static inline void *alloc_code_gen_buffer(struct uc_struct *uc) { TCGContext *tcg_ctx = uc->tcg_ctx; @@ -576,6 +584,13 @@ static inline void *alloc_code_gen_buffer(struct uc_struct *uc) return buf; } #elif defined(USE_MMAP) +void free_code_gen_buffer(struct uc_struct *uc) +{ + TCGContext *tcg_ctx = uc->tcg_ctx; + if (tcg_ctx->code_gen_buffer) + munmap(tcg_ctx->code_gen_buffer, tcg_ctx->code_gen_buffer_size); +} + static inline void *alloc_code_gen_buffer(struct uc_struct *uc) { int flags = MAP_PRIVATE | MAP_ANONYMOUS; @@ -648,6 +663,13 @@ static inline void *alloc_code_gen_buffer(struct uc_struct *uc) return buf; } #else +void free_code_gen_buffer(struct uc_struct *uc) +{ + TCGContext *tcg_ctx = uc->tcg_ctx; + if (tcg_ctx->code_gen_buffer) + g_free(tcg_ctx->code_gen_buffer); +} + static inline void *alloc_code_gen_buffer(struct uc_struct *uc) { TCGContext *tcg_ctx = uc->tcg_ctx; diff --git a/qemu/unicorn_common.h b/qemu/unicorn_common.h index 61f47fec..e35d129e 100644 --- a/qemu/unicorn_common.h +++ b/qemu/unicorn_common.h @@ -21,6 +21,7 @@ static inline bool cpu_physical_mem_write(AddressSpace *as, hwaddr addr, } void tb_cleanup(struct uc_struct *uc); +void free_code_gen_buffer(struct uc_struct *uc); /** Freeing common resources */ static void release_common(void *t) @@ -35,9 +36,6 @@ static void release_common(void *t) g_free(def->args_ct); g_free(def->sorted_args); g_free(s->tcg_op_defs); - if (s->code_gen_buffer) { - munmap(s->code_gen_buffer, s->code_gen_buffer_size); - } TCGPool *po, *to; for (po = s->pool_first; po; po = to) { @@ -53,6 +51,7 @@ static void release_common(void *t) address_space_destroy(&(s->uc->as)); memory_free(s->uc); tb_cleanup(s->uc); + free_code_gen_buffer(s->uc); #if TCG_TARGET_REG_BITS == 32 for(i = 0; i < s->nb_globals; i++) { diff --git a/qemu/x86_64.h b/qemu/x86_64.h index df78a28a..75a37221 100644 --- a/qemu/x86_64.h +++ b/qemu/x86_64.h @@ -14,6 +14,7 @@ #define memory_map_ptr memory_map_ptr_x86_64 #define memory_unmap memory_unmap_x86_64 #define memory_free memory_free_x86_64 +#define free_code_gen_buffer free_code_gen_buffer_x86_64 #define helper_raise_exception helper_raise_exception_x86_64 #define tcg_enabled tcg_enabled_x86_64 #define tcg_exec_init tcg_exec_init_x86_64