Skip to content

Commit d6c14cd

Browse files
committed
Add CMocka unit tests for wasm_runtime functions and improve error handling
1 parent ea063cd commit d6c14cd

File tree

8 files changed

+519
-2
lines changed

8 files changed

+519
-2
lines changed

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "../aot/aot_runtime.h"
3232
#endif
3333

34-
static void
34+
WASM_RUNTIME_API_INTER void
3535
set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
3636
{
3737
if (error_buf != NULL) {
@@ -40,7 +40,7 @@ set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
4040
}
4141
}
4242

43-
static void
43+
WASM_RUNTIME_API_INTER void
4444
set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
4545
{
4646
va_list args;

core/iwasm/interpreter/wasm_runtime.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ extern "C" {
1919

2020
#define EXCEPTION_BUF_LEN 128
2121

22+
/* Test visibility macro for internal functions */
23+
#ifndef WASM_RUNTIME_API_INTER
24+
#ifdef WAMR_BUILD_TEST
25+
#define WASM_RUNTIME_API_INTER
26+
#else
27+
#define WASM_RUNTIME_API_INTER static
28+
#endif
29+
#endif
30+
2231
typedef struct WASMModuleInstance WASMModuleInstance;
2332
typedef struct WASMFunctionInstance WASMFunctionInstance;
2433
typedef struct WASMMemoryInstance WASMMemoryInstance;

tests/unit/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ endif()
5050
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
5151
FetchContent_MakeAvailable(googletest)
5252

53+
# Fetch CMocka for C unit tests
54+
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24")
55+
FetchContent_Declare(
56+
cmocka
57+
URL https://git.cryptomilk.org/projects/cmocka.git/snapshot/cmocka-2.0.1.tar.gz
58+
DOWNLOAD_EXTRACT_TIMESTAMP ON
59+
)
60+
else()
61+
FetchContent_Declare(
62+
cmocka
63+
URL https://git.cryptomilk.org/projects/cmocka.git/snapshot/cmocka-2.0.1.tar.gz
64+
)
65+
endif()
66+
FetchContent_MakeAvailable(cmocka)
67+
5368
include(GoogleTest)
5469
enable_testing()
5570

@@ -65,6 +80,7 @@ add_subdirectory(gc)
6580
add_subdirectory(tid-allocator)
6681
add_subdirectory(unsupported-features)
6782
add_subdirectory(smart-tests)
83+
add_subdirectory(wasm-runtime)
6884

6985
if (NOT WAMR_BUILD_TARGET STREQUAL "X86_32")
7086
add_subdirectory(aot-stack-frame)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright (C) 2019 Intel Corporation. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
cmake_minimum_required(VERSION 3.14)
5+
6+
project(test-wasm-runtime)
7+
8+
# Enable test build flag
9+
add_definitions(-DWAMR_BUILD_TEST=1)
10+
11+
# Test-specific feature configuration
12+
set(WAMR_BUILD_AOT 0)
13+
set(WAMR_BUILD_FAST_INTERP 0)
14+
set(WAMR_BUILD_INTERP 1)
15+
set(WAMR_BUILD_JIT 0)
16+
set(WAMR_BUILD_LIBC_WASI 0)
17+
set(WAMR_BUILD_APP_FRAMEWORK 0)
18+
19+
# Test both multi-module enabled and disabled
20+
if(NOT DEFINED WAMR_BUILD_MULTI_MODULE)
21+
set(WAMR_BUILD_MULTI_MODULE 0)
22+
endif()
23+
24+
include(../unit_common.cmake)
25+
26+
# Test source files
27+
set(TEST_SOURCES
28+
test_runner.c
29+
mocks/wasm_loader_mock.c
30+
${WAMR_RUNTIME_LIB_SOURCE}
31+
)
32+
33+
# Create test executable
34+
add_executable(wasm-runtime-test ${TEST_SOURCES})
35+
36+
target_include_directories(wasm-runtime-test PRIVATE
37+
${CMAKE_CURRENT_LIST_DIR}/mocks
38+
)
39+
40+
# Link dependencies
41+
target_link_libraries(wasm-runtime-test cmocka::cmocka m)
42+
target_link_options(wasm-runtime-test PRIVATE -Wl,--wrap=wasm_loader_load)
43+
44+
# Add to ctest
45+
add_test(NAME wasm-runtime-test COMMAND wasm-runtime-test)
46+
set_tests_properties(wasm-runtime-test PROPERTIES TIMEOUT 30)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <stdarg.h>
2+
#include <stddef.h>
3+
#include <setjmp.h>
4+
#include <cmocka.h>
5+
#include <string.h>
6+
#include "wasm_loader_mock.h"
7+
#include "wasm_export.h"
8+
#include "bh_platform.h"
9+
10+
WASMModule *
11+
__wrap_wasm_loader_load(uint8 *buf, uint32 size,
12+
#if WASM_ENABLE_MULTI_MODULE != 0
13+
bool main_module,
14+
#endif
15+
const LoadArgs *args, char *error_buf, uint32 error_buf_size)
16+
{
17+
/* Check expected parameters */
18+
check_expected_ptr(buf);
19+
check_expected_uint(size);
20+
#if WASM_ENABLE_MULTI_MODULE != 0
21+
check_expected_uint(main_module);
22+
#endif
23+
check_expected_ptr(args);
24+
25+
/* Mock error buffer writing if provided */
26+
bool populate_error = mock_type(bool);
27+
if (populate_error && error_buf) {
28+
const char *error_msg = mock_ptr_type(const char *);
29+
if (error_msg && error_buf_size > 0) {
30+
strncpy(error_buf, error_msg, error_buf_size);
31+
if (error_buf_size > 0) {
32+
error_buf[error_buf_size - 1] = '\0';
33+
}
34+
}
35+
}
36+
37+
/* Return mocked module pointer */
38+
return mock_ptr_type(WASMModule *);
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef WASM_LOADER_MOCK_H
2+
#define WASM_LOADER_MOCK_H
3+
4+
#include <stdint.h>
5+
#include <stdbool.h>
6+
#include "wasm_loader.h"
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
/* Mock function declarations - only compiled when WAMR_BUILD_TEST is defined */
13+
#ifdef WAMR_BUILD_TEST
14+
15+
WASMModule *
16+
wasm_loader_load(uint8 *buf, uint32 size,
17+
#if WASM_ENABLE_MULTI_MODULE != 0
18+
bool main_module,
19+
#endif
20+
const LoadArgs *args, char *error_buf, uint32 error_buf_size);
21+
22+
WASMModule *
23+
wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
24+
uint32 error_buf_size);
25+
26+
#endif /* WAMR_BUILD_TEST */
27+
28+
#ifdef __cplusplus
29+
}
30+
#endif
31+
32+
#endif /* WASM_LOADER_MOCK_H */
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdarg.h>
2+
#include <stddef.h>
3+
#include <setjmp.h>
4+
#include <stdint.h>
5+
#include <cmocka.h>
6+
7+
/* Include test implementations */
8+
#include "wasm_runtime_test.c"
9+
10+
int
11+
main(void)
12+
{
13+
int result = 0;
14+
15+
/* Run all test groups */
16+
// result |= cmocka_run_group_tests(set_error_buf_tests, NULL, NULL);
17+
// result |= cmocka_run_group_tests(set_error_buf_v_tests, NULL, NULL);
18+
19+
/* Run tests with multi-module enabled */
20+
result |= cmocka_run_group_tests(wasm_load_tests_multi_module_enabled, NULL,
21+
NULL);
22+
23+
/* Run tests with multi-module disabled (if compiled) */
24+
#if WASM_ENABLE_MULTI_MODULE == 0
25+
result |= cmocka_run_group_tests(wasm_load_tests_multi_module_disabled,
26+
NULL, NULL);
27+
#endif
28+
29+
return result;
30+
}

0 commit comments

Comments
 (0)