Compare commits

..

9 Commits

19 changed files with 816 additions and 1032 deletions

161
flake.nix
View File

@ -6,6 +6,8 @@ rec {
flake-utils.url = "github:numtide/flake-utils";
};
# TODO: Rewrite all perl scripts -> modularize common code and unify menu actions
outputs =
{
self,
@ -159,6 +161,7 @@ rec {
'';
};
# Provides clang to compile C++ to Wasm
wasi-sdk = stdenv.mkDerivation rec {
pname = "wasi-sdk";
version = "29";
@ -191,6 +194,7 @@ rec {
'';
};
# Provides the iwasm interpreter
iwasm = stdenv.mkDerivation rec {
pname = "iwasm";
version = "2.4.4";
@ -225,6 +229,7 @@ rec {
'';
};
# Provides the wamrc compiler
wamrc = stdenv.mkDerivation rec {
pname = "wamrc";
version = "2.4.4";
@ -259,147 +264,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 +275,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 +389,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/nixos.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";

View File

@ -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 ) = @_;

View File

@ -10,23 +10,25 @@ use lib $FindBin::Bin;
use Util;
use TUI;
use POSIX qw(strftime);
use feature 'say';
my $date = Util::date_now;
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/nixos.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: just @_...";
system("$justbin -d $local_root -f $justfile @_")
and die "Build failed";
system( 'perl', $compile_pl, $module, $target, $mode ) == 0
or die "Build failed\n";
sleep(1);
}
# Find and select experiments
@ -36,64 +38,241 @@ 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;
# 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 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] )
? "false"
: "true";
# ========================================================================================= #
# 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_aot",
));
my @xip_variants = ( "Compile AOT with --xip", "Compile AOT without --xip" );
my $selected_xip_variant;
if ( grep { $_ eq "aot" } @selected_modes ) {
($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 .text.wamr_mmap variant
# ========================================================================================= #
my @mmap_variants = (
"Place mmap_space in .text.wamr_mmap",
"Let the linker decide where mmap_space is located"
);
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 .text.wamr_aot variant
# ========================================================================================= #
my @aot_section_variants = (
"Place AOT array in .text.wamr_aot",
"Let the linker decide where the AOT array is located",
);
my $selected_aot_variant;
if ( grep { $_ eq "aot" } @selected_modes ) {
($selected_aot_variant) =
TUI::select_from_list( "Select WAMR Array.Text Variant",
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 .text.wamr_global_heap variant
# ========================================================================================= #
my @global_heap_variants = (
"Place pool allocator's global_heap in .text.wamr_global_heap",
"Let the linker decide where global_heap is located"
);
my $selected_global_heap_variant;
if ( $selected_allocator_variant eq $allocator_variants[0] ) {
($selected_global_heap_variant) =
TUI::select_from_list( "Select WAMR Global Heap Variant",
0, @global_heap_variants );
die "No global heap variant selected" unless $selected_global_heap_variant;
local $ENV{WAMR_USE_GLOBAL_HEAP_IN_TEXT} =
( $selected_global_heap_variant eq $global_heap_variants[0] )
? "true"
: "false";
}
# ========================================================================================= #
# Select .text.wamr_runtime_pool variant
# ========================================================================================= #
my @runtime_pool_variants = (
"Place usage allocator's runtime_pool in .text.wamr_runtime_pool",
"Let the linker decide where runtime_pool is located"
);
my $selected_runtime_pool_variant;
if ( $selected_allocator_variant eq $allocator_variants[1] ) {
($selected_runtime_pool_variant) =
TUI::select_from_list( "Select WAMR Runtime Pool Variant",
0, @runtime_pool_variants );
die "No runtime pool variant selected"
unless $selected_runtime_pool_variant;
local $ENV{WAMR_USE_RUNTIME_POOL_IN_TEXT} =
( $selected_runtime_pool_variant eq $runtime_pool_variants[0] )
? "true"
: "false";
}
# ========================================================================================= #
# Select .text.wamr_linear_pool variant
# ========================================================================================= #
my @linear_pool_variants = (
"Place usage allocator's linear_pool in .text.wamr_linear_pool",
"Let the linker decide where linear_pool is located"
);
my $selected_linear_pool_variant;
if ( $selected_allocator_variant eq $allocator_variants[1] ) {
($selected_linear_pool_variant) =
TUI::select_from_list( "Select WAMR Linear Pool Variant",
0, @linear_pool_variants );
die "No linear pool variant selected" unless $selected_linear_pool_variant;
local $ENV{WAMR_USE_LINEAR_POOL_IN_TEXT} =
( $selected_linear_pool_variant eq $linear_pool_variants[0] )
? "true"
: "false";
}
# ========================================================================================= #
# 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",
);
my @selected_catch_tags =
TUI::select_from_list( "Select FAIL Flags", 1, sort keys %catch_flag_map );
# ========================================================================================= #
# 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 $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";
just( "build", $experiment, $target, $mode );
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
&& $selected_xip_variant eq $xip_variants[0] )
? "xip"
: "";
my $aot_info =
( $mode eq "aot"
&& $selected_aot_variant
&& $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 $global_heap_info =
( ( $mode eq "aot" || $mode eq "interp" )
&& $selected_global_heap_variant
&& $selected_global_heap_variant eq $global_heap_variants[0] )
? "wamr_global_heap"
: "";
my $runtime_pool_info =
( ( $mode eq "aot" || $mode eq "interp" )
&& $selected_runtime_pool_variant
&& $selected_runtime_pool_variant eq
$runtime_pool_variants[0] )
? "wamr_runtime_pool"
: "";
my $linear_pool_info =
( ( $mode eq "aot" || $mode eq "interp" )
&& $selected_linear_pool_variant
&& $selected_linear_pool_variant eq $linear_pool_variants[0] )
? "wamr_linear_pool"
: "";
my $flags_info = join " ", @selected_catch_tags;
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" );
my $info_str = join " ",
grep { length } (
$mode, $aot_info, $mmap_info,
$allocator_info, $global_heap_info, $runtime_pool_info,
$linear_pool_info, $xip_info, $flags_info
);
system(
join " ",
(
"mv",
"$local_root/build-$experiment",
"$local_builds_dir/${date}_$experiment-$target-$mode",
)
);
}
}
# Build experiment
my $date = strftime( "%m-%d_%H-%M-%S", localtime );
compile( $experiment, $target, $mode );
# 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 $fhandle, '>', $runner_flags_path )
or die "Cannot write $runner_flags_path: $!";
print $fhandle "$catch_flag_map{$_}\n" for @selected_catch_tags;
close($fhandle);
system(
join " ",
(
"mv",
"$local_root/build-$experiment",
"$local_builds_dir/${date}_${experiment}_${mode}_${target}",
)
);
}
}
}

