Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a validator for aot module #3995

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build-scripts/config_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,7 @@ endif()
if (NOT WAMR_BUILD_SANITIZER STREQUAL "")
message (" Sanitizer ${WAMR_BUILD_SANITIZER} enabled")
endif ()
if (WAMR_BUILD_AOT_VALIDATOR EQUAL 1)
message (" AOT validator enabled")
add_definitions(-DWASM_ENABLE_AOT_VALIDATOR=1)
endif ()
4 changes: 4 additions & 0 deletions core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -698,4 +698,8 @@
#define WASM_ENABLE_SHARED_HEAP 0
#endif

#ifndef WASM_ENABLE_AOT_VALIDATOR
#define WASM_ENABLE_AOT_VALIDATOR 0
#endif

#endif /* end of _CONFIG_H_ */
13 changes: 10 additions & 3 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +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"
Expand Down Expand Up @@ -1106,9 +1109,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;
Expand Down Expand Up @@ -4403,6 +4403,13 @@ 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);

#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;
}
Expand Down
2 changes: 0 additions & 2 deletions core/iwasm/aot/aot_perf_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -117,4 +116,3 @@ aot_create_perf_map(const AOTModule *module, char *error_buf,

return ret;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add an empty line at the end of the file

#endif /* WASM_ENABLE_LINUX_PERF != 0 */
45 changes: 45 additions & 0 deletions core/iwasm/aot/aot_validator.c
Original file line number Diff line number Diff line change
@@ -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;
}
15 changes: 15 additions & 0 deletions core/iwasm/aot/aot_validator.h
Original file line number Diff line number Diff line change
@@ -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_ */
14 changes: 13 additions & 1 deletion core/iwasm/aot/iwasm_aot.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions wamr-compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()

Expand Down
Loading