diff --git a/tests/unit/mem-alloc/CMakeLists.txt b/tests/unit/mem-alloc/CMakeLists.txt index 9431c11a0..01c159e71 100644 --- a/tests/unit/mem-alloc/CMakeLists.txt +++ b/tests/unit/mem-alloc/CMakeLists.txt @@ -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) diff --git a/tests/unit/mem-alloc/mem_alloc_test.c b/tests/unit/mem-alloc/mem_alloc_test.c index 878e0e0d4..b6aa65491 100644 --- a/tests/unit/mem-alloc/mem_alloc_test.c +++ b/tests/unit/mem-alloc/mem_alloc_test.c @@ -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) diff --git a/tests/unit/mem-alloc/test_runner.c b/tests/unit/mem-alloc/test_runner.c index d9621f73a..b9513e92d 100644 --- a/tests/unit/mem-alloc/test_runner.c +++ b/tests/unit/mem-alloc/test_runner.c @@ -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),