test(mem-alloc): add tests for freeing read-only and freed pointers

This commit is contained in:
liang.he@intel.com 2026-03-30 14:34:25 +08:00
parent 0cb713d3af
commit 5ac967ca2b
3 changed files with 69 additions and 1 deletions

View File

@ -14,7 +14,6 @@ set(WAMR_BUILD_FAST_INTERP 0)
set(WAMR_BUILD_INTERP 1)
set(WAMR_BUILD_JIT 0)
set(WAMR_BUILD_LIBC_WASI 0)
set(WAMR_BUILD_APP_FRAMEWORK 0)
include(../unit_common.cmake)

View File

@ -371,6 +371,72 @@ test_mixed_alloc_many(void **state)
mem_allocator_destroy(allocator);
}
/* Test: free a .ro data */
static void
test_free_ro_data(void **state)
{
mem_allocator_t allocator;
char heap_buf[64 * 1024];
void *ptr;
allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
assert_non_null(allocator);
/* Freeing a .ro data pointer should not crash */
const char *ro_str = "This is a read-only string.";
// FIXME: This case should trigger an exception because the pointer is not
// allocated by the allocator, but currently it just does nothing. We should
// add a check in mem_allocator_free to detect this case and return an
// error. mem_allocator_free(allocator, (void *)ro_str);
mem_allocator_destroy(allocator);
}
/* Test: free a freed pointer */
static void
test_free_freed_pointer(void **state)
{
mem_allocator_t allocator;
char heap_buf[64 * 1024];
void *ptr;
allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
assert_non_null(allocator);
ptr = mem_allocator_malloc(allocator, 64);
assert_non_null(ptr);
mem_allocator_free(allocator, ptr);
/* Freeing the same pointer again should not crash */
mem_allocator_free(allocator, ptr);
mem_allocator_free(allocator, ptr);
mem_allocator_destroy(allocator);
}
/* Test: free a freed pointer from aligned-alloc */
static void
test_free_freed_pointer_aligned(void **state)
{
mem_allocator_t allocator;
char heap_buf[64 * 1024];
void *ptr;
allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
assert_non_null(allocator);
ptr = mem_allocator_malloc_aligned(allocator, 128, 64);
assert_non_null(ptr);
mem_allocator_free(allocator, ptr);
/* Freeing the same pointer again should not crash */
mem_allocator_free(allocator, ptr);
mem_allocator_free(allocator, ptr);
mem_allocator_free(allocator, ptr);
mem_allocator_destroy(allocator);
}
/* Test: wasm_runtime_aligned_alloc with valid inputs in POOL mode */
static void
test_wasm_runtime_aligned_alloc_valid(void **state)

View File

@ -26,6 +26,9 @@ main(void)
cmocka_unit_test(test_mixed_obj_to_hmu),
cmocka_unit_test(test_aligned_alloc_many),
cmocka_unit_test(test_mixed_alloc_many),
cmocka_unit_test(test_free_freed_pointer),
cmocka_unit_test(test_free_freed_pointer_aligned),
cmocka_unit_test(test_free_ro_data),
cmocka_unit_test(test_wasm_runtime_aligned_alloc_valid),
cmocka_unit_test(test_wasm_runtime_aligned_alloc_zero_size),
cmocka_unit_test(test_wasm_runtime_aligned_alloc_zero_alignment),