Fix memory leak in code_gen_buffer

This commit is contained in:
lazymio 2022-04-03 21:44:09 +02:00
parent ca81d46ad5
commit 7e6d21d27c
No known key found for this signature in database
GPG Key ID: DFF27E34A47CB873
2 changed files with 10 additions and 4 deletions

View File

@ -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);

View File

@ -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;