[Sanitizers] Rename Symbolizer::SymbolizeCode to Symbolizer::SymbolizePC
[blocksruntime.git] / lib / sanitizer_common / sanitizer_common.cc
blob77ffda34acf6bba047075e2a7d4d3a2eaf99db94
1 //===-- sanitizer_common.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 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common.h"
15 #include "sanitizer_flags.h"
16 #include "sanitizer_libc.h"
17 #include "sanitizer_stacktrace.h"
18 #include "sanitizer_symbolizer.h"
20 namespace __sanitizer {
22 const char *SanitizerToolName = "SanitizerTool";
24 uptr GetPageSizeCached() {
25 static uptr PageSize;
26 if (!PageSize)
27 PageSize = GetPageSize();
28 return PageSize;
32 // By default, dump to stderr. If |log_to_file| is true and |report_fd_pid|
33 // isn't equal to the current PID, try to obtain file descriptor by opening
34 // file "report_path_prefix.<PID>".
35 fd_t report_fd = kStderrFd;
37 // Set via __sanitizer_set_report_path.
38 bool log_to_file = false;
39 char report_path_prefix[sizeof(report_path_prefix)];
41 // PID of process that opened |report_fd|. If a fork() occurs, the PID of the
42 // child thread will be different from |report_fd_pid|.
43 uptr report_fd_pid = 0;
45 // PID of the tracer task in StopTheWorld. It shares the address space with the
46 // main process, but has a different PID and thus requires special handling.
47 uptr stoptheworld_tracer_pid = 0;
48 // Cached pid of parent process - if the parent process dies, we want to keep
49 // writing to the same log file.
50 uptr stoptheworld_tracer_ppid = 0;
52 static DieCallbackType DieCallback;
53 void SetDieCallback(DieCallbackType callback) {
54 DieCallback = callback;
57 DieCallbackType GetDieCallback() {
58 return DieCallback;
61 void NORETURN Die() {
62 if (DieCallback) {
63 DieCallback();
65 internal__exit(1);
68 static CheckFailedCallbackType CheckFailedCallback;
69 void SetCheckFailedCallback(CheckFailedCallbackType callback) {
70 CheckFailedCallback = callback;
73 void NORETURN CheckFailed(const char *file, int line, const char *cond,
74 u64 v1, u64 v2) {
75 if (CheckFailedCallback) {
76 CheckFailedCallback(file, line, cond, v1, v2);
78 Report("Sanitizer CHECK failed: %s:%d %s (%lld, %lld)\n", file, line, cond,
79 v1, v2);
80 Die();
83 uptr ReadFileToBuffer(const char *file_name, char **buff,
84 uptr *buff_size, uptr max_len) {
85 uptr PageSize = GetPageSizeCached();
86 uptr kMinFileLen = PageSize;
87 uptr read_len = 0;
88 *buff = 0;
89 *buff_size = 0;
90 // The files we usually open are not seekable, so try different buffer sizes.
91 for (uptr size = kMinFileLen; size <= max_len; size *= 2) {
92 uptr openrv = OpenFile(file_name, /*write*/ false);
93 if (internal_iserror(openrv)) return 0;
94 fd_t fd = openrv;
95 UnmapOrDie(*buff, *buff_size);
96 *buff = (char*)MmapOrDie(size, __FUNCTION__);
97 *buff_size = size;
98 // Read up to one page at a time.
99 read_len = 0;
100 bool reached_eof = false;
101 while (read_len + PageSize <= size) {
102 uptr just_read = internal_read(fd, *buff + read_len, PageSize);
103 if (just_read == 0) {
104 reached_eof = true;
105 break;
107 read_len += just_read;
109 internal_close(fd);
110 if (reached_eof) // We've read the whole file.
111 break;
113 return read_len;
116 typedef bool UptrComparisonFunction(const uptr &a, const uptr &b);
118 template<class T>
119 static inline bool CompareLess(const T &a, const T &b) {
120 return a < b;
123 void SortArray(uptr *array, uptr size) {
124 InternalSort<uptr*, UptrComparisonFunction>(&array, size, CompareLess);
127 // We want to map a chunk of address space aligned to 'alignment'.
128 // We do it by maping a bit more and then unmaping redundant pieces.
129 // We probably can do it with fewer syscalls in some OS-dependent way.
130 void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type) {
131 // uptr PageSize = GetPageSizeCached();
132 CHECK(IsPowerOfTwo(size));
133 CHECK(IsPowerOfTwo(alignment));
134 uptr map_size = size + alignment;
135 uptr map_res = (uptr)MmapOrDie(map_size, mem_type);
136 uptr map_end = map_res + map_size;
137 uptr res = map_res;
138 if (res & (alignment - 1)) // Not aligned.
139 res = (map_res + alignment) & ~(alignment - 1);
140 uptr end = res + size;
141 if (res != map_res)
142 UnmapOrDie((void*)map_res, res - map_res);
143 if (end != map_end)
144 UnmapOrDie((void*)end, map_end - end);
145 return (void*)res;
148 const char *StripPathPrefix(const char *filepath,
149 const char *strip_path_prefix) {
150 if (filepath == 0) return 0;
151 if (strip_path_prefix == 0) return filepath;
152 const char *pos = internal_strstr(filepath, strip_path_prefix);
153 if (pos == 0) return filepath;
154 pos += internal_strlen(strip_path_prefix);
155 if (pos[0] == '.' && pos[1] == '/')
156 pos += 2;
157 return pos;
160 void PrintSourceLocation(InternalScopedString *buffer, const char *file,
161 int line, int column) {
162 CHECK(file);
163 buffer->append("%s",
164 StripPathPrefix(file, common_flags()->strip_path_prefix));
165 if (line > 0) {
166 buffer->append(":%d", line);
167 if (column > 0)
168 buffer->append(":%d", column);
172 void PrintModuleAndOffset(InternalScopedString *buffer, const char *module,
173 uptr offset) {
174 buffer->append("(%s+0x%zx)",
175 StripPathPrefix(module, common_flags()->strip_path_prefix),
176 offset);
179 void ReportErrorSummary(const char *error_message) {
180 if (!common_flags()->print_summary)
181 return;
182 InternalScopedBuffer<char> buff(kMaxSummaryLength);
183 internal_snprintf(buff.data(), buff.size(),
184 "SUMMARY: %s: %s", SanitizerToolName, error_message);
185 __sanitizer_report_error_summary(buff.data());
188 void ReportErrorSummary(const char *error_type, const char *file,
189 int line, const char *function) {
190 if (!common_flags()->print_summary)
191 return;
192 InternalScopedBuffer<char> buff(kMaxSummaryLength);
193 internal_snprintf(
194 buff.data(), buff.size(), "%s %s:%d %s", error_type,
195 file ? StripPathPrefix(file, common_flags()->strip_path_prefix) : "??",
196 line, function ? function : "??");
197 ReportErrorSummary(buff.data());
200 void ReportErrorSummary(const char *error_type, StackTrace *stack) {
201 if (!common_flags()->print_summary)
202 return;
203 AddressInfo ai;
204 #if !SANITIZER_GO
205 if (stack->size > 0 && Symbolizer::Get()->IsAvailable()) {
206 // Currently, we include the first stack frame into the report summary.
207 // Maybe sometimes we need to choose another frame (e.g. skip memcpy/etc).
208 uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
209 Symbolizer::Get()->SymbolizePC(pc, &ai, 1);
211 #endif
212 ReportErrorSummary(error_type, ai.file, ai.line, ai.function);
215 LoadedModule::LoadedModule(const char *module_name, uptr base_address) {
216 full_name_ = internal_strdup(module_name);
217 base_address_ = base_address;
218 n_ranges_ = 0;
221 void LoadedModule::addAddressRange(uptr beg, uptr end) {
222 CHECK_LT(n_ranges_, kMaxNumberOfAddressRanges);
223 ranges_[n_ranges_].beg = beg;
224 ranges_[n_ranges_].end = end;
225 n_ranges_++;
228 bool LoadedModule::containsAddress(uptr address) const {
229 for (uptr i = 0; i < n_ranges_; i++) {
230 if (ranges_[i].beg <= address && address < ranges_[i].end)
231 return true;
233 return false;
236 char *StripModuleName(const char *module) {
237 if (module == 0)
238 return 0;
239 const char *short_module_name = internal_strrchr(module, '/');
240 if (short_module_name)
241 short_module_name += 1;
242 else
243 short_module_name = module;
244 return internal_strdup(short_module_name);
247 } // namespace __sanitizer
249 using namespace __sanitizer; // NOLINT
251 extern "C" {
252 void __sanitizer_set_report_path(const char *path) {
253 if (!path)
254 return;
255 uptr len = internal_strlen(path);
256 if (len > sizeof(report_path_prefix) - 100) {
257 Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n",
258 path[0], path[1], path[2], path[3],
259 path[4], path[5], path[6], path[7]);
260 Die();
262 if (report_fd != kStdoutFd &&
263 report_fd != kStderrFd &&
264 report_fd != kInvalidFd)
265 internal_close(report_fd);
266 report_fd = kInvalidFd;
267 log_to_file = false;
268 if (internal_strcmp(path, "stdout") == 0) {
269 report_fd = kStdoutFd;
270 } else if (internal_strcmp(path, "stderr") == 0) {
271 report_fd = kStderrFd;
272 } else {
273 internal_strncpy(report_path_prefix, path, sizeof(report_path_prefix));
274 report_path_prefix[len] = '\0';
275 log_to_file = true;
279 void NOINLINE __sanitizer_sandbox_on_notify(void *reserved) {
280 (void)reserved;
281 PrepareForSandboxing();
284 void __sanitizer_report_error_summary(const char *error_summary) {
285 Printf("%s\n", error_summary);
287 } // extern "C"