add linux-posix + linux-baremetal recipes

This commit is contained in:
2026-03-12 13:45:33 +01:00
parent 66eb0b3814
commit 50c6e9adea
3 changed files with 255 additions and 107 deletions

262
justfile
View File

@ -7,10 +7,31 @@ WASI_ROOT := env("WASI_ROOT")
WAMR_ROOT := env("WAMR_ROOT")
LIBIWASM_DEBUG := env("LIBIWASM_DEBUG")
LIBIWASM_RELEASE := env("LIBIWASM_RELEASE")
CROSS_CC := env("CROSSCC")
CROSS_CXX := env("CROSSCXX")
LIBIWASM_LINUX_DEBUG := env("LIBIWASM_LINUX_DEBUG")
LIBIWASM_LINUX_RELEASE := env("LIBIWASM_LINUX_RELEASE")
CROSS_CC := env("CROSS_CC")
LINUX_CC := env("LINUX_CC")
# C -> WASM
# =================================================================================================================== #
# Helper recipes
# =================================================================================================================== #
[default]
[private]
list:
@just --list --unsorted
[private]
create-build-dir module:
mkdir -p {{ BUILD_DIR }}-{{ module }}
[doc("Delete the build directory")]
clean module:
rm -rf {{ BUILD_DIR }}-{{ module }}
# =================================================================================================================== #
# Build WASM module recipes
# =================================================================================================================== #
WASI_CC := f"{{WASI_ROOT}}/bin/clang"
WASI_CFLAGS := "\
@ -26,9 +47,6 @@ WASI_CFLAGS := "\
-Wl,--export=__heap_base \
-Wl,--export=__data_end \
"
# WASM -> AOT
WAMRC := "wamrc"
WAMRCFLAGS := "\
--target=i386 \
@ -37,7 +55,25 @@ WAMRCFLAGS := "\
"
XXD := "xxd"
# Load AOT from WAMR (FAIL+Baremetal)
[doc("C -> WASM: Compile a C function to a WASM module using WASI-SDK")]
[group("build-module")]
build-wasm-module module: (create-build-dir module)
{{ WASI_CC }} {{ WASI_CFLAGS }} targets/wasm-module/{{ module }}.c -o {{ BUILD_DIR }}-{{ module }}/wasm_module.wasm
[doc("WASM -> AOT: Compile a WASM module ahead-of-time using WAMR")]
[group("build-module")]
build-wasm-aot module: (build-wasm-module module)
{{ WAMRC }} {{ WAMRCFLAGS }} -o {{ BUILD_DIR }}-{{ module }}/wasm_module.aot {{ BUILD_DIR }}-{{ module }}/wasm_module.wasm
[doc("AOT -> C-Array: Dump a WASM module compiled ahead-of-time to a binary array")]
[group("build-module")]
build-wasm-aot-array module: (build-wasm-aot module)
{{ XXD }} -i {{ BUILD_DIR }}-{{ module }}/wasm_module.aot > {{ BUILD_DIR }}-{{ module }}/wasm_module_array.c
# =================================================================================================================== #
# Host program recipes
# =================================================================================================================== #
# FAIL*
CROSS_CFLAGS := "\
-I./targets/wasm-host \
@ -66,38 +102,63 @@ CROSS_INCLUDES := f"\
-I{{WAMR_ROOT}}/core/shared/platform/baremetal \
"
# Build recipes
# LINUX-POSIX
[default]
[private]
list:
@just --list --unsorted
LINUX_CFLAGS := "\
-I./targets/wasm-host \
-O0 \
-m32 \
-ffunction-sections \
-fdata-sections \
-ggdb \
"
LINUX_LDFLAGS := f"\
-Wl,--build-id=none \
-m32 \
-Wl,-rpath,{{LIBIWASM_LINUX_DEBUG}} \
-L{{LIBIWASM_LINUX_DEBUG}} \
-liwasm \
-lm \
"
LINUX_INCLUDES := f"\
-I{{WAMR_ROOT}}/core/iwasm/include \
-I{{WAMR_ROOT}}/core/shared/utils \
-I{{WAMR_ROOT}}/core/shared/platform/linux \
"
[private]
create-build-dir module:
mkdir -p {{ BUILD_DIR }}-{{ module }}
# LINUX-Baremetal
[doc("Delete the build directory")]
clean module:
rm -rf {{ BUILD_DIR }}-{{ module }}
LINUX_BAREMETAL_CFLAGS := "\
-I./targets/wasm-host \
-O0 \
-m32 \
-ffunction-sections \
-fdata-sections \
-ffreestanding \
-ggdb \
"
LINUX_BAREMETAL_LDFLAGS := f"\
-Wl,--build-id=none \
-static \
-nostdlib \
-m32 \
-L{{LIBIWASM_DEBUG}} \
-liwasm \
-lc \
-lgcc \
-lm \
--entry main \
"
LINUX_BAREMETAL_INCLUDES := f"\
-I{{WAMR_ROOT}}/core/iwasm/include \
-I{{WAMR_ROOT}}/core/shared/utils \
-I{{WAMR_ROOT}}/core/shared/platform/baremetal \
"
[doc("C -> WASM: Compile a C function to a WASM module using WASI-SDK")]
[group("build")]
build-wasm-module module: (create-build-dir module)
{{ WASI_CC }} {{ WASI_CFLAGS }} targets/wasm-module/{{ module }}.c -o {{ BUILD_DIR }}-{{ module }}/wasm_module.wasm
[doc("WASM -> AOT: Compile a WASM module ahead-of-time using WAMR")]
[group("build")]
build-wasm-aot module: (build-wasm-module module)
{{ WAMRC }} {{ WAMRCFLAGS }} -o {{ BUILD_DIR }}-{{ module }}/wasm_module.aot {{ BUILD_DIR }}-{{ module }}/wasm_module.wasm
[doc("AOT -> C-Array: Dump a WASM module compiled ahead-of-time to a binary array")]
[group("build")]
build-wasm-aot-array module: (build-wasm-aot module)
{{ XXD }} -i {{ BUILD_DIR }}-{{ module }}/wasm_module.aot > {{ BUILD_DIR }}-{{ module }}/wasm_module_array.c
# TODO: Grave mistake to template the commands depending on the target... Should've used shebang recipes instead
[doc("Compile C-Host: The host uses WAMR to load the AOT module")]
[group("build")]
[group("build-host")]
build-wasm-host module target="fail": (build-wasm-aot-array module)
cp targets/wasm-host/{{ target }}.c {{ BUILD_DIR }}-{{ module }}/module_host.c
sed -i \
@ -105,50 +166,53 @@ build-wasm-host module target="fail": (build-wasm-aot-array module)
-e "s/__WASM_ARRAY__/build_{{ module }}_wasm_module_aot/g" \
-e "s/__WASM_ARRAY_LEN__/build_{{ module }}_wasm_module_aot_len/g" \
{{ BUILD_DIR }}-{{ module }}/module_host.c
{{ CROSS_CC }} {{ CROSS_CFLAGS }} {{ CROSS_INCLUDES }} \
-c {{ BUILD_DIR }}-{{ module }}/module_host.c \
-o {{ BUILD_DIR }}-{{ module }}/system.o
# Build depending on the target platform
{{ if target == "fail" { CROSS_CC + " " + CROSS_CFLAGS + " " + CROSS_INCLUDES } else if target == "linux" { LINUX_CC + " " + LINUX_CFLAGS + " " + LINUX_INCLUDES } else if target == "linux-baremetal" { CROSS_CC + " " + LINUX_BAREMETAL_CFLAGS + " " + LINUX_BAREMETAL_INCLUDES } else { error("invalid target") } }} \
-c {{ BUILD_DIR }}-{{ module }}/module_host.c \
-o {{ BUILD_DIR }}-{{ module }}/system.o
[doc("Compile bootloader")]
[group("build")]
build-system-startup module:
{{ CROSS_CC }} targets/startup.s {{ CROSS_CFLAGS }} -c -o {{ BUILD_DIR }}-{{ module }}/startup.o
[group("build-host")]
build-system-startup module target="fail":
{{ if target == "fail" { CROSS_CC } else if target == "linux" { LINUX_CC } else if target == "linux-baremetal" { CROSS_CC } else { error("invalid target") } }} \
targets/startup.s \
{{ if target == "fail" { CROSS_CFLAGS } else if target == "linux" { LINUX_CFLAGS } else if target == "linux-baremetal" { LINUX_BAREMETAL_CFLAGS } else { error("invalid target") } }} \
-c -o {{ BUILD_DIR }}-{{ module }}/startup.o
[doc("Compile newlib syscall stubs")]
[group("build")]
build-system-syscalls module:
{{ CROSS_CC }} targets/syscalls.c {{ CROSS_CFLAGS }} -c -o {{ BUILD_DIR }}-{{ module }}/syscalls.o
[group("build-host")]
build-system-syscalls module target="fail":
{{ if target == "fail" { CROSS_CC } else if target == "linux" { LINUX_CC } else if target == "linux-baremetal" { CROSS_CC } else { error("invalid target") } }} \
targets/syscalls.c \
{{ if target == "fail" { CROSS_CFLAGS } else if target == "linux" { LINUX_CFLAGS } else if target == "linux-baremetal" { LINUX_BAREMETAL_CFLAGS } else { error("invalid target") } }} \
-c -o {{ BUILD_DIR }}-{{ module }}/syscalls.o
[doc("Link C-Host, syscall stubs and bootloader")]
[group("build")]
build-system module target="fail": (build-wasm-host module target) (build-system-syscalls module) (build-system-startup module)
{{ CROSS_CC }} -Wl,-T targets/linker.ld \
[group("build-host")]
link-system module target="fail": (build-wasm-host module target) (build-system-syscalls module target) (build-system-startup module target)
{{ if target == "fail" { CROSS_CC } else if target == "linux" { LINUX_CC } else if target == "linux-baremetal" { CROSS_CC } else { error("invalid target") } }} \
{{ if target == "fail" { "-Wl,-T targets/linker.ld" } else { "" } }} \
{{ BUILD_DIR }}-{{ module }}/system.o \
{{ BUILD_DIR }}-{{ module }}/syscalls.o \
{{ BUILD_DIR }}-{{ module }}/startup.o \
{{ CROSS_LDFLAGS }} -o {{ BUILD_DIR }}-{{ module }}/system.elf
{{ if target == "fail" { f"{{BUILD_DIR}}-{{module}}/syscalls.o" + " " + f"{{BUILD_DIR}}-{{module}}/startup.o" } else { "" } }} \
{{ if target == "linux-baremetal" { f"{{BUILD_DIR}}-{{module}}/syscalls.o" } else { "" } }} \
{{ if target == "fail" { CROSS_LDFLAGS } else if target == "linux" { LINUX_LDFLAGS } else if target == "linux-baremetal" { LINUX_BAREMETAL_LDFLAGS } else { error("invalid target") } }} \
-o {{ BUILD_DIR }}-{{ module }}/system.elf
[doc("Create bootdisk")]
[group("build")]
build-iso module target="fail": (build-system module target)
[group("build-host")]
build-iso module target="fail": (link-system module target)
mkdir -p {{ BUILD_DIR }}-{{ module }}/grub/boot/grub
cp targets/grub.cfg {{ BUILD_DIR }}-{{ module }}/grub/boot/grub/
cp {{ BUILD_DIR }}-{{ module }}/system.elf {{ BUILD_DIR }}-{{ module }}/grub/boot/
grub-mkrescue -o {{ BUILD_DIR }}-{{ module }}/system.iso {{ BUILD_DIR }}-{{ module }}/grub
# FAIL* recipes
BOCHS_RUNNER := "bochs-experiment-runner.py"
FAIL_TRACE := "fail-x86-tracing"
FAIL_DUMP := "dump-trace"
FAIL_IMPORT := "import-trace --database-option-file ./db.conf"
FAIL_PRUNE := "prune-trace --database-option-file ./db.conf"
FAIL_SERVER := "generic-experiment-server --database-option-file ./db.conf"
FAIL_INJECT := "generic-experiment-client"
RESULT_BROWSER := "resultbrowser.py -c ./db.conf"
# =================================================================================================================== #
# MySQL recipes
# =================================================================================================================== #
[doc("Start MySQL container to receive FAIL* trace/campaign results")]
[group("fail")]
[group("fail-db")]
start-db:
docker run -d \
--name fail-db \
@ -160,20 +224,33 @@ start-db:
mysql
[doc("Connect to MySQL database using DBeaver")]
[group("fail")]
[group("fail-db")]
connect-db:
dbeaver -con "name=fail|driver=mysql|host=localhost|port=3306|database=fail|user=fail|password=fail"
[doc("Stop MySQL container")]
[group("fail")]
[group("fail-db")]
stop-db:
docker stop fail-db
[doc("Remove MySQL container")]
[group("fail")]
[group("fail-db")]
remove-db:
docker container rm fail-db
# =================================================================================================================== #
# FAIL* recipes
# =================================================================================================================== #
BOCHS_RUNNER := "bochs-experiment-runner.py"
FAIL_TRACE := "fail-x86-tracing"
FAIL_DUMP := "dump-trace"
FAIL_IMPORT := "import-trace"
FAIL_PRUNE := "prune-trace"
FAIL_SERVER := "generic-experiment-server"
FAIL_INJECT := "generic-experiment-client"
RESULT_BROWSER := "resultbrowser.py"
[doc("Trace a golden run using FAIL*")]
[group("fail")]
trace module:
@ -201,33 +278,50 @@ trace module:
[doc("Import a FAIL* golden run trace")]
[group("fail")]
import module:
{{ FAIL_IMPORT }} -t {{ BUILD_DIR }}-{{ module }}/trace.pb -i MemoryImporter \
{{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
-i MemoryImporter \
-e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b mem
{{ FAIL_IMPORT }} -t {{ BUILD_DIR }}-{{ module }}/trace.pb -i RegisterImporter \
{{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
-i RegisterImporter \
-e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b regs --flags
{{ FAIL_IMPORT }} -t {{ BUILD_DIR }}-{{ module }}/trace.pb -i RegisterImporter \
{{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
-i RegisterImporter \
-e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b ip --no-gp --ip
{{ FAIL_IMPORT }} -t {{ BUILD_DIR }}-{{ module }}/trace.pb -i ElfImporter --objdump objdump \
{{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
-i ElfImporter --objdump objdump \
-e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b ip
{{ FAIL_IMPORT }} -t {{ BUILD_DIR }}-{{ module }}/trace.pb -i ElfImporter --objdump objdump \
{{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
-i ElfImporter --objdump objdump \
-e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b mem
{{ FAIL_IMPORT }} -t {{ BUILD_DIR }}-{{ module }}/trace.pb -i ElfImporter --objdump objdump \
{{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
-i ElfImporter --objdump objdump \
-e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b regs
{{ FAIL_IMPORT }} -t {{ BUILD_DIR }}-{{ module }}/trace.pb -i ElfImporter --objdump objdump \
{{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
-i ElfImporter --objdump objdump \
-e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b ip --sources
{{ FAIL_IMPORT }} -t {{ BUILD_DIR }}-{{ module }}/trace.pb -i ElfImporter --objdump objdump \
{{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
-i ElfImporter --objdump objdump \
-e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b mem --sources
{{ FAIL_IMPORT }} -t {{ BUILD_DIR }}-{{ module }}/trace.pb -i ElfImporter --objdump objdump \
{{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
-i ElfImporter --objdump objdump \
-e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b regs --sources
{{ FAIL_PRUNE }} -v {{ module }} -b %% --overwrite
{{ FAIL_PRUNE }} --database-option-file ./db.conf -v {{ module }} -b %% --overwrite
@echo "Next step: \"just server {{ module }}\""
[doc("Start the FAIL* campaign server")]
[group("fail")]
server module:
{{ FAIL_SERVER }} -v {{ module }} -b % --inject-single-bit --inject-registers &
{{ FAIL_SERVER }} --database-option-file ./db.conf -v {{ module }} -b % --inject-single-bit --inject-registers &
@echo "Next step: \"just client {{ module }}\""
[doc("Stop the FAIL* campaign server")]
[group("fail")]
stop-server:
pkill -f {{ FAIL_SERVER }}
[doc("Start a FAIL* campaign client")]
[group("fail")]
client module:
@ -244,7 +338,7 @@ client module:
-Wf,--timeout=500000 \
-Wf,--ok-marker=ok_marker \
-Wf,--fail-marker=fail_marker \
2>/dev/null | grep -B 2 -A 8 'INJECT'
> /dev/null
@echo "Next step: \"just result {{ module }}\" or \"just resultbrowser\""
[doc("Query FAIL* marker statistics from the database")]
@ -263,4 +357,18 @@ result module:
[doc("Start the FAIL* resultbrowser")]
[group("fail")]
resultbrowser:
{{ RESULT_BROWSER }} --host=0.0.0.0 --port=5000
{{ RESULT_BROWSER }} -c ./db.conf --host=0.0.0.0 --port=5000
# =================================================================================================================== #
# Debugging recipes
# =================================================================================================================== #
[doc("Launch radare2 at os_main")]
[group("debug")]
radare module:
radare2 -AA -c "s dbg.os_main; pdf" {{ BUILD_DIR }}-{{ module }}/system.elf
[doc("Launch gdb")]
[group("debug")]
gdb module:
gdb --tui {{ BUILD_DIR }}-{{ module }}/system.elf