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