1 //===-- sanitizer_common.cc -----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common.h"
15 #include "sanitizer_flags.h"
16 #include "sanitizer_libc.h"
17 #include "sanitizer_stacktrace.h"
18 #include "sanitizer_symbolizer.h"
20 namespace __sanitizer
{
22 const char *SanitizerToolName
= "SanitizerTool";
24 uptr
GetPageSizeCached() {
27 PageSize
= GetPageSize();
32 // By default, dump to stderr. If |log_to_file| is true and |report_fd_pid|
33 // isn't equal to the current PID, try to obtain file descriptor by opening
34 // file "report_path_prefix.<PID>".
35 fd_t report_fd
= kStderrFd
;
37 // Set via __sanitizer_set_report_path.
38 bool log_to_file
= false;
39 char report_path_prefix
[sizeof(report_path_prefix
)];
41 // PID of process that opened |report_fd|. If a fork() occurs, the PID of the
42 // child thread will be different from |report_fd_pid|.
43 uptr report_fd_pid
= 0;
45 // PID of the tracer task in StopTheWorld. It shares the address space with the
46 // main process, but has a different PID and thus requires special handling.
47 uptr stoptheworld_tracer_pid
= 0;
48 // Cached pid of parent process - if the parent process dies, we want to keep
49 // writing to the same log file.
50 uptr stoptheworld_tracer_ppid
= 0;
52 static DieCallbackType DieCallback
;
53 void SetDieCallback(DieCallbackType callback
) {
54 DieCallback
= callback
;
57 DieCallbackType
GetDieCallback() {
68 static CheckFailedCallbackType CheckFailedCallback
;
69 void SetCheckFailedCallback(CheckFailedCallbackType callback
) {
70 CheckFailedCallback
= callback
;
73 void NORETURN
CheckFailed(const char *file
, int line
, const char *cond
,
75 if (CheckFailedCallback
) {
76 CheckFailedCallback(file
, line
, cond
, v1
, v2
);
78 Report("Sanitizer CHECK failed: %s:%d %s (%lld, %lld)\n", file
, line
, cond
,
83 uptr
ReadFileToBuffer(const char *file_name
, char **buff
,
84 uptr
*buff_size
, uptr max_len
) {
85 uptr PageSize
= GetPageSizeCached();
86 uptr kMinFileLen
= PageSize
;
90 // The files we usually open are not seekable, so try different buffer sizes.
91 for (uptr size
= kMinFileLen
; size
<= max_len
; size
*= 2) {
92 uptr openrv
= OpenFile(file_name
, /*write*/ false);
93 if (internal_iserror(openrv
)) return 0;
95 UnmapOrDie(*buff
, *buff_size
);
96 *buff
= (char*)MmapOrDie(size
, __FUNCTION__
);
98 // Read up to one page at a time.
100 bool reached_eof
= false;
101 while (read_len
+ PageSize
<= size
) {
102 uptr just_read
= internal_read(fd
, *buff
+ read_len
, PageSize
);
103 if (just_read
== 0) {
107 read_len
+= just_read
;
110 if (reached_eof
) // We've read the whole file.
116 typedef bool UptrComparisonFunction(const uptr
&a
, const uptr
&b
);
119 static inline bool CompareLess(const T
&a
, const T
&b
) {
123 void SortArray(uptr
*array
, uptr size
) {
124 InternalSort
<uptr
*, UptrComparisonFunction
>(&array
, size
, CompareLess
);
127 // We want to map a chunk of address space aligned to 'alignment'.
128 // We do it by maping a bit more and then unmaping redundant pieces.
129 // We probably can do it with fewer syscalls in some OS-dependent way.
130 void *MmapAlignedOrDie(uptr size
, uptr alignment
, const char *mem_type
) {
131 // uptr PageSize = GetPageSizeCached();
132 CHECK(IsPowerOfTwo(size
));
133 CHECK(IsPowerOfTwo(alignment
));
134 uptr map_size
= size
+ alignment
;
135 uptr map_res
= (uptr
)MmapOrDie(map_size
, mem_type
);
136 uptr map_end
= map_res
+ map_size
;
138 if (res
& (alignment
- 1)) // Not aligned.
139 res
= (map_res
+ alignment
) & ~(alignment
- 1);
140 uptr end
= res
+ size
;
142 UnmapOrDie((void*)map_res
, res
- map_res
);
144 UnmapOrDie((void*)end
, map_end
- end
);
148 const char *StripPathPrefix(const char *filepath
,
149 const char *strip_path_prefix
) {
150 if (filepath
== 0) return 0;
151 if (strip_path_prefix
== 0) return filepath
;
152 const char *pos
= internal_strstr(filepath
, strip_path_prefix
);
153 if (pos
== 0) return filepath
;
154 pos
+= internal_strlen(strip_path_prefix
);
155 if (pos
[0] == '.' && pos
[1] == '/')
160 void PrintSourceLocation(InternalScopedString
*buffer
, const char *file
,
161 int line
, int column
) {
164 StripPathPrefix(file
, common_flags()->strip_path_prefix
));
166 buffer
->append(":%d", line
);
168 buffer
->append(":%d", column
);
172 void PrintModuleAndOffset(InternalScopedString
*buffer
, const char *module
,
174 buffer
->append("(%s+0x%zx)",
175 StripPathPrefix(module
, common_flags()->strip_path_prefix
),
179 void ReportErrorSummary(const char *error_message
) {
180 if (!common_flags()->print_summary
)
182 InternalScopedBuffer
<char> buff(kMaxSummaryLength
);
183 internal_snprintf(buff
.data(), buff
.size(),
184 "SUMMARY: %s: %s", SanitizerToolName
, error_message
);
185 __sanitizer_report_error_summary(buff
.data());
188 void ReportErrorSummary(const char *error_type
, const char *file
,
189 int line
, const char *function
) {
190 if (!common_flags()->print_summary
)
192 InternalScopedBuffer
<char> buff(kMaxSummaryLength
);
194 buff
.data(), buff
.size(), "%s %s:%d %s", error_type
,
195 file
? StripPathPrefix(file
, common_flags()->strip_path_prefix
) : "??",
196 line
, function
? function
: "??");
197 ReportErrorSummary(buff
.data());
200 void ReportErrorSummary(const char *error_type
, StackTrace
*stack
) {
201 if (!common_flags()->print_summary
)
205 if (stack
->size
> 0 && Symbolizer::Get()->IsAvailable()) {
206 // Currently, we include the first stack frame into the report summary.
207 // Maybe sometimes we need to choose another frame (e.g. skip memcpy/etc).
208 uptr pc
= StackTrace::GetPreviousInstructionPc(stack
->trace
[0]);
209 Symbolizer::Get()->SymbolizePC(pc
, &ai
, 1);
212 ReportErrorSummary(error_type
, ai
.file
, ai
.line
, ai
.function
);
215 LoadedModule::LoadedModule(const char *module_name
, uptr base_address
) {
216 full_name_
= internal_strdup(module_name
);
217 base_address_
= base_address
;
221 void LoadedModule::addAddressRange(uptr beg
, uptr end
) {
222 CHECK_LT(n_ranges_
, kMaxNumberOfAddressRanges
);
223 ranges_
[n_ranges_
].beg
= beg
;
224 ranges_
[n_ranges_
].end
= end
;
228 bool LoadedModule::containsAddress(uptr address
) const {
229 for (uptr i
= 0; i
< n_ranges_
; i
++) {
230 if (ranges_
[i
].beg
<= address
&& address
< ranges_
[i
].end
)
236 char *StripModuleName(const char *module
) {
239 const char *short_module_name
= internal_strrchr(module
, '/');
240 if (short_module_name
)
241 short_module_name
+= 1;
243 short_module_name
= module
;
244 return internal_strdup(short_module_name
);
247 } // namespace __sanitizer
249 using namespace __sanitizer
; // NOLINT
252 void __sanitizer_set_report_path(const char *path
) {
255 uptr len
= internal_strlen(path
);
256 if (len
> sizeof(report_path_prefix
) - 100) {
257 Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n",
258 path
[0], path
[1], path
[2], path
[3],
259 path
[4], path
[5], path
[6], path
[7]);
262 if (report_fd
!= kStdoutFd
&&
263 report_fd
!= kStderrFd
&&
264 report_fd
!= kInvalidFd
)
265 internal_close(report_fd
);
266 report_fd
= kInvalidFd
;
268 if (internal_strcmp(path
, "stdout") == 0) {
269 report_fd
= kStdoutFd
;
270 } else if (internal_strcmp(path
, "stderr") == 0) {
271 report_fd
= kStderrFd
;
273 internal_strncpy(report_path_prefix
, path
, sizeof(report_path_prefix
));
274 report_path_prefix
[len
] = '\0';
279 void NOINLINE
__sanitizer_sandbox_on_notify(void *reserved
) {
281 PrepareForSandboxing();
284 void __sanitizer_report_error_summary(const char *error_summary
) {
285 Printf("%s\n", error_summary
);