[clang][extract-api] Emit "navigator" property of "name" in SymbolGraph
[llvm-project.git] / compiler-rt / lib / sanitizer_common / sanitizer_posix.cpp
blob3b330a3705e220507e6633d88f633c2066ecd81b
1 //===-- sanitizer_posix.cpp -----------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries and implements POSIX-specific functions from
11 // sanitizer_posix.h.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_platform.h"
16 #if SANITIZER_POSIX
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"
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <sys/mman.h>
30 #if SANITIZER_FREEBSD
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.
33 #undef MAP_NORESERVE
34 #define MAP_NORESERVE 0
35 #endif
37 namespace __sanitizer {
39 // ------------- sanitizer_common.h
40 uptr GetMmapGranularity() {
41 return GetPageSize();
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);
48 int reserrno;
49 if (UNLIKELY(internal_iserror(res, &reserrno)))
50 ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno, raw_report);
51 IncreaseTotalMmap(size);
52 return (void *)res;
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);
70 int reserrno;
71 if (UNLIKELY(internal_iserror(res, &reserrno))) {
72 if (reserrno == ENOMEM)
73 return nullptr;
74 ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno);
76 IncreaseTotalMmap(size);
77 return (void *)res;
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))
90 return nullptr;
91 uptr map_end = map_res + map_size;
92 uptr res = map_res;
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());
99 if (end != map_end)
100 UnmapOrDie((void*)end, map_end - end);
101 return (void*)res;
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);
108 int reserrno;
109 if (UNLIKELY(internal_iserror(p, &reserrno)))
110 ReportMmapFailureAndDie(size, mem_type, "allocate noreserve", reserrno);
111 IncreaseTotalMmap(size);
112 return (void *)p;
115 static void *MmapFixedImpl(uptr fixed_addr, uptr size, bool tolerate_enomem,
116 const char *name) {
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);
121 int reserrno;
122 if (UNLIKELY(internal_iserror(p, &reserrno))) {
123 if (tolerate_enomem && reserrno == ENOMEM)
124 return nullptr;
125 char mem_type[40];
126 internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
127 fixed_addr);
128 ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno);
130 IncreaseTotalMmap(size);
131 return (void *)p;
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);
150 #if !SANITIZER_MAC
151 void MprotectMallocZones(void *addr, int prot) {}
152 #endif
154 fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *errno_p) {
155 if (ShouldMockFailureToOpen(filename))
156 return kInvalidFd;
157 int flags;
158 switch (mode) {
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))
165 return kInvalidFd;
166 return ReserveStandardFds(res);
169 void CloseFile(fd_t fd) {
170 internal_close(fd);
173 bool ReadFromFile(fd_t fd, void *buff, uptr buff_size, uptr *bytes_read,
174 error_t *error_p) {
175 uptr res = internal_read(fd, buff, buff_size);
176 if (internal_iserror(res, error_p))
177 return false;
178 if (bytes_read)
179 *bytes_read = res;
180 return true;
183 bool WriteToFile(fd_t fd, const void *buff, uptr buff_size, uptr *bytes_written,
184 error_t *error_p) {
185 uptr res = internal_write(fd, buff, buff_size);
186 if (internal_iserror(res, error_p))
187 return false;
188 if (bytes_written)
189 *bytes_written = res;
190 return true;
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);
198 CHECK_GT(fsize, 0);
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);
208 int mmap_errno = 0;
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);
212 return nullptr;
214 return (void *)p;
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
227 // memory).
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,
237 range_end))
238 return false;
240 return true;
243 #if !SANITIZER_MAC
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,
252 segment.filename);
254 Report("End of process memory map.\n");
255 UnmapOrDie(filename, kBufSize);
257 #endif
259 const char *GetPwd() {
260 return GetEnv("PWD");
263 bool IsPathSeparator(const char c) {
264 return c == '/';
267 bool IsAbsolutePath(const char *path) {
268 return path != nullptr && IsPathSeparator(path[0]);
271 void ReportFile::Write(const char *buffer, uptr length) {
272 SpinMutexLock l(mu);
273 ReopenIfNecessary();
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;
285 *end = segment.end;
286 return true;
289 return false;
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 {
307 switch (GetType()) {
308 case SIGFPE:
309 return "FPE";
310 case SIGILL:
311 return "ILL";
312 case SIGABRT:
313 return "ABRT";
314 case SIGSEGV:
315 return "SEGV";
316 case SIGBUS:
317 return "BUS";
318 case SIGTRAP:
319 return "TRAP";
321 return "UNKNOWN SIGNAL";
324 fd_t ReserveStandardFds(fd_t fd) {
325 CHECK_GE(fd, 0);
326 if (fd > 2)
327 return fd;
328 bool used[3];
329 internal_memset(used, 0, sizeof(used));
330 while (fd <= 2) {
331 used[fd] = true;
332 fd = internal_dup(fd);
334 for (int i = 0; i <= 2; ++i)
335 if (used[i])
336 internal_close(i);
337 return fd;
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)
348 return -1;
349 char shmname[200];
350 CHECK(internal_strlen(name) < sizeof(shmname) - 10);
351 internal_snprintf(shmname, sizeof(shmname), "/dev/shm/%zu [%s]",
352 internal_getpid(), name);
353 int o_cloexec = 0;
354 #if defined(O_CLOEXEC)
355 o_cloexec = O_CLOEXEC;
356 #endif
357 int fd = ReserveStandardFds(
358 internal_open(shmname, O_RDWR | O_CREAT | O_TRUNC | o_cloexec, S_IRWXU));
359 CHECK_GE(fd, 0);
360 int res = internal_ftruncate(fd, size);
361 #if !defined(O_CLOEXEC)
362 res = fcntl(fd, F_SETFD, FD_CLOEXEC);
363 CHECK_EQ(0, res);
364 #endif
365 CHECK_EQ(0, res);
366 res = internal_unlink(shmname);
367 CHECK_EQ(0, res);
368 *flags &= ~(MAP_ANON | MAP_ANONYMOUS);
369 return fd;
371 #else
372 int GetNamedMappingFd(const char *name, uptr size, int *flags) {
373 return -1;
375 #endif
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)
382 return;
383 internal_prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, addr, size, (uptr)name);
385 #else
386 void DecorateMapping(uptr addr, uptr size, const char *name) {
388 #endif
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);
395 return res;
399 } // namespace __sanitizer
401 #endif // SANITIZER_POSIX