Debian Codegraph @

third_party/rust/minidump-writer/src/linux/auxv/mod.rs

raw
1 use {
2 self::reader::ProcfsAuxvIter,
3 crate::{serializers::*, Pid},
4 error_graph::WriteErrorList,
5 failspot::failspot,
6 std::{fs::File, io::BufReader},
7 thiserror::Error,
8 };
9
10 mod reader;
11
12 /// The type used in auxv keys and values.
13 #[cfg(target_pointer_width = "32")]
14 pub type AuxvType = u32;
15 /// The type used in auxv keys and values.
16 #[cfg(target_pointer_width = "64")]
17 pub type AuxvType = u64;
18
19 #[cfg(target_os = "android")]
20 mod consts {
21 use super::AuxvType;
22 pub const AT_PHDR: AuxvType = 3;
23 pub const AT_PHNUM: AuxvType = 5;
24 pub const AT_ENTRY: AuxvType = 9;
25 pub const AT_SYSINFO_EHDR: AuxvType = 33;
26 }
27 #[cfg(not(target_os = "android"))]
28 mod consts {
29 use super::AuxvType;
30 pub const AT_PHDR: AuxvType = libc::AT_PHDR;
31 pub const AT_PHNUM: AuxvType = libc::AT_PHNUM;
32 pub const AT_ENTRY: AuxvType = libc::AT_ENTRY;
33 pub const AT_SYSINFO_EHDR: AuxvType = libc::AT_SYSINFO_EHDR;
34 }
35
36 /// An auxv key-value pair.
37 #[derive(Debug, PartialEq, Eq)]
38 pub struct AuxvPair {
39 pub key: AuxvType,
40 pub value: AuxvType,
41 }
42
43 /// Auxv info that can be passed from crashing process
44 ///
45 /// Since `/proc/{pid}/auxv` can sometimes be inaccessible, the calling process should prefer to transfer this
46 /// information directly using the Linux `getauxval()` call (if possible).
47 ///
48 /// Any field that is set to `0` will be considered unset. In that case, minidump-writer might try other techniques
49 /// to obtain it (like reading `/proc/{pid}/auxv`).
50 #[repr(C)]
51 #[derive(Clone, Debug, Default)]
52 pub struct DirectAuxvDumpInfo {
53 /// The value of `getauxval(AT_PHNUM)`
54 pub program_header_count: AuxvType,
55 /// The value of `getauxval(AT_PHDR)`
56 pub program_header_address: AuxvType,
57 /// The value of `getauxval(AT_SYSINFO_EHDR)`
58 pub linux_gate_address: AuxvType,
59 /// The value of `getauxval(AT_ENTRY)`
60 pub entry_address: AuxvType,
61 }
62
63 impl From<DirectAuxvDumpInfo> for AuxvDumpInfo {
64 fn from(f: DirectAuxvDumpInfo) -> AuxvDumpInfo {
65 AuxvDumpInfo {
66 program_header_count: (f.program_header_count > 0).then_some(f.program_header_count),
67 program_header_address: (f.program_header_address > 0)
68 .then_some(f.program_header_address),
69 linux_gate_address: (f.linux_gate_address > 0).then_some(f.linux_gate_address),
70 entry_address: (f.entry_address > 0).then_some(f.entry_address),
71 }
72 }
73 }
74
75 #[derive(Debug, Default)]
76 pub struct AuxvDumpInfo {
77 program_header_count: Option<AuxvType>,
78 program_header_address: Option<AuxvType>,
79 linux_gate_address: Option<AuxvType>,
80 entry_address: Option<AuxvType>,
81 }
82
83 impl AuxvDumpInfo {
84 pub fn try_filling_missing_info(
85 &mut self,
86 pid: Pid,
87 mut soft_errors: impl WriteErrorList<AuxvError>,
88 ) -> Result<(), AuxvError> {
89 if self.is_complete() {
90 return Ok(());
91 }
92
93 let auxv_path = format!("/proc/{pid}/auxv");
94 let auxv_file = File::open(&auxv_path).map_err(|e| AuxvError::OpenError(auxv_path, e))?;
95
96 for pair_result in ProcfsAuxvIter::new(BufReader::new(auxv_file)) {
97 let AuxvPair { key, value } = match pair_result {
98 Ok(pair) => pair,
99 Err(e) => {
100 soft_errors.push(e);
101 continue;
102 }
103 };
104 let dest_field = match key {
105 consts::AT_PHNUM => &mut self.program_header_count,
106 consts::AT_PHDR => &mut self.program_header_address,
107 consts::AT_SYSINFO_EHDR => &mut self.linux_gate_address,
108 consts::AT_ENTRY => &mut self.entry_address,
109 _ => continue,
110 };
111 if dest_field.is_none() {
112 *dest_field = Some(value);
113 }
114 }
115
116 failspot!(FillMissingAuxvInfo soft_errors.push(AuxvError::InvalidFormat));
117
118 Ok(())
119 }
120 pub fn get_program_header_count(&self) -> Option<AuxvType> {
121 self.program_header_count
122 }
123 pub fn get_program_header_address(&self) -> Option<AuxvType> {
124 self.program_header_address
125 }
126 pub fn get_linux_gate_address(&self) -> Option<AuxvType> {
127 self.linux_gate_address
128 }
129 pub fn get_entry_address(&self) -> Option<AuxvType> {
130 self.entry_address
131 }
132 pub fn is_complete(&self) -> bool {
133 self.program_header_count.is_some()
134 && self.program_header_address.is_some()
135 && self.linux_gate_address.is_some()
136 && self.entry_address.is_some()
137 }
138 }
139
140 #[derive(Debug, Error, serde::Serialize)]
141 pub enum AuxvError {
142 #[error("Failed to open file {0}")]
143 OpenError(
144 String,
145 #[source]
146 #[serde(serialize_with = "serialize_io_error")]
147 std::io::Error,
148 ),
149 #[error("No auxv entry found for PID {0}")]
150 NoAuxvEntryFound(Pid),
151 #[error("Invalid auxv format (should not hit EOF before AT_NULL)")]
152 InvalidFormat,
153 #[error("IO Error")]
154 IOError(
155 #[from]
156 #[serde(serialize_with = "serialize_io_error")]
157 std::io::Error,
158 ),
159 }