File: test-aslr.rs

package info (click to toggle)
rustc 1.70.0%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 722,120 kB
  • sloc: xml: 147,962; javascript: 10,210; sh: 8,590; python: 8,220; ansic: 5,901; cpp: 4,635; makefile: 4,001; asm: 2,856
file content (43 lines) | stat: -rw-r--r-- 1,290 bytes parent folder | download | duplicates (10)
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
41
42
43
const NUM_RUNS: usize = 10;

fn run_self(exe: &str) -> usize {
    use std::process::Command;
    let mut set = std::collections::HashSet::new();

    let mut cmd = Command::new(exe);
    cmd.arg("--report");
    (0..NUM_RUNS).for_each(|_| {
        set.insert(cmd.output().expect("failed to execute process").stdout);
    });
    set.len()
}

fn main() {
    let mut args = std::env::args();
    let arg0 = args.next().unwrap();
    match args.next() {
        Some(s) if s.eq("--report") => {
            println!("main = {:#?}", &main as *const _);
        }
        Some(s) if s.eq("--test-no-aslr") => {
            let cnt = run_self(&arg0);
            if cnt != 1 {
                eprintln!("FAIL: {} most likely ASLR", arg0);
                std::process::exit(1);
            }
            println!("PASS: {} does no ASLR", arg0);
        }
        Some(s) if s.eq("--test-aslr") => {
            let cnt = run_self(&arg0);
            if cnt == 1 {
                eprintln!("FAIL: {} most likely no ASLR", arg0);
                std::process::exit(1);
            }
            println!("PASS: {} does ASLR", arg0);
        }
        Some(_) | None => {
            println!("Usage: {} --test-no-aslr | --test-aslr", arg0);
            std::process::exit(1);
        }
    }
}