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