1*7c3d14c8STreehugger Robot //===-- sanitizer_common.cc -----------------------------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot // The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is shared between AddressSanitizer and ThreadSanitizer
11*7c3d14c8STreehugger Robot // run-time libraries.
12*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
13*7c3d14c8STreehugger Robot
14*7c3d14c8STreehugger Robot #include "sanitizer_common.h"
15*7c3d14c8STreehugger Robot #include "sanitizer_allocator_interface.h"
16*7c3d14c8STreehugger Robot #include "sanitizer_allocator_internal.h"
17*7c3d14c8STreehugger Robot #include "sanitizer_flags.h"
18*7c3d14c8STreehugger Robot #include "sanitizer_libc.h"
19*7c3d14c8STreehugger Robot #include "sanitizer_placement_new.h"
20*7c3d14c8STreehugger Robot #include "sanitizer_stacktrace_printer.h"
21*7c3d14c8STreehugger Robot #include "sanitizer_symbolizer.h"
22*7c3d14c8STreehugger Robot
23*7c3d14c8STreehugger Robot namespace __sanitizer {
24*7c3d14c8STreehugger Robot
25*7c3d14c8STreehugger Robot const char *SanitizerToolName = "SanitizerTool";
26*7c3d14c8STreehugger Robot
27*7c3d14c8STreehugger Robot atomic_uint32_t current_verbosity;
28*7c3d14c8STreehugger Robot uptr PageSizeCached;
29*7c3d14c8STreehugger Robot
30*7c3d14c8STreehugger Robot StaticSpinMutex report_file_mu;
31*7c3d14c8STreehugger Robot ReportFile report_file = {&report_file_mu, kStderrFd, "", "", 0};
32*7c3d14c8STreehugger Robot
RawWrite(const char * buffer)33*7c3d14c8STreehugger Robot void RawWrite(const char *buffer) {
34*7c3d14c8STreehugger Robot report_file.Write(buffer, internal_strlen(buffer));
35*7c3d14c8STreehugger Robot }
36*7c3d14c8STreehugger Robot
ReopenIfNecessary()37*7c3d14c8STreehugger Robot void ReportFile::ReopenIfNecessary() {
38*7c3d14c8STreehugger Robot mu->CheckLocked();
39*7c3d14c8STreehugger Robot if (fd == kStdoutFd || fd == kStderrFd) return;
40*7c3d14c8STreehugger Robot
41*7c3d14c8STreehugger Robot uptr pid = internal_getpid();
42*7c3d14c8STreehugger Robot // If in tracer, use the parent's file.
43*7c3d14c8STreehugger Robot if (pid == stoptheworld_tracer_pid)
44*7c3d14c8STreehugger Robot pid = stoptheworld_tracer_ppid;
45*7c3d14c8STreehugger Robot if (fd != kInvalidFd) {
46*7c3d14c8STreehugger Robot // If the report file is already opened by the current process,
47*7c3d14c8STreehugger Robot // do nothing. Otherwise the report file was opened by the parent
48*7c3d14c8STreehugger Robot // process, close it now.
49*7c3d14c8STreehugger Robot if (fd_pid == pid)
50*7c3d14c8STreehugger Robot return;
51*7c3d14c8STreehugger Robot else
52*7c3d14c8STreehugger Robot CloseFile(fd);
53*7c3d14c8STreehugger Robot }
54*7c3d14c8STreehugger Robot
55*7c3d14c8STreehugger Robot const char *exe_name = GetProcessName();
56*7c3d14c8STreehugger Robot if (common_flags()->log_exe_name && exe_name) {
57*7c3d14c8STreehugger Robot internal_snprintf(full_path, kMaxPathLength, "%s.%s.%zu", path_prefix,
58*7c3d14c8STreehugger Robot exe_name, pid);
59*7c3d14c8STreehugger Robot } else {
60*7c3d14c8STreehugger Robot internal_snprintf(full_path, kMaxPathLength, "%s.%zu", path_prefix, pid);
61*7c3d14c8STreehugger Robot }
62*7c3d14c8STreehugger Robot fd = OpenFile(full_path, WrOnly);
63*7c3d14c8STreehugger Robot if (fd == kInvalidFd) {
64*7c3d14c8STreehugger Robot const char *ErrorMsgPrefix = "ERROR: Can't open file: ";
65*7c3d14c8STreehugger Robot WriteToFile(kStderrFd, ErrorMsgPrefix, internal_strlen(ErrorMsgPrefix));
66*7c3d14c8STreehugger Robot WriteToFile(kStderrFd, full_path, internal_strlen(full_path));
67*7c3d14c8STreehugger Robot Die();
68*7c3d14c8STreehugger Robot }
69*7c3d14c8STreehugger Robot fd_pid = pid;
70*7c3d14c8STreehugger Robot }
71*7c3d14c8STreehugger Robot
SetReportPath(const char * path)72*7c3d14c8STreehugger Robot void ReportFile::SetReportPath(const char *path) {
73*7c3d14c8STreehugger Robot if (!path)
74*7c3d14c8STreehugger Robot return;
75*7c3d14c8STreehugger Robot uptr len = internal_strlen(path);
76*7c3d14c8STreehugger Robot if (len > sizeof(path_prefix) - 100) {
77*7c3d14c8STreehugger Robot Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n",
78*7c3d14c8STreehugger Robot path[0], path[1], path[2], path[3],
79*7c3d14c8STreehugger Robot path[4], path[5], path[6], path[7]);
80*7c3d14c8STreehugger Robot Die();
81*7c3d14c8STreehugger Robot }
82*7c3d14c8STreehugger Robot
83*7c3d14c8STreehugger Robot SpinMutexLock l(mu);
84*7c3d14c8STreehugger Robot if (fd != kStdoutFd && fd != kStderrFd && fd != kInvalidFd)
85*7c3d14c8STreehugger Robot CloseFile(fd);
86*7c3d14c8STreehugger Robot fd = kInvalidFd;
87*7c3d14c8STreehugger Robot if (internal_strcmp(path, "stdout") == 0) {
88*7c3d14c8STreehugger Robot fd = kStdoutFd;
89*7c3d14c8STreehugger Robot } else if (internal_strcmp(path, "stderr") == 0) {
90*7c3d14c8STreehugger Robot fd = kStderrFd;
91*7c3d14c8STreehugger Robot } else {
92*7c3d14c8STreehugger Robot internal_snprintf(path_prefix, kMaxPathLength, "%s", path);
93*7c3d14c8STreehugger Robot }
94*7c3d14c8STreehugger Robot }
95*7c3d14c8STreehugger Robot
96*7c3d14c8STreehugger Robot // PID of the tracer task in StopTheWorld. It shares the address space with the
97*7c3d14c8STreehugger Robot // main process, but has a different PID and thus requires special handling.
98*7c3d14c8STreehugger Robot uptr stoptheworld_tracer_pid = 0;
99*7c3d14c8STreehugger Robot // Cached pid of parent process - if the parent process dies, we want to keep
100*7c3d14c8STreehugger Robot // writing to the same log file.
101*7c3d14c8STreehugger Robot uptr stoptheworld_tracer_ppid = 0;
102*7c3d14c8STreehugger Robot
ReportMmapFailureAndDie(uptr size,const char * mem_type,const char * mmap_type,error_t err,bool raw_report)103*7c3d14c8STreehugger Robot void NORETURN ReportMmapFailureAndDie(uptr size, const char *mem_type,
104*7c3d14c8STreehugger Robot const char *mmap_type, error_t err,
105*7c3d14c8STreehugger Robot bool raw_report) {
106*7c3d14c8STreehugger Robot static int recursion_count;
107*7c3d14c8STreehugger Robot if (raw_report || recursion_count) {
108*7c3d14c8STreehugger Robot // If raw report is requested or we went into recursion, just die.
109*7c3d14c8STreehugger Robot // The Report() and CHECK calls below may call mmap recursively and fail.
110*7c3d14c8STreehugger Robot RawWrite("ERROR: Failed to mmap\n");
111*7c3d14c8STreehugger Robot Die();
112*7c3d14c8STreehugger Robot }
113*7c3d14c8STreehugger Robot recursion_count++;
114*7c3d14c8STreehugger Robot Report("ERROR: %s failed to "
115*7c3d14c8STreehugger Robot "%s 0x%zx (%zd) bytes of %s (error code: %d)\n",
116*7c3d14c8STreehugger Robot SanitizerToolName, mmap_type, size, size, mem_type, err);
117*7c3d14c8STreehugger Robot #ifndef SANITIZER_GO
118*7c3d14c8STreehugger Robot DumpProcessMap();
119*7c3d14c8STreehugger Robot #endif
120*7c3d14c8STreehugger Robot UNREACHABLE("unable to mmap");
121*7c3d14c8STreehugger Robot }
122*7c3d14c8STreehugger Robot
ReadFileToBuffer(const char * file_name,char ** buff,uptr * buff_size,uptr * read_len,uptr max_len,error_t * errno_p)123*7c3d14c8STreehugger Robot bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
124*7c3d14c8STreehugger Robot uptr *read_len, uptr max_len, error_t *errno_p) {
125*7c3d14c8STreehugger Robot uptr PageSize = GetPageSizeCached();
126*7c3d14c8STreehugger Robot uptr kMinFileLen = PageSize;
127*7c3d14c8STreehugger Robot *buff = nullptr;
128*7c3d14c8STreehugger Robot *buff_size = 0;
129*7c3d14c8STreehugger Robot *read_len = 0;
130*7c3d14c8STreehugger Robot // The files we usually open are not seekable, so try different buffer sizes.
131*7c3d14c8STreehugger Robot for (uptr size = kMinFileLen; size <= max_len; size *= 2) {
132*7c3d14c8STreehugger Robot fd_t fd = OpenFile(file_name, RdOnly, errno_p);
133*7c3d14c8STreehugger Robot if (fd == kInvalidFd) return false;
134*7c3d14c8STreehugger Robot UnmapOrDie(*buff, *buff_size);
135*7c3d14c8STreehugger Robot *buff = (char*)MmapOrDie(size, __func__);
136*7c3d14c8STreehugger Robot *buff_size = size;
137*7c3d14c8STreehugger Robot *read_len = 0;
138*7c3d14c8STreehugger Robot // Read up to one page at a time.
139*7c3d14c8STreehugger Robot bool reached_eof = false;
140*7c3d14c8STreehugger Robot while (*read_len + PageSize <= size) {
141*7c3d14c8STreehugger Robot uptr just_read;
142*7c3d14c8STreehugger Robot if (!ReadFromFile(fd, *buff + *read_len, PageSize, &just_read, errno_p)) {
143*7c3d14c8STreehugger Robot UnmapOrDie(*buff, *buff_size);
144*7c3d14c8STreehugger Robot return false;
145*7c3d14c8STreehugger Robot }
146*7c3d14c8STreehugger Robot if (just_read == 0) {
147*7c3d14c8STreehugger Robot reached_eof = true;
148*7c3d14c8STreehugger Robot break;
149*7c3d14c8STreehugger Robot }
150*7c3d14c8STreehugger Robot *read_len += just_read;
151*7c3d14c8STreehugger Robot }
152*7c3d14c8STreehugger Robot CloseFile(fd);
153*7c3d14c8STreehugger Robot if (reached_eof) // We've read the whole file.
154*7c3d14c8STreehugger Robot break;
155*7c3d14c8STreehugger Robot }
156*7c3d14c8STreehugger Robot return true;
157*7c3d14c8STreehugger Robot }
158*7c3d14c8STreehugger Robot
159*7c3d14c8STreehugger Robot typedef bool UptrComparisonFunction(const uptr &a, const uptr &b);
160*7c3d14c8STreehugger Robot
161*7c3d14c8STreehugger Robot template<class T>
CompareLess(const T & a,const T & b)162*7c3d14c8STreehugger Robot static inline bool CompareLess(const T &a, const T &b) {
163*7c3d14c8STreehugger Robot return a < b;
164*7c3d14c8STreehugger Robot }
165*7c3d14c8STreehugger Robot
SortArray(uptr * array,uptr size)166*7c3d14c8STreehugger Robot void SortArray(uptr *array, uptr size) {
167*7c3d14c8STreehugger Robot InternalSort<uptr*, UptrComparisonFunction>(&array, size, CompareLess);
168*7c3d14c8STreehugger Robot }
169*7c3d14c8STreehugger Robot
StripPathPrefix(const char * filepath,const char * strip_path_prefix)170*7c3d14c8STreehugger Robot const char *StripPathPrefix(const char *filepath,
171*7c3d14c8STreehugger Robot const char *strip_path_prefix) {
172*7c3d14c8STreehugger Robot if (!filepath) return nullptr;
173*7c3d14c8STreehugger Robot if (!strip_path_prefix) return filepath;
174*7c3d14c8STreehugger Robot const char *res = filepath;
175*7c3d14c8STreehugger Robot if (const char *pos = internal_strstr(filepath, strip_path_prefix))
176*7c3d14c8STreehugger Robot res = pos + internal_strlen(strip_path_prefix);
177*7c3d14c8STreehugger Robot if (res[0] == '.' && res[1] == '/')
178*7c3d14c8STreehugger Robot res += 2;
179*7c3d14c8STreehugger Robot return res;
180*7c3d14c8STreehugger Robot }
181*7c3d14c8STreehugger Robot
StripModuleName(const char * module)182*7c3d14c8STreehugger Robot const char *StripModuleName(const char *module) {
183*7c3d14c8STreehugger Robot if (!module)
184*7c3d14c8STreehugger Robot return nullptr;
185*7c3d14c8STreehugger Robot if (SANITIZER_WINDOWS) {
186*7c3d14c8STreehugger Robot // On Windows, both slash and backslash are possible.
187*7c3d14c8STreehugger Robot // Pick the one that goes last.
188*7c3d14c8STreehugger Robot if (const char *bslash_pos = internal_strrchr(module, '\\'))
189*7c3d14c8STreehugger Robot return StripModuleName(bslash_pos + 1);
190*7c3d14c8STreehugger Robot }
191*7c3d14c8STreehugger Robot if (const char *slash_pos = internal_strrchr(module, '/')) {
192*7c3d14c8STreehugger Robot return slash_pos + 1;
193*7c3d14c8STreehugger Robot }
194*7c3d14c8STreehugger Robot return module;
195*7c3d14c8STreehugger Robot }
196*7c3d14c8STreehugger Robot
ReportErrorSummary(const char * error_message)197*7c3d14c8STreehugger Robot void ReportErrorSummary(const char *error_message) {
198*7c3d14c8STreehugger Robot if (!common_flags()->print_summary)
199*7c3d14c8STreehugger Robot return;
200*7c3d14c8STreehugger Robot InternalScopedString buff(kMaxSummaryLength);
201*7c3d14c8STreehugger Robot buff.append("SUMMARY: %s: %s", SanitizerToolName, error_message);
202*7c3d14c8STreehugger Robot __sanitizer_report_error_summary(buff.data());
203*7c3d14c8STreehugger Robot }
204*7c3d14c8STreehugger Robot
205*7c3d14c8STreehugger Robot #ifndef SANITIZER_GO
ReportErrorSummary(const char * error_type,const AddressInfo & info)206*7c3d14c8STreehugger Robot void ReportErrorSummary(const char *error_type, const AddressInfo &info) {
207*7c3d14c8STreehugger Robot if (!common_flags()->print_summary)
208*7c3d14c8STreehugger Robot return;
209*7c3d14c8STreehugger Robot InternalScopedString buff(kMaxSummaryLength);
210*7c3d14c8STreehugger Robot buff.append("%s ", error_type);
211*7c3d14c8STreehugger Robot RenderFrame(&buff, "%L %F", 0, info, common_flags()->symbolize_vs_style,
212*7c3d14c8STreehugger Robot common_flags()->strip_path_prefix);
213*7c3d14c8STreehugger Robot ReportErrorSummary(buff.data());
214*7c3d14c8STreehugger Robot }
215*7c3d14c8STreehugger Robot #endif
216*7c3d14c8STreehugger Robot
217*7c3d14c8STreehugger Robot // Removes the ANSI escape sequences from the input string (in-place).
RemoveANSIEscapeSequencesFromString(char * str)218*7c3d14c8STreehugger Robot void RemoveANSIEscapeSequencesFromString(char *str) {
219*7c3d14c8STreehugger Robot if (!str)
220*7c3d14c8STreehugger Robot return;
221*7c3d14c8STreehugger Robot
222*7c3d14c8STreehugger Robot // We are going to remove the escape sequences in place.
223*7c3d14c8STreehugger Robot char *s = str;
224*7c3d14c8STreehugger Robot char *z = str;
225*7c3d14c8STreehugger Robot while (*s != '\0') {
226*7c3d14c8STreehugger Robot CHECK_GE(s, z);
227*7c3d14c8STreehugger Robot // Skip over ANSI escape sequences with pointer 's'.
228*7c3d14c8STreehugger Robot if (*s == '\033' && *(s + 1) == '[') {
229*7c3d14c8STreehugger Robot s = internal_strchrnul(s, 'm');
230*7c3d14c8STreehugger Robot if (*s == '\0') {
231*7c3d14c8STreehugger Robot break;
232*7c3d14c8STreehugger Robot }
233*7c3d14c8STreehugger Robot s++;
234*7c3d14c8STreehugger Robot continue;
235*7c3d14c8STreehugger Robot }
236*7c3d14c8STreehugger Robot // 's' now points at a character we want to keep. Copy over the buffer
237*7c3d14c8STreehugger Robot // content if the escape sequence has been perviously skipped andadvance
238*7c3d14c8STreehugger Robot // both pointers.
239*7c3d14c8STreehugger Robot if (s != z)
240*7c3d14c8STreehugger Robot *z = *s;
241*7c3d14c8STreehugger Robot
242*7c3d14c8STreehugger Robot // If we have not seen an escape sequence, just advance both pointers.
243*7c3d14c8STreehugger Robot z++;
244*7c3d14c8STreehugger Robot s++;
245*7c3d14c8STreehugger Robot }
246*7c3d14c8STreehugger Robot
247*7c3d14c8STreehugger Robot // Null terminate the string.
248*7c3d14c8STreehugger Robot *z = '\0';
249*7c3d14c8STreehugger Robot }
250*7c3d14c8STreehugger Robot
set(const char * module_name,uptr base_address)251*7c3d14c8STreehugger Robot void LoadedModule::set(const char *module_name, uptr base_address) {
252*7c3d14c8STreehugger Robot clear();
253*7c3d14c8STreehugger Robot full_name_ = internal_strdup(module_name);
254*7c3d14c8STreehugger Robot base_address_ = base_address;
255*7c3d14c8STreehugger Robot }
256*7c3d14c8STreehugger Robot
clear()257*7c3d14c8STreehugger Robot void LoadedModule::clear() {
258*7c3d14c8STreehugger Robot InternalFree(full_name_);
259*7c3d14c8STreehugger Robot full_name_ = nullptr;
260*7c3d14c8STreehugger Robot while (!ranges_.empty()) {
261*7c3d14c8STreehugger Robot AddressRange *r = ranges_.front();
262*7c3d14c8STreehugger Robot ranges_.pop_front();
263*7c3d14c8STreehugger Robot InternalFree(r);
264*7c3d14c8STreehugger Robot }
265*7c3d14c8STreehugger Robot }
266*7c3d14c8STreehugger Robot
addAddressRange(uptr beg,uptr end,bool executable)267*7c3d14c8STreehugger Robot void LoadedModule::addAddressRange(uptr beg, uptr end, bool executable) {
268*7c3d14c8STreehugger Robot void *mem = InternalAlloc(sizeof(AddressRange));
269*7c3d14c8STreehugger Robot AddressRange *r = new(mem) AddressRange(beg, end, executable);
270*7c3d14c8STreehugger Robot ranges_.push_back(r);
271*7c3d14c8STreehugger Robot }
272*7c3d14c8STreehugger Robot
containsAddress(uptr address) const273*7c3d14c8STreehugger Robot bool LoadedModule::containsAddress(uptr address) const {
274*7c3d14c8STreehugger Robot for (const AddressRange &r : ranges()) {
275*7c3d14c8STreehugger Robot if (r.beg <= address && address < r.end)
276*7c3d14c8STreehugger Robot return true;
277*7c3d14c8STreehugger Robot }
278*7c3d14c8STreehugger Robot return false;
279*7c3d14c8STreehugger Robot }
280*7c3d14c8STreehugger Robot
281*7c3d14c8STreehugger Robot static atomic_uintptr_t g_total_mmaped;
282*7c3d14c8STreehugger Robot
IncreaseTotalMmap(uptr size)283*7c3d14c8STreehugger Robot void IncreaseTotalMmap(uptr size) {
284*7c3d14c8STreehugger Robot if (!common_flags()->mmap_limit_mb) return;
285*7c3d14c8STreehugger Robot uptr total_mmaped =
286*7c3d14c8STreehugger Robot atomic_fetch_add(&g_total_mmaped, size, memory_order_relaxed) + size;
287*7c3d14c8STreehugger Robot // Since for now mmap_limit_mb is not a user-facing flag, just kill
288*7c3d14c8STreehugger Robot // a program. Use RAW_CHECK to avoid extra mmaps in reporting.
289*7c3d14c8STreehugger Robot RAW_CHECK((total_mmaped >> 20) < common_flags()->mmap_limit_mb);
290*7c3d14c8STreehugger Robot }
291*7c3d14c8STreehugger Robot
DecreaseTotalMmap(uptr size)292*7c3d14c8STreehugger Robot void DecreaseTotalMmap(uptr size) {
293*7c3d14c8STreehugger Robot if (!common_flags()->mmap_limit_mb) return;
294*7c3d14c8STreehugger Robot atomic_fetch_sub(&g_total_mmaped, size, memory_order_relaxed);
295*7c3d14c8STreehugger Robot }
296*7c3d14c8STreehugger Robot
TemplateMatch(const char * templ,const char * str)297*7c3d14c8STreehugger Robot bool TemplateMatch(const char *templ, const char *str) {
298*7c3d14c8STreehugger Robot if ((!str) || str[0] == 0)
299*7c3d14c8STreehugger Robot return false;
300*7c3d14c8STreehugger Robot bool start = false;
301*7c3d14c8STreehugger Robot if (templ && templ[0] == '^') {
302*7c3d14c8STreehugger Robot start = true;
303*7c3d14c8STreehugger Robot templ++;
304*7c3d14c8STreehugger Robot }
305*7c3d14c8STreehugger Robot bool asterisk = false;
306*7c3d14c8STreehugger Robot while (templ && templ[0]) {
307*7c3d14c8STreehugger Robot if (templ[0] == '*') {
308*7c3d14c8STreehugger Robot templ++;
309*7c3d14c8STreehugger Robot start = false;
310*7c3d14c8STreehugger Robot asterisk = true;
311*7c3d14c8STreehugger Robot continue;
312*7c3d14c8STreehugger Robot }
313*7c3d14c8STreehugger Robot if (templ[0] == '$')
314*7c3d14c8STreehugger Robot return str[0] == 0 || asterisk;
315*7c3d14c8STreehugger Robot if (str[0] == 0)
316*7c3d14c8STreehugger Robot return false;
317*7c3d14c8STreehugger Robot char *tpos = (char*)internal_strchr(templ, '*');
318*7c3d14c8STreehugger Robot char *tpos1 = (char*)internal_strchr(templ, '$');
319*7c3d14c8STreehugger Robot if ((!tpos) || (tpos1 && tpos1 < tpos))
320*7c3d14c8STreehugger Robot tpos = tpos1;
321*7c3d14c8STreehugger Robot if (tpos)
322*7c3d14c8STreehugger Robot tpos[0] = 0;
323*7c3d14c8STreehugger Robot const char *str0 = str;
324*7c3d14c8STreehugger Robot const char *spos = internal_strstr(str, templ);
325*7c3d14c8STreehugger Robot str = spos + internal_strlen(templ);
326*7c3d14c8STreehugger Robot templ = tpos;
327*7c3d14c8STreehugger Robot if (tpos)
328*7c3d14c8STreehugger Robot tpos[0] = tpos == tpos1 ? '$' : '*';
329*7c3d14c8STreehugger Robot if (!spos)
330*7c3d14c8STreehugger Robot return false;
331*7c3d14c8STreehugger Robot if (start && spos != str0)
332*7c3d14c8STreehugger Robot return false;
333*7c3d14c8STreehugger Robot start = false;
334*7c3d14c8STreehugger Robot asterisk = false;
335*7c3d14c8STreehugger Robot }
336*7c3d14c8STreehugger Robot return true;
337*7c3d14c8STreehugger Robot }
338*7c3d14c8STreehugger Robot
339*7c3d14c8STreehugger Robot static const char kPathSeparator = SANITIZER_WINDOWS ? ';' : ':';
340*7c3d14c8STreehugger Robot
FindPathToBinary(const char * name)341*7c3d14c8STreehugger Robot char *FindPathToBinary(const char *name) {
342*7c3d14c8STreehugger Robot if (FileExists(name)) {
343*7c3d14c8STreehugger Robot return internal_strdup(name);
344*7c3d14c8STreehugger Robot }
345*7c3d14c8STreehugger Robot
346*7c3d14c8STreehugger Robot const char *path = GetEnv("PATH");
347*7c3d14c8STreehugger Robot if (!path)
348*7c3d14c8STreehugger Robot return nullptr;
349*7c3d14c8STreehugger Robot uptr name_len = internal_strlen(name);
350*7c3d14c8STreehugger Robot InternalScopedBuffer<char> buffer(kMaxPathLength);
351*7c3d14c8STreehugger Robot const char *beg = path;
352*7c3d14c8STreehugger Robot while (true) {
353*7c3d14c8STreehugger Robot const char *end = internal_strchrnul(beg, kPathSeparator);
354*7c3d14c8STreehugger Robot uptr prefix_len = end - beg;
355*7c3d14c8STreehugger Robot if (prefix_len + name_len + 2 <= kMaxPathLength) {
356*7c3d14c8STreehugger Robot internal_memcpy(buffer.data(), beg, prefix_len);
357*7c3d14c8STreehugger Robot buffer[prefix_len] = '/';
358*7c3d14c8STreehugger Robot internal_memcpy(&buffer[prefix_len + 1], name, name_len);
359*7c3d14c8STreehugger Robot buffer[prefix_len + 1 + name_len] = '\0';
360*7c3d14c8STreehugger Robot if (FileExists(buffer.data()))
361*7c3d14c8STreehugger Robot return internal_strdup(buffer.data());
362*7c3d14c8STreehugger Robot }
363*7c3d14c8STreehugger Robot if (*end == '\0') break;
364*7c3d14c8STreehugger Robot beg = end + 1;
365*7c3d14c8STreehugger Robot }
366*7c3d14c8STreehugger Robot return nullptr;
367*7c3d14c8STreehugger Robot }
368*7c3d14c8STreehugger Robot
369*7c3d14c8STreehugger Robot static char binary_name_cache_str[kMaxPathLength];
370*7c3d14c8STreehugger Robot static char process_name_cache_str[kMaxPathLength];
371*7c3d14c8STreehugger Robot
GetProcessName()372*7c3d14c8STreehugger Robot const char *GetProcessName() {
373*7c3d14c8STreehugger Robot return process_name_cache_str;
374*7c3d14c8STreehugger Robot }
375*7c3d14c8STreehugger Robot
ReadProcessName(char * buf,uptr buf_len)376*7c3d14c8STreehugger Robot static uptr ReadProcessName(/*out*/ char *buf, uptr buf_len) {
377*7c3d14c8STreehugger Robot ReadLongProcessName(buf, buf_len);
378*7c3d14c8STreehugger Robot char *s = const_cast<char *>(StripModuleName(buf));
379*7c3d14c8STreehugger Robot uptr len = internal_strlen(s);
380*7c3d14c8STreehugger Robot if (s != buf) {
381*7c3d14c8STreehugger Robot internal_memmove(buf, s, len);
382*7c3d14c8STreehugger Robot buf[len] = '\0';
383*7c3d14c8STreehugger Robot }
384*7c3d14c8STreehugger Robot return len;
385*7c3d14c8STreehugger Robot }
386*7c3d14c8STreehugger Robot
UpdateProcessName()387*7c3d14c8STreehugger Robot void UpdateProcessName() {
388*7c3d14c8STreehugger Robot ReadProcessName(process_name_cache_str, sizeof(process_name_cache_str));
389*7c3d14c8STreehugger Robot }
390*7c3d14c8STreehugger Robot
391*7c3d14c8STreehugger Robot // Call once to make sure that binary_name_cache_str is initialized
CacheBinaryName()392*7c3d14c8STreehugger Robot void CacheBinaryName() {
393*7c3d14c8STreehugger Robot if (binary_name_cache_str[0] != '\0')
394*7c3d14c8STreehugger Robot return;
395*7c3d14c8STreehugger Robot ReadBinaryName(binary_name_cache_str, sizeof(binary_name_cache_str));
396*7c3d14c8STreehugger Robot ReadProcessName(process_name_cache_str, sizeof(process_name_cache_str));
397*7c3d14c8STreehugger Robot }
398*7c3d14c8STreehugger Robot
ReadBinaryNameCached(char * buf,uptr buf_len)399*7c3d14c8STreehugger Robot uptr ReadBinaryNameCached(/*out*/char *buf, uptr buf_len) {
400*7c3d14c8STreehugger Robot CacheBinaryName();
401*7c3d14c8STreehugger Robot uptr name_len = internal_strlen(binary_name_cache_str);
402*7c3d14c8STreehugger Robot name_len = (name_len < buf_len - 1) ? name_len : buf_len - 1;
403*7c3d14c8STreehugger Robot if (buf_len == 0)
404*7c3d14c8STreehugger Robot return 0;
405*7c3d14c8STreehugger Robot internal_memcpy(buf, binary_name_cache_str, name_len);
406*7c3d14c8STreehugger Robot buf[name_len] = '\0';
407*7c3d14c8STreehugger Robot return name_len;
408*7c3d14c8STreehugger Robot }
409*7c3d14c8STreehugger Robot
PrintCmdline()410*7c3d14c8STreehugger Robot void PrintCmdline() {
411*7c3d14c8STreehugger Robot char **argv = GetArgv();
412*7c3d14c8STreehugger Robot if (!argv) return;
413*7c3d14c8STreehugger Robot Printf("\nCommand: ");
414*7c3d14c8STreehugger Robot for (uptr i = 0; argv[i]; ++i)
415*7c3d14c8STreehugger Robot Printf("%s ", argv[i]);
416*7c3d14c8STreehugger Robot Printf("\n\n");
417*7c3d14c8STreehugger Robot }
418*7c3d14c8STreehugger Robot
419*7c3d14c8STreehugger Robot // Malloc hooks.
420*7c3d14c8STreehugger Robot static const int kMaxMallocFreeHooks = 5;
421*7c3d14c8STreehugger Robot struct MallocFreeHook {
422*7c3d14c8STreehugger Robot void (*malloc_hook)(const void *, uptr);
423*7c3d14c8STreehugger Robot void (*free_hook)(const void *);
424*7c3d14c8STreehugger Robot };
425*7c3d14c8STreehugger Robot
426*7c3d14c8STreehugger Robot static MallocFreeHook MFHooks[kMaxMallocFreeHooks];
427*7c3d14c8STreehugger Robot
RunMallocHooks(const void * ptr,uptr size)428*7c3d14c8STreehugger Robot void RunMallocHooks(const void *ptr, uptr size) {
429*7c3d14c8STreehugger Robot for (int i = 0; i < kMaxMallocFreeHooks; i++) {
430*7c3d14c8STreehugger Robot auto hook = MFHooks[i].malloc_hook;
431*7c3d14c8STreehugger Robot if (!hook) return;
432*7c3d14c8STreehugger Robot hook(ptr, size);
433*7c3d14c8STreehugger Robot }
434*7c3d14c8STreehugger Robot }
435*7c3d14c8STreehugger Robot
RunFreeHooks(const void * ptr)436*7c3d14c8STreehugger Robot void RunFreeHooks(const void *ptr) {
437*7c3d14c8STreehugger Robot for (int i = 0; i < kMaxMallocFreeHooks; i++) {
438*7c3d14c8STreehugger Robot auto hook = MFHooks[i].free_hook;
439*7c3d14c8STreehugger Robot if (!hook) return;
440*7c3d14c8STreehugger Robot hook(ptr);
441*7c3d14c8STreehugger Robot }
442*7c3d14c8STreehugger Robot }
443*7c3d14c8STreehugger Robot
InstallMallocFreeHooks(void (* malloc_hook)(const void *,uptr),void (* free_hook)(const void *))444*7c3d14c8STreehugger Robot static int InstallMallocFreeHooks(void (*malloc_hook)(const void *, uptr),
445*7c3d14c8STreehugger Robot void (*free_hook)(const void *)) {
446*7c3d14c8STreehugger Robot if (!malloc_hook || !free_hook) return 0;
447*7c3d14c8STreehugger Robot for (int i = 0; i < kMaxMallocFreeHooks; i++) {
448*7c3d14c8STreehugger Robot if (MFHooks[i].malloc_hook == nullptr) {
449*7c3d14c8STreehugger Robot MFHooks[i].malloc_hook = malloc_hook;
450*7c3d14c8STreehugger Robot MFHooks[i].free_hook = free_hook;
451*7c3d14c8STreehugger Robot return i + 1;
452*7c3d14c8STreehugger Robot }
453*7c3d14c8STreehugger Robot }
454*7c3d14c8STreehugger Robot return 0;
455*7c3d14c8STreehugger Robot }
456*7c3d14c8STreehugger Robot
457*7c3d14c8STreehugger Robot } // namespace __sanitizer
458*7c3d14c8STreehugger Robot
459*7c3d14c8STreehugger Robot using namespace __sanitizer; // NOLINT
460*7c3d14c8STreehugger Robot
461*7c3d14c8STreehugger Robot extern "C" {
__sanitizer_set_report_path(const char * path)462*7c3d14c8STreehugger Robot void __sanitizer_set_report_path(const char *path) {
463*7c3d14c8STreehugger Robot report_file.SetReportPath(path);
464*7c3d14c8STreehugger Robot }
465*7c3d14c8STreehugger Robot
__sanitizer_set_report_fd(void * fd)466*7c3d14c8STreehugger Robot void __sanitizer_set_report_fd(void *fd) {
467*7c3d14c8STreehugger Robot report_file.fd = (fd_t)reinterpret_cast<uptr>(fd);
468*7c3d14c8STreehugger Robot report_file.fd_pid = internal_getpid();
469*7c3d14c8STreehugger Robot }
470*7c3d14c8STreehugger Robot
__sanitizer_report_error_summary(const char * error_summary)471*7c3d14c8STreehugger Robot void __sanitizer_report_error_summary(const char *error_summary) {
472*7c3d14c8STreehugger Robot Printf("%s\n", error_summary);
473*7c3d14c8STreehugger Robot }
474*7c3d14c8STreehugger Robot
475*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__sanitizer_set_death_callback(void (* callback)(void))476*7c3d14c8STreehugger Robot void __sanitizer_set_death_callback(void (*callback)(void)) {
477*7c3d14c8STreehugger Robot SetUserDieCallback(callback);
478*7c3d14c8STreehugger Robot }
479*7c3d14c8STreehugger Robot
480*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__sanitizer_install_malloc_and_free_hooks(void (* malloc_hook)(const void *,uptr),void (* free_hook)(const void *))481*7c3d14c8STreehugger Robot int __sanitizer_install_malloc_and_free_hooks(void (*malloc_hook)(const void *,
482*7c3d14c8STreehugger Robot uptr),
483*7c3d14c8STreehugger Robot void (*free_hook)(const void *)) {
484*7c3d14c8STreehugger Robot return InstallMallocFreeHooks(malloc_hook, free_hook);
485*7c3d14c8STreehugger Robot }
486*7c3d14c8STreehugger Robot } // extern "C"
487