Files
leleka/scripts/dev/elf-sections-equal.sh
T
Pavlo Ratushnyi f78b89757c STRA-338 S12a: inline unit-test modules move to sibling tests.rs files in 118 oversize files
scripts/dev/extract_tests.py moves a trailing cfg(test) mod tests block into foo/tests.rs for 118 files of leleka-runtime, cuda-driver, control-plane, gateway-text, deploy and nine smaller crates (31,000 lines), leaving production code and test ids untouched; 22 files that are themselves loaded through a path attribute carry an explicit path to their tests file. Not touched: the files the kar-off-ab branch edits (backend.rs, backend_original_serve.rs, backend_original_serve_replay.rs, qwen4exp_resident_expert_source.rs and seven deploy files).

Verified: cargo check --all-targets for the whole workspace on x86_64 Linux and on the Windows host; the 2429 library unit-test ids of the workspace are identical before and after (Linux listing diff empty); the loadable sections of leleka-inference, leleka-daemon, leleka-cli and strata-next are byte-identical between a release build before and after (scripts/dev/elf-sections-equal.sh), so the shipped code is the same. The architecture ratchets exclude tests.rs from the path-include count; the baseline drops from 329 to 311 oversize files and from 302,000 to 273,600 lines.
2026-11-05 15:00:09 +01:00

36 lines
1.5 KiB
Bash

#!/usr/bin/env bash
# Prove that two release binaries run the same code: compare every loadable section byte for byte (STRA-338).
#
# scripts/dev/elf-sections-equal.sh A B exit 0 when .text, .rodata, .data* , .eh_frame*, .gcc_except_table, .init/.fini*,
# .tdata, .got and .plt of A and B are identical; exit 1 and a diff otherwise
#
# Two builds of the same program can differ in the symbol table only: LLVM suffixes private symbols with `.llvm.<hash>` and
# the hash follows the module layout, so `cmp` and `sha256sum` of the whole file are the wrong test after a pure source move.
# Build both with the same RUSTFLAGS `--remap-path-prefix=<worktree>=/src` so that panic locations carry no machine path.
# Needs GNU objcopy (binutils); run it in Linux or WSL.
set -euo pipefail
SECTIONS=(.text .rodata .data .data.rel.ro .eh_frame .eh_frame_hdr .gcc_except_table .init_array .fini_array .tdata .got .plt .plt.got .init .fini)
sums() { # $1 = ELF file: one "section sha256 size" line per non-empty section
local file=$1 dir section
dir=$(mktemp -d)
for section in "${SECTIONS[@]}"; do
if objcopy -O binary --only-section="$section" "$file" "$dir/s" 2>/dev/null && [[ -s "$dir/s" ]]; then
echo "$section $(sha256sum <"$dir/s" | cut -c1-64) $(stat -c %s "$dir/s")"
fi
done
rm -rf "$dir"
}
main() {
[[ $# -eq 2 ]] || { sed -n '2,11p' "$0"; exit 2; }
if diff <(sums "$1") <(sums "$2"); then
echo "sections identical: $1 == $2"
else
echo "sections DIFFER" >&2
exit 1
fi
}
main "$@"