|
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. |
3 | 3 | use std::process::Command; |
4 | 4 |
|
5 | 5 | #[test] |
6 | 6 | fn raw_exit_scan_requires_an_unsafe_call() { |
7 | 7 | let source = include_str!("../src/process.rs"); |
8 | 8 | let signature = source.find("fn process_exit_signals").unwrap(); |
9 | 9 | 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() |
12 | 17 | .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"); |
15 | 34 | let directory = std::env::temp_dir().join(format!("nera-exit-contract-{}", std::process::id())); |
16 | 35 | std::fs::create_dir_all(&directory).unwrap(); |
17 | 36 | 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 | | -"#; |
49 | 37 | 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(); |
51 | 43 | Command::new("rustc") |
52 | 44 | .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())) |
53 | 49 | .arg(&file) |
54 | 50 | .arg("-o") |
55 | 51 | .arg(directory.join("contract.rmeta")) |
|
0 commit comments