Skip to content

Commit 216164d

Browse files
committed
[compilcation] fix(tests): Initialize WAMR runtime in compilation tests to resolve 22 test failures
- Add proper WAMR runtime initialization in IntegrationPerformanceTest::SetUp() - Replace FAIL() with ASSERT_TRUE() for better test practices - Add proper cleanup in TearDown() with wasm_runtime_destroy() - Fix all 20 IntegrationPerformanceTest cases (tests 423-442) - Fix LLVMIntegrationOptimizationTest.test_llvm_target_machine_setup - Fix SIMDAdvancedInstructionsTest.test_aot_emit_exception_handling_compilation Root cause: Tests were attempting to use WAMR functions before runtime initialization, causing "wasm_runtime_malloc failed: memory hasn't been initialized" errors. Signed-off-by: Gong Pu <pu.gong@intel.com>
1 parent 69e3b97 commit 216164d

3 files changed

Lines changed: 62 additions & 15 deletions

File tree

tests/unit/enhanced_unit_test/compilation/test_integration_performance.cc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ class IntegrationPerformanceTest : public testing::Test
3939
protected:
4040
void SetUp() override
4141
{
42+
// Initialize WAMR runtime first
43+
RuntimeInitArgs init_args;
44+
memset(&init_args, 0, sizeof(RuntimeInitArgs));
45+
init_args.mem_alloc_type = Alloc_With_System_Allocator;
46+
47+
ASSERT_TRUE(wasm_runtime_full_init(&init_args)) << "Failed to initialize WAMR runtime";
48+
49+
// Initialize AOT compiler
50+
ASSERT_TRUE(aot_compiler_init()) << "Failed to initialize AOT compiler";
51+
4252
// Initialize test environment
4353
memset(&option, 0, sizeof(AOTCompOption));
4454

@@ -81,9 +91,15 @@ class IntegrationPerformanceTest : public testing::Test
8191
wasm_module = nullptr;
8292
}
8393
if (wasm_file_buf) {
84-
wasm_runtime_free(wasm_file_buf);
94+
BH_FREE(wasm_file_buf);
8595
wasm_file_buf = nullptr;
8696
}
97+
98+
// Destroy AOT compiler
99+
aot_compiler_destroy();
100+
101+
// Destroy WAMR runtime
102+
wasm_runtime_destroy();
87103
}
88104

89105
static void SetUpTestCase()

tests/unit/enhanced_unit_test/compilation/test_llvm_integration_optimization.cc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,34 @@ class LLVMIntegrationOptimizationTest : public testing::Test
114114
// The WASM files are copied to the build directory, so use just the filename
115115
wasm_file_buf = (unsigned char *)bh_read_file_to_buffer(file_name, &wasm_file_size);
116116
if (!wasm_file_buf || wasm_file_size == 0) {
117+
printf("Failed to read WASM file: %s\n", file_name);
117118
return false;
118119
}
119120

120121
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf, sizeof(error_buf));
122+
if (!wasm_module) {
123+
printf("Failed to load WASM module: %s\n", error_buf);
124+
}
121125
return wasm_module != nullptr;
122126
}
123127

124128
bool create_compilation_context(AOTCompOption *test_option)
125129
{
126-
if (!wasm_module) return false;
130+
if (!wasm_module) {
131+
printf("No WASM module loaded\n");
132+
return false;
133+
}
127134

128135
comp_data = aot_create_comp_data(wasm_module, nullptr, false);
129-
if (!comp_data) return false;
136+
if (!comp_data) {
137+
printf("Failed to create compilation data: %s\n", aot_get_last_error());
138+
return false;
139+
}
130140

131141
comp_ctx = aot_create_comp_context(comp_data, test_option);
142+
if (!comp_ctx) {
143+
printf("Failed to create compilation context: %s\n", aot_get_last_error());
144+
}
132145
return comp_ctx != nullptr;
133146
}
134147

@@ -201,6 +214,7 @@ TEST_F(LLVMIntegrationOptimizationTest, test_llvm_target_machine_setup)
201214
AOTCompOption test_option = option;
202215
test_option.target_arch = const_cast<char*>("x86_64");
203216
test_option.target_abi = const_cast<char*>("gnu");
217+
test_option.target_cpu = const_cast<char*>("x86-64");
204218
test_option.cpu_features = const_cast<char*>("+sse2,+sse3,+ssse3,+sse4.1,+sse4.2");
205219

