Skip to content

Commit cf86aa4

Browse files
committed
Fix block memory leak
-fsanitize=address report when CTRL+a x exiting: ================================================================= ==297977==ERROR: LeakSanitizer: detected memory leaks Direct leak of 23400 byte(s) in 117 object(s) allocated from: #0 0x761b706fd340 in calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:77 #1 0x633ad9c5e310 in block_translate src/emulate.c:649 #2 0x633ad9c5e310 in block_find_or_translate src/emulate.c:865 #3 0x633ad9c5e310 in rv_step src/emulate.c:1029 #4 0x633ad9c5e310 in rv_run src/riscv.c:498 #5 0x633ad9c5e310 in main src/main.c:279 Direct leak of 3136 byte(s) in 125 object(s) allocated from: #0 0x761b706fd9c7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69 #1 0x633ad9c5fea4 in match_pattern src/emulate.c:767 #2 0x633ad9c5fea4 in block_find_or_translate src/emulate.c:872 #3 0x633ad9c5fea4 in rv_step src/emulate.c:1029 #4 0x633ad9c5fea4 in rv_run src/riscv.c:498 #5 0x633ad9c5fea4 in main src/main.c:279 Register a clean up callback, async_block_clear() to free all the allocated memory fix this when emulator exits.
1 parent b4329fb commit cf86aa4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/riscv.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,35 @@ static void capture_keyboard_input()
300300
}
301301
#endif
302302

303+
/*
304+
*
305+
* atexit() registers void (*)(void) callbacks, so no parameters can be passed.
306+
* Memory must be freed at runtime. block_map_clear() requires a RISC-V instance
307+
* and runs in interpreter mode. Instead of modifying its signature, access the
308+
* global RISC-V instance in main.c with external linkage.
309+
*
310+
*/
311+
extern riscv_t *rv;
312+
static void async_block_clear()
313+
{
314+
if (rv && rv->block_map.size)
315+
#if !RV32_HAS(JIT)
316+
block_map_clear(rv);
317+
#else /* TODO: JIT mode */
318+
else
319+
return;
320+
#endif /* !RV32_HAS(JIT) */
321+
}
322+
303323
riscv_t *rv_create(riscv_user_t rv_attr)
304324
{
305325
assert(rv_attr);
306326

307327
riscv_t *rv = calloc(1, sizeof(riscv_t));
308328
assert(rv);
309329

330+
atexit(async_block_clear);
331+
310332
/* copy over the attr */
311333
rv->data = rv_attr;
312334

0 commit comments

Comments
 (0)