diff --git a/flake.nix b/flake.nix index 252e03c..553f436 100644 --- a/flake.nix +++ b/flake.nix @@ -6,27 +6,29 @@ rec { flake-utils.url = "github:numtide/flake-utils"; }; - outputs = { - self, - nixpkgs, - flake-utils, - }: - # Create a shell (and possibly package) for each possible system, not only x86_64-linux + outputs = + { + self, + nixpkgs, + flake-utils, + }: + # Create a shell (and possibly package) for each possible system, not only x86_64-linux flake-utils.lib.eachDefaultSystem ( - system: let + system: + let # ========================================================================================= # Define pkgs/stdenvs # ========================================================================================= pkgs = import nixpkgs { inherit system; config.allowUnfree = true; - overlays = []; + overlays = [ ]; }; i386_pkgs = import nixpkgs { inherit system; config.allowUnfree = true; - overlays = []; + overlays = [ ]; # Build crosscompiler crossSystem = { @@ -37,23 +39,23 @@ rec { boost_pkgs = import - (builtins.fetchTarball { - url = "https://github.com/NixOS/nixpkgs/archive/824421b1796332ad1bcb35bc7855da832c43305f.tar.gz"; - sha256 = "sha256:1w6cjnakz1yi66rs8c6nmhymsr7bj82vs2hz200ipi1sfiq8dy4y"; - }) - { - inherit system; - }; + (builtins.fetchTarball { + url = "https://github.com/NixOS/nixpkgs/archive/824421b1796332ad1bcb35bc7855da832c43305f.tar.gz"; + sha256 = "sha256:1w6cjnakz1yi66rs8c6nmhymsr7bj82vs2hz200ipi1sfiq8dy4y"; + }) + { + inherit system; + }; libdwarf_pkgs = import - (builtins.fetchTarball { - url = "https://github.com/NixOS/nixpkgs/archive/f597e7e9fcf37d8ed14a12835ede0a7d362314bd.tar.gz"; - sha256 = "sha256:1l79hh7jh7m8yc5mvc8dbg6s8rf30bgm994kf07xriqbzwfn158r"; - }) - { - inherit system; - }; + (builtins.fetchTarball { + url = "https://github.com/NixOS/nixpkgs/archive/f597e7e9fcf37d8ed14a12835ede0a7d362314bd.tar.gz"; + sha256 = "sha256:1l79hh7jh7m8yc5mvc8dbg6s8rf30bgm994kf07xriqbzwfn158r"; + }) + { + inherit system; + }; inherit (pkgs) lib stdenv; @@ -62,38 +64,39 @@ rec { # ========================================================================================= # Setup the shell when entering the "nix develop" environment (bash script). - shellHook = let - # Add project-local fish abbrs here - abbrs = { - fail = "perl ./scripts/menu.pl"; + shellHook = + let + # Add project-local fish abbrs here + abbrs = { + fail = "perl ./scripts/menu.pl"; - # C/C++: - # cmake-debug = "${cmakeDebug}"; - # cmake-release = "${cmakeRelease}"; - # build-debug = "${buildDebug}"; - # build-release = "${buildRelease}"; - }; + # C/C++: + # cmake-debug = "${cmakeDebug}"; + # cmake-release = "${cmakeRelease}"; + # build-debug = "${buildDebug}"; + # build-release = "${buildRelease}"; + }; - eraseAbbr = name: value: "abbr --erase ${name} 2>/dev/null"; - createAbbr = name: value: ''abbr -a ${name} "${value}"''; + eraseAbbr = name: value: "abbr --erase ${name} 2>/dev/null"; + createAbbr = name: value: ''abbr -a ${name} "${value}"''; - # This will be sourced by the global fish config if INIT_PROJECT_SHELL gets unset - unloadProjectShell = pkgs.writers.writeFish "unload-shell.fish" '' - echo "Unloading \"${description}\" environment..." + # This will be sourced by the global fish config if INIT_PROJECT_SHELL gets unset + unloadProjectShell = pkgs.writers.writeFish "unload-shell.fish" '' + echo "Unloading \"${description}\" environment..." - ${builtins.concatStringsSep "\n" (lib.mapAttrsToList eraseAbbr abbrs)} - ''; + ${builtins.concatStringsSep "\n" (lib.mapAttrsToList eraseAbbr abbrs)} + ''; - # This will be sourced by the global fish config if INIT_PROJECT_SHELL gets set - initProjectShell = pkgs.writers.writeFish "init-shell.fish" '' - # Unload just in case, to not have redefinition errors - source ${unloadProjectShell} + # This will be sourced by the global fish config if INIT_PROJECT_SHELL gets set + initProjectShell = pkgs.writers.writeFish "init-shell.fish" '' + # Unload just in case, to not have redefinition errors + source ${unloadProjectShell} - echo "Sourcing \"${description}\" environment..." + echo "Sourcing \"${description}\" environment..." - ${builtins.concatStringsSep "\n" (lib.mapAttrsToList createAbbr abbrs)} - ''; - in + ${builtins.concatStringsSep "\n" (lib.mapAttrsToList createAbbr abbrs)} + ''; + in builtins.concatStringsSep "\n" [ # Launch into pure fish shell '' @@ -114,13 +117,12 @@ rec { # =========================================================================================== python = pkgs.python314.withPackages ( - p: - with p; [ - setuptools - flask - flask-mysqldb - pyyaml - ] + p: with p; [ + setuptools + flask + flask-mysqldb + pyyaml + ] ); # perl = pkgs.perl.withPackages (p: @@ -161,9 +163,10 @@ rec { pname = "wasi-sdk"; version = "29"; - src = let - baseurl = "https://github.com/WebAssembly/wasi-sdk/releases/download"; - in + src = + let + baseurl = "https://github.com/WebAssembly/wasi-sdk/releases/download"; + in builtins.fetchTarball { url = "${baseurl}/${pname}-${version}/${pname}-${version}.0-x86_64-linux.tar.gz"; sha256 = "sha256:16afis71iqfvwiny4dz0lk9f7wbary0wa67ybwyhywr8g57ss6hq"; @@ -192,9 +195,10 @@ rec { pname = "iwasm"; version = "2.4.4"; - src = let - baseurl = "https://github.com/bytecodealliance/wasm-micro-runtime/releases/download"; - in + src = + let + baseurl = "https://github.com/bytecodealliance/wasm-micro-runtime/releases/download"; + in builtins.fetchTarball { url = "${baseurl}/WAMR-${version}/${pname}-${version}-x86_64-ubuntu-22.04.tar.gz"; sha256 = "sha256:05irihz3yf7hpc0a59qz9i62imhrsni9xy9nxwsn6b8s92c1yzrp"; @@ -225,9 +229,10 @@ rec { pname = "wamrc"; version = "2.4.4"; - src = let - baseurl = "https://github.com/bytecodealliance/wasm-micro-runtime/releases/download"; - in + src = + let + baseurl = "https://github.com/bytecodealliance/wasm-micro-runtime/releases/download"; + in builtins.fetchTarball { url = "${baseurl}/WAMR-${version}/${pname}-${version}-x86_64-ubuntu-22.04.tar.gz"; sha256 = "sha256:0264arh03gc35z0zdvw07qdvqgfvsxr3qgl1aszghwicmdmh4sqj"; @@ -294,50 +299,75 @@ rec { ''; }; - mkLibiwasm = { - buildenv, - platform, - buildtype, - cflags, - extraCmakeFlags ? [], - }: + mkLibiwasm = + { + buildenv, + platform, + buildtype, + cflags, + extraCmakeFlags ? [ ], + patches ? [ ], + }: buildenv.mkDerivation { pname = "libiwasm"; version = "2.4.4"; src = wamr_src; - nativeBuildInputs = with pkgs; [cmake]; + inherit patches; + + nativeBuildInputs = with pkgs; [ cmake ]; dontStrip = true; cmakeBuildType = buildtype; - cmakeFlags = - extraCmakeFlags - ++ [ - "-DCMAKE_VERBOSE_MAKEFILE=ON" - "-DCMAKE_COLOR_DIAGNOSTICS=ON" + 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" - ]; + "-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" @@ -349,6 +379,7 @@ rec { platform = "baremetal"; buildtype = "MinSizeRel"; cflags = "-O2 -ggdb3 -DNDEBUG"; + patches = [ ./patches/disable_wamr_mmap.patch ]; extraCmakeFlags = [ "-DCMAKE_SYSTEM_NAME=Generic" "-DCMAKE_SYSTEM_PROCESSOR=i386" @@ -407,7 +438,7 @@ rec { # Add dependencies to buildInputs if they will end up copied or linked into the final output or otherwise used at runtime: # - Libraries used by compilers, for example zlib # - Interpreters needed by patchShebangs for scripts which are installed, which can be the case for e.g. perl - buildInputs = with pkgs; []; + buildInputs = with pkgs; [ ]; # =========================================================================================== # Define buildable + installable packages @@ -462,7 +493,8 @@ rec { runHook postInstall ''; }; - in { + in + { # Provide package for "nix build" packages = { default = fail-bin; @@ -472,7 +504,7 @@ rec { wamrc = wamrc; }; apps = { - default = flake-utils.lib.mkApp {drv = fail-bin;}; + default = flake-utils.lib.mkApp { drv = fail-bin; }; fail = fail-bin; wasi-sdk = wasi-sdk; iwasm = iwasm; @@ -502,6 +534,8 @@ rec { 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"; CROSS_CC = "${i386_pkgs.stdenv.cc}/bin/i386-elf-gcc"; diff --git a/patches/disable_wamr_mmap.patch b/patches/disable_wamr_mmap.patch new file mode 100644 index 0000000..16374cb --- /dev/null +++ b/patches/disable_wamr_mmap.patch @@ -0,0 +1,13 @@ +diff --git a/core/shared/platform/baremetal/platform_init.c b/core/shared/platform/baremetal/platform_init.c +index bbf7c102..f0fd43b7 100644 +--- a/core/shared/platform/baremetal/platform_init.c ++++ b/core/shared/platform/baremetal/platform_init.c +@@ -45,7 +45,7 @@ os_dumps_proc_mem_info(char *out, unsigned int size) + // #12 0x08048112 in main + + // To place mmap memory inside the .text segment: +-__attribute__((section(".text.wamr_mmap"), aligned(4096))) ++// __attribute__((section(".text.wamr_mmap"), aligned(4096))) + static uint8_t mmap_space[MMAP_SPACE_SIZE]; + static size_t mmap_offset = 0; // Free space begins here + diff --git a/scripts/build.pl b/scripts/build.pl index a32201d..7130de0 100755 --- a/scripts/build.pl +++ b/scripts/build.pl @@ -45,12 +45,24 @@ my @selected_modes = TUI::select_from_list( "Select Execution Modes", 1, @modes ); die "No mode selected" unless @selected_modes; +# Select WAMR mmap variant +my @wamr_variants = ( "no .wamr_mmap", ".wamr_mmap" ); +my @selected_variants = + TUI::select_from_list( "Select WAMR mmap Variant", 1, @wamr_variants ); +die "No variant selected" unless @selected_variants; + +# Select XIP variant +my @xip_variants = ( "no --xip", "--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; + # Select 0.info contents my $info = join " ", TUI::select_from_list( "Select '0.info' Contents for ", 1, ( "baseline", "--catch-outer", "--catch-text", - ".wamr_mmap", + # ".wamr_mmap", ".wamr_aot", )); @@ -60,18 +72,28 @@ system( "mkdir", "-p", "$local_builds_dir" ); foreach my $experiment (@selected_experiments) { foreach my $target (@selected_targets) { foreach my $mode (@selected_modes) { - just( "build", $experiment, $target, $mode ); + foreach my $variant (@selected_variants) { + foreach my $xip_variant (@selected_xip_variants) { + # These variables control which libiwasm and wamrc flags are used + local $ENV{WAMR_USE_MMAP} = ( $variant eq ".wamr_mmap" ) ? "true" : "false"; + local $ENV{WAMR_USE_XIP} = ( $xip_variant eq "--xip" ) ? "true" : "false"; - system( "echo '$mode $info' > $local_root/build-$experiment/0.info" ); + just( "build", $experiment, $target, $mode ); - system( - join " ", - ( - "mv", - "$local_root/build-$experiment", - "$local_builds_dir/${date}_$experiment-$target-$mode", - ) - ); + my $mmap = $variant eq ".wamr_mmap" ? ".wamr_mmap" : ""; + my $xip = $xip_variant eq "--xip" ? "--xip" : ""; + system( "echo '$mode $mmap $info $xip' > $local_root/build-$experiment/0.info" ); + + system( + join " ", + ( + "mv", + "$local_root/build-$experiment", + "$local_builds_dir/${date}_$experiment-$target-$mode", + ) + ); + } + } } } } diff --git a/scripts/nixos.just b/scripts/nixos.just index ef0304a..2db3fa1 100644 --- a/scripts/nixos.just +++ b/scripts/nixos.just @@ -10,6 +10,8 @@ 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") diff --git a/scripts/runner.pl b/scripts/runner.pl index 90eba7a..04bd775 100644 --- a/scripts/runner.pl +++ b/scripts/runner.pl @@ -223,7 +223,8 @@ sub inject { # Catch invalid instruction pointers "-Wf,--catch-outerspace", - # Messes with AOT compilation target if mmap memory is part of .text + # Messes with Wasm compilation targets if mmap memory is part of .text + # (because the linear memory is allocated using os_mmap) "-Wf,--catch-write-textsegment", "-Wf,--timeout=500000", diff --git a/scripts/wasm.just b/scripts/wasm.just index 6be71f1..e4ab237 100644 --- a/scripts/wasm.just +++ b/scripts/wasm.just @@ -48,12 +48,12 @@ LINUX_LDFLAGS_NOWASM := "\ -lm \ " WAMRC := "wamrc" +WAMR_USE_XIP := env_var_or_default("WAMR_USE_XIP", "false") CROSS_WAMRCFLAGS := "\ --target=i386 \ --cpu=generic \ --opt-level=0 \ ---xip \ -" +" + if WAMR_USE_XIP == "true" { "--xip" } else { "" } LINUX_WAMRCFLAGS := "\ --target=i386 \ --cpu=generic \ @@ -74,7 +74,6 @@ build-wasm-aot-linux module: 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": @@ -144,8 +143,10 @@ copy-auxiliary module: # =================================================================================================================== # # FAIL* +WAMR_USE_MMAP := env_var_or_default("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{{LIBIWASM_DEBUG}} -liwasm {{CROSS_LDFLAGS_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 \ @@ -178,7 +179,7 @@ LINUX_BAREMETAL_LDFLAGS := f"\ -static \ -nostdlib \ -m32 \ --L{{LIBIWASM_DEBUG}} \ +-L{{ACTIVE_LIBIWASM_DEBUG}} \ -liwasm \ -lc \ -lgcc \ diff --git a/targets/wasm-module/sum1_repl_late.cpp b/targets/wasm-module/sum1_repl_late.cpp index 2f03c0b..f42af17 100644 --- a/targets/wasm-module/sum1_repl_late.cpp +++ b/targets/wasm-module/sum1_repl_late.cpp @@ -36,10 +36,13 @@ extern "C" EXPORT("wasm_module") int wasm_module(void) { YC = 0; ZC = 0; + // "early" starts trace here + sum<0>(); sum<1>(); sum<2>(); + // "late" starts trace here fail_start_trace(); naive_vote(); diff --git a/targets/wasm-module/sum2_repl_cored_late.cpp b/targets/wasm-module/sum2_repl_cored_late.cpp index 5bb4142..d82d58f 100644 --- a/targets/wasm-module/sum2_repl_cored_late.cpp +++ b/targets/wasm-module/sum2_repl_cored_late.cpp @@ -69,10 +69,13 @@ extern "C" EXPORT("wasm_module") int wasm_module(void) { YC = 0; ZC = 0; + // "early" starts trace here + sum<0, SIG_X>(); sum<1, SIG_Y>(); sum<2, SIG_Z>(); + // "late" starts trace here fail_start_trace(); sign_t static_sig = cored_vote(); diff --git a/targets/wasm-module/sum3_repl_early.cpp b/targets/wasm-module/sum3_repl_early.cpp index 2e87373..b1f5a6c 100644 --- a/targets/wasm-module/sum3_repl_early.cpp +++ b/targets/wasm-module/sum3_repl_early.cpp @@ -36,12 +36,15 @@ extern "C" EXPORT("wasm_module") int wasm_module(void) { YC = 0; ZC = 0; + // "early" starts trace here fail_start_trace(); sum<0>(); sum<1>(); sum<2>(); + // "late" starts trace here + naive_vote(); fail_stop_trace(); diff --git a/targets/wasm-module/sum4_repl_cored_early.cpp b/targets/wasm-module/sum4_repl_cored_early.cpp index bf9f133..14d8537 100644 --- a/targets/wasm-module/sum4_repl_cored_early.cpp +++ b/targets/wasm-module/sum4_repl_cored_early.cpp @@ -69,12 +69,15 @@ extern "C" EXPORT("wasm_module") int wasm_module(void) { YC = 0; ZC = 0; + // "early" starts trace here fail_start_trace(); sum<0, SIG_X>(); sum<1, SIG_Y>(); sum<2, SIG_Z>(); + // "late" starts trace here + sign_t static_sig = cored_vote(); fail_stop_trace();