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
17 changes: 0 additions & 17 deletions core/iwasm/lib/native/libc/libc_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -984,23 +984,6 @@ wasm_native_global_lookup(const char *module_name, const char *global_name,
global_def++;
}

/* Lookup non-constant globals which cannot be defined by table */
if (!strcmp(module_name, "env")) {
if (!strcmp(global_name, "_stdin")) {
global->global_data_linked.addr = (uintptr_t)stdin;
global->is_addr = true;
return true;
} else if (!strcmp(global_name, "_stdout")) {
global->global_data_linked.addr = (uintptr_t)stdout;
global->is_addr = true;
return true;
} else if (!strcmp(global_name, "_stderr")) {
global->global_data_linked.addr = (uintptr_t)stderr;
global->is_addr = true;
return true;
}
}

return false;
}

Expand Down
7 changes: 5 additions & 2 deletions core/iwasm/products/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ include (../../lib/native/base/wasm_lib_base.cmake)
include (../../lib/native/libc/wasm_libc.cmake)
include (${SHARED_LIB_DIR}/platform/${PLATFORM}/shared_platform.cmake)
include (${SHARED_LIB_DIR}/mem-alloc/mem_alloc.cmake)
include (${SHARED_LIB_DIR}/utils/shared_utils.cmake)

add_library (vmlib
${WASM_PLATFORM_LIB_SOURCE}
Expand All @@ -76,7 +77,8 @@ add_library (vmlib
${WASM_LIB_BASE_DIR}/base_lib_export.c
${WASM_LIBC_SOURCE}
${PLATFORM_SHARED_SOURCE}
${MEM_ALLOC_SHARED_SOURCE})
${MEM_ALLOC_SHARED_SOURCE}
${UTILS_SHARED_SOURCE})

add_executable (iwasm main.c ext_lib_export.c)

Expand All @@ -91,7 +93,8 @@ add_library (libiwasm SHARED
${WASM_LIB_BASE_DIR}/base_lib_export.c
${WASM_LIBC_SOURCE}
${PLATFORM_SHARED_SOURCE}
${MEM_ALLOC_SHARED_SOURCE})
${MEM_ALLOC_SHARED_SOURCE}
${UTILS_SHARED_SOURCE})

install (TARGETS libiwasm DESTINATION lib)

Expand Down
31 changes: 19 additions & 12 deletions core/iwasm/products/zephyr/simple/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
#include "bh_memory.h"
#include "test_wasm.h"

#define CONFIG_GLOBAL_HEAP_BUF_SIZE 131072
#define CONFIG_APP_STACK_SIZE 8192
#define CONFIG_APP_HEAP_SIZE 8192
#define CONFIG_MAIN_THREAD_STACK_SIZE 4096

static int app_argc;
static char **app_argv;

Expand Down Expand Up @@ -54,7 +59,7 @@ app_instance_main(wasm_module_inst_t module_inst)
return NULL;
}

static char global_heap_buf[512 * 1024] = { 0 };
static char global_heap_buf[CONFIG_GLOBAL_HEAP_BUF_SIZE] = { 0 };

