Skip to content

Commit 1d47fc5

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

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

core/iwasm/aot/aot_loader.c

+8
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"
@@ -1111,6 +1112,7 @@ load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
11111112
return false;
11121113

11131114
read_uint32(buf, buf_end, module->memory_count);
1115+
11141116
total_size = sizeof(AOTMemory) * (uint64)module->memory_count;
11151117
if (!(module->memories =
11161118
loader_malloc(total_size, error_buf, error_buf_size))) {
@@ -4403,6 +4405,12 @@ aot_load_from_aot_file(const uint8 *buf, uint32 size, const LoadArgs *args,
44034405
os_thread_jit_write_protect_np(true); /* Make memory executable */
44044406
os_icache_flush(module->code, module->code_size);
44054407

4408+
/*TODO: use a CLI option to control? */
4409+
if (!aot_module_validate(module, error_buf, error_buf_size)) {
4410+
aot_unload(module);
4411+
return NULL;
4412+
}
4413+
44064414
LOG_VERBOSE("Load module success.\n");
44074415
return module;
44084416
}

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)