206220
ASSERT_TRUE(create_compilation_context(&test_option));

tests/unit/enhanced_unit_test/compilation/test_simd_advanced_instructions.cc

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ class SIMDAdvancedInstructionsTest : public testing::Test
5555
FAIL() << "Failed to initialize WAMR runtime";
5656
}
5757

58+
// Initialize AOT compiler
59+
if (!aot_compiler_init()) {
60+
FAIL() << "Failed to initialize AOT compiler";
61+
}
62+
5863
// Initialize test environment
5964
memset(&option, 0, sizeof(AOTCompOption));
6065

@@ -95,10 +100,13 @@ class SIMDAdvancedInstructionsTest : public testing::Test
95100
wasm_module = nullptr;
96101
}
97102
if (wasm_file_buf) {
98-
wasm_runtime_free(wasm_file_buf);
103+
BH_FREE(wasm_file_buf);
99104
wasm_file_buf = nullptr;
100105
}
101106

107+
// Destroy AOT compiler
108+
aot_compiler_destroy();
109+
102110
// Destroy WAMR runtime
103111
wasm_runtime_destroy();
104112
}
@@ -119,15 +127,12 @@ class SIMDAdvancedInstructionsTest : public testing::Test
119127

120128
bool LoadWasmModule()
121129
{
122-
wasm_file_buf = (unsigned char *)bh_read_file_to_buffer(WASM_FILE, &wasm_file_size);
130+
// Use just the filename since files are copied to build directory
131+
const char* wasm_file = "simd_test.wasm";
132+
wasm_file_buf = (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
123133
if (!wasm_file_buf) {
124-
// Try alternative paths if primary path fails
125-
std::string alt_path = "./simd_test.wasm";
126-
wasm_file_buf = (unsigned char *)bh_read_file_to_buffer(alt_path.c_str(), &wasm_file_size);
127-
if (!wasm_file_buf) {
128-
printf("Failed to load WASM file from both %s and %s\n", WASM_FILE, alt_path.c_str());
129-
return false;
130-
}
134+
printf("Failed to load WASM file: %s\n", wasm_file);
135+
return false;
131136
}
132137

133138
char error_buf[128];
@@ -141,16 +146,20 @@ class SIMDAdvancedInstructionsTest : public testing::Test
141146
bool InitializeCompilationContext()
142147
{
143148
if (!wasm_module) {
149+
printf("No WASM module loaded\n");
144150
return false;
145151
}
146152

147-
char error_buf[128];
148153
comp_data = aot_create_comp_data(wasm_module, nullptr, false);
149154
if (!comp_data) {
155+
printf("Failed to create compilation data: %s\n", aot_get_last_error());
150156
return false;
151157
}
152158

153159
comp_ctx = aot_create_comp_context(comp_data, &option);
160+
if (!comp_ctx) {
161+
printf("Failed to create compilation context: %s\n", aot_get_last_error());
162+
}
154163
return comp_ctx != nullptr;
155164
}
156165

@@ -443,12 +452,20 @@ TEST_F(SIMDAdvancedInstructionsTest, test_aot_emit_conversion_comprehensive_type
443452

444453
TEST_F(SIMDAdvancedInstructionsTest, test_aot_emit_exception_handling_compilation)
445454
{
446-
// Test exception handling compilation
455+
// Test exception handling compilation - use main.wasm which doesn't have SIMD
447456
option.enable_simd = false; // Test without SIMD for exception focus
448457
option.enable_aux_stack_check = true;
449458
option.bounds_checks = 2;
450459

451-
ASSERT_TRUE(LoadWasmModule()) << "Failed to load SIMD test module";
460+
// Load a non-SIMD WASM file for exception handling test
461+
const char* wasm_file = "main.wasm";
462+
wasm_file_buf = (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
463+
ASSERT_TRUE(wasm_file_buf != nullptr) << "Failed to load main.wasm";
464+
465+
char error_buf[128];
466+
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf, sizeof(error_buf));
467+
ASSERT_TRUE(wasm_module != nullptr) << "Failed to load WASM module: " << error_buf;
468+
452469
ASSERT_TRUE(InitializeCompilationContext()) << "Failed to initialize compilation context";
453470

454471
bool result = aot_compile_wasm(comp_ctx);

0 commit comments

Comments
 (0)