1 //===-- sanitizer_symbolizer_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 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries.
11 // Windows-specific implementation of symbolizer parts.
12 //===----------------------------------------------------------------------===//
13 
14 #include "sanitizer_platform.h"
15 #if SANITIZER_WINDOWS
16 
17 #include "sanitizer_dbghelp.h"
18 #include "sanitizer_symbolizer_internal.h"
19 
20 namespace __sanitizer {
21 
22 decltype(::StackWalk64) *StackWalk64;
23 decltype(::SymCleanup) *SymCleanup;
24 decltype(::SymFromAddr) *SymFromAddr;
25 decltype(::SymFunctionTableAccess64) *SymFunctionTableAccess64;
26 decltype(::SymGetLineFromAddr64) *SymGetLineFromAddr64;
27 decltype(::SymGetModuleBase64) *SymGetModuleBase64;
28 decltype(::SymGetSearchPathW) *SymGetSearchPathW;
29 decltype(::SymInitialize) *SymInitialize;
30 decltype(::SymSetOptions) *SymSetOptions;
31 decltype(::SymSetSearchPathW) *SymSetSearchPathW;
32 decltype(::UnDecorateSymbolName) *UnDecorateSymbolName;
33 
34 namespace {
35 
36 class WinSymbolizerTool final : public SymbolizerTool {
37  public:
38   // The constructor is provided to avoid synthesized memsets.
39   WinSymbolizerTool() {}
40 
41   bool SymbolizePC(uptr addr, SymbolizedStack *stack) override;
42   bool SymbolizeData(uptr addr, DataInfo *info) override {
43     return false;
44   }
45   const char *Demangle(const char *name) override;
46 };
47 
48 bool is_dbghelp_initialized = false;
49 
50 bool TrySymInitialize() {
51   SymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_UNDNAME | SYMOPT_LOAD_LINES);
52   return SymInitialize(GetCurrentProcess(), 0, TRUE);
53   // FIXME: We don't call SymCleanup() on exit yet - should we?
54 }
55 
56 }  // namespace
57 
58 // Initializes DbgHelp library, if it's not yet initialized. Calls to this
59 // function should be synchronized with respect to other calls to DbgHelp API
60 // (e.g. from WinSymbolizerTool).
61 void InitializeDbgHelpIfNeeded() {
62   if (is_dbghelp_initialized)
63     return;
64 
65   HMODULE dbghelp = LoadLibraryA("dbghelp.dll");
66   CHECK(dbghelp && "failed to load dbghelp.dll");
67 
68 #define DBGHELP_IMPORT(name)                                                  \
69   do {                                                                        \
70     name =                                                                    \
71         reinterpret_cast<decltype(::name) *>(GetProcAddress(dbghelp, #name)); \
72     CHECK(name != nullptr);                                                   \
73   } while (0)
74   DBGHELP_IMPORT(StackWalk64);
75   DBGHELP_IMPORT(SymCleanup);
76   DBGHELP_IMPORT(SymFromAddr);
77   DBGHELP_IMPORT(SymFunctionTableAccess64);
78   DBGHELP_IMPORT(SymGetLineFromAddr64);
79   DBGHELP_IMPORT(SymGetModuleBase64);
80   DBGHELP_IMPORT(SymGetSearchPathW);
81   DBGHELP_IMPORT(SymInitialize);
82   DBGHELP_IMPORT(SymSetOptions);
83   DBGHELP_IMPORT(SymSetSearchPathW);
84   DBGHELP_IMPORT(UnDecorateSymbolName);
85 #undef DBGHELP_IMPORT
86 
87   if (!TrySymInitialize()) {
88     // OK, maybe the client app has called SymInitialize already.
89     // That's a bit unfortunate for us as all the DbgHelp functions are
90     // single-threaded and we can't coordinate with the app.
91     // FIXME: Can we stop the other threads at this point?
92     // Anyways, we have to reconfigure stuff to make sure that SymInitialize
93     // has all the appropriate options set.
94     // Cross our fingers and reinitialize DbgHelp.
95     Report("*** WARNING: Failed to initialize DbgHelp!              ***\n");
96     Report("*** Most likely this means that the app is already      ***\n");
97     Report("*** using DbgHelp, possibly with incompatible flags.    ***\n");
98     Report("*** Due to technical reasons, symbolization might crash ***\n");
99     Report("*** or produce wrong results.                           ***\n");
100     SymCleanup(GetCurrentProcess());
101     TrySymInitialize();
102   }
103   is_dbghelp_initialized = true;
104 
105   // When an executable is run from a location different from the one where it
106   // was originally built, we may not see the nearby PDB files.
107   // To work around this, let's append the directory of the main module
108   // to the symbol search path.  All the failures below are not fatal.
109   const size_t kSymPathSize = 2048;
110   static wchar_t path_buffer[kSymPathSize + 1 + MAX_PATH];
111   if (!SymGetSearchPathW(GetCurrentProcess(), path_buffer, kSymPathSize)) {
112     Report("*** WARNING: Failed to SymGetSearchPathW ***\n");
113     return;
114   }
115   size_t sz = wcslen(path_buffer);
116   if (sz) {
117     CHECK_EQ(0, wcscat_s(path_buffer, L";"));
118     sz++;
119   }
120   DWORD res = GetModuleFileNameW(NULL, path_buffer + sz, MAX_PATH);
121   if (res == 0 || res == MAX_PATH) {
122     Report("*** WARNING: Failed to getting the EXE directory ***\n");
123     return;
124   }
125   // Write the zero character in place of the last backslash to get the
126   // directory of the main module at the end of path_buffer.
127   wchar_t *last_bslash = wcsrchr(path_buffer + sz, L'\\');
128   CHECK_NE(last_bslash, 0);
129   *last_bslash = L'\0';
130   if (!SymSetSearchPathW(GetCurrentProcess(), path_buffer)) {
131     Report("*** WARNING: Failed to SymSetSearchPathW\n");
132     return;
133   }
134 }
135 
136 #ifdef __clang__
137 #pragma clang diagnostic push
138 #pragma clang diagnostic ignored "-Wframe-larger-than="
139 #endif
140 bool WinSymbolizerTool::SymbolizePC(uptr addr, SymbolizedStack *frame) {
141   InitializeDbgHelpIfNeeded();
142 
143   // See https://docs.microsoft.com/en-us/windows/win32/debug/retrieving-symbol-information-by-address
144   char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(CHAR)];
145   PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer;
146   symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
147   symbol->MaxNameLen = MAX_SYM_NAME;
148   DWORD64 offset = 0;
149   BOOL got_objname = SymFromAddr(GetCurrentProcess(),
150                                  (DWORD64)addr, &offset, symbol);
151   if (!got_objname)
152     return false;
153 
154   DWORD unused;
155   IMAGEHLP_LINE64 line_info;
156   line_info.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
157   BOOL got_fileline = SymGetLineFromAddr64(GetCurrentProcess(), (DWORD64)addr,
158                                            &unused, &line_info);
159   frame->info.function = internal_strdup(symbol->Name);
160   frame->info.function_offset = (uptr)offset;
161   if (got_fileline) {
162     frame->info.file = internal_strdup(line_info.FileName);
163     frame->info.line = line_info.LineNumber;
164   }
165   // Only consider this a successful symbolization attempt if we got file info.
166   // Otherwise, try llvm-symbolizer.
167   return got_fileline;
168 }
169 #ifdef __clang__
170 #pragma clang diagnostic pop
171 #endif
172 
173 const char *WinSymbolizerTool::Demangle(const char *name) {
174   CHECK(is_dbghelp_initialized);
175   static char demangle_buffer[1000];
176   if (name[0] == '\01' &&
177       UnDecorateSymbolName(name + 1, demangle_buffer, sizeof(demangle_buffer),
178                            UNDNAME_NAME_ONLY))
179     return demangle_buffer;
180   else
181     return name;
182 }
183 
184 const char *Symbolizer::PlatformDemangle(const char *name) {
185   return name;
186 }
187 
188 namespace {
189 struct ScopedHandle {
190   ScopedHandle() : h_(nullptr) {}
191   explicit ScopedHandle(HANDLE h) : h_(h) {}
192   ~ScopedHandle() {
193     if (h_)
194       ::CloseHandle(h_);
195   }
196   HANDLE get() { return h_; }
197   HANDLE *receive() { return &h_; }
198   HANDLE release() {
199     HANDLE h = h_;
200     h_ = nullptr;
201     return h;
202   }
203   HANDLE h_;
204 };
205 } // namespace
206 
207 bool SymbolizerProcess::StartSymbolizerSubprocess() {
208   // Create inherited pipes for stdin and stdout.
209   ScopedHandle stdin_read, stdin_write;
210   ScopedHandle stdout_read, stdout_write;
211   SECURITY_ATTRIBUTES attrs;
212   attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
213   attrs.bInheritHandle = TRUE;
214   attrs.lpSecurityDescriptor = nullptr;
215   if (!::CreatePipe(stdin_read.receive(), stdin_write.receive(), &attrs, 0) ||
216       !::CreatePipe(stdout_read.receive(), stdout_write.receive(), &attrs, 0)) {
217     VReport(2, "WARNING: %s CreatePipe failed (error code: %d)\n",
218             SanitizerToolName, path_, GetLastError());
219     return false;
220   }
221 
222   // Don't inherit the writing end of stdin or the reading end of stdout.
223   if (!SetHandleInformation(stdin_write.get(), HANDLE_FLAG_INHERIT, 0) ||
224       !SetHandleInformation(stdout_read.get(), HANDLE_FLAG_INHERIT, 0)) {
225     VReport(2, "WARNING: %s SetHandleInformation failed (error code: %d)\n",
226             SanitizerToolName, path_, GetLastError());
227     return false;
228   }
229 
230   // Compute the command line. Wrap double quotes around everything.
231   const char *argv[kArgVMax];
232   GetArgV(path_, argv);
233   InternalScopedString command_line(kMaxPathLength * 3);
234   for (int i = 0; argv[i]; i++) {
235     const char *arg = argv[i];
236     int arglen = internal_strlen(arg);
237     // Check that tool command lines are simple and that complete escaping is
238     // unnecessary.
239     CHECK(!internal_strchr(arg, '"') && "quotes in args unsupported");
240     CHECK(!internal_strstr(arg, "\\\\") &&
241           "double backslashes in args unsupported");
242     CHECK(arglen > 0 && arg[arglen - 1] != '\\' &&
243           "args ending in backslash and empty args unsupported");
244     command_line.append("\"%s\" ", arg);
245   }
246   VReport(3, "Launching symbolizer command: %s\n", command_line.data());
247 
248   // Launch llvm-symbolizer with stdin and stdout redirected.
249   STARTUPINFOA si;
250   memset(&si, 0, sizeof(si));
251   si.cb = sizeof(si);
252   si.dwFlags |= STARTF_USESTDHANDLES;
253   si.hStdInput = stdin_read.get();
254   si.hStdOutput = stdout_write.get();
255   PROCESS_INFORMATION pi;
256   memset(&pi, 0, sizeof(pi));
257   if (!CreateProcessA(path_,               // Executable
258                       command_line.data(), // Command line
259                       nullptr,             // Process handle not inheritable
260                       nullptr,             // Thread handle not inheritable
261                       TRUE,                // Set handle inheritance to TRUE
262                       0,                   // Creation flags
263                       nullptr,             // Use parent's environment block
264                       nullptr,             // Use parent's starting directory
265                       &si, &pi)) {
266     VReport(2, "WARNING: %s failed to create process for %s (error code: %d)\n",
267             SanitizerToolName, path_, GetLastError());
268     return false;
269   }
270 
271   // Process creation succeeded, so transfer handle ownership into the fields.
272   input_fd_ = stdout_read.release();
273   output_fd_ = stdin_write.release();
274 
275   // The llvm-symbolizer process is responsible for quitting itself when the
276   // stdin pipe is closed, so we don't need these handles. Close them to prevent
277   // leaks. If we ever want to try to kill the symbolizer process from the
278   // parent, we'll want to hang on to these handles.
279   CloseHandle(pi.hProcess);
280   CloseHandle(pi.hThread);
281   return true;
282 }
283 
284 static void ChooseSymbolizerTools(IntrusiveList<SymbolizerTool> *list,
285                                   LowLevelAllocator *allocator) {
286   if (!common_flags()->symbolize) {
287     VReport(2, "Symbolizer is disabled.\n");
288     return;
289   }
290 
291   // Add llvm-symbolizer in case the binary has dwarf.
292   const char *user_path = common_flags()->external_symbolizer_path;
293   const char *path =
294       user_path ? user_path : FindPathToBinary("llvm-symbolizer.exe");
295   if (path) {
296     VReport(2, "Using llvm-symbolizer at %spath: %s\n",
297             user_path ? "user-specified " : "", path);
298     list->push_back(new(*allocator) LLVMSymbolizer(path, allocator));
299   } else {
300     if (user_path && user_path[0] == '\0') {
301       VReport(2, "External symbolizer is explicitly disabled.\n");
302     } else {
303       VReport(2, "External symbolizer is not present.\n");
304     }
305   }
306 
307   // Add the dbghelp based symbolizer.
308   list->push_back(new(*allocator) WinSymbolizerTool());
309 }
310 
311 Symbolizer *Symbolizer::PlatformInit() {
312   IntrusiveList<SymbolizerTool> list;
313   list.clear();
314   ChooseSymbolizerTools(&list, &symbolizer_allocator_);
315 
316   return new(symbolizer_allocator_) Symbolizer(list);
317 }
318 
319 void Symbolizer::LateInitialize() {
320   Symbolizer::GetOrInit()->LateInitializeTools();
321 }
322 
323 }  // namespace __sanitizer
324 
325 #endif  // _WIN32
326