From 7e6d21d27c8316fbaca3ad8a7f11fbb33cd8f8da Mon Sep 17 00:00:00 2001 From: lazymio Date: Sun, 3 Apr 2022 21:44:09 +0200 Subject: [PATCH] Fix memory leak in code_gen_buffer --- qemu/accel/tcg/translate-all.c | 12 ++++++++---- qemu/include/tcg/tcg.h | 2 ++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/qemu/accel/tcg/translate-all.c b/qemu/accel/tcg/translate-all.c index 0d50cf76..9a6f79fe 100644 --- a/qemu/accel/tcg/translate-all.c +++ b/qemu/accel/tcg/translate-all.c @@ -879,16 +879,18 @@ static inline void *alloc_code_gen_buffer(struct uc_struct *uc) void free_code_gen_buffer(struct uc_struct *uc) { TCGContext *tcg_ctx = uc->tcg_ctx; - if (tcg_ctx->code_gen_buffer) { - VirtualFree(tcg_ctx->code_gen_buffer, 0, MEM_RELEASE); + if (tcg_ctx->initial_buffer) { + VirtualFree(tcg_ctx->initial_buffer, 0, MEM_RELEASE); } } #else 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); + if (tcg_ctx->initial_buffer) { + if (munmap(tcg_ctx->initial_buffer, tcg_ctx->initial_buffer_size)) { + perror("fail code_gen_buffer"); + } } } @@ -953,6 +955,8 @@ static inline void code_gen_alloc(struct uc_struct *uc, size_t tb_size) TCGContext *tcg_ctx = uc->tcg_ctx; tcg_ctx->code_gen_buffer_size = size_code_gen_buffer(tb_size); tcg_ctx->code_gen_buffer = alloc_code_gen_buffer(uc); + tcg_ctx->initial_buffer = tcg_ctx->code_gen_buffer; + tcg_ctx->initial_buffer_size = tcg_ctx->code_gen_buffer_size; if (tcg_ctx->code_gen_buffer == NULL) { fprintf(stderr, "Could not allocate dynamic translator buffer\n"); exit(1); diff --git a/qemu/include/tcg/tcg.h b/qemu/include/tcg/tcg.h index 85eec860..33acbd7c 100644 --- a/qemu/include/tcg/tcg.h +++ b/qemu/include/tcg/tcg.h @@ -642,6 +642,8 @@ struct TCGContext { void *code_gen_prologue; void *code_gen_epilogue; void *code_gen_buffer; + void *initial_buffer; + size_t initial_buffer_size; size_t code_gen_buffer_size; void *code_gen_ptr; void *data_gen_ptr;