blob: e0f9997e49d2d55557ab5ee33e1cd3fca52b53f4 [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
Alexey Samsonovfd67c832012-11-02 09:23:36 +000019// By default, dump to stderr. If report_fd is kInvalidFd, try to obtain file
20// descriptor by opening file in report_path.
Alexey Samsonov20ba98f2012-11-02 09:38:47 +000021static fd_t report_fd = kStderrFd;
Kostya Serebryany45d849c2012-09-14 04:35:14 +000022static char report_path[4096]; // Set via __sanitizer_set_report_path.
23
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000024static void (*DieCallback)(void);
25void SetDieCallback(void (*callback)(void)) {
26 DieCallback = callback;
27}
28
29void NORETURN Die() {
30 if (DieCallback) {
31 DieCallback();
32 }
33 Exit(1);
34}
35
36static CheckFailedCallbackType CheckFailedCallback;
37void SetCheckFailedCallback(CheckFailedCallbackType callback) {
38 CheckFailedCallback = callback;
39}
40
41void NORETURN CheckFailed(const char *file, int line, const char *cond,
42 u64 v1, u64 v2) {
43 if (CheckFailedCallback) {
44 CheckFailedCallback(file, line, cond, v1, v2);
45 }
46 Report("Sanitizer CHECK failed: %s:%d %s (%zd, %zd)\n", file, line, cond,
47 v1, v2);
48 Die();
49}
50
Alexey Samsonov58358892012-11-02 15:18:34 +000051static void MaybeOpenReportFile() {
52 if (report_fd != kInvalidFd)
53 return;
54 fd_t fd = internal_open(report_path, true);
55 if (fd == kInvalidFd) {
56 report_fd = kStderrFd;
57 Report("ERROR: Can't open file: %s\n", report_path);
58 Die();
59 }
60 report_fd = fd;
61}
62
63bool PrintsToTty() {
64 MaybeOpenReportFile();
65 return internal_isatty(report_fd);
66}
67
Alexey Samsonovee072902012-06-06 09:26:25 +000068void RawWrite(const char *buffer) {
69 static const char *kRawWriteError = "RawWrite can't output requested buffer!";
70 uptr length = (uptr)internal_strlen(buffer);
Alexey Samsonov58358892012-11-02 15:18:34 +000071 MaybeOpenReportFile();
Kostya Serebryany45d849c2012-09-14 04:35:14 +000072 if (length != internal_write(report_fd, buffer, length)) {
73 internal_write(report_fd, kRawWriteError, internal_strlen(kRawWriteError));
Alexey Samsonovee072902012-06-06 09:26:25 +000074 Die();
75 }
76}
77
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +000078uptr ReadFileToBuffer(const char *file_name, char **buff,
79 uptr *buff_size, uptr max_len) {
80 const uptr kMinFileLen = kPageSize;
81 uptr read_len = 0;
82 *buff = 0;
83 *buff_size = 0;
84 // The files we usually open are not seekable, so try different buffer sizes.
85 for (uptr size = kMinFileLen; size <= max_len; size *= 2) {
86 fd_t fd = internal_open(file_name, /*write*/ false);
87 if (fd == kInvalidFd) return 0;
88 UnmapOrDie(*buff, *buff_size);
89 *buff = (char*)MmapOrDie(size, __FUNCTION__);
90 *buff_size = size;
91 // Read up to one page at a time.
92 read_len = 0;
93 bool reached_eof = false;
94 while (read_len + kPageSize <= size) {
95 uptr just_read = internal_read(fd, *buff + read_len, kPageSize);
96 if (just_read == 0) {
97 reached_eof = true;
98 break;
99 }
100 read_len += just_read;
101 }
102 internal_close(fd);
103 if (reached_eof) // We've read the whole file.
104 break;
105 }
106 return read_len;
107}
108
Alexey Samsonovd77fbba2012-07-16 11:27:17 +0000109// We don't want to use std::sort to avoid including <algorithm>, as
110// we may end up with two implementation of std::sort - one in instrumented
111// code, and the other in runtime.
112// qsort() from stdlib won't work as it calls malloc(), which results
113// in deadlock in ASan allocator.
114// We re-implement in-place sorting w/o recursion as straightforward heapsort.
Alexey Samsonove4a88982012-06-15 07:00:31 +0000115void SortArray(uptr *array, uptr size) {
Alexey Samsonovd77fbba2012-07-16 11:27:17 +0000116 if (size < 2)
117 return;
118 // Stage 1: insert elements to the heap.
119 for (uptr i = 1; i < size; i++) {
120 uptr j, p;
121 for (j = i; j > 0; j = p) {
122 p = (j - 1) / 2;
123 if (array[j] > array[p])
124 Swap(array[j], array[p]);
125 else
126 break;
127 }
128 }
129 // Stage 2: swap largest element with the last one,
130 // and sink the new top.
131 for (uptr i = size - 1; i > 0; i--) {
132 Swap(array[0], array[i]);
133 uptr j, max_ind;
134 for (j = 0; j < i; j = max_ind) {
135 uptr left = 2 * j + 1;
136 uptr right = 2 * j + 2;
137 max_ind = j;
138 if (left < i && array[left] > array[max_ind])
139 max_ind = left;
140 if (right < i && array[right] > array[max_ind])
141 max_ind = right;
142 if (max_ind != j)
143 Swap(array[j], array[max_ind]);
144 else
145 break;
146 }
147 }
Alexey Samsonove4a88982012-06-15 07:00:31 +0000148}
149
Alexey Samsonovee072902012-06-06 09:26:25 +0000150} // namespace __sanitizer
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000151
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000152using namespace __sanitizer; // NOLINT
153
154extern "C" {
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000155void __sanitizer_set_report_path(const char *path) {
156 if (!path) return;
157 uptr len = internal_strlen(path);
Alexey Samsonov20ba98f2012-11-02 09:38:47 +0000158 if (len > sizeof(report_path) - 100) {
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000159 Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n",
160 path[0], path[1], path[2], path[3],
161 path[4], path[5], path[6], path[7]);
162 Die();
163 }
Alexey Samsonov20ba98f2012-11-02 09:38:47 +0000164 internal_snprintf(report_path, sizeof(report_path), "%s.%d", path, GetPid());
165 report_fd = kInvalidFd;
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000166}
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000167
168void __sanitizer_set_report_fd(int fd) {
Alexey Samsonov20ba98f2012-11-02 09:38:47 +0000169 if (report_fd != kStdoutFd &&
170 report_fd != kStderrFd &&
171 report_fd != kInvalidFd)
172 internal_close(report_fd);
173 report_fd = fd;
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000174}
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000175} // extern "C"