416
scripts/compile.pl Normal file
View File

@ -0,0 +1,416 @@
#!/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;
# Defines forwarded to the host compilation so wasm_host.c sees the same
# WASM_MEM_ALLOC_WITH_USAGE value as libiwasm.a was built with.
my @host_variant_cflags;
push @host_variant_cflags, '-DWASM_MEM_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 <module> <target> <mode>\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 = 'targets/wasm-host/wasm_host.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, @host_variant_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, @host_variant_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, @host_variant_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" );
}

View File

@ -1,134 +0,0 @@
[doc("Trace a golden run using FAIL*")]
[group("4: fail")]
trace module:
{{ BOCHS_RUNNER }} \
-V {{ FAIL_SHARE }}/vgabios.bin \
-b {{ FAIL_SHARE }}/BIOS-bochs-latest \
-1 \
-f {{ FAIL_TRACE }} \
-e {{ BUILD_DIR }}-{{ module }}/system.elf \
-i {{ BUILD_DIR }}-{{ module }}/system.iso \
-- \
-Wf,--start-symbol=fail_start_trace \
-Wf,--save-symbol=fail_start_trace \
-Wf,--end-symbol=fail_stop_trace \
-Wf,--state-file={{ BUILD_DIR }}-{{ module }}/state \
-Wf,--trace-file={{ BUILD_DIR }}-{{ module }}/trace.pb \
-Wf,--elf-file={{ BUILD_DIR }}-{{ module }}/system.elf
@echo "Next step: \"just import {{ module }}\""
# [doc("Dump a FAIL* golden run trace")]
# [group("fail")]
# dump module:
# {{ FAIL_DUMP }} {{ BUILD_DIR }}-{{ module }}/trace.pb
[doc("Import a FAIL* golden run trace")]
[group("4: fail")]
import module:
{{ 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 }} --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 }} --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 }} --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 }} --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 }} --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 }} --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 }} --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 }} --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 }} --database-option-file ./db.conf -v {{ module }} -b %% --overwrite
@echo "Next step: \"just server {{ module }}\""
[doc("Start the FAIL* campaign server")]
[group("4: fail")]
server module:
{{ FAIL_SERVER }} \
--port {{ FAIL_SERVER_PORT }} \
--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("4: fail")]
stop-server:
pkill -f {{ FAIL_SERVER }}
[doc("Start a FAIL* campaign client")]
[group("4: fail")]
client module:
# -Wf,--catch-write-textsegment
# -Wf,--catch-outerspace
{{ BOCHS_RUNNER }} \
-V {{ FAIL_SHARE }}/vgabios.bin \
-b {{ FAIL_SHARE }}/BIOS-bochs-latest \
-f {{ FAIL_INJECT }} \
-e {{ BUILD_DIR }}-{{ module }}/system.elf \
-i {{ BUILD_DIR }}-{{ module }}/system.iso \
-j {{ num_cpus() }} \
-- \
-Wf,--server-port={{ FAIL_SERVER_PORT }} \
-Wf,--state-dir={{ BUILD_DIR }}-{{ module }}/state \
-Wf,--trap \
-Wf,--catch-outerspace \
-Wf,--timeout=500000 \
-Wf,--ok-marker=fail_marker_positive \
-Wf,--fail-marker=fail_marker_negative \
-Wf,--detected-marker=fail_marker_detected \
> /dev/null
@echo "Next step: \"just result {{ module }}\" or \"just resultbrowser\""
[doc("Query FAIL* marker statistics from the database")]
[group("4: fail")]
result module:
@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 = \"{{ module }}\" \
GROUP BY v.id, resulttype \
ORDER BY variant, benchmark, resulttype;" | mariadb --defaults-file=./db.conf -t
[doc("Dump FAIL* markers to CSV")]
[group("4: fail")]
result-csv module:
@echo "SELECT \
CONCAT(\"0x\", HEX(p.injection_instr_absolute)) AS fault_address, \
SUM(t.time2 - t.time1 + 1) AS total_fail_markers \
FROM trace t \
JOIN variant v 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 p.id = r.pilot_id \
WHERE v.variant = \"{{ module }}\" AND r.resulttype = \"FAIL_MARKER\" \
GROUP BY p.injection_instr_absolute \
ORDER BY SUM(t.time2 - t.time1 + 1) DESC;" | mariadb --defaults-file=./db.conf --batch --raw | sed 's/\t/,/g'
[doc("Start the FAIL* resultbrowser")]
[group("4: fail")]
resultbrowser:
{{ RESULT_BROWSER }} -c ./db.conf --host=0.0.0.0 --port={{ RESULTBROWSER_PORT }}

