mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
example/wasm: split example, eventise and introduce wamr_run
This commit is contained in:
parent
4cc9bd9e4e
commit
df057e09cf
@ -9,13 +9,14 @@ USEPKG += wamr
|
|||||||
|
|
||||||
USEMODULE += xtimer
|
USEMODULE += xtimer
|
||||||
USEMODULE += sema
|
USEMODULE += sema
|
||||||
|
USEMODULE += event
|
||||||
|
|
||||||
export WAMR_CONFIG := $(abspath config.cmake)
|
export WAMR_CONFIG := $(abspath config.cmake)
|
||||||
|
|
||||||
WPEDANTIC := 0
|
WPEDANTIC := 0
|
||||||
WERROR := 0
|
WERROR := 0
|
||||||
|
|
||||||
BLOBS += main.wasm
|
BLOBS += main.wasm hello.wasm
|
||||||
|
|
||||||
# Comment this out to disable code in RIOT that does safety checking
|
# Comment this out to disable code in RIOT that does safety checking
|
||||||
# which is not needed in a production environment but helps in the
|
# which is not needed in a production environment but helps in the
|
||||||
@ -25,8 +26,8 @@ DEVELHELP ?= 1
|
|||||||
# Change this to 0 show compiler invocation lines by default:
|
# Change this to 0 show compiler invocation lines by default:
|
||||||
QUIET ?= 1
|
QUIET ?= 1
|
||||||
|
|
||||||
######################################################
|
#######################################################
|
||||||
# Load the rest of the usual RIOT make infastructure #
|
# Load the rest of the usual RIOT make infrastructure #
|
||||||
######################################################
|
#######################################################
|
||||||
|
|
||||||
include $(RIOTBASE)/Makefile.include
|
include $(RIOTBASE)/Makefile.include
|
||||||
|
@ -4,5 +4,3 @@ The source for main.wasm can be found in
|
|||||||
<RIOT>/build/pkg/wamr/product-mini/app-samples/hello-world/
|
<RIOT>/build/pkg/wamr/product-mini/app-samples/hello-world/
|
||||||
|
|
||||||
https://github.com/bytecodealliance/wasm-micro-runtime/tree/main/product-mini/app-samples/hello-world
|
https://github.com/bytecodealliance/wasm-micro-runtime/tree/main/product-mini/app-samples/hello-world
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@ -18,18 +19,16 @@
|
|||||||
|
|
||||||
#include <thread.h>
|
#include <thread.h>
|
||||||
|
|
||||||
/*provide some test program*/
|
|
||||||
#include "blob/main.wasm.h"
|
|
||||||
|
|
||||||
#define DEFAULT_THREAD_STACKSIZE (6 * 1024)
|
#define DEFAULT_THREAD_STACKSIZE (6 * 1024)
|
||||||
#define DEFAULT_THREAD_PRIORITY 50
|
#define DEFAULT_THREAD_PRIORITY 50
|
||||||
|
|
||||||
static int app_argc;
|
|
||||||
static char **app_argv;
|
|
||||||
|
|
||||||
/* execs the main function in an instantiated module */
|
/* execs the main function in an instantiated module */
|
||||||
static int iwasm_instance_exec_main(wasm_module_inst_t module_inst, int argc, char **argv)
|
static int iwasm_instance_exec_main(wasm_module_inst_t module_inst, int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
/* exception memory is part of instance -> no buffer need */
|
||||||
const char *exception = 0;
|
const char *exception = 0;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
wasm_application_execute_main(module_inst, argc, argv);
|
wasm_application_execute_main(module_inst, argc, argv);
|
||||||
@ -45,14 +44,15 @@ static int iwasm_instance_exec_main(wasm_module_inst_t module_inst, int argc, ch
|
|||||||
int iwasm_module_exec_main(wasm_module_t wasm_module , int argc, char **argv)
|
int iwasm_module_exec_main(wasm_module_t wasm_module , int argc, char **argv)
|
||||||
{
|
{
|
||||||
wasm_module_inst_t wasm_module_inst = NULL;
|
wasm_module_inst_t wasm_module_inst = NULL;
|
||||||
char error_buf[128];
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
/* instantiate the module */
|
{ /* instantiate the module */
|
||||||
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, 8 * 1024,
|
char error_buf[128];
|
||||||
8 * 1024, error_buf, sizeof(error_buf)))) {
|
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, 8 * 1024,
|
||||||
puts(error_buf);
|
8 * 1024, error_buf, sizeof(error_buf)))) {
|
||||||
return -1;
|
puts(error_buf);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ret = iwasm_instance_exec_main(wasm_module_inst, argc, argv);
|
ret = iwasm_instance_exec_main(wasm_module_inst, argc, argv);
|
||||||
/* destroy the module instance */
|
/* destroy the module instance */
|
||||||
@ -61,14 +61,13 @@ int iwasm_module_exec_main(wasm_module_t wasm_module , int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* bytecode needs to be writeable*/
|
/* bytecode needs to be writeable*/
|
||||||
int iwasm_bytecode_exec_main(void *bytecode, size_t bytecode_len, int argc, char **argv){
|
int iwasm_bytecode_exec_main(uint8_t * bytecode, size_t bytecode_len, int argc, char **argv){
|
||||||
// (uint8_t *) bytecode;
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
wasm_module_t wasm_module = NULL;
|
wasm_module_t wasm_module = NULL;
|
||||||
|
|
||||||
{ /* load WASM module */
|
{ /* load WASM module */
|
||||||
char error_buf[128];
|
char error_buf[128];
|
||||||
if (!(wasm_module = wasm_runtime_load((uint8_t * )bytecode, bytecode_len,
|
if (!(wasm_module = wasm_runtime_load(bytecode, bytecode_len,
|
||||||
error_buf, sizeof(error_buf)))) {
|
error_buf, sizeof(error_buf)))) {
|
||||||
puts(error_buf);
|
puts(error_buf);
|
||||||
return -1;
|
return -1;
|
||||||
@ -116,27 +115,20 @@ bool iwasm_runtime_init(void)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "event.h"
|
||||||
|
event_queue_t iwasm_q = EVENT_QUEUE_INIT_DETACHED;
|
||||||
|
|
||||||
void * iwasm_thread(void *arg1)
|
void * iwasm_thread(void *arg1)
|
||||||
{
|
{
|
||||||
(void) arg1; /*unused*/
|
(void) arg1; /*unused*/
|
||||||
uint8_t *wasm_buf = NULL;
|
|
||||||
unsigned wasm_buf_size = 0;
|
|
||||||
|
|
||||||
if(!iwasm_runtime_init())
|
if(!iwasm_runtime_init())
|
||||||
return (void *)-1;
|
return (void *)-1;
|
||||||
|
|
||||||
/* we need the bytecode to be writable while loading
|
event_queue_claim(&iwasm_q);
|
||||||
* loading adds size information to stack POPs */
|
event_loop(&iwasm_q);
|
||||||
//static uint8_t wasm_test_file[] = main_wasm;
|
|
||||||
wasm_buf = malloc(main_wasm_len);
|
|
||||||
memcpy(wasm_buf, main_wasm, main_wasm_len);
|
|
||||||
/* no copy need if architecture has const in writable mem */
|
|
||||||
/* wasm_buf = (uint8_t *) main_wasm; */
|
|
||||||
wasm_buf_size = main_wasm_len;
|
|
||||||
|
|
||||||
iwasm_bytecode_exec_main(wasm_buf, wasm_buf_size, app_argc, app_argv);
|
/*next lines should not be reached*/
|
||||||
|
|
||||||
free(wasm_buf);
|
|
||||||
wasm_runtime_destroy();
|
wasm_runtime_destroy();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -159,20 +151,77 @@ bool iwasm_start_thread(void)
|
|||||||
.arg = NULL,
|
.arg = NULL,
|
||||||
.name = "simple_wamr"
|
.name = "simple_wamr"
|
||||||
};
|
};
|
||||||
|
event_queue_init_detached(&iwasm_q);
|
||||||
b.stack=malloc(b.stacksize);
|
b.stack=malloc(b.stacksize);
|
||||||
kernel_pid_t tpid = thread_create (b.stack, b.stacksize, b.priority, b.flags, b.task_func, b.arg, b.name);
|
kernel_pid_t tpid = thread_create (b.stack, b.stacksize, b.priority, b.flags, b.task_func, b.arg, b.name);
|
||||||
|
|
||||||
return tpid != 0 ? true : false;;
|
return tpid != 0 ? true : false;;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* this seems to be a very direct interpretation of i like to have a wamr_run */
|
#include "mutex.h"
|
||||||
int wamr_run(void *bytecode, size_t bytecode_len, int argc, char **argv){
|
|
||||||
return iwasm_bytecode_exec_main(bytecode, bytecode_len, argc, argv);
|
typedef struct run_bytecode_event { event_t super;
|
||||||
|
void *bytecode;
|
||||||
|
size_t bytecode_len;
|
||||||
|
int argc;
|
||||||
|
char **argv;
|
||||||
|
mutex_t finish;
|
||||||
|
} run_bytecode_event_t;
|
||||||
|
|
||||||
|
void _wamr_run_call(event_t *event){
|
||||||
|
run_bytecode_event_t * e = (run_bytecode_event_t *) event;
|
||||||
|
e->argc = iwasm_bytecode_exec_main((uint8_t * )e->bytecode, e->bytecode_len, e->argc, e->argv);
|
||||||
|
mutex_unlock(&e->finish);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define telltruth(X) ((X) ? "true" : "false")
|
/* this seems to be a very direct interpretation of "i like to have a wamr_run" */
|
||||||
int main(void)
|
int wamr_run(void *bytecode, size_t bytecode_len, int argc, char **argv){
|
||||||
{
|
run_bytecode_event_t * e = malloc(sizeof(run_bytecode_event_t));
|
||||||
printf("iwasm_thread_initilised: %s\n",telltruth(iwasm_start_thread()));
|
if (!e) return -1;
|
||||||
|
|
||||||
|
*e = (run_bytecode_event_t){ .super.handler = _wamr_run_call,
|
||||||
|
.bytecode = bytecode,
|
||||||
|
.bytecode_len = bytecode_len,
|
||||||
|
.argc = argc, .argv = argv,
|
||||||
|
.finish = MUTEX_INIT_LOCKED};
|
||||||
|
event_post(&iwasm_q, (event_t*) e);
|
||||||
|
|
||||||
|
mutex_lock(&e->finish);
|
||||||
|
|
||||||
|
int ret = e->argc;
|
||||||
|
free(e);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int wamr_run_cp(const void *bytecode, size_t bytecode_len, int argc, char *argv[]){
|
||||||
|
/* we need the bytecode to be writable while loading
|
||||||
|
* loading adds size information to stack POPs */
|
||||||
|
uint8_t * wasm_buf = malloc(bytecode_len);
|
||||||
|
if (!wasm_buf) return -1;
|
||||||
|
|
||||||
|
memcpy(wasm_buf, bytecode, bytecode_len);
|
||||||
|
/* no copy need if architecture has const in writable mem */
|
||||||
|
/* wasm_buf = (uint8_t *) main_wasm; */
|
||||||
|
|
||||||
|
unsigned wasm_buf_size = bytecode_len;
|
||||||
|
|
||||||
|
/* iwasm uses argv[0] casted to an int to store mains return value */
|
||||||
|
static char * empty = "";
|
||||||
|
char ** parv;
|
||||||
|
if(argc > 0){
|
||||||
|
parv = malloc(sizeof(argv[0]) * argc);
|
||||||
|
if (!parv) return -1;
|
||||||
|
memcpy(parv, argv, sizeof(argv[0]) * argc);
|
||||||
|
}else{
|
||||||
|
argc = 1;
|
||||||
|
parv = malloc(sizeof(argv[0]) * argc);
|
||||||
|
if (!parv) return -1;
|
||||||
|
parv[0] = empty;
|
||||||
|
}
|
||||||
|
int ret = wamr_run(wasm_buf, wasm_buf_size, argc, parv);
|
||||||
|
free(parv);
|
||||||
|
free(wasm_buf);
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
31
examples/wasm/wasm-main.c
Normal file
31
examples/wasm/wasm-main.c
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/*provide some test program*/
|
||||||
|
#include "blob/main.wasm.h"
|
||||||
|
#include "blob/hello.wasm.h"
|
||||||
|
|
||||||
|
bool iwasm_start_thread(void);
|
||||||
|
/* this seems to be a very direct interpretation of "i like to have a wamr_run" */
|
||||||
|
int wamr_run(void *bytecode, size_t bytecode_len, int argc, char **argv);
|
||||||
|
/* this creates a copy bytecode and argv
|
||||||
|
* if argc is 0 it is set to 1 and argv[0] is set to ""
|
||||||
|
* to create some space for a return value */
|
||||||
|
int wamr_run_cp(const void *bytecode, size_t bytecode_len, int argc, const char **argv);
|
||||||
|
|
||||||
|
#define telltruth(X) ((X) ? "true" : "false")
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
printf("iwasm_thread_initilised: %s\n", telltruth(iwasm_start_thread()));
|
||||||
|
|
||||||
|
int app_argc = 0;
|
||||||
|
const char *app_argv1 = "test";
|
||||||
|
const char **app_argv = 0;
|
||||||
|
int ret = wamr_run_cp(main_wasm, main_wasm_len, app_argc, app_argv);
|
||||||
|
printf("ret = %d\n", ret);
|
||||||
|
ret = wamr_run_cp(hello_wasm, hello_wasm_len, app_argc, app_argv);
|
||||||
|
printf("ret = %d\n", ret);
|
||||||
|
}
|
@ -1,69 +1,60 @@
|
|||||||
LLVM_VERSION = 11
|
#sometimes there might not be a wasm-ld (Ubuntu:focal)
|
||||||
|
#lets check if we can find a specific version
|
||||||
|
|
||||||
#LINK_FLAGS_bak =\
|
ifneq ($(shell (command -v wasm-ld)),)
|
||||||
# -nostartfiles \
|
WASM-LD ?= wasm-ld
|
||||||
# --nostdlib \
|
else
|
||||||
# --nostdinc \
|
ifneq ($(shell (command -v wasm-ld-11)),)
|
||||||
# --print-map\
|
LLVM_VERSION = 11
|
||||||
# --initial-memory= \
|
else
|
||||||
# --allow-undefined-file=<value>
|
ifneq ($(shell (command -v wasm-ld-10)),)
|
||||||
# --initial-memory=65536
|
LLVM_VERSION = 10
|
||||||
|
else
|
||||||
|
ifneq ($(shell (command -v wasm-ld-9)),)
|
||||||
|
LLVM_VERSION = 9
|
||||||
|
else
|
||||||
|
ifneq ($(shell (command -v wasm-ld-8)),)
|
||||||
|
LLVM_VERSION = 8
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
# --export-all Export all symbols (normally combined with --no-gc-sections)
|
ifneq ($(LLVM_VERSION),)
|
||||||
# --export-dynamic Put symbols in the dynamic symbol table
|
CLANG ?= clang-$(LLVM_VERSION)
|
||||||
# --export-table Export function table to the environment
|
CLANGPP ?= clang++-$(LLVM_VERSION)
|
||||||
# --export=<value> Force a symbol to be exported
|
WASM-LD ?= wasm-ld-$(LLVM_VERSION)
|
||||||
# --fatal-warnings Treat warnings as errors
|
else
|
||||||
# --import-memory Import memory from the environment
|
CLANG ?= clang
|
||||||
# --import-table Import function table from the environment
|
CLANGPP ?= clang++
|
||||||
# --initial-memory=<value>
|
endif
|
||||||
# Initial size of the linear memory
|
|
||||||
# --lto-O<opt-level> Optimization level for LTO
|
|
||||||
# --lto-partitions=<value>
|
|
||||||
# Number of LTO codegen partitions
|
|
||||||
# -L <dir> Add a directory to the library search path
|
|
||||||
# -l <libName> Root name of library to use
|
|
||||||
# --max-memory=<value> Maximum size of the linear memory
|
|
||||||
# --merge-data-segments Enable merging data segments
|
|
||||||
# --mllvm <value> Options to pass to LLVM
|
|
||||||
# --pie Create a position independent executable
|
|
||||||
# --print-gc-sections List removed unused sections
|
|
||||||
# --relocatable Create relocatable object file
|
|
||||||
# --shared-memory Use shared linear memory
|
|
||||||
# --shared Build a shared object
|
|
||||||
# --stack-first Place stack at start of linear memory rather than after data
|
|
||||||
# --strip-all Strip all symbols
|
|
||||||
# --strip-debug Strip debugging information
|
|
||||||
# -S Alias for --strip-debug
|
|
||||||
# -s Alias for --strip-all
|
|
||||||
# --thinlto-cache-dir=<value>
|
|
||||||
# Path to ThinLTO cached object file directory
|
|
||||||
# --thinlto-cache-policy=<value>
|
|
||||||
# Pruning policy for the ThinLTO cache
|
|
||||||
# --thinlto-jobs=<value> Number of ThinLTO jobs
|
|
||||||
# --threads Run the linker multi-threaded
|
|
||||||
# --undefined=<value> Force undefined symbol during linking
|
|
||||||
# --verbose Verbose mode
|
|
||||||
# --version Display the version number and exit
|
|
||||||
# -z <option> Linker option extensions
|
|
||||||
|
|
||||||
|
WASM-LD ?= echo !! NO wasm-ld(-VERSION) found !!; false
|
||||||
|
|
||||||
LINK_FLAGS = --export main \
|
LINK_FLAGS = -z stack-size=4096 \
|
||||||
|
--export main \
|
||||||
|
--export=__heap_base \
|
||||||
|
--export=__data_end \
|
||||||
--allow-undefined \
|
--allow-undefined \
|
||||||
--export-dynamic \
|
|
||||||
--no-entry \
|
--no-entry \
|
||||||
--strip-all \
|
--strip-all \
|
||||||
|
--export-dynamic \
|
||||||
-error-limit=0 \
|
-error-limit=0 \
|
||||||
--lto-O3 \
|
--lto-O3 \
|
||||||
-O3 \
|
-O3 \
|
||||||
--gc-sections\
|
--gc-sections\
|
||||||
-z stack-size=4096 \
|
--initial-memory=65536 \
|
||||||
|
|
||||||
#COMPILE_FLAGS_mov=\
|
# --initial-memory may only be set in 64kB steps (pagesize of WASM)
|
||||||
-Wl,--allow-undefined\
|
# even though this one page is 64kB
|
||||||
-Wl,--strip-debug \
|
# - data starts at 0, (1024 is chosen by lld)
|
||||||
-nostartfiles \
|
# - stack starts at 4kB down
|
||||||
-std=c++14 \
|
# - heap at 4kB up (see stack-size option)
|
||||||
|
# -> memory can be smaller than first page
|
||||||
|
# without stack-size option stack will start at 64kB
|
||||||
|
# -> heap needs a second page
|
||||||
|
# wasm-ld 8 and 11 do not need --initial-memory=64kB
|
||||||
|
|
||||||
COMPILE_FLAGS = -Wall \
|
COMPILE_FLAGS = -Wall \
|
||||||
--target=wasm32-unknown-unknown-wasm \
|
--target=wasm32-unknown-unknown-wasm \
|
||||||
@ -74,22 +65,28 @@ COMPILE_FLAGS = -Wall \
|
|||||||
-ffunction-sections \
|
-ffunction-sections \
|
||||||
-fdata-sections \
|
-fdata-sections \
|
||||||
|
|
||||||
|
#one might consider adding these
|
||||||
|
# -nostartfiles \
|
||||||
|
# --nostdlib \
|
||||||
|
# --nostdinc \
|
||||||
|
# -std=c++14 \
|
||||||
|
|
||||||
%.show: %.wasm
|
%.show: %.wasm
|
||||||
wasm2wat $<
|
wasm2wat $<
|
||||||
|
|
||||||
%.wasm: %.o Makefile
|
%.wasm: %.o Makefile
|
||||||
wasm-ld-$(LLVM_VERSION) -o $@ $(LINK_FLAGS) $<
|
$(WASM-LD) -o $@ $(LINK_FLAGS) $<
|
||||||
|
|
||||||
|
|
||||||
%.o: %.cpp Makefile FORCE
|
%.o: %.cpp Makefile FORCE
|
||||||
clang++-$(LLVM_VERSION) \
|
$(CLANGPP) \
|
||||||
-c \
|
-c \
|
||||||
$(COMPILE_FLAGS) \
|
$(COMPILE_FLAGS) \
|
||||||
-o $@ \
|
-o $@ \
|
||||||
$<
|
$<
|
||||||
|
|
||||||
%.o: %.c Makefile FORCE
|
%.o: %.c Makefile FORCE
|
||||||
clang-$(LLVM_VERSION) \
|
$(CLANG)\
|
||||||
-c \
|
-c \
|
||||||
$(COMPILE_FLAGS) \
|
$(COMPILE_FLAGS) \
|
||||||
-o $@ \
|
-o $@ \
|
||||||
|
Loading…
Reference in New Issue
Block a user