From bd6df6b4e970ff8e446b75e7f949adc85ee36956 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Fri, 5 Jun 2026 17:14:34 +0200 Subject: [PATCH] replace build.just with compile.pl --- flake.nix | 160 +-------------- scripts/Util.pm | 23 +++ scripts/build.just | 480 --------------------------------------------- scripts/build.pl | 217 ++++++++++---------- scripts/compile.pl | 417 +++++++++++++++++++++++++++++++++++++++ scripts/menu.pl | 22 ++- 6 files changed, 563 insertions(+), 756 deletions(-) delete mode 100644 scripts/build.just create mode 100644 scripts/compile.pl diff --git a/flake.nix b/flake.nix index 3c5172e..73afe2d 100644 --- a/flake.nix +++ b/flake.nix @@ -6,6 +6,10 @@ rec { flake-utils.url = "github:numtide/flake-utils"; }; + # TODO: Merge wasm_host_pool/_allocator and use #define flags instead + # TODO: Rewrite all perl scripts -> modularize common code and unify menu actions + # TODO: Write a test suite that verifies the build process (against the produced .elf, check if the expected stuff is in there) + outputs = { self, @@ -259,147 +263,6 @@ rec { ''; }; - wamr_src = pkgs.fetchFromGitea { - domain = "gitea.local.chriphost.de"; - owner = "christoph"; - # domain = "git.cs.tu-dortmund.de"; - # owner = "christoph.urlacher"; - repo = "wamr"; - - # With mmap_space in .text.wamr_mmap - rev = "f618cfffaf5ac344fa39abcd16c35278cdacee15"; - hash = "sha256-nOLQLIVImPoClGWvmqgMFeC8s5MHLzkTSl/M03eQDaA="; - - # With fail_marker_group1 in wasm_set_exception_local - # rev = "177fe78618ce3f74ed497c13ea80e8fbad779e82"; - # hash = "sha256-H+ubCmL1YkI7kG7pYBs1+vpROm0hy2sryi0MWeQO3Bs="; - - # With mmap_space in .text.wamr_aot - # rev = "cda2009deb85511089b04b0ac736ad4da2d07e58"; - # hash = "sha256-CN6xTiwzF4Jbrpf21TF5c/C03Xb3urwkibRuIXjoU/w="; - - # Without mmap_space in .text.wamr_aot - # rev = "4e7aed33fe53bf3ee4a3f2fe582c74816f850759"; - # hash = "sha256-/4BKwoFDRfkA+DmbWagxdtkCDAED5rxbz5e4xvjvVWU="; - }; - - wamr = stdenv.mkDerivation { - pname = "wamr"; - version = "2.4.4"; - src = wamr_src; - - dontBuild = true; - installPhase = '' - runHook preInstall - - mkdir -p $out - cp -rv ./* $out/ - - runHook postInstall - ''; - }; - - mkLibiwasm = - { - buildenv, - platform, - buildtype, - cflags, - extraCmakeFlags ? [ ], - patches ? [ ], - }: - buildenv.mkDerivation { - pname = "libiwasm"; - version = "2.4.4"; - src = wamr_src; - - inherit patches; - - nativeBuildInputs = with pkgs; [ cmake ]; - - dontStrip = true; - cmakeBuildType = buildtype; - cmakeFlags = extraCmakeFlags ++ [ - "-DCMAKE_VERBOSE_MAKEFILE=ON" - "-DCMAKE_COLOR_DIAGNOSTICS=ON" - - "-DWAMR_BUILD_PLATFORM=${platform}" - "-DWAMR_BUILD_TARGET=X86_32" - "-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=0" - "-DWAMR_BUILD_SIMD=0" - ]; - - # Since GCC 15, implicit declarations are an error. Disable this. - NIX_CFLAGS_COMPILE = "-Wno-error=implicit-function-declaration " + cflags; - }; - - libiwasm-baremetal-mmap-debug = mkLibiwasm { - buildenv = i386_pkgs.stdenv; - platform = "baremetal"; - buildtype = "Debug"; - cflags = "-O0 -ggdb3"; - extraCmakeFlags = [ - "-DCMAKE_SYSTEM_NAME=Generic" - "-DCMAKE_SYSTEM_PROCESSOR=i386" - "-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY" - ]; - }; - libiwasm-baremetal-mmap-release = mkLibiwasm { - buildenv = i386_pkgs.stdenv; - platform = "baremetal"; - buildtype = "MinSizeRel"; - cflags = "-O2 -ggdb3 -DNDEBUG"; - extraCmakeFlags = [ - "-DCMAKE_SYSTEM_NAME=Generic" - "-DCMAKE_SYSTEM_PROCESSOR=i386" - "-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY" - ]; - }; - libiwasm-baremetal-debug = mkLibiwasm { - buildenv = i386_pkgs.stdenv; - platform = "baremetal"; - buildtype = "Debug"; - cflags = "-O0 -ggdb3"; - patches = [ ./patches/disable_wamr_mmap.patch ]; - extraCmakeFlags = [ - "-DCMAKE_SYSTEM_NAME=Generic" - "-DCMAKE_SYSTEM_PROCESSOR=i386" - "-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY" - ]; - }; - libiwasm-baremetal-release = mkLibiwasm { - buildenv = i386_pkgs.stdenv; - platform = "baremetal"; - buildtype = "MinSizeRel"; - cflags = "-O2 -ggdb3 -DNDEBUG"; - patches = [ ./patches/disable_wamr_mmap.patch ]; - extraCmakeFlags = [ - "-DCMAKE_SYSTEM_NAME=Generic" - "-DCMAKE_SYSTEM_PROCESSOR=i386" - "-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY" - ]; - }; - - libiwasm-linux-debug = mkLibiwasm { - buildenv = pkgs.multiStdenv; - platform = "linux"; - buildtype = "Debug"; - cflags = "-O0 -ggdb3"; - }; - libiwasm-linux-release = mkLibiwasm { - buildenv = pkgs.multiStdenv; - platform = "linux"; - buildtype = "MinSizeRel"; - cflags = "-O2 -ggdb3 -DNDEBUG"; - }; - # =========================================================================================== # Specify dependencies # https://nixos.org/manual/nixpkgs/stable/#ssec-stdenv-dependencies-overview @@ -411,7 +274,7 @@ rec { # - Setup hooks, for example makeWrapper/autoPatchelfHook # - Interpreters needed by patchShebangs for build scripts (with the --build flag), which can be the case for e.g. perl nativeBuildInputs = with pkgs; [ - just + cmake gdb xxd wabt @@ -525,19 +388,10 @@ rec { # Dynamic libraries from buildinputs: # LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath buildInputs; - JUST_WORKING_DIRECTORY = "/home/christoph/Notes/TU/MastersThesis/FailNix"; - JUST_JUSTFILE = "/home/christoph/Notes/TU/MastersThesis/FailNix/scripts/build.just"; - - # Those are read by the justfile + # Those are read by compile.pl / build.pl FAIL_SHARE = "${fail-bin}/share"; WASI_ROOT = wasi-sdk; - WAMR_ROOT = wamr; - LIBIWASM_DEBUG = "${libiwasm-baremetal-debug}/lib"; - LIBIWASM_RELEASE = "${libiwasm-baremetal-release}/lib"; - LIBIWASM_MMAP_DEBUG = "${libiwasm-baremetal-mmap-debug}/lib"; - LIBIWASM_MMAP_RELEASE = "${libiwasm-baremetal-mmap-release}/lib"; - LIBIWASM_LINUX_DEBUG = "${libiwasm-linux-debug}/lib"; - LIBIWASM_LINUX_RELEASE = "${libiwasm-linux-release}/lib"; + WAMR_ROOT = "/home/christoph/Notes/TU/MastersThesis/FailNix/wamr"; CROSS_CC = "${i386_pkgs.stdenv.cc}/bin/i386-elf-gcc"; CROSS_CXX = "${i386_pkgs.stdenv.cc}/bin/i386-elf-g++"; LINUX_CC = "${pkgs.multiStdenv.cc}/bin/gcc"; diff --git a/scripts/Util.pm b/scripts/Util.pm index ace7009..5821bbf 100644 --- a/scripts/Util.pm +++ b/scripts/Util.pm @@ -49,6 +49,29 @@ sub date_now { return $date; } +sub run { + my @cmd = @_; + say " + @cmd"; + system(@cmd) == 0 + or die "Command failed (exit " . ( $? >> 8 ) . "): @cmd\n"; +} + +sub read_file { + my ($file) = @_; + open( my $readhandle, '<', $file ) or die "failed to open $file: $!"; + local $/; + my $content = <$readhandle> // die "failed to read $file: $!"; + close($readhandle) or die "failed to close $file: $!"; + return $content; +} + +sub write_file { + my ( $file, $content ) = @_; + open( my $writehandle, '>', $file ) or die "failed to open $file: $!"; + print $writehandle $content or die "failed to write $file: $!"; + close($writehandle) or die "failed to close $file: $!"; +} + sub rewrite_file { my ( $file, $matches, $replacement ) = @_; diff --git a/scripts/build.just b/scripts/build.just deleted file mode 100644 index 789befa..0000000 --- a/scripts/build.just +++ /dev/null @@ -1,480 +0,0 @@ -BUILD_DIR := "build" - -# Load environment variables set by "nix develop"-shell - -WASI_ROOT := env("WASI_ROOT") -WAMR_ROOT := env("WAMR_ROOT") -LIBIWASM_DEBUG := env("LIBIWASM_DEBUG") -LIBIWASM_RELEASE := env("LIBIWASM_RELEASE") -LIBIWASM_MMAP_DEBUG := env("LIBIWASM_MMAP_DEBUG") -LIBIWASM_MMAP_RELEASE := env("LIBIWASM_MMAP_RELEASE") -LIBIWASM_LINUX_DEBUG := env("LIBIWASM_LINUX_DEBUG") -LIBIWASM_LINUX_RELEASE := env("LIBIWASM_LINUX_RELEASE") -CROSS_CC := env("CROSS_CC") -LINUX_CC := env("LINUX_CC") -WAMR_USE_AOT_SECTION := env_var_or_default("WAMR_USE_AOT_SECTION", "false") - -# =================================================================================================================== # -# Helper recipes -# =================================================================================================================== # - -[default] -[private] -list: - @just --list - -[private] -create-build-dir module: - mkdir -p {{ BUILD_DIR }}-{{ module }} - -[doc("Delete the build directory")] -clean module: - rm -rf {{ BUILD_DIR }}-{{ module }} - -# =================================================================================================================== # -# Just do it -# =================================================================================================================== # -# Somehow this breaks the syntax highlighting: -# [arg("mode", pattern="c|aot|interp", help="Which WASM mode to use")] -# [arg("target", pattern="fail|linux|linux-baremetal", help="Which platform to compile for")] - -[doc("Perform all steps for a fail/linux/linux-bm build with aot/interp WASM")] -[group("5: just do it")] -build module="__help" target="fail" mode="aot": - #!/usr/bin/env sh - - if [ "{{ module }}" = "__help" ]; then - just --usage build - exit 0 - fi - - just clean {{ module }} - just create-build-dir {{ module }} - just copy-auxiliary {{ module }} - - if [ "{{ mode }}" = "aot" ]; then - just build-wasm-module {{ module }} - just build-wasm-aot {{ module }} {{ target }} - just build-wasm-aot-array {{ module }} - - just prepare-aot-host {{ module }} - just build-wasm-host {{ module }} {{ target }} - - just build-system-startup {{ module }} {{ target }} - just build-system-syscalls {{ module }} {{ target }} - just link-system {{ module }} {{ target }} - elif [ "{{ mode }}" = "interp" ]; then - just build-wasm-module {{ module }} - just build-wasm-interp-array {{ module }} - - just prepare-interp-host {{ module }} - just build-wasm-host {{ module }} {{ target }} - - just build-system-startup {{ module }} {{ target }} - just build-system-syscalls {{ module }} {{ target }} - just link-system {{ module }} {{ target }} - elif [ "{{ mode }}" = "c" ]; then - just build-c-module {{ module }} {{ target }} - - just build-c-host {{ module }} {{ target }} - - just build-system-startup {{ module }} {{ target }} - just link-c-system {{ module }} {{ target }} - else - echo "unknown mode: {{ mode }}" >&2 - exit 1 - fi - - just build-iso {{ module }} - -# =================================================================================================================== # -# Build WASM module recipes -# =================================================================================================================== # - -WASI_CC := f"{{WASI_ROOT}}/bin/clang" -WASI_CFLAGS := "\ ---target=wasm32 \ ---sysroot={{WASI_ROOT}}/share/wasi-sysroot \ --z stack-size=4096 \ --O0 \ --nostdlib \ --Wl,--no-entry \ --Wl,--export=wasm_module \ --Wl,--no-gc-sections \ --Wl,--initial-memory=65536 \ --Wl,--export=__heap_base \ --Wl,--export=__data_end \ -" -CROSS_CFLAGS_NOWASM := "\ --O0 \ --m32 \ --ffunction-sections \ --fdata-sections \ --ffreestanding \ --fpermissive \ --ggdb3 \ -" -CROSS_LDFLAGS_NOWASM := "\ --Wl,--build-id=none \ --static \ --nostdlib \ --m32 \ --lc \ --lgcc \ --lm \ -" -LINUX_CFLAGS_NOWASM := "\ --O0 \ --m32 \ --ffunction-sections \ --fdata-sections \ --fpermissive \ --ggdb3 \ -" -LINUX_LDFLAGS_NOWASM := "\ --Wl,--build-id=none \ --m32 \ --lm \ -" -WAMRC := "wamrc" -WAMR_USE_XIP := env("WAMR_USE_XIP", "false") -CROSS_WAMRCFLAGS := "\ ---target=i386 \ ---cpu=generic \ ---opt-level=0 \ -" + if WAMR_USE_XIP == "true" { "--xip" } else { "" } -LINUX_WAMRCFLAGS := "\ ---target=i386 \ ---cpu=generic \ ---opt-level=0 \ -" -XXD := "xxd" - -[doc("C -> WASM: Compile a C function to a WASM module using WASI-SDK")] -[group("1: build module")] -build-wasm-module module: - {{ WASI_CC }} {{ WASI_CFLAGS }} targets/wasm-module/{{ module }}.cpp -o {{ BUILD_DIR }}-{{ module }}/wasm_module.wasm - -[private] -build-wasm-aot-linux module: - {{ WAMRC }} {{ LINUX_WAMRCFLAGS }} -o {{ BUILD_DIR }}-{{ module }}/wasm_module.aot {{ BUILD_DIR }}-{{ module }}/wasm_module.wasm - -[private] -build-wasm-aot-cross module: - {{ WAMRC }} {{ CROSS_WAMRCFLAGS }} -o {{ BUILD_DIR }}-{{ module }}/wasm_module.aot {{ BUILD_DIR }}-{{ module }}/wasm_module.wasm - -[doc("WASM -> AOT: Compile a WASM module ahead-of-time using WAMR")] -[group("1: build module")] -build-wasm-aot module target="fail": - #!/usr/bin/env sh - if [ "{{ target }}" = "fail" ]; then - just build-wasm-aot-cross "{{ module }}" - elif [ "{{ target }}" = "linux" ]; then - just build-wasm-aot-linux "{{ module }}" - elif [ "{{ target }}" = "linux-baremetal" ]; then - just build-wasm-aot-cross "{{ module }}" - else - echo "unknown target: {{ target }}" >&2 - exit 1 - fi - -[doc("AOT -> C-Array: Dump a WASM module compiled ahead-of-time to a binary array")] -[group("1: build module")] -build-wasm-aot-array module: - {{ XXD }} -i {{ BUILD_DIR }}-{{ module }}/wasm_module.aot > {{ BUILD_DIR }}-{{ module }}/wasm_aot_array.c - if [ "{{ WAMR_USE_AOT_SECTION }}" = "true" ]; then \ - sed -i '1s/^/__attribute__((section(".text.wamr_aot"), aligned(4096)))\n/' {{ BUILD_DIR }}-{{ module }}/wasm_aot_array.c; \ - fi - -[doc("WASM -> C-Array: Dump a WASM module to a binary array")] -[group("1: build module")] -build-wasm-interp-array module: - {{ XXD }} -i {{ BUILD_DIR }}-{{ module }}/wasm_module.wasm > {{ BUILD_DIR }}-{{ module }}/wasm_interp_array.c - -[private] -build-c-module-fail module: - {{ CROSS_CC }} {{ CROSS_CFLAGS_NOWASM }} \ - -c targets/wasm-module/{{ module }}.cpp \ - -o {{ BUILD_DIR }}-{{ module }}/c_module.o - -[private] -build-c-module-linux module: - {{ LINUX_CC }} {{ LINUX_CFLAGS_NOWASM }} \ - -c targets/wasm-module/{{ module }}.cpp \ - -o {{ BUILD_DIR }}-{{ module }}/c_module.o - -[doc("C -> Object: Compile a C function (no WASM)")] -[group("1: build module")] -build-c-module module target="fail": - #!/usr/bin/env sh - if [ "{{ target }}" = "fail" ]; then - just build-c-module-fail "{{ module }}" - elif [ "{{ target }}" = "linux" ]; then - just build-c-module-linux "{{ module }}" - else - echo "unknown target: {{ target }}" >&2 - exit 1 - fi - -[private] -copy-auxiliary module: - cp flake.nix {{ BUILD_DIR }}-{{ module }}/flake.nix - cp scripts/runner.pl {{ BUILD_DIR }}-{{ module }}/runner.pl - cp scripts/build.just {{ BUILD_DIR }}-{{ module }}/wasm.just - cp targets/lib.h {{ BUILD_DIR }}-{{ module }}/lib.h - cp targets/linker.ld {{ BUILD_DIR }}-{{ module }}/linker.ld - cp targets/startup.s {{ BUILD_DIR }}-{{ module }}/startup.s - cp targets/syscalls.c {{ BUILD_DIR }}-{{ module }}/syscalls.c - cp targets/wasm-module/{{ module }}.cpp {{ BUILD_DIR }}-{{ module }}/wasm-module.cpp - -# =================================================================================================================== # -# Host program recipes -# =================================================================================================================== # -# FAIL* - -WAMR_USE_MMAP := env("WAMR_USE_MMAP", "false") -ACTIVE_LIBIWASM_DEBUG := if WAMR_USE_MMAP == "true" { LIBIWASM_MMAP_DEBUG } else { LIBIWASM_DEBUG } -CROSS_CFLAGS := f"-I./targets/wasm-host {{CROSS_CFLAGS_NOWASM}}" -CROSS_LDFLAGS := f"-L{{ACTIVE_LIBIWASM_DEBUG}} -liwasm {{CROSS_LDFLAGS_NOWASM}}" -CROSS_INCLUDES := f"\ --I{{WAMR_ROOT}}/core/iwasm/include \ --I{{WAMR_ROOT}}/core/shared/utils \ --I{{WAMR_ROOT}}/core/shared/platform/baremetal \ -" - -# LINUX-POSIX - -LINUX_CFLAGS := f"-I./targets/wasm-host {{LINUX_CFLAGS_NOWASM}}" -LINUX_LDFLAGS := f"-Wl,-rpath,{{LIBIWASM_LINUX_DEBUG}} -L{{LIBIWASM_LINUX_DEBUG}} -liwasm {{LINUX_LDFLAGS_NOWASM}}" -LINUX_INCLUDES := f"\ --I{{WAMR_ROOT}}/core/iwasm/include \ --I{{WAMR_ROOT}}/core/shared/utils \ --I{{WAMR_ROOT}}/core/shared/platform/linux \ -" - -# LINUX-Baremetal - -LINUX_BAREMETAL_CFLAGS := "\ --I./targets/wasm-host \ --O0 \ --m32 \ --ffunction-sections \ --fdata-sections \ --ffreestanding \ --ggdb3 \ -" -LINUX_BAREMETAL_LDFLAGS := f"\ --Wl,--build-id=none \ --static \ --nostdlib \ --m32 \ --L{{ACTIVE_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("Insert the AOT array into the host program")] -[group("2: build host")] -prepare-aot-host module: - cp targets/wasm-host/wasm_host.c {{ BUILD_DIR }}-{{ module }}/module_host.c - sed -i \ - -e "s/__WASM_ARRAY_FILE__/wasm_aot_array.c/g" \ - -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 - -[doc("Insert the WASM array into the host program")] -[group("2: build host")] -prepare-interp-host module: - cp targets/wasm-host/wasm_host.c {{ BUILD_DIR }}-{{ module }}/module_host.c - sed -i \ - -e "s/__WASM_ARRAY_FILE__/wasm_interp_array.c/g" \ - -e "s/__WASM_ARRAY__/build_{{ module }}_wasm_module_wasm/g" \ - -e "s/__WASM_ARRAY_LEN__/build_{{ module }}_wasm_module_wasm_len/g" \ - {{ BUILD_DIR }}-{{ module }}/module_host.c - -[private] -build-wasm-host-fail module: - {{ CROSS_CC }} {{ CROSS_CFLAGS }} {{ CROSS_INCLUDES }} \ - -DTARGET_FAIL \ - -c {{ BUILD_DIR }}-{{ module }}/module_host.c \ - -o {{ BUILD_DIR }}-{{ module }}/system.o - -[private] -build-wasm-host-linux module: - {{ LINUX_CC }} {{ LINUX_CFLAGS }} {{ LINUX_INCLUDES }} \ - -DTARGET_LINUX \ - -c {{ BUILD_DIR }}-{{ module }}/module_host.c \ - -o {{ BUILD_DIR }}-{{ module }}/system.o - -[private] -build-wasm-host-linux-baremetal module: - {{ CROSS_CC }} {{ LINUX_BAREMETAL_CFLAGS }} {{ LINUX_BAREMETAL_INCLUDES }} \ - -DTARGET_LINUX_BAREMETAL \ - -c {{ BUILD_DIR }}-{{ module }}/module_host.c \ - -o {{ BUILD_DIR }}-{{ module }}/system.o - -[doc("Compile C-Host: The host uses WAMR to load the WASM/AOT module")] -[group("2: build host")] -build-wasm-host module target="fail": - #!/usr/bin/env sh - if [ "{{ target }}" = "fail" ]; then - just build-wasm-host-fail "{{ module }}" - elif [ "{{ target }}" = "linux" ]; then - just build-wasm-host-linux "{{ module }}" - elif [ "{{ target }}" = "linux-baremetal" ]; then - just build-wasm-host-linux-baremetal "{{ module }}" - else - echo "unknown target: {{ target }}" >&2 - exit 1 - fi - -[private] -build-c-host-fail module: - {{ CROSS_CC }} {{ CROSS_CFLAGS_NOWASM }} \ - -DTARGET_FAIL \ - -c targets/c-host/c_host.c \ - -o {{ BUILD_DIR }}-{{ module }}/c_host.o - -[private] -build-c-host-linux module: - {{ LINUX_CC }} {{ LINUX_CFLAGS_NOWASM }} \ - -DTARGET_LINUX \ - -c targets/c-host/c_host.c \ - -o {{ BUILD_DIR }}-{{ module }}/c_host.o - -[doc("Insert the C function into the host program (no WASM)")] -[group("2: build host")] -build-c-host module target="fail": - #!/usr/bin/env sh - if [ "{{ target }}" = "fail" ]; then - just build-c-host-fail "{{ module }}" - elif [ "{{ target }}" = "linux" ]; then - just build-c-host-linux "{{ module }}" - else - echo "unknown target: {{ target }}" >&2 - exit 1 - fi - - cp targets/c-host/c_host.c {{ BUILD_DIR }}-{{ module }}/module_host.c - -[private] -build-system-startup-fail module: - {{ CROSS_CC }} targets/startup.s {{ CROSS_CFLAGS }} -c -o {{ BUILD_DIR }}-{{ module }}/startup.o - -[doc("Compile bootloader")] -[group("2: build host")] -build-system-startup module target="fail": - #!/usr/bin/env sh - if [ "{{ target }}" = "fail" ]; then - just build-system-startup-fail "{{ module }}" - else - echo "{{ target }} doesn't need bootloader" - fi - -[private] -build-system-syscalls-fail module: - {{ CROSS_CC }} targets/syscalls.c {{ CROSS_CFLAGS }} -c -o {{ BUILD_DIR }}-{{ module }}/syscalls.o - -[private] -build-system-syscalls-linux-baremetal module: - {{ CROSS_CC }} targets/syscalls.c {{ LINUX_BAREMETAL_CFLAGS }} -c -o {{ BUILD_DIR }}-{{ module }}/syscalls.o - -[doc("Compile newlib syscall stubs")] -[group("2: build host")] -build-system-syscalls module target="fail": - #!/usr/bin/env sh - if [ "{{ target }}" = "fail" ]; then - just build-system-syscalls-fail "{{ module }}" - elif [ "{{ target }}" = "linux-baremetal" ]; then - just build-system-syscalls-linux-baremetal "{{ module }}" - else - echo "{{ target }} doesn't require syscall stubs" - fi - -[private] -link-system-fail module: - {{ CROSS_CC }} \ - -Wl,-T targets/linker.ld \ - {{ BUILD_DIR }}-{{ module }}/system.o \ - {{ BUILD_DIR }}-{{ module }}/startup.o \ - {{ BUILD_DIR }}-{{ module }}/syscalls.o \ - {{ CROSS_LDFLAGS }} \ - -o {{ BUILD_DIR }}-{{ module }}/system.elf - -[private] -link-system-linux module: - {{ LINUX_CC }} \ - {{ BUILD_DIR }}-{{ module }}/system.o \ - {{ LINUX_LDFLAGS }} \ - -o {{ BUILD_DIR }}-{{ module }}/system.elf - -[private] -link-system-linux-baremetal module: - {{ CROSS_CC }} \ - {{ BUILD_DIR }}-{{ module }}/system.o \ - {{ BUILD_DIR }}-{{ module }}/syscalls.o \ - {{ LINUX_BAREMETAL_LDFLAGS }} \ - -o {{ BUILD_DIR }}-{{ module }}/system.elf - -[doc("Link C-Host, syscall stubs and bootloader")] -[group("2: build host")] -link-system module target="fail": - #!/usr/bin/env sh - if [ "{{ target }}" = "fail" ]; then - just link-system-fail "{{ module }}" - elif [ "{{ target }}" = "linux" ]; then - just link-system-linux "{{ module }}" - elif [ "{{ target }}" = "linux-baremetal" ]; then - just link-system-linux-baremetal "{{ module }}" - else - echo "unknown target: {{ target }}" >&2 - exit 1 - fi - -[private] -link-c-system-fail module: - {{ CROSS_CC }} \ - -Wl,-T targets/linker.ld \ - {{ BUILD_DIR }}-{{ module }}/c_host.o \ - {{ BUILD_DIR }}-{{ module }}/startup.o \ - {{ BUILD_DIR }}-{{ module }}/c_module.o \ - {{ CROSS_LDFLAGS_NOWASM }} \ - -o {{ BUILD_DIR }}-{{ module }}/system.elf - -[private] -link-c-system-linux module: - {{ LINUX_CC }} \ - {{ BUILD_DIR }}-{{ module }}/c_host.o \ - {{ BUILD_DIR }}-{{ module }}/c_module.o \ - {{ LINUX_LDFLAGS_NOWASM }} \ - -o {{ BUILD_DIR }}-{{ module }}/system.elf - -[doc("Link C-Host, C-function and bootloader")] -[group("2: build host")] -link-c-system module target="fail": - #!/usr/bin/env sh - if [ "{{ target }}" = "fail" ]; then - just link-c-system-fail "{{ module }}" - elif [ "{{ target }}" = "linux" ]; then - just link-c-system-linux "{{ module }}" - else - echo "unknown target: {{ target }}" >&2 - exit 1 - fi - -[doc("Create bootdisk")] -[group("2: build host")] -build-iso module: - 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 diff --git a/scripts/build.pl b/scripts/build.pl index 10b652b..b84dbac 100755 --- a/scripts/build.pl +++ b/scripts/build.pl @@ -16,17 +16,18 @@ use feature 'say'; my $local_root = '/home/christoph/Notes/TU/MastersThesis/FailNix'; my $local_builds_dir = "$local_root/builds"; my $local_experiments_dir = "$local_root/targets/wasm-module"; -my $justbin = "$local_root/just-bin/just"; -my $justfile = "$local_root/scripts/build.just"; +my $compile_pl = "$local_root/scripts/compile.pl"; -my @targets = ( "fail", "linux", "linux-baremetal" ); -my @modes = ( "c", "aot", "interp" ); +sub compile { + my ( $module, $target, $mode ) = @_; + say "Running: WAMR_USE_AOT_IN_TEXT=$ENV{WAMR_USE_AOT_IN_TEXT}", + " WAMR_USE_MMAP_IN_TEXT=$ENV{WAMR_USE_MMAP_IN_TEXT}", + " WAMR_USE_XIP=$ENV{WAMR_USE_XIP}", + " WAMR_USE_ALLOCATOR=$ENV{WAMR_USE_ALLOCATOR}", + " compile.pl $module $target $mode"; -sub just { - say -"Running: WAMR_USE_AOT_SECTION=$ENV{WAMR_USE_AOT_SECTION} WAMR_USE_MMAP=$ENV{WAMR_USE_MMAP} WAMR_USE_XIP=$ENV{WAMR_USE_XIP} just @_..."; - system("$justbin -d $local_root -f $justfile @_ >/dev/null 2>&1") - and die "Build failed"; + system( 'perl', $compile_pl, $module, $target, $mode ) == 0 + or die "Build failed\n"; sleep(1); } @@ -37,11 +38,13 @@ my @selected_experiments = die "No experiment selected" unless @selected_experiments; # Select targets +my @targets = ( "fail", "linux", "linux-baremetal" ); my @selected_targets = TUI::select_from_list( "Select Target Platforms", 1, @targets ); die "No target selected" unless @selected_targets; # Select modes +my @modes = ( "c", "aot", "interp" ); my @selected_modes = TUI::select_from_list( "Select Execution Modes", 1, @modes ); die "No mode selected" unless @selected_modes; @@ -51,27 +54,53 @@ my @aot_section_variants = ( "Place AOT array in .text.wamr_aot", "Let the linker decide where the AOT array is located", ); -my @selected_aot_variants = +my $selected_aot_variant = TUI::select_from_list( "Select WAMR Array.Text Variant", - 1, @aot_section_variants ); -die "No AOT section variant selected" unless @selected_aot_variants; + 0, @aot_section_variants ); +die "No AOT section variant selected" unless $selected_aot_variant; +local $ENV{WAMR_USE_AOT_IN_TEXT} = + ( $selected_aot_variant eq $aot_section_variants[0] ) + ? "true" + : "false"; # Select WAMR mmap variant my @mmap_variants = ( - "Locate mmap_space in .text.wamr_mmap", + "Place mmap_space in .text.wamr_mmap", "Let the linker decide where mmap_space is located" ); -my @selected_mmap_variants = - TUI::select_from_list( "Select WAMR Mmap.Text Variant", 1, @mmap_variants ); -die "No variant selected" unless @selected_mmap_variants; +my $selected_mmap_variant = + TUI::select_from_list( "Select WAMR Mmap.Text Variant", 0, @mmap_variants ); +die "No variant selected" unless $selected_mmap_variant; +local $ENV{WAMR_USE_MMAP_IN_TEXT} = + ( $selected_mmap_variant eq $mmap_variants[0] ) + ? "true" + : "false"; + +# Select WAMR allocator variant +my @allocator_variants = ( + "Pool allocator (Alloc_With_Pool)", + "Allocator with usage (Alloc_With_Allocator)", +); +my $selected_allocator_variant = + TUI::select_from_list( "Select WAMR Allocator Variant", + 0, @allocator_variants ); +die "No allocator variant selected" unless $selected_allocator_variant; +local $ENV{WAMR_USE_ALLOCATOR} = + ( $selected_allocator_variant eq $allocator_variants[0] ) + ? "true" + : "false"; # Select XIP variant my @xip_variants = ( "Compile AOT with --xip", "Compile AOT without --xip" ); -my @selected_xip_variants = - TUI::select_from_list( "Select WAMRC XIP Variant", 1, @xip_variants ); -die "No XIP variant selected" unless @selected_xip_variants; +my $selected_xip_variant = + TUI::select_from_list( "Select WAMRC XIP Variant", 0, @xip_variants ); +die "No XIP variant selected" unless $selected_xip_variant; +local $ENV{WAMR_USE_XIP} = + ( $selected_xip_variant eq $xip_variants[0] ) + ? "true" + : "false"; -# Select FAIL catch flags (written to runner_flags in each build dir, not a build loop) +# Select FAIL catch flags (written to runner_flags in each build dir) my %catch_flag_map = ( "--catch-outer" => "--catch-outerspace", "--catch-text" => "--catch-write-textsegment", @@ -79,111 +108,71 @@ my %catch_flag_map = ( my @selected_catch_tags = TUI::select_from_list( "Select FAIL Flags", 1, sort keys %catch_flag_map ); -# Select 0.info contents -my $info = join " ", TUI::select_from_list( - "Select '0.info' Contents for ", - 1, - ( - # mmap, xip, wamr_aot, --catch-outer, --catch-text are auto-included from variant selections - "baseline", - ) -); - # Build everything # TODO: linux-baremetal target is broken system( "mkdir", "-p", "$local_builds_dir" ); foreach my $experiment (@selected_experiments) { foreach my $target (@selected_targets) { foreach my $mode (@selected_modes) { - foreach my $mmap_variant (@selected_mmap_variants) { - foreach my $xip_variant (@selected_xip_variants) { - foreach my $aot_variant (@selected_aot_variants) { - # Skip redundant combinations where variants don't apply to this mode - # "c" uses no WAMR at all - # "interp" uses mmap but no AOT - next - if $mode eq "c" - && ( $mmap_variant ne $mmap_variants[0] - || $xip_variant ne $xip_variants[0] - || $aot_variant ne $aot_section_variants[0] ); - next - if $mode eq "interp" - && ( $xip_variant ne $xip_variants[0] - || $aot_variant ne $aot_section_variants[0] ); + my $aot_info = + ( $mode eq "aot" + && $selected_aot_variant eq $aot_section_variants[0] ) + ? "wamr_aot" + : ""; + my $mmap_info = + ( ( $mode eq "aot" || $mode eq "interp" ) + && $selected_mmap_variant eq $mmap_variants[0] ) + ? "wamr_mmap" + : ""; + my $allocator_info = + ( ( $mode eq "aot" || $mode eq "interp" ) + && $selected_allocator_variant eq $allocator_variants[0] ) + ? "alloc_pool" + : "alloc_usage"; + my $xip_info = + ( $mode eq "aot" && $selected_xip_variant eq $xip_variants[0] ) + ? "xip" + : ""; + my $flags_info = join " ", @selected_catch_tags; + my $info_str = join " ", + grep { length } ( + $mode, $aot_info, $mmap_info, + $allocator_info, $xip_info, $flags_info + ); - # These variables control which libiwasm, wamrc flags, and build options are used - local $ENV{WAMR_USE_MMAP} = - ( $mmap_variant eq - "Locate mmap_space in .text.wamr_mmap" ) - ? "true" - : "false"; - local $ENV{WAMR_USE_XIP} = - ( $xip_variant eq "Compile AOT with --xip" ) - ? "true" - : "false"; - local $ENV{WAMR_USE_AOT_SECTION} = - ( $aot_variant eq - "Place AOT array in .text.wamr_aot" ) - ? "true" - : "false"; + # Build experiment + my $date = strftime( "%m-%d_%H-%M-%S", localtime ); + compile( $experiment, $target, $mode ); - my $aot_info = - ( $mode eq "aot" - && $aot_variant eq - "Place AOT array in .text.wamr_aot" ) - ? "wamr_aot" - : ""; - my $mmap_info = - ( ( $mode eq "aot" || $mode eq "interp" ) - && $mmap_variant eq - "Locate mmap_space in .text.wamr_mmap" ) - ? "wamr_mmap" - : ""; - my $xip_info = - ( $mode eq "aot" - && $xip_variant eq "Compile AOT with --xip" ) - ? "xip" - : ""; - my $flags_info = join " ", @selected_catch_tags; - my $info_str = join " ", - grep { length } ( - $mode, $aot_info, $mmap_info, - $xip_info, $flags_info, $info - ); + # Write extra info for the menu + system("echo '$info_str' > $local_root/build-$experiment/0.info"); - # Build experiment - my $date = strftime( "%m-%d_%H-%M-%S", localtime ); - just( "build", $experiment, $target, $mode ); + # Write runner_flags so runner.pl knows which FAIL flags to use + my $runner_flags_path = + "$local_root/build-$experiment/runner_flags"; + open( my $fhandle, '>', $runner_flags_path ) + or die "Cannot write $runner_flags_path: $!"; + print $fhandle "$catch_flag_map{$_}\n" for @selected_catch_tags; + close($fhandle); - # Write extra info for the menu - system( - "echo '$info_str' > $local_root/build-$experiment/0.info" - ); - - # Write runner_flags so runner.pl knows which FAIL flags to use - my $runner_flags_path = - "$local_root/build-$experiment/runner_flags"; - open( my $rfh, '>', $runner_flags_path ) - or die "Cannot write $runner_flags_path: $!"; - print $rfh "$catch_flag_map{$_}\n" - for @selected_catch_tags; - close($rfh); - - my $aot_name = $aot_info ? "_aot-text" : ""; - my $mmap_name = $mmap_info ? "_mmap-text" : ""; - my $xip_name = $xip_info ? "_xip" : ""; - system( - join " ", - ( - "mv", - "$local_root/build-$experiment", - "$local_builds_dir/${date}_${experiment}_${mode}_${target}${aot_name}${mmap_name}${xip_name}", - ) - ); - } - } - } + my $aot_name = $aot_info ? "_aot-text" : ""; + my $mmap_name = $mmap_info ? "_mmap-text" : ""; + my $allocator_name = $allocator_info ? "_alloc-usage" : ""; + my $xip_name = $xip_info ? "_xip" : ""; + system( + join " ", + ( + "mv", + "$local_root/build-$experiment", + join "", + ( + "$local_builds_dir/", + "${date}_${experiment}_${mode}_${target}", + "${aot_name}${mmap_name}${allocator_name}${xip_name}", + ) + ) + ); } } } diff --git a/scripts/compile.pl b/scripts/compile.pl new file mode 100644 index 0000000..3d1d6fe --- /dev/null +++ b/scripts/compile.pl @@ -0,0 +1,417 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use File::Copy qw(copy); +use File::Path qw(rmtree make_path); +use Cwd qw(abs_path); +use FindBin; +use lib $FindBin::Bin; +use Util; + +# ========================================================================================= # +# Project root +# ========================================================================================= # + +my $root = abs_path("$FindBin::Bin/.."); +chdir($root) or die "Cannot chdir to $root: $!\n"; + +# ========================================================================================= # +# Environment set by "nix develop" +# ========================================================================================= # + +sub require_env { + my ($name) = @_; + return $ENV{$name} + // die "$name is not set (run from 'nix develop' shell)\n"; +} + +my $wasi_root = require_env('WASI_ROOT'); +my $wamr_root = require_env('WAMR_ROOT'); +my $cross_cc = require_env('CROSS_CC'); +my $linux_cc = require_env('LINUX_CC'); + +my $use_aot_in_text = ( $ENV{WAMR_USE_AOT_IN_TEXT} // 'false' ) eq 'true'; +my $use_mmap_in_text = ( $ENV{WAMR_USE_MMAP_IN_TEXT} // 'false' ) eq 'true'; +my $use_xip = ( $ENV{WAMR_USE_XIP} // 'false' ) eq 'true'; +my $use_allocator = ( $ENV{WAMR_USE_ALLOCATOR} // 'false' ) eq 'true'; + +# ========================================================================================= # +# WAMR cmake configuration +# ========================================================================================= # + +# Flags common to all platforms +my @wamr_cmake_base = ( + '-DCMAKE_BUILD_TYPE=Debug', '-DWAMR_BUILD_TARGET=X86_32', + '-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=0', + '-DWAMR_BUILD_SIMD=0', +); + +my @wamr_cmake_baremetal = ( + '-DWAMR_BUILD_PLATFORM=baremetal', + '-DCMAKE_SYSTEM_NAME=Generic', + '-DCMAKE_SYSTEM_PROCESSOR=i386', + '-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY', +); + +my @wamr_cmake_linux = ( '-DWAMR_BUILD_PLATFORM=linux', ); + +# Variant defines passed as CMAKE_C_FLAGS +my @variant_cflags = + ( '-Wno-error=implicit-function-declaration', '-O0', '-ggdb3' ); +push @variant_cflags, '-DWAMR_MMAP_IN_TEXT=0' unless $use_mmap_in_text; +push @variant_cflags, '-DWASM_MEM_ALLOC_WITH_USAGE=1' if $use_allocator; +my $cmake_c_flags = join( ' ', @variant_cflags ); + +# Variant options passed as cmake variables +my @variant_cmake_flags; +push @variant_cmake_flags, '-DWAMR_BUILD_ALLOC_WITH_USAGE=1' if $use_allocator; + +# ========================================================================================= # +# Compiler / linker flags +# ========================================================================================= # + +my @wasi_cflags = ( + '--target=wasm32', "--sysroot=$wasi_root/share/wasi-sysroot", + '-z', 'stack-size=4096', + '-O0', '-nostdlib', + '-Wl,--no-entry', '-Wl,--export=wasm_module', + '-Wl,--no-gc-sections', '-Wl,--initial-memory=65536', + '-Wl,--export=__heap_base', '-Wl,--export=__data_end', +); + +my @cross_cflags = + qw(-O0 -m32 -ffunction-sections -fdata-sections -ffreestanding -fpermissive -ggdb3); +my @linux_cflags = + qw(-O0 -m32 -ffunction-sections -fdata-sections -fpermissive -ggdb3); +my @linux_baremetal_cflags = + qw(-O0 -m32 -ffunction-sections -fdata-sections -ffreestanding -ggdb3); + +# libiwasm.a is passed as a direct file path (no -L/-liwasm) +my @cross_ldflags_base = ( + '-Wl,--build-id=none', '-static', '-nostdlib', '-m32', + '-lc', '-lgcc', '-lm' +); +my @linux_ldflags_base = ( '-Wl,--build-id=none', '-m32', '-lm' ); +my @baremetal_ldflags_base = ( + '-Wl,--build-id=none', '-static', '-nostdlib', '-m32', + '-lc', '-lgcc', '-lm', '--entry', + 'main' +); +my @cross_ldflags_nowasm = ( + '-Wl,--build-id=none', '-static', '-nostdlib', '-m32', + '-lc', '-lgcc', '-lm' +); +my @linux_ldflags_nowasm = ( '-Wl,--build-id=none', '-m32', '-lm' ); + +my @wamr_inc_baremetal = ( + "-I$wamr_root/core/iwasm/include", + "-I$wamr_root/core/shared/utils", + "-I$wamr_root/core/shared/platform/baremetal", +); +my @wamr_inc_linux = ( + "-I$wamr_root/core/iwasm/include", + "-I$wamr_root/core/shared/utils", + "-I$wamr_root/core/shared/platform/linux", +); + +my @cross_wamrcflags = ( '--target=i386', '--cpu=generic', '--opt-level=0' ); +push @cross_wamrcflags, '--xip' if $use_xip; +my @linux_wamrcflags = ( '--target=i386', '--cpu=generic', '--opt-level=0' ); + +# ========================================================================================= # +# Entry point +# ========================================================================================= # + +die "Usage: compile.pl \n" + . " target: fail | linux | linux-baremetal\n" + . " mode: aot | interp | c\n" + unless @ARGV == 3; + +my ( $module, $target, $mode ) = @ARGV; + +build( $module, $target, $mode ); + +# ========================================================================================= # +# Build +# ========================================================================================= # + +sub build { + my ( $module, $target, $mode ) = @_; + my $bd = "build-$module"; + + rmtree($bd); + make_path($bd); + copy_auxiliary( $module, $bd ); + build_libiwasm( $module, $bd, $target ); + + if ( $mode eq 'aot' || $mode eq 'interp' ) { + compile_wasm_module( $module, $bd ); + if ( $mode eq 'aot' ) { + compile_wasm_aot( $module, $bd, $target ); + make_aot_array( $module, $bd ); + } + else { + make_interp_array( $module, $bd ); + } + prepare_wasm_host( $module, $bd, $mode ); + compile_wasm_host( $module, $bd, $target ); + compile_startup( $module, $bd, $target ); + compile_syscalls( $module, $bd, $target ); + link_wasm( $module, $bd, $target ); + } + elsif ( $mode eq 'c' ) { + compile_c_module( $module, $bd, $target ); + compile_c_host( $module, $bd, $target ); + compile_startup( $module, $bd, $target ); + link_c( $module, $bd, $target ); + } + else { + die "Unknown mode '$mode'; expected: aot, interp, c\n"; + } + + build_iso( $module, $bd ); +} + +# ========================================================================================= # +# Steps +# ========================================================================================= # + +sub build_libiwasm { + my ( $module, $bd, $target ) = @_; + my $cmake_bd = "$bd/wamr-build"; + make_path($cmake_bd); + + my ( $cmake_cc, @sys_flags ) = + ( $target eq 'linux' ) + ? ( $linux_cc, @wamr_cmake_linux ) + : ( $cross_cc, @wamr_cmake_baremetal ); + + Util::run( + 'cmake', '-S', + $wamr_root, '-B', + $cmake_bd, "-DCMAKE_C_COMPILER=$cmake_cc", + @wamr_cmake_base, @sys_flags, + @variant_cmake_flags, "-DCMAKE_C_FLAGS=$cmake_c_flags", + ); + Util::run( 'cmake', '--build', $cmake_bd ); + copy( "$cmake_bd/libiwasm.a", "$bd/libiwasm.a" ) + or die "failed to copy $cmake_bd/libiwasm.a: $!"; +} + +sub copy_auxiliary { + my ( $module, $bd ) = @_; + my @files = ( + [ 'flake.nix', "$bd/flake.nix" ], + [ 'scripts/runner.pl', "$bd/runner.pl" ], + [ 'scripts/compile.pl', "$bd/compile.pl" ], + [ 'targets/lib.h', "$bd/lib.h" ], + [ 'targets/linker.ld', "$bd/linker.ld" ], + [ 'targets/startup.s', "$bd/startup.s" ], + [ 'targets/syscalls.c', "$bd/syscalls.c" ], + [ "targets/wasm-module/$module.cpp", "$bd/wasm-module.cpp" ], + ); + for my $pair (@files) { + copy( $pair->[0], $pair->[1] ) + or die "failed to copy $pair->[0] -> $pair->[1]: $!"; + } +} + +sub compile_wasm_module { + my ( $module, $bd ) = @_; + Util::run( "$wasi_root/bin/clang", @wasi_cflags, + "targets/wasm-module/$module.cpp", + '-o', "$bd/wasm_module.wasm" ); +} + +sub compile_wasm_aot { + my ( $module, $bd, $target ) = @_; + my @flags = ( $target eq 'linux' ) ? @linux_wamrcflags : @cross_wamrcflags; + Util::run( 'wamrc', @flags, '-o', "$bd/wasm_module.aot", "$bd/wasm_module.wasm" ); +} + +sub make_aot_array { + my ( $module, $bd ) = @_; + open( my $xxd, '-|', 'xxd', '-i', "$bd/wasm_module.aot" ) + or die "failed to run xxd: $!"; + my $content = do { local $/; <$xxd> }; + close($xxd); + + $content = + qq{__attribute__((section(".text.wamr_aot"), aligned(4096)))\n} + . $content + if $use_aot_in_text; + + Util::write_file( "$bd/wasm_aot_array.c", $content ); +} + +sub make_interp_array { + my ( $module, $bd ) = @_; + open( my $xxd, '-|', 'xxd', '-i', "$bd/wasm_module.wasm" ) + or die "Cannot run xxd: $!\n"; + my $content = do { local $/; <$xxd> }; + close($xxd); + Util::write_file( "$bd/wasm_interp_array.c", $content ); +} + +sub prepare_wasm_host { + my ( $module, $bd, $mode ) = @_; + my $template = + $use_allocator + ? 'targets/wasm-host/wasm_host_allocator.c' + : 'targets/wasm-host/wasm_host_pool.c'; + + my $mod_c = ( $module =~ s/-/_/gr ); + my ( $array_file, $array_sym, $len_sym ) = + ( $mode eq 'aot' ) + ? ( + 'wasm_aot_array.c', + "build_${mod_c}_wasm_module_aot", + "build_${mod_c}_wasm_module_aot_len" + ) + : ( + 'wasm_interp_array.c', + "build_${mod_c}_wasm_module_wasm", + "build_${mod_c}_wasm_module_wasm_len" + ); + + my $content = Util::read_file($template); + $content =~ s/__WASM_ARRAY_FILE__/$array_file/g; + $content =~ s/__WASM_ARRAY__/$array_sym/g; + $content =~ s/__WASM_ARRAY_LEN__/$len_sym/g; + Util::write_file( "$bd/module_host.c", $content ); +} + +sub compile_wasm_host { + my ( $module, $bd, $target ) = @_; + if ( $target eq 'fail' ) { + Util::run( + $cross_cc, '-I./targets/wasm-host', + @cross_cflags, @wamr_inc_baremetal, + '-DTARGET_FAIL', '-c', + "$bd/module_host.c", '-o', + "$bd/system.o" + ); + } + elsif ( $target eq 'linux' ) { + Util::run( + $linux_cc, '-I./targets/wasm-host', + @linux_cflags, @wamr_inc_linux, + '-DTARGET_LINUX', '-c', + "$bd/module_host.c", '-o', + "$bd/system.o" + ); + } + elsif ( $target eq 'linux-baremetal' ) { + Util::run( + $cross_cc, '-I./targets/wasm-host', + @linux_baremetal_cflags, @wamr_inc_baremetal, + '-DTARGET_LINUX_BAREMETAL', '-c', + "$bd/module_host.c", '-o', + "$bd/system.o" + ); + } + else { + die "Unknown target '$target'\n"; + } +} + +sub compile_c_module { + my ( $module, $bd, $target ) = @_; + my ( $cc, @flags ) = + ( $target eq 'linux' ) + ? ( $linux_cc, @linux_cflags ) + : ( $cross_cc, @cross_cflags ); + Util::run( $cc, @flags, '-c', "targets/wasm-module/$module.cpp", + '-o', "$bd/c_module.o" ); +} + +sub compile_c_host { + my ( $module, $bd, $target ) = @_; + my ( $cc, @flags ) = + ( $target eq 'linux' ) + ? ( $linux_cc, @linux_cflags, '-DTARGET_LINUX' ) + : ( $cross_cc, @cross_cflags, '-DTARGET_FAIL' ); + Util::run( $cc, @flags, '-c', 'targets/c-host/c_host.c', '-o', "$bd/c_host.o" ); + copy( 'targets/c-host/c_host.c', "$bd/module_host.c" ); +} + +sub compile_startup { + my ( $module, $bd, $target ) = @_; + return unless $target eq 'fail'; + Util::run( $cross_cc, 'targets/startup.s', '-I./targets/wasm-host', @cross_cflags, + '-c', '-o', "$bd/startup.o" ); +} + +sub compile_syscalls { + my ( $module, $bd, $target ) = @_; + if ( $target eq 'fail' ) { + Util::run( $cross_cc, 'targets/syscalls.c', + '-I./targets/wasm-host', @cross_cflags, + '-c', '-o', "$bd/syscalls.o" ); + } + elsif ( $target eq 'linux-baremetal' ) { + Util::run( $cross_cc, 'targets/syscalls.c', @linux_baremetal_cflags, + '-c', '-o', "$bd/syscalls.o" ); + } + + # Linux needs neither startup nor syscall stubs +} + +sub link_wasm { + my ( $module, $bd, $target ) = @_; + if ( $target eq 'fail' ) { + Util::run( + $cross_cc, '-Wl,-T', + 'targets/linker.ld', "$bd/system.o", + "$bd/startup.o", "$bd/syscalls.o", + "$bd/libiwasm.a", @cross_ldflags_base, + '-o', "$bd/system.elf" + ); + } + elsif ( $target eq 'linux' ) { + Util::run( $linux_cc, "$bd/system.o", "$bd/libiwasm.a", @linux_ldflags_base, + '-o', "$bd/system.elf" ); + } + elsif ( $target eq 'linux-baremetal' ) { + Util::run( $cross_cc, "$bd/system.o", "$bd/syscalls.o", "$bd/libiwasm.a", + @baremetal_ldflags_base, '-o', "$bd/system.elf" ); + } + else { + die "Unknown target '$target'\n"; + } +} + +sub link_c { + my ( $module, $bd, $target ) = @_; + if ( $target eq 'fail' ) { + Util::run( + $cross_cc, '-Wl,-T', + 'targets/linker.ld', "$bd/c_host.o", + "$bd/startup.o", "$bd/c_module.o", + @cross_ldflags_nowasm, '-o', + "$bd/system.elf" + ); + } + elsif ( $target eq 'linux' ) { + Util::run( $linux_cc, "$bd/c_host.o", "$bd/c_module.o", @linux_ldflags_nowasm, + '-o', "$bd/system.elf" ); + } + else { + die "C mode is not supported for target '$target'\n"; + } +} + +sub build_iso { + my ( $module, $bd ) = @_; + make_path("$bd/grub/boot/grub"); + copy( 'targets/grub.cfg', "$bd/grub/boot/grub/grub.cfg" ) + or die "failed to copy grub.cfg: $!\n"; + copy( "$bd/system.elf", "$bd/grub/boot/system.elf" ) + or die "failed to copy system.elf: $!\n"; + Util::run( 'grub-mkrescue', '-o', "$bd/system.iso", "$bd/grub" ); +} diff --git a/scripts/menu.pl b/scripts/menu.pl index 517b0f2..62c3865 100644 --- a/scripts/menu.pl +++ b/scripts/menu.pl @@ -14,19 +14,14 @@ use Text::CSV_XS; use feature 'say'; -# TODO: Less navigation if the object is selected first, then the action -# - Differentiate between local/remote object -# - List all types in the same list? -# - Select actions afterwards and apply fitting ones all in one go -# - Hide unfeasible actions # TODO: Much can be extracted into utility functions my $local_obsidian = '/home/christoph/Notes/Obsidian/Chriphost'; my $local_obsidian_attach = "$local_obsidian/attach"; -my $local_wamr = '/home/christoph/Notes/TU/MastersThesis/05 WAMR'; my $local_newlib = '/home/christoph/Notes/TU/MastersThesis/07 NewLib'; my $local_root = '/home/christoph/Notes/TU/MastersThesis/FailNix'; +my $local_wamr = "$local_root/wamr"; my $local_scripts_dir = "$local_root/scripts"; my $local_builds_dir = "$local_root/builds"; my $local_archive_dir = "$local_root/injections"; @@ -405,8 +400,7 @@ my %handlers = ( my $selected_build = $selected_builds[0]; my $build_dir = "$local_builds_dir/$selected_build"; - my $build_name = $selected_build =~ s/.*-.*-.*:.*:.*?_(.*)-.*-.*/$1/r; - my $module_source = "$local_root/build-$build_name"; + my $build_name = $selected_build =~ s/.*?_.*?_(.*?)_.*$/$1/r; say "$build_name"; @@ -418,7 +412,7 @@ my %handlers = ( '-ex', 'set disassembly-flavor intel', '-ex', - "set substitute-path '$module_source' '$build_dir'", + "set substitute-path 'build-$build_name' '$build_dir'", '-ex', "set substitute-path '/build/source/core' '$local_wamr/core'", '-ex', @@ -433,6 +427,16 @@ my %handlers = ( 'break fail_marker_detected', '-ex', 'break fail_marker_negative', + '-ex', + 'break fail_marker_group1', + '-ex', + 'break os_mmap', + '-ex', + 'break wamr_malloc', + '-ex', + 'break wamr_realloc', + '-ex', + 'break wamr_free', ); },