xref: /freebsd/contrib/llvm-project/compiler-rt/lib/scudo/standalone/linux.cpp (revision 0d8fe2373503aeac48492f28073049a8bfa4feb5)
1 //===-- linux.cpp -----------------------------------------------*- C++ -*-===//
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 #include "platform.h"
10 
11 #if SCUDO_LINUX
12 
13 #include "common.h"
14 #include "linux.h"
15 #include "mutex.h"
16 #include "string_utils.h"
17 
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <linux/futex.h>
21 #include <sched.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/mman.h>
25 #include <sys/stat.h>
26 #include <sys/syscall.h>
27 #include <sys/time.h>
28 #include <time.h>
29 #include <unistd.h>
30 
31 #if SCUDO_ANDROID
32 #include <sys/prctl.h>
33 // Definitions of prctl arguments to set a vma name in Android kernels.
34 #define ANDROID_PR_SET_VMA 0x53564d41
35 #define ANDROID_PR_SET_VMA_ANON_NAME 0
36 #endif
37 
38 namespace scudo {
39 
40 uptr getPageSize() { return static_cast<uptr>(sysconf(_SC_PAGESIZE)); }
41 
42 void NORETURN die() { abort(); }
43 
44 void *map(void *Addr, uptr Size, UNUSED const char *Name, uptr Flags,
45           UNUSED MapPlatformData *Data) {
46   int MmapFlags = MAP_PRIVATE | MAP_ANONYMOUS;
47   int MmapProt;
48   if (Flags & MAP_NOACCESS) {
49     MmapFlags |= MAP_NORESERVE;
50     MmapProt = PROT_NONE;
51   } else {
52     MmapProt = PROT_READ | PROT_WRITE;
53 #if defined(__aarch64__)
54 #ifndef PROT_MTE
55 #define PROT_MTE 0x20
56 #endif
57     if (Flags & MAP_MEMTAG)
58       MmapProt |= PROT_MTE;
59 #endif
60   }
61   if (Addr) {
62     // Currently no scenario for a noaccess mapping with a fixed address.
63     DCHECK_EQ(Flags & MAP_NOACCESS, 0);
64     MmapFlags |= MAP_FIXED;
65   }
66   void *P = mmap(Addr, Size, MmapProt, MmapFlags, -1, 0);
67   if (P == MAP_FAILED) {
68     if (!(Flags & MAP_ALLOWNOMEM) || errno != ENOMEM)
69       dieOnMapUnmapError(errno == ENOMEM);
70     return nullptr;
71   }
72 #if SCUDO_ANDROID
73   if (!(Flags & MAP_NOACCESS))
74     prctl(ANDROID_PR_SET_VMA, ANDROID_PR_SET_VMA_ANON_NAME, P, Size, Name);
75 #endif
76   return P;
77 }
78 
79 void unmap(void *Addr, uptr Size, UNUSED uptr Flags,
80            UNUSED MapPlatformData *Data) {
81   if (munmap(Addr, Size) != 0)
82     dieOnMapUnmapError();
83 }
84 
85 void releasePagesToOS(uptr BaseAddress, uptr Offset, uptr Size,
86                       UNUSED MapPlatformData *Data) {
87   void *Addr = reinterpret_cast<void *>(BaseAddress + Offset);
88   while (madvise(Addr, Size, MADV_DONTNEED) == -1 && errno == EAGAIN) {
89   }
90 }
91 
92 // Calling getenv should be fine (c)(tm) at any time.
93 const char *getEnv(const char *Name) { return getenv(Name); }
94 
95 namespace {
96 enum State : u32 { Unlocked = 0, Locked = 1, Sleeping = 2 };
97 }
98 
99 bool HybridMutex::tryLock() {
100   return atomic_compare_exchange(&M, Unlocked, Locked) == Unlocked;
101 }
102 
103 // The following is based on https://akkadia.org/drepper/futex.pdf.
104 void HybridMutex::lockSlow() {
105   u32 V = atomic_compare_exchange(&M, Unlocked, Locked);
106   if (V == Unlocked)
107     return;
108   if (V != Sleeping)
109     V = atomic_exchange(&M, Sleeping, memory_order_acquire);
110   while (V != Unlocked) {
111     syscall(SYS_futex, reinterpret_cast<uptr>(&M), FUTEX_WAIT_PRIVATE, Sleeping,
112             nullptr, nullptr, 0);
113     V = atomic_exchange(&M, Sleeping, memory_order_acquire);
114   }
115 }
116 
117 void HybridMutex::unlock() {
118   if (atomic_fetch_sub(&M, 1U, memory_order_release) != Locked) {
119     atomic_store(&M, Unlocked, memory_order_release);
120     syscall(SYS_futex, reinterpret_cast<uptr>(&M), FUTEX_WAKE_PRIVATE, 1,
121             nullptr, nullptr, 0);
122   }
123 }
124 
125 u64 getMonotonicTime() {
126   timespec TS;
127   clock_gettime(CLOCK_MONOTONIC, &TS);
128   return static_cast<u64>(TS.tv_sec) * (1000ULL * 1000 * 1000) +
129          static_cast<u64>(TS.tv_nsec);
130 }
131 
132 u32 getNumberOfCPUs() {
133   cpu_set_t CPUs;
134   // sched_getaffinity can fail for a variety of legitimate reasons (lack of
135   // CAP_SYS_NICE, syscall filtering, etc), in which case we shall return 0.
136   if (sched_getaffinity(0, sizeof(cpu_set_t), &CPUs) != 0)
137     return 0;
138   return static_cast<u32>(CPU_COUNT(&CPUs));
139 }
140 
141 u32 getThreadID() {
142 #if SCUDO_ANDROID
143   return static_cast<u32>(gettid());
144 #else
145   return static_cast<u32>(syscall(SYS_gettid));
146 #endif
147 }
148 
149 // Blocking is possibly unused if the getrandom block is not compiled in.
150 bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) {
151   if (!Buffer || !Length || Length > MaxRandomLength)
152     return false;
153   ssize_t ReadBytes;
154 #if defined(SYS_getrandom)
155 #if !defined(GRND_NONBLOCK)
156 #define GRND_NONBLOCK 1
157 #endif
158   // Up to 256 bytes, getrandom will not be interrupted.
159   ReadBytes =
160       syscall(SYS_getrandom, Buffer, Length, Blocking ? 0 : GRND_NONBLOCK);
161   if (ReadBytes == static_cast<ssize_t>(Length))
162     return true;
163 #endif // defined(SYS_getrandom)
164   // Up to 256 bytes, a read off /dev/urandom will not be interrupted.
165   // Blocking is moot here, O_NONBLOCK has no effect when opening /dev/urandom.
166   const int FileDesc = open("/dev/urandom", O_RDONLY);
167   if (FileDesc == -1)
168     return false;
169   ReadBytes = read(FileDesc, Buffer, Length);
170   close(FileDesc);
171   return (ReadBytes == static_cast<ssize_t>(Length));
172 }
173 
174 // Allocation free syslog-like API.
175 extern "C" WEAK int async_safe_write_log(int pri, const char *tag,
176                                          const char *msg);
177 
178 void outputRaw(const char *Buffer) {
179   if (&async_safe_write_log) {
180     constexpr s32 AndroidLogInfo = 4;
181     constexpr uptr MaxLength = 1024U;
182     char LocalBuffer[MaxLength];
183     while (strlen(Buffer) > MaxLength) {
184       uptr P;
185       for (P = MaxLength - 1; P > 0; P--) {
186         if (Buffer[P] == '\n') {
187           memcpy(LocalBuffer, Buffer, P);
188           LocalBuffer[P] = '\0';
189           async_safe_write_log(AndroidLogInfo, "scudo", LocalBuffer);
190           Buffer = &Buffer[P + 1];
191           break;
192         }
193       }
194       // If no newline was found, just log the buffer.
195       if (P == 0)
196         break;
197     }
198     async_safe_write_log(AndroidLogInfo, "scudo", Buffer);
199   } else {
200     (void)write(2, Buffer, strlen(Buffer));
201   }
202 }
203 
204 extern "C" WEAK void android_set_abort_message(const char *);
205 
206 void setAbortMessage(const char *Message) {
207   if (&android_set_abort_message)
208     android_set_abort_message(Message);
209 }
210 
211 } // namespace scudo
212 
213 #endif // SCUDO_LINUX
214