Skip to content

Commit

Permalink
Enable memory leak check (bytecodealliance#1429)
Browse files Browse the repository at this point in the history
Report the memory leak info when building iwasm with
`cmake .. -DWAMR_BUILD_GC_VERIFY=1`
  • Loading branch information
wenyongh authored Sep 1, 2022
1 parent 1877480 commit d095876
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 16 deletions.
20 changes: 20 additions & 0 deletions core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,26 @@
#define BH_ENABLE_GC_VERIFY 0
#endif

/* Enable global heap pool if heap verification is enabled */
#if BH_ENABLE_GC_VERIFY != 0
#define WASM_ENABLE_GLOBAL_HEAP_POOL 1
#endif

/* Global heap pool */
#ifndef WASM_ENABLE_GLOBAL_HEAP_POOL
#define WASM_ENABLE_GLOBAL_HEAP_POOL 0
#endif

/* Global heap pool size in bytes */
#ifndef WASM_GLOBAL_HEAP_SIZE
#if WASM_ENABLE_SPEC_TEST != 0
/* Spec test requires more heap pool size */
#define WASM_GLOBAL_HEAP_SIZE (300 * 1024 * 1024)
#else
#define WASM_GLOBAL_HEAP_SIZE (10 * 1024 * 1024)
#endif
#endif

/* Max app number of all modules */
#define MAX_APP_INSTALLATIONS 3

Expand Down
13 changes: 11 additions & 2 deletions core/iwasm/common/wasm_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,17 @@ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
void
wasm_runtime_memory_destroy()
{
if (memory_mode == MEMORY_MODE_POOL)
mem_allocator_destroy(pool_allocator);
if (memory_mode == MEMORY_MODE_POOL) {
#if BH_ENABLE_GC_VERIFY == 0
(void)mem_allocator_destroy(pool_allocator);
#else
int ret = mem_allocator_destroy(pool_allocator);
if (ret != 0) {
/* Memory leak detected */
exit(-1);
}
#endif
}
memory_mode = MEMORY_MODE_UNKNOWN;
}

Expand Down
11 changes: 6 additions & 5 deletions core/shared/mem-alloc/ems/ems_kfc.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,24 @@ int
gc_destroy_with_pool(gc_handle_t handle)
{
gc_heap_t *heap = (gc_heap_t *)handle;
int ret = GC_SUCCESS;

#if BH_ENABLE_GC_VERIFY != 0
hmu_t *cur = (hmu_t *)heap->base_addr;
hmu_t *end = (hmu_t *)((char *)heap->base_addr + heap->current_size);

if (!heap->is_heap_corrupted
&& (hmu_t *)((char *)cur + hmu_get_size(cur)) != end) {
os_printf("Memory leak detected:\n");
gci_dump(heap);
#if WASM_ENABLE_SPEC_TEST != 0
while (1) {
}
#endif
ret = GC_ERROR;
}
#endif

os_mutex_destroy(&heap->lock);
memset(heap->base_addr, 0, heap->current_size);
memset(heap, 0, sizeof(gc_heap_t));
return GC_SUCCESS;
return ret;
}

uint32
Expand Down
4 changes: 2 additions & 2 deletions core/shared/mem-alloc/mem_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ mem_allocator_create_with_struct_and_pool(void *struct_buf,
pool_buf, pool_buf_size);
}

void
int
mem_allocator_destroy(mem_allocator_t allocator)
{
gc_destroy_with_pool((gc_handle_t)allocator);
return gc_destroy_with_pool((gc_handle_t)allocator);
}

uint32
Expand Down
4 changes: 4 additions & 0 deletions core/shared/mem-alloc/mem_alloc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ set (MEM_ALLOC_DIR ${CMAKE_CURRENT_LIST_DIR})

include_directories(${MEM_ALLOC_DIR})

if (WAMR_BUILD_GC_VERIFY EQUAL 1)
add_definitions (-DBH_ENABLE_GC_VERIFY=1)
endif ()

file (GLOB_RECURSE source_all
${MEM_ALLOC_DIR}/ems/*.c
${MEM_ALLOC_DIR}/tlsf/*.c
Expand Down
2 changes: 1 addition & 1 deletion core/shared/mem-alloc/mem_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mem_allocator_create_with_struct_and_pool(void *struct_buf,
void *pool_buf,
uint32_t pool_buf_size);

void
int
mem_allocator_destroy(mem_allocator_t allocator);

uint32
Expand Down
2 changes: 1 addition & 1 deletion product-mini/platforms/nuttx/wamr.mk
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ endif

ifeq ($(CONFIG_INTERPRETERS_WAMR_GLOBAL_HEAP_POOL),y)
CFLAGS += -DWASM_ENABLE_GLOBAL_HEAP_POOL=1
CFLAGS += -DWASM_GLOBAL_HEAP_SIZE=$(CONFIG_INTERPRETERS_WAMR_GLOBAL_HEAP_POOL_SIZE)
CFLAGS += -DWASM_GLOBAL_HEAP_SIZE="$(CONFIG_INTERPRETERS_WAMR_GLOBAL_HEAP_POOL_SIZE) * 1024"
else
CFLAGS += -DWASM_ENABLE_GLOBAL_HEAP_POOL=0
endif
Expand Down
6 changes: 1 addition & 5 deletions product-mini/platforms/posix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,7 @@ moudle_destroyer(uint8 *buffer, uint32 size)
#endif /* WASM_ENABLE_MULTI_MODULE */

#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
#ifdef __NuttX__
static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE * BH_KB] = { 0 };
#else
static char global_heap_buf[10 * 1024 * 1024] = { 0 };
#endif
static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
#endif

int
Expand Down

0 comments on commit d095876

Please sign in to comment.