use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
static REQUIRED: AtomicBool = AtomicBool::new(false);
pub fn require_site_patching_off() {
REQUIRED.store(true, Ordering::Release);
}
pub fn site_patching_off_required() -> bool {
REQUIRED.load(Ordering::Acquire)
}
pub fn violation(required: bool, runtime_patches: bool, runtime_collects: bool) -> Option<String> {
if !required {
return None;
}
if runtime_patches {
return Some(
"--backend=in-guest-trap runs with syscall site patching off, but the in-guest \
runtime installed this process with site patching on: it reads \
REVERIE_LITEINST_SITE_PATCHING from the environment, and code in the guest that \
ran before it (a shared library's constructor, or a function the runtime calls \
while installing) changed the value Hermit set; run this program with \
--backend=liteinst"
.to_owned(),
);
}
(!runtime_collects).then(|| {
"--backend=in-guest-trap needs every process's syscall site statistics, but the \
in-guest runtime installed this process without collecting them: it reads its \
statistics socket from the environment, and code in the guest that ran before it \
removed the variable Hermit set, so this process could not show that it patched \
nothing; run this program with --backend=liteinst"
.to_owned()
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_the_captured_settings_decide_and_only_when_requested() {
assert_eq!(violation(true, false, true), None);
let patches = violation(true, true, true).expect("a patching runtime is refused");
assert!(
patches.contains("installed this process with site patching on"),
"{patches}"
);
assert!(patches.contains("--backend=liteinst"), "{patches}");
assert_eq!(violation(true, true, false), Some(patches));
let silent = violation(true, false, false).expect("an image that reports nothing");
assert!(silent.contains("without collecting them"), "{silent}");
for (patches, collects) in [(true, true), (true, false), (false, false)] {
assert_eq!(violation(false, patches, collects), None);
}
}
}