Bug Summary

File:projects/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cc
Warning:line 327, column 3
Value stored to 'str' is never read

Annotated Source Code

1//===-- sanitizer_symbolizer_libcdep.cc -----------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is shared between AddressSanitizer and ThreadSanitizer
11// run-time libraries.
12//===----------------------------------------------------------------------===//
13
14#include "sanitizer_allocator_internal.h"
15#include "sanitizer_internal_defs.h"
16#include "sanitizer_symbolizer_internal.h"
17
18namespace __sanitizer {
19
20const char *ExtractToken(const char *str, const char *delims, char **result) {
21 uptr prefix_len = internal_strcspn(str, delims);
22 *result = (char*)InternalAlloc(prefix_len + 1);
23 internal_memcpy(*result, str, prefix_len);
24 (*result)[prefix_len] = '\0';
25 const char *prefix_end = str + prefix_len;
26 if (*prefix_end != '\0') prefix_end++;
27 return prefix_end;
28}
29
30const char *ExtractInt(const char *str, const char *delims, int *result) {
31 char *buff;
32 const char *ret = ExtractToken(str, delims, &buff);
33 if (buff != 0) {
34 *result = (int)internal_atoll(buff);
35 }
36 InternalFree(buff);
37 return ret;
38}
39
40const char *ExtractUptr(const char *str, const char *delims, uptr *result) {
41 char *buff;
42 const char *ret = ExtractToken(str, delims, &buff);
43 if (buff != 0) {
44 *result = (uptr)internal_atoll(buff);
45 }
46 InternalFree(buff);
47 return ret;
48}
49
50const char *ExtractTokenUpToDelimiter(const char *str, const char *delimiter,
51 char **result) {
52 const char *found_delimiter = internal_strstr(str, delimiter);
53 uptr prefix_len =
54 found_delimiter ? found_delimiter - str : internal_strlen(str);
55 *result = (char *)InternalAlloc(prefix_len + 1);
56 internal_memcpy(*result, str, prefix_len);
57 (*result)[prefix_len] = '\0';
58 const char *prefix_end = str + prefix_len;
59 if (*prefix_end != '\0') prefix_end += internal_strlen(delimiter);
60 return prefix_end;
61}
62
63SymbolizedStack *Symbolizer::SymbolizePC(uptr addr) {
64 BlockingMutexLock l(&mu_);
65 const char *module_name;
66 uptr module_offset;
67 ModuleArch arch;
68 SymbolizedStack *res = SymbolizedStack::New(addr);
69 if (!FindModuleNameAndOffsetForAddress(addr, &module_name, &module_offset,
70 &arch))
71 return res;
72 // Always fill data about module name and offset.
73 res->info.FillModuleInfo(module_name, module_offset, arch);
74 for (auto &tool : tools_) {
75 SymbolizerScope sym_scope(this);
76 if (tool.SymbolizePC(addr, res)) {
77 return res;
78 }
79 }
80 return res;
81}
82
83bool Symbolizer::SymbolizeData(uptr addr, DataInfo *info) {
84 BlockingMutexLock l(&mu_);
85 const char *module_name;
86 uptr module_offset;
87 ModuleArch arch;
88 if (!FindModuleNameAndOffsetForAddress(addr, &module_name, &module_offset,
89 &arch))
90 return false;
91 info->Clear();
92 info->module = internal_strdup(module_name);
93 info->module_offset = module_offset;
94 info->module_arch = arch;
95 for (auto &tool : tools_) {
96 SymbolizerScope sym_scope(this);
97 if (tool.SymbolizeData(addr, info)) {
98 return true;
99 }
100 }
101 return true;
102}
103
104bool Symbolizer::GetModuleNameAndOffsetForPC(uptr pc, const char **module_name,
105 uptr *module_address) {
106 BlockingMutexLock l(&mu_);
107 const char *internal_module_name = nullptr;
108 ModuleArch arch;
109 if (!FindModuleNameAndOffsetForAddress(pc, &internal_module_name,
110 module_address, &arch))
111 return false;
112
113 if (module_name)
114 *module_name = module_names_.GetOwnedCopy(internal_module_name);
115 return true;
116}
117
118void Symbolizer::Flush() {
119 BlockingMutexLock l(&mu_);
120 for (auto &tool : tools_) {
121 SymbolizerScope sym_scope(this);
122 tool.Flush();
123 }
124}
125
126const char *Symbolizer::Demangle(const char *name) {
127 BlockingMutexLock l(&mu_);
128 for (auto &tool : tools_) {
129 SymbolizerScope sym_scope(this);
130 if (const char *demangled = tool.Demangle(name))
131 return demangled;
132 }
133 return PlatformDemangle(name);
134}
135
136void Symbolizer::PrepareForSandboxing() {
137 BlockingMutexLock l(&mu_);
138 PlatformPrepareForSandboxing();
139}
140
141bool Symbolizer::FindModuleNameAndOffsetForAddress(uptr address,
142 const char **module_name,
143 uptr *module_offset,
144 ModuleArch *module_arch) {
145 const LoadedModule *module = FindModuleForAddress(address);
146 if (module == nullptr)
147 return false;
148 *module_name = module->full_name();
149 *module_offset = address - module->base_address();
150 *module_arch = module->arch();
151 return true;
152}
153
154const LoadedModule *Symbolizer::FindModuleForAddress(uptr address) {
155 bool modules_were_reloaded = false;
156 if (!modules_fresh_) {
157 modules_.init();
158 RAW_CHECK(modules_.size() > 0)do { if (__builtin_expect(!!(!(modules_.size() > 0)), 0)) {
RawWrite("modules_.size() > 0"); Die(); } } while (0)
;
159 modules_fresh_ = true;
160 modules_were_reloaded = true;
161 }
162 for (uptr i = 0; i < modules_.size(); i++) {
163 if (modules_[i].containsAddress(address)) {
164 return &modules_[i];
165 }
166 }
167 // Reload the modules and look up again, if we haven't tried it yet.
168 if (!modules_were_reloaded) {
169 // FIXME: set modules_fresh_ from dlopen()/dlclose() interceptors.
170 // It's too aggressive to reload the list of modules each time we fail
171 // to find a module for a given address.
172 modules_fresh_ = false;
173 return FindModuleForAddress(address);
174 }
175 return 0;
176}
177
178Symbolizer *Symbolizer::GetOrInit() {
179 SpinMutexLock l(&init_mu_);
180 if (symbolizer_)
181 return symbolizer_;
182 symbolizer_ = PlatformInit();
183 CHECK(symbolizer_)do { __sanitizer::u64 v1 = (u64)((symbolizer_)); __sanitizer::
u64 v2 = (u64)(0); if (__builtin_expect(!!(!(v1 != v2)), 0)) __sanitizer
::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cc"
, 183, "(" "(symbolizer_)" ") " "!=" " (" "0" ")", v1, v2); }
while (false)
;
184 return symbolizer_;
185}
186
187// For now we assume the following protocol:
188// For each request of the form
189// <module_name> <module_offset>
190// passed to STDIN, external symbolizer prints to STDOUT response:
191// <function_name>
192// <file_name>:<line_number>:<column_number>
193// <function_name>
194// <file_name>:<line_number>:<column_number>
195// ...
196// <empty line>
197class LLVMSymbolizerProcess : public SymbolizerProcess {
198 public:
199 explicit LLVMSymbolizerProcess(const char *path) : SymbolizerProcess(path) {}
200
201 private:
202 bool ReachedEndOfOutput(const char *buffer, uptr length) const override {
203 // Empty line marks the end of llvm-symbolizer output.
204 return length >= 2 && buffer[length - 1] == '\n' &&
205 buffer[length - 2] == '\n';
206 }
207
208 // When adding a new architecture, don't forget to also update
209 // script/asan_symbolize.py and sanitizer_common.h.
210 void GetArgV(const char *path_to_binary,
211 const char *(&argv)[kArgVMax]) const override {
212#if defined(__x86_64h__)
213 const char* const kSymbolizerArch = "--default-arch=x86_64h";
214#elif defined(__x86_64__1)
215 const char* const kSymbolizerArch = "--default-arch=x86_64";
216#elif defined(__i386__)
217 const char* const kSymbolizerArch = "--default-arch=i386";
218#elif defined(__aarch64__)
219 const char* const kSymbolizerArch = "--default-arch=arm64";
220#elif defined(__arm__)
221 const char* const kSymbolizerArch = "--default-arch=arm";
222#elif defined(__powerpc64__) && __BYTE_ORDER__1234 == __ORDER_BIG_ENDIAN__4321
223 const char* const kSymbolizerArch = "--default-arch=powerpc64";
224#elif defined(__powerpc64__) && __BYTE_ORDER__1234 == __ORDER_LITTLE_ENDIAN__1234
225 const char* const kSymbolizerArch = "--default-arch=powerpc64le";
226#elif defined(__s390x__)
227 const char* const kSymbolizerArch = "--default-arch=s390x";
228#elif defined(__s390__)
229 const char* const kSymbolizerArch = "--default-arch=s390";
230#else
231 const char* const kSymbolizerArch = "--default-arch=unknown";
232#endif
233
234 const char *const inline_flag = common_flags()->symbolize_inline_frames
235 ? "--inlining=true"
236 : "--inlining=false";
237 int i = 0;
238 argv[i++] = path_to_binary;
239 argv[i++] = inline_flag;
240 argv[i++] = kSymbolizerArch;
241 argv[i++] = nullptr;
242 }
243};
244
245LLVMSymbolizer::LLVMSymbolizer(const char *path, LowLevelAllocator *allocator)
246 : symbolizer_process_(new(*allocator) LLVMSymbolizerProcess(path)) {}
247
248// Parse a <file>:<line>[:<column>] buffer. The file path may contain colons on
249// Windows, so extract tokens from the right hand side first. The column info is
250// also optional.
251static const char *ParseFileLineInfo(AddressInfo *info, const char *str) {
252 char *file_line_info = 0;
253 str = ExtractToken(str, "\n", &file_line_info);
254 CHECK(file_line_info)do { __sanitizer::u64 v1 = (u64)((file_line_info)); __sanitizer
::u64 v2 = (u64)(0); if (__builtin_expect(!!(!(v1 != v2)), 0)
) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cc"
, 254, "(" "(file_line_info)" ") " "!=" " (" "0" ")", v1, v2)
; } while (false)
;
255
256 if (uptr size = internal_strlen(file_line_info)) {
257 char *back = file_line_info + size - 1;
258 for (int i = 0; i < 2; ++i) {
259 while (back > file_line_info && IsDigit(*back)) --back;
260 if (*back != ':' || !IsDigit(back[1])) break;
261 info->column = info->line;
262 info->line = internal_atoll(back + 1);
263 // Truncate the string at the colon to keep only filename.
264 *back = '\0';
265 --back;
266 }
267 ExtractToken(file_line_info, "", &info->file);
268 }
269
270 InternalFree(file_line_info);
271 return str;
272}
273
274// Parses one or more two-line strings in the following format:
275// <function_name>
276// <file_name>:<line_number>[:<column_number>]
277// Used by LLVMSymbolizer, Addr2LinePool and InternalSymbolizer, since all of
278// them use the same output format.
279void ParseSymbolizePCOutput(const char *str, SymbolizedStack *res) {
280 bool top_frame = true;
281 SymbolizedStack *last = res;
282 while (true) {
283 char *function_name = 0;
284 str = ExtractToken(str, "\n", &function_name);
285 CHECK(function_name)do { __sanitizer::u64 v1 = (u64)((function_name)); __sanitizer
::u64 v2 = (u64)(0); if (__builtin_expect(!!(!(v1 != v2)), 0)
) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cc"
, 285, "(" "(function_name)" ") " "!=" " (" "0" ")", v1, v2);
} while (false)
;
286 if (function_name[0] == '\0') {
287 // There are no more frames.
288 InternalFree(function_name);
289 break;
290 }
291 SymbolizedStack *cur;
292 if (top_frame) {
293 cur = res;
294 top_frame = false;
295 } else {
296 cur = SymbolizedStack::New(res->info.address);
297 cur->info.FillModuleInfo(res->info.module, res->info.module_offset,
298 res->info.module_arch);
299 last->next = cur;
300 last = cur;
301 }
302
303 AddressInfo *info = &cur->info;
304 info->function = function_name;
305 str = ParseFileLineInfo(info, str);
306
307 // Functions and filenames can be "??", in which case we write 0
308 // to address info to mark that names are unknown.
309 if (0 == internal_strcmp(info->function, "??")) {
310 InternalFree(info->function);
311 info->function = 0;
312 }
313 if (0 == internal_strcmp(info->file, "??")) {
314 InternalFree(info->file);
315 info->file = 0;
316 }
317 }
318}
319
320// Parses a two-line string in the following format:
321// <symbol_name>
322// <start_address> <size>
323// Used by LLVMSymbolizer and InternalSymbolizer.
324void ParseSymbolizeDataOutput(const char *str, DataInfo *info) {
325 str = ExtractToken(str, "\n", &info->name);
326 str = ExtractUptr(str, " ", &info->start);
327 str = ExtractUptr(str, "\n", &info->size);
Value stored to 'str' is never read
328}
329
330bool LLVMSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
331 AddressInfo *info = &stack->info;
332 const char *buf = FormatAndSendCommand(
333 /*is_data*/ false, info->module, info->module_offset, info->module_arch);
334 if (buf) {
335 ParseSymbolizePCOutput(buf, stack);
336 return true;
337 }
338 return false;
339}
340
341bool LLVMSymbolizer::SymbolizeData(uptr addr, DataInfo *info) {
342 const char *buf = FormatAndSendCommand(
343 /*is_data*/ true, info->module, info->module_offset, info->module_arch);
344 if (buf) {
345 ParseSymbolizeDataOutput(buf, info);
346 info->start += (addr - info->module_offset); // Add the base address.
347 return true;
348 }
349 return false;
350}
351
352const char *LLVMSymbolizer::FormatAndSendCommand(bool is_data,
353 const char *module_name,
354 uptr module_offset,
355 ModuleArch arch) {
356 CHECK(module_name)do { __sanitizer::u64 v1 = (u64)((module_name)); __sanitizer::
u64 v2 = (u64)(0); if (__builtin_expect(!!(!(v1 != v2)), 0)) __sanitizer
::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cc"
, 356, "(" "(module_name)" ") " "!=" " (" "0" ")", v1, v2); }
while (false)
;
357 const char *is_data_str = is_data ? "DATA " : "";
358 if (arch == kModuleArchUnknown) {
359 if (internal_snprintf(buffer_, kBufferSize, "%s\"%s\" 0x%zx\n", is_data_str,
360 module_name,
361 module_offset) >= static_cast<int>(kBufferSize)) {
362 Report("WARNING: Command buffer too small");
363 return nullptr;
364 }
365 } else {
366 if (internal_snprintf(buffer_, kBufferSize, "%s\"%s:%s\" 0x%zx\n",
367 is_data_str, module_name, ModuleArchToString(arch),
368 module_offset) >= static_cast<int>(kBufferSize)) {
369 Report("WARNING: Command buffer too small");
370 return nullptr;
371 }
372 }
373 return symbolizer_process_->SendCommand(buffer_);
374}
375
376SymbolizerProcess::SymbolizerProcess(const char *path, bool use_forkpty)
377 : path_(path),
378 input_fd_(kInvalidFd((fd_t)-1)),
379 output_fd_(kInvalidFd((fd_t)-1)),
380 times_restarted_(0),
381 failed_to_start_(false),
382 reported_invalid_path_(false),
383 use_forkpty_(use_forkpty) {
384 CHECK(path_)do { __sanitizer::u64 v1 = (u64)((path_)); __sanitizer::u64 v2
= (u64)(0); if (__builtin_expect(!!(!(v1 != v2)), 0)) __sanitizer
::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cc"
, 384, "(" "(path_)" ") " "!=" " (" "0" ")", v1, v2); } while
(false)
;
385 CHECK_NE(path_[0], '\0')do { __sanitizer::u64 v1 = (u64)((path_[0])); __sanitizer::u64
v2 = (u64)(('\0')); if (__builtin_expect(!!(!(v1 != v2)), 0)
) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cc"
, 385, "(" "(path_[0])" ") " "!=" " (" "('\\0')" ")", v1, v2)
; } while (false)
;
386}
387
388static bool IsSameModule(const char* path) {
389 if (const char* ProcessName = GetProcessName()) {
390 if (const char* SymbolizerName = StripModuleName(path)) {
391 return !internal_strcmp(ProcessName, SymbolizerName);
392 }
393 }
394 return false;
395}
396
397const char *SymbolizerProcess::SendCommand(const char *command) {
398 if (failed_to_start_)
399 return nullptr;
400 if (IsSameModule(path_)) {
401 Report("WARNING: Symbolizer was blocked from starting itself!\n");
402 failed_to_start_ = true;
403 return nullptr;
404 }
405 for (; times_restarted_ < kMaxTimesRestarted; times_restarted_++) {
406 // Start or restart symbolizer if we failed to send command to it.
407 if (const char *res = SendCommandImpl(command))
408 return res;
409 Restart();
410 }
411 if (!failed_to_start_) {
412 Report("WARNING: Failed to use and restart external symbolizer!\n");
413 failed_to_start_ = true;
414 }
415 return 0;
416}
417
418const char *SymbolizerProcess::SendCommandImpl(const char *command) {
419 if (input_fd_ == kInvalidFd((fd_t)-1) || output_fd_ == kInvalidFd((fd_t)-1))
420 return 0;
421 if (!WriteToSymbolizer(command, internal_strlen(command)))
422 return 0;
423 if (!ReadFromSymbolizer(buffer_, kBufferSize))
424 return 0;
425 return buffer_;
426}
427
428bool SymbolizerProcess::Restart() {
429 if (input_fd_ != kInvalidFd((fd_t)-1))
430 CloseFile(input_fd_);
431 if (output_fd_ != kInvalidFd((fd_t)-1))
432 CloseFile(output_fd_);
433 return StartSymbolizerSubprocess();
434}
435
436bool SymbolizerProcess::ReadFromSymbolizer(char *buffer, uptr max_length) {
437 if (max_length == 0)
438 return true;
439 uptr read_len = 0;
440 while (true) {
441 uptr just_read = 0;
442 bool success = ReadFromFile(input_fd_, buffer + read_len,
443 max_length - read_len - 1, &just_read);
444 // We can't read 0 bytes, as we don't expect external symbolizer to close
445 // its stdout.
446 if (!success || just_read == 0) {
447 Report("WARNING: Can't read from symbolizer at fd %d\n", input_fd_);
448 return false;
449 }
450 read_len += just_read;
451 if (ReachedEndOfOutput(buffer, read_len))
452 break;
453 if (read_len + 1 == max_length) {
454 Report("WARNING: Symbolizer buffer too small");
455 read_len = 0;
456 break;
457 }
458 }
459 buffer[read_len] = '\0';
460 return true;
461}
462
463bool SymbolizerProcess::WriteToSymbolizer(const char *buffer, uptr length) {
464 if (length == 0)
465 return true;
466 uptr write_len = 0;
467 bool success = WriteToFile(output_fd_, buffer, length, &write_len);
468 if (!success || write_len != length) {
469 Report("WARNING: Can't write to symbolizer at fd %d\n", output_fd_);
470 return false;
471 }
472 return true;
473}
474
475} // namespace __sanitizer