#!/usr/bin/env bash # SPDX-License-Identifier: LicenseRef-Starship-1.0 # Copyright (c) 2023-2026 Robert A. James # test_capsule_ram_blocks.sh -- the tests of the RAM block count as a build knob # (docs/v4.0.0/plans/2026-11-18-capsule-blocks.md, ruling 3 and step 1). # # What is checked: # - the knob, BLK_RAM_USER_BLOCKS, is the VM's user RAM block count (2048 by default), and # block_subsystem.h derives BLK_RAM_BLOCKS from it (2080 = 2048 + 32 reserved, as before); # - mkcapsule's floor is the knob, not a literal 2048: built with the knob at 4096 it refuses a page # at 3000 and takes one at 4096; built with the default it refuses 2047 and takes 2048; # - kernel/Makefile gives the knob to the kernel's C code, to mkcapsule and to mkartemis. # # No QEMU. Prints PASS:/FAIL: per check and RESULT:; exits 0 only if every check passes. set -uo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" T=$(mktemp -d) trap 'rm -rf "$T"' EXIT ok=1 check() { if eval "$2"; then echo "PASS: $1"; else echo "FAIL: $1"; ok=0; fi; } SRC="$ROOT/tools/mkcapsule.c $ROOT/tools/pkcs8_ed25519.c $ROOT/kernel/src/crypto/ed25519.c \ $ROOT/kernel/src/crypto/fe25519.c $ROOT/kernel/src/crypto/scalar25519.c \ $ROOT/kernel/src/crypto/sha512.c $ROOT/kernel/src/crypto/x509_ed25519.c" INC="-I$ROOT/v3/include -I$ROOT/kernel/include -I$ROOT/tools" build() { # build [knob] cc -std=c99 -O2 $INC ${2:+-DBLK_RAM_USER_BLOCKS=$2} -o "$1" $SRC 2> "$T/cc-$(basename "$1").log" } capsule() { # capsule : a one-page capsule mkdir -p "$1"; printf 'Block %s\n( one page )\n' "$2" > "$1/one.4th" } # the knob, as C sees it cat > "$T/layout.c" <<'EOF' #include "block_subsystem.h" #include int main(void) { printf("%u %u %u\n", (unsigned)BLK_RAM_USER_BLOCKS, (unsigned)BLK_RAM_BLOCKS, (unsigned)BLK_DISK_START); return 0; } EOF cc -std=c99 $INC -o "$T/layout" "$T/layout.c" 2> "$T/cc-layout.log" check "the knob's default is 2048, and BLK_RAM_BLOCKS and BLK_DISK_START are 2080 as before" \ '[ "$("$T/layout" 2>/dev/null)" = "2048 2080 2080" ]' # mkcapsule's floor is the knob build "$T/mk-default" build "$T/mk-4096" 4096 capsule "$T/c2047" 2047; capsule "$T/c2048" 2048; capsule "$T/c3000" 3000; capsule "$T/c4096" 4096 check "default build: a page at 2047 is refused" '! "$T/mk-default" --lint "$T/c2047" > /dev/null 2>&1' check "default build: a page at 2048 is taken" '"$T/mk-default" --lint "$T/c2048" > /dev/null 2>&1' check "knob 4096: a page at 3000 is refused" '! "$T/mk-4096" --lint "$T/c3000" > /dev/null 2>&1' check "knob 4096: a page at 4096 is taken" '"$T/mk-4096" --lint "$T/c4096" > /dev/null 2>&1' # kernel/Makefile hands the knob on (a here-string, not a pipe: grep -q would end a printf with SIGPIPE under pipefail) DB=$(cd "$ROOT" && make -s -pn -f kernel/Makefile ARCH=amd64 BLK_RAM_USER_BLOCKS=4096 2>/dev/null) check "kernel/Makefile: the kernel's C flags carry it" 'grep -q -- "-DBLK_RAM_USER_BLOCKS=4096" <<< "$DB"' check "kernel/Makefile: mkcapsule is built with it" 'grep -q -- "^MKCAPSULE_DEFS *:\?= *-DBLK_RAM_USER_BLOCKS=4096" <<< "$DB"' check "kernel/Makefile: mkartemis is built with it" 'grep -q -- "^MKARTEMIS_DEFS *:\?= *-DBLK_RAM_USER_BLOCKS=4096" <<< "$DB"' if [ $ok = 1 ]; then echo "RESULT: PASS"; else echo "RESULT: FAIL"; fi [ $ok = 1 ]