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

source code of compiler-rt/lib/scudo/standalone/linux.cpp