Skip to content

Commit 918fd31

Browse files
authored
fix: Avoid accidentally mutating CONTEXT when stack walking (#77)
1 parent 4edb2bb commit 918fd31

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

snapshot/win/process_reader_win.cc

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,20 @@ void DoStackWalk(ProcessReaderWin::Thread* thread,
151151
stack_frame.AddrStack.Mode = AddrModeFlat;
152152

153153
int machine_type = IMAGE_FILE_MACHINE_I386;
154-
LPVOID ctx = NULL;
154+
CONTEXT ctx;
155155
#if defined(ARCH_CPU_X86)
156-
const CONTEXT* ctx_ = thread->context.context<CONTEXT>();
157-
stack_frame.AddrPC.Offset = ctx_->Eip;
158-
stack_frame.AddrFrame.Offset = ctx_->Ebp;
159-
stack_frame.AddrStack.Offset = ctx_->Esp;
160-
ctx = (LPVOID)ctx_;
156+
ctx = *thread->context.context<CONTEXT>();
157+
stack_frame.AddrPC.Offset = ctx.Eip;
158+
stack_frame.AddrFrame.Offset = ctx.Ebp;
159+
stack_frame.AddrStack.Offset = ctx.Esp;
161160
#elif defined(ARCH_CPU_X86_64)
162161
// if (!is_64_reading_32) {
163162
machine_type = IMAGE_FILE_MACHINE_AMD64;
164163

165-
const CONTEXT* ctx_ = thread->context.context<CONTEXT>();
166-
stack_frame.AddrPC.Offset = ctx_->Rip;
167-
stack_frame.AddrFrame.Offset = ctx_->Rbp;
168-
stack_frame.AddrStack.Offset = ctx_->Rsp;
169-
ctx = (LPVOID)ctx_;
164+
ctx = *thread->context.context<CONTEXT>();
165+
stack_frame.AddrPC.Offset = ctx.Rip;
166+
stack_frame.AddrFrame.Offset = ctx.Rbp;
167+
stack_frame.AddrStack.Offset = ctx.Rsp;
170168
// } else {
171169
// const WOW64_CONTEXT* ctx_ = &thread->context.wow64;
172170
// stack_frame.AddrPC.Offset = ctx_->Eip;
@@ -176,7 +174,7 @@ void DoStackWalk(ProcessReaderWin::Thread* thread,
176174
// }
177175

178176
// TODO: we dont support this right away, maybe in the future
179-
//#elif defined(ARCH_CPU_ARM64)
177+
// #elif defined(ARCH_CPU_ARM64)
180178
// machine_type = IMAGE_FILE_MACHINE_ARM64;
181179
#else
182180
#error Unsupported Windows Arch
@@ -192,7 +190,7 @@ void DoStackWalk(ProcessReaderWin::Thread* thread,
192190
process,
193191
thread_handle,
194192
&stack_frame,
195-
ctx,
193+
&ctx,
196194
NULL,
197195
SymFunctionTableAccess64,
198196
SymGetModuleBase64,

0 commit comments

Comments
 (0)