Skip to content

Commit 8b2d537

Browse files
committed
test(runtime): check the exit boundary with real runtime types
1 parent 93be70e commit 8b2d537

1 file changed

Lines changed: 34 additions & 38 deletions

File tree

‎crates/nera-runtime/tests/exit_signal_contract.rs‎

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,51 @@
1-
//! Check the real private EXIT adapter requires an unsafe call inside the crate,
2-
//! independently of its visibility to external library users.
1+
//! Check the real private EXIT adapter's unsafe call boundary independently of
2+
//! its external visibility. Runtime tests own traversal and signal semantics.
33
use std::process::Command;
44

55
#[test]
66
fn raw_exit_scan_requires_an_unsafe_call() {
77
let source = include_str!("../src/process.rs");
88
let signature = source.find("fn process_exit_signals").unwrap();
99
let start = source[..signature].rfind('\n').unwrap() + 1;
10-
let end = source[start..]
11-
.find("\n// ---------------------------------------------------------------------------")
10+
let end = source[signature..].find('{').unwrap() + signature;
11+
let declaration = &source[start..end];
12+
// Only the declaration controls whether a call requires unsafe. Compile it
13+
// with the real runtime types, without duplicating private mailbox state or
14+
// substituting fake term/heap operations for the implementation.
15+
let adapter = format!("{declaration}{{ unreachable!() }}");
16+
let dependencies = std::env::current_exe()
1217
.unwrap()
13-
+ start;
14-
let adapter = &source[start..end];
18+
.parent()
19+
.unwrap()
20+
.to_path_buf();
21+
let library = std::fs::read_dir(&dependencies)
22+
.unwrap()
23+
.map(|entry| entry.unwrap().path())
24+
.find(|path| {
25+
path.extension()
26+
.is_some_and(|extension| extension == "rlib")
27+
&& path
28+
.file_name()
29+
.unwrap()
30+
.to_string_lossy()
31+
.starts_with("libnera_runtime-")
32+
})
33+
.expect("the runtime library built for this integration test");
1534
let directory = std::env::temp_dir().join(format!("nera-exit-contract-{}", std::process::id()));
1635
std::fs::create_dir_all(&directory).unwrap();
1736
let file = directory.join("contract.rs");
18-
let prelude = r#"
19-
mod atom_table {
20-
pub const ATOM_EXIT_SIGNAL: u32 = 1;
21-
pub const ATOM_KILL: u32 = 2;
22-
pub const ATOM_KILLED: u32 = 3;
23-
pub const ATOM_NORMAL: u32 = 4;
24-
}
25-
mod term {
26-
pub const BOXED_TUPLE: u64 = 1;
27-
pub const TOMBSTONE_VALUE: u64 = 7;
28-
pub struct BoxedHeader(pub u64);
29-
impl BoxedHeader {
30-
pub fn subtype(&self) -> u64 { 0 }
31-
pub fn arity(&self) -> usize { 0 }
32-
}
33-
}
34-
pub struct ErlTerm(pub u64);
35-
impl ErlTerm {
36-
fn is_boxed(&self) -> bool { false }
37-
fn as_boxed_ptr(&self) -> Option<*const u64> { None }
38-
fn as_atom_id(&self) -> Option<u32> { None }
39-
fn atom(id: u32) -> Self { Self(id as u64) }
40-
}
41-
struct Mailbox { head: u32, capacity: u32, buf: *mut u64 }
42-
impl Mailbox {
43-
fn slot_count(&self) -> u32 { 0 }
44-
unsafe fn remove_scanned_slot(&mut self, _: usize) {}
45-
}
46-
struct Otp { trap_exit: bool }
47-
pub struct Process { mailbox: Mailbox, otp: Otp }
48-
"#;
4937
let compile = |body: &str| {
50-
std::fs::write(&file, format!("{prelude}\n{adapter}\n{body}")).unwrap();
38+
std::fs::write(
39+
&file,
40+
format!("use nera_runtime::{{process::Process, term::ErlTerm}};\n{adapter}\n{body}"),
41+
)
42+
.unwrap();
5143
Command::new("rustc")
5244
.args(["--edition=2021", "--crate-type=lib", "--emit=metadata"])
45+
.arg("--extern")
46+
.arg(format!("nera_runtime={}", library.display()))
47+
.arg("-L")
48+
.arg(format!("dependency={}", dependencies.display()))
5349
.arg(&file)
5450
.arg("-o")
5551
.arg(directory.join("contract.rmeta"))

0 commit comments

Comments
 (0)