summaryrefslogtreecommitdiffstats
path: root/third_party/rust/minidump-writer/tests/ptrace_dumper.rs
blob: 1be27f08094147370b9f1f55c31ec10763dd57a0 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//! All of these tests are specific to ptrace
#![cfg(any(target_os = "linux", target_os = "android"))]

use minidump_writer::ptrace_dumper::PtraceDumper;
use nix::sys::mman::{mmap, MapFlags, ProtFlags};
use nix::sys::signal::Signal;
use std::convert::TryInto;
use std::io::{BufRead, BufReader};
use std::mem::size_of;
use std::os::unix::io::AsFd;
use std::os::unix::process::ExitStatusExt;

mod common;
use common::*;

#[test]
fn test_setup() {
    spawn_child("setup", &[]);
}

#[test]
fn test_thread_list_from_child() {
    // Child spawns and looks in the parent (== this process) for its own thread-ID
    spawn_child("thread_list", &[]);
}

#[test]
fn test_thread_list_from_parent() {
    let num_of_threads = 5;
    let mut child = start_child_and_wait_for_threads(num_of_threads);
    let pid = child.id() as i32;
    let mut dumper = PtraceDumper::new(pid).expect("Couldn't init dumper");
    assert_eq!(dumper.threads.len(), num_of_threads);
    dumper.suspend_threads().expect("Could not suspend threads");

    // let mut matching_threads = 0;
    for (idx, curr_thread) in dumper.threads.iter().enumerate() {
        println!("curr_thread: {:?}", curr_thread);
        let info = dumper
            .get_thread_info_by_index(idx)
            .expect("Could not get thread info by index");
        let (_valid_stack_ptr, stack_len) = dumper
            .get_stack_info(info.stack_pointer)
            .expect("Could not get stack_pointer");

        assert!(stack_len > 0);

        // TODO: I currently know of no way to write the thread_id into the registers using Rust,
        //       so this check is deactivated for now, because it always fails
        /*
        // In the helper program, we stored a pointer to the thread id in a
        // specific register. Check that we can recover its value.
        #[cfg(target_arch = "x86_64")]
        let process_tid_location = info.regs.rcx;
        #[cfg(target_arch = "x86")]
        let process_tid_location = info.regs.ecx;
        #[cfg(target_arch = "arm")]
        let process_tid_location = info.regs.uregs[3];
        #[cfg(target_arch = "aarch64")]
        let process_tid_location = info.regs.regs[3];
        #[cfg(target_arch = "mips")]
        let process_tid_location = info.mcontext.gregs[1];

        let thread_id_data = PtraceDumper::copy_from_process(
            *curr_thread,
            process_tid_location as *mut libc::c_void,
            4,
        )
        .expect("Could not copy from process");
        let found_thread_id = i32::from_ne_bytes(
            thread_id_data
                .as_slice()
                .try_into()
                .expect("couldn't parse i32 from read data"),
        );
        matching_threads += if *curr_thread == found_thread_id {
            1
        } else {
            0
        }; */
    }
    dumper.resume_threads().expect("Failed to resume threads");
    child.kill().expect("Failed to kill process");

    // Reap child
    let waitres = child.wait().expect("Failed to wait for child");
    let status = waitres.signal().expect("Child did not die due to signal");
    assert_eq!(waitres.code(), None);
    assert_eq!(status, Signal::SIGKILL as i32);

    // We clean up the child process before checking the final result
    // TODO: I currently know of no way to write the thread_id into the registers using Rust,
    //       so this check is deactivated for now, because it always fails
    // assert_eq!(matching_threads, num_of_threads);
}

// #[cfg(not(any(target_arch = "mips", target_arch = "arm-eabi"))]
#[cfg(not(target_arch = "mips"))]
#[test]
// Ensure that the linux-gate VDSO is included in the mapping list.
fn test_mappings_include_linux_gate() {
    spawn_child("mappings_include_linux_gate", &[]);
}

#[test]
fn test_linux_gate_mapping_id() {
    if std::env::var("CI").is_ok() {
        println!("disabled on CI, but works locally");
        return;
    }

    spawn_child("linux_gate_mapping_id", &[]);
}

#[test]
fn test_merged_mappings() {
    let page_size = nix::unistd::sysconf(nix::unistd::SysconfVar::PAGE_SIZE).unwrap();
    let page_size = std::num::NonZeroUsize::new(page_size.unwrap() as usize).unwrap();
    let map_size = std::num::NonZeroUsize::new(3 * page_size.get()).unwrap();

    let path: &'static str = std::env!("CARGO_BIN_EXE_test");
    let file = std::fs::File::open(path).unwrap();

    // mmap two segments out of the helper binary, one
    // enclosed in the other, but with different protections.
    let mapped_mem = unsafe {
        mmap(
            None,
            map_size,
            ProtFlags::PROT_READ,
            MapFlags::MAP_SHARED,
            Some(file.as_fd()),
            0,
        )
        .unwrap()
    };

    // Carve a page out of the first mapping with different permissions.
    let _inside_mapping = unsafe {
        mmap(
            std::num::NonZeroUsize::new(mapped_mem as usize + 2 * page_size.get()),
            page_size,
            ProtFlags::PROT_NONE,
            MapFlags::MAP_SHARED | MapFlags::MAP_FIXED,
            Some(file.as_fd()),
            // Map a different offset just to
            // better test real-world conditions.
            page_size.get().try_into().unwrap(), // try_into() in order to work for 32 and 64 bit
        )
    };

    spawn_child(
        "merged_mappings",
        &[
            path,
            &format!("{}", mapped_mem as usize),
            &format!("{map_size}"),
        ],
    );
}

#[test]
// Ensure that the linux-gate VDSO is included in the mapping list.
fn test_file_id() {
    spawn_child("file_id", &[]);
}

#[test]
fn test_find_mapping() {
    spawn_child(
        "find_mappings",
        &[
            &format!("{}", libc::printf as *const () as usize),
            &format!("{}", String::new as *const () as usize),
        ],
    );
}

#[test]
fn test_copy_from_process_self() {
    if std::env::var("CI").is_ok() {
        println!("disabled on CI, but works locally");
        return;
    }

    let stack_var: libc::c_long = 0x11223344;
    let heap_var: Box<libc::c_long> = Box::new(0x55667788);
    spawn_child(
        "copy_from_process",
        &[
            &format!("{}", &stack_var as *const libc::c_long as usize),
            &format!("{}", heap_var.as_ref() as *const libc::c_long as usize),
        ],
    );
}

#[test]
fn test_sanitize_stack_copy() {
    let num_of_threads = 1;
    let mut child = start_child_and_return(&["spawn_alloc_wait"]);
    let pid = child.id() as i32;

    let mut f = BufReader::new(child.stdout.as_mut().expect("Can't open stdout"));
    let mut buf = String::new();
    let _ = f
        .read_line(&mut buf)
        .expect("Couldn't read address provided by child");
    let mut output = buf.split_whitespace();
    let heap_addr = usize::from_str_radix(output.next().unwrap().trim_start_matches("0x"), 16)
        .expect("unable to parse mmap_addr");

    let mut dumper = PtraceDumper::new(pid).expect("Couldn't init dumper");
    assert_eq!(dumper.threads.len(), num_of_threads);
    dumper.suspend_threads().expect("Could not suspend threads");
    let thread_info = dumper
        .get_thread_info_by_index(0)
        .expect("Couldn't find thread_info");

    let defaced;
    #[cfg(target_pointer_width = "64")]
    {
        defaced = 0x0defaced0defacedusize.to_ne_bytes()
    }
    #[cfg(target_pointer_width = "32")]
    {
        defaced = 0x0defacedusize.to_ne_bytes()
    };

    let mut simulated_stack = vec![0xffu8; 2 * size_of::<usize>()];
    // Pointers into the stack shouldn't be sanitized.
    simulated_stack[size_of::<usize>()..].copy_from_slice(&thread_info.stack_pointer.to_ne_bytes());

    dumper
        .sanitize_stack_copy(
            &mut simulated_stack,
            thread_info.stack_pointer,
            size_of::<usize>(),
        )
        .expect("Could not sanitize stack");

    assert!(simulated_stack[size_of::<usize>()..] != defaced);
    // Memory prior to the stack pointer should be cleared.
    assert_eq!(
        &simulated_stack[0..size_of::<usize>()],
        vec![0u8; size_of::<usize>()].as_slice()
    );

    // Small integers should not be sanitized.
    for ii in -4096..=4096isize {
        simulated_stack = vec![0u8; 2 * size_of::<usize>()];
        simulated_stack[0..size_of::<usize>()].copy_from_slice(&(ii as usize).to_ne_bytes());
        dumper
            .sanitize_stack_copy(&mut simulated_stack, thread_info.stack_pointer, 0)
            .expect("Failed to sanitize with small integers");
        assert!(simulated_stack[size_of::<usize>()..] != defaced);
    }

    // The instruction pointer definitely should point into an executable mapping.
    let instr_ptr = thread_info.get_instruction_pointer();
    let mapping_info = dumper
        .find_mapping_no_bias(instr_ptr)
        .expect("Failed to find mapping info");
    assert!(mapping_info.is_executable());

    // Pointers to code shouldn't be sanitized.
    simulated_stack = vec![0u8; 2 * size_of::<usize>()];
    simulated_stack[size_of::<usize>()..].copy_from_slice(&instr_ptr.to_ne_bytes());
    dumper
        .sanitize_stack_copy(&mut simulated_stack, thread_info.stack_pointer, 0)
        .expect("Failed to sanitize with instr_ptr");
    assert!(simulated_stack[0..size_of::<usize>()] != defaced);
    assert!(simulated_stack[size_of::<usize>()..] != defaced);

    // String fragments should be sanitized.
    let junk = "abcdefghijklmnop".as_bytes();
    simulated_stack.copy_from_slice(&junk[0..2 * size_of::<usize>()]);
    dumper
        .sanitize_stack_copy(&mut simulated_stack, thread_info.stack_pointer, 0)
        .expect("Failed to sanitize with junk");
    assert_eq!(simulated_stack[0..size_of::<usize>()], defaced);
    assert_eq!(simulated_stack[size_of::<usize>()..], defaced);

    // Heap pointers should be sanititzed.

    // NOTE: The original test used the heap-address below, but here thread_info.regs.rcx
    //       is the instruction pointer, and thus in direct conflict with the "instruction pointer"
    //       testcase.
    //       Instead we just allocate something on the heap in the child and pass that address to this test.
    // #[cfg(target_arch = "x86_64")]
    // let heap_addr = thread_info.regs.rcx as usize;
    // #[cfg(target_arch = "x86")]
    // let heap_addr = thread_info.regs.ecx as usize;
    // #[cfg(target_arch = "arm")]
    // let heap_addr = thread_info.regs.uregs[3] as usize;
    // #[cfg(target_arch = "aarch64")]
    // let heap_addr = thread_info.regs.regs[3] as usize;
    // #[cfg(target_arch = "mips")]
    // let heap_addr = thread_info.mcontext.gregs[1] as usize;

    simulated_stack = vec![0u8; 2 * size_of::<usize>()];

    simulated_stack[0..size_of::<usize>()].copy_from_slice(&heap_addr.to_ne_bytes());
    dumper
        .sanitize_stack_copy(&mut simulated_stack, thread_info.stack_pointer, 0)
        .expect("Failed to sanitize with heap addr");

    assert_eq!(simulated_stack[0..size_of::<usize>()], defaced);

    dumper.resume_threads().expect("Failed to resume threads");
    child.kill().expect("Failed to kill process");

    // Reap child
    let waitres = child.wait().expect("Failed to wait for child");
    let status = waitres.signal().expect("Child did not die due to signal");
    assert_eq!(waitres.code(), None);
    assert_eq!(status, Signal::SIGKILL as i32);
}