1353944Sdim//===-- sanitizer_symbolizer_win.cpp --------------------------------------===//
2353944Sdim//
3353944Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353944Sdim// See https://llvm.org/LICENSE.txt for license information.
5353944Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6353944Sdim//
7353944Sdim//===----------------------------------------------------------------------===//
8353944Sdim//
9353944Sdim// This file is shared between AddressSanitizer and ThreadSanitizer
10353944Sdim// run-time libraries.
11353944Sdim// Windows-specific implementation of symbolizer parts.
12353944Sdim//===----------------------------------------------------------------------===//
13353944Sdim
14353944Sdim#include "sanitizer_platform.h"
15353944Sdim#if SANITIZER_WINDOWS
16353944Sdim
17353944Sdim#include "sanitizer_dbghelp.h"
18353944Sdim#include "sanitizer_symbolizer_internal.h"
19353944Sdim
20353944Sdimnamespace __sanitizer {
21353944Sdim
22353944Sdimdecltype(::StackWalk64) *StackWalk64;
23353944Sdimdecltype(::SymCleanup) *SymCleanup;
24353944Sdimdecltype(::SymFromAddr) *SymFromAddr;
25353944Sdimdecltype(::SymFunctionTableAccess64) *SymFunctionTableAccess64;
26353944Sdimdecltype(::SymGetLineFromAddr64) *SymGetLineFromAddr64;
27353944Sdimdecltype(::SymGetModuleBase64) *SymGetModuleBase64;
28353944Sdimdecltype(::SymGetSearchPathW) *SymGetSearchPathW;
29353944Sdimdecltype(::SymInitialize) *SymInitialize;
30353944Sdimdecltype(::SymSetOptions) *SymSetOptions;
31353944Sdimdecltype(::SymSetSearchPathW) *SymSetSearchPathW;
32353944Sdimdecltype(::UnDecorateSymbolName) *UnDecorateSymbolName;
33353944Sdim
34353944Sdimnamespace {
35353944Sdim
36353944Sdimclass WinSymbolizerTool : public SymbolizerTool {
37353944Sdim public:
38353944Sdim  // The constructor is provided to avoid synthesized memsets.
39353944Sdim  WinSymbolizerTool() {}
40353944Sdim
41353944Sdim  bool SymbolizePC(uptr addr, SymbolizedStack *stack) override;
42353944Sdim  bool SymbolizeData(uptr addr, DataInfo *info) override {
43353944Sdim    return false;
44353944Sdim  }
45353944Sdim  const char *Demangle(const char *name) override;
46353944Sdim};
47353944Sdim
48353944Sdimbool is_dbghelp_initialized = false;
49353944Sdim
50353944Sdimbool TrySymInitialize() {
51353944Sdim  SymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_UNDNAME | SYMOPT_LOAD_LINES);
52353944Sdim  return SymInitialize(GetCurrentProcess(), 0, TRUE);
53353944Sdim  // FIXME: We don't call SymCleanup() on exit yet - should we?
54353944Sdim}
55353944Sdim
56353944Sdim}  // namespace
57353944Sdim
58353944Sdim// Initializes DbgHelp library, if it's not yet initialized. Calls to this
59353944Sdim// function should be synchronized with respect to other calls to DbgHelp API
60353944Sdim// (e.g. from WinSymbolizerTool).
61353944Sdimvoid InitializeDbgHelpIfNeeded() {
62353944Sdim  if (is_dbghelp_initialized)
63353944Sdim    return;
64353944Sdim
65353944Sdim  HMODULE dbghelp = LoadLibraryA("dbghelp.dll");
66353944Sdim  CHECK(dbghelp && "failed to load dbghelp.dll");
67353944Sdim
68353944Sdim#define DBGHELP_IMPORT(name)                                                  \
69353944Sdim  do {                                                                        \
70353944Sdim    name =                                                                    \
71353944Sdim        reinterpret_cast<decltype(::name) *>(GetProcAddress(dbghelp, #name)); \
72353944Sdim    CHECK(name != nullptr);                                                   \
73353944Sdim  } while (0)
74353944Sdim  DBGHELP_IMPORT(StackWalk64);
75353944Sdim  DBGHELP_IMPORT(SymCleanup);
76353944Sdim  DBGHELP_IMPORT(SymFromAddr);
77353944Sdim  DBGHELP_IMPORT(SymFunctionTableAccess64);
78353944Sdim  DBGHELP_IMPORT(SymGetLineFromAddr64);
79353944Sdim  DBGHELP_IMPORT(SymGetModuleBase64);
80353944Sdim  DBGHELP_IMPORT(SymGetSearchPathW);
81353944Sdim  DBGHELP_IMPORT(SymInitialize);
82353944Sdim  DBGHELP_IMPORT(SymSetOptions);
83353944Sdim  DBGHELP_IMPORT(SymSetSearchPathW);
84353944Sdim  DBGHELP_IMPORT(UnDecorateSymbolName);
85353944Sdim#undef DBGHELP_IMPORT
86353944Sdim
87353944Sdim  if (!TrySymInitialize()) {
88353944Sdim    // OK, maybe the client app has called SymInitialize already.
89353944Sdim    // That's a bit unfortunate for us as all the DbgHelp functions are
90353944Sdim    // single-threaded and we can't coordinate with the app.
91353944Sdim    // FIXME: Can we stop the other threads at this point?
92353944Sdim    // Anyways, we have to reconfigure stuff to make sure that SymInitialize
93353944Sdim    // has all the appropriate options set.
94353944Sdim    // Cross our fingers and reinitialize DbgHelp.
95353944Sdim    Report("*** WARNING: Failed to initialize DbgHelp!              ***\n");
96353944Sdim    Report("*** Most likely this means that the app is already      ***\n");
97353944Sdim    Report("*** using DbgHelp, possibly with incompatible flags.    ***\n");
98353944Sdim    Report("*** Due to technical reasons, symbolization might crash ***\n");
99353944Sdim    Report("*** or produce wrong results.                           ***\n");
100353944Sdim    SymCleanup(GetCurrentProcess());
101353944Sdim    TrySymInitialize();
102353944Sdim  }
103353944Sdim  is_dbghelp_initialized = true;
104353944Sdim
105353944Sdim  // When an executable is run from a location different from the one where it
106353944Sdim  // was originally built, we may not see the nearby PDB files.
107353944Sdim  // To work around this, let's append the directory of the main module
108353944Sdim  // to the symbol search path.  All the failures below are not fatal.
109353944Sdim  const size_t kSymPathSize = 2048;
110353944Sdim  static wchar_t path_buffer[kSymPathSize + 1 + MAX_PATH];
111353944Sdim  if (!SymGetSearchPathW(GetCurrentProcess(), path_buffer, kSymPathSize)) {
112353944Sdim    Report("*** WARNING: Failed to SymGetSearchPathW ***\n");
113353944Sdim    return;
114353944Sdim  }
115353944Sdim  size_t sz = wcslen(path_buffer);
116353944Sdim  if (sz) {
117353944Sdim    CHECK_EQ(0, wcscat_s(path_buffer, L";"));
118353944Sdim    sz++;
119353944Sdim  }
120353944Sdim  DWORD res = GetModuleFileNameW(NULL, path_buffer + sz, MAX_PATH);
121353944Sdim  if (res == 0 || res == MAX_PATH) {
122353944Sdim    Report("*** WARNING: Failed to getting the EXE directory ***\n");
123353944Sdim    return;
124353944Sdim  }
125353944Sdim  // Write the zero character in place of the last backslash to get the
126353944Sdim  // directory of the main module at the end of path_buffer.
127353944Sdim  wchar_t *last_bslash = wcsrchr(path_buffer + sz, L'\\');
128353944Sdim  CHECK_NE(last_bslash, 0);
129353944Sdim  *last_bslash = L'\0';
130353944Sdim  if (!SymSetSearchPathW(GetCurrentProcess(), path_buffer)) {
131353944Sdim    Report("*** WARNING: Failed to SymSetSearchPathW\n");
132353944Sdim    return;
133353944Sdim  }
134353944Sdim}
135353944Sdim
136353944Sdimbool WinSymbolizerTool::SymbolizePC(uptr addr, SymbolizedStack *frame) {
137353944Sdim  InitializeDbgHelpIfNeeded();
138353944Sdim
139353944Sdim  // See http://msdn.microsoft.com/en-us/library/ms680578(VS.85).aspx
140353944Sdim  char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(CHAR)];
141353944Sdim  PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer;
142353944Sdim  symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
143353944Sdim  symbol->MaxNameLen = MAX_SYM_NAME;
144353944Sdim  DWORD64 offset = 0;
145353944Sdim  BOOL got_objname = SymFromAddr(GetCurrentProcess(),
146353944Sdim                                 (DWORD64)addr, &offset, symbol);
147353944Sdim  if (!got_objname)
148353944Sdim    return false;
149353944Sdim
150353944Sdim  DWORD unused;
151353944Sdim  IMAGEHLP_LINE64 line_info;
152353944Sdim  line_info.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
153353944Sdim  BOOL got_fileline = SymGetLineFromAddr64(GetCurrentProcess(), (DWORD64)addr,
154353944Sdim                                           &unused, &line_info);
155353944Sdim  frame->info.function = internal_strdup(symbol->Name);
156353944Sdim  frame->info.function_offset = (uptr)offset;
157353944Sdim  if (got_fileline) {
158353944Sdim    frame->info.file = internal_strdup(line_info.FileName);
159353944Sdim    frame->info.line = line_info.LineNumber;
160353944Sdim  }
161353944Sdim  // Only consider this a successful symbolization attempt if we got file info.
162353944Sdim  // Otherwise, try llvm-symbolizer.
163353944Sdim  return got_fileline;
164353944Sdim}
165353944Sdim
166353944Sdimconst char *WinSymbolizerTool::Demangle(const char *name) {
167353944Sdim  CHECK(is_dbghelp_initialized);
168353944Sdim  static char demangle_buffer[1000];
169353944Sdim  if (name[0] == '\01' &&
170353944Sdim      UnDecorateSymbolName(name + 1, demangle_buffer, sizeof(demangle_buffer),
171353944Sdim                           UNDNAME_NAME_ONLY))
172353944Sdim    return demangle_buffer;
173353944Sdim  else
174353944Sdim    return name;
175353944Sdim}
176353944Sdim
177353944Sdimconst char *Symbolizer::PlatformDemangle(const char *name) {
178353944Sdim  return name;
179353944Sdim}
180353944Sdim
181353944Sdimnamespace {
182353944Sdimstruct ScopedHandle {
183353944Sdim  ScopedHandle() : h_(nullptr) {}
184353944Sdim  explicit ScopedHandle(HANDLE h) : h_(h) {}
185353944Sdim  ~ScopedHandle() {
186353944Sdim    if (h_)
187353944Sdim      ::CloseHandle(h_);
188353944Sdim  }
189353944Sdim  HANDLE get() { return h_; }
190353944Sdim  HANDLE *receive() { return &h_; }
191353944Sdim  HANDLE release() {
192353944Sdim    HANDLE h = h_;
193353944Sdim    h_ = nullptr;
194353944Sdim    return h;
195353944Sdim  }
196353944Sdim  HANDLE h_;
197353944Sdim};
198353944Sdim} // namespace
199353944Sdim
200353944Sdimbool SymbolizerProcess::StartSymbolizerSubprocess() {
201353944Sdim  // Create inherited pipes for stdin and stdout.
202353944Sdim  ScopedHandle stdin_read, stdin_write;
203353944Sdim  ScopedHandle stdout_read, stdout_write;
204353944Sdim  SECURITY_ATTRIBUTES attrs;
205353944Sdim  attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
206353944Sdim  attrs.bInheritHandle = TRUE;
207353944Sdim  attrs.lpSecurityDescriptor = nullptr;
208353944Sdim  if (!::CreatePipe(stdin_read.receive(), stdin_write.receive(), &attrs, 0) ||
209353944Sdim      !::CreatePipe(stdout_read.receive(), stdout_write.receive(), &attrs, 0)) {
210353944Sdim    VReport(2, "WARNING: %s CreatePipe failed (error code: %d)\n",
211353944Sdim            SanitizerToolName, path_, GetLastError());
212353944Sdim    return false;
213353944Sdim  }
214353944Sdim
215353944Sdim  // Don't inherit the writing end of stdin or the reading end of stdout.
216353944Sdim  if (!SetHandleInformation(stdin_write.get(), HANDLE_FLAG_INHERIT, 0) ||
217353944Sdim      !SetHandleInformation(stdout_read.get(), HANDLE_FLAG_INHERIT, 0)) {
218353944Sdim    VReport(2, "WARNING: %s SetHandleInformation failed (error code: %d)\n",
219353944Sdim            SanitizerToolName, path_, GetLastError());
220353944Sdim    return false;
221353944Sdim  }
222353944Sdim
223353944Sdim  // Compute the command line. Wrap double quotes around everything.
224353944Sdim  const char *argv[kArgVMax];
225353944Sdim  GetArgV(path_, argv);
226353944Sdim  InternalScopedString command_line(kMaxPathLength * 3);
227353944Sdim  for (int i = 0; argv[i]; i++) {
228353944Sdim    const char *arg = argv[i];
229353944Sdim    int arglen = internal_strlen(arg);
230353944Sdim    // Check that tool command lines are simple and that complete escaping is
231353944Sdim    // unnecessary.
232353944Sdim    CHECK(!internal_strchr(arg, '"') && "quotes in args unsupported");
233353944Sdim    CHECK(!internal_strstr(arg, "\\\\") &&
234353944Sdim          "double backslashes in args unsupported");
235353944Sdim    CHECK(arglen > 0 && arg[arglen - 1] != '\\' &&
236353944Sdim          "args ending in backslash and empty args unsupported");
237353944Sdim    command_line.append("\"%s\" ", arg);
238353944Sdim  }
239353944Sdim  VReport(3, "Launching symbolizer command: %s\n", command_line.data());
240353944Sdim
241353944Sdim  // Launch llvm-symbolizer with stdin and stdout redirected.
242353944Sdim  STARTUPINFOA si;
243353944Sdim  memset(&si, 0, sizeof(si));
244353944Sdim  si.cb = sizeof(si);
245353944Sdim  si.dwFlags |= STARTF_USESTDHANDLES;
246353944Sdim  si.hStdInput = stdin_read.get();
247353944Sdim  si.hStdOutput = stdout_write.get();
248353944Sdim  PROCESS_INFORMATION pi;
249353944Sdim  memset(&pi, 0, sizeof(pi));
250353944Sdim  if (!CreateProcessA(path_,               // Executable
251353944Sdim                      command_line.data(), // Command line
252353944Sdim                      nullptr,             // Process handle not inheritable
253353944Sdim                      nullptr,             // Thread handle not inheritable
254353944Sdim                      TRUE,                // Set handle inheritance to TRUE
255353944Sdim                      0,                   // Creation flags
256353944Sdim                      nullptr,             // Use parent's environment block
257353944Sdim                      nullptr,             // Use parent's starting directory
258353944Sdim                      &si, &pi)) {
259353944Sdim    VReport(2, "WARNING: %s failed to create process for %s (error code: %d)\n",
260353944Sdim            SanitizerToolName, path_, GetLastError());
261353944Sdim    return false;
262353944Sdim  }
263353944Sdim
264353944Sdim  // Process creation succeeded, so transfer handle ownership into the fields.
265353944Sdim  input_fd_ = stdout_read.release();
266353944Sdim  output_fd_ = stdin_write.release();
267353944Sdim
268353944Sdim  // The llvm-symbolizer process is responsible for quitting itself when the
269353944Sdim  // stdin pipe is closed, so we don't need these handles. Close them to prevent
270353944Sdim  // leaks. If we ever want to try to kill the symbolizer process from the
271353944Sdim  // parent, we'll want to hang on to these handles.
272353944Sdim  CloseHandle(pi.hProcess);
273353944Sdim  CloseHandle(pi.hThread);
274353944Sdim  return true;
275353944Sdim}
276353944Sdim
277353944Sdimstatic void ChooseSymbolizerTools(IntrusiveList<SymbolizerTool> *list,
278353944Sdim                                  LowLevelAllocator *allocator) {
279353944Sdim  if (!common_flags()->symbolize) {
280353944Sdim    VReport(2, "Symbolizer is disabled.\n");
281353944Sdim    return;
282353944Sdim  }
283353944Sdim
284353944Sdim  // Add llvm-symbolizer in case the binary has dwarf.
285353944Sdim  const char *user_path = common_flags()->external_symbolizer_path;
286353944Sdim  const char *path =
287353944Sdim      user_path ? user_path : FindPathToBinary("llvm-symbolizer.exe");
288353944Sdim  if (path) {
289353944Sdim    VReport(2, "Using llvm-symbolizer at %spath: %s\n",
290353944Sdim            user_path ? "user-specified " : "", path);
291353944Sdim    list->push_back(new(*allocator) LLVMSymbolizer(path, allocator));
292353944Sdim  } else {
293353944Sdim    if (user_path && user_path[0] == '\0') {
294353944Sdim      VReport(2, "External symbolizer is explicitly disabled.\n");
295353944Sdim    } else {
296353944Sdim      VReport(2, "External symbolizer is not present.\n");
297353944Sdim    }
298353944Sdim  }
299353944Sdim
300353944Sdim  // Add the dbghelp based symbolizer.
301353944Sdim  list->push_back(new(*allocator) WinSymbolizerTool());
302353944Sdim}
303353944Sdim
304353944SdimSymbolizer *Symbolizer::PlatformInit() {
305353944Sdim  IntrusiveList<SymbolizerTool> list;
306353944Sdim  list.clear();
307353944Sdim  ChooseSymbolizerTools(&list, &symbolizer_allocator_);
308353944Sdim
309353944Sdim  return new(symbolizer_allocator_) Symbolizer(list);
310353944Sdim}
311353944Sdim
312353944Sdimvoid Symbolizer::LateInitialize() {
313353944Sdim  Symbolizer::GetOrInit();
314353944Sdim}
315353944Sdim
316353944Sdim}  // namespace __sanitizer
317353944Sdim
318353944Sdim#endif  // _WIN32
319