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