Compare commits

...

4 Commits

29 changed files with 3408 additions and 249 deletions

View File

@ -1,8 +1,8 @@
docker: docker:
docker build -t wasm-base wasm-base docker build -t wasm-base -f wasm-base.dockerfile . --build-arg CACHE_DATE="$(shell date)"
run: run:
docker run --rm -it wasm-base:latest /bin/bash docker run --rm -it wasm-base:latest /usr/bin/fish
run-external: run-external:
docker run --rm -it -v "./wasm-base/examples:/home/ubuntu/examples" wasm-base:latest /bin/bash docker run --rm -it -v "./examples:/home/ubuntu/examples" wasm-base:latest /usr/bin/fish

109
examples/Makefile Normal file
View File

@ -0,0 +1,109 @@
ARCH ?= bochs
BUILD_DIR := build-${ARCH}
FAIL_BIN ?= /home/fail/bin
FAIL_SERVER ?= ${FAIL_BIN}/generic-experiment-server
FAIL_TRACE ?= ${FAIL_BIN}/generic-tracing-client
FAIL_INJECT ?= ${FAIL_BIN}/generic-experiment-client
FAIL_DUMP ?= ${FAIL_BIN}/dump-trace
FAIL_IMPORT ?= ${FAIL_BIN}/import-trace --enable-sanitychecks
FAIL_PRUNE ?= ${FAIL_BIN}/prune-trace
BOCHS_RUNNER ?= ${FAIL_BIN}/bochs-experiment-runner.py
RESULT_BROWSER ?= ${FAIL_BIN}/resultbrowser.py
EXPERIMENTS := $(patsubst %.c,%,$(shell echo *.c))
include arch/${ARCH}.mk
$(foreach element,$(EXPERIMENTS),$(eval $(call arch-make-targets,$(element))))
all: help
help:
@echo "Small Playground for FAIL* Injections"
@echo "-------------------------------------"
@echo "Current Configuartion"
@echo " ARCH=${ARCH}"
clean:
rm -f *.wasm
rm -f *.aot
rm -f *_wasm.c
rm -f *.d
rm -f *.o
rm -f embed/*_host.c
rm -f *_host.elf
rm -rf ${BUILD_DIR}
clean-%:
rm -rf ${BUILD_DIR}/$(patsubst clean-%,%,$@)
contrib/clean-db '${ARCH}/$(patsubst clean-%,%,$@)'
build-%:
@echo "****************************************************************\n\
* The next step is to trace a golden run. The golden run executes the\n\
* system-under-test (SUT) within the emulator. A trace file is \n\
* produced and saved as: ${BUILD_DIR}/main/trace.pb\n\
*\n\
* $ make trace-$(patsubst build-%,%,$@)\n\
****************************************************************"
trace-%:
@echo "****************************************************************\n\
* The trace is now generated. It can be viewed with\n\
*\n\
* $ make dump-$(patsubst trace-%,%,$@)\n\
*\n\
* Next, we have to import the trace into the database\n\
*\n\
* $ make import-$(patsubst trace-%,%,$@)\n\
****************************************************************"
dump-%: ${BUILD_DIR}/%/trace.pb
${FAIL_DUMP} $(shell dirname $<)/trace.pb
${HOME}/.my.cnf:
@echo "[client]" > $@
@echo "user=fail" >> $@
@echo "database=fail" >> $@
@echo "password=fail" >> $@
@echo "host=db" >> $@
@echo "port=3306" >> $@
import-%: import-arch-%
@echo "****************************************************************\n\
* The golden run sits now within the MySQL database. If you are interested,\n\
* use the 'mysql' command to inspect the curent state of the DB. The tables\n\
* trace, fsppilot, and fspgroup are of special interest.\n\
*\n\
* Next, we have to run the campaign sever and the injection client\n\
*\n\
* $ make server-$(patsubst import-%,%,$@) &\n\
* $ make client-$(patsubst import-%,%,$@) \n\n\
* Afterwards, the results can be viewd with\n\
* $ make result-$(subst import-,,$@)\n\
****************************************************************"
server-%:
${FAIL_SERVER} -v ${ARCH}/$(subst server-,,$@) -b %
result-%:
@echo "select variant, benchmark, resulttype, sum(t.time2 - t.time1 + 1) as faults\
FROM variant v \
JOIN trace t ON v.id = t.variant_id \
JOIN fspgroup g ON g.variant_id = t.variant_id AND g.instr2 = t.instr2 AND g.data_address = t.data_address\
JOIN result_GenericExperimentMessage r ON r.pilot_id = g.pilot_id \
JOIN fsppilot p ON r.pilot_id = p.id \
WHERE v.variant = \"${ARCH}/$(patsubst result-%,%,$@)\"\
GROUP BY v.id, resulttype \
ORDER BY variant, benchmark, resulttype;" |mysql -t
resultbrowser:
${RESULT_BROWSER} --host=0.0.0.0 --port=5000
# Do never remove implicitly generated stuff
.SECONDARY:

View File

@ -1,3 +1,6 @@
# This makefile embeds an application compiled to WASM into a native C program.
# The WASM module will be loaded by the WASM runtime.
# Paths # Paths
WAMR_ROOT := /opt/wamr WAMR_ROOT := /opt/wamr
WAMRC := /opt/wamr-wamrc/wamrc WAMRC := /opt/wamr-wamrc/wamrc
@ -18,8 +21,8 @@ EMBED_INCL := -I$(WAMR_ROOT)/core/iwasm/include \
-I$(WAMR_ROOT)/core/shared/platform/linux \ -I$(WAMR_ROOT)/core/shared/platform/linux \
-I$(WAMR_ROOT)/core/shared/utils \ -I$(WAMR_ROOT)/core/shared/utils \
-I$(WAMR_ROOT)/core/shared/utils/uncommon -I$(WAMR_ROOT)/core/shared/utils/uncommon
EMBED_SOURCES := $(WAMR_ROOT)/core/shared/utils/uncommon/bh_read_file.c EMBED_SOURCES := $(WAMR_ROOT)/core/shared/utils/uncommon/bh_read_file.c # Not used when reading from buffer
EMBED_CFLAGS := -O0 -g -Wall $(EMBED_INCL) $(EMBED_SOURCES) EMBED_CFLAGS := -O0 -g -Wall $(EMBED_INCL)
AOT_LDFLAGS := -Wl,-rpath,$(WAMRC_LIB) AOT_LDFLAGS := -Wl,-rpath,$(WAMRC_LIB)
AOT_LDLIBS := -L$(WAMRC_LIB) -lvmlib -lm AOT_LDLIBS := -L$(WAMRC_LIB) -lvmlib -lm
INTER_LDFLAGS := -Wl,-rpath,$(IWASM_LIB) INTER_LDFLAGS := -Wl,-rpath,$(IWASM_LIB)
@ -33,7 +36,7 @@ CWASMS := $(SRCS:.c=_wasm.c)
AOTS := $(SRCS:.c=.aot) AOTS := $(SRCS:.c=.aot)
OBJS := $(SRCS:.c=.o) OBJS := $(SRCS:.c=.o)
DEPS := $(SRCS:.c=.d) DEPS := $(SRCS:.c=.d)
HOSTS := $(SRCS:.c=.host) HOSTS := $(SRCS:.c=_host.elf)
.PHONY: all build-wasms build-aots build-cwasms build-hosts clean .PHONY: all build-wasms build-aots build-cwasms build-hosts clean
@ -51,7 +54,6 @@ build-aots: $(AOTS)
%.aot: %.wasm %.aot: %.wasm
$(WAMRC) -o $@ $< $(WAMRC) -o $@ $<
# $(WAMRC) --enable-wasi -o $@ $<
# Convert the .aot files to C-style arrays (to embed them into the resulting host binary) # Convert the .aot files to C-style arrays (to embed them into the resulting host binary)
build-cwasms: $(CWASMS) build-cwasms: $(CWASMS)
@ -65,12 +67,12 @@ build-hosts: $(HOSTS)
# The C-style array is called %_aot, e.g. test_aot # The C-style array is called %_aot, e.g. test_aot
# We have to modify the host to refer to that with the correct name # We have to modify the host to refer to that with the correct name
%.host: %_wasm.c %_host.elf: %_wasm.c
cp embed/host.c embed/$*_host.c cp embed/host.c embed/$*_host.c
sed -i \ sed -i \
-e "s/__WASM_ARRAY_FILE__/$*_wasm.c/g" \ -e "s@__WASM_ARRAY_FILE__@../$*_wasm.c@g" \
-e "s/__WASM_ARRAY__/$*_aot/g" \ -e "s@__WASM_ARRAY__@$*_aot@g" \
-e "s/__WASM_ARRAY_LEN__/$*_aot_len/g" \ -e "s@__WASM_ARRAY_LEN__@$*_aot_len@g" \
embed/$*_host.c embed/$*_host.c
$(EMBED_CC) $(EMBED_CFLAGS) embed/$*_host.c -o $@ $(INTER_LDFLAGS) $(INTER_LDLIBS) $(EMBED_CC) $(EMBED_CFLAGS) embed/$*_host.c -o $@ $(INTER_LDFLAGS) $(INTER_LDLIBS)
@ -81,14 +83,4 @@ clean:
rm -f *.d rm -f *.d
rm -f *.o rm -f *.o
rm -f embed/*_host.c rm -f embed/*_host.c
rm -f *.host rm -f *_host.elf
# Compile to C-embedded
# embed: $(OBJS)
# $(EMBED_CC) $(OBJS) -o embed $(AOT_LDFLAGS) $(AOT_LDLIBS)
# %.o: %.c
# $(EMBED_CC) $(EMBED_CFLAGS) -o $@ $<
# embed_host:
# $(EMBED_CC) $(EMBED_CFLAGS) embed/host.c -o host $(INTER_LDFLAGS) $(INTER_LDLIBS)

154
examples/arch/bochs.mk Normal file
View File

@ -0,0 +1,154 @@
# C -> WASM
WASI_ROOT := /opt/wasi-sdk
WASI_CC := ${WASI_ROOT}/bin/clang
WASI_CFLAGS := --target=wasm32 \
--sysroot=${WASI_ROOT}/share/wasi-sysroot \
-z stack-size=4096 \
-nostdlib \
-O0 -g -Wall \
-Wl,--no-entry \
-Wl,--initial-memory=65536 \
-Wl,--export-all \
-Wl,--export=__heap_base \
-Wl,--export=__data_end
# WASM -> Baremetal
WAMR := /opt/wamr
IWASM_LIB := /opt/wamr-libiwasm
CC := gcc
CFLAGS := -I. -include arch/${ARCH}/lib.c -O0 -g -m32 -fomit-frame-pointer
LDFLAGS := -Wl,-T linker.ld $^ -Wl,--build-id=none -static -nostdlib -m32 \
-Wl,-rpath,${IWASM_LIB} -L${IWASM_LIB} -liwasm
INCL := -I${WAMR}/core/iwasm/include \
-I${WAMR}/core/iwasm/common \
-I${WAMR}/core/shared/platform/include \
-I${WAMR}/core/shared/platform/linux \
-I${WAMR}/core/shared/mem-alloc \
-I${WAMR}/core/shared/utils \
-I${WAMR}/core/shared/utils/uncommon
WAMRC := /opt/wamr-wamrc/wamrc
WAMRCFLAGS := --target=i386 --format=object
XXD := busybox xxd
WASM2C := wasm2c
################################################################
# C -> WASM
${BUILD_DIR}/%/module.wasm: %.c
mkdir -p $(shell dirname $@)
${WASI_CC} ${WASI_CFLAGS} $< -o $@
################################################################
# WASM -> Native Object File
# ${BUILD_DIR}/%/system.o: ${BUILD_DIR}/%/module.wasm
# ${WAMRC} ${WAMRCFLAGS} -o ${BUILD_DIR}/$*/system.o ${BUILD_DIR}/$*/module.wasm
#
# ${BUILD_DIR}/startup.o: arch/bochs/startup.s
# ${CC} $< ${CFLAGS} -c -ffunction-sections -o $@
#
# ${BUILD_DIR}/%/system.elf: ${BUILD_DIR}/%/system.o ${BUILD_DIR}/startup.o
# ${CC} ${LDFLAGS} -o $@
################################################################
# WASM -> AOT -> Loaded by Runtime
${BUILD_DIR}/%/module.aot: ${BUILD_DIR}/%/module.wasm
${WAMRC} -o $@ $<
${BUILD_DIR}/%/module_wasm.c: ${BUILD_DIR}/%/module.aot
${XXD} -i $< > $@
${BUILD_DIR}/%/system.o: ${BUILD_DIR}/%/module_wasm.c
cp embed/host.c ${BUILD_DIR}/$*/module_host.c
sed -i \
-e "s/__WASM_ARRAY_FILE__/module_wasm.c/g" \
-e "s/__WASM_ARRAY__/build_bochs_$*_module_aot/g" \
-e "s/__WASM_ARRAY_LEN__/build_bochs_$*_module_aot_len/g" \
${BUILD_DIR}/$*/module_host.c
${CC} ${CFLAGS} ${INCL} -c -ffunction-sections ${BUILD_DIR}/$*/module_host.c -o $@
${BUILD_DIR}/startup.o: arch/bochs/startup.s
${CC} $< ${CFLAGS} -c -ffunction-sections -o $@
${BUILD_DIR}/%/system.elf: ${BUILD_DIR}/%/system.o ${BUILD_DIR}/startup.o
${CC} ${LDFLAGS} -o $@
################################################################
# Bochs
${BUILD_DIR}/%/system.iso: ${BUILD_DIR}/%/system.elf
rm -rf $(shell dirname $<)/grub
mkdir -p $(shell dirname $<)/grub/boot/grub
cp arch/bochs/grub.cfg $(shell dirname $<)/grub/boot/grub
cp $< $(shell dirname $<)/grub/boot/system.elf
grub-mkrescue -o $@ $(shell dirname $<)/grub
BOCHS_RUNNER_ARGS = \
-V arch/bochs/vgabios.bin \
-b arch/bochs/BIOS-bochs-latest \
${BUILD_DIR}/%/trace.pb: ${BUILD_DIR}/%/system.iso
${BOCHS_RUNNER} ${BOCHS_RUNNER_ARGS} -1 \
-f ${FAIL_TRACE} \
-e $(shell dirname $<)/system.elf \
-i $(shell dirname $<)/system.iso \
-- \
-Wf,--state-file=$(shell dirname $<)/state \
-Wf,--trace-file=$(shell dirname $<)/trace.pb \
-Wf,--start-symbol=start_trace \
-Wf,--end-symbol=stop_trace \
-Wf,--check-bounds
client-%:
${BOCHS_RUNNER} ${BOCHS_RUNNER_ARGS} \
-f ${FAIL_INJECT} \
-e ${BUILD_DIR}/$(subst client-,,$@)/system.elf \
-i ${BUILD_DIR}/$(subst client-,,$@)/system.iso \
-j $(shell getconf _NPROCESSORS_ONLN) \
-- \
-Wf,--state-dir=${BUILD_DIR}/$(subst client-,,$@)/state \
-Wf,--trap \
-Wf,--timeout=10 \
-Wf,--ok-marker=ok_marker \
-Wf,--fail-marker=fail_marker \
-Wf,--catch-write-textsegment \
-Wf,--catch-outerspace \
2>/dev/null | grep -B 2 -A 8 'INJECT'
inject-%:
${BOCHS_RUNNER} ${BOCHS_RUNNER_ARGS} -1 \
-f ${FAIL_INJECT} \
-e ${BUILD_DIR}/$(subst inject-,,$@)/system.elf \
-i ${BUILD_DIR}/$(subst inject-,,$@)/system.iso \
-j 1 -- \
-Wf,--state-dir=${BUILD_DIR}/$(subst inject-,,$@)/state \
-Wf,--trap \
-Wf,--timeout=10 \
-Wf,--ok-marker=ok_marker \
-Wf,--fail-marker=fail_marker \
-Wf,--catch-write-textsegment \
-Wf,--catch-outerspace
# TODO: Command line interface changed
import-arch-%: ${BUILD_DIR}/%/trace.pb ${HOME}/.my.cnf
# ${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b mem -t $< -e $(shell dirname $<)/system.elf -i mem --memory-type ram
# ${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b regs-trace -t $< -e $(shell dirname $<)/system.elf -i mem --memory-type register
# ${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b regs -t $< -e $(shell dirname $<)/system.elf -i regs
# ${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b ip -t $< -e $(shell dirname $<)/system.elf -i regs --no-gp --ip
# ${FAIL_PRUNE} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b %% --overwrite
${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b mem -t $< -e $(shell dirname $<)/system.elf -i MemoryImporter
${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b regs-trace -t $< -e $(shell dirname $<)/system.elf -i MemoryImporter
${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b regs -t $< -e $(shell dirname $<)/system.elf -i RegisterImporter
${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b ip -t $< -e $(shell dirname $<)/system.elf -i RegisterImporter --no-gp --ip
${FAIL_PRUNE} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b %% --overwrite
define arch-make-targets
build-$1: ${BUILD_DIR}/$1/system.iso
trace-$1: ${BUILD_DIR}/$1/trace.pb
endef

Binary file not shown.

View File

@ -0,0 +1,7 @@
set timeout=0
set default=0
menuentry "CoRedOS" {
multiboot /boot/system.elf
boot
}

View File

@ -0,0 +1,4 @@
#pragma once
#define ARCH_ASM_CLOBBER_ALL "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp"

View File

@ -0,0 +1,59 @@
## Bare bone boot.s from wiki.osdev.org
# multiboot header
.section .rodata.multiboot
.align 4
# magic number
.long 0x1BADB002
# flags: align, meminfo
.long 0x3
# checksum: -(magic+flags)
.long -(0x1BADB002 + 0x3)
# the initial kernel stack
.section .kernel_stack
.global os_stack
.size os_stack, 4096
#.Lstack_bottom:
os_stack:
.byte 0
#.skip 16384 # 16 KiB
.skip 4094 # 4 KiB
.byte 0
.Lstack_top:
# The linker script specifies _start as the entry point to the kernel and the
# bootloader will jump to this position once the kernel has been loaded. It
# doesn't make sense to return from this function as the bootloader is gone.
.section .text.startup
.global _start
.type _start, @function
_start:
# Welcome to kernel mode!
# To set up a stack, we simply set the esp register to point to the top of
# our stack (as it grows downwards).
movl $.Lstack_top, %esp
# We are now ready to actually execute C code. (see ./startup.cc)
call os_main
# In case the function returns, we'll want to put the computer into an
# infinite loop. To do that, we use the clear interrupt ('cli') instruction
# to disable interrupts, the halt instruction ('hlt') to stop the CPU until
# the next interrupt arrives, and jumping to the halt instruction if it ever
# continues execution, just to be safe. We will create a local label rather
# than real symbol and jump to there endlessly.
cli
hlt
.Lhang:
jmp .Lhang
# Set the size of the _start symbol to the current location '.' minus its start.
# This is useful when debugging or when you implement call tracing.
.size _start, . - _start

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,7 @@
set timeout=0
set default=0
menuentry "CoRedOS" {
multiboot /boot/system.elf
boot
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,75 @@
// https://github.com/bytecodealliance/wasm-micro-runtime/blob/WAMR-2.4.4/doc/embed_wamr.md
// #include <stdio.h>
#include <string.h>
#include "lib.c"
// #include "bh_read_file.h"
#include "bh_platform.h"
#include "wasm_export.h"
#include "module_wasm.c"
#define STACK_SIZE (8 * 1024)
#define HEAP_SIZE (8 * 1024)
// TODO: Set this up so the lsp actually finds the includes...
MAIN() {
char error_buf[128];
wasm_module_t module;
wasm_module_inst_t module_inst;
wasm_function_inst_t func;
wasm_exec_env_t exec_env;
uint32 stack_size = 8 * 1024;
uint32 heap_size = 8 * 1024;
/* initialize the wasm runtime */
RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(init_args));
init_args.mem_alloc_type = Alloc_With_System_Allocator;
if (!wasm_runtime_full_init(&init_args)) {
return;
}
/* parse the WASM file from buffer and create a WASM module */
module = wasm_runtime_load(build_bochs_sum_module_aot, build_bochs_sum_module_aot_len, error_buf,
sizeof(error_buf));
/* create an instance of the WASM module (WASM linear memory is ready) */
module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
error_buf, sizeof(error_buf));
/* lookup a WASM function by its name, the function signature can NULL here */
func = wasm_runtime_lookup_function(module_inst, "wasm_module");
/* create an execution environment to execute arbitrary WASM functions */
exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
/* arguments are always transferred in 32-bit element */
uint32 args[1];
/* call an arbitrary WASM function */
MARKER(start_trace);
if (!wasm_runtime_call_wasm(exec_env, func, 0, args)) {
/* function wasn't called correctly */
POSIX_PRINTF("Failed to call function 'wasm_module'!\n");
}
MARKER(stop_trace);
POSIX_PRINTF("Sum: %d\n!", args[0]);
/* the return value is stored in args[0] */
if (args[0] == 100) {
MARKER(ok_marker);
} else {
MARKER(fail_marker);
}
wasm_runtime_destroy_exec_env(exec_env);
wasm_runtime_deinstantiate(module_inst);
wasm_runtime_unload(module);
wasm_runtime_destroy();
}

View File

@ -0,0 +1,68 @@
unsigned char build_bochs_sum_module_aot[] = {
0x00, 0x61, 0x6f, 0x74, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x3e, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x78, 0x38, 0x36, 0x5f, 0x36, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x14, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x7f, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x7f, 0x01, 0x00, 0x00,
0x41, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00,
0x41, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00,
0x41, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00,
0x41, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00,
0x41, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00,
0x41, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00,
0x41, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00,
0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x7f, 0x00, 0x00, 0x00,
0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00,
0x41, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00,
0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x02, 0x00, 0x00, 0x00,
0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xc3, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x2e,
0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x8b, 0x47, 0x10,
0x48, 0xba, 0x64, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x48, 0x8b,
0x88, 0x78, 0x01, 0x00, 0x00, 0x8b, 0x80, 0xd0, 0x01, 0x00, 0x00, 0x83,
0xc0, 0xf0, 0x48, 0x89, 0x54, 0x01, 0x08, 0xb8, 0x64, 0x00, 0x00, 0x00,
0xc3, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x2a, 0x01, 0x00, 0x00,
0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x07, 0x00,
0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x12, 0x00, 0x5f, 0x5f, 0x77, 0x61, 0x73, 0x6d, 0x5f, 0x63,
0x61, 0x6c, 0x6c, 0x5f, 0x63, 0x74, 0x6f, 0x72, 0x73, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x77, 0x61, 0x73, 0x6d,
0x5f, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x00, 0x01, 0x00, 0x00, 0x00,
0x03, 0x00, 0x0d, 0x00, 0x5f, 0x5f, 0x64, 0x73, 0x6f, 0x5f, 0x68, 0x61,
0x6e, 0x64, 0x6c, 0x65, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x03, 0x00, 0x0b, 0x00, 0x5f, 0x5f, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x65,
0x6e, 0x64, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x0c, 0x00,
0x5f, 0x5f, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x5f, 0x6c, 0x6f, 0x77, 0x00,
0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x0d, 0x00, 0x5f, 0x5f, 0x73, 0x74,
0x61, 0x63, 0x6b, 0x5f, 0x68, 0x69, 0x67, 0x68, 0x00, 0x00, 0x00, 0x00,
0x05, 0x00, 0x00, 0x00, 0x03, 0x00, 0x0e, 0x00, 0x5f, 0x5f, 0x67, 0x6c,
0x6f, 0x62, 0x61, 0x6c, 0x5f, 0x62, 0x61, 0x73, 0x65, 0x00, 0x00, 0x00,
0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x0c, 0x00, 0x5f, 0x5f, 0x68, 0x65,
0x61, 0x70, 0x5f, 0x62, 0x61, 0x73, 0x65, 0x00, 0x07, 0x00, 0x00, 0x00,
0x03, 0x00, 0x0b, 0x00, 0x5f, 0x5f, 0x68, 0x65, 0x61, 0x70, 0x5f, 0x65,
0x6e, 0x64, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x0e, 0x00,
0x5f, 0x5f, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x5f, 0x62, 0x61, 0x73,
0x65, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x03, 0x00, 0x0d, 0x00,
0x5f, 0x5f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x62, 0x61, 0x73, 0x65,
0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x03, 0x00, 0x16, 0x00,
0x5f, 0x5f, 0x77, 0x61, 0x73, 0x6d, 0x5f, 0x66, 0x69, 0x72, 0x73, 0x74,
0x5f, 0x70, 0x61, 0x67, 0x65, 0x5f, 0x65, 0x6e, 0x64, 0x00, 0x00, 0x00,
0x05, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
unsigned int build_bochs_sum_module_aot_len = 776;

Binary file not shown.

Binary file not shown.

Binary file not shown.

75
examples/embed/host.c Normal file
View File

@ -0,0 +1,75 @@
// https://github.com/bytecodealliance/wasm-micro-runtime/blob/WAMR-2.4.4/doc/embed_wamr.md
// #include <stdio.h>
#include <string.h>
#include "lib.c"
// #include "bh_read_file.h"
#include "bh_platform.h"
#include "wasm_export.h"
#include "__WASM_ARRAY_FILE__"
#define STACK_SIZE (8 * 1024)
#define HEAP_SIZE (8 * 1024)
// TODO: Set this up so the lsp actually finds the includes...
MAIN() {
char error_buf[128];
wasm_module_t module;
wasm_module_inst_t module_inst;
wasm_function_inst_t func;
wasm_exec_env_t exec_env;
uint32 stack_size = 8 * 1024;
uint32 heap_size = 8 * 1024;
/* initialize the wasm runtime */
RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(init_args));
init_args.mem_alloc_type = Alloc_With_System_Allocator;
if (!wasm_runtime_full_init(&init_args)) {
return;
}
/* parse the WASM file from buffer and create a WASM module */
module = wasm_runtime_load(__WASM_ARRAY__, __WASM_ARRAY_LEN__, error_buf,
sizeof(error_buf));
/* create an instance of the WASM module (WASM linear memory is ready) */
module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
error_buf, sizeof(error_buf));
/* lookup a WASM function by its name, the function signature can NULL here */
func = wasm_runtime_lookup_function(module_inst, "wasm_module");
/* create an execution environment to execute arbitrary WASM functions */
exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
/* arguments are always transferred in 32-bit element */
uint32 args[1];
/* call an arbitrary WASM function */
MARKER(start_trace);
if (!wasm_runtime_call_wasm(exec_env, func, 0, args)) {
/* function wasn't called correctly */
POSIX_PRINTF("Failed to call function 'wasm_module'!\n");
}
MARKER(stop_trace);
POSIX_PRINTF("Sum: %d\n!", args[0]);
/* the return value is stored in args[0] */
if (args[0] == 100) {
MARKER(ok_marker);
} else {
MARKER(fail_marker);
}
wasm_runtime_destroy_exec_env(exec_env);
wasm_runtime_deinstantiate(module_inst);
wasm_runtime_unload(module);
wasm_runtime_destroy();
}

2503
examples/embed/wasm_export.h Normal file

File diff suppressed because it is too large Load Diff

31
examples/lib.c Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#define INLINE __attribute__((always_inline)) inline
#define NOINLINE __attribute__((noinline))
#define __QUOTE(x) #x
#define QUOTE(x) __QUOTE(x)
#ifndef MARKER
#define MARKER(str) \
__asm__ volatile(QUOTE(str) ":" \
: /* no inputs */ \
: /* no outputs */ \
: "memory", ARCH_ASM_CLOBBER_ALL)
#endif
#ifndef MAIN
#define MAIN() void os_main(void)
#endif
#ifndef POSIX_PRINTF
#define POSIX_PRINTF(...)
#endif
typedef __UINT8_TYPE__ uint8_t;
typedef __UINT16_TYPE__ uint16_t;
typedef __UINT32_TYPE__ uint32_t;
typedef __INT8_TYPE__ int8_t;
typedef __INT16_TYPE__ int16_t;
typedef __INT32_TYPE__ int32_t;

70
examples/linker.ld Normal file
View File

@ -0,0 +1,70 @@
/* Kernel entry function */
ENTRY(_start)
OUTPUT_FORMAT(elf32-i386)
SECTIONS {
/DISCARD/ : {
*(".text.inlined*")
*(.comment)
*(.eh_frame)
*(.note.gnu.build-id)
}
/* Set kernel start address */
. = 0x100000;
/* Code and readonly data */
.text : {
/* fill gaps with int3 opcode to detect invalid jumps */
FILL(0xcc)
/* multiboot header */
multiboot_header = .;
KEEP (*(".rodata.multiboot"))
/* /\* fixed address for IRQ handlers *\/ */
/* . = 0x1000; */
/* /\* start of interrupt handlers *\/ */
/* _stext_irqs = .; */
/* /\* IRQ Handlers *\/ */
/* KEEP (*(".text.irqhandlers*")) /\* ASM *\/ */
/* KEEP (*(".text.irq_handler*")) /\* C *\/ */
/* *(".text.isrs*") /\* C *\/ */
/* *(".text.isr_*") /\* C *\/ */
/* KEEP (*(".text.OSEKOS_ISR*")) */
/* KEEP (*(".text.idt")) /\* ASM *\/ */
/* /\* sysenter handler *\/ */
/* KEEP (*(".text.sysenter_syscall")) */
/* _etext_irqs = .; */
/* . += 16; /\* padding after data, workaround for import-trace *\/ */
KEEP (*(".text.startup"))
*(".text*")
*(".rodata*")
}
/* Data and Stacks */
. = 0x200000;
.data : {
KEEP (*(".startup_stack"))
KEEP (*(".kernel_stack"))
*(".data*")
*(COMMON);
}
/* Memory-mapped I/O APIC */
_sioapic = 0xFEC00000;
ioapic = 0xFEC00000;
_eioapic = 0xFEC00FFF;
/* Memory-mapped Local APIC */
_slapic = 0xFEE00000;
lapic = 0xFEE00000;
_elapic = 0xFEE00FFF;
}

78
examples/stubs.c Normal file
View File

@ -0,0 +1,78 @@
#include <stddef.h>
#include <stdint.h>
// https://git.musl-libc.org/cgit/musl/tree/src/string/memcpy.c
void *memcpy(void *restrict dest, const void *restrict src, size_t n) {
unsigned char *d = dest;
const unsigned char *s = src;
for (; n; n--)
*d++ = *s++;
return dest;
}
// https://git.musl-libc.org/cgit/musl/tree/src/string/memmove.c
void *memmove(void *dest, const void *src, size_t n) {
char *d = dest;
const char *s = src;
if (d == s)
return d;
if ((uintptr_t)s - (uintptr_t)d - n <= -2 * n)
return memcpy(d, s, n);
if (d < s) {
for (; n; n--)
*d++ = *s++;
} else {
while (n)
n--, d[n] = s[n];
}
return dest;
}
// https://git.musl-libc.org/cgit/musl/tree/src/string/memset.c
void *memset(void *dest, int c, size_t n) {
unsigned char *s = dest;
size_t k;
/* Fill head and tail with minimal branching. Each
* conditional ensures that all the subsequently used
* offsets are well-defined and in the dest region. */
if (!n)
return dest;
s[0] = c;
s[n - 1] = c;
if (n <= 2)
return dest;
s[1] = c;
s[2] = c;
s[n - 2] = c;
s[n - 3] = c;
if (n <= 6)
return dest;
s[3] = c;
s[n - 4] = c;
if (n <= 8)
return dest;
/* Advance pointer to align it at a 4-byte boundary,
* and truncate n to a multiple of 4. The previous code
* already took care of any head/tail that get cut off
* by the alignment. */
k = -(uintptr_t)s & 3;
s += k;
n -= k;
n &= -4;
for (; n; n--, s++)
*s = c;
return dest;
}

7
examples/sum.c Normal file
View File

@ -0,0 +1,7 @@
int wasm_module(void) {
int sum = 0;
for (int i = 0; i < 100; ++i) {
sum += 1;
}
return sum;
}

147
wasm-base.dockerfile Normal file
View File

@ -0,0 +1,147 @@
FROM ubuntu:noble AS wamr-builder
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive TZ=Europe/Berline apt-get install -y --no-install-recommends \
build-essential \
gcc-multilib \
g++-multilib \
libgcc-11-dev \
lib32gcc-11-dev \
libc6-dev-i386 \
busybox \
git \
ca-certificates \
cmake \
ccache \
python3-minimal \
python3-pip \
ninja-build \
&& apt-get clean
# TODO: Can't find pthread on X86_32
# RUN ln -s /usr/lib/x86_64-linux-gnu/libpthread.so.0 /usr/lib/x86_64-linux-gnu/libpthread.so
# RUN ln -s /usr/lib32/libpthread.so.0 /usr/lib32/libpthread.so
RUN python3 -m pip config set global.break-system-packages true
# Clone WAMR
RUN git clone https://gitea.vps.chriphost.de/christoph/wamr \
&& cd wamr \
&& git checkout WAMR-2.4.4
WORKDIR /wamr
# Build WAMR iwasm (vmcore standalone interpreter)
# RUN cd product-mini/platforms/linux \
# && mkdir build_iwasm && cd build_iwasm \
# && cmake \
# -DWAMR_BUILD_PLATFORM=linux \
# -DWAMR_BUILD_TARGET=X86_64 \
# -DWAMR_BUILD_AOT=0 \
# -DWAMR_BUILD_WAMR_COMPILER=0 \
# -DWAMR_BUILD_INTERP=1 \
# -DWAMR_BUILD_FAST_INTERP=0 \
# -DWAMR_BUILD_JIT=0 \
# -DWAMR_BUILD_FAST_JIT=0 \
# -DWAMR_BUILD_LIBC_BUILTIN=1 \
# -DWAMR_BUILD_LIBC_WASI=1 \
# .. \
# && make -j$(nproc)
# Build WAMR wamrc (standalone compiler)
RUN cd wamr-compiler \
&& ./build_llvm.sh \
&& mkdir build_wamrc && cd build_wamrc \
&& cmake \
-DWAMR_BUILD_PLATFORM=linux \
-DWAMR_BUILD_TARGET=X86_64 \
-DWAMR_BUILD_AOT=0 \
-DWAMR_BUILD_WAMR_COMPILER=1 \
-DWAMR_BUILD_INTERP=0 \
-DWAMR_BUILD_FAST_INTERP=0 \
-DWAMR_BUILD_JIT=0 \
-DWAMR_BUILD_FAST_JIT=0 \
-DWAMR_BUILD_LIBC_BUILTIN=1 \
-DWAMR_BUILD_LIBC_WASI=1 \
.. \
&& make -j$(nproc)
# The libs might change often, so re-clone the repository each time.
# We don't want this for iwasm/wamrc because their builds take longer.
ARG CACHE_DATE=1970-01-01
# Clone WAMR
WORKDIR /
RUN git clone https://gitea.vps.chriphost.de/christoph/wamr wamrlib \
&& cd wamrlib \
&& git checkout WAMR-2.4.4
WORKDIR /wamrlib
# Build WAMR libvmlib (compiler runtime - to compile wasm modules embedded in a native application)
# RUN cd wamr-compiler \
# && ./build_llvm.sh \
# # && ./build_llvm.sh --extra-cmake-flags "-DCMAKE_C_FLAGS=-m32 -DCMAKE_CXX_FLAGS=-m32 -DCMAKE_EXE_LINKER_FLAGS=-m32 -lstdc++" \
# && mkdir build_libvmlib && cd build_libvmlib \
# && cmake \
# -DWAMR_BUILD_PLATFORM=baremetal \
# -DWAMR_BUILD_TARGET=X86_32 \
# -DWAMR_BUILD_AOT=1 \
# -DWAMR_BUILD_WAMR_COMPILER=0 \
# -DWAMR_BUILD_INTERP=0 \
# -DWAMR_BUILD_FAST_INTERP=0 \
# -DWAMR_BUILD_JIT=0 \
# -DWAMR_BUILD_FAST_JIT=0 \
# -DWAMR_BUILD_LIBC_BUILTIN=1 \
# -DWAMR_BUILD_LIBC_WASI=0 \
# -DWAMR_BUILD_SIMD=0 \
# .. \
# && make -j$(nproc)
# Build WAMR libiwasm (vmcore interpreter runtime - to run wasm modules embedded in a native application)
RUN mkdir build_libiwasm && cd build_libiwasm \
&& cmake \
-DWAMR_BUILD_PLATFORM=baremetal \
-DWAMR_BUILD_TARGET=X86_32 \
-DWAMR_BUILD_AOT=0 \
-DWAMR_BUILD_WAMR_COMPILER=0 \
-DWAMR_BUILD_INTERP=1 \
-DWAMR_BUILD_FAST_INTERP=0 \
-DWAMR_BUILD_JIT=0 \
-DWAMR_BUILD_FAST_JIT=0 \
-DWAMR_BUILD_LIBC_BUILTIN=1 \
-DWAMR_BUILD_LIBC_WASI=0 \
-DWAMR_BUILD_SIMD=0 \
.. \
&& make -j$(nproc)
# =============================================================================
FROM ghcr.io/webassembly/wasi-sdk:wasi-sdk-29
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive TZ=Europe/Berline apt-get install -y --no-install-recommends \
build-essential \
gcc-multilib \
busybox \
git \
ca-certificates \
wget \
neovim \
ranger \
wabt \
fish \
grub-common \
xorriso \
grub-pc-bin \
&& apt-get clean
COPY --from=wamr-builder /wamr /opt/wamr
COPY --from=wamr-builder /wamrlib /opt/wamrlib
RUN ln -sf /opt/wamr/product-mini/platforms/linux/build_iwasm /opt/wamr-iwasm \
&& ln -sf /opt/wamr/wamr-compiler/build_wamrc /opt/wamr-wamrc \
&& ln -sf /opt/wamrlib/wamr-compiler/build_libvmlib /opt/wamr-libvmlib \
&& ln -sf /opt/wamrlib/build_libiwasm /opt/wamr-libiwasm
COPY ./examples /home/ubuntu/examples
WORKDIR /home/ubuntu/examples
ENV PATH="$PATH:/opt/wasi-sdk/bin:/opt/wamr-iwasm:/opt/wamr-wamrc"

View File

@ -1,112 +0,0 @@
FROM ubuntu:noble AS wamr-builder
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive TZ=Europe/Berline apt-get install -y --no-install-recommends \
build-essential \
busybox \
git \
cmake \
ca-certificates \
g++-multilib \
libgcc-11-dev \
lib32gcc-11-dev \
ccache \
python3-minimal \
python3-pip \
ninja-build \
&& apt-get clean
RUN python3 -m pip config set global.break-system-packages true
# Clone WAMR
RUN git clone https://github.com/bytecodealliance/wasm-micro-runtime \
&& cd wasm-micro-runtime \
&& git checkout WAMR-2.4.4
WORKDIR /wasm-micro-runtime
# Build WAMR iwasm (standalone interpreter)
RUN cd product-mini/platforms/linux \
&& mkdir build_iwasm && cd build_iwasm \
&& cmake \
-DWAMR_BUILD_AOT=0 \
-DWAMR_BUILD_WAMR_COMPILER=0 \
-DWAMR_BUILD_INTERP=1 \
-DWAMR_BUILD_FAST_INTERP=0 \
-DWAMR_BUILD_JIT=0 \
-DWAMR_BUILD_FAST_JIT=0 \
-DWAMR_BUILD_LIBC_BUILTIN=1 \
-DWAMR_BUILD_LIBC_WASI=1 \
.. \
&& make -j$(nproc)
# Build WAMR libiwasm (interpreter runtime)
RUN mkdir build_libiwasm && cd build_libiwasm \
&& cmake \
-DWAMR_BUILD_AOT=1 \
-DWAMR_BUILD_WAMR_COMPILER=0 \
-DWAMR_BUILD_INTERP=1 \
-DWAMR_BUILD_FAST_INTERP=0 \
-DWAMR_BUILD_JIT=0 \
-DWAMR_BUILD_FAST_JIT=0 \
-DWAMR_BUILD_LIBC_BUILTIN=1 \
-DWAMR_BUILD_LIBC_WASI=1 \
.. \
&& make -j$(nproc)
# Build WAMR wamrc (standalone compiler)
RUN cd wamr-compiler \
&& ./build_llvm.sh \
&& mkdir build_wamrc && cd build_wamrc \
&& cmake \
-DWAMR_BUILD_AOT=0 \
-DWAMR_BUILD_WAMR_COMPILER=1 \
-DWAMR_BUILD_INTERP=0 \
-DWAMR_BUILD_FAST_INTERP=0 \
-DWAMR_BUILD_JIT=0 \
-DWAMR_BUILD_FAST_JIT=0 \
-DWAMR_BUILD_LIBC_BUILTIN=1 \
-DWAMR_BUILD_LIBC_WASI=1 \
.. \
&& make -j$(nproc)
# Build WAMR libvmlib (compiler runtime)
RUN cd wamr-compiler \
&& ./build_llvm.sh \
&& mkdir build_libvmlib && cd build_libvmlib \
&& cmake \
-DWAMR_BUILD_AOT=1 \
-DWAMR_BUILD_WAMR_COMPILER=0 \
-DWAMR_BUILD_INTERP=1 \
-DWAMR_BUILD_FAST_INTERP=0 \
-DWAMR_BUILD_JIT=0 \
-DWAMR_BUILD_FAST_JIT=0 \
-DWAMR_BUILD_LIBC_BUILTIN=1 \
-DWAMR_BUILD_LIBC_WASI=1 \
.. \
&& make -j$(nproc)
# =============================================================================
FROM ghcr.io/webassembly/wasi-sdk:wasi-sdk-29
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive TZ=Europe/Berline apt-get install -y --no-install-recommends \
git \
ca-certificates \
wget \
neovim \
ranger \
wabt \
busybox \
&& apt-get clean
COPY --from=wamr-builder /wasm-micro-runtime /opt/wamr
RUN ln -sf /opt/wamr/product-mini/platforms/linux/build_iwasm /opt/wamr-iwasm \
&& ln -sf /opt/wamr/build_libiwasm /opt/wamr-libiwasm \
&& ln -sf /opt/wamr/wamr-compiler/build_wamrc /opt/wamr-wamrc \
&& ln -sf /opt/wamr/wamr-compiler/build_libvmlib /opt/wamr-libvmlib
COPY ./examples /home/ubuntu/examples
WORKDIR /home/ubuntu/examples
ENV PATH="$PATH:/opt/wasi-sdk/bin:/opt/wamr-iwasm:/opt/wamr-wamrc"

View File

@ -1,90 +0,0 @@
// https://github.com/bytecodealliance/wasm-micro-runtime/blob/WAMR-2.4.4/doc/embed_wamr.md
#include <stdio.h>
#include <string.h>
#include "bh_platform.h"
#include "bh_read_file.h"
#include "wasm_export.h"
#include "../__WASM_ARRAY_FILE__"
#define STACK_SIZE (8 * 1024)
#define HEAP_SIZE (8 * 1024)
// TODO: Set this up so the lsp actually finds the includes...
int main(int argc, char **argv) {
// uint8 *buffer;
// uint32 size;
char error_buf[128];
wasm_module_t module;
wasm_module_inst_t module_inst;
wasm_function_inst_t func;
wasm_exec_env_t exec_env;
uint32 stack_size = 8 * 1024;
uint32 heap_size = 8 * 1024;
/* initialize the wasm runtime by default configurations */
// wasm_runtime_init();
printf("Initializing WAMR runtime\n");
RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(init_args));
init_args.mem_alloc_type = Alloc_With_System_Allocator;
if (!wasm_runtime_full_init(&init_args)) {
printf("Failed to initialize WAMR runtime\n");
return 1;
}
/* read WASM file into a memory buffer */
// printf("Instantiating module from file '%s'\n", argv[1]);
// buffer = bh_read_file_to_buffer(argv[1], &size);
/* add line below if we want to export native functions to WASM app */
// wasm_runtime_register_natives(...);
/* parse the WASM file from buffer and create a WASM module */
// module = wasm_runtime_load(buffer, size, error_buf, sizeof(error_buf));
printf("Instantiating module from buffer\n");
module = wasm_runtime_load(__WASM_ARRAY__, __WASM_ARRAY_LEN__, error_buf,
sizeof(error_buf));
/* create an instance of the WASM module (WASM linear memory is ready) */
module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
error_buf, sizeof(error_buf));
/* lookup a WASM function by its name
The function signature can NULL here */
// func = wasm_runtime_lookup_function(module_inst, "wasm_module");
/* creat an execution environment to execute the WASM functions */
// exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
// TODO: Also when using execute_main?
/* arguments are always transferred in 32-bit element */
uint32 args[1];
args[0] = 8;
args[1] = 16;
// TODO: The arguments have to be passed differently, probably using
// wasm_runtime_set_wasi_args?
/* call the WASM function */
printf("Calling WASM function\n");
wasm_application_execute_main(module_inst, 2, args);
// if (wasm_runtime_call_wasm(exec_env, func, 1, args)) {
// /* the return value is stored in argv[0] */
// printf("wasm function return: %d\n", args[0]);
// } else {
// /* exception is thrown if call fails */
// printf("%s\n", wasm_runtime_get_exception(module_inst));
// }
// wasm_runtime_destroy_exec_env(exec_env);
wasm_runtime_deinstantiate(module_inst);
wasm_runtime_unload(module);
wasm_runtime_destroy();
return 0;
}

View File

@ -1,25 +0,0 @@
// https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/build_wasm_app.md#user-content-build-wasm-applications-with-wasi-sdk
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
char *buf;
printf("Hello world!\n");
printf("Got %d args!\n", argc);
buf = malloc(1024);
if (!buf) {
printf("malloc buf failed\n");
return -1;
}
printf("buf ptr: %p\n", buf);
sprintf(buf, "%s", "1234\n");
printf("buf: %s", buf);
free(buf);
return 0;
}