Skip to content

Commit c6712b4

Browse files
authored
add a validator for aot module (#3995)
- Add AOT module validation to ensure memory constraints are met - Enable AOT validator in build configuration and update related source files
1 parent b2c7cb2 commit c6712b4

File tree

8 files changed

+92
-6
lines changed

8 files changed

+92
-6
lines changed

build-scripts/config_common.cmake

+4
Original file line numberDiff line numberDiff line change
@@ -610,4 +610,8 @@ if (WAMR_BUILD_SHRUNK_MEMORY EQUAL 1)
610610
else ()
611611
add_definitions (-DWASM_ENABLE_SHRUNK_MEMORY=0)
612612
message (" Shrunk memory disabled")
613+
endif()
614+
if (WAMR_BUILD_AOT_VALIDATOR EQUAL 1)
615+
message (" AOT validator enabled")
616+
add_definitions (-DWASM_ENABLE_AOT_VALIDATOR=1)
613617
endif ()

core/config.h

+4
Original file line numberDiff line numberDiff line change
@@ -702,4 +702,8 @@
702702
#define WASM_ENABLE_SHRUNK_MEMORY 1
703703
#endif
704704

705+
#ifndef WASM_ENABLE_AOT_VALIDATOR
706+
#define WASM_ENABLE_AOT_VALIDATOR 0
707+
#endif
708+
705709
#endif /* end of _CONFIG_H_ */

core/iwasm/aot/aot_loader.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include "../common/wasm_native.h"
1111
#include "../common/wasm_loader_common.h"
1212
#include "../compilation/aot.h"
13+
#if WASM_ENABLE_AOT_VALIDATOR != 0
14+
#include "aot_validator.h"
15+
#endif
1316

1417
#if WASM_ENABLE_DEBUG_AOT != 0
1518
#include "debug/elf_parser.h"
@@ -1106,9 +1109,6 @@ load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
11061109
const uint8 *buf = *p_buf;
11071110

11081111
read_uint32(buf, buf_end, module->import_memory_count);
1109-
/* We don't support import_memory_count > 0 currently */
1110-
if (module->import_memory_count > 0)
1111-
return false;
11121112

11131113
read_uint32(buf, buf_end, module->memory_count);
11141114
total_size = sizeof(AOTMemory) * (uint64)module->memory_count;
@@ -4403,6 +4403,13 @@ aot_load_from_aot_file(const uint8 *buf, uint32 size, const LoadArgs *args,
44034403
os_thread_jit_write_protect_np(true); /* Make memory executable */
44044404
os_icache_flush(module->code, module->code_size);
44054405

4406+
#if WASM_ENABLE_AOT_VALIDATOR != 0
4407+
if (!aot_module_validate(module, error_buf, error_buf_size)) {
4408+
aot_unload(module);
4409+
return NULL;
4410+
}
4411+
#endif /* WASM_ENABLE_AOT_VALIDATOR != 0 */
4412+
44064413
LOG_VERBOSE("Load module success.\n");
44074414
return module;
44084415
}

core/iwasm/aot/aot_perf_map.c

-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "bh_log.h"
88
#include "bh_platform.h"
99

10-
#if WASM_ENABLE_LINUX_PERF != 0
1110
struct func_info {
1211
uint32 idx;
1312
void *ptr;
@@ -117,4 +116,3 @@ aot_create_perf_map(const AOTModule *module, char *error_buf,
117116

118117
return ret;
119118
}
120-
#endif /* WASM_ENABLE_LINUX_PERF != 0 */

core/iwasm/aot/aot_validator.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2025 Intel Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#include "aot_validator.h"
7+
8+
static void
9+
set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
10+
{
11+
if (error_buf != NULL) {
12+
snprintf(error_buf, error_buf_size,
13+
"AOT module load failed: from validator. %s", string);
14+
}
15+
}
16+
17+
static bool
18+
aot_memory_info_validate(const AOTModule *module, char *error_buf,
19+
uint32 error_buf_size)
20+
{
21+
if (module->import_memory_count > 0) {
22+
set_error_buf(error_buf, error_buf_size,
23+
"import memory is not supported");
24+
return false;
25+
}
26+
27+
if (module->memory_count < 1) {
28+
set_error_buf(error_buf, error_buf_size,
29+
"there should be >=1 memory in one aot module");
30+
return false;
31+
}
32+
33+
return true;
34+
}
35+
36+
bool
37+
aot_module_validate(const AOTModule *module, char *error_buf,
38+
uint32 error_buf_size)
39+
{
40+
if (!aot_memory_info_validate(module, error_buf, error_buf_size)) {
41+
return false;
42+
}
43+
44+
return true;
45+
}

core/iwasm/aot/aot_validator.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (C) 2025 Intel Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#ifndef _AOT_VALIDATOR_H_
7+
#define _AOT_VALIDATOR_H_
8+
9+
#include "aot_runtime.h"
10+
11+
bool
12+
aot_module_validate(const AOTModule *module, char *error_buf,
13+
uint32 error_buf_size);
14+
15+
#endif /* _AOT_VALIDATOR_H_ */

core/iwasm/aot/iwasm_aot.cmake

+13-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,19 @@ add_definitions (-DWASM_ENABLE_AOT=1)
77

88
include_directories (${IWASM_AOT_DIR})
99

10-
file (GLOB c_source_all ${IWASM_AOT_DIR}/*.c)
10+
list (APPEND c_source_all
11+
${IWASM_AOT_DIR}/aot_intrinsic.c
12+
${IWASM_AOT_DIR}/aot_loader.c
13+
${IWASM_AOT_DIR}/aot_runtime.c
14+
)
15+
16+
if (WAMR_BUILD_LINUX_PERF EQUAL 1)
17+
list (APPEND c_source_all ${IWASM_AOT_DIR}/aot_perf_map.c)
18+
endif ()
19+
20+
if (WAMR_BUILD_AOT_VALIDATOR EQUAL 1)
21+
list (APPEND c_source_all ${IWASM_AOT_DIR}/aot_validator.c)
22+
endif ()
1123

1224
if (WAMR_BUILD_TARGET STREQUAL "X86_64" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
1325
set (arch_source ${IWASM_AOT_DIR}/arch/aot_reloc_x86_64.c)

wamr-compiler/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ if (WAMR_BUILD_LLVM_LEGACY_PM EQUAL 1)
5858
endif ()
5959

6060
if (LINUX)
61+
set(WAMR_BUILD_LINUX_PERF 1)
6162
add_definitions(-DWASM_ENABLE_LINUX_PERF=1)
6263
endif ()
6364

0 commit comments

Comments
 (0)