Skip to content

Commit 9af538d

Browse files
lum1n0usclaude
andcommitted
test(mem-alloc): add alignment validation tests
Verify invalid alignments and size requirements are enforced. Fix alignment validation to reject zero. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent f74175a commit 9af538d

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

core/shared/mem-alloc/ems/ems_alloc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,8 @@ gc_alloc_vo_aligned_internal(void *vheap, gc_size_t size, gc_size_t alignment,
651651
max_alignment = (uint32_t)os_getpagesize();
652652

653653
/* Validation */
654-
if ((alignment & (alignment - 1)) != 0) {
655-
/* Not power of 2 */
654+
if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
655+
/* Zero or not power of 2 */
656656
return NULL;
657657
}
658658

tests/unit/mem-alloc/mem_alloc_test.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,63 @@ test_normal_realloc_works(void **state)
148148
mem_allocator_free(allocator, new_ptr);
149149
mem_allocator_destroy(allocator);
150150
}
151+
152+
/* Test: Invalid alignments (not power of 2 or zero) */
153+
static void
154+
test_aligned_alloc_invalid_not_power_of_2(void **state)
155+
{
156+
mem_allocator_t allocator;
157+
char heap_buf[64 * 1024];
158+
void *ptr;
159+
160+
allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
161+
assert_non_null(allocator);
162+
163+
/* These should all fail (zero or not power of 2) */
164+
int invalid_alignments[] = {0, 3, 5, 7, 9, 15, 17, 100};
165+
for (int i = 0; i < sizeof(invalid_alignments) / sizeof(invalid_alignments[0]); i++) {
166+
ptr = mem_allocator_malloc_aligned(allocator, 128, invalid_alignments[i]);
167+
assert_null(ptr);
168+
}
169+
170+
/* Small powers of 2 should succeed (adjusted to GC_MIN_ALIGNMENT) */
171+
ptr = mem_allocator_malloc_aligned(allocator, 8, 1);
172+
assert_non_null(ptr);
173+
mem_allocator_free(allocator, ptr);
174+
175+
ptr = mem_allocator_malloc_aligned(allocator, 8, 2);
176+
assert_non_null(ptr);
177+
mem_allocator_free(allocator, ptr);
178+
179+
ptr = mem_allocator_malloc_aligned(allocator, 8, 4);
180+
assert_non_null(ptr);
181+
mem_allocator_free(allocator, ptr);
182+
183+
mem_allocator_destroy(allocator);
184+
}
185+
186+
/* Test: Size must be multiple of alignment */
187+
static void
188+
test_aligned_alloc_size_not_multiple(void **state)
189+
{
190+
mem_allocator_t allocator;
191+
char heap_buf[64 * 1024];
192+
void *ptr;
193+
194+
allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
195+
assert_non_null(allocator);
196+
197+
/* Size not multiple of alignment - should fail */
198+
ptr = mem_allocator_malloc_aligned(allocator, 100, 64);
199+
assert_null(ptr);
200+
201+
ptr = mem_allocator_malloc_aligned(allocator, 65, 64);
202+
assert_null(ptr);
203+
204+
/* Size is multiple - should succeed */
205+
ptr = mem_allocator_malloc_aligned(allocator, 128, 64);
206+
assert_non_null(ptr);
207+
mem_allocator_free(allocator, ptr);
208+
209+
mem_allocator_destroy(allocator);
210+
}

tests/unit/mem-alloc/test_runner.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ main(void)
2020
cmocka_unit_test(test_aligned_alloc_valid_alignments),
2121
cmocka_unit_test(test_realloc_rejects_aligned),
2222
cmocka_unit_test(test_normal_realloc_works),
23+
cmocka_unit_test(test_aligned_alloc_invalid_not_power_of_2),
24+
cmocka_unit_test(test_aligned_alloc_size_not_multiple),
2325
};
2426

2527
return cmocka_run_group_tests(tests, NULL, NULL);

0 commit comments

Comments
 (0)