Enable native/app address validation and conversion for wasm app (#102)

Enable setting external memory space for wasm app, the feature is disabled by default;
Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused.
This commit is contained in:
wenyongh
2019-08-21 16:39:50 +08:00
committed by GitHub
parent 9ed6d6af0a
commit de81b95ab8
11 changed files with 428 additions and 39 deletions

View File

@ -152,6 +152,13 @@ wasm_runtime_instantiate(const wasm_module_t module,
void
wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
bool
wasm_runtime_set_ext_memory(wasm_module_inst_t module_inst,
uint8_t *ext_mem_data, uint32_t ext_mem_size,
char *error_buf, uint32_t error_buf_size);
#endif
/**
* Load WASM module instance from AOT file.
*
@ -384,36 +391,6 @@ int32_t
wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
void *native_ptr);
/**
* Find the unique main function from a WASM module instance
* and execute that function.
*
* @param module_inst the WASM module instance
* @param argc the number of arguments
* @param argv the arguments array
*
* @return true if the main function is called, false otherwise.
*/
bool
wasm_application_execute_main(wasm_module_inst_t module_inst,
int argc, char *argv[]);
/**
* Find the specified function in argv[0] from WASM module of current instance
* and execute that function.
*
* @param module_inst the WASM module instance
* @param name the name of the function to execute
* @param argc the number of arguments
* @param argv the arguments array
*
* @return true if the specified function is called, false otherwise.
*/
bool
wasm_application_execute_func(wasm_module_inst_t module_inst,
const char *name, int argc, char *argv[]);
#ifdef __cplusplus
}
#endif

View File

@ -74,6 +74,24 @@ GET_F64_FROM_ADDR (uint32 *addr)
}
#endif /* WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0 */
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
#define CHECK_EXT_MEMORY_SPACE() \
else if (module->ext_mem_data \
&& module->ext_mem_base_offset <= offset1 \
&& offset1 < module->ext_mem_base_offset \
+ module->ext_mem_size) { \
maddr = module->ext_mem_data \
+ (offset1 - module->ext_mem_base_offset); \
if (maddr < module->ext_mem_data) \
goto out_of_bounds; \
maddr1 = maddr + LOAD_SIZE[opcode - WASM_OP_I32_LOAD]; \
if (maddr1 > module->ext_mem_data_end) \
goto out_of_bounds; \
}
#else
#define CHECK_EXT_MEMORY_SPACE()
#endif
#define CHECK_MEMORY_OVERFLOW() do { \
uint32 offset1 = offset + addr; \
uint8 *maddr1; \
@ -89,7 +107,8 @@ GET_F64_FROM_ADDR (uint32 *addr)
if (maddr1 > memory->end_addr) \
goto out_of_bounds; \
} \
else { \
else if (offset1 < memory->heap_base_offset \
+ (memory->heap_data_end - memory->heap_data)) { \
maddr = memory->heap_data + offset1 - memory->heap_base_offset; \
if (maddr < memory->heap_data) \
goto out_of_bounds; \
@ -97,6 +116,9 @@ GET_F64_FROM_ADDR (uint32 *addr)
if (maddr1 > memory->heap_data_end) \
goto out_of_bounds; \
} \
CHECK_EXT_MEMORY_SPACE() \
else \
goto out_of_bounds; \
} while (0)
static inline uint32

View File

@ -992,6 +992,37 @@ wasm_runtime_deinstantiate(WASMModuleInstance *module_inst)
wasm_free(module_inst);
}
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
bool
wasm_runtime_set_ext_memory(WASMModuleInstance *module_inst,
uint8 *ext_mem_data, uint32 ext_mem_size,
char *error_buf, uint32 error_buf_size)
{
if (module_inst->ext_mem_data) {
set_error_buf(error_buf, error_buf_size,
"Set external memory failed: "
"an external memory has been set.");
return false;
}
if (!ext_mem_data
|| ext_mem_size > 1 * BH_GB
|| ext_mem_data + ext_mem_size < ext_mem_data) {
set_error_buf(error_buf, error_buf_size,
"Set external memory failed: "
"invalid input.");
return false;
}
module_inst->ext_mem_data = ext_mem_data;
module_inst->ext_mem_data_end = ext_mem_data + ext_mem_size;
module_inst->ext_mem_size = ext_mem_size;
module_inst->ext_mem_base_offset = DEFAULT_EXT_MEM_BASE_OFFSET;
return true;
}
#endif
bool
wasm_runtime_enlarge_memory(WASMModuleInstance *module, int inc_page_count)
{
@ -1166,24 +1197,40 @@ wasm_runtime_validate_app_addr(WASMModuleInstance *module_inst,
uint8 *addr;
/* integer overflow check */
if(app_offset < 0 ||
app_offset + size < app_offset) {
if(app_offset + size < app_offset) {
goto fail;
}
memory = module_inst->default_memory;
if (app_offset < memory->heap_base_offset) {
if (0 <= app_offset
&& app_offset < memory->heap_base_offset) {
addr = memory->memory_data + app_offset;
if (!(memory->base_addr <= addr && addr + size <= memory->end_addr))
goto fail;
return true;
}
else {
else if (memory->heap_base_offset < app_offset
&& app_offset < memory->heap_base_offset
+ (memory->heap_data_end - memory->heap_data)) {
addr = memory->heap_data + (app_offset - memory->heap_base_offset);
if (!(memory->heap_data <= addr && addr + size <= memory->heap_data_end))
goto fail;
return true;
}
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
else if (module_inst->ext_mem_data
&& module_inst->ext_mem_base_offset <= app_offset
&& app_offset < module_inst->ext_mem_base_offset
+ module_inst->ext_mem_size) {
addr = module_inst->ext_mem_data
+ (app_offset - module_inst->ext_mem_base_offset);
if (!(module_inst->ext_mem_data <= addr
&& addr + size <= module_inst->ext_mem_data_end))
goto fail;
return true;
}
#endif
fail:
wasm_runtime_set_exception(module_inst, "out of bounds memory access");
@ -1202,7 +1249,13 @@ wasm_runtime_validate_native_addr(WASMModuleInstance *module_inst,
}
if ((memory->base_addr <= addr && addr + size <= memory->end_addr)
|| (memory->heap_data <= addr && addr + size <= memory->heap_data_end))
|| (memory->heap_data <= addr && addr + size <= memory->heap_data_end)
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
|| (module_inst->ext_mem_data
&& module_inst->ext_mem_data <= addr
&& addr + size <= module_inst->ext_mem_data_end)
#endif
)
return true;
fail:
@ -1215,10 +1268,22 @@ wasm_runtime_addr_app_to_native(WASMModuleInstance *module_inst,
int32 app_offset)
{
WASMMemoryInstance *memory = module_inst->default_memory;
if (app_offset < memory->heap_base_offset)
if (0 <= app_offset && app_offset < memory->heap_base_offset)
return memory->memory_data + app_offset;
else
else if (memory->heap_base_offset < app_offset
&& app_offset < memory->heap_base_offset
+ (memory->heap_data_end - memory->heap_data))
return memory->heap_data + (app_offset - memory->heap_base_offset);
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
else if (module_inst->ext_mem_data
&& module_inst->ext_mem_base_offset <= app_offset
&& app_offset < module_inst->ext_mem_base_offset
+ module_inst->ext_mem_size)
return module_inst->ext_mem_data
+ (app_offset - module_inst->ext_mem_base_offset);
#endif
else
return NULL;
}
int32
@ -1229,9 +1294,19 @@ wasm_runtime_addr_native_to_app(WASMModuleInstance *module_inst,
if (memory->base_addr <= (uint8*)native_ptr
&& (uint8*)native_ptr < memory->end_addr)
return (uint8*)native_ptr - memory->memory_data;
else
else if (memory->heap_data <= (uint8*)native_ptr
&& (uint8*)native_ptr < memory->heap_data_end)
return memory->heap_base_offset
+ ((uint8*)native_ptr - memory->heap_data);
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
else if (module_inst->ext_mem_data
&& module_inst->ext_mem_data <= (uint8*)native_ptr
&& (uint8*)native_ptr < module_inst->ext_mem_data_end)
return module_inst->ext_mem_base_offset
+ ((uint8*)native_ptr - module_inst->ext_mem_data);
#endif
else
return 0;
}
uint32

View File

@ -148,6 +148,13 @@ typedef struct WASMModuleInstance {
uint32 temp_ret;
uint32 llvm_stack;
#if WASM_ENABLE_EXT_MEMORY_SPACE != 0
int32 ext_mem_base_offset;
uint8 *ext_mem_data;
uint8 *ext_mem_data_end;
uint32 ext_mem_size;
#endif
/* Default WASM stack size of threads of this Module instance. */
uint32 wasm_stack_size;