Debian Codegraph @

third_party/rust/minidump-writer/src/windows/ffi.rs

raw
1 //! Contains bindings for [`MiniDumpWriteDump`](https://learn.microsoft.com/en-us/windows/win32/api/minidumpapiset/nf-minidumpapiset-minidumpwritedump)
2 //! and related structures, as they are not present in `winapi` and we don't want
3 //! to depend on `windows-sys` due to version churn.
4 //!
5 //! Also has a binding for [`GetThreadContext`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadcontext)
6 //! as the `CONTEXT` structures in `winapi` are not correctly aligned which can
7 //! cause crashes or bad data, so the [`crash_context::ffi::CONTEXT`] is used
8 //! instead. See [#63](https://github.com/rust-minidump/minidump-writer/issues/63)
9
10 #![allow(
11 non_snake_case,
12 non_camel_case_types,
13 non_upper_case_globals,
14 clippy::upper_case_acronyms
15 )]
16
17 pub use crash_context::{capture_context, CONTEXT, EXCEPTION_POINTERS, EXCEPTION_RECORD};
18
19 pub type HANDLE = isize;
20 pub type BOOL = i32;
21 pub const FALSE: BOOL = 0;
22
23 pub type Hresult = i32;
24 pub const STATUS_NONCONTINUABLE_EXCEPTION: i32 = -1073741787;
25
26 pub type PROCESS_ACCESS_RIGHTS = u32;
27 pub const PROCESS_ALL_ACCESS: PROCESS_ACCESS_RIGHTS = 2097151;
28
29 pub type THREAD_ACCESS_RIGHTS = u32;
30 pub const THREAD_SUSPEND_RESUME: THREAD_ACCESS_RIGHTS = 2;
31 pub const THREAD_GET_CONTEXT: THREAD_ACCESS_RIGHTS = 8;
32 pub const THREAD_QUERY_INFORMATION: THREAD_ACCESS_RIGHTS = 64;
33
34 bitflags::bitflags! {
35 /// <https://learn.microsoft.com/en-us/windows/win32/api/minidumpapiset/ne-minidumpapiset-minidump_type>
36 #[derive(Copy, Clone, Debug)]
37 #[repr(transparent)]
38 pub struct MinidumpType: u32 {
39 /// Include just the information necessary to capture stack traces for all
40 /// existing threads in a process.
41 const Normal = 0;
42 /// Include the data sections from all loaded modules.
43 ///
44 /// This results in the inclusion of global variables, which can make
45 /// the minidump file significantly larger.
46 const WithDataSegs = 1 << 0;
47 /// Include all accessible memory in the process.
48 ///
49 /// The raw memory data is included at the end, so that the initial
50 /// structures can be mapped directly without the raw memory information.
51 /// This option can result in a very large file.
52 const WithFullMemory = 1 << 1;
53 /// Include high-level information about the operating system handles that
54 /// are active when the minidump is made.
55 const WithHandleData = 1 << 2;
56 /// Stack and backing store memory written to the minidump file should be
57 /// filtered to remove all but the pointer values necessary to reconstruct a
58 /// stack trace.
59 const FilterMemory = 1 << 3;
60 /// Stack and backing store memory should be scanned for pointer references
61 /// to modules in the module list.
62 ///
63 /// If a module is referenced by stack or backing store memory, the
64 /// [`MINIDUMP_CALLBACK_OUTPUT_0::ModuleWriteFlags`] field is set to
65 /// [`ModuleWriteFlags::ModuleReferencedByMemory`].
66 const ScanMemory = 1 << 4;
67 /// Include information from the list of modules that were recently
68 /// unloaded, if this information is maintained by the operating system.
69 const WithUnloadedModules = 1 << 5;
70 /// Include pages with data referenced by locals or other stack memory.
71 /// This option can increase the size of the minidump file significantly.
72 const WithIndirectlyReferencedMemory = 1 << 6;
73 /// Filter module paths for information such as user names or important
74 /// directories.
75 ///
76 /// This option may prevent the system from locating the image file and
77 /// should be used only in special situations.
78 const FilterModulePaths = 1 << 7;
79 /// Include complete per-process and per-thread information from the
80 /// operating system.
81 const WithProcessThreadData = 1 << 8;
82 /// Scan the virtual address space for [`PAGE_READWRITE`](https://learn.microsoft.com/en-us/windows/win32/memory/memory-protection-constants)
83 /// memory to be included.
84 const WithPrivateReadWriteMemory = 1 << 9;
85 /// Reduce the data that is dumped by eliminating memory regions that
86 /// are not essential to meet criteria specified for the dump.
87 ///
88 /// This can avoid dumping memory that may contain data that is private
89 /// to the user. However, it is not a guarantee that no private information
90 /// will be present.
91 const WithoutOptionalData = 1 << 10;
92 /// Include memory region information.
93 ///
94 /// See [MINIDUMP_MEMORY_INFO_LIST](https://learn.microsoft.com/en-us/windows/win32/api/minidumpapiset/ns-minidumpapiset-minidump_memory_info_list)
95 const WithFullMemoryInfo = 1 << 11;
96 /// Include thread state information.
97 ///
98 /// See [MINIDUMP_THREAD_INFO_LIST](https://learn.microsoft.com/en-us/windows/win32/api/minidumpapiset/ns-minidumpapiset-minidump_thread_info_list)
99 const WithThreadInfo = 1 << 12;
100 /// Include all code and code-related sections from loaded modules to
101 /// capture executable content.
102 ///
103 /// For per-module control, use the [`ModuleWriteFlags::ModuleWriteCodeSegs`]
104 const WithCodeSegs = 1 << 13;
105 /// Turns off secondary auxiliary-supported memory gathering.
106 const WithoutAuxiliaryState = 1 << 14;
107 /// Requests that auxiliary data providers include their state in the
108 /// dump image; the state data that is included is provider dependent.
109 ///
110 /// This option can result in a large dump image.
111 const WithFullAuxiliaryState = 1 << 15;
112 /// Scans the virtual address space for [`PAGE_WRITECOPY`](https://learn.microsoft.com/en-us/windows/win32/memory/memory-protection-constants) memory to be included.
113 const WithPrivateWriteCopyMemory = 1 << 16;
114 /// If you specify [`MinidumpType::MiniDumpWithFullMemory`], the
115 /// `MiniDumpWriteDump` function will fail if the function cannot read
116 /// the memory regions; however, if you include
117 /// [`IgnoreInaccessibleMemory`], the `MiniDumpWriteDump` function will
118 /// ignore the memory read failures and continue to generate the dump.
119 ///
120 /// Note that the inaccessible memory regions are not included in the dump.
121 const IgnoreInaccessibleMemory = 1 << 17;
122 /// Adds security token related data.
123 ///
124 /// This will make the "!token" extension work when processing a user-mode dump.
125 const WithTokenInformation = 1 << 18;
126 /// Adds module header related data.
127 const WithModuleHeaders = 1 << 19;
128 /// Adds filter triage related data.
129 const FilterTriage = 1 << 20;
130 /// Adds AVX crash state context registers.
131 const WithAvxXStateContext = 1 << 21;
132 /// Adds Intel Processor Trace related data.
133 const WithIptTrace = 1 << 22;
134 /// Scans inaccessible partial memory pages.
135 const ScanInaccessiblePartialPages = 1 << 23;
136 /// Exclude all memory with the virtual protection attribute of [`PAGE_WRITECOMBINE`](https://learn.microsoft.com/en-us/windows/win32/memory/memory-protection-constants).
137 const FilterWriteCombinedMemory = 1 << 24;
138 }
139 }
140
141 pub type VS_FIXEDFILEINFO_FILE_FLAGS = u32;
142
143 #[repr(C, packed(4))]
144 pub struct MINIDUMP_USER_STREAM {
145 pub Type: u32,
146 pub BufferSize: u32,
147 pub Buffer: *mut std::ffi::c_void,
148 }
149 #[repr(C, packed(4))]
150 pub struct MINIDUMP_USER_STREAM_INFORMATION {
151 pub UserStreamCount: u32,
152 pub UserStreamArray: *mut MINIDUMP_USER_STREAM,
153 }
154
155 #[repr(C, packed(4))]
156 pub struct MINIDUMP_EXCEPTION_INFORMATION {
157 pub ThreadId: u32,
158 pub ExceptionPointers: *mut EXCEPTION_POINTERS,
159 pub ClientPointers: BOOL,
160 }
161
162 pub type VS_FIXEDFILEINFO_FILE_OS = i32;
163 pub type VS_FIXEDFILEINFO_FILE_TYPE = i32;
164 pub type VS_FIXEDFILEINFO_FILE_SUBTYPE = i32;
165
166 #[repr(C)]
167 pub struct VS_FIXEDFILEINFO {
168 pub dwSignature: u32,
169 pub dwStrucVersion: u32,
170 pub dwFileVersionMS: u32,
171 pub dwFileVersionLS: u32,
172 pub dwProductVersionMS: u32,
173 pub dwProductVersionLS: u32,
174 pub dwFileFlagsMask: u32,
175 pub dwFileFlags: VS_FIXEDFILEINFO_FILE_FLAGS,
176 pub dwFileOS: VS_FIXEDFILEINFO_FILE_OS,
177 pub dwFileType: VS_FIXEDFILEINFO_FILE_TYPE,
178 pub dwFileSubtype: VS_FIXEDFILEINFO_FILE_SUBTYPE,
179 pub dwFileDateMS: u32,
180 pub dwFileDateLS: u32,
181 }
182 #[repr(C, packed(4))]
183 pub struct MINIDUMP_MODULE_CALLBACK {
184 pub FullPath: *mut u16,
185 pub BaseOfImage: u64,
186 pub SizeOfImage: u32,
187 pub CheckSum: u32,
188 pub TimeDateStamp: u32,
189 pub VersionInfo: VS_FIXEDFILEINFO,
190 pub CvRecord: *mut std::ffi::c_void,
191 pub SizeOfCvRecord: u32,
192 pub MiscRecord: *mut std::ffi::c_void,
193 pub SizeOfMiscRecord: u32,
194 }
195
196 #[repr(C, packed(4))]
197 pub struct MINIDUMP_INCLUDE_THREAD_CALLBACK {
198 pub ThreadId: u32,
199 }
200
201 #[repr(C, packed(4))]
202 pub struct MINIDUMP_INCLUDE_MODULE_CALLBACK {
203 pub BaseOfImage: u64,
204 }
205
206 #[repr(C, packed(4))]
207 pub struct MINIDUMP_IO_CALLBACK {
208 pub Handle: HANDLE,
209 pub Offset: u64,
210 pub Buffer: *mut std::ffi::c_void,
211 pub BufferBytes: u32,
212 }
213
214 #[repr(C, packed(4))]
215 pub struct MINIDUMP_READ_MEMORY_FAILURE_CALLBACK {
216 pub Offset: u64,
217 pub Bytes: u32,
218 pub FailureStatus: Hresult,
219 }
220
221 #[repr(C, packed(4))]
222 pub struct MINIDUMP_VM_QUERY_CALLBACK {
223 pub Offset: u64,
224 }
225
226 #[repr(C, packed(4))]
227 pub struct MINIDUMP_VM_PRE_READ_CALLBACK {
228 pub Offset: u64,
229 pub Buffer: *mut std::ffi::c_void,
230 pub Size: u32,
231 }
232
233 #[repr(C, packed(4))]
234 pub struct MINIDUMP_VM_POST_READ_CALLBACK {
235 pub Offset: u64,
236 pub Buffer: *mut std::ffi::c_void,
237 pub Size: u32,
238 pub Completed: u32,
239 pub Status: Hresult,
240 }
241
242 /// Oof, so we have a problem with these structs, they are all packed(4), but
243 /// `CONTEXT` is aligned by either 4 (x86) or 16 (x86_64/aarch64)...which Rust
244 /// doesn't currently allow https://github.com/rust-lang/rust/issues/59154, so
245 /// we need to basically cheat with a big byte array until that issue is fixed (possibly never)
246 #[repr(C)]
247 pub struct CALLBACK_CONTEXT([u8; std::mem::size_of::<CONTEXT>()]);
248
249 cfg_if::cfg_if! {
250 if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
251 #[repr(C, packed(4))]
252 pub struct MINIDUMP_THREAD_CALLBACK {
253 pub ThreadId: u32,
254 pub ThreadHandle: HANDLE,
255 pub Context: CALLBACK_CONTEXT,
256 pub SizeOfContext: u32,
257 pub StackBase: u64,
258 pub StackEnd: u64,
259 }
260
261 #[repr(C, packed(4))]
262 pub struct MINIDUMP_THREAD_EX_CALLBACK {
263 pub ThreadId: u32,
264 pub ThreadHandle: HANDLE,
265 pub Context: CALLBACK_CONTEXT,
266 pub SizeOfContext: u32,
267 pub StackBase: u64,
268 pub StackEnd: u64,
269 pub BackingStoreBase: u64,
270 pub BackingStoreEnd: u64,
271 }
272 } else if #[cfg(target_arch = "aarch64")] {
273 #[repr(C, packed(4))]
274 pub struct MINIDUMP_THREAD_CALLBACK {
275 pub ThreadId: u32,
276 pub ThreadHandle: HANDLE,
277 pub Pad: u32,
278 pub Context: CALLBACK_CONTEXT,
279 pub SizeOfContext: u32,
280 pub StackBase: u64,
281 pub StackEnd: u64,
282 }
283
284 #[repr(C, packed(4))]
285 pub struct MINIDUMP_THREAD_EX_CALLBACK {
286 pub ThreadId: u32,
287 pub ThreadHandle: HANDLE,
288 pub Pad: u32,
289 pub Context: CALLBACK_CONTEXT,
290 pub SizeOfContext: u32,
291 pub StackBase: u64,
292 pub StackEnd: u64,
293 pub BackingStoreBase: u64,
294 pub BackingStoreEnd: u64,
295 }
296 }
297 }
298
299 #[repr(C)]
300 pub union MINIDUMP_CALLBACK_INPUT_0 {
301 pub Status: Hresult,
302 pub Thread: std::mem::ManuallyDrop<MINIDUMP_THREAD_CALLBACK>,
303 pub ThreadEx: std::mem::ManuallyDrop<MINIDUMP_THREAD_EX_CALLBACK>,
304 pub Module: std::mem::ManuallyDrop<MINIDUMP_MODULE_CALLBACK>,
305 pub IncludeThread: std::mem::ManuallyDrop<MINIDUMP_INCLUDE_THREAD_CALLBACK>,
306 pub IncludeModule: std::mem::ManuallyDrop<MINIDUMP_INCLUDE_MODULE_CALLBACK>,
307 pub Io: std::mem::ManuallyDrop<MINIDUMP_IO_CALLBACK>,
308 pub ReadMemoryFailure: std::mem::ManuallyDrop<MINIDUMP_READ_MEMORY_FAILURE_CALLBACK>,
309 pub SecondaryFlags: u32,
310 pub VmQuery: std::mem::ManuallyDrop<MINIDUMP_VM_QUERY_CALLBACK>,
311 pub VmPreRead: std::mem::ManuallyDrop<MINIDUMP_VM_PRE_READ_CALLBACK>,
312 pub VmPostRead: std::mem::ManuallyDrop<MINIDUMP_VM_POST_READ_CALLBACK>,
313 }
314
315 #[repr(C, packed(4))]
316 pub struct MINIDUMP_CALLBACK_INPUT {
317 pub ProcessId: u32,
318 pub ProcessHandle: HANDLE,
319 pub CallbackType: u32,
320 pub Anonymous: MINIDUMP_CALLBACK_INPUT_0,
321 }
322
323 pub type VIRTUAL_ALLOCATION_TYPE = u32;
324
325 #[repr(C, packed(4))]
326 pub struct MINIDUMP_MEMORY_INFO {
327 pub BaseAddress: u64,
328 pub AllocationBase: u64,
329 pub AllocationProtect: u32,
330 __alignment1: u32,
331 pub RegionSize: u64,
332 pub State: VIRTUAL_ALLOCATION_TYPE,
333 pub Protect: u32,
334 pub Type: u32,
335 __alignment2: u32,
336 }
337
338 #[repr(C, packed(4))]
339 pub struct MINIDUMP_CALLBACK_OUTPUT_0_0 {
340 pub MemoryBase: u64,
341 pub MemorySize: u32,
342 }
343
344 #[repr(C)]
345 pub struct MINIDUMP_CALLBACK_OUTPUT_0_1 {
346 pub CheckCancel: BOOL,
347 pub Cancel: BOOL,
348 }
349
350 #[repr(C)]
351 pub struct MINIDUMP_CALLBACK_OUTPUT_0_2 {
352 pub VmRegion: MINIDUMP_MEMORY_INFO,
353 pub Continue: BOOL,
354 }
355
356 #[repr(C)]
357 pub struct MINIDUMP_CALLBACK_OUTPUT_0_3 {
358 pub VmQueryStatus: Hresult,
359 pub VmQueryResult: MINIDUMP_MEMORY_INFO,
360 }
361
362 #[repr(C)]
363 pub struct MINIDUMP_CALLBACK_OUTPUT_0_4 {
364 pub VmReadStatus: Hresult,
365 pub VmReadBytesCompleted: u32,
366 }
367
368 bitflags::bitflags! {
369 /// Identifies the type of module information that will be written to the
370 /// minidump file by the MiniDumpWriteDump function.
371 #[derive(Copy, Clone)]
372 #[repr(transparent)]
373 pub struct ModuleWriteFlags: u32 {
374 /// Only module information will be written to the minidump file.
375 const ModuleWriteModule = 0x0001;
376 const ModuleWriteDataSeg = 0x0002;
377 const ModuleWriteMiscRecord = 0x0004;
378 const ModuleWriteCvRecord = 0x0008;
379 const ModuleReferencedByMemory = 0x0010;
380 const ModuleWriteTlsData = 0x0020;
381 const ModuleWriteCodeSegs = 0x0040;
382 }
383 }
384
385 #[repr(C)]
386 pub union MINIDUMP_CALLBACK_OUTPUT_0 {
387 pub ModuleWriteFlags: ModuleWriteFlags,
388 pub ThreadWriteFlags: u32,
389 pub SecondaryFlags: u32,
390 pub Anonymous1: std::mem::ManuallyDrop<MINIDUMP_CALLBACK_OUTPUT_0_0>,
391 pub Anonymous2: std::mem::ManuallyDrop<MINIDUMP_CALLBACK_OUTPUT_0_1>,
392 pub Handle: HANDLE,
393 pub Anonymous3: std::mem::ManuallyDrop<MINIDUMP_CALLBACK_OUTPUT_0_2>,
394 pub Anonymous4: std::mem::ManuallyDrop<MINIDUMP_CALLBACK_OUTPUT_0_3>,
395 pub Anonymous5: std::mem::ManuallyDrop<MINIDUMP_CALLBACK_OUTPUT_0_4>,
396 pub Status: Hresult,
397 }
398
399 #[repr(C, packed(4))]
400 pub struct MINIDUMP_CALLBACK_OUTPUT {
401 pub Anonymous: MINIDUMP_CALLBACK_OUTPUT_0,
402 }
403
404 pub type MINIDUMP_CALLBACK_ROUTINE = Option<
405 unsafe extern "system" fn(
406 CallbackParam: *mut std::ffi::c_void,
407 CallbackInput: *const MINIDUMP_CALLBACK_INPUT,
408 CallbackOutput: *mut MINIDUMP_CALLBACK_OUTPUT,
409 ) -> BOOL,
410 >;
411
412 #[repr(C, packed(4))]
413 pub struct MINIDUMP_CALLBACK_INFORMATION {
414 pub CallbackRoutine: MINIDUMP_CALLBACK_ROUTINE,
415 pub CallbackParam: *mut std::ffi::c_void,
416 }
417
418 #[link(name = "kernel32")]
419 extern "system" {
420 pub fn CloseHandle(handle: HANDLE) -> BOOL;
421 pub fn GetCurrentProcess() -> HANDLE;
422 pub fn GetCurrentThreadId() -> u32;
423 pub fn OpenProcess(
424 desired_access: PROCESS_ACCESS_RIGHTS,
425 inherit_handle: BOOL,
426 process_id: u32,
427 ) -> HANDLE;
428 pub fn OpenThread(
429 desired_access: THREAD_ACCESS_RIGHTS,
430 inherit_handle: BOOL,
431 thread_id: u32,
432 ) -> HANDLE;
433 pub fn ResumeThread(thread: HANDLE) -> u32;
434 pub fn SuspendThread(thread: HANDLE) -> u32;
435 pub fn GetThreadContext(thread: HANDLE, context: *mut CONTEXT) -> BOOL;
436 }
437
438 #[link(name = "dbghelp")]
439 extern "system" {
440 pub fn MiniDumpWriteDump(
441 process: HANDLE,
442 process_id: u32,
443 file: HANDLE,
444 dump_type: MinidumpType,
445 exception_param: *const MINIDUMP_EXCEPTION_INFORMATION,
446 user_stream_param: *const MINIDUMP_USER_STREAM_INFORMATION,
447 callback_param: *const MINIDUMP_CALLBACK_INFORMATION,
448 ) -> BOOL;
449 }