use std::path::PathBuf;
use std::process::Command;
use object::{Object, ObjectSection};
fn synth() -> &'static str {
env!("CARGO_BIN_EXE_synth")
}
fn fixture() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../..")
.join("scripts/repro/mem707_multi_sp.wat")
}
fn compile(extra: &[&str], out: &str) -> std::process::Output {
let fx = fixture();
let mut args = vec![
"compile",
fx.to_str().unwrap(),
"--target",
"cortex-m3",
"--native-pointer-abi",
"--all-exports",
"--relocatable",
"-o",
out,
];
args.extend_from_slice(extra);
Command::new(synth())
.args(&args)
.output()
.expect("run synth")
}
fn bss_size(path: &str) -> u64 {
let data = std::fs::read(path).expect("read .o");
let obj = object::File::parse(&*data).expect("parse ELF");
obj.sections()
.find(|s| s.name() == Ok(".bss"))
.expect(".bss present")
.size()
}
fn global_slots(path: &str) -> Vec<i32> {
let bytes = std::fs::read(path).expect("read .o");
let obj = object::File::parse(&*bytes).expect("parse ELF");
let data = obj
.section_by_name(".data")
.expect(".data present")
.data()
.expect(".data bytes");
data.chunks_exact(4)
.map(|w| i32::from_le_bytes(w.try_into().unwrap()))
.collect()
}
#[test]
fn all_aliased_sp_globals_rebase_707() {
let out = compile(&["--shadow-stack-size", "512"], "/tmp/mem707_test.o");
assert!(
out.status.success(),
"#707: the 3-SP fused node must now COMPILE (was refused): {}",
String::from_utf8_lossy(&out.stderr)
);
assert_eq!(
global_slots("/tmp/mem707_test.o"),
vec![512, 512, 512, 4096],
"the three mutable SP globals co-rebase; the immutable constant is untouched"
);
assert_eq!(bss_size("/tmp/mem707_test.o"), 512);
}
#[test]
fn no_flag_leaves_multi_sp_full_707() {
let out = compile(&[], "/tmp/mem707_noflag_test.o");
assert!(out.status.success());
assert_eq!(
global_slots("/tmp/mem707_noflag_test.o"),
vec![4096, 4096, 4096, 4096],
"no flag must leave every slot at its declared value"
);
assert_eq!(bss_size("/tmp/mem707_noflag_test.o"), 4096);
}