1 //===-- sanitizer_common.cc -----------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between AddressSanitizer and ThreadSanitizer
10 //===----------------------------------------------------------------------===//
12 #include "sanitizer_common.h"
13 #include "sanitizer_flags.h"
14 #include "sanitizer_libc.h"
16 namespace __sanitizer
{
18 const char *SanitizerToolName
= "SanitizerTool";
20 uptr
GetPageSizeCached() {
23 PageSize
= GetPageSize();
28 // By default, dump to stderr. If |log_to_file| is true and |report_fd_pid|
29 // isn't equal to the current PID, try to obtain file descriptor by opening
30 // file "report_path_prefix.<PID>".
31 fd_t report_fd
= kStderrFd
;
33 // Set via __sanitizer_set_report_path.
34 bool log_to_file
= false;
35 char report_path_prefix
[sizeof(report_path_prefix
)];
37 // PID of process that opened |report_fd|. If a fork() occurs, the PID of the
38 // child thread will be different from |report_fd_pid|.
39 uptr report_fd_pid
= 0;
41 // PID of the tracer task in StopTheWorld. It shares the address space with the
42 // main process, but has a different PID and thus requires special handling.
43 uptr stoptheworld_tracer_pid
= 0;
44 // Cached pid of parent process - if the parent process dies, we want to keep
45 // writing to the same log file.
46 uptr stoptheworld_tracer_ppid
= 0;
48 static DieCallbackType DieCallback
;
49 void SetDieCallback(DieCallbackType callback
) {
50 DieCallback
= callback
;
53 DieCallbackType
GetDieCallback() {
64 static CheckFailedCallbackType CheckFailedCallback
;
65 void SetCheckFailedCallback(CheckFailedCallbackType callback
) {
66 CheckFailedCallback
= callback
;
69 void NORETURN
CheckFailed(const char *file
, int line
, const char *cond
,
71 if (CheckFailedCallback
) {
72 CheckFailedCallback(file
, line
, cond
, v1
, v2
);
74 Report("Sanitizer CHECK failed: %s:%d %s (%lld, %lld)\n", file
, line
, cond
,
79 uptr
ReadFileToBuffer(const char *file_name
, char **buff
,
80 uptr
*buff_size
, uptr max_len
) {
81 uptr PageSize
= GetPageSizeCached();
82 uptr kMinFileLen
= PageSize
;
86 // The files we usually open are not seekable, so try different buffer sizes.
87 for (uptr size
= kMinFileLen
; size
<= max_len
; size
*= 2) {
88 uptr openrv
= OpenFile(file_name
, /*write*/ false);
89 if (internal_iserror(openrv
)) return 0;
91 UnmapOrDie(*buff
, *buff_size
);
92 *buff
= (char*)MmapOrDie(size
, __func__
);
94 // Read up to one page at a time.
96 bool reached_eof
= false;
97 while (read_len
+ PageSize
<= size
) {
98 uptr just_read
= internal_read(fd
, *buff
+ read_len
, PageSize
);
103 read_len
+= just_read
;
106 if (reached_eof
) // We've read the whole file.
112 typedef bool UptrComparisonFunction(const uptr
&a
, const uptr
&b
);
115 static inline bool CompareLess(const T
&a
, const T
&b
) {
119 void SortArray(uptr
*array
, uptr size
) {
120 InternalSort
<uptr
*, UptrComparisonFunction
>(&array
, size
, CompareLess
);
123 // We want to map a chunk of address space aligned to 'alignment'.
124 // We do it by maping a bit more and then unmaping redundant pieces.
125 // We probably can do it with fewer syscalls in some OS-dependent way.
126 void *MmapAlignedOrDie(uptr size
, uptr alignment
, const char *mem_type
) {
127 // uptr PageSize = GetPageSizeCached();
128 CHECK(IsPowerOfTwo(size
));
129 CHECK(IsPowerOfTwo(alignment
));
130 uptr map_size
= size
+ alignment
;
131 uptr map_res
= (uptr
)MmapOrDie(map_size
, mem_type
);
132 uptr map_end
= map_res
+ map_size
;
134 if (res
& (alignment
- 1)) // Not aligned.
135 res
= (map_res
+ alignment
) & ~(alignment
- 1);
136 uptr end
= res
+ size
;
138 UnmapOrDie((void*)map_res
, res
- map_res
);
140 UnmapOrDie((void*)end
, map_end
- end
);
144 const char *StripPathPrefix(const char *filepath
,
145 const char *strip_path_prefix
) {
146 if (filepath
== 0) return 0;
147 if (strip_path_prefix
== 0) return filepath
;
148 const char *pos
= internal_strstr(filepath
, strip_path_prefix
);
149 if (pos
== 0) return filepath
;
150 pos
+= internal_strlen(strip_path_prefix
);
151 if (pos
[0] == '.' && pos
[1] == '/')
156 const char *StripModuleName(const char *module
) {
159 if (const char *slash_pos
= internal_strrchr(module
, '/'))
160 return slash_pos
+ 1;
164 void ReportErrorSummary(const char *error_message
) {
165 if (!common_flags()->print_summary
)
167 InternalScopedBuffer
<char> buff(kMaxSummaryLength
);
168 internal_snprintf(buff
.data(), buff
.size(),
169 "SUMMARY: %s: %s", SanitizerToolName
, error_message
);
170 __sanitizer_report_error_summary(buff
.data());
173 void ReportErrorSummary(const char *error_type
, const char *file
,
174 int line
, const char *function
) {
175 if (!common_flags()->print_summary
)
177 InternalScopedBuffer
<char> buff(kMaxSummaryLength
);
179 buff
.data(), buff
.size(), "%s %s:%d %s", error_type
,
180 file
? StripPathPrefix(file
, common_flags()->strip_path_prefix
) : "??",
181 line
, function
? function
: "??");
182 ReportErrorSummary(buff
.data());
185 LoadedModule::LoadedModule(const char *module_name
, uptr base_address
) {
186 full_name_
= internal_strdup(module_name
);
187 base_address_
= base_address
;
191 void LoadedModule::addAddressRange(uptr beg
, uptr end
, bool executable
) {
192 CHECK_LT(n_ranges_
, kMaxNumberOfAddressRanges
);
193 ranges_
[n_ranges_
].beg
= beg
;
194 ranges_
[n_ranges_
].end
= end
;
195 exec_
[n_ranges_
] = executable
;
199 bool LoadedModule::containsAddress(uptr address
) const {
200 for (uptr i
= 0; i
< n_ranges_
; i
++) {
201 if (ranges_
[i
].beg
<= address
&& address
< ranges_
[i
].end
)
207 static atomic_uintptr_t g_total_mmaped
;
209 void IncreaseTotalMmap(uptr size
) {
210 if (!common_flags()->mmap_limit_mb
) return;
212 atomic_fetch_add(&g_total_mmaped
, size
, memory_order_relaxed
) + size
;
213 if ((total_mmaped
>> 20) > common_flags()->mmap_limit_mb
) {
214 // Since for now mmap_limit_mb is not a user-facing flag, just CHECK.
215 uptr mmap_limit_mb
= common_flags()->mmap_limit_mb
;
216 common_flags()->mmap_limit_mb
= 0; // Allow mmap in CHECK.
217 RAW_CHECK(total_mmaped
>> 20 < mmap_limit_mb
);
221 void DecreaseTotalMmap(uptr size
) {
222 if (!common_flags()->mmap_limit_mb
) return;
223 atomic_fetch_sub(&g_total_mmaped
, size
, memory_order_relaxed
);
226 } // namespace __sanitizer
228 using namespace __sanitizer
; // NOLINT
231 void __sanitizer_set_report_path(const char *path
) {
234 uptr len
= internal_strlen(path
);
235 if (len
> sizeof(report_path_prefix
) - 100) {
236 Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n",
237 path
[0], path
[1], path
[2], path
[3],
238 path
[4], path
[5], path
[6], path
[7]);
241 if (report_fd
!= kStdoutFd
&&
242 report_fd
!= kStderrFd
&&
243 report_fd
!= kInvalidFd
)
244 internal_close(report_fd
);
245 report_fd
= kInvalidFd
;
247 if (internal_strcmp(path
, "stdout") == 0) {
248 report_fd
= kStdoutFd
;
249 } else if (internal_strcmp(path
, "stderr") == 0) {
250 report_fd
= kStderrFd
;
252 internal_strncpy(report_path_prefix
, path
, sizeof(report_path_prefix
));
253 report_path_prefix
[len
] = '\0';
258 void __sanitizer_report_error_summary(const char *error_summary
) {
259 Printf("%s\n", error_summary
);