Add a basic sample to show how native runtime invokes wasm apps in WAMR and how wasm apps invoke native functions. (#207)

* Add printingAdd print time for wamrc, fix posix mmap bug time for wamrc, fixed a posix mmap bug.

Change-Id: Ib6517b8a69cf022a1a6a74efa1f98155aec143bc

* Add a basic sample to show how native runtime invokes wasm app in WAMR, and how wasm app invokes native functions.

Change-Id: I700ae413ad5e9ea04540d5187952305e1ee92d73
This commit is contained in:
Shi Lei
2020-03-20 16:39:13 +08:00
committed by GitHub
parent b6cae54b54
commit 67495919b0
9 changed files with 460 additions and 6 deletions

178
samples/basic/src/main.c Normal file
View File

@ -0,0 +1,178 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_export.h"
#include "bh_read_file.h"
int intToStr(int x, char* str, int str_len, int digit);
int get_pow(int x, int y);
void print_usage(void)
{
fprintf(stdout, "Options:\r\n");
fprintf(stdout, " -f [path of wasm file] \n");
}
int main(int argc, char *argv_main[])
{
static char global_heap_buf[512 * 1024];
char *buffer, error_buf[128];
int opt;
char * wasm_path;
wasm_module_t module = NULL;
wasm_module_inst_t module_inst = NULL;
wasm_exec_env_t exec_env = NULL;
uint32 buf_size, stack_size = 8092, heap_size = 8092;
wasm_function_inst_t func = NULL;
wasm_function_inst_t func2 = NULL;
char * native_buffer = NULL;
int32_t wasm_buffer = 0;
RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(RuntimeInitArgs));
while ((opt = getopt(argc, argv_main, "hf:")) != -1)
{
switch (opt)
{
case 'f':
wasm_path = optarg;
break;
case 'h':
print_usage();
return 0;
case '?':
print_usage();
return 0;
}
}
if (optind == 1) {
print_usage();
return 0;
}
// Define an array of NativeSymbol for the APIs to be exported.
// Note: the array must be static defined since runtime
// will keep it after registration
// For the function signature specifications, goto the link:
// https://github.com/bytecodealliance/wasm-micro-runtime/blob/master/doc/export_native_api.md
static NativeSymbol native_symbols[] =
{
{
"intToStr", // the name of WASM function name
intToStr, // the native function pointer
"(i*~i)i" // the function prototype signature, avoid to use i32
},
{
"get_pow", // the name of WASM function name
get_pow, // the native function pointer
"(ii)i" // the function prototype signature, avoid to use i32
}
};
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
// Native symbols need below registration phase
init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
init_args.native_module_name = "env";
init_args.native_symbols = native_symbols;
if (!wasm_runtime_full_init(&init_args)) {
printf("Init runtime environment failed.\n");
return -1;
}
buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
if(!buffer) {
printf("Open wasm app file [%s] failed.\n", wasm_path);
goto fail;
}
module = wasm_runtime_load(buffer, buf_size, error_buf, sizeof(error_buf));
if(!module) {
printf("Load wasm module failed. error: %s\n", error_buf);
goto fail;
}
module_inst = wasm_runtime_instantiate(module,
stack_size,
heap_size,
error_buf,
sizeof(error_buf));
if(!module_inst) {
printf("Instantiate wasm module failed. error: %s\n", error_buf);
goto fail;
}
exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
if(!exec_env) {
printf("Create wasm execution environment failed.\n");
goto fail;
}
uint32 argv[4];
double arg_d = 0.000101;
argv[0] = 10;
// the second arg will occupy two array elements
memcpy(&argv[1], &arg_d, sizeof(arg_d));
*(float*)(argv+3) = 300.002;
if(!(func = wasm_runtime_lookup_function(module_inst, "generate_float", NULL))){
printf("The generate_float wasm function is not found.\n");
goto fail;
}
// pass 4 elements for function arguments
if (!wasm_runtime_call_wasm(exec_env, func, 4, argv) ) {
printf("call wasm function generate_float failed. %s\n", wasm_runtime_get_exception(module_inst));
goto fail;
}
float ret_val = *(float*)argv;
printf("Native finished calling wasm function generate_float(), returned a float value: %ff\n", ret_val );
// Next we will pass a buffer to the WASM function
uint32 argv2[4];
// must allocate buffer from wasm instance memory space (never use pointer from host runtime)
wasm_buffer = wasm_runtime_module_malloc(module_inst, 100, (void**)&native_buffer);
*(float*)argv2 = ret_val; // the first argument
argv2[1] = wasm_buffer; // the second argument is the wasm buffer address
argv2[2] = 100; // the third argument is the wasm buffer size
argv2[3] = 3; // the last argument is the digits after decimal point for converting float to string
if(!(func2 = wasm_runtime_lookup_function(module_inst, "float_to_string", NULL))){
printf("The wasm function float_to_string wasm function is not found.\n");
goto fail;
}
if (wasm_runtime_call_wasm(exec_env, func2, 4, argv2) ) {
printf("Native finished calling wasm function: float_to_string, returned a formatted string: %s\n", native_buffer);
}
else {
printf("call wasm function float_to_string failed. error: %s\n", wasm_runtime_get_exception(module_inst));
goto fail;
}
fail:
if(exec_env) wasm_runtime_destroy_exec_env(exec_env);
if(module_inst) {
if(wasm_buffer) wasm_runtime_module_free(module_inst, wasm_buffer);
wasm_runtime_deinstantiate(module_inst);
}
if(module) wasm_runtime_unload(module);
if(buffer) BH_FREE(buffer);
wasm_runtime_destroy();
return 0;
}

View File

@ -0,0 +1,65 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bh_platform.h"
#include "wasm_export.h"
#include "math.h"
// The first parameter is not exec_env because it is invoked by native funtions
void reverse(char * str, int len)
{
int i = 0, j = len - 1, temp;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
}
// The first parameter exec_env must be defined using type wasm_exec_env_t
// which is the calling convention for exporting native API by WAMR.
//
// Converts a given integer x to string str[].
// digit is the number of digits required in the output.
// If digit is more than the number of digits in x,
// then 0s are added at the beginning.
int intToStr(wasm_exec_env_t exec_env, int x, char* str, int str_len, int digit)
{
int i = 0;
printf ("calling into native function: %s\n", __FUNCTION__);
while (x) {
// native is responsible for checking the str_len overflow
if (i >= str_len) {
return -1;
}
str[i++] = (x % 10) + '0';
x = x / 10;
}
// If number of digits required is more, then
// add 0s at the beginning
while (i < digit) {
if (i >= str_len) {
return -1;
}
str[i++] = '0';
}
reverse(str, i);
if (i >= str_len)
return -1;
str[i] = '\0';
return i;
}
int get_pow(wasm_exec_env_t exec_env, int x, int y) {
printf ("calling into native function: %s\n", __FUNCTION__);
return (int)pow(x, y);
}