blob: 5e68c95b7475bfb688ebef156353b3f99b617ba1 [file] [log] [blame]
Alexey Samsonovee072902012-06-06 09:26:25 +00001//===-- 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//===----------------------------------------------------------------------===//
13
14#include "sanitizer_common.h"
Alexey Samsonovf2b811a2013-10-04 08:55:03 +000015#include "sanitizer_flags.h"
Alexey Samsonovee072902012-06-06 09:26:25 +000016#include "sanitizer_libc.h"
Alexey Samsonov5dc6cff2013-11-01 17:02:14 +000017#include "sanitizer_stacktrace.h"
18#include "sanitizer_symbolizer.h"
Alexey Samsonove4a88982012-06-15 07:00:31 +000019
Alexey Samsonovee072902012-06-06 09:26:25 +000020namespace __sanitizer {
21
Kostya Serebryanybda64b42013-01-31 14:11:21 +000022const char *SanitizerToolName = "SanitizerTool";
23
Kostya Serebryanyf22c6972012-11-23 15:38:49 +000024uptr GetPageSizeCached() {
25 static uptr PageSize;
26 if (!PageSize)
27 PageSize = GetPageSize();
28 return PageSize;
29}
30
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000031
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>".
Peter Collingbourne6d4a7d372013-05-17 16:17:19 +000035fd_t report_fd = kStderrFd;
Reid Klecknerd483c072013-09-05 03:19:57 +000036
37// Set via __sanitizer_set_report_path.
38bool log_to_file = false;
39char report_path_prefix[sizeof(report_path_prefix)];
40
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000041// 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|.
Reid Klecknerd483c072013-09-05 03:19:57 +000043uptr report_fd_pid = 0;
Kostya Serebryany45d849c2012-09-14 04:35:14 +000044
Sergey Matveev7bc300c2013-12-04 14:37:01 +000045// 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.
47uptr 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.
50uptr stoptheworld_tracer_ppid = 0;
51
Sergey Matveevef7db732013-08-26 13:20:31 +000052static DieCallbackType DieCallback;
53void SetDieCallback(DieCallbackType callback) {
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000054 DieCallback = callback;
55}
56
Sergey Matveevef7db732013-08-26 13:20:31 +000057DieCallbackType GetDieCallback() {
58 return DieCallback;
59}
60
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000061void NORETURN Die() {
62 if (DieCallback) {
63 DieCallback();
64 }
Alexey Samsonovaadd1f22013-02-20 13:54:32 +000065 internal__exit(1);
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000066}
67
68static CheckFailedCallbackType CheckFailedCallback;
69void SetCheckFailedCallback(CheckFailedCallbackType callback) {
70 CheckFailedCallback = callback;
71}
72
73void 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);
77 }
Dmitry Vyukovaf96edb2013-01-11 15:07:49 +000078 Report("Sanitizer CHECK failed: %s:%d %s (%lld, %lld)\n", file, line, cond,
79 v1, v2);
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000080 Die();
81}
82
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +000083uptr ReadFileToBuffer(const char *file_name, char **buff,
84 uptr *buff_size, uptr max_len) {
Kostya Serebryanyf22c6972012-11-23 15:38:49 +000085 uptr PageSize = GetPageSizeCached();
86 uptr kMinFileLen = PageSize;
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +000087 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) {
Peter Collingbourne6f4be192013-05-08 14:43:49 +000092 uptr openrv = OpenFile(file_name, /*write*/ false);
93 if (internal_iserror(openrv)) return 0;
94 fd_t fd = openrv;
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +000095 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;
Kostya Serebryanyf22c6972012-11-23 15:38:49 +0000101 while (read_len + PageSize <= size) {
102 uptr just_read = internal_read(fd, *buff + read_len, PageSize);
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +0000103 if (just_read == 0) {
104 reached_eof = true;
105 break;
106 }
107 read_len += just_read;
108 }
109 internal_close(fd);
110 if (reached_eof) // We've read the whole file.
111 break;
112 }
113 return read_len;
114}
115
Sergey Matveevd9da20f2013-05-13 11:58:48 +0000116typedef bool UptrComparisonFunction(const uptr &a, const uptr &b);
117
118template<class T>
119static inline bool CompareLess(const T &a, const T &b) {
120 return a < b;
121}
122
Alexey Samsonove4a88982012-06-15 07:00:31 +0000123void SortArray(uptr *array, uptr size) {
Sergey Matveevd9da20f2013-05-13 11:58:48 +0000124 InternalSort<uptr*, UptrComparisonFunction>(&array, size, CompareLess);
Alexey Samsonove4a88982012-06-15 07:00:31 +0000125}
126
Kostya Serebryany1e3d3872012-12-06 06:10:31 +0000127// 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.
130void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type) {
Bill Wendlingf9528842012-12-06 07:43:17 +0000131// uptr PageSize = GetPageSizeCached();
Kostya Serebryany1e3d3872012-12-06 06:10:31 +0000132 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.
Dmitry Vyukove3e05572012-12-06 15:42:54 +0000139 res = (map_res + alignment) & ~(alignment - 1);
Kostya Serebryany1e3d3872012-12-06 06:10:31 +0000140 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;
146}
147
Alexey Samsonovf2b811a2013-10-04 08:55:03 +0000148const 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;
158}
159
Alexey Samsonov4d369187e2013-11-14 09:41:24 +0000160void PrintSourceLocation(InternalScopedString *buffer, const char *file,
161 int line, int column) {
Alexey Samsonovf2b811a2013-10-04 08:55:03 +0000162 CHECK(file);
Alexey Samsonov4d369187e2013-11-14 09:41:24 +0000163 buffer->append("%s",
164 StripPathPrefix(file, common_flags()->strip_path_prefix));
Alexey Samsonovf2b811a2013-10-04 08:55:03 +0000165 if (line > 0) {
Alexey Samsonov4d369187e2013-11-14 09:41:24 +0000166 buffer->append(":%d", line);
Alexey Samsonovf2b811a2013-10-04 08:55:03 +0000167 if (column > 0)
Alexey Samsonov4d369187e2013-11-14 09:41:24 +0000168 buffer->append(":%d", column);
Alexey Samsonovf2b811a2013-10-04 08:55:03 +0000169 }
170}
171
Alexey Samsonov4d369187e2013-11-14 09:41:24 +0000172void 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);
Alexey Samsonovf2b811a2013-10-04 08:55:03 +0000177}
178
Alexey Samsonov5dc6cff2013-11-01 17:02:14 +0000179void ReportErrorSummary(const char *error_message) {
Alexey Samsonov498e5902013-11-14 08:56:59 +0000180 if (!common_flags()->print_summary)
181 return;
Alexey Samsonov5dc6cff2013-11-01 17:02:14 +0000182 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());
186}
187
Kostya Serebryanyb4c2c5c2013-02-06 12:36:49 +0000188void ReportErrorSummary(const char *error_type, const char *file,
189 int line, const char *function) {
Alexey Samsonov498e5902013-11-14 08:56:59 +0000190 if (!common_flags()->print_summary)
191 return;
Alexey Samsonov5dc6cff2013-11-01 17:02:14 +0000192 InternalScopedBuffer<char> buff(kMaxSummaryLength);
Alexey Samsonovf2b811a2013-10-04 08:55:03 +0000193 internal_snprintf(
Alexey Samsonov5dc6cff2013-11-01 17:02:14 +0000194 buff.data(), buff.size(), "%s %s:%d %s", error_type,
Alexey Samsonovf2b811a2013-10-04 08:55:03 +0000195 file ? StripPathPrefix(file, common_flags()->strip_path_prefix) : "??",
196 line, function ? function : "??");
Alexey Samsonov5dc6cff2013-11-01 17:02:14 +0000197 ReportErrorSummary(buff.data());
198}
199
200void ReportErrorSummary(const char *error_type, StackTrace *stack) {
Alexey Samsonov498e5902013-11-14 08:56:59 +0000201 if (!common_flags()->print_summary)
202 return;
Alexey Samsonov5dc6cff2013-11-01 17:02:14 +0000203 AddressInfo ai;
Alexey Samsonov31d4c342013-11-01 18:00:22 +0000204#if !SANITIZER_GO
Alexey Samsonov6322e032013-12-25 07:09:44 +0000205 if (stack->size > 0 && Symbolizer::Get()->CanReturnFileLineInfo()) {
Alexey Samsonov5dc6cff2013-11-01 17:02:14 +0000206 // 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]);
Timur Iskhodzhanovb5f983e2013-12-18 14:17:39 +0000209 Symbolizer::Get()->SymbolizePC(pc, &ai, 1);
Alexey Samsonov5dc6cff2013-11-01 17:02:14 +0000210 }
Alexey Samsonov31d4c342013-11-01 18:00:22 +0000211#endif
Alexey Samsonov5dc6cff2013-11-01 17:02:14 +0000212 ReportErrorSummary(error_type, ai.file, ai.line, ai.function);
Kostya Serebryanyb4c2c5c2013-02-06 12:36:49 +0000213}
214
Alexey Samsonov7a36e612013-09-10 14:36:16 +0000215LoadedModule::LoadedModule(const char *module_name, uptr base_address) {
216 full_name_ = internal_strdup(module_name);
217 base_address_ = base_address;
218 n_ranges_ = 0;
219}
220
221void 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_++;
226}
227
228bool 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;
232 }
233 return false;
234}
235
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000236char *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);
245}
246
Alexey Samsonovee072902012-06-06 09:26:25 +0000247} // namespace __sanitizer
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000248
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000249using namespace __sanitizer; // NOLINT
250
251extern "C" {
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000252void __sanitizer_set_report_path(const char *path) {
Dmitry Vyukov7ac0b2b2013-10-15 12:25:29 +0000253 if (!path)
254 return;
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000255 uptr len = internal_strlen(path);
Alexander Potapenkob4ba9952013-01-18 16:44:27 +0000256 if (len > sizeof(report_path_prefix) - 100) {
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000257 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();
261 }
Alexey Samsonov20ba98f2012-11-02 09:38:47 +0000262 if (report_fd != kStdoutFd &&
263 report_fd != kStderrFd &&
264 report_fd != kInvalidFd)
265 internal_close(report_fd);
Dmitry Vyukov7ac0b2b2013-10-15 12:25:29 +0000266 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;
276 }
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000277}
Alexander Potapenko1746f552012-12-10 13:10:40 +0000278
279void NOINLINE __sanitizer_sandbox_on_notify(void *reserved) {
280 (void)reserved;
281 PrepareForSandboxing();
282}
Kostya Serebryanyb4c2c5c2013-02-06 12:36:49 +0000283
284void __sanitizer_report_error_summary(const char *error_summary) {
Nick Lewycky622348e2013-10-23 07:45:53 +0000285 Printf("%s\n", error_summary);
Kostya Serebryanyb4c2c5c2013-02-06 12:36:49 +0000286}
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000287} // extern "C"