run.sh raw

   1  #!/bin/bash
   2  # Milestone-4 verification: memfd_secret(2) upgrade path.
   3  #
   4  # Builds tests/secalloc-memfd/main.go and runs the binary. The program
   5  # allocates a secure buffer and parses /proc/self/maps to determine whether
   6  # the data pages are backed by memfd_secret (gold standard) or by an
   7  # anonymous mapping (portable fallback).
   8  #
   9  # Expected outcomes:
  10  #   - Linux ≥5.14 with CONFIG_SECRETMEM=y: SECRETMEM_OK on stdout. This is
  11  #     the runner's host environment and the test ASSERTS this outcome.
  12  #   - Linux without CONFIG_SECRETMEM or older kernel: ANONYMOUS_FALLBACK.
  13  #     This is acceptable; the runner treats it as pass IF the kernel
  14  #     actually lacks support (detected via /proc/kallsyms probe) or if the
  15  #     MOXIE_ALLOW_ANONYMOUS_FALLBACK env var is set.
  16  #   - Darwin: ANONYMOUS_FALLBACK. Pass.
  17  set -euo pipefail
  18  
  19  MOXIEROOT="${MOXIEROOT:-$(cd "$(dirname "$0")/../.." && pwd)}"
  20  MOXIE="${MOXIE:-$MOXIEROOT/moxie}"
  21  TMPDIR=$(mktemp -d)
  22  trap "rm -rf $TMPDIR" EXIT
  23  
  24  export PATH="/usr/lib/llvm19/bin:$PATH"
  25  export MOXIEROOT
  26  
  27  BIN="$TMPDIR/secalloc_memfd_test"
  28  
  29  cd "$MOXIEROOT"
  30  
  31  if ! "$MOXIE" build -o "$BIN" ./tests/secalloc-memfd 2>"$TMPDIR/build.err"; then
  32      echo "FAIL secalloc-memfd: compilation failed"
  33      cat "$TMPDIR/build.err"
  34      exit 1
  35  fi
  36  
  37  set +e
  38  "$BIN" >"$TMPDIR/stdout" 2>"$TMPDIR/stderr"
  39  rc=$?
  40  set -e
  41  
  42  stdout=$(cat "$TMPDIR/stdout")
  43  stderr=$(cat "$TMPDIR/stderr")
  44  
  45  if [ "$rc" -ne 0 ]; then
  46      echo "FAIL secalloc-memfd: expected clean exit, got rc=$rc"
  47      echo "--- stdout ---"
  48      echo "$stdout"
  49      echo "--- stderr ---"
  50      echo "$stderr"
  51      exit 1
  52  fi
  53  
  54  # Detect whether the kernel actually supports memfd_secret. Use the same
  55  # probe we use at runtime: look for __x64_sys_memfd_secret (x86_64) or
  56  # __arm64_sys_memfd_secret (arm64) in /proc/kallsyms. Requires CAP_SYSLOG
  57  # or kernel.kptr_restrict=0 to see the symbols; fall back to "assume
  58  # unsupported" if the symbol list is empty.
  59  kernel_supports_memfd_secret=0
  60  if [ "$(uname -s)" = "Linux" ]; then
  61      if grep -qE 'T __(x64|arm64)_sys_memfd_secret' /proc/kallsyms 2>/dev/null; then
  62          kernel_supports_memfd_secret=1
  63      fi
  64  fi
  65  
  66  if echo "$stdout" | grep -q "SECRETMEM_OK"; then
  67      echo "OK   secalloc-memfd (memfd_secret upgrade path active)"
  68      exit 0
  69  fi
  70  
  71  if echo "$stdout" | grep -q "ANONYMOUS_FALLBACK"; then
  72      if [ "$kernel_supports_memfd_secret" -eq 0 ] || [ "${MOXIE_ALLOW_ANONYMOUS_FALLBACK:-}" = "1" ]; then
  73          echo "OK   secalloc-memfd (anonymous fallback; kernel lacks memfd_secret)"
  74          exit 0
  75      fi
  76      echo "FAIL secalloc-memfd: kernel supports memfd_secret but arena fell back to anonymous"
  77      echo "--- stdout ---"
  78      echo "$stdout"
  79      echo "--- stderr ---"
  80      echo "$stderr"
  81      exit 1
  82  fi
  83  
  84  echo "FAIL secalloc-memfd: neither SECRETMEM_OK nor ANONYMOUS_FALLBACK found"
  85  echo "--- stdout ---"
  86  echo "$stdout"
  87  echo "--- stderr ---"
  88  echo "$stderr"
  89  exit 1
  90