void iwasm_main(void *arg1, void *arg2, void *arg3)
{
Expand All @@ -72,7 +77,7 @@ void iwasm_main(void *arg1, void *arg2, void *arg3)
(void) arg3;

if (bh_memory_init_with_pool(global_heap_buf, sizeof(global_heap_buf))
!= 0) {
!= 0) {
wasm_printf("Init global heap failed.\n");
return;
}
Expand All @@ -91,14 +96,17 @@ void iwasm_main(void *arg1, void *arg2, void *arg3)

/* 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 fail2;
}

/* 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,
CONFIG_APP_STACK_SIZE,
CONFIG_APP_HEAP_SIZE,
error_buf,
sizeof(error_buf)))) {
wasm_printf("%s\n", error_buf);
goto fail3;
}
Expand All @@ -119,24 +127,23 @@ void iwasm_main(void *arg1, void *arg2, void *arg3)
fail1: bh_memory_destroy();
}

#define DEFAULT_THREAD_STACKSIZE (6 * 1024)
#define DEFAULT_THREAD_PRIORITY 5
#define MAIN_THREAD_STACK_SIZE (CONFIG_MAIN_THREAD_STACK_SIZE)
#define MAIN_THREAD_PRIORITY 5

K_THREAD_STACK_DEFINE(iwasm_main_thread_stack, DEFAULT_THREAD_STACKSIZE);
K_THREAD_STACK_DEFINE(iwasm_main_thread_stack, MAIN_THREAD_STACK_SIZE);
static struct k_thread iwasm_main_thread;

bool iwasm_init(void)
{
k_tid_t tid = k_thread_create(&iwasm_main_thread, iwasm_main_thread_stack,
DEFAULT_THREAD_STACKSIZE, iwasm_main, NULL, NULL, NULL,
DEFAULT_THREAD_PRIORITY, 0, K_NO_WAIT);
MAIN_THREAD_STACK_SIZE,
iwasm_main, NULL, NULL, NULL,
MAIN_THREAD_PRIORITY, 0, K_NO_WAIT);
return tid ? true : false;
}

#ifndef CONFIG_AEE_ENABLE
void main(void)
{
iwasm_init();
}
#endif

110 changes: 110 additions & 0 deletions core/iwasm/runtime/include/bh_memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef _BH_MEMORY_H
#define _BH_MEMORY_H

#ifdef __cplusplus
extern "C" {
#endif

#define BH_KB (1024)
#define BH_MB ((BH_KB)*1024)
#define BH_GB ((BH_MB)*1024)

/**
* Initialize memory allocator with a pool, the bh_malloc/bh_free function
* will malloc/free memory from the pool
*
* @param mem the pool buffer
* @param bytes the size bytes of the buffer
*
* @return 0 if success, -1 otherwise
*/
int bh_memory_init_with_pool(void *mem, unsigned int bytes);

/**
* Initialize memory allocator with memory allocator, the bh_malloc/bh_free
* function will malloc/free memory with the allocator passed
*
* @param malloc_func the malloc function
* @param free_func the free function
*
* @return 0 if success, -1 otherwise
*/
int bh_memory_init_with_allocator(void *malloc_func, void *free_func);

/**
* Destroy memory
*/
void bh_memory_destroy();

/**
* Get the pool size of memory, if memory is initialized with allocator,
* return 1GB by default.
*/
int bh_memory_pool_size();

#if BEIHAI_ENABLE_MEMORY_PROFILING == 0

/**
* This function allocates a memory chunk from system
*
* @param size bytes need allocate
*
* @return the pointer to memory allocated
*/
void* bh_malloc(unsigned int size);

/**
* This function frees memory chunk
*
* @param ptr the pointer to memory need free
*/
void bh_free(void *ptr);

#else

void* bh_malloc_profile(const char *file, int line, const char *func, unsigned int size);
void bh_free_profile(const char *file, int line, const char *func, void *ptr);

#define bh_malloc(size) bh_malloc_profile(__FILE__, __LINE__, __func__, size)
#define bh_free(ptr) bh_free_profile(__FILE__, __LINE__, __func__, ptr)

/**
* Print current memory profiling data
*
* @param file file name of the caller
* @param line line of the file of the caller
* @param func function name of the caller
*/
void memory_profile_print(const char *file, int line, const char *func, int alloc);

/**
* Summarize memory usage and print it out
* Can use awk to analyze the output like below:
* awk -F: '{print $2,$4,$6,$8,$9}' OFS="\t" ./out.txt | sort -n -r -k 1
*/
void memory_usage_summarize();

#endif

#ifdef __cplusplus
}
#endif

#endif /* #ifndef _BH_MEMORY_H */

7 changes: 6 additions & 1 deletion core/iwasm/runtime/vmcore-wasm/wasm_application.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,15 @@ wasm_application_execute_func(WASMModuleInstance *module_inst,
break;
case VALUE_TYPE_I64:
{
char buf[16];
union { uint64 val; uint32 parts[2]; } u;
u.parts[0] = argv1[0];
u.parts[1] = argv1[1];
wasm_printf("0x%llx:i64", u.val);
if (sizeof(long) == 4)
snprintf(buf, sizeof(buf), "%s", "0x%llx:i64");
else
snprintf(buf, sizeof(buf), "%s", "0x%lx:i64");
wasm_printf(buf, u.val);
break;
}
case VALUE_TYPE_F32:
Expand Down
Loading