feat: add wasm sample program
This commit is contained in:
parent
a6578ea92c
commit
989f985d12
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,2 +1,5 @@
|
||||
*.so
|
||||
cmake-build-*
|
||||
*.wasm
|
||||
/.idea
|
||||
/cmake-build-*
|
||||
/wasm-micro-runtime
|
||||
|
31
main.c
31
main.c
@ -5,18 +5,45 @@
|
||||
#include "wrt/dl.h"
|
||||
#include "wrt/wrt.h"
|
||||
|
||||
uint8_t *read_file(const char *filename, uint32_t *fsize) {
|
||||
FILE *f = fopen(filename, "rb");
|
||||
if (f == NULL) {
|
||||
LOG_ERR("read_file: fopen failed");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
*fsize = ftell(f);
|
||||
fseek(f, 0, SEEK_SET); // same as rewind(f);
|
||||
|
||||
uint8_t *buffer = malloc(*fsize + 1);
|
||||
fread(buffer, *fsize, 1, f);
|
||||
fclose(f);
|
||||
|
||||
buffer[*fsize] = 0;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void test_wrt() {
|
||||
uint32_t buf_size;
|
||||
uint8_t *buf = read_file("wasm_sample.wasm", &buf_size);
|
||||
|
||||
WRTContext context;
|
||||
wrt_platform_init(&context, malloc, free);
|
||||
wrt_mem_init(&context, 512 * 1024, 128);
|
||||
wrt_program_init(&context, NULL, 0, NULL, 0);
|
||||
wrt_program_init(&context, buf, buf_size, NULL, 0);
|
||||
wrt_wamr_init(&context, "entry", 8092, 8092);
|
||||
wrt_run(&context);
|
||||
wrt_free(&context);
|
||||
|
||||
free(buf);
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Hello, World!\n");
|
||||
LOG_INFO("WRT Test");
|
||||
|
||||
test_wrt();
|
||||
|
||||
return 0;
|
||||
}
|
15
tests/wasm/build.sh
Executable file
15
tests/wasm/build.sh
Executable file
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
WAMR_DIR=../../wasm-micro-runtime
|
||||
PROG_SRCS=sample.c
|
||||
BIN_FILE=wasm_sample.wasm
|
||||
|
||||
clang \
|
||||
--target=wasm32 -O3 -z stack-size=4096 -Wl,--initial-memory=65536 \
|
||||
--sysroot=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot \
|
||||
-Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt \
|
||||
-Wl,--export=entry \
|
||||
-Wl,--strip-all,--no-entry -nostdlib \
|
||||
-Wl,--export=calculate -Wl,--allow-undefined \
|
||||
-o $BIN_FILE \
|
||||
$PROG_SRCS
|
23
tests/wasm/sample.c
Normal file
23
tests/wasm/sample.c
Normal file
@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
|
||||
typedef enum {
|
||||
WRT_OK = 0,
|
||||
WRT_ERROR = -1,
|
||||
} Status;
|
||||
|
||||
int dl_open(const char *filename, int flags);
|
||||
int dl_sym(int handle, const char *symbol, const char *signature);
|
||||
Status dl_close(int handle);
|
||||
|
||||
unsigned fib(unsigned n) { return n < 2 ? n : fib(n - 1) + fib(n - 2); }
|
||||
|
||||
void entry() {
|
||||
// for (unsigned i = 0; i <= 35; i++) printf("fib(%2u) = %u\n", i, fib(i));
|
||||
|
||||
int hnd = dl_open("./SimpleLib.so", 1);
|
||||
printf("hnd = %d\n", hnd);
|
||||
int fib_iterate_sym = dl_sym(hnd, "fib_iterate", "(i)i");
|
||||
printf("fib_iterate_sym = %d\n", fib_iterate_sym);
|
||||
Status status = dl_close(hnd);
|
||||
printf("status = %d\n", status);
|
||||
}
|
@ -12,7 +12,7 @@
|
||||
|
||||
#define _LOG(color, level, fmt, ...) \
|
||||
do { \
|
||||
fprintf(stdout, color "[" level "]\t(%s:%d):\t" fmt __COLOR_RESET "\n", __FILE__, \
|
||||
fprintf(stdout, color "[" level "] (%s:%d): " fmt __COLOR_RESET "\n", __FILE__, \
|
||||
__LINE__, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
|
4
wrt/dl.c
4
wrt/dl.c
@ -30,6 +30,7 @@ int dl_open(wasm_exec_env_t exec_env, const char *filename, int flags) {
|
||||
for (int i = 0; i < DL_MAX_HANDLES; i++) {
|
||||
if (dl_context.hnd[i].handle == NULL) {
|
||||
dl_context.hnd[i].handle = handle;
|
||||
LOG_DBG("dl_open: lib = %s, flags = %d, handle = %d", filename, flags, i + 1);
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
@ -176,6 +177,7 @@ int dl_sym(wasm_exec_env_t exec_env, int handle, const char *symbol, const char
|
||||
dl_context.sym[i].backref = handle;
|
||||
dl_context.sym[i].symbol = sym;
|
||||
dl_context.sym[i].cif = cif;
|
||||
LOG_DBG("dl_sym: lib_hnd = %d, sym = %s, sym_id = %d", handle, symbol, i + 1);
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
@ -231,6 +233,8 @@ Status dl_close(wasm_exec_env_t exec_env, int handle) {
|
||||
dl_free(dl_context.sym[i].cif->arg_types);
|
||||
dl_free(dl_context.sym[i].cif);
|
||||
dl_context.sym[i].cif = NULL;
|
||||
|
||||
LOG_DBG("dl_close: destroy sym_id = %d", i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,8 +77,8 @@ Status wrt_wamr_init(WRTContext *context, char *entry_func, uint32_t stack_size,
|
||||
|
||||
// register dl functions
|
||||
static NativeSymbol native_symbols[] = {
|
||||
EXPORT_WASM_API_WITH_SIG(dl_open, "(si)i"),
|
||||
EXPORT_WASM_API_WITH_SIG(dl_sym, "(iss)i"),
|
||||
EXPORT_WASM_API_WITH_SIG(dl_open, "($i)i"),
|
||||
EXPORT_WASM_API_WITH_SIG(dl_sym, "(i$$)i"),
|
||||
EXPORT_WASM_API_WITH_SIG(dl_call, "(i)i"), // TODO
|
||||
EXPORT_WASM_API_WITH_SIG(dl_close, "(i)i"),
|
||||
};
|
||||
|
Reference in New Issue
Block a user