blob: d56cfdafd201280467c9318e924973856e8a5094 [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 Samsonovee072902012-06-06 09:26:25 +000051void RawWrite(const char *buffer) {
52 static const char *kRawWriteError = "RawWrite can't output requested buffer!";
53 uptr length = (uptr)internal_strlen(buffer);
Kostya Serebryany45d849c2012-09-14 04:35:14 +000054 if (report_fd == kInvalidFd) {
55 fd_t fd = internal_open(report_path, true);
56 if (fd == kInvalidFd) {
Alexey Samsonov20ba98f2012-11-02 09:38:47 +000057 report_fd = kStderrFd;
Kostya Serebryany45d849c2012-09-14 04:35:14 +000058 Report("ERROR: Can't open file: %s\n", report_path);
59 Die();
60 }
61 report_fd = fd;
62 }
63 if (length != internal_write(report_fd, buffer, length)) {
64 internal_write(report_fd, kRawWriteError, internal_strlen(kRawWriteError));
Alexey Samsonovee072902012-06-06 09:26:25 +000065 Die();
66 }
67}
68
Alexey Samsonovfe44fbd2012-06-07 05:38:26 +000069uptr ReadFileToBuffer(const char *file_name, char **buff,
70 uptr *buff_size, uptr max_len) {
71 const uptr kMinFileLen = kPageSize;
72 uptr read_len = 0;
73 *buff = 0;
74 *buff_size = 0;
75 // The files we usually open are not seekable, so try different buffer sizes.
76 for (uptr size = kMinFileLen; size <= max_len; size *= 2) {
77 fd_t fd = internal_open(file_name, /*write*/ false);
78 if (fd == kInvalidFd) return 0;
79 UnmapOrDie(*buff, *buff_size);
80 *buff = (char*)MmapOrDie(size, __FUNCTION__);
81 *buff_size = size;
82 // Read up to one page at a time.
83 read_len = 0;
84 bool reached_eof = false;
85 while (read_len + kPageSize <= size) {
86 uptr just_read = internal_read(fd, *buff + read_len, kPageSize);
87 if (just_read == 0) {
88 reached_eof = true;
89 break;
90 }
91 read_len += just_read;
92 }
93 internal_close(fd);
94 if (reached_eof) // We've read the whole file.
95 break;
96 }
97 return read_len;
98}
99
Alexey Samsonovd77fbba2012-07-16 11:27:17 +0000100// We don't want to use std::sort to avoid including <algorithm>, as
101// we may end up with two implementation of std::sort - one in instrumented
102// code, and the other in runtime.
103// qsort() from stdlib won't work as it calls malloc(), which results
104// in deadlock in ASan allocator.
105// We re-implement in-place sorting w/o recursion as straightforward heapsort.
Alexey Samsonove4a88982012-06-15 07:00:31 +0000106void SortArray(uptr *array, uptr size) {
Alexey Samsonovd77fbba2012-07-16 11:27:17 +0000107 if (size < 2)
108 return;
109 // Stage 1: insert elements to the heap.
110 for (uptr i = 1; i < size; i++) {
111 uptr j, p;
112 for (j = i; j > 0; j = p) {
113 p = (j - 1) / 2;
114 if (array[j] > array[p])
115 Swap(array[j], array[p]);
116 else
117 break;
118 }
119 }
120 // Stage 2: swap largest element with the last one,
121 // and sink the new top.
122 for (uptr i = size - 1; i > 0; i--) {
123 Swap(array[0], array[i]);
124 uptr j, max_ind;
125 for (j = 0; j < i; j = max_ind) {
126 uptr left = 2 * j + 1;
127 uptr right = 2 * j + 2;
128 max_ind = j;
129 if (left < i && array[left] > array[max_ind])
130 max_ind = left;
131 if (right < i && array[right] > array[max_ind])
132 max_ind = right;
133 if (max_ind != j)
134 Swap(array[j], array[max_ind]);
135 else
136 break;
137 }
138 }
Alexey Samsonove4a88982012-06-15 07:00:31 +0000139}
140
Alexey Samsonovee072902012-06-06 09:26:25 +0000141} // namespace __sanitizer
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000142
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000143using namespace __sanitizer; // NOLINT
144
145extern "C" {
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000146void __sanitizer_set_report_path(const char *path) {
147 if (!path) return;
148 uptr len = internal_strlen(path);
Alexey Samsonov20ba98f2012-11-02 09:38:47 +0000149 if (len > sizeof(report_path) - 100) {
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000150 Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n",
151 path[0], path[1], path[2], path[3],
152 path[4], path[5], path[6], path[7]);
153 Die();
154 }
Alexey Samsonov20ba98f2012-11-02 09:38:47 +0000155 internal_snprintf(report_path, sizeof(report_path), "%s.%d", path, GetPid());
156 report_fd = kInvalidFd;
Kostya Serebryany45d849c2012-09-14 04:35:14 +0000157}
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000158
159void __sanitizer_set_report_fd(int fd) {
Alexey Samsonov20ba98f2012-11-02 09:38:47 +0000160 if (report_fd != kStdoutFd &&
161 report_fd != kStderrFd &&
162 report_fd != kInvalidFd)
163 internal_close(report_fd);
164 report_fd = fd;
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000165}
Alexey Samsonovfd67c832012-11-02 09:23:36 +0000166} // extern "C"