|
16 | 16 |
|
17 | 17 | #include "mem_alloc.h" |
18 | 18 | #include "ems_gc_internal.h" |
| 19 | +#include "wasm_export.h" |
19 | 20 |
|
20 | 21 | /* Test helper: Check if pointer is aligned */ |
21 | 22 | static inline bool |
@@ -364,3 +365,154 @@ test_mixed_alloc_many(void **state) |
364 | 365 |
|
365 | 366 | mem_allocator_destroy(allocator); |
366 | 367 | } |
| 368 | + |
| 369 | +/* Test: wasm_runtime_aligned_alloc with valid inputs in POOL mode */ |
| 370 | +static void |
| 371 | +test_wasm_runtime_aligned_alloc_valid(void **state) |
| 372 | +{ |
| 373 | + RuntimeInitArgs init_args; |
| 374 | + void *ptr; |
| 375 | + |
| 376 | + memset(&init_args, 0, sizeof(RuntimeInitArgs)); |
| 377 | + init_args.mem_alloc_type = Alloc_With_Pool; |
| 378 | + init_args.mem_alloc_option.pool.heap_buf = malloc(256 * 1024); |
| 379 | + init_args.mem_alloc_option.pool.heap_size = 256 * 1024; |
| 380 | + |
| 381 | + assert_true(wasm_runtime_init()); |
| 382 | + assert_true(wasm_runtime_full_init(&init_args)); |
| 383 | + |
| 384 | + /* Test valid aligned allocation */ |
| 385 | + ptr = wasm_runtime_aligned_alloc(128, 64); |
| 386 | + assert_non_null(ptr); |
| 387 | + assert_true(is_aligned(ptr, 64)); |
| 388 | + |
| 389 | + /* Free should work */ |
| 390 | + wasm_runtime_free(ptr); |
| 391 | + |
| 392 | + wasm_runtime_destroy(); |
| 393 | + free(init_args.mem_alloc_option.pool.heap_buf); |
| 394 | +} |
| 395 | + |
| 396 | +/* Test: wasm_runtime_aligned_alloc with zero size */ |
| 397 | +static void |
| 398 | +test_wasm_runtime_aligned_alloc_zero_size(void **state) |
| 399 | +{ |
| 400 | + RuntimeInitArgs init_args; |
| 401 | + void *ptr; |
| 402 | + |
| 403 | + memset(&init_args, 0, sizeof(RuntimeInitArgs)); |
| 404 | + init_args.mem_alloc_type = Alloc_With_Pool; |
| 405 | + init_args.mem_alloc_option.pool.heap_buf = malloc(256 * 1024); |
| 406 | + init_args.mem_alloc_option.pool.heap_size = 256 * 1024; |
| 407 | + |
| 408 | + assert_true(wasm_runtime_init()); |
| 409 | + assert_true(wasm_runtime_full_init(&init_args)); |
| 410 | + |
| 411 | + /* Zero size should return NULL */ |
| 412 | + ptr = wasm_runtime_aligned_alloc(0, 64); |
| 413 | + assert_null(ptr); |
| 414 | + |
| 415 | + wasm_runtime_destroy(); |
| 416 | + free(init_args.mem_alloc_option.pool.heap_buf); |
| 417 | +} |
| 418 | + |
| 419 | +/* Test: wasm_runtime_aligned_alloc with zero alignment */ |
| 420 | +static void |
| 421 | +test_wasm_runtime_aligned_alloc_zero_alignment(void **state) |
| 422 | +{ |
| 423 | + RuntimeInitArgs init_args; |
| 424 | + void *ptr; |
| 425 | + |
| 426 | + memset(&init_args, 0, sizeof(RuntimeInitArgs)); |
| 427 | + init_args.mem_alloc_type = Alloc_With_Pool; |
| 428 | + init_args.mem_alloc_option.pool.heap_buf = malloc(256 * 1024); |
| 429 | + init_args.mem_alloc_option.pool.heap_size = 256 * 1024; |
| 430 | + |
| 431 | + assert_true(wasm_runtime_init()); |
| 432 | + assert_true(wasm_runtime_full_init(&init_args)); |
| 433 | + |
| 434 | + /* Zero alignment should return NULL */ |
| 435 | + ptr = wasm_runtime_aligned_alloc(128, 0); |
| 436 | + assert_null(ptr); |
| 437 | + |
| 438 | + wasm_runtime_destroy(); |
| 439 | + free(init_args.mem_alloc_option.pool.heap_buf); |
| 440 | +} |
| 441 | + |
| 442 | +/* Test: wasm_runtime_aligned_alloc in SYSTEM_ALLOCATOR mode returns NULL */ |
| 443 | +static void |
| 444 | +test_wasm_runtime_aligned_alloc_system_mode(void **state) |
| 445 | +{ |
| 446 | + RuntimeInitArgs init_args; |
| 447 | + void *ptr; |
| 448 | + |
| 449 | + memset(&init_args, 0, sizeof(RuntimeInitArgs)); |
| 450 | + init_args.mem_alloc_type = Alloc_With_System_Allocator; |
| 451 | + |
| 452 | + assert_true(wasm_runtime_init()); |
| 453 | + assert_true(wasm_runtime_full_init(&init_args)); |
| 454 | + |
| 455 | + /* Should return NULL in non-POOL mode */ |
| 456 | + ptr = wasm_runtime_aligned_alloc(128, 64); |
| 457 | + assert_null(ptr); |
| 458 | + |
| 459 | + wasm_runtime_destroy(); |
| 460 | +} |
| 461 | + |
| 462 | +/* Test: wasm_runtime_realloc rejects aligned allocations */ |
| 463 | +static void |
| 464 | +test_wasm_runtime_realloc_rejects_aligned(void **state) |
| 465 | +{ |
| 466 | + RuntimeInitArgs init_args; |
| 467 | + void *ptr, *new_ptr; |
| 468 | + |
| 469 | + memset(&init_args, 0, sizeof(RuntimeInitArgs)); |
| 470 | + init_args.mem_alloc_type = Alloc_With_Pool; |
| 471 | + init_args.mem_alloc_option.pool.heap_buf = malloc(256 * 1024); |
| 472 | + init_args.mem_alloc_option.pool.heap_size = 256 * 1024; |
| 473 | + |
| 474 | + assert_true(wasm_runtime_init()); |
| 475 | + assert_true(wasm_runtime_full_init(&init_args)); |
| 476 | + |
| 477 | + /* Allocate with alignment */ |
| 478 | + ptr = wasm_runtime_aligned_alloc(128, 64); |
| 479 | + assert_non_null(ptr); |
| 480 | + |
| 481 | + /* Realloc should return NULL */ |
| 482 | + new_ptr = wasm_runtime_realloc(ptr, 256); |
| 483 | + assert_null(new_ptr); |
| 484 | + |
| 485 | + /* Original pointer still valid */ |
| 486 | + wasm_runtime_free(ptr); |
| 487 | + |
| 488 | + wasm_runtime_destroy(); |
| 489 | + free(init_args.mem_alloc_option.pool.heap_buf); |
| 490 | +} |
| 491 | + |
| 492 | +/* Test: wasm_runtime_aligned_alloc with various alignments */ |
| 493 | +static void |
| 494 | +test_wasm_runtime_aligned_alloc_multiple_alignments(void **state) |
| 495 | +{ |
| 496 | + RuntimeInitArgs init_args; |
| 497 | + int alignments[] = {8, 16, 32, 64, 128, 256}; |
| 498 | + int num_alignments = sizeof(alignments) / sizeof(alignments[0]); |
| 499 | + |
| 500 | + memset(&init_args, 0, sizeof(RuntimeInitArgs)); |
| 501 | + init_args.mem_alloc_type = Alloc_With_Pool; |
| 502 | + init_args.mem_alloc_option.pool.heap_buf = malloc(512 * 1024); |
| 503 | + init_args.mem_alloc_option.pool.heap_size = 512 * 1024; |
| 504 | + |
| 505 | + assert_true(wasm_runtime_init()); |
| 506 | + assert_true(wasm_runtime_full_init(&init_args)); |
| 507 | + |
| 508 | + for (int i = 0; i < num_alignments; i++) { |
| 509 | + int align = alignments[i]; |
| 510 | + void *ptr = wasm_runtime_aligned_alloc(align * 2, align); |
| 511 | + assert_non_null(ptr); |
| 512 | + assert_true(is_aligned(ptr, align)); |
| 513 | + wasm_runtime_free(ptr); |
| 514 | + } |
| 515 | + |
| 516 | + wasm_runtime_destroy(); |
| 517 | + free(init_args.mem_alloc_option.pool.heap_buf); |
| 518 | +} |
0 commit comments