annotate libsanitizer/sanitizer_common/sanitizer_symbolizer_win.cc @ 144:8f4e72ab4e11

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