-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathsegfault.rs
40 lines (35 loc) · 942 Bytes
/
segfault.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//! A 100% memory-safe segmentation fault.
//!
//! We first use the soundness hole (and our transmute implementation) to create a mutable null reference to a `u8`.
//! Then, we dereference it to get a segmentation fault!
/// Segfaults the program.
///
/// See [`crate::transmute()`]
pub fn segfault() -> ! {
let null = crate::null_mut::<u8>();
*null = 42;
// If null doesn't work, try max. Surely that'll stop it.
// Confirmed to be effective on WASM.
let max = crate::not_alloc::<u8>();
*max = 69;
unreachable!("Sorry, your platform is too strong.")
}
#[cfg(test)]
mod tests {
#[test]
fn test_segfault() {
use std::process::Command;
let output = Command::new("cargo")
.arg("run")
.arg("segfault")
.output()
.unwrap();
if output.status.success()
|| std::str::from_utf8(&output.stderr)
.unwrap()
.contains("Sorry, your platform is too strong.")
{
panic!("Segfault failed to segfault");
}
}
}