View File

@ -1,35 +0,0 @@
import "fail.just"
BUILD_DIR := "build"
# FAIL* variables
FAIL_SERVER_PORT := "22941"
RESULTBROWSER_PORT := "22941"
FAIL_BIN := "fail/bin"
FAIL_SHARE := "fail/share"
BOCHS_RUNNER := f"{{FAIL_BIN}}/bochs-experiment-runner.py"
FAIL_TRACE := f"{{FAIL_BIN}}/fail-x86-tracing"
FAIL_DUMP := f"{{FAIL_BIN}}/dump-trace"
FAIL_IMPORT := f"{{FAIL_BIN}}/import-trace"
FAIL_PRUNE := f"{{FAIL_BIN}}/prune-trace"
FAIL_SERVER := f"{{FAIL_BIN}}/generic-experiment-server"
FAIL_INJECT := f"{{FAIL_BIN}}/generic-experiment-client"
RESULT_BROWSER := f"{{FAIL_BIN}}/resultbrowser.py"
# =================================================================================================================== #
# Helper recipes
# =================================================================================================================== #
[default]
[private]
list:
@./just --list --unsorted
# Create a database:
# - mysql -u smchurla -p
# - CREATE DATABASE database_name;
# - SHOW DATABASES;
procs:
ps -u smchurla

