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 "internal_defs.h"
15 #include "linux.h"
16 #include "mutex.h"
17 #include "string_utils.h"
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <linux/futex.h>
22 #include <sched.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/mman.h>
27 #include <sys/stat.h>
28 #include <sys/syscall.h>
29 #include <sys/time.h>
30 #include <time.h>
31 #include <unistd.h>
32 
33 #if SCUDO_ANDROID
34 #include <sys/prctl.h>
35 // Definitions of prctl arguments to set a vma name in Android kernels.
36 #define ANDROID_PR_SET_VMA 0x53564d41
37 #define ANDROID_PR_SET_VMA_ANON_NAME 0
38 #endif
39 
40 namespace scudo {
41 
42 uptr getPageSize() { return static_cast<uptr>(sysconf(_SC_PAGESIZE)); }
43 
44 void NORETURN die() { abort(); }
45 
46 void *map(void *Addr, uptr Size, UNUSED const char *Name, uptr Flags,
47           UNUSED MapPlatformData *Data) {
48   int MmapFlags = MAP_PRIVATE | MAP_ANONYMOUS;
49   int MmapProt;
50   if (Flags & MAP_NOACCESS) {
51     MmapFlags |= MAP_NORESERVE;
52     MmapProt = PROT_NONE;
53   } else {
54     MmapProt = PROT_READ | PROT_WRITE;
55   }
56 #if defined(__aarch64__)
57 #ifndef PROT_MTE
58 #define PROT_MTE 0x20
59 #endif
60   if (Flags & MAP_MEMTAG)
61     MmapProt |= PROT_MTE;
62 #endif
63   if (Addr)
64     MmapFlags |= MAP_FIXED;
65   void *P = mmap(Addr, Size, MmapProt, MmapFlags, -1, 0);
66   if (P == MAP_FAILED) {
67     if (!(Flags & MAP_ALLOWNOMEM) || errno != ENOMEM)
68       dieOnMapUnmapError(errno == ENOMEM ? Size : 0);
69     return nullptr;
70   }
71 #if SCUDO_ANDROID
72   if (Name)
73     prctl(ANDROID_PR_SET_VMA, ANDROID_PR_SET_VMA_ANON_NAME, P, Size, Name);
74 #endif
75   return P;
76 }
77 
78 void unmap(void *Addr, uptr Size, UNUSED uptr Flags,
79            UNUSED MapPlatformData *Data) {
80   if (munmap(Addr, Size) != 0)
81     dieOnMapUnmapError();
82 }
83 
84 void setMemoryPermission(uptr Addr, uptr Size, uptr Flags,
85                          UNUSED MapPlatformData *Data) {
86   int Prot = (Flags & MAP_NOACCESS) ? PROT_NONE : (PROT_READ | PROT_WRITE);
87   if (mprotect(reinterpret_cast<void *>(Addr), Size, Prot) != 0)
88     dieOnMapUnmapError();
89 }
90 
91 void releasePagesToOS(uptr BaseAddress, uptr Offset, uptr Size,
92                       UNUSED MapPlatformData *Data) {
93   void *Addr = reinterpret_cast<void *>(BaseAddress + Offset);
94 
95   while (madvise(Addr, Size, MADV_DONTNEED) == -1 && errno == EAGAIN) {
96   }
97 }
98 
99 // Calling getenv should be fine (c)(tm) at any time.
100 const char *getEnv(const char *Name) { return getenv(Name); }
101 
102 namespace {
103 enum State : u32 { Unlocked = 0, Locked = 1, Sleeping = 2 };
104 }
105 
106 bool HybridMutex::tryLock() {
107   return atomic_compare_exchange(&M, Unlocked, Locked) == Unlocked;
108 }
109 
110 // The following is based on https://akkadia.org/drepper/futex.pdf.
111 void HybridMutex::lockSlow() {
112   u32 V = atomic_compare_exchange(&M, Unlocked, Locked);
113   if (V == Unlocked)
114     return;
115   if (V != Sleeping)
116     V = atomic_exchange(&M, Sleeping, memory_order_acquire);
117   while (V != Unlocked) {
118     syscall(SYS_futex, reinterpret_cast<uptr>(&M), FUTEX_WAIT_PRIVATE, Sleeping,
119             nullptr, nullptr, 0);
120     V = atomic_exchange(&M, Sleeping, memory_order_acquire);
121   }
122 }
123 
124 void HybridMutex::unlock() {
125   if (atomic_fetch_sub(&M, 1U, memory_order_release) != Locked) {
126     atomic_store(&M, Unlocked, memory_order_release);
127     syscall(SYS_futex, reinterpret_cast<uptr>(&M), FUTEX_WAKE_PRIVATE, 1,
128             nullptr, nullptr, 0);
129   }
130 }
131 
132 void HybridMutex::assertHeldImpl() {
133   CHECK(atomic_load(&M, memory_order_acquire) != Unlocked);
134 }
135 
136 u64 getMonotonicTime() {
137   timespec TS;
138   clock_gettime(CLOCK_MONOTONIC, &TS);
139   return static_cast<u64>(TS.tv_sec) * (1000ULL * 1000 * 1000) +
140          static_cast<u64>(TS.tv_nsec);
141 }
142 
143 u64 getMonotonicTimeFast() {
144 #if defined(CLOCK_MONOTONIC_COARSE)
145   timespec TS;
146   clock_gettime(CLOCK_MONOTONIC_COARSE, &TS);
147   return static_cast<u64>(TS.tv_sec) * (1000ULL * 1000 * 1000) +
148          static_cast<u64>(TS.tv_nsec);
149 #else
150   return getMonotonicTime();
151 #endif
152 }
153 
154 u32 getNumberOfCPUs() {
155   cpu_set_t CPUs;
156   // sched_getaffinity can fail for a variety of legitimate reasons (lack of
157   // CAP_SYS_NICE, syscall filtering, etc), in which case we shall return 0.
158   if (sched_getaffinity(0, sizeof(cpu_set_t), &CPUs) != 0)
159     return 0;
160   return static_cast<u32>(CPU_COUNT(&CPUs));
161 }
162 
163 u32 getThreadID() {
164 #if SCUDO_ANDROID
165   return static_cast<u32>(gettid());
166 #else
167   return static_cast<u32>(syscall(SYS_gettid));
168 #endif
169 }
170 
171 // Blocking is possibly unused if the getrandom block is not compiled in.
172 bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) {
173   if (!Buffer || !Length || Length > MaxRandomLength)
174     return false;
175   ssize_t ReadBytes;
176 #if defined(SYS_getrandom)
177 #if !defined(GRND_NONBLOCK)
178 #define GRND_NONBLOCK 1
179 #endif
180   // Up to 256 bytes, getrandom will not be interrupted.
181   ReadBytes =
182       syscall(SYS_getrandom, Buffer, Length, Blocking ? 0 : GRND_NONBLOCK);
183   if (ReadBytes == static_cast<ssize_t>(Length))
184     return true;
185 #endif // defined(SYS_getrandom)
186   // Up to 256 bytes, a read off /dev/urandom will not be interrupted.
187   // Blocking is moot here, O_NONBLOCK has no effect when opening /dev/urandom.
188   const int FileDesc = open("/dev/urandom", O_RDONLY);
189   if (FileDesc == -1)
190     return false;
191   ReadBytes = read(FileDesc, Buffer, Length);
192   close(FileDesc);
193   return (ReadBytes == static_cast<ssize_t>(Length));
194 }
195 
196 // Allocation free syslog-like API.
197 extern "C" WEAK int async_safe_write_log(int pri, const char *tag,
198                                          const char *msg);
199 
200 static uptr GetRSSFromBuffer(const char *Buf) {
201   // The format of the file is:
202   // 1084 89 69 11 0 79 0
203   // We need the second number which is RSS in pages.
204   const char *Pos = Buf;
205   // Skip the first number.
206   while (*Pos >= '0' && *Pos <= '9')
207     Pos++;
208   // Skip whitespaces.
209   while (!(*Pos >= '0' && *Pos <= '9') && *Pos != 0)
210     Pos++;
211   // Read the number.
212   u64 Rss = 0;
213   for (; *Pos >= '0' && *Pos <= '9'; Pos++)
214     Rss = Rss * 10 + static_cast<u64>(*Pos) - '0';
215   return static_cast<uptr>(Rss * getPageSizeCached());
216 }
217 
218 uptr GetRSS() {
219   // TODO: We currently use sanitizer_common's GetRSS which reads the
220   // RSS from /proc/self/statm by default. We might want to
221   // call getrusage directly, even if it's less accurate.
222   auto Fd = open("/proc/self/statm", O_RDONLY);
223   char Buf[64];
224   s64 Len = read(Fd, Buf, sizeof(Buf) - 1);
225   close(Fd);
226   if (Len <= 0)
227     return 0;
228   Buf[Len] = 0;
229 
230   return GetRSSFromBuffer(Buf);
231 }
232 
233 void outputRaw(const char *Buffer) {
234   if (&async_safe_write_log) {
235     constexpr s32 AndroidLogInfo = 4;
236     constexpr uptr MaxLength = 1024U;
237     char LocalBuffer[MaxLength];
238     while (strlen(Buffer) > MaxLength) {
239       uptr P;
240       for (P = MaxLength - 1; P > 0; P--) {
241         if (Buffer[P] == '\n') {
242           memcpy(LocalBuffer, Buffer, P);
243           LocalBuffer[P] = '\0';
244           async_safe_write_log(AndroidLogInfo, "scudo", LocalBuffer);
245           Buffer = &Buffer[P + 1];
246           break;
247         }
248       }
249       // If no newline was found, just log the buffer.
250       if (P == 0)
251         break;
252     }
253     async_safe_write_log(AndroidLogInfo, "scudo", Buffer);
254   } else {
255     (void)write(2, Buffer, strlen(Buffer));
256   }
257 }
258 
259 extern "C" WEAK void android_set_abort_message(const char *);
260 
261 void setAbortMessage(const char *Message) {
262   if (&android_set_abort_message)
263     android_set_abort_message(Message);
264 }
265 
266 } // namespace scudo
267 
268 #endif // SCUDO_LINUX
269