blob: 3bb8e8118d4055d62cb30a6abeec3fe273be021a [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"
15#include "sanitizer_libc.h"
Alexey Samsonove4a88982012-06-15 07:00:31 +000016
Alexey Samsonovee072902012-06-06 09:26:25 +000017namespace __sanitizer {
18
Kostya Serebryanybda64b42013-01-31 14:11:21 +000019const char *SanitizerToolName = "SanitizerTool";
Alexander Potapenko845b5752013-03-15 14:37:21 +000020uptr SanitizerVerbosity = 0;
Kostya Serebryanybda64b42013-01-31 14:11:21 +000021
Kostya Serebryanyf22c6972012-11-23 15:38:49 +000022uptr GetPageSizeCached() {
23 static uptr PageSize;
24 if (!PageSize)
25 PageSize = GetPageSize();
26 return PageSize;
27}
28
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000029static bool log_to_file = false; // Set to true by __sanitizer_set_report_path
30
31// By default, dump to stderr. If |log_to_file| is true and |report_fd_pid|
32// isn't equal to the current PID, try to obtain file descriptor by opening
33// file "report_path_prefix.<PID>".
Alexey Samsonov20ba98f2012-11-02 09:38:47 +000034static fd_t report_fd = kStderrFd;
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000035static char report_path_prefix[4096]; // Set via __sanitizer_set_report_path.
36// PID of process that opened |report_fd|. If a fork() occurs, the PID of the
37// child thread will be different from |report_fd_pid|.
38static int report_fd_pid = 0;
Kostya Serebryany45d849c2012-09-14 04:35:14 +000039
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000040static void (*DieCallback)(void);
41void SetDieCallback(void (*callback)(void)) {
42 DieCallback = callback;
43}
44
45void NORETURN Die() {
46 if (DieCallback) {
47 DieCallback();
48 }
Alexey Samsonovaadd1f22013-02-20 13:54:32 +000049 internal__exit(1);
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000050}
51
52static CheckFailedCallbackType CheckFailedCallback;
53void SetCheckFailedCallback(CheckFailedCallbackType callback) {
54 CheckFailedCallback = callback;
55}
56
57void NORETURN CheckFailed(const char *file, int line, const char *cond,
58 u64 v1, u64 v2) {
59 if (CheckFailedCallback) {
60 CheckFailedCallback(file, line, cond, v1, v2);
61 }
Dmitry Vyukovaf96edb2013-01-11 15:07:49 +000062 Report("Sanitizer CHECK failed: %s:%d %s (%lld, %lld)\n", file, line, cond,
63 v1, v2);
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000064 Die();
65}
66
Alexey Samsonov58358892012-11-02 15:18:34 +000067static void MaybeOpenReportFile() {
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000068 if (!log_to_file || (report_fd_pid == GetPid())) return;
Alexey Samsonov2d048122013-01-20 13:22:06 +000069 InternalScopedBuffer<char> report_path_full(4096);
70 internal_snprintf(report_path_full.data(), report_path_full.size(),
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000071 "%s.%d", report_path_prefix, GetPid());
Peter Collingbourne6f4be192013-05-08 14:43:49 +000072 uptr openrv = OpenFile(report_path_full.data(), true);
73 if (internal_iserror(openrv)) {
Alexey Samsonov58358892012-11-02 15:18:34 +000074 report_fd = kStderrFd;
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000075 log_to_file = false;
Alexey Samsonov2d048122013-01-20 13:22:06 +000076 Report("ERROR: Can't open file: %s\n", report_path_full.data());
Alexey Samsonov58358892012-11-02 15:18:34 +000077 Die();
78 }
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000079 if (report_fd != kInvalidFd) {
80 // We're in the child. Close the parent's log.
81 internal_close(report_fd);
82 }
Peter Collingbourne6f4be192013-05-08 14:43:49 +000083 report_fd = openrv;
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000084 report_fd_pid = GetPid();
Alexey Samsonov58358892012-11-02 15:18:34 +000085}
86
87bool PrintsToTty() {
88 MaybeOpenReportFile();
89 return internal_isatty(report_fd);
90}
91
Alexey Samsonovee072902012-06-06 09:26:25 +000092void RawWrite(const char *buffer) {
93 static const char *kRawWriteError = "RawWrite can't output requested buffer!";
94 uptr length = (uptr)internal_strlen(buffer);
Alexey Samsonov58358892012-11-02 15:18:34 +000095 MaybeOpenReportFile();
Kostya Serebryany45d849c2012-09-14 04:35:14 +000096 if (length != internal_write(report_fd, buffer, length)) {
97 internal_write(report_fd, kRawWriteError, internal_strlen(kRawWriteError));
Alexey Samsonovee072902012-06-06 09:26:25 +000098 Die();
99 }
100}
101
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +0000102uptr ReadFileToBuffer(const char *file_name, char **buff,
103 uptr *buff_size, uptr max_len) {
Kostya Serebryanyf22c6972012-11-23 15:38:49 +0000104 uptr PageSize = GetPageSizeCached();
105 uptr kMinFileLen = PageSize;
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +0000106 uptr read_len = 0;
107 *buff = 0;
108 *buff_size = 0;
109 // The files we usually open are not seekable, so try different buffer sizes.
110 for (uptr size = kMinFileLen; size <= max_len; size *= 2) {
Peter Collingbourne6f4be192013-05-08 14:43:49 +0000111 uptr openrv = OpenFile(file_name, /*write*/ false);
112 if (internal_iserror(openrv)) return 0;
113 fd_t fd = openrv;
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +0000114 UnmapOrDie(*buff, *buff_size);
115 *buff = (char*)MmapOrDie(size, __FUNCTION__);
116 *buff_size = size;
117 // Read up to one page at a time.
118 read_len = 0;
119 bool reached_eof = false;
Kostya Serebryanyf22c6972012-11-23 15:38:49 +0000120 while (read_len + PageSize <= size) {
121 uptr just_read = internal_read(fd, *buff + read_len, PageSize);
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +0000122 if (just_read == 0) {
123 reached_eof = true;
124 break;
125 }
126 read_len += just_read;
127 }
128 internal_close(fd);
129 if (reached_eof) // We've read the whole file.
130 break;
131 }
132 return read_len;
133}
134
Alexey Samsonovd77fbba2012-07-16 11:27:17 +0000135// We don't want to use std::sort to avoid including <algorithm>, as
136// we may end up with two implementation of std::sort - one in instrumented
137// code, and the other in runtime.
138// qsort() from stdlib won't work as it calls malloc(), which results
139// in deadlock in ASan allocator.
140// We re-implement in-place sorting w/o recursion as straightforward heapsort.
Alexey Samsonove4a88982012-06-15 07:00:31 +0000141void SortArray(uptr *array, uptr size) {
Alexey Samsonovd77fbba2012-07-16 11:27:17 +0000142 if (size < 2)
143 return;
144 // Stage 1: insert elements to the heap.
145 for (uptr i = 1; i < size; i++) {
146 uptr j, p;
147 for (j = i; j > 0; j = p) {
148 p = (j - 1) / 2;
149 if (array[j] > array[p])
150 Swap(array[j], array[p]);
151 else
152 break;
153 }
154 }
155 // Stage 2: swap largest element with the last one,
156 // and sink the new top.
157 for (uptr i = size - 1; i > 0; i--) {
158 Swap(array[0], array[i]);
159 uptr j, max_ind;
160 for (j = 0; j < i; j = max_ind) {
161 uptr left = 2 * j + 1;
162 uptr right = 2 * j + 2;
163 max_ind = j;
164 if (left < i && array[left] > array[max_ind])
165 max_ind = left;
166 if (right < i && array[right] > array[max_ind])
167 max_ind = right;
168 if (max_ind != j)
169 Swap(array[j], array[max_ind]);
170 else
171 break;
172 }
173 }
Alexey Samsonove4a88982012-06-15 07:00:31 +0000174}
175
Kostya Serebryany1e3d3872012-12-06 06:10:31 +0000176// We want to map a chunk of address space aligned to 'alignment'.
177// We do it by maping a bit more and then unmaping redundant pieces.
178// We probably can do it with fewer syscalls in some OS-dependent way.
179void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type) {
Bill Wendlingf9528842012-12-06 07:43:17 +0000180// uptr PageSize = GetPageSizeCached();
Kostya Serebryany1e3d3872012-12-06 06:10:31 +0000181 CHECK(IsPowerOfTwo(size));
182 CHECK(IsPowerOfTwo(alignment));
183 uptr map_size = size + alignment;
184 uptr map_res = (uptr)MmapOrDie(map_size, mem_type);
185 uptr map_end = map_res + map_size;
186 uptr res = map_res;
187 if (res & (alignment - 1)) // Not aligned.
Dmitry Vyukove3e05572012-12-06 15:42:54 +0000188 res = (map_res + alignment) & ~(alignment - 1);
Kostya Serebryany1e3d3872012-12-06 06:10:31 +0000189 uptr end = res + size;
190 if (res != map_res)
191 UnmapOrDie((void*)map_res, res - map_res);
192 if (end != map_end)
193 UnmapOrDie((void*)end, map_end - end);
194 return (void*)res;
195}
196
Kostya Serebryanyb4c2c5c2013-02-06 12:36:49 +0000197void ReportErrorSummary(const char *error_type, const char *file,
198 int line, const char *function) {
199 const int kMaxSize = 1024; // We don't want a summary too long.
200 InternalScopedBuffer<char> buff(kMaxSize);
Kostya Serebryany4fb340d2013-02-06 14:24:00 +0000201 internal_snprintf(buff.data(), kMaxSize, "%s: %s %s:%d %s",
Kostya Serebryanyb4c2c5c2013-02-06 12:36:49 +0000202 SanitizerToolName, error_type,
Kostya Serebryany7b0b9b32013-02-07 08:04:56 +0000203 file ? file : "??", line, function ? function : "??");
Kostya Serebryanyb4c2c5c2013-02-06 12:36:49 +0000204 __sanitizer_report_error_summary(buff.data());
205}
206
Alexey Samsonovee072902012-06-06 09:26:25 +0000207} // namespace __sanitizer
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000208
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000209using namespace __sanitizer; // NOLINT
210
211extern "C" {
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000212void __sanitizer_set_report_path(const char *path) {
213 if (!path) return;
214 uptr len = internal_strlen(path);
Alexander Potapenkob4ba9952013-01-18 16:44:27 +0000215 if (len > sizeof(report_path_prefix) - 100) {
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000216 Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n",
217 path[0], path[1], path[2], path[3],
218 path[4], path[5], path[6], path[7]);
219 Die();
220 }
Alexander Potapenkob4ba9952013-01-18 16:44:27 +0000221 internal_strncpy(report_path_prefix, path, sizeof(report_path_prefix));
222 report_path_prefix[len] = '\0';
Alexey Samsonov20ba98f2012-11-02 09:38:47 +0000223 report_fd = kInvalidFd;
Alexander Potapenkob4ba9952013-01-18 16:44:27 +0000224 log_to_file = true;
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000225}
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000226
227void __sanitizer_set_report_fd(int fd) {
Alexey Samsonov20ba98f2012-11-02 09:38:47 +0000228 if (report_fd != kStdoutFd &&
229 report_fd != kStderrFd &&
230 report_fd != kInvalidFd)
231 internal_close(report_fd);
232 report_fd = fd;
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000233}
Alexander Potapenko1746f552012-12-10 13:10:40 +0000234
235void NOINLINE __sanitizer_sandbox_on_notify(void *reserved) {
236 (void)reserved;
237 PrepareForSandboxing();
238}
Kostya Serebryanyb4c2c5c2013-02-06 12:36:49 +0000239
240void __sanitizer_report_error_summary(const char *error_summary) {
241 Printf("SUMMARY: %s\n", error_summary);
242}
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000243} // extern "C"