View File

@ -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',
);
},

View File

@ -1,246 +0,0 @@
import "wasm.just"
import "fail.just"
BUILD_DIR := "build"
# Load environment variables set by "nix develop"-shell
FAIL_SHARE := env("FAIL_SHARE")
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")
# FAIL* variables
FAIL_SERVER_PORT := "1111"
RESULTBROWSER_PORT := "5000"
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"
# =================================================================================================================== #
# 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 }}
# =================================================================================================================== #
# MySQL recipes
# =================================================================================================================== #
[doc("Start MySQL container to receive FAIL* trace/campaign results")]
[group("3: fail db")]
start-db:
docker run -d \
--name fail-db \
-e MYSQL_ROOT_PASSWORD=fail \
-e MYSQL_USER=fail \
-e MYSQL_PASSWORD=fail \
-e MYSQL_DATABASE=fail \
-p 3306:3306 \
mysql
[doc("Connect to MySQL database using DBeaver")]
[group("3: 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("3: fail db")]
stop-db:
docker stop fail-db
[doc("Remove MySQL container")]
[group("3: fail db")]
remove-db:
docker container rm fail-db
# =================================================================================================================== #
# Debugging recipes
# =================================================================================================================== #
[doc("Launch gdb")]
[group("debug")]
gdb module:
gdb --tui {{ BUILD_DIR }}-{{ module }}/system.elf
# [doc("Launch radare2 at address and disassemble")]
# [group("debug")]
# r2 module addr="dbg.os_main":
# # -e asm.section=true
# # -e asm.bytes=true
# radare2 -AA \
# -c "f TARGET @ {{ addr }}; s {{ addr }}; pd-- 30" \
# -e asm.syntax=intel \
# -e asm.lines=false \
# -e asm.xrefs=true \
# -e asm.flags=true \
# -e asm.comments=true \
# -e asm.functions=true \
# -e asm.var=true \
# -e asm.cmt.right=true \
# -e asm.dwarf=true \
# -e asm.pseudo=false \
# -e asm.describe=false \
# -e bin.relocs.apply=true \
# {{ BUILD_DIR }}-{{ module }}/system.elf
[doc("Disassemble with objdump at address")]
[group("debug")]
dump dir addr="0x100000" saddr="0x100100":
objdump {{ dir }}/system.elf \
--disassemble-all \
--disassembler-options=intel \
--disassembler-color=terminal \
--source \
--demangle \
--section=.text \
--start-address={{ addr }} \
--stop-address={{ saddr }} \
--prefix={{ dir }} \
--prefix-strip=7
[doc("Launch radare2 at address (interactive)")]
[group("debug")]
r2i module addr="dbg.os_main":
# -e asm.section=true
# -e asm.bytes=true
radare2 -AA \
-c "s {{ addr }}" \
-e scr.color=3 \
-e scr.scrollbar=0 \
-e scr.responsive=true \
-e scr.interactive=true \
-e scr.utf8=true \
-e scr.utf8.curvy=true \
-e asm.syntax=intel \
-e asm.lines=false \
-e asm.xrefs=true \
-e asm.flags=true \
-e asm.comments=true \
-e asm.functions=true \
-e asm.var=true \
-e asm.cmt.right=true \
-e asm.dwarf=true \
-e asm.pseudo=false \
-e asm.describe=false \
-e bin.relocs.apply=true \
{{ BUILD_DIR }}-{{ module }}/system.elf
# =================================================================================================================== #
# Just do it
# =================================================================================================================== #
[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 }}
[doc("Run binary")]
[group("5: just do it")]
run module:
@echo "Running {{ module }}:"
@{{ BUILD_DIR }}-{{ module }}/system.elf
[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-run module="__help" target="fail" mode="aot": (build module target mode) (run module)
[doc("Send binaries to mars")]
[group("5: just do it")]
upload module:
scp -r {{ BUILD_DIR }}-{{ module }} mars:~/Documents/failnix/{{ BUILD_DIR }}-{{ module }}
[doc("Send markers to local")]
[group("5: just do it")]
download-markers:
scp mars:~/Documents/failnix/markers.csv ./markers.csv
[doc("Perform all steps for a FAIL* campaign")]
[group("5: just do it")]
inject module:
just start-db
@echo "Waiting for database..."
sleep 20
just trace {{ module }}
just import {{ module }}
just server {{ module }}
just client {{ module }}
just result {{ module }}
[doc("Copy build directory to injections/ with timestamp")]
[group("5: just do it")]
archive module suffix:
cp -rv {{ BUILD_DIR }}-{{ module }} ./injections/`date +%Y-%m-%d_%H-%M`_{{ module }}_"{{ suffix }}"

View File

@ -206,6 +206,15 @@ sub inject {
);
say "Server command: $server_command";
# Read catch flags written by build.pl into the experiment directory
my $runner_flags_file = "$remote_builds_dir/$experiment/runner_flags";
my @catch_flags = ();
if ( -e $runner_flags_file ) {
open( my $rfh, '<', $runner_flags_file ) or die "Cannot open $runner_flags_file: $!";
@catch_flags = map { chomp; "-Wf,$_" } grep { /\S/ } <$rfh>;
close($rfh);
}
my $client_command = join " ", (
"nice $bochs_runner",
"-V $fail_share/vgabios.bin",
@ -220,12 +229,7 @@ sub inject {
"-Wf,--state-dir=$remote_builds_dir/$experiment/state",
"-Wf,--trap",
# Catch invalid instruction pointers
"-Wf,--catch-outerspace",
# 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",
@catch_flags,
"-Wf,--timeout=500000",
"-Wf,--ok-marker=fail_marker_positive",

View File

@ -1,391 +0,0 @@
# =================================================================================================================== #
# 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_var_or_default("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
# Add __attribute__((section...)) to this array, so it is located into the .text.wamr_aot segment...
sed -i '1s/^/__attribute__((section(".text.wamr_aot"), aligned(4096)))\n/' {{ BUILD_DIR }}-{{ module }}/wasm_aot_array.c
[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/wasm.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_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{{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

View File

@ -46,20 +46,31 @@ SECTIONS {
KEEP (*(".text.startup"))
/* .wamr_aot and .wamr_mmap have to be placed before .text,
* otherwise they'll be caught by the wildcard */
/* place before .text, otherwise they'll be caught by the wildcard */
/* _wamr_aot_start = .; */
_wamr_aot_start = .;
*(".text.wamr_aot")
/* _wamr_aot_end = .; */
_wamr_aot_end = .;
/* _wamr_mmap_start = .; */
_wamr_mmap_start = .;
*(".text.wamr_mmap")
/* _wamr_mmap_end = .; */
_wamr_mmap_end = .;
/* _text_start = .; */
_wamr_runtime_pool_start = .;
*(".text.wamr_runtime_pool")
_wamr_runtime_pool_end = .;
_wamr_linear_pool_start = .;
*(".text.wamr_linear_pool")
_wamr_linear_pool_end = .;
_wamr_global_heap_start = .;
*(".text.wamr_global_heap")
_wamr_global_heap_end = .;
_text_start = .;
*(".text*")
/* _text_end = .; */
_text_end = .;
*(".rodata*")
}

View File

@ -3,7 +3,6 @@
#include "bh_platform.h"
#include "__WASM_ARRAY_FILE__"
#ifdef TARGET_LINUX
#include <stdio.h>
#include <string.h>
@ -34,7 +33,99 @@ void host_print(wasm_exec_env_t exec_env, const char *msg) {
#define STACK_SIZE (4 * 1024)
#define HEAP_SIZE STACK_SIZE
#define RUNTIME_POOL_SIZE 4 * STACK_SIZE
#if WASM_MEM_ALLOC_WITH_USAGE
// Another bump allocator
#define WASM_LINEAR_MEMORY_SIZE (64 * 1024) // Need to match --initial-memory?
#define RUNTIME_POOL_SIZE (2 * 1024 * 1024)
#define LINEAR_POOL_SIZE (WASM_LINEAR_MEMORY_SIZE + 512 * 1024)
#define ALIGNMENT 8
typedef struct {
char *buf;
size_t size;
size_t offset;
} BumpPool;
#if WAMR_RUNTIME_POOL_IN_TEXT
__attribute__((section(".text.wamr_runtime_pool"), aligned(4096)))
#endif
static char runtime_pool_buf[RUNTIME_POOL_SIZE];
#if WAMR_LINEAR_POOL_IN_TEXT
__attribute__((section(".text.wamr_linear_pool"), aligned(4096)))
#endif
static char linear_pool_buf[LINEAR_POOL_SIZE];
// mem_alloc_usage_t: 0 = Alloc_For_Runtime, 1 = Alloc_For_LinearMemory
static BumpPool pools[] = {
{ runtime_pool_buf, RUNTIME_POOL_SIZE, 0 },
{ linear_pool_buf, LINEAR_POOL_SIZE, 0 },
};
static size_t align_up(size_t x, size_t a) {
return (x + a - 1) & ~(a - 1);
}
static size_t alloc_size(void *ptr) {
size_t header_size = align_up(sizeof(size_t), ALIGNMENT);
return *(size_t *)((char *)ptr - header_size);
}
static void *bump_alloc(BumpPool *pool, unsigned int size) {
size_t header_size = align_up(sizeof(size_t), ALIGNMENT);
size_t start = align_up(pool->offset, ALIGNMENT);
size_t end = start + header_size + align_up(size, ALIGNMENT);
if (end > pool->size) {
return NULL;
}
*(size_t *)&pool->buf[start] = size;
void *ptr = &pool->buf[start + header_size];
pool->offset = end;
return ptr;
}
void *wamr_malloc(mem_alloc_usage_t usage, unsigned int size) {
PRINT("wamr_malloc: usage=%d size=%u\n", usage, size);
return bump_alloc(&pools[usage], size);
}
void *wamr_realloc(mem_alloc_usage_t usage, bool full_size_mmaped, void *ptr,
unsigned int new_size) {
PRINT("wamr_realloc: usage=%d full_size_mmaped=%d ptr=%p new_size=%u\n",
usage, full_size_mmaped, ptr, new_size);
void *new_addr = bump_alloc(&pools[usage], new_size);
if (!new_addr) {
return NULL;
}
if (ptr) {
size_t old_size = alloc_size(ptr);
memcpy(new_addr, ptr, old_size < new_size ? old_size : new_size);
}
return new_addr;
}
void wamr_free(mem_alloc_usage_t usage, void *ptr) {
// PRINT("wamr_free: usage=%d ptr=%p\n", usage, ptr);
}
#else
#define RUNTIME_POOL_SIZE (4 * STACK_SIZE)
#if WAMR_GLOBAL_HEAP_IN_TEXT
__attribute__((section(".text.wamr_global_heap"), aligned(4096)))
#endif
static char global_heap_buf[RUNTIME_POOL_SIZE];
#endif // WASM_MEM_ALLOC_WITH_USAGE
MAIN {
char error_buf[128];
@ -42,11 +133,18 @@ MAIN {
// Step 1: Initialize WAMR Runtime
static RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(RuntimeInitArgs));
static char global_heap_buf[RUNTIME_POOL_SIZE];
#if WASM_MEM_ALLOC_WITH_USAGE
init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = (void *)wamr_malloc;
init_args.mem_alloc_option.allocator.realloc_func = (void *)wamr_realloc;
init_args.mem_alloc_option.allocator.free_func = (void *)wamr_free;
#else
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);
#endif
init_args.max_thread_num = 1;
if (!wasm_runtime_full_init(&init_args)) {
PRINT_ERROR("wasm_runtime_full_init failed.\n");

2
wamr

Submodule wamr updated: f618cfffaf...9cef1e4289