Compare commits
15 Commits
0809803bfd
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
25cb90296f
|
|||
|
650f457c5b
|
|||
|
2f8f737bb8
|
|||
|
6381727851
|
|||
|
6307a01b6f
|
|||
|
fc57777a5b
|
|||
|
b62e56c130
|
|||
|
e742cec923
|
|||
|
b8f4e987b7
|
|||
|
ecfb162062
|
|||
|
20e96abee1
|
|||
|
ce0c7f4f7f
|
|||
|
8f09e49c8a
|
|||
|
f25e213f9d
|
|||
|
103de5345e
|
26
.gitea/workflows/wasm-docker.yaml
Normal file
26
.gitea/workflows/wasm-docker.yaml
Normal file
@ -0,0 +1,26 @@
|
||||
name: Build WASM Base Docker Image
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [disabled]
|
||||
# branches: [main]
|
||||
# paths:
|
||||
# - ".gitea/workflows/wasm-docker.yaml"
|
||||
# - "wasm-base.dockerfile"
|
||||
|
||||
jobs:
|
||||
wasm-docker:
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
- name: Login to container registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: gitea.vps.chriphost.de
|
||||
username: ${{ secrets.CONTAINER_REGISTRY_USER }}
|
||||
password: ${{ secrets.CONTAINER_REGISTRY_TOKEN }}
|
||||
- name: Build WASM Base Docker Image
|
||||
run: docker build --file wasm-base.dockerfile --tag gitea.vps.chriphost.de/christoph/wasm-base:latest .
|
||||
- name: Push WASM Base Docker Image
|
||||
run: docker push gitea.vps.chriphost.de/christoph/wasm-base:latest
|
||||
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,5 +1 @@
|
||||
wasm-base/examples/*.wasm
|
||||
wasm-base/examples/*.aot
|
||||
wasm-base/examples/embed/*_host.c
|
||||
wasm-base/examples/*.host
|
||||
wasm-base/examples/*_wasm.c
|
||||
examples/build-*/
|
||||
|
||||
12
Makefile
12
Makefile
@ -1,8 +1,14 @@
|
||||
docker:
|
||||
docker build -t wasm-base wasm-base
|
||||
docker build -t gitea.vps.chriphost.de/christoph/wasm-base:latest -f wasm-base.dockerfile . --build-arg CACHE_DATE="$(shell date)"
|
||||
|
||||
run:
|
||||
docker run --rm -it wasm-base:latest /bin/bash
|
||||
docker run --rm -it gitea.vps.chriphost.de/christoph/wasm-base:latest /usr/bin/fish
|
||||
|
||||
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" gitea.vps.chriphost.de/christoph/wasm-base:latest /usr/bin/fish
|
||||
|
||||
bochs:
|
||||
bochs -f ./examples/arch/bochs/bochsrc-docker.txt -q
|
||||
|
||||
bochs-host:
|
||||
nix shell nixpkgs#bochs --command sh -c "bochs -f ./examples/arch/bochs/bochsrc-host.txt -q"
|
||||
|
||||
109
examples/Makefile
Normal file
109
examples/Makefile
Normal 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:
|
||||
152
examples/arch/bochs.mk
Normal file
152
examples/arch/bochs.mk
Normal file
@ -0,0 +1,152 @@
|
||||
# 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 \
|
||||
-O0 -nostdlib \
|
||||
-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
|
||||
# NOTE: When compiling I get "error: bp cannot be used in ‘asm’ here"
|
||||
# I could remove "ebp" from the clobber list (ARCH_ASM_CLOBBER_ALL) or
|
||||
# use the -fomit-frame-pointer flag to tell gcc it shouldn't rely on ebp for enter/leave...
|
||||
CFLAGS := -I. -O0 -m32 -ffunction-sections -std=c11 -fomit-frame-pointer
|
||||
LDFLAGS = -Wl,-T linker.ld $^ -Wl,--build-id=none -static -nostdlib -m32 \
|
||||
-Wl,-rpath,${IWASM_LIB} -L${IWASM_LIB} -liwasm -lgcc
|
||||
INCL := -I${WAMR}/core/iwasm/include \
|
||||
-I${WAMR}/core/shared/utils \
|
||||
-I${WAMR}/core/shared/platform/baremetal
|
||||
WAMRC := /opt/wamr-wamrc/wamrc
|
||||
WAMRCFLAGS := --target=i386 --format=object
|
||||
XXD := busybox xxd
|
||||
|
||||
|
||||
################################################################
|
||||
# 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 -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 ${BUILD_DIR}/$*/module_host.c -o $@
|
||||
|
||||
${BUILD_DIR}/startup.o: arch/bochs/startup.s
|
||||
${CC} $< ${CFLAGS} -c -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
|
||||
${BUILD_DIR}/%/trace.pb:
|
||||
${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
|
||||
BIN
examples/arch/bochs/BIOS-bochs-latest
Normal file
BIN
examples/arch/bochs/BIOS-bochs-latest
Normal file
Binary file not shown.
9
examples/arch/bochs/bochsrc-docker.txt
Normal file
9
examples/arch/bochs/bochsrc-docker.txt
Normal file
@ -0,0 +1,9 @@
|
||||
megs: 32
|
||||
display_library: nogui
|
||||
romimage: file=/home/ubuntu/examples/arch/bochs/BIOS-bochs-latest
|
||||
vgaromimage: file=/home/ubuntu/examples/arch/bochs/vgabios.bin
|
||||
ata0-master: type=cdrom, path=/home/ubuntu/examples/build-bochs/sum/system.iso, status=inserted
|
||||
boot: cdrom
|
||||
log: bochslog.txt
|
||||
clock: sync=realtime, time0=local
|
||||
cpu: count=1, ips=1000000
|
||||
10
examples/arch/bochs/bochsrc-host.txt
Normal file
10
examples/arch/bochs/bochsrc-host.txt
Normal file
@ -0,0 +1,10 @@
|
||||
megs: 32
|
||||
display_library: nogui
|
||||
romimage: file="/home/christoph/Notes/TU/MastersThesis/3 Wasm/examples/arch/bochs/BIOS-bochs-latest"
|
||||
vgaromimage: file="/home/christoph/Notes/TU/MastersThesis/3 Wasm/examples/arch/bochs/vgabios.bin"
|
||||
ata0-master: type=cdrom, path="/home/christoph/Notes/TU/MastersThesis/3 Wasm/examples/build-bochs/sum/system.iso", status=inserted
|
||||
boot: cdrom
|
||||
log: bochslog.txt
|
||||
clock: sync=realtime, time0=local
|
||||
cpu: count=1, ips=1000000
|
||||
sound: driver=dummy
|
||||
7
examples/arch/bochs/grub.cfg
Normal file
7
examples/arch/bochs/grub.cfg
Normal file
@ -0,0 +1,7 @@
|
||||
set timeout=0
|
||||
set default=0
|
||||
|
||||
menuentry "CoRedOS" {
|
||||
multiboot /boot/system.elf
|
||||
boot
|
||||
}
|
||||
59
examples/arch/bochs/startup.s
Normal file
59
examples/arch/bochs/startup.s
Normal 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
|
||||
BIN
examples/arch/bochs/vgabios.bin
Normal file
BIN
examples/arch/bochs/vgabios.bin
Normal file
Binary file not shown.
88
examples/arch/linux.mk
Normal file
88
examples/arch/linux.mk
Normal file
@ -0,0 +1,88 @@
|
||||
# C -> WASM
|
||||
WASI_ROOT := /opt/wasi-sdk
|
||||
WASI_CC := ${WASI_ROOT}/bin/clang
|
||||
WASI_CFLAGS := --target=wasm64 \
|
||||
--sysroot=${WASI_ROOT}/share/wasi-sysroot \
|
||||
-z stack-size=4096 \
|
||||
-O0 -g -nostdlib \
|
||||
-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-64
|
||||
CC := gcc
|
||||
# NOTE: When compiling I get "error: bp cannot be used in ‘asm’ here"
|
||||
# I could remove "ebp" from the clobber list (ARCH_ASM_CLOBBER_ALL) or
|
||||
# use the -fomit-frame-pointer flag to tell gcc it shouldn't rely on ebp for enter/leave...
|
||||
CFLAGS := -I. -O0 -g -ffunction-sections -std=c11 -fomit-frame-pointer
|
||||
LDFLAGS = $^ -Wl,--build-id=none -static -nostdlib \
|
||||
-Wl,-rpath,${IWASM_LIB} -L${IWASM_LIB} -liwasm -lgcc
|
||||
INCL := -I${WAMR}/core/iwasm/include \
|
||||
-I${WAMR}/core/shared/utils \
|
||||
-I${WAMR}/core/shared/platform/baremetal
|
||||
WAMRC := /opt/wamr-wamrc/wamrc
|
||||
WAMRCFLAGS := --target=i386 --format=object
|
||||
XXD := busybox xxd
|
||||
|
||||
|
||||
################################################################
|
||||
# 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 -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_linux_$*_module_aot/g" \
|
||||
-e "s/__WASM_ARRAY_LEN__/build_linux_$*_module_aot_len/g" \
|
||||
${BUILD_DIR}/$*/module_host.c
|
||||
${CC} ${CFLAGS} ${INCL} -c ${BUILD_DIR}/$*/module_host.c -o $@
|
||||
|
||||
${BUILD_DIR}/startup.o: arch/bochs/startup.s
|
||||
${CC} $< ${CFLAGS} -c -o $@
|
||||
|
||||
${BUILD_DIR}/%/system.elf: ${BUILD_DIR}/%/system.o ${BUILD_DIR}/startup.o
|
||||
${CC} ${LDFLAGS} -o $@
|
||||
|
||||
|
||||
define arch-make-targets
|
||||
|
||||
build-$1: ${BUILD_DIR}/$1/system.elf
|
||||
|
||||
gdb-$1:
|
||||
gdb --tui ${BUILD_DIR}/$1/system.elf
|
||||
|
||||
valgrind-$1:
|
||||
valgrind ${BUILD_DIR}/$1/system.elf
|
||||
|
||||
objdump-$1:
|
||||
objdump --disassemble --start-address=0x401000 --stop-address=0x401200 --disassembler-options intel --disassembler-color=on --source ${BUILD_DIR}/$1/system.elf
|
||||
|
||||
endef
|
||||
73
examples/embed/host.c
Normal file
73
examples/embed/host.c
Normal file
@ -0,0 +1,73 @@
|
||||
// https://github.com/bytecodealliance/wasm-micro-runtime/blob/WAMR-2.4.4/doc/embed_wamr.md
|
||||
|
||||
#include "lib.c"
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "wasm_export.h"
|
||||
|
||||
#include "__WASM_ARRAY_FILE__"
|
||||
|
||||
#define STACK_SIZE (4 * 1024)
|
||||
#define HEAP_SIZE (4 * 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;
|
||||
|
||||
/* initialize the wasm runtime */
|
||||
static char global_heap_buf[HEAP_SIZE];
|
||||
RuntimeInitArgs init_args;
|
||||
memset(&init_args, 0, sizeof(RuntimeInitArgs));
|
||||
|
||||
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);
|
||||
init_args.max_thread_num = 1;
|
||||
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
2503
examples/embed/wasm_export.h
Normal file
File diff suppressed because it is too large
Load Diff
35
examples/lib.c
Normal file
35
examples/lib.c
Normal file
@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#define INLINE __attribute__((always_inline)) inline
|
||||
#define NOINLINE __attribute__((noinline))
|
||||
|
||||
#define __QUOTE(x) #x
|
||||
#define QUOTE(x) __QUOTE(x)
|
||||
|
||||
#ifndef ARCH_ASM_CLOBBER_ALL
|
||||
#define ARCH_ASM_CLOBBER_ALL "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp"
|
||||
#endif
|
||||
|
||||
#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
70
examples/linker.ld
Normal 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
78
examples/stubs.c
Normal 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
7
examples/sum.c
Normal file
@ -0,0 +1,7 @@
|
||||
int wasm_module(void) {
|
||||
int sum = 0;
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
sum += 1;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
170
wasm-base.dockerfile
Normal file
170
wasm-base.dockerfile
Normal file
@ -0,0 +1,170 @@
|
||||
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 \
|
||||
-DCMAKE_COLOR_DIAGNOSTICS=ON \
|
||||
.. \
|
||||
&& make -j$(nproc)
|
||||
|
||||
RUN mkdir build_libiwasm_64 && cd build_libiwasm_64 \
|
||||
&& cmake \
|
||||
-DWAMR_BUILD_PLATFORM=baremetal \
|
||||
-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=0 \
|
||||
-DWAMR_BUILD_SIMD=0 \
|
||||
-DCMAKE_COLOR_DIAGNOSTICS=ON \
|
||||
.. \
|
||||
&& 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 \
|
||||
gdb \
|
||||
valgrind \
|
||||
fish \
|
||||
grub-common \
|
||||
xorriso \
|
||||
grub-pc-bin \
|
||||
bochs \
|
||||
&& apt-get clean
|
||||
|
||||
COPY --from=wamr-builder /wamrlib /opt/wamr
|
||||
COPY --from=wamr-builder /wamr/wamr-compiler/build_wamrc /opt/wamr/wamr-compiler/build_wamrc
|
||||
COPY --from=wamr-builder /wamr/product-mini/platforms/linux/build_iwasm /opt/wamr/product-mini/platforms/linux/build_iwasm
|
||||
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/wamr/wamr-compiler/build_libvmlib /opt/wamr-libvmlib \
|
||||
&& ln -sf /opt/wamr/build_libiwasm /opt/wamr-libiwasm \
|
||||
&& ln -sf /opt/wamr/build_libiwasm_64 /opt/wamr-libiwasm-64
|
||||
|
||||
COPY ./examples /home/ubuntu/examples
|
||||
WORKDIR /home/ubuntu/examples
|
||||
|
||||
ENV PATH="$PATH:/opt/wasi-sdk/bin:/opt/wamr-iwasm:/opt/wamr-wamrc"
|
||||
@ -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"
|
||||
@ -1,94 +0,0 @@
|
||||
# Paths
|
||||
WAMR_ROOT := /opt/wamr
|
||||
WAMRC := /opt/wamr-wamrc/wamrc
|
||||
IWASM := /opt/wamr-iwasm/iwasm
|
||||
WAMRC_LIB := /opt/wamr-libvmlib
|
||||
IWASM_LIB := /opt/wamr-libiwasm
|
||||
|
||||
WASI_ROOT := /opt/wasi-sdk
|
||||
WASI_CC := $(WASI_ROOT)/bin/clang
|
||||
WASI_CFLAGS := --target=wasm32-wasi \
|
||||
--sysroot=$(WASI_ROOT)/share/wasi-sysroot \
|
||||
-O0 -g -Wall
|
||||
|
||||
# Embedding
|
||||
EMBED_CC := gcc
|
||||
EMBED_INCL := -I$(WAMR_ROOT)/core/iwasm/include \
|
||||
-I$(WAMR_ROOT)/core/iwasm/common \
|
||||
-I$(WAMR_ROOT)/core/shared/platform/linux \
|
||||
-I$(WAMR_ROOT)/core/shared/utils \
|
||||
-I$(WAMR_ROOT)/core/shared/utils/uncommon
|
||||
EMBED_SOURCES := $(WAMR_ROOT)/core/shared/utils/uncommon/bh_read_file.c
|
||||
EMBED_CFLAGS := -O0 -g -Wall $(EMBED_INCL) $(EMBED_SOURCES)
|
||||
AOT_LDFLAGS := -Wl,-rpath,$(WAMRC_LIB)
|
||||
AOT_LDLIBS := -L$(WAMRC_LIB) -lvmlib -lm
|
||||
INTER_LDFLAGS := -Wl,-rpath,$(IWASM_LIB)
|
||||
INTER_LDLIBS := -L$(IWASM_LIB) -liwasm -lm
|
||||
XXD := busybox xxd
|
||||
|
||||
# Files
|
||||
SRCS := $(wildcard *.c)
|
||||
WASMS := $(SRCS:.c=.wasm)
|
||||
CWASMS := $(SRCS:.c=_wasm.c)
|
||||
AOTS := $(SRCS:.c=.aot)
|
||||
OBJS := $(SRCS:.c=.o)
|
||||
DEPS := $(SRCS:.c=.d)
|
||||
HOSTS := $(SRCS:.c=.host)
|
||||
|
||||
|
||||
.PHONY: all build-wasms build-aots build-cwasms build-hosts clean
|
||||
|
||||
all: build-wasms build-aots build-cwasms build-hosts
|
||||
|
||||
# Compile to wasm bytecode using wasi-sdk
|
||||
build-wasms: $(WASMS)
|
||||
|
||||
%.wasm: %.c
|
||||
$(WASI_CC) $(WASI_CFLAGS) $< -o $@
|
||||
|
||||
# Compile ahead-of-time module using wamrc
|
||||
build-aots: $(AOTS)
|
||||
|
||||
%.aot: %.wasm
|
||||
$(WAMRC) -o $@ $<
|
||||
# $(WAMRC) --enable-wasi -o $@ $<
|
||||
|
||||
# Convert the .aot files to C-style arrays (to embed them into the resulting host binary)
|
||||
build-cwasms: $(CWASMS)
|
||||
|
||||
%_wasm.c: %.aot
|
||||
$(XXD) -i $< > $@
|
||||
|
||||
# Compile the host that will load and run the compiled wasm module
|
||||
# The compiled wasm module is embedded as C-style array
|
||||
build-hosts: $(HOSTS)
|
||||
|
||||
# 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
|
||||
%.host: %_wasm.c
|
||||
cp embed/host.c embed/$*_host.c
|
||||
sed -i \
|
||||
-e "s/__WASM_ARRAY_FILE__/$*_wasm.c/g" \
|
||||
-e "s/__WASM_ARRAY__/$*_aot/g" \
|
||||
-e "s/__WASM_ARRAY_LEN__/$*_aot_len/g" \
|
||||
embed/$*_host.c
|
||||
$(EMBED_CC) $(EMBED_CFLAGS) embed/$*_host.c -o $@ $(INTER_LDFLAGS) $(INTER_LDLIBS)
|
||||
|
||||
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
|
||||
|
||||
# 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)
|
||||
@ -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;
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user