blob: 8f95d2b64a202408724bcc062b265576519f338f [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";
20
Kostya Serebryanyf22c6972012-11-23 15:38:49 +000021uptr GetPageSizeCached() {
22 static uptr PageSize;
23 if (!PageSize)
24 PageSize = GetPageSize();
25 return PageSize;
26}
27
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000028static bool log_to_file = false; // Set to true by __sanitizer_set_report_path
29
30// By default, dump to stderr. If |log_to_file| is true and |report_fd_pid|
31// isn't equal to the current PID, try to obtain file descriptor by opening
32// file "report_path_prefix.<PID>".
Alexey Samsonov20ba98f2012-11-02 09:38:47 +000033static fd_t report_fd = kStderrFd;
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000034static char report_path_prefix[4096]; // Set via __sanitizer_set_report_path.
35// PID of process that opened |report_fd|. If a fork() occurs, the PID of the
36// child thread will be different from |report_fd_pid|.
37static int report_fd_pid = 0;
Kostya Serebryany45d849c2012-09-14 04:35:14 +000038
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000039static void (*DieCallback)(void);
40void SetDieCallback(void (*callback)(void)) {
41 DieCallback = callback;
42}
43
44void NORETURN Die() {
45 if (DieCallback) {
46 DieCallback();
47 }
48 Exit(1);
49}
50
51static CheckFailedCallbackType CheckFailedCallback;
52void SetCheckFailedCallback(CheckFailedCallbackType callback) {
53 CheckFailedCallback = callback;
54}
55
56void NORETURN CheckFailed(const char *file, int line, const char *cond,
57 u64 v1, u64 v2) {
58 if (CheckFailedCallback) {
59 CheckFailedCallback(file, line, cond, v1, v2);
60 }
Dmitry Vyukovaf96edb2013-01-11 15:07:49 +000061 Report("Sanitizer CHECK failed: %s:%d %s (%lld, %lld)\n", file, line, cond,
62 v1, v2);
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000063 Die();
64}
65
Alexey Samsonov58358892012-11-02 15:18:34 +000066static void MaybeOpenReportFile() {
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000067 if (!log_to_file || (report_fd_pid == GetPid())) return;
Alexey Samsonov2d048122013-01-20 13:22:06 +000068 InternalScopedBuffer<char> report_path_full(4096);
69 internal_snprintf(report_path_full.data(), report_path_full.size(),
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000070 "%s.%d", report_path_prefix, GetPid());
Alexey Samsonov39313b72013-02-01 15:58:46 +000071 fd_t fd = OpenFile(report_path_full.data(), true);
Alexey Samsonov58358892012-11-02 15:18:34 +000072 if (fd == kInvalidFd) {
73 report_fd = kStderrFd;
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000074 log_to_file = false;
Alexey Samsonov2d048122013-01-20 13:22:06 +000075 Report("ERROR: Can't open file: %s\n", report_path_full.data());
Alexey Samsonov58358892012-11-02 15:18:34 +000076 Die();
77 }
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000078 if (report_fd != kInvalidFd) {
79 // We're in the child. Close the parent's log.
80 internal_close(report_fd);
81 }
Alexey Samsonov58358892012-11-02 15:18:34 +000082 report_fd = fd;
Alexander Potapenkob4ba9952013-01-18 16:44:27 +000083 report_fd_pid = GetPid();
Alexey Samsonov58358892012-11-02 15:18:34 +000084}
85
86bool PrintsToTty() {
87 MaybeOpenReportFile();
88 return internal_isatty(report_fd);
89}
90
Alexey Samsonovee072902012-06-06 09:26:25 +000091void RawWrite(const char *buffer) {
92 static const char *kRawWriteError = "RawWrite can't output requested buffer!";
93 uptr length = (uptr)internal_strlen(buffer);
Alexey Samsonov58358892012-11-02 15:18:34 +000094 MaybeOpenReportFile();
Kostya Serebryany45d849c2012-09-14 04:35:14 +000095 if (length != internal_write(report_fd, buffer, length)) {
96 internal_write(report_fd, kRawWriteError, internal_strlen(kRawWriteError));
Alexey Samsonovee072902012-06-06 09:26:25 +000097 Die();
98 }
99}
100
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +0000101uptr ReadFileToBuffer(const char *file_name, char **buff,
102 uptr *buff_size, uptr max_len) {
Kostya Serebryanyf22c6972012-11-23 15:38:49 +0000103 uptr PageSize = GetPageSizeCached();
104 uptr kMinFileLen = PageSize;
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +0000105 uptr read_len = 0;
106 *buff = 0;
107 *buff_size = 0;
108 // The files we usually open are not seekable, so try different buffer sizes.
109 for (uptr size = kMinFileLen; size <= max_len; size *= 2) {
Alexey Samsonov39313b72013-02-01 15:58:46 +0000110 fd_t fd = OpenFile(file_name, /*write*/ false);
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +0000111 if (fd == kInvalidFd) return 0;
112 UnmapOrDie(*buff, *buff_size);
113 *buff = (char*)MmapOrDie(size, __FUNCTION__);
114 *buff_size = size;
115 // Read up to one page at a time.
116 read_len = 0;
117 bool reached_eof = false;
Kostya Serebryanyf22c6972012-11-23 15:38:49 +0000118 while (read_len + PageSize <= size) {
119 uptr just_read = internal_read(fd, *buff + read_len, PageSize);
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +0000120 if (just_read == 0) {
121 reached_eof = true;
122 break;
123 }
124 read_len += just_read;
125 }
126 internal_close(fd);
127 if (reached_eof) // We've read the whole file.
128 break;
129 }
130 return read_len;
131}
132
Alexey Samsonovd77fbba2012-07-16 11:27:17 +0000133// We don't want to use std::sort to avoid including <algorithm>, as
134// we may end up with two implementation of std::sort - one in instrumented
135// code, and the other in runtime.
136// qsort() from stdlib won't work as it calls malloc(), which results
137// in deadlock in ASan allocator.
138// We re-implement in-place sorting w/o recursion as straightforward heapsort.
Alexey Samsonove4a88982012-06-15 07:00:31 +0000139void SortArray(uptr *array, uptr size) {
Alexey Samsonovd77fbba2012-07-16 11:27:17 +0000140 if (size < 2)
141 return;
142 // Stage 1: insert elements to the heap.
143 for (uptr i = 1; i < size; i++) {
144 uptr j, p;
145 for (j = i; j > 0; j = p) {
146 p = (j - 1) / 2;
147 if (array[j] > array[p])
148 Swap(array[j], array[p]);
149 else
150 break;
151 }
152 }
153 // Stage 2: swap largest element with the last one,
154 // and sink the new top.
155 for (uptr i = size - 1; i > 0; i--) {
156 Swap(array[0], array[i]);
157 uptr j, max_ind;
158 for (j = 0; j < i; j = max_ind) {
159 uptr left = 2 * j + 1;
160 uptr right = 2 * j + 2;
161 max_ind = j;
162 if (left < i && array[left] > array[max_ind])
163 max_ind = left;
164 if (right < i && array[right] > array[max_ind])
165 max_ind = right;
166 if (max_ind != j)
167 Swap(array[j], array[max_ind]);
168 else
169 break;
170 }
171 }
Alexey Samsonove4a88982012-06-15 07:00:31 +0000172}
173
Kostya Serebryany1e3d3872012-12-06 06:10:31 +0000174// We want to map a chunk of address space aligned to 'alignment'.
175// We do it by maping a bit more and then unmaping redundant pieces.
176// We probably can do it with fewer syscalls in some OS-dependent way.
177void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type) {
Bill Wendlingf9528842012-12-06 07:43:17 +0000178// uptr PageSize = GetPageSizeCached();
Kostya Serebryany1e3d3872012-12-06 06:10:31 +0000179 CHECK(IsPowerOfTwo(size));
180 CHECK(IsPowerOfTwo(alignment));
181 uptr map_size = size + alignment;
182 uptr map_res = (uptr)MmapOrDie(map_size, mem_type);
183 uptr map_end = map_res + map_size;
184 uptr res = map_res;
185 if (res & (alignment - 1)) // Not aligned.
Dmitry Vyukove3e05572012-12-06 15:42:54 +0000186 res = (map_res + alignment) & ~(alignment - 1);
Kostya Serebryany1e3d3872012-12-06 06:10:31 +0000187 uptr end = res + size;
188 if (res != map_res)
189 UnmapOrDie((void*)map_res, res - map_res);
190 if (end != map_end)
191 UnmapOrDie((void*)end, map_end - end);
192 return (void*)res;
193}
194
Kostya Serebryanyb4c2c5c2013-02-06 12:36:49 +0000195void ReportErrorSummary(const char *error_type, const char *file,
196 int line, const char *function) {
197 const int kMaxSize = 1024; // We don't want a summary too long.
198 InternalScopedBuffer<char> buff(kMaxSize);
Kostya Serebryany4fb340d2013-02-06 14:24:00 +0000199 internal_snprintf(buff.data(), kMaxSize, "%s: %s %s:%d %s",
Kostya Serebryanyb4c2c5c2013-02-06 12:36:49 +0000200 SanitizerToolName, error_type,
Kostya Serebryany7b0b9b32013-02-07 08:04:56 +0000201 file ? file : "??", line, function ? function : "??");
Kostya Serebryanyb4c2c5c2013-02-06 12:36:49 +0000202 __sanitizer_report_error_summary(buff.data());
203}
204
Alexey Samsonovee072902012-06-06 09:26:25 +0000205} // namespace __sanitizer
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000206
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000207using namespace __sanitizer; // NOLINT
208
209extern "C" {
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000210void __sanitizer_set_report_path(const char *path) {
211 if (!path) return;
212 uptr len = internal_strlen(path);
Alexander Potapenkob4ba9952013-01-18 16:44:27 +0000213 if (len > sizeof(report_path_prefix) - 100) {
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000214 Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n",
215 path[0], path[1], path[2], path[3],
216 path[4], path[5], path[6], path[7]);
217 Die();
218 }
Alexander Potapenkob4ba9952013-01-18 16:44:27 +0000219 internal_strncpy(report_path_prefix, path, sizeof(report_path_prefix));
220 report_path_prefix[len] = '\0';
Alexey Samsonov20ba98f2012-11-02 09:38:47 +0000221 report_fd = kInvalidFd;
Alexander Potapenkob4ba9952013-01-18 16:44:27 +0000222 log_to_file = true;
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000223}
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000224
225void __sanitizer_set_report_fd(int fd) {
Alexey Samsonov20ba98f2012-11-02 09:38:47 +0000226 if (report_fd != kStdoutFd &&
227 report_fd != kStderrFd &&
228 report_fd != kInvalidFd)
229 internal_close(report_fd);
230 report_fd = fd;
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000231}
Alexander Potapenko1746f552012-12-10 13:10:40 +0000232
233void NOINLINE __sanitizer_sandbox_on_notify(void *reserved) {
234 (void)reserved;
235 PrepareForSandboxing();
236}
Kostya Serebryanyb4c2c5c2013-02-06 12:36:49 +0000237
238void __sanitizer_report_error_summary(const char *error_summary) {
239 Printf("SUMMARY: %s\n", error_summary);
240}
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000241} // extern "C"