1
0
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:
Karl Fessel 2021-09-23 00:17:43 +02:00
parent 4cc9bd9e4e
commit df057e09cf
5 changed files with 173 additions and 97 deletions

View File

@ -9,13 +9,14 @@ USEPKG += wamr
USEMODULE += xtimer
USEMODULE += sema
USEMODULE += event
export WAMR_CONFIG := $(abspath config.cmake)
WPEDANTIC := 0
WERROR := 0
BLOBS += main.wasm
BLOBS += main.wasm hello.wasm
# Comment this out to disable code in RIOT that does safety checking
# 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:
QUIET ?= 1
######################################################
# Load the rest of the usual RIOT make infastructure #
######################################################
#######################################################
# Load the rest of the usual RIOT make infrastructure #
#######################################################
include $(RIOTBASE)/Makefile.include

View File

@ -4,5 +4,3 @@ The source for main.wasm can be found in
<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

View File

@ -8,6 +8,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
@ -18,18 +19,16 @@
#include <thread.h>
/*provide some test program*/
#include "blob/main.wasm.h"
#define DEFAULT_THREAD_STACKSIZE (6 * 1024)
#define DEFAULT_THREAD_PRIORITY 50
static int app_argc;
static char **app_argv;
/* execs the main function in an instantiated module */
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;
int ret = 0;
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)
{
wasm_module_inst_t wasm_module_inst = NULL;
char error_buf[128];
int ret = 0;
/* instantiate the module */
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, 8 * 1024,
8 * 1024, error_buf, sizeof(error_buf)))) {
puts(error_buf);
return -1;
{ /* instantiate the module */
char error_buf[128];
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, 8 * 1024,
8 * 1024, error_buf, sizeof(error_buf)))) {
puts(error_buf);
return -1;
}
}
ret = iwasm_instance_exec_main(wasm_module_inst, argc, argv);
/* 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*/
int iwasm_bytecode_exec_main(void *bytecode, size_t bytecode_len, int argc, char **argv){
// (uint8_t *) bytecode;
int iwasm_bytecode_exec_main(uint8_t * bytecode, size_t bytecode_len, int argc, char **argv){
int ret = 0;
wasm_module_t wasm_module = NULL;
{ /* load WASM module */
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)))) {
puts(error_buf);
return -1;
@ -116,27 +115,20 @@ bool iwasm_runtime_init(void)
return true;
}
#include "event.h"
event_queue_t iwasm_q = EVENT_QUEUE_INIT_DETACHED;
void * iwasm_thread(void *arg1)
{
(void) arg1; /*unused*/
uint8_t *wasm_buf = NULL;
unsigned wasm_buf_size = 0;
if(!iwasm_runtime_init())
return (void *)-1;
/* we need the bytecode to be writable while loading
* loading adds size information to stack POPs */
//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;
event_queue_claim(&iwasm_q);
event_loop(&iwasm_q);
iwasm_bytecode_exec_main(wasm_buf, wasm_buf_size, app_argc, app_argv);
free(wasm_buf);
/*next lines should not be reached*/
wasm_runtime_destroy();
return NULL;
}
@ -159,20 +151,77 @@ bool iwasm_start_thread(void)
.arg = NULL,
.name = "simple_wamr"
};
event_queue_init_detached(&iwasm_q);
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);
return tpid != 0 ? true : false;;
}
/* 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){
return iwasm_bytecode_exec_main(bytecode, bytecode_len, argc, argv);
#include "mutex.h"
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")
int main(void)
{
printf("iwasm_thread_initilised: %s\n",telltruth(iwasm_start_thread()));
/* 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){
run_bytecode_event_t * e = malloc(sizeof(run_bytecode_event_t));
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
View 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);
}

View File

@ -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 =\
# -nostartfiles \
# --nostdlib \
# --nostdinc \
# --print-map\
# --initial-memory= \
# --allow-undefined-file=<value>
# --initial-memory=65536
ifneq ($(shell (command -v wasm-ld)),)
WASM-LD ?= wasm-ld
else
ifneq ($(shell (command -v wasm-ld-11)),)
LLVM_VERSION = 11
else
ifneq ($(shell (command -v wasm-ld-10)),)
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)
# --export-dynamic Put symbols in the dynamic symbol table
# --export-table Export function table to the environment
# --export=<value> Force a symbol to be exported
# --fatal-warnings Treat warnings as errors
# --import-memory Import memory from the environment
# --import-table Import function table from the environment
# --initial-memory=<value>
# 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
ifneq ($(LLVM_VERSION),)
CLANG ?= clang-$(LLVM_VERSION)
CLANGPP ?= clang++-$(LLVM_VERSION)
WASM-LD ?= wasm-ld-$(LLVM_VERSION)
else
CLANG ?= clang
CLANGPP ?= clang++
endif
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 \
--export-dynamic \
--no-entry \
--strip-all \
--export-dynamic \
-error-limit=0 \
--lto-O3 \
-O3 \
--gc-sections\
-z stack-size=4096 \
--initial-memory=65536 \
#COMPILE_FLAGS_mov=\
-Wl,--allow-undefined\
-Wl,--strip-debug \
-nostartfiles \
-std=c++14 \
# --initial-memory may only be set in 64kB steps (pagesize of WASM)
# even though this one page is 64kB
# - data starts at 0, (1024 is chosen by lld)
# - stack starts at 4kB down
# - 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 \
--target=wasm32-unknown-unknown-wasm \
@ -74,22 +65,28 @@ COMPILE_FLAGS = -Wall \
-ffunction-sections \
-fdata-sections \
#one might consider adding these
# -nostartfiles \
# --nostdlib \
# --nostdinc \
# -std=c++14 \
%.show: %.wasm
wasm2wat $<
%.wasm: %.o Makefile
wasm-ld-$(LLVM_VERSION) -o $@ $(LINK_FLAGS) $<
$(WASM-LD) -o $@ $(LINK_FLAGS) $<
%.o: %.cpp Makefile FORCE
clang++-$(LLVM_VERSION) \
$(CLANGPP) \
-c \
$(COMPILE_FLAGS) \
-o $@ \
$<
%.o: %.c Makefile FORCE
clang-$(LLVM_VERSION) \
$(CLANG)\
-c \
$(COMPILE_FLAGS) \
-o $@ \