Skip to content

Commit 5ac967c

Browse files
committed
test(mem-alloc): add tests for freeing read-only and freed pointers
1 parent 0cb713d commit 5ac967c

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

tests/unit/mem-alloc/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ set(WAMR_BUILD_FAST_INTERP 0)
1414
set(WAMR_BUILD_INTERP 1)
1515
set(WAMR_BUILD_JIT 0)
1616
set(WAMR_BUILD_LIBC_WASI 0)
17-
set(WAMR_BUILD_APP_FRAMEWORK 0)
1817

1918
include(../unit_common.cmake)
2019

tests/unit/mem-alloc/mem_alloc_test.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,72 @@ test_mixed_alloc_many(void **state)
371371
mem_allocator_destroy(allocator);
372372
}
373373

374+
/* Test: free a .ro data */
375+
static void
376+
test_free_ro_data(void **state)
377+
{
378+
379+
mem_allocator_t allocator;
380+
char heap_buf[64 * 1024];
381+
void *ptr;
382+
383+
allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
384+
assert_non_null(allocator);
385+
386+
/* Freeing a .ro data pointer should not crash */
387+
const char *ro_str = "This is a read-only string.";
388+
// FIXME: This case should trigger an exception because the pointer is not
389+
// allocated by the allocator, but currently it just does nothing. We should
390+
// add a check in mem_allocator_free to detect this case and return an
391+
// error. mem_allocator_free(allocator, (void *)ro_str);
392+
mem_allocator_destroy(allocator);
393+
}
394+
395+
/* Test: free a freed pointer */
396+
static void
397+
test_free_freed_pointer(void **state)
398+
{
399+
mem_allocator_t allocator;
400+
char heap_buf[64 * 1024];
401+
void *ptr;
402+
403+
allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
404+
assert_non_null(allocator);
405+
406+
ptr = mem_allocator_malloc(allocator, 64);
407+
assert_non_null(ptr);
408+
409+
mem_allocator_free(allocator, ptr);
410+
/* Freeing the same pointer again should not crash */
411+
mem_allocator_free(allocator, ptr);
412+
mem_allocator_free(allocator, ptr);
413+
414+
mem_allocator_destroy(allocator);
415+
}
416+
417+
/* Test: free a freed pointer from aligned-alloc */
418+
static void
419+
test_free_freed_pointer_aligned(void **state)
420+
{
421+
mem_allocator_t allocator;
422+
char heap_buf[64 * 1024];
423+
void *ptr;
424+
425+
allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
426+
assert_non_null(allocator);
427+
428+
ptr = mem_allocator_malloc_aligned(allocator, 128, 64);
429+
assert_non_null(ptr);
430+
431+
mem_allocator_free(allocator, ptr);
432+
/* Freeing the same pointer again should not crash */
433+
mem_allocator_free(allocator, ptr);
434+
mem_allocator_free(allocator, ptr);
435+
mem_allocator_free(allocator, ptr);
436+
437+
mem_allocator_destroy(allocator);
438+
}
439+
374440
/* Test: wasm_runtime_aligned_alloc with valid inputs in POOL mode */
375441
static void
376442
test_wasm_runtime_aligned_alloc_valid(void **state)

tests/unit/mem-alloc/test_runner.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ main(void)
2626
cmocka_unit_test(test_mixed_obj_to_hmu),
2727
cmocka_unit_test(test_aligned_alloc_many),
2828
cmocka_unit_test(test_mixed_alloc_many),
29+
cmocka_unit_test(test_free_freed_pointer),
30+
cmocka_unit_test(test_free_freed_pointer_aligned),
31+
cmocka_unit_test(test_free_ro_data),
2932
cmocka_unit_test(test_wasm_runtime_aligned_alloc_valid),
3033
cmocka_unit_test(test_wasm_runtime_aligned_alloc_zero_size),
3134
cmocka_unit_test(test_wasm_runtime_aligned_alloc_zero_alignment),

0 commit comments

Comments
 (0)