Skip to content

Commit 530ff6f

Browse files
unsafe-wrap some of the win32 backtrace impl
1 parent 978918f commit 530ff6f

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/backtrace/win32.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ struct MyContext(CONTEXT);
9797
#[inline(always)]
9898
pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
9999
// Allocate necessary structures for doing the stack walk
100-
let process = GetCurrentProcess();
101-
let thread = GetCurrentThread();
100+
let process = unsafe { GetCurrentProcess() };
101+
let thread = unsafe { GetCurrentThread() };
102102

103-
let mut context = mem::zeroed::<MyContext>();
104-
RtlCaptureContext(&mut context.0);
103+
let mut context = unsafe { mem::zeroed::<MyContext>() };
104+
unsafe { RtlCaptureContext(&mut context.0) };
105105

106106
// Ensure this process's symbols are initialized
107107
let dbghelp = match dbghelp::init() {
@@ -112,14 +112,15 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
112112
let function_table_access = dbghelp.SymFunctionTableAccess64();
113113
let get_module_base = dbghelp.SymGetModuleBase64();
114114

115-
let process_handle = GetCurrentProcess();
115+
let process_handle = unsafe { GetCurrentProcess() };
116116

117117
// Attempt to use `StackWalkEx` if we can, but fall back to `StackWalk64`
118118
// since it's in theory supported on more systems.
119-
match (*dbghelp.dbghelp()).StackWalkEx() {
119+
match unsafe { (*dbghelp.dbghelp()).StackWalkEx() } {
120120
#[allow(non_snake_case)]
121121
Some(StackWalkEx) => {
122-
let mut inner: STACKFRAME_EX = mem::zeroed();
122+
// It is a C struct meant to be used in classic "out-pointer" fashion, so we zero it
123+
let mut inner: STACKFRAME_EX = unsafe { mem::zeroed() };
123124
inner.StackFrameSize = mem::size_of::<STACKFRAME_EX>() as u32;
124125
let mut frame = super::Frame {
125126
inner: Frame {
@@ -133,7 +134,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
133134
_ => unreachable!(),
134135
};
135136

136-
while StackWalkEx(
137+
while unsafe { StackWalkEx(
137138
image as u32,
138139
process,
139140
thread,
@@ -144,9 +145,9 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
144145
Some(get_module_base),
145146
None,
146147
0,
147-
) == TRUE
148+
) == TRUE }
148149
{
149-
frame.inner.base_address = get_module_base(process_handle, frame.ip() as _) as _;
150+
frame.inner.base_address = unsafe { get_module_base(process_handle, frame.ip() as _) as _ };
150151

151152
if !cb(&frame) {
152153
break;
@@ -156,7 +157,8 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
156157
None => {
157158
let mut frame = super::Frame {
158159
inner: Frame {
159-
stack_frame: StackFrame::Old(mem::zeroed()),
160+
// It is a C struct meant to be used in classic "out-pointer" fashion, so we zero it
161+
stack_frame: StackFrame::Old(unsafe { mem::zeroed() }),
160162
base_address: 0 as _,
161163
},
162164
};
@@ -166,7 +168,7 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
166168
_ => unreachable!(),
167169
};
168170

169-
while dbghelp.StackWalk64()(
171+
while unsafe { dbghelp.StackWalk64()(
170172
image as u32,
171173
process,
172174
thread,
@@ -176,9 +178,9 @@ pub unsafe fn trace(cb: &mut dyn FnMut(&super::Frame) -> bool) {
176178
Some(function_table_access),
177179
Some(get_module_base),
178180
None,
179-
) == TRUE
181+
) == TRUE }
180182
{
181-
frame.inner.base_address = get_module_base(process_handle, frame.ip() as _) as _;
183+
frame.inner.base_address = unsafe { get_module_base(process_handle, frame.ip() as _) as _ };
182184

183185
if !cb(&frame) {
184186
break;

0 commit comments

Comments
 (0)