From 89b077b27539181bbb50d78da185100627d3c391 Mon Sep 17 00:00:00 2001 From: "liang.he@intel.com" Date: Mon, 30 Dec 2024 23:39:17 +0000 Subject: [PATCH 1/6] Add AOT module validation to ensure memory constraints are met --- core/iwasm/aot/aot_loader.c | 10 +++++--- core/iwasm/aot/aot_validator.c | 45 ++++++++++++++++++++++++++++++++++ core/iwasm/aot/aot_validator.h | 15 ++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 core/iwasm/aot/aot_validator.c create mode 100644 core/iwasm/aot/aot_validator.h diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index bde3ee034d..c36b18ca49 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -10,6 +10,7 @@ #include "../common/wasm_native.h" #include "../common/wasm_loader_common.h" #include "../compilation/aot.h" +#include "aot_validator.h" #if WASM_ENABLE_DEBUG_AOT != 0 #include "debug/elf_parser.h" @@ -1106,9 +1107,6 @@ load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, const uint8 *buf = *p_buf; read_uint32(buf, buf_end, module->import_memory_count); - /* We don't support import_memory_count > 0 currently */ - if (module->import_memory_count > 0) - return false; read_uint32(buf, buf_end, module->memory_count); total_size = sizeof(AOTMemory) * (uint64)module->memory_count; @@ -4403,6 +4401,12 @@ aot_load_from_aot_file(const uint8 *buf, uint32 size, const LoadArgs *args, os_thread_jit_write_protect_np(true); /* Make memory executable */ os_icache_flush(module->code, module->code_size); + /*TODO: use a CLI option to control? */ + if (!aot_module_validate(module, error_buf, error_buf_size)) { + aot_unload(module); + return NULL; + } + LOG_VERBOSE("Load module success.\n"); return module; } diff --git a/core/iwasm/aot/aot_validator.c b/core/iwasm/aot/aot_validator.c new file mode 100644 index 0000000000..d481dbf31a --- /dev/null +++ b/core/iwasm/aot/aot_validator.c @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "aot_validator.h" + +static void +set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) +{ + if (error_buf != NULL) { + snprintf(error_buf, error_buf_size, + "AOT module load failed: from validator. %s", string); + } +} + +static bool +aot_memory_info_validate(const AOTModule *module, char *error_buf, + uint32 error_buf_size) +{ + if (module->import_memory_count > 0) { + set_error_buf(error_buf, error_buf_size, + "import memory is not supported"); + return false; + } + + if (module->memory_count < 1) { + set_error_buf(error_buf, error_buf_size, + "there should be >=1 memory in one aot module"); + return false; + } + + return true; +} + +bool +aot_module_validate(const AOTModule *module, char *error_buf, + uint32 error_buf_size) +{ + if (!aot_memory_info_validate(module, error_buf, error_buf_size)) { + return false; + } + + return true; +} \ No newline at end of file diff --git a/core/iwasm/aot/aot_validator.h b/core/iwasm/aot/aot_validator.h new file mode 100644 index 0000000000..baec27640a --- /dev/null +++ b/core/iwasm/aot/aot_validator.h @@ -0,0 +1,15 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef _AOT_VALIDATOR_H_ +#define _AOT_VALIDATOR_H_ + +#include "aot_runtime.h" + +bool +aot_module_validate(const AOTModule *module, char *error_buf, + uint32 error_buf_size); + +#endif /* _AOT_VALIDATOR_H_ */ From 4373555d182339b3dfe819c8387b0db4599ac795 Mon Sep 17 00:00:00 2001 From: "liang.he@intel.com" Date: Tue, 31 Dec 2024 02:13:41 +0000 Subject: [PATCH 2/6] Refactor source file list formatting in SConscript for improved readability --- core/iwasm/aot/SConscript | 1 + 1 file changed, 1 insertion(+) diff --git a/core/iwasm/aot/SConscript b/core/iwasm/aot/SConscript index 790f284041..9f294e999f 100644 --- a/core/iwasm/aot/SConscript +++ b/core/iwasm/aot/SConscript @@ -14,6 +14,7 @@ src = Split(''' aot_loader.c aot_runtime.c aot_intrinsic.c +aot_validator.c ''') if rtconfig.ARCH == 'arm': From d3ef7b3b950e540ac9a7cf90f4b0992b95f3cfab Mon Sep 17 00:00:00 2001 From: "liang.he@intel.com" Date: Thu, 2 Jan 2025 07:05:47 +0000 Subject: [PATCH 3/6] Add aot_validator.c to the build configuration --- product-mini/platforms/nuttx/wamr.mk | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/product-mini/platforms/nuttx/wamr.mk b/product-mini/platforms/nuttx/wamr.mk index 44d8694e57..9bced25f3f 100644 --- a/product-mini/platforms/nuttx/wamr.mk +++ b/product-mini/platforms/nuttx/wamr.mk @@ -126,7 +126,8 @@ CFLAGS += -DWASM_ENABLE_AOT=1 CSRCS += aot_loader.c \ $(AOT_RELOC) \ aot_intrinsic.c \ - aot_runtime.c + aot_runtime.c \ + aot_validator.c ifeq ($(CONFIG_INTERPRETERS_WAMR_DEBUG_AOT),y) CFLAGS += -DWASM_ENABLE_DEBUG_AOT=1 CSRCS += elf_parser.c \ From 1f97b4f779469a6ac7d64b59fcd6d4c5ae477d49 Mon Sep 17 00:00:00 2001 From: "liang.he@intel.com" Date: Fri, 3 Jan 2025 02:40:41 +0000 Subject: [PATCH 4/6] Enable AOT validator in build configuration and update related source files --- build-scripts/config_common.cmake | 4 ++++ core/config.h | 4 ++++ core/iwasm/aot/SConscript | 1 - core/iwasm/aot/aot_loader.c | 5 ++++- core/iwasm/aot/aot_perf_map.c | 2 -- core/iwasm/aot/aot_validator.c | 2 +- core/iwasm/aot/iwasm_aot.cmake | 14 +++++++++++++- product-mini/platforms/nuttx/wamr.mk | 3 +-- wamr-compiler/CMakeLists.txt | 1 + 9 files changed, 28 insertions(+), 8 deletions(-) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 6a30bfb7b7..6173e73b5f 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -610,4 +610,8 @@ if (WAMR_BUILD_SHRUNK_MEMORY EQUAL 1) else () add_definitions (-DWASM_ENABLE_SHRUNK_MEMORY=0) message (" Shrunk memory disabled") +endif() +if (WAMR_BUILD_AOT_VALIDATOR EQUAL 1) + message (" AOT validator enabled") + add_definitions (-DWASM_ENABLE_AOT_VALIDATOR=1) endif () diff --git a/core/config.h b/core/config.h index 27d26f0937..fbbbf6771d 100644 --- a/core/config.h +++ b/core/config.h @@ -702,4 +702,8 @@ #define WASM_ENABLE_SHRUNK_MEMORY 1 #endif +#ifndef WASM_ENABLE_AOT_VALIDATOR +#define WASM_ENABLE_AOT_VALIDATOR 0 +#endif + #endif /* end of _CONFIG_H_ */ diff --git a/core/iwasm/aot/SConscript b/core/iwasm/aot/SConscript index 9f294e999f..790f284041 100644 --- a/core/iwasm/aot/SConscript +++ b/core/iwasm/aot/SConscript @@ -14,7 +14,6 @@ src = Split(''' aot_loader.c aot_runtime.c aot_intrinsic.c -aot_validator.c ''') if rtconfig.ARCH == 'arm': diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index c36b18ca49..97360e73e7 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -10,7 +10,9 @@ #include "../common/wasm_native.h" #include "../common/wasm_loader_common.h" #include "../compilation/aot.h" +#if WASM_ENABLE_AOT_VALIDATOR != 0 #include "aot_validator.h" +#endif #if WASM_ENABLE_DEBUG_AOT != 0 #include "debug/elf_parser.h" @@ -4401,11 +4403,12 @@ aot_load_from_aot_file(const uint8 *buf, uint32 size, const LoadArgs *args, os_thread_jit_write_protect_np(true); /* Make memory executable */ os_icache_flush(module->code, module->code_size); - /*TODO: use a CLI option to control? */ +#if WASM_ENABLE_AOT_VALIDATOR != 0 if (!aot_module_validate(module, error_buf, error_buf_size)) { aot_unload(module); return NULL; } +#endif /* WASM_ENABLE_AOT_VALIDATOR != 0 */ LOG_VERBOSE("Load module success.\n"); return module; diff --git a/core/iwasm/aot/aot_perf_map.c b/core/iwasm/aot/aot_perf_map.c index 22700dcdd6..b96bcd1bf4 100644 --- a/core/iwasm/aot/aot_perf_map.c +++ b/core/iwasm/aot/aot_perf_map.c @@ -7,7 +7,6 @@ #include "bh_log.h" #include "bh_platform.h" -#if WASM_ENABLE_LINUX_PERF != 0 struct func_info { uint32 idx; void *ptr; @@ -117,4 +116,3 @@ aot_create_perf_map(const AOTModule *module, char *error_buf, return ret; } -#endif /* WASM_ENABLE_LINUX_PERF != 0 */ \ No newline at end of file diff --git a/core/iwasm/aot/aot_validator.c b/core/iwasm/aot/aot_validator.c index d481dbf31a..17886059a1 100644 --- a/core/iwasm/aot/aot_validator.c +++ b/core/iwasm/aot/aot_validator.c @@ -42,4 +42,4 @@ aot_module_validate(const AOTModule *module, char *error_buf, } return true; -} \ No newline at end of file +} diff --git a/core/iwasm/aot/iwasm_aot.cmake b/core/iwasm/aot/iwasm_aot.cmake index efff88dd07..c82501fad4 100644 --- a/core/iwasm/aot/iwasm_aot.cmake +++ b/core/iwasm/aot/iwasm_aot.cmake @@ -7,7 +7,19 @@ add_definitions (-DWASM_ENABLE_AOT=1) include_directories (${IWASM_AOT_DIR}) -file (GLOB c_source_all ${IWASM_AOT_DIR}/*.c) +list (APPEND c_source_all + ${IWASM_AOT_DIR}/aot_intrinsic.c + ${IWASM_AOT_DIR}/aot_loader.c + ${IWASM_AOT_DIR}/aot_runtime.c +) + +if (WAMR_BUILD_LINUX_PERF EQUAL 1) + list (APPEND c_source_all ${IWASM_AOT_DIR}/aot_perf_map.c) +endif () + +if (WAMR_BUILD_AOT_VALIDATOR EQUAL 1) + list (APPEND c_source_all ${IWASM_AOT_DIR}/aot_validator.c) +endif () if (WAMR_BUILD_TARGET STREQUAL "X86_64" OR WAMR_BUILD_TARGET STREQUAL "AMD_64") set (arch_source ${IWASM_AOT_DIR}/arch/aot_reloc_x86_64.c) diff --git a/product-mini/platforms/nuttx/wamr.mk b/product-mini/platforms/nuttx/wamr.mk index 9bced25f3f..44d8694e57 100644 --- a/product-mini/platforms/nuttx/wamr.mk +++ b/product-mini/platforms/nuttx/wamr.mk @@ -126,8 +126,7 @@ CFLAGS += -DWASM_ENABLE_AOT=1 CSRCS += aot_loader.c \ $(AOT_RELOC) \ aot_intrinsic.c \ - aot_runtime.c \ - aot_validator.c + aot_runtime.c ifeq ($(CONFIG_INTERPRETERS_WAMR_DEBUG_AOT),y) CFLAGS += -DWASM_ENABLE_DEBUG_AOT=1 CSRCS += elf_parser.c \ diff --git a/wamr-compiler/CMakeLists.txt b/wamr-compiler/CMakeLists.txt index bc56f40308..f54a3e542a 100644 --- a/wamr-compiler/CMakeLists.txt +++ b/wamr-compiler/CMakeLists.txt @@ -58,6 +58,7 @@ if (WAMR_BUILD_LLVM_LEGACY_PM EQUAL 1) endif () if (LINUX) + set(WAMR_BUILD_LINUX_PERF 1) add_definitions(-DWASM_ENABLE_LINUX_PERF=1) endif () From 6c0a4d406a1182808f7e899a279bbc5846ef57f3 Mon Sep 17 00:00:00 2001 From: "liang.he@intel.com" Date: Thu, 9 Jan 2025 04:47:06 +0000 Subject: [PATCH 5/6] Fix formatting issue in aot_perf_map.c for consistency --- core/iwasm/aot/aot_perf_map.c | 1 + 1 file changed, 1 insertion(+) diff --git a/core/iwasm/aot/aot_perf_map.c b/core/iwasm/aot/aot_perf_map.c index b96bcd1bf4..e744086719 100644 --- a/core/iwasm/aot/aot_perf_map.c +++ b/core/iwasm/aot/aot_perf_map.c @@ -116,3 +116,4 @@ aot_create_perf_map(const AOTModule *module, char *error_buf, return ret; } + From 58e56c3847d188ba3530b22b0a12c7b38e685206 Mon Sep 17 00:00:00 2001 From: "liang.he@intel.com" Date: Thu, 9 Jan 2025 04:56:14 +0000 Subject: [PATCH 6/6] Remove unnecessary blank line in aot_perf_map.c for cleaner code --- core/iwasm/aot/aot_perf_map.c | 1 - core/iwasm/aot/aot_validator.c | 2 +- core/iwasm/aot/aot_validator.h | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/core/iwasm/aot/aot_perf_map.c b/core/iwasm/aot/aot_perf_map.c index e744086719..b96bcd1bf4 100644 --- a/core/iwasm/aot/aot_perf_map.c +++ b/core/iwasm/aot/aot_perf_map.c @@ -116,4 +116,3 @@ aot_create_perf_map(const AOTModule *module, char *error_buf, return ret; } - diff --git a/core/iwasm/aot/aot_validator.c b/core/iwasm/aot/aot_validator.c index 17886059a1..58757f767f 100644 --- a/core/iwasm/aot/aot_validator.c +++ b/core/iwasm/aot/aot_validator.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 Intel Corporation. All rights reserved. + * Copyright (C) 2025 Intel Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ diff --git a/core/iwasm/aot/aot_validator.h b/core/iwasm/aot/aot_validator.h index baec27640a..dd8f0ecb5e 100644 --- a/core/iwasm/aot/aot_validator.h +++ b/core/iwasm/aot/aot_validator.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 Intel Corporation. All rights reserved. + * Copyright (C) 2025 Intel Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */