Skip to content
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ cd iwasm/products/linux/bin
You will get the following output:
```
Hello world!
buf ptr: 0x000101ac
buf ptr: 0x400002b0
buf: 1234
```
If you would like to run the test app on Zephyr, we have embedded a test sample into its OS image. You will need to execute:
Expand Down
9 changes: 7 additions & 2 deletions core/iwasm/products/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ if (NOT ("$ENV{VALGRIND}" STREQUAL "YES"))
add_definitions(-DNVALGRIND)
endif ()

# Currently build as 32-bit by default.
set (BUILD_AS_64BIT_SUPPORT "NO")
# Currently build as 64-bit by default.
set (BUILD_AS_64BIT_SUPPORT "YES")

if (CMAKE_SIZEOF_VOID_P EQUAL 8)
if (${BUILD_AS_64BIT_SUPPORT} STREQUAL "YES")
Expand All @@ -44,6 +44,11 @@ else ()
endif ()
endif ()

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif (NOT CMAKE_BUILD_TYPE)
message ("CMAKE_BUILD_TYPE = " ${CMAKE_BUILD_TYPE})

set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections -Wall -Wno-unused-parameter -Wno-pedantic")

Expand Down
41 changes: 21 additions & 20 deletions core/iwasm/products/linux/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ static int print_help()
{
wasm_printf("Usage: iwasm [-options] wasm_file [args...]\n");
wasm_printf("options:\n");
wasm_printf(" -f|--function name Specify function name to run "
"in module rather than main\n");
wasm_printf(" -f|--function name Specify function name to run in module\n"
" rather than main\n");
#if WASM_ENABLE_LOG != 0
wasm_printf(
" -v=X Set log verbose level (0 to 2, default is 1), larger level with more log\n");
wasm_printf(" -v=X Set log verbose level (0 to 2, default is 1),\n"
" larger level with more log\n");
#endif
wasm_printf(
" --repl Start a very simple REPL (read-eval-print-loop) mode \n"
" that runs commands in the form of `FUNC ARG...`\n");
wasm_printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
" that runs commands in the form of `FUNC ARG...`\n");
return 1;
}

Expand All @@ -64,7 +63,7 @@ app_instance_func(wasm_module_inst_t module_inst, const char *func_name)
const char *exception;

wasm_application_execute_func(module_inst, func_name, app_argc - 1,
app_argv + 1);
app_argv + 1);
if ((exception = wasm_runtime_get_exception(module_inst)))
wasm_printf("%s\n", exception);
return NULL;
Expand Down Expand Up @@ -122,7 +121,7 @@ app_instance_repl(wasm_module_inst_t module_inst)
}
if (app_argc != 0) {
wasm_application_execute_func(module_inst, app_argv[0],
app_argc - 1, app_argv + 1);
app_argc - 1, app_argv + 1);
}
free(app_argv);
}
Expand Down Expand Up @@ -177,7 +176,7 @@ int main(int argc, char *argv[])
app_argv = argv;

if (bh_memory_init_with_pool(global_heap_buf, sizeof(global_heap_buf))
!= 0) {
!= 0) {
wasm_printf("Init global heap failed.\n");
return -1;
}
Expand All @@ -190,19 +189,22 @@ int main(int argc, char *argv[])

/* load WASM byte buffer from WASM bin file */
if (!(wasm_file_buf = (uint8*) wasm_read_file_to_buffer(wasm_file,
&wasm_file_size)))
&wasm_file_size)))
goto fail2;

/* load WASM module */
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
error_buf, sizeof(error_buf)))) {
error_buf, sizeof(error_buf)))) {
wasm_printf("%s\n", error_buf);
goto fail3;
}

/* instantiate the module */
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, 8 * 1024,
8 * 1024, error_buf, sizeof(error_buf)))) {
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module,
16 * 1024, /* stack size */
8 * 1024, /* heap size */
error_buf,
sizeof(error_buf)))) {
wasm_printf("%s\n", error_buf);
goto fail4;
}
Expand All @@ -217,21 +219,20 @@ int main(int argc, char *argv[])
/* destroy the module instance */
wasm_runtime_deinstantiate(wasm_module_inst);

fail4:
fail4:
/* unload the module */
wasm_runtime_unload(wasm_module);

fail3:
fail3:
/* free the file buffer */
wasm_free(wasm_file_buf);

fail2:
fail2:
/* destroy runtime environment */
wasm_runtime_destroy();

fail1: bh_memory_destroy();

(void) func_name;
fail1:
bh_memory_destroy();
return 0;
}

65 changes: 35 additions & 30 deletions core/iwasm/runtime/vmcore-wasm/wasm_interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,28 @@ GET_F64_FROM_ADDR (uint32 *addr)
}
#endif /* WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0 */

#define is_valid_addr(memory, heap, addr) \
(memory->base_addr <= addr && addr <= memory->end_addr) \

#define CHECK_MEMORY_OVERFLOW() do { \
uint32 offset1 = offset + addr; \
uint8 *maddr1; \
if (flags != 2) \
LOG_VERBOSE("unaligned load/store in wasm interp, flag is: %d.\n", flags);\
if (offset + addr < addr) { \
wasm_runtime_set_exception(module, "out of bounds memory access"); \
goto got_exception; \
} \
maddr = memory->memory_data + (offset + addr); \
if (!is_valid_addr(memory, NULL, maddr)) { \
wasm_runtime_set_exception(module, "out of bounds memory access"); \
goto got_exception; \
if (offset1 < offset) \
goto out_of_bounds; \
if (offset1 < heap_base_offset) { \
maddr = memory->memory_data + offset1; \
if (maddr < memory->base_addr) \
goto out_of_bounds; \
maddr1 = maddr + LOAD_SIZE[opcode - WASM_OP_I32_LOAD]; \
if (maddr1 > memory->end_addr) \
goto out_of_bounds; \
} \
maddr1 = maddr + LOAD_SIZE[opcode - WASM_OP_I32_LOAD]; \
if (!is_valid_addr(memory, NULL, maddr1)) { \
wasm_runtime_set_exception(module, "out of bounds memory access"); \
goto got_exception; \
else { \
maddr = memory->heap_data + offset1 - memory->heap_base_offset; \
if (maddr < memory->heap_data) \
goto out_of_bounds; \
maddr1 = maddr + LOAD_SIZE[opcode - WASM_OP_I32_LOAD]; \
if (maddr1 > memory->heap_data_end) \
goto out_of_bounds; \
} \
} while (0)

Expand Down Expand Up @@ -712,6 +714,7 @@ wasm_interp_call_func_bytecode(WASMThread *self,
{
WASMModuleInstance *module = self->module_inst;
WASMMemoryInstance *memory = module->default_memory;
int32 heap_base_offset = memory ? memory->heap_base_offset : 0;
WASMTableInstance *table = module->default_table;
uint8 opcode_IMPDEP2 = WASM_OP_IMPDEP2;
WASMInterpFrame *frame = NULL;
Expand Down Expand Up @@ -1247,26 +1250,25 @@ wasm_interp_call_func_bytecode(WASMThread *self,

HANDLE_OP (WASM_OP_MEMORY_GROW):
{
uint32 reserved, prev_page_count, delta, tmp;
uint32 reserved, delta, prev_page_count = memory->cur_page_count;

read_leb_uint32(frame_ip, frame_ip_end, reserved);
prev_page_count = memory->cur_page_count;
delta = POP_I32();
PUSH_I32(prev_page_count);
if (delta == 0)
HANDLE_OP_END ();
else if (delta + prev_page_count > memory->max_page_count ||
delta + prev_page_count < prev_page_count) {
tmp = POP_I32();

if (!wasm_runtime_enlarge_memory(module, delta)) {
/* fail to memory.grow, return -1 */
PUSH_I32(-1);
(void)tmp;
HANDLE_OP_END ();
if (wasm_runtime_get_exception(module)) {
printf("%s\n", wasm_runtime_get_exception(module));
wasm_runtime_set_exception(module, NULL);
}
}
else {
/* success, return previous page count */
PUSH_I32(prev_page_count);
/* update the memory instance ptr */
memory = module->default_memory;
}

if (!wasm_runtime_enlarge_memory(module, delta))
goto got_exception;

memory = module->default_memory;

(void)reserved;
HANDLE_OP_END ();
Expand Down Expand Up @@ -2093,6 +2095,9 @@ wasm_interp_call_func_bytecode(WASMThread *self,
HANDLE_OP_END ();
}

out_of_bounds:
wasm_runtime_set_exception(module, "out of bounds memory access");

got_exception:
if (depths && depths != depth_buf) {
wasm_free(depths);
Expand Down
24 changes: 18 additions & 6 deletions core/iwasm/runtime/vmcore-wasm/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,20 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
char *error_buf, uint32 error_buf_size)
{
const uint8 *p = *p_buf, *p_end = buf_end;
uint32 pool_size = bh_memory_pool_size();
uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
/ NumBytesPerPage;

read_leb_uint32(p, p_end, memory->flags);
read_leb_uint32(p, p_end, memory->init_page_count);
if (memory->flags & 1)
if (memory->flags & 1) {
read_leb_uint32(p, p_end, memory->max_page_count);
if (memory->max_page_count > max_page_count)
memory->max_page_count = max_page_count;
}
else
/* Limit the maximum memory size to 4GB */
memory->max_page_count = 0x10000;
/* Limit the maximum memory size to max_page_count */
memory->max_page_count = max_page_count;

*p_buf = p;
return true;
Expand Down Expand Up @@ -351,14 +357,20 @@ load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
char *error_buf, uint32 error_buf_size)
{
const uint8 *p = *p_buf, *p_end = buf_end;
uint32 pool_size = bh_memory_pool_size();
uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
/ NumBytesPerPage;

read_leb_uint32(p, p_end, memory->flags);
read_leb_uint32(p, p_end, memory->init_page_count);
if (memory->flags & 1)
if (memory->flags & 1) {
read_leb_uint32(p, p_end, memory->max_page_count);
if (memory->max_page_count > max_page_count)
memory->max_page_count = max_page_count;
}
else
/* Limit the maximum memory size to 4GB */
memory->max_page_count = 0x10000;
/* Limit the maximum memory size to max_page_count */
memory->max_page_count = max_page_count;

*p_buf = p;
return true;
Expand Down
Loading