Skip to content

Commit 716fcb7

Browse files
committed
Add AOT module validation to ensure memory constraints are met
1 parent 7f3e0df commit 716fcb7

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

core/iwasm/aot/aot_loader.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "../common/wasm_native.h"
1111
#include "../common/wasm_loader_common.h"
1212
#include "../compilation/aot.h"
13+
#include "aot_validator.h"
1314

1415
#if WASM_ENABLE_DEBUG_AOT != 0
1516
#include "debug/elf_parser.h"
@@ -1106,9 +1107,6 @@ load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
11061107
const uint8 *buf = *p_buf;
11071108

11081109
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;
11121110

11131111
read_uint32(buf, buf_end, module->memory_count);
11141112
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,
44034401
os_thread_jit_write_protect_np(true); /* Make memory executable */
44044402
os_icache_flush(module->code, module->code_size);
44054403

4404+
/*TODO: use a CLI option to control? */
4405+
if (!aot_module_validate(module, error_buf, error_buf_size)) {
4406+
aot_unload(module);
4407+
return NULL;
4408+
}
4409+
44064410
LOG_VERBOSE("Load module success.\n");
44074411
return module;
44084412
}

core/iwasm/aot/aot_validator.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2019 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) 2019 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_ */

0 commit comments

Comments
 (0)