summaryrefslogtreecommitdiffstats
path: root/toolkit/crashreporter/process_reader/src/lib.rs
blob: 09936e46f1c3ccdec425fac2005bd027b755b2db (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::{ffi::CString, mem::size_of};

use error::ReadError;

pub mod error;
mod platform;

#[cfg(target_os = "windows")]
pub type ProcessHandle = windows_sys::Win32::Foundation::HANDLE;

#[cfg(any(target_os = "linux", target_os = "android"))]
pub type ProcessHandle = libc::pid_t;

#[cfg(any(target_os = "macos"))]
pub type ProcessHandle = mach2::mach_types::task_t;

pub struct ProcessReader {
    process: ProcessHandle,
}

impl ProcessReader {
    pub fn copy_null_terminated_string(&self, address: usize) -> Result<CString, ReadError> {
        // Try copying the string word-by-word first, this is considerably
        // faster than one byte at a time.
        if let Ok(string) = self.copy_null_terminated_string_word_by_word(address) {
            return Ok(string);
        }

        // Reading the string one word at a time failed, let's try again one
        // byte at a time. It's slow but it might work in situations where the
        // string alignment causes word-by-word access to straddle page
        // boundaries.
        let mut length = 0;
        let mut string = Vec::<u8>::new();

        loop {
            let char = self.copy_object::<u8>(address + length)?;
            length += 1;
            string.push(char);

            if char == 0 {
                break;
            }
        }

        // SAFETY: If we reach this point we've read at least one byte and we
        // know that the last one we read is nul.
        Ok(unsafe { CString::from_vec_with_nul_unchecked(string) })
    }

    fn copy_null_terminated_string_word_by_word(
        &self,
        address: usize,
    ) -> Result<CString, ReadError> {
        const WORD_SIZE: usize = size_of::<usize>();
        let mut length = 0;
        let mut string = Vec::<u8>::new();

        loop {
            let array = self.copy_array::<u8>(address + length, WORD_SIZE)?;
            let null_terminator = array.iter().position(|&e| e == 0);
            length += null_terminator.unwrap_or(WORD_SIZE);
            string.extend(array.into_iter());

            if null_terminator.is_some() {
                string.truncate(length + 1);
                break;
            }
        }

        // SAFETY: If we reach this point we've read at least one byte and we
        // know that the last one we read is nul.
        Ok(unsafe { CString::from_vec_with_nul_unchecked(string) })
    }
}