1//===-- sanitizer_unwind_win.cpp ------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// Sanitizer unwind Windows specific functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "sanitizer_platform.h"
14#if SANITIZER_WINDOWS
15
16#define WIN32_LEAN_AND_MEAN
17#define NOGDI
18#include <windows.h>
19
20#include "sanitizer_dbghelp.h" // for StackWalk64
21#include "sanitizer_stacktrace.h"
22#include "sanitizer_symbolizer.h" // for InitializeDbgHelpIfNeeded
23
24using namespace __sanitizer;
25
26#if !SANITIZER_GO
27void BufferedStackTrace::UnwindSlow(uptr pc, u32 max_depth) {
28 CHECK_GE(max_depth, 2);
29 // FIXME: CaptureStackBackTrace might be too slow for us.
30 // FIXME: Compare with StackWalk64.
31 // FIXME: Look at LLVMUnhandledExceptionFilter in Signals.inc
32 size = CaptureStackBackTrace(1, Min(max_depth, kStackTraceMax),
33 (void **)&trace_buffer[0], 0);
34 if (size == 0)
35 return;
36
37 // Skip the RTL frames by searching for the PC in the stacktrace.
38 uptr pc_location = LocatePcInTrace(pc);
39 PopStackFrames(pc_location);
40
41 // Replace the first frame with the PC because the frame in the
42 // stacktrace might be incorrect.
43 trace_buffer[0] = pc;
44}
45
46PVOID CALLBACK FallbackFunctionTableAccess(HANDLE hProcess,
47 DWORD64 dwAddrBase) {
48 // First try DbgHelp's function.
49 if (PVOID pResult =
50 __sanitizer::SymFunctionTableAccess64(hProcess, dwAddrBase)) {
51 return pResult;
52 }
53
54 // Fall back to RtlLookupFunctionEntry for dynamic code.
55 // Function registered with RtlAddFunctionTable is not necessarily registered
56 // with DbgHelp, so this is required to cover some edge cases (e.g. JIT
57 // compilers can use Rtl* functions).
58# if SANITIZER_WINDOWS64
59 DWORD64 dw64ImageBase = 0;
60 return RtlLookupFunctionEntry(dwAddrBase, &dw64ImageBase, nullptr);
61# else
62 return nullptr;
63# endif
64}
65
66DWORD64 CALLBACK FallbackGetModuleBase(HANDLE hProcess, DWORD64 dwAddr) {
67 if (DWORD64 dwResult = __sanitizer::SymGetModuleBase64(hProcess, dwAddr)) {
68 return dwResult;
69 }
70
71 // Both GetModuleBase and FunctionTableAccess must provide this fallback,
72 // otherwise dynamic functions won't be properly unwound.
73# if SANITIZER_WINDOWS64
74 DWORD64 dw64ImageBase = 0;
75 if (RtlLookupFunctionEntry(dwAddr, &dw64ImageBase, nullptr)) {
76 return dw64ImageBase;
77 }
78# endif
79
80 return 0;
81}
82
83# ifdef __clang__
84# pragma clang diagnostic push
85# pragma clang diagnostic ignored "-Wframe-larger-than="
86# endif
87void BufferedStackTrace::UnwindSlow(uptr pc, void *context, u32 max_depth) {
88 CHECK(context);
89 CHECK_GE(max_depth, 2);
90 CONTEXT ctx = *(CONTEXT *)context;
91 STACKFRAME64 stack_frame;
92 memset(&stack_frame, 0, sizeof(stack_frame));
93
94 InitializeDbgHelpIfNeeded();
95
96 size = 0;
97# if SANITIZER_WINDOWS64
98# if SANITIZER_ARM64
99 int machine_type = IMAGE_FILE_MACHINE_ARM64;
100 stack_frame.AddrPC.Offset = ctx.Pc;
101 stack_frame.AddrFrame.Offset = ctx.Fp;
102 stack_frame.AddrStack.Offset = ctx.Sp;
103# else
104 int machine_type = IMAGE_FILE_MACHINE_AMD64;
105 stack_frame.AddrPC.Offset = ctx.Rip;
106 stack_frame.AddrFrame.Offset = ctx.Rbp;
107 stack_frame.AddrStack.Offset = ctx.Rsp;
108# endif
109# else
110# if SANITIZER_ARM
111 int machine_type = IMAGE_FILE_MACHINE_ARM;
112 stack_frame.AddrPC.Offset = ctx.Pc;
113 stack_frame.AddrFrame.Offset = ctx.R11;
114 stack_frame.AddrStack.Offset = ctx.Sp;
115# elif SANITIZER_MIPS32
116 int machine_type = IMAGE_FILE_MACHINE_R4000;
117 stack_frame.AddrPC.Offset = ctx.Fir;
118 stack_frame.AddrFrame.Offset = ctx.IntS8;
119 stack_frame.AddrStack.Offset = ctx.IntSp;
120# else
121 int machine_type = IMAGE_FILE_MACHINE_I386;
122 stack_frame.AddrPC.Offset = ctx.Eip;
123 stack_frame.AddrFrame.Offset = ctx.Ebp;
124 stack_frame.AddrStack.Offset = ctx.Esp;
125# endif
126# endif
127 stack_frame.AddrPC.Mode = AddrModeFlat;
128 stack_frame.AddrFrame.Mode = AddrModeFlat;
129 stack_frame.AddrStack.Mode = AddrModeFlat;
130 while (StackWalk64(machine_type, GetCurrentProcess(), GetCurrentThread(),
131 &stack_frame, &ctx, NULL, FallbackFunctionTableAccess,
132 FallbackGetModuleBase, NULL) &&
133 size < Min(max_depth, kStackTraceMax)) {
134 trace_buffer[size++] = (uptr)stack_frame.AddrPC.Offset;
135 }
136}
137# ifdef __clang__
138# pragma clang diagnostic pop
139# endif
140# endif // #if !SANITIZER_GO
141
142#endif // SANITIZER_WINDOWS
143