Debian Codegraph @

third_party/rust/minidump-writer/src/mac/streams/thread_list.rs

raw
1 use super::*;
2 use crate::minidump_cpu::RawContextCPU;
3
4 impl MinidumpWriter {
5 /// Writes the [`MDStreamType::ThreadListStream`] which is an array of
6 /// [`miniduimp_common::format::MINIDUMP_THREAD`]
7 pub(crate) fn write_thread_list(
8 &mut self,
9 buffer: &mut DumpBuf,
10 dumper: &TaskDumper,
11 ) -> Result<MDRawDirectory, WriterError> {
12 let threads = self.threads(dumper);
13
14 let list_header = MemoryWriter::<u32>::alloc_with_val(buffer, threads.len() as u32)?;
15
16 let mut dirent = MDRawDirectory {
17 stream_type: MDStreamType::ThreadListStream as u32,
18 location: list_header.location(),
19 };
20
21 let mut thread_list = MemoryArrayWriter::<MDRawThread>::alloc_array(buffer, threads.len())?;
22 dirent.location.data_size += thread_list.location().data_size;
23
24 for (i, tid) in threads.enumerate() {
25 let thread = self.write_thread(tid, buffer, dumper)?;
26 thread_list.set_value_at(buffer, thread, i)?;
27 }
28
29 Ok(dirent)
30 }
31
32 fn write_thread(
33 &mut self,
34 tid: u32,
35 buffer: &mut DumpBuf,
36 dumper: &TaskDumper,
37 ) -> Result<MDRawThread, WriterError> {
38 let mut thread = MDRawThread {
39 thread_id: tid,
40 suspend_count: 0,
41 priority_class: 0,
42 priority: 0,
43 teb: 0,
44 stack: MDMemoryDescriptor::default(),
45 thread_context: MDLocationDescriptor::default(),
46 };
47
48 let thread_state = dumper.read_thread_state(tid)?;
49
50 self.write_stack_from_start_address(thread_state.sp(), &mut thread, buffer, dumper)?;
51
52 let mut cpu: RawContextCPU = Default::default();
53 Self::fill_cpu_context(&thread_state, &mut cpu);
54 let cpu_section = MemoryWriter::alloc_with_val(buffer, cpu)?;
55 thread.thread_context = cpu_section.location();
56 Ok(thread)
57 }
58
59 fn write_stack_from_start_address(
60 &mut self,
61 start: u64,
62 thread: &mut MDRawThread,
63 buffer: &mut DumpBuf,
64 dumper: &TaskDumper,
65 ) -> Result<(), WriterError> {
66 thread.stack.start_of_memory_range = start;
67 thread.stack.memory.data_size = 0;
68 thread.stack.memory.rva = buffer.position() as u32;
69
70 let stack_size = self.calculate_stack_size(start, dumper);
71
72 // In some situations the stack address for the thread can come back 0.
73 // In these cases we skip over the threads in question and stuff the
74 // stack with a clearly borked value.
75 //
76 // In other cases, notably a stack overflow, we might fail to read the
77 // stack eg. InvalidAddress in which case we use a different borked
78 // value to indicate the different failure
79 let stack_location = if stack_size != 0 {
80 dumper
81 .read_task_memory(start, stack_size)
82 .map(|stack_buffer| {
83 let stack_location = MDLocationDescriptor {
84 data_size: stack_buffer.len() as u32,
85 rva: buffer.position() as u32,
86 };
87 buffer.write_all(&stack_buffer);
88 stack_location
89 })
90 .ok()
91 } else {
92 None
93 };
94
95 thread.stack.memory = stack_location.unwrap_or_else(|| {
96 let borked = if stack_size == 0 {
97 0xdeadbeef
98 } else {
99 0xdeaddead
100 };
101
102 thread.stack.start_of_memory_range = borked;
103
104 let stack_location = MDLocationDescriptor {
105 data_size: 16,
106 rva: buffer.position() as u32,
107 };
108 buffer.write_all(&borked.to_ne_bytes());
109 buffer.write_all(&borked.to_ne_bytes());
110 stack_location
111 });
112
113 // Add the stack memory as a raw block of memory, this is written to
114 // the minidump as part of the memory list stream
115 self.memory_blocks.push(thread.stack);
116 Ok(())
117 }
118
119 fn calculate_stack_size(&self, start_address: u64, dumper: &TaskDumper) -> usize {
120 if start_address == 0 {
121 return 0;
122 }
123
124 let mut region = if let Ok(region) = dumper.get_vm_region(start_address) {
125 region
126 } else {
127 return 0;
128 };
129
130 // Failure or stack corruption, since mach_vm_region had to go
131 // higher in the process address space to find a valid region.
132 if start_address < region.range.start {
133 return 0;
134 }
135
136 let root_range_start = region.range.start;
137 let mut stack_size = region.range.end - region.range.start;
138
139 // If the user tag is VM_MEMORY_STACK, look for more readable regions with
140 // the same tag placed immediately above the computed stack region. Under
141 // some circumstances, the stack for thread 0 winds up broken up into
142 // multiple distinct abutting regions. This can happen for several reasons,
143 // including user code that calls setrlimit(RLIMIT_STACK, ...) or changes
144 // the access on stack pages by calling mprotect.
145 if region.info.user_tag == mach2::vm_statistics::VM_MEMORY_STACK {
146 loop {
147 let proposed_next_region_base = region.range.end;
148
149 region = if let Ok(reg) = dumper.get_vm_region(region.range.end) {
150 reg
151 } else {
152 break;
153 };
154
155 if region.range.start != proposed_next_region_base
156 || region.info.user_tag != mach2::vm_statistics::VM_MEMORY_STACK
157 || (region.info.protection & mach2::vm_prot::VM_PROT_READ) == 0
158 {
159 break;
160 }
161
162 stack_size += region.range.end - region.range.start;
163 }
164 }
165
166 (root_range_start + stack_size - start_address) as usize
167 }
168
169 pub(crate) fn fill_cpu_context(
170 thread_state: &crate::mac::mach::ThreadState,
171 out: &mut RawContextCPU,
172 ) {
173 let ts = thread_state.arch_state();
174
175 cfg_if::cfg_if! {
176 if #[cfg(target_arch = "x86_64")] {
177 out.context_flags = format::ContextFlagsCpu::CONTEXT_AMD64.bits();
178
179 out.rax = ts.__rax;
180 out.rbx = ts.__rbx;
181 out.rcx = ts.__rcx;
182 out.rdx = ts.__rdx;
183 out.rdi = ts.__rdi;
184 out.rsi = ts.__rsi;
185 out.rbp = ts.__rbp;
186 out.rsp = ts.__rsp;
187 out.r8 = ts.__r8;
188 out.r9 = ts.__r9;
189 out.r10 = ts.__r10;
190 out.r11 = ts.__r11;
191 out.r12 = ts.__r12;
192 out.r13 = ts.__r13;
193 out.r14 = ts.__r14;
194 out.r15 = ts.__r15;
195 out.rip = ts.__rip;
196 // according to AMD's software developer guide, bits above 18 are
197 // not used in the flags register. Since the minidump format
198 // specifies 32 bits for the flags register, we can truncate safely
199 // with no loss.
200 out.eflags = ts.__rflags as _;
201 out.cs = ts.__cs as u16;
202 out.fs = ts.__fs as u16;
203 out.gs = ts.__gs as u16;
204 } else if #[cfg(target_arch = "aarch64")] {
205 // This is kind of a lie as we don't actually include the full float state..?
206 out.context_flags = format::ContextFlagsArm64Old::CONTEXT_ARM64_OLD_FULL.bits() as u64;
207
208 out.cpsr = ts.cpsr;
209 out.iregs[..29].copy_from_slice(&ts.x[..29]);
210 out.iregs[29] = ts.fp;
211 out.iregs[30] = ts.lr;
212 out.sp = ts.sp;
213 out.pc = ts.pc;
214 } else {
215 compile_error!("unsupported target arch");
216 }
217 }
218 }
219 }