1 //===-- sanitizer_posix.cpp -----------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries and implements POSIX-specific functions from
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_platform.h"
18 #include "sanitizer_common.h"
19 #include "sanitizer_file.h"
20 #include "sanitizer_flags.h"
21 #include "sanitizer_libc.h"
22 #include "sanitizer_posix.h"
23 #include "sanitizer_procmaps.h"
31 // The MAP_NORESERVE define has been removed in FreeBSD 11.x, and even before
32 // that, it was never implemented. So just define it to zero.
34 #define MAP_NORESERVE 0
37 namespace __sanitizer
{
39 // ------------- sanitizer_common.h
40 uptr
GetMmapGranularity() {
44 void *MmapOrDie(uptr size
, const char *mem_type
, bool raw_report
) {
45 size
= RoundUpTo(size
, GetPageSizeCached());
46 uptr res
= MmapNamed(nullptr, size
, PROT_READ
| PROT_WRITE
,
47 MAP_PRIVATE
| MAP_ANON
, mem_type
);
49 if (UNLIKELY(internal_iserror(res
, &reserrno
)))
50 ReportMmapFailureAndDie(size
, mem_type
, "allocate", reserrno
, raw_report
);
51 IncreaseTotalMmap(size
);
55 void UnmapOrDie(void *addr
, uptr size
) {
56 if (!addr
|| !size
) return;
57 uptr res
= internal_munmap(addr
, size
);
58 if (UNLIKELY(internal_iserror(res
))) {
59 Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n",
60 SanitizerToolName
, size
, size
, addr
);
61 CHECK("unable to unmap" && 0);
63 DecreaseTotalMmap(size
);
66 void *MmapOrDieOnFatalError(uptr size
, const char *mem_type
) {
67 size
= RoundUpTo(size
, GetPageSizeCached());
68 uptr res
= MmapNamed(nullptr, size
, PROT_READ
| PROT_WRITE
,
69 MAP_PRIVATE
| MAP_ANON
, mem_type
);
71 if (UNLIKELY(internal_iserror(res
, &reserrno
))) {
72 if (reserrno
== ENOMEM
)
74 ReportMmapFailureAndDie(size
, mem_type
, "allocate", reserrno
);
76 IncreaseTotalMmap(size
);
80 // We want to map a chunk of address space aligned to 'alignment'.
81 // We do it by mapping a bit more and then unmapping redundant pieces.
82 // We probably can do it with fewer syscalls in some OS-dependent way.
83 void *MmapAlignedOrDieOnFatalError(uptr size
, uptr alignment
,
84 const char *mem_type
) {
85 CHECK(IsPowerOfTwo(size
));
86 CHECK(IsPowerOfTwo(alignment
));
87 uptr map_size
= size
+ alignment
;
88 uptr map_res
= (uptr
)MmapOrDieOnFatalError(map_size
, mem_type
);
89 if (UNLIKELY(!map_res
))
91 uptr map_end
= map_res
+ map_size
;
93 if (!IsAligned(res
, alignment
)) {
94 res
= (map_res
+ alignment
- 1) & ~(alignment
- 1);
95 UnmapOrDie((void*)map_res
, res
- map_res
);
97 uptr end
= res
+ size
;
98 end
= RoundUpTo(end
, GetPageSizeCached());
100 UnmapOrDie((void*)end
, map_end
- end
);
104 void *MmapNoReserveOrDie(uptr size
, const char *mem_type
) {
105 size
= RoundUpTo(size
, GetPageSizeCached());
106 uptr p
= MmapNamed(nullptr, size
, PROT_READ
| PROT_WRITE
,
107 MAP_PRIVATE
| MAP_ANON
| MAP_NORESERVE
, mem_type
);
109 if (UNLIKELY(internal_iserror(p
, &reserrno
)))
110 ReportMmapFailureAndDie(size
, mem_type
, "allocate noreserve", reserrno
);
111 IncreaseTotalMmap(size
);
115 static void *MmapFixedImpl(uptr fixed_addr
, uptr size
, bool tolerate_enomem
,
117 size
= RoundUpTo(size
, GetPageSizeCached());
118 fixed_addr
= RoundDownTo(fixed_addr
, GetPageSizeCached());
119 uptr p
= MmapNamed((void *)fixed_addr
, size
, PROT_READ
| PROT_WRITE
,
120 MAP_PRIVATE
| MAP_ANON
| MAP_FIXED
, name
);
122 if (UNLIKELY(internal_iserror(p
, &reserrno
))) {
123 if (tolerate_enomem
&& reserrno
== ENOMEM
)
126 internal_snprintf(mem_type
, sizeof(mem_type
), "memory at address 0x%zx",
128 ReportMmapFailureAndDie(size
, mem_type
, "allocate", reserrno
);
130 IncreaseTotalMmap(size
);
134 void *MmapFixedOrDie(uptr fixed_addr
, uptr size
, const char *name
) {
135 return MmapFixedImpl(fixed_addr
, size
, false /*tolerate_enomem*/, name
);
138 void *MmapFixedOrDieOnFatalError(uptr fixed_addr
, uptr size
, const char *name
) {
139 return MmapFixedImpl(fixed_addr
, size
, true /*tolerate_enomem*/, name
);
142 bool MprotectNoAccess(uptr addr
, uptr size
) {
143 return 0 == internal_mprotect((void*)addr
, size
, PROT_NONE
);
146 bool MprotectReadOnly(uptr addr
, uptr size
) {
147 return 0 == internal_mprotect((void *)addr
, size
, PROT_READ
);
151 void MprotectMallocZones(void *addr
, int prot
) {}
154 fd_t
OpenFile(const char *filename
, FileAccessMode mode
, error_t
*errno_p
) {
155 if (ShouldMockFailureToOpen(filename
))
159 case RdOnly
: flags
= O_RDONLY
; break;
160 case WrOnly
: flags
= O_WRONLY
| O_CREAT
| O_TRUNC
; break;
161 case RdWr
: flags
= O_RDWR
| O_CREAT
; break;
163 fd_t res
= internal_open(filename
, flags
, 0660);
164 if (internal_iserror(res
, errno_p
))
166 return ReserveStandardFds(res
);
169 void CloseFile(fd_t fd
) {
173 bool ReadFromFile(fd_t fd
, void *buff
, uptr buff_size
, uptr
*bytes_read
,
175 uptr res
= internal_read(fd
, buff
, buff_size
);
176 if (internal_iserror(res
, error_p
))
183 bool WriteToFile(fd_t fd
, const void *buff
, uptr buff_size
, uptr
*bytes_written
,
185 uptr res
= internal_write(fd
, buff
, buff_size
);
186 if (internal_iserror(res
, error_p
))
189 *bytes_written
= res
;
193 void *MapFileToMemory(const char *file_name
, uptr
*buff_size
) {
194 fd_t fd
= OpenFile(file_name
, RdOnly
);
195 CHECK(fd
!= kInvalidFd
);
196 uptr fsize
= internal_filesize(fd
);
197 CHECK_NE(fsize
, (uptr
)-1);
199 *buff_size
= RoundUpTo(fsize
, GetPageSizeCached());
200 uptr map
= internal_mmap(nullptr, *buff_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
201 return internal_iserror(map
) ? nullptr : (void *)map
;
204 void *MapWritableFileToMemory(void *addr
, uptr size
, fd_t fd
, OFF_T offset
) {
205 uptr flags
= MAP_SHARED
;
206 if (addr
) flags
|= MAP_FIXED
;
207 uptr p
= internal_mmap(addr
, size
, PROT_READ
| PROT_WRITE
, flags
, fd
, offset
);
209 if (internal_iserror(p
, &mmap_errno
)) {
210 Printf("could not map writable file (%d, %lld, %zu): %zd, errno: %d\n",
211 fd
, (long long)offset
, size
, p
, mmap_errno
);
217 static inline bool IntervalsAreSeparate(uptr start1
, uptr end1
,
218 uptr start2
, uptr end2
) {
219 CHECK(start1
<= end1
);
220 CHECK(start2
<= end2
);
221 return (end1
< start2
) || (end2
< start1
);
224 // FIXME: this is thread-unsafe, but should not cause problems most of the time.
225 // When the shadow is mapped only a single thread usually exists (plus maybe
226 // several worker threads on Mac, which aren't expected to map big chunks of
228 bool MemoryRangeIsAvailable(uptr range_start
, uptr range_end
) {
229 MemoryMappingLayout
proc_maps(/*cache_enabled*/true);
230 if (proc_maps
.Error())
231 return true; // and hope for the best
232 MemoryMappedSegment segment
;
233 while (proc_maps
.Next(&segment
)) {
234 if (segment
.start
== segment
.end
) continue; // Empty range.
235 CHECK_NE(0, segment
.end
);
236 if (!IntervalsAreSeparate(segment
.start
, segment
.end
- 1, range_start
,
244 void DumpProcessMap() {
245 MemoryMappingLayout
proc_maps(/*cache_enabled*/true);
246 const sptr kBufSize
= 4095;
247 char *filename
= (char*)MmapOrDie(kBufSize
, __func__
);
248 MemoryMappedSegment
segment(filename
, kBufSize
);
249 Report("Process memory map follows:\n");
250 while (proc_maps
.Next(&segment
)) {
251 Printf("\t%p-%p\t%s\n", (void *)segment
.start
, (void *)segment
.end
,
254 Report("End of process memory map.\n");
255 UnmapOrDie(filename
, kBufSize
);
259 const char *GetPwd() {
260 return GetEnv("PWD");
263 bool IsPathSeparator(const char c
) {
267 bool IsAbsolutePath(const char *path
) {
268 return path
!= nullptr && IsPathSeparator(path
[0]);
271 void ReportFile::Write(const char *buffer
, uptr length
) {
274 internal_write(fd
, buffer
, length
);
277 bool GetCodeRangeForFile(const char *module
, uptr
*start
, uptr
*end
) {
278 MemoryMappingLayout
proc_maps(/*cache_enabled*/false);
279 InternalMmapVector
<char> buff(kMaxPathLength
);
280 MemoryMappedSegment
segment(buff
.data(), buff
.size());
281 while (proc_maps
.Next(&segment
)) {
282 if (segment
.IsExecutable() &&
283 internal_strcmp(module
, segment
.filename
) == 0) {
284 *start
= segment
.start
;
292 uptr
SignalContext::GetAddress() const {
293 auto si
= static_cast<const siginfo_t
*>(siginfo
);
294 return (uptr
)si
->si_addr
;
297 bool SignalContext::IsMemoryAccess() const {
298 auto si
= static_cast<const siginfo_t
*>(siginfo
);
299 return si
->si_signo
== SIGSEGV
|| si
->si_signo
== SIGBUS
;
302 int SignalContext::GetType() const {
303 return static_cast<const siginfo_t
*>(siginfo
)->si_signo
;
306 const char *SignalContext::Describe() const {
321 return "UNKNOWN SIGNAL";
324 fd_t
ReserveStandardFds(fd_t fd
) {
329 internal_memset(used
, 0, sizeof(used
));
332 fd
= internal_dup(fd
);
334 for (int i
= 0; i
<= 2; ++i
)
340 bool ShouldMockFailureToOpen(const char *path
) {
341 return common_flags()->test_only_emulate_no_memorymap
&&
342 internal_strncmp(path
, "/proc/", 6) == 0;
345 #if SANITIZER_LINUX && !SANITIZER_ANDROID && !SANITIZER_GO
346 int GetNamedMappingFd(const char *name
, uptr size
, int *flags
) {
347 if (!common_flags()->decorate_proc_maps
|| !name
)
350 CHECK(internal_strlen(name
) < sizeof(shmname
) - 10);
351 internal_snprintf(shmname
, sizeof(shmname
), "/dev/shm/%zu [%s]",
352 internal_getpid(), name
);
354 #if defined(O_CLOEXEC)
355 o_cloexec
= O_CLOEXEC
;
357 int fd
= ReserveStandardFds(
358 internal_open(shmname
, O_RDWR
| O_CREAT
| O_TRUNC
| o_cloexec
, S_IRWXU
));
360 int res
= internal_ftruncate(fd
, size
);
361 #if !defined(O_CLOEXEC)
362 res
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
366 res
= internal_unlink(shmname
);
368 *flags
&= ~(MAP_ANON
| MAP_ANONYMOUS
);
372 int GetNamedMappingFd(const char *name
, uptr size
, int *flags
) {
377 #if SANITIZER_ANDROID
378 #define PR_SET_VMA 0x53564d41
379 #define PR_SET_VMA_ANON_NAME 0
380 void DecorateMapping(uptr addr
, uptr size
, const char *name
) {
381 if (!common_flags()->decorate_proc_maps
|| !name
)
383 internal_prctl(PR_SET_VMA
, PR_SET_VMA_ANON_NAME
, addr
, size
, (uptr
)name
);
386 void DecorateMapping(uptr addr
, uptr size
, const char *name
) {
390 uptr
MmapNamed(void *addr
, uptr length
, int prot
, int flags
, const char *name
) {
391 int fd
= GetNamedMappingFd(name
, length
, &flags
);
392 uptr res
= internal_mmap(addr
, length
, prot
, flags
, fd
, 0);
393 if (!internal_iserror(res
))
394 DecorateMapping(res
, length
, name
);
399 } // namespace __sanitizer
401 #endif // SANITIZER_POSIX