1*7c3d14c8STreehugger Robot //===-- sanitizer_posix.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 and implements POSIX-specific functions from
12*7c3d14c8STreehugger Robot // sanitizer_posix.h.
13*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
14*7c3d14c8STreehugger Robot
15*7c3d14c8STreehugger Robot #include "sanitizer_platform.h"
16*7c3d14c8STreehugger Robot
17*7c3d14c8STreehugger Robot #if SANITIZER_POSIX
18*7c3d14c8STreehugger Robot
19*7c3d14c8STreehugger Robot #include "sanitizer_common.h"
20*7c3d14c8STreehugger Robot #include "sanitizer_libc.h"
21*7c3d14c8STreehugger Robot #include "sanitizer_posix.h"
22*7c3d14c8STreehugger Robot #include "sanitizer_procmaps.h"
23*7c3d14c8STreehugger Robot #include "sanitizer_stacktrace.h"
24*7c3d14c8STreehugger Robot
25*7c3d14c8STreehugger Robot #include <fcntl.h>
26*7c3d14c8STreehugger Robot #include <signal.h>
27*7c3d14c8STreehugger Robot #include <sys/mman.h>
28*7c3d14c8STreehugger Robot
29*7c3d14c8STreehugger Robot #if SANITIZER_LINUX
30*7c3d14c8STreehugger Robot #include <sys/utsname.h>
31*7c3d14c8STreehugger Robot #endif
32*7c3d14c8STreehugger Robot
33*7c3d14c8STreehugger Robot #if SANITIZER_LINUX && !SANITIZER_ANDROID
34*7c3d14c8STreehugger Robot #include <sys/personality.h>
35*7c3d14c8STreehugger Robot #endif
36*7c3d14c8STreehugger Robot
37*7c3d14c8STreehugger Robot #if SANITIZER_FREEBSD
38*7c3d14c8STreehugger Robot // The MAP_NORESERVE define has been removed in FreeBSD 11.x, and even before
39*7c3d14c8STreehugger Robot // that, it was never implemented. So just define it to zero.
40*7c3d14c8STreehugger Robot #undef MAP_NORESERVE
41*7c3d14c8STreehugger Robot #define MAP_NORESERVE 0
42*7c3d14c8STreehugger Robot #endif
43*7c3d14c8STreehugger Robot
44*7c3d14c8STreehugger Robot namespace __sanitizer {
45*7c3d14c8STreehugger Robot
46*7c3d14c8STreehugger Robot // ------------- sanitizer_common.h
GetMmapGranularity()47*7c3d14c8STreehugger Robot uptr GetMmapGranularity() {
48*7c3d14c8STreehugger Robot return GetPageSize();
49*7c3d14c8STreehugger Robot }
50*7c3d14c8STreehugger Robot
51*7c3d14c8STreehugger Robot #if SANITIZER_WORDSIZE == 32
52*7c3d14c8STreehugger Robot // Take care of unusable kernel area in top gigabyte.
GetKernelAreaSize()53*7c3d14c8STreehugger Robot static uptr GetKernelAreaSize() {
54*7c3d14c8STreehugger Robot #if SANITIZER_LINUX && !SANITIZER_X32
55*7c3d14c8STreehugger Robot const uptr gbyte = 1UL << 30;
56*7c3d14c8STreehugger Robot
57*7c3d14c8STreehugger Robot // Firstly check if there are writable segments
58*7c3d14c8STreehugger Robot // mapped to top gigabyte (e.g. stack).
59*7c3d14c8STreehugger Robot MemoryMappingLayout proc_maps(/*cache_enabled*/true);
60*7c3d14c8STreehugger Robot uptr end, prot;
61*7c3d14c8STreehugger Robot while (proc_maps.Next(/*start*/nullptr, &end,
62*7c3d14c8STreehugger Robot /*offset*/nullptr, /*filename*/nullptr,
63*7c3d14c8STreehugger Robot /*filename_size*/0, &prot)) {
64*7c3d14c8STreehugger Robot if ((end >= 3 * gbyte)
65*7c3d14c8STreehugger Robot && (prot & MemoryMappingLayout::kProtectionWrite) != 0)
66*7c3d14c8STreehugger Robot return 0;
67*7c3d14c8STreehugger Robot }
68*7c3d14c8STreehugger Robot
69*7c3d14c8STreehugger Robot #if !SANITIZER_ANDROID
70*7c3d14c8STreehugger Robot // Even if nothing is mapped, top Gb may still be accessible
71*7c3d14c8STreehugger Robot // if we are running on 64-bit kernel.
72*7c3d14c8STreehugger Robot // Uname may report misleading results if personality type
73*7c3d14c8STreehugger Robot // is modified (e.g. under schroot) so check this as well.
74*7c3d14c8STreehugger Robot struct utsname uname_info;
75*7c3d14c8STreehugger Robot int pers = personality(0xffffffffUL);
76*7c3d14c8STreehugger Robot if (!(pers & PER_MASK)
77*7c3d14c8STreehugger Robot && uname(&uname_info) == 0
78*7c3d14c8STreehugger Robot && internal_strstr(uname_info.machine, "64"))
79*7c3d14c8STreehugger Robot return 0;
80*7c3d14c8STreehugger Robot #endif // SANITIZER_ANDROID
81*7c3d14c8STreehugger Robot
82*7c3d14c8STreehugger Robot // Top gigabyte is reserved for kernel.
83*7c3d14c8STreehugger Robot return gbyte;
84*7c3d14c8STreehugger Robot #else
85*7c3d14c8STreehugger Robot return 0;
86*7c3d14c8STreehugger Robot #endif // SANITIZER_LINUX && !SANITIZER_X32
87*7c3d14c8STreehugger Robot }
88*7c3d14c8STreehugger Robot #endif // SANITIZER_WORDSIZE == 32
89*7c3d14c8STreehugger Robot
GetMaxVirtualAddress()90*7c3d14c8STreehugger Robot uptr GetMaxVirtualAddress() {
91*7c3d14c8STreehugger Robot #if SANITIZER_WORDSIZE == 64
92*7c3d14c8STreehugger Robot # if defined(__aarch64__) && SANITIZER_IOS && !SANITIZER_IOSSIM
93*7c3d14c8STreehugger Robot // Ideally, we would derive the upper bound from MACH_VM_MAX_ADDRESS. The
94*7c3d14c8STreehugger Robot // upper bound can change depending on the device.
95*7c3d14c8STreehugger Robot return 0x200000000 - 1;
96*7c3d14c8STreehugger Robot # elif defined(__powerpc64__) || defined(__aarch64__)
97*7c3d14c8STreehugger Robot // On PowerPC64 we have two different address space layouts: 44- and 46-bit.
98*7c3d14c8STreehugger Robot // We somehow need to figure out which one we are using now and choose
99*7c3d14c8STreehugger Robot // one of 0x00000fffffffffffUL and 0x00003fffffffffffUL.
100*7c3d14c8STreehugger Robot // Note that with 'ulimit -s unlimited' the stack is moved away from the top
101*7c3d14c8STreehugger Robot // of the address space, so simply checking the stack address is not enough.
102*7c3d14c8STreehugger Robot // This should (does) work for both PowerPC64 Endian modes.
103*7c3d14c8STreehugger Robot // Similarly, aarch64 has multiple address space layouts: 39, 42 and 47-bit.
104*7c3d14c8STreehugger Robot return (1ULL << (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1)) - 1;
105*7c3d14c8STreehugger Robot # elif defined(__mips64)
106*7c3d14c8STreehugger Robot return (1ULL << 40) - 1; // 0x000000ffffffffffUL;
107*7c3d14c8STreehugger Robot # elif defined(__s390x__)
108*7c3d14c8STreehugger Robot return (1ULL << 53) - 1; // 0x001fffffffffffffUL;
109*7c3d14c8STreehugger Robot # else
110*7c3d14c8STreehugger Robot return (1ULL << 47) - 1; // 0x00007fffffffffffUL;
111*7c3d14c8STreehugger Robot # endif
112*7c3d14c8STreehugger Robot #else // SANITIZER_WORDSIZE == 32
113*7c3d14c8STreehugger Robot # if defined(__s390__)
114*7c3d14c8STreehugger Robot return (1ULL << 31) - 1; // 0x7fffffff;
115*7c3d14c8STreehugger Robot # else
116*7c3d14c8STreehugger Robot uptr res = (1ULL << 32) - 1; // 0xffffffff;
117*7c3d14c8STreehugger Robot if (!common_flags()->full_address_space)
118*7c3d14c8STreehugger Robot res -= GetKernelAreaSize();
119*7c3d14c8STreehugger Robot CHECK_LT(reinterpret_cast<uptr>(&res), res);
120*7c3d14c8STreehugger Robot return res;
121*7c3d14c8STreehugger Robot # endif
122*7c3d14c8STreehugger Robot #endif // SANITIZER_WORDSIZE
123*7c3d14c8STreehugger Robot }
124*7c3d14c8STreehugger Robot
MmapOrDie(uptr size,const char * mem_type,bool raw_report)125*7c3d14c8STreehugger Robot void *MmapOrDie(uptr size, const char *mem_type, bool raw_report) {
126*7c3d14c8STreehugger Robot size = RoundUpTo(size, GetPageSizeCached());
127*7c3d14c8STreehugger Robot uptr res = internal_mmap(nullptr, size,
128*7c3d14c8STreehugger Robot PROT_READ | PROT_WRITE,
129*7c3d14c8STreehugger Robot MAP_PRIVATE | MAP_ANON, -1, 0);
130*7c3d14c8STreehugger Robot int reserrno;
131*7c3d14c8STreehugger Robot if (internal_iserror(res, &reserrno))
132*7c3d14c8STreehugger Robot ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno, raw_report);
133*7c3d14c8STreehugger Robot IncreaseTotalMmap(size);
134*7c3d14c8STreehugger Robot return (void *)res;
135*7c3d14c8STreehugger Robot }
136*7c3d14c8STreehugger Robot
UnmapOrDie(void * addr,uptr size)137*7c3d14c8STreehugger Robot void UnmapOrDie(void *addr, uptr size) {
138*7c3d14c8STreehugger Robot if (!addr || !size) return;
139*7c3d14c8STreehugger Robot uptr res = internal_munmap(addr, size);
140*7c3d14c8STreehugger Robot if (internal_iserror(res)) {
141*7c3d14c8STreehugger Robot Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n",
142*7c3d14c8STreehugger Robot SanitizerToolName, size, size, addr);
143*7c3d14c8STreehugger Robot CHECK("unable to unmap" && 0);
144*7c3d14c8STreehugger Robot }
145*7c3d14c8STreehugger Robot DecreaseTotalMmap(size);
146*7c3d14c8STreehugger Robot }
147*7c3d14c8STreehugger Robot
148*7c3d14c8STreehugger Robot // We want to map a chunk of address space aligned to 'alignment'.
149*7c3d14c8STreehugger Robot // We do it by maping a bit more and then unmaping redundant pieces.
150*7c3d14c8STreehugger Robot // We probably can do it with fewer syscalls in some OS-dependent way.
MmapAlignedOrDie(uptr size,uptr alignment,const char * mem_type)151*7c3d14c8STreehugger Robot void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type) {
152*7c3d14c8STreehugger Robot CHECK(IsPowerOfTwo(size));
153*7c3d14c8STreehugger Robot CHECK(IsPowerOfTwo(alignment));
154*7c3d14c8STreehugger Robot uptr map_size = size + alignment;
155*7c3d14c8STreehugger Robot uptr map_res = (uptr)MmapOrDie(map_size, mem_type);
156*7c3d14c8STreehugger Robot uptr map_end = map_res + map_size;
157*7c3d14c8STreehugger Robot uptr res = map_res;
158*7c3d14c8STreehugger Robot if (res & (alignment - 1)) // Not aligned.
159*7c3d14c8STreehugger Robot res = (map_res + alignment) & ~(alignment - 1);
160*7c3d14c8STreehugger Robot uptr end = res + size;
161*7c3d14c8STreehugger Robot if (res != map_res)
162*7c3d14c8STreehugger Robot UnmapOrDie((void*)map_res, res - map_res);
163*7c3d14c8STreehugger Robot if (end != map_end)
164*7c3d14c8STreehugger Robot UnmapOrDie((void*)end, map_end - end);
165*7c3d14c8STreehugger Robot return (void*)res;
166*7c3d14c8STreehugger Robot }
167*7c3d14c8STreehugger Robot
MmapNoReserveOrDie(uptr size,const char * mem_type)168*7c3d14c8STreehugger Robot void *MmapNoReserveOrDie(uptr size, const char *mem_type) {
169*7c3d14c8STreehugger Robot uptr PageSize = GetPageSizeCached();
170*7c3d14c8STreehugger Robot uptr p = internal_mmap(nullptr,
171*7c3d14c8STreehugger Robot RoundUpTo(size, PageSize),
172*7c3d14c8STreehugger Robot PROT_READ | PROT_WRITE,
173*7c3d14c8STreehugger Robot MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
174*7c3d14c8STreehugger Robot -1, 0);
175*7c3d14c8STreehugger Robot int reserrno;
176*7c3d14c8STreehugger Robot if (internal_iserror(p, &reserrno))
177*7c3d14c8STreehugger Robot ReportMmapFailureAndDie(size, mem_type, "allocate noreserve", reserrno);
178*7c3d14c8STreehugger Robot IncreaseTotalMmap(size);
179*7c3d14c8STreehugger Robot return (void *)p;
180*7c3d14c8STreehugger Robot }
181*7c3d14c8STreehugger Robot
MmapFixedOrDie(uptr fixed_addr,uptr size)182*7c3d14c8STreehugger Robot void *MmapFixedOrDie(uptr fixed_addr, uptr size) {
183*7c3d14c8STreehugger Robot uptr PageSize = GetPageSizeCached();
184*7c3d14c8STreehugger Robot uptr p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)),
185*7c3d14c8STreehugger Robot RoundUpTo(size, PageSize),
186*7c3d14c8STreehugger Robot PROT_READ | PROT_WRITE,
187*7c3d14c8STreehugger Robot MAP_PRIVATE | MAP_ANON | MAP_FIXED,
188*7c3d14c8STreehugger Robot -1, 0);
189*7c3d14c8STreehugger Robot int reserrno;
190*7c3d14c8STreehugger Robot if (internal_iserror(p, &reserrno)) {
191*7c3d14c8STreehugger Robot char mem_type[30];
192*7c3d14c8STreehugger Robot internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
193*7c3d14c8STreehugger Robot fixed_addr);
194*7c3d14c8STreehugger Robot ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno);
195*7c3d14c8STreehugger Robot }
196*7c3d14c8STreehugger Robot IncreaseTotalMmap(size);
197*7c3d14c8STreehugger Robot return (void *)p;
198*7c3d14c8STreehugger Robot }
199*7c3d14c8STreehugger Robot
MprotectNoAccess(uptr addr,uptr size)200*7c3d14c8STreehugger Robot bool MprotectNoAccess(uptr addr, uptr size) {
201*7c3d14c8STreehugger Robot return 0 == internal_mprotect((void*)addr, size, PROT_NONE);
202*7c3d14c8STreehugger Robot }
203*7c3d14c8STreehugger Robot
MprotectReadOnly(uptr addr,uptr size)204*7c3d14c8STreehugger Robot bool MprotectReadOnly(uptr addr, uptr size) {
205*7c3d14c8STreehugger Robot return 0 == internal_mprotect((void *)addr, size, PROT_READ);
206*7c3d14c8STreehugger Robot }
207*7c3d14c8STreehugger Robot
OpenFile(const char * filename,FileAccessMode mode,error_t * errno_p)208*7c3d14c8STreehugger Robot fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *errno_p) {
209*7c3d14c8STreehugger Robot int flags;
210*7c3d14c8STreehugger Robot switch (mode) {
211*7c3d14c8STreehugger Robot case RdOnly: flags = O_RDONLY; break;
212*7c3d14c8STreehugger Robot case WrOnly: flags = O_WRONLY | O_CREAT; break;
213*7c3d14c8STreehugger Robot case RdWr: flags = O_RDWR | O_CREAT; break;
214*7c3d14c8STreehugger Robot }
215*7c3d14c8STreehugger Robot fd_t res = internal_open(filename, flags, 0660);
216*7c3d14c8STreehugger Robot if (internal_iserror(res, errno_p))
217*7c3d14c8STreehugger Robot return kInvalidFd;
218*7c3d14c8STreehugger Robot return res;
219*7c3d14c8STreehugger Robot }
220*7c3d14c8STreehugger Robot
CloseFile(fd_t fd)221*7c3d14c8STreehugger Robot void CloseFile(fd_t fd) {
222*7c3d14c8STreehugger Robot internal_close(fd);
223*7c3d14c8STreehugger Robot }
224*7c3d14c8STreehugger Robot
ReadFromFile(fd_t fd,void * buff,uptr buff_size,uptr * bytes_read,error_t * error_p)225*7c3d14c8STreehugger Robot bool ReadFromFile(fd_t fd, void *buff, uptr buff_size, uptr *bytes_read,
226*7c3d14c8STreehugger Robot error_t *error_p) {
227*7c3d14c8STreehugger Robot uptr res = internal_read(fd, buff, buff_size);
228*7c3d14c8STreehugger Robot if (internal_iserror(res, error_p))
229*7c3d14c8STreehugger Robot return false;
230*7c3d14c8STreehugger Robot if (bytes_read)
231*7c3d14c8STreehugger Robot *bytes_read = res;
232*7c3d14c8STreehugger Robot return true;
233*7c3d14c8STreehugger Robot }
234*7c3d14c8STreehugger Robot
WriteToFile(fd_t fd,const void * buff,uptr buff_size,uptr * bytes_written,error_t * error_p)235*7c3d14c8STreehugger Robot bool WriteToFile(fd_t fd, const void *buff, uptr buff_size, uptr *bytes_written,
236*7c3d14c8STreehugger Robot error_t *error_p) {
237*7c3d14c8STreehugger Robot uptr res = internal_write(fd, buff, buff_size);
238*7c3d14c8STreehugger Robot if (internal_iserror(res, error_p))
239*7c3d14c8STreehugger Robot return false;
240*7c3d14c8STreehugger Robot if (bytes_written)
241*7c3d14c8STreehugger Robot *bytes_written = res;
242*7c3d14c8STreehugger Robot return true;
243*7c3d14c8STreehugger Robot }
244*7c3d14c8STreehugger Robot
RenameFile(const char * oldpath,const char * newpath,error_t * error_p)245*7c3d14c8STreehugger Robot bool RenameFile(const char *oldpath, const char *newpath, error_t *error_p) {
246*7c3d14c8STreehugger Robot uptr res = internal_rename(oldpath, newpath);
247*7c3d14c8STreehugger Robot return !internal_iserror(res, error_p);
248*7c3d14c8STreehugger Robot }
249*7c3d14c8STreehugger Robot
MapFileToMemory(const char * file_name,uptr * buff_size)250*7c3d14c8STreehugger Robot void *MapFileToMemory(const char *file_name, uptr *buff_size) {
251*7c3d14c8STreehugger Robot fd_t fd = OpenFile(file_name, RdOnly);
252*7c3d14c8STreehugger Robot CHECK(fd != kInvalidFd);
253*7c3d14c8STreehugger Robot uptr fsize = internal_filesize(fd);
254*7c3d14c8STreehugger Robot CHECK_NE(fsize, (uptr)-1);
255*7c3d14c8STreehugger Robot CHECK_GT(fsize, 0);
256*7c3d14c8STreehugger Robot *buff_size = RoundUpTo(fsize, GetPageSizeCached());
257*7c3d14c8STreehugger Robot uptr map = internal_mmap(nullptr, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0);
258*7c3d14c8STreehugger Robot return internal_iserror(map) ? nullptr : (void *)map;
259*7c3d14c8STreehugger Robot }
260*7c3d14c8STreehugger Robot
MapWritableFileToMemory(void * addr,uptr size,fd_t fd,OFF_T offset)261*7c3d14c8STreehugger Robot void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset) {
262*7c3d14c8STreehugger Robot uptr flags = MAP_SHARED;
263*7c3d14c8STreehugger Robot if (addr) flags |= MAP_FIXED;
264*7c3d14c8STreehugger Robot uptr p = internal_mmap(addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
265*7c3d14c8STreehugger Robot int mmap_errno = 0;
266*7c3d14c8STreehugger Robot if (internal_iserror(p, &mmap_errno)) {
267*7c3d14c8STreehugger Robot Printf("could not map writable file (%d, %lld, %zu): %zd, errno: %d\n",
268*7c3d14c8STreehugger Robot fd, (long long)offset, size, p, mmap_errno);
269*7c3d14c8STreehugger Robot return nullptr;
270*7c3d14c8STreehugger Robot }
271*7c3d14c8STreehugger Robot return (void *)p;
272*7c3d14c8STreehugger Robot }
273*7c3d14c8STreehugger Robot
IntervalsAreSeparate(uptr start1,uptr end1,uptr start2,uptr end2)274*7c3d14c8STreehugger Robot static inline bool IntervalsAreSeparate(uptr start1, uptr end1,
275*7c3d14c8STreehugger Robot uptr start2, uptr end2) {
276*7c3d14c8STreehugger Robot CHECK(start1 <= end1);
277*7c3d14c8STreehugger Robot CHECK(start2 <= end2);
278*7c3d14c8STreehugger Robot return (end1 < start2) || (end2 < start1);
279*7c3d14c8STreehugger Robot }
280*7c3d14c8STreehugger Robot
281*7c3d14c8STreehugger Robot // FIXME: this is thread-unsafe, but should not cause problems most of the time.
282*7c3d14c8STreehugger Robot // When the shadow is mapped only a single thread usually exists (plus maybe
283*7c3d14c8STreehugger Robot // several worker threads on Mac, which aren't expected to map big chunks of
284*7c3d14c8STreehugger Robot // memory).
MemoryRangeIsAvailable(uptr range_start,uptr range_end)285*7c3d14c8STreehugger Robot bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
286*7c3d14c8STreehugger Robot MemoryMappingLayout proc_maps(/*cache_enabled*/true);
287*7c3d14c8STreehugger Robot uptr start, end;
288*7c3d14c8STreehugger Robot while (proc_maps.Next(&start, &end,
289*7c3d14c8STreehugger Robot /*offset*/nullptr, /*filename*/nullptr,
290*7c3d14c8STreehugger Robot /*filename_size*/0, /*protection*/nullptr)) {
291*7c3d14c8STreehugger Robot if (start == end) continue; // Empty range.
292*7c3d14c8STreehugger Robot CHECK_NE(0, end);
293*7c3d14c8STreehugger Robot if (!IntervalsAreSeparate(start, end - 1, range_start, range_end))
294*7c3d14c8STreehugger Robot return false;
295*7c3d14c8STreehugger Robot }
296*7c3d14c8STreehugger Robot return true;
297*7c3d14c8STreehugger Robot }
298*7c3d14c8STreehugger Robot
DumpProcessMap()299*7c3d14c8STreehugger Robot void DumpProcessMap() {
300*7c3d14c8STreehugger Robot MemoryMappingLayout proc_maps(/*cache_enabled*/true);
301*7c3d14c8STreehugger Robot uptr start, end;
302*7c3d14c8STreehugger Robot const sptr kBufSize = 4095;
303*7c3d14c8STreehugger Robot char *filename = (char*)MmapOrDie(kBufSize, __func__);
304*7c3d14c8STreehugger Robot Report("Process memory map follows:\n");
305*7c3d14c8STreehugger Robot while (proc_maps.Next(&start, &end, /* file_offset */nullptr,
306*7c3d14c8STreehugger Robot filename, kBufSize, /* protection */nullptr)) {
307*7c3d14c8STreehugger Robot Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename);
308*7c3d14c8STreehugger Robot }
309*7c3d14c8STreehugger Robot Report("End of process memory map.\n");
310*7c3d14c8STreehugger Robot UnmapOrDie(filename, kBufSize);
311*7c3d14c8STreehugger Robot }
312*7c3d14c8STreehugger Robot
GetPwd()313*7c3d14c8STreehugger Robot const char *GetPwd() {
314*7c3d14c8STreehugger Robot return GetEnv("PWD");
315*7c3d14c8STreehugger Robot }
316*7c3d14c8STreehugger Robot
IsPathSeparator(const char c)317*7c3d14c8STreehugger Robot bool IsPathSeparator(const char c) {
318*7c3d14c8STreehugger Robot return c == '/';
319*7c3d14c8STreehugger Robot }
320*7c3d14c8STreehugger Robot
IsAbsolutePath(const char * path)321*7c3d14c8STreehugger Robot bool IsAbsolutePath(const char *path) {
322*7c3d14c8STreehugger Robot return path != nullptr && IsPathSeparator(path[0]);
323*7c3d14c8STreehugger Robot }
324*7c3d14c8STreehugger Robot
Write(const char * buffer,uptr length)325*7c3d14c8STreehugger Robot void ReportFile::Write(const char *buffer, uptr length) {
326*7c3d14c8STreehugger Robot SpinMutexLock l(mu);
327*7c3d14c8STreehugger Robot static const char *kWriteError =
328*7c3d14c8STreehugger Robot "ReportFile::Write() can't output requested buffer!\n";
329*7c3d14c8STreehugger Robot ReopenIfNecessary();
330*7c3d14c8STreehugger Robot if (length != internal_write(fd, buffer, length)) {
331*7c3d14c8STreehugger Robot internal_write(fd, kWriteError, internal_strlen(kWriteError));
332*7c3d14c8STreehugger Robot Die();
333*7c3d14c8STreehugger Robot }
334*7c3d14c8STreehugger Robot }
335*7c3d14c8STreehugger Robot
GetCodeRangeForFile(const char * module,uptr * start,uptr * end)336*7c3d14c8STreehugger Robot bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end) {
337*7c3d14c8STreehugger Robot uptr s, e, off, prot;
338*7c3d14c8STreehugger Robot InternalScopedString buff(kMaxPathLength);
339*7c3d14c8STreehugger Robot MemoryMappingLayout proc_maps(/*cache_enabled*/false);
340*7c3d14c8STreehugger Robot while (proc_maps.Next(&s, &e, &off, buff.data(), buff.size(), &prot)) {
341*7c3d14c8STreehugger Robot if ((prot & MemoryMappingLayout::kProtectionExecute) != 0
342*7c3d14c8STreehugger Robot && internal_strcmp(module, buff.data()) == 0) {
343*7c3d14c8STreehugger Robot *start = s;
344*7c3d14c8STreehugger Robot *end = e;
345*7c3d14c8STreehugger Robot return true;
346*7c3d14c8STreehugger Robot }
347*7c3d14c8STreehugger Robot }
348*7c3d14c8STreehugger Robot return false;
349*7c3d14c8STreehugger Robot }
350*7c3d14c8STreehugger Robot
Create(void * siginfo,void * context)351*7c3d14c8STreehugger Robot SignalContext SignalContext::Create(void *siginfo, void *context) {
352*7c3d14c8STreehugger Robot auto si = (siginfo_t *)siginfo;
353*7c3d14c8STreehugger Robot uptr addr = (uptr)si->si_addr;
354*7c3d14c8STreehugger Robot uptr pc, sp, bp;
355*7c3d14c8STreehugger Robot GetPcSpBp(context, &pc, &sp, &bp);
356*7c3d14c8STreehugger Robot WriteFlag write_flag = GetWriteFlag(context);
357*7c3d14c8STreehugger Robot bool is_memory_access = si->si_signo == SIGSEGV;
358*7c3d14c8STreehugger Robot return SignalContext(context, addr, pc, sp, bp, is_memory_access, write_flag);
359*7c3d14c8STreehugger Robot }
360*7c3d14c8STreehugger Robot
361*7c3d14c8STreehugger Robot } // namespace __sanitizer
362*7c3d14c8STreehugger Robot
363*7c3d14c8STreehugger Robot #endif // SANITIZER_POSIX
364