blob: dedab61631bc5883f0e0eb65e090a44584629bc2 [file] [log] [blame]
Dynamic Tools Team517193e2019-09-11 14:48:41 +00001//===-- 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
38namespace scudo {
39
40uptr getPageSize() { return static_cast<uptr>(sysconf(_SC_PAGESIZE)); }
41
42void NORETURN die() { abort(); }
43
44void *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;
Peter Collingbournecc3d4932020-12-21 18:39:03 -080053 }
Peter Collingbourne9ece4092020-12-17 21:02:01 -080054#if defined(__aarch64__)
55#ifndef PROT_MTE
56#define PROT_MTE 0x20
57#endif
Peter Collingbournecc3d4932020-12-21 18:39:03 -080058 if (Flags & MAP_MEMTAG)
59 MmapProt |= PROT_MTE;
Dynamic Tools Team48429c72019-12-04 17:46:15 -080060#endif
Dynamic Tools Team517193e2019-09-11 14:48:41 +000061 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)
Kostya Kortchinskyc79ab1b2021-05-24 09:26:21 -070069 dieOnMapUnmapError(errno == ENOMEM ? Size : 0);
Dynamic Tools Team517193e2019-09-11 14:48:41 +000070 return nullptr;
71 }
72#if SCUDO_ANDROID
Peter Collingbournecc3d4932020-12-21 18:39:03 -080073 if (Name)
Dynamic Tools Team517193e2019-09-11 14:48:41 +000074 prctl(ANDROID_PR_SET_VMA, ANDROID_PR_SET_VMA_ANON_NAME, P, Size, Name);
75#endif
76 return P;
77}
78
79void unmap(void *Addr, uptr Size, UNUSED uptr Flags,
80 UNUSED MapPlatformData *Data) {
81 if (munmap(Addr, Size) != 0)
82 dieOnMapUnmapError();
83}
84
Peter Collingbournecc3d4932020-12-21 18:39:03 -080085void setMemoryPermission(uptr Addr, uptr Size, uptr Flags,
86 UNUSED MapPlatformData *Data) {
87 int Prot = (Flags & MAP_NOACCESS) ? PROT_NONE : (PROT_READ | PROT_WRITE);
88 if (mprotect(reinterpret_cast<void *>(Addr), Size, Prot) != 0)
89 dieOnMapUnmapError();
90}
91
Dynamic Tools Team517193e2019-09-11 14:48:41 +000092void releasePagesToOS(uptr BaseAddress, uptr Offset, uptr Size,
93 UNUSED MapPlatformData *Data) {
94 void *Addr = reinterpret_cast<void *>(BaseAddress + Offset);
Vitaly Buka6c5c1bb2021-05-28 01:53:42 -070095
Dynamic Tools Team517193e2019-09-11 14:48:41 +000096 while (madvise(Addr, Size, MADV_DONTNEED) == -1 && errno == EAGAIN) {
97 }
98}
99
100// Calling getenv should be fine (c)(tm) at any time.
101const char *getEnv(const char *Name) { return getenv(Name); }
102
103namespace {
104enum State : u32 { Unlocked = 0, Locked = 1, Sleeping = 2 };
105}
106
107bool HybridMutex::tryLock() {
108 return atomic_compare_exchange(&M, Unlocked, Locked) == Unlocked;
109}
110
111// The following is based on https://akkadia.org/drepper/futex.pdf.
112void HybridMutex::lockSlow() {
113 u32 V = atomic_compare_exchange(&M, Unlocked, Locked);
114 if (V == Unlocked)
115 return;
116 if (V != Sleeping)
117 V = atomic_exchange(&M, Sleeping, memory_order_acquire);
118 while (V != Unlocked) {
119 syscall(SYS_futex, reinterpret_cast<uptr>(&M), FUTEX_WAIT_PRIVATE, Sleeping,
120 nullptr, nullptr, 0);
121 V = atomic_exchange(&M, Sleeping, memory_order_acquire);
122 }
123}
124
125void HybridMutex::unlock() {
126 if (atomic_fetch_sub(&M, 1U, memory_order_release) != Locked) {
127 atomic_store(&M, Unlocked, memory_order_release);
128 syscall(SYS_futex, reinterpret_cast<uptr>(&M), FUTEX_WAKE_PRIVATE, 1,
129 nullptr, nullptr, 0);
130 }
131}
132
133u64 getMonotonicTime() {
134 timespec TS;
135 clock_gettime(CLOCK_MONOTONIC, &TS);
136 return static_cast<u64>(TS.tv_sec) * (1000ULL * 1000 * 1000) +
137 static_cast<u64>(TS.tv_nsec);
138}
139
140u32 getNumberOfCPUs() {
141 cpu_set_t CPUs;
Dynamic Tools Team7fef7302020-01-20 09:50:22 -0800142 // sched_getaffinity can fail for a variety of legitimate reasons (lack of
143 // CAP_SYS_NICE, syscall filtering, etc), in which case we shall return 0.
144 if (sched_getaffinity(0, sizeof(cpu_set_t), &CPUs) != 0)
145 return 0;
Dynamic Tools Team517193e2019-09-11 14:48:41 +0000146 return static_cast<u32>(CPU_COUNT(&CPUs));
147}
148
Dynamic Tools Team0d7d2ae2020-04-21 15:30:50 -0700149u32 getThreadID() {
150#if SCUDO_ANDROID
151 return static_cast<u32>(gettid());
152#else
153 return static_cast<u32>(syscall(SYS_gettid));
154#endif
155}
156
Dynamic Tools Team517193e2019-09-11 14:48:41 +0000157// Blocking is possibly unused if the getrandom block is not compiled in.
158bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) {
159 if (!Buffer || !Length || Length > MaxRandomLength)
160 return false;
161 ssize_t ReadBytes;
162#if defined(SYS_getrandom)
163#if !defined(GRND_NONBLOCK)
164#define GRND_NONBLOCK 1
165#endif
166 // Up to 256 bytes, getrandom will not be interrupted.
167 ReadBytes =
168 syscall(SYS_getrandom, Buffer, Length, Blocking ? 0 : GRND_NONBLOCK);
169 if (ReadBytes == static_cast<ssize_t>(Length))
170 return true;
171#endif // defined(SYS_getrandom)
172 // Up to 256 bytes, a read off /dev/urandom will not be interrupted.
173 // Blocking is moot here, O_NONBLOCK has no effect when opening /dev/urandom.
174 const int FileDesc = open("/dev/urandom", O_RDONLY);
175 if (FileDesc == -1)
176 return false;
177 ReadBytes = read(FileDesc, Buffer, Length);
178 close(FileDesc);
179 return (ReadBytes == static_cast<ssize_t>(Length));
180}
181
Dynamic Tools Team0f811902020-01-28 09:38:59 -0800182// Allocation free syslog-like API.
183extern "C" WEAK int async_safe_write_log(int pri, const char *tag,
184 const char *msg);
185
Dynamic Tools Team517193e2019-09-11 14:48:41 +0000186void outputRaw(const char *Buffer) {
Dynamic Tools Team0f811902020-01-28 09:38:59 -0800187 if (&async_safe_write_log) {
188 constexpr s32 AndroidLogInfo = 4;
Dynamic Tools Teamee9ca982020-04-13 07:25:35 -0700189 constexpr uptr MaxLength = 1024U;
190 char LocalBuffer[MaxLength];
191 while (strlen(Buffer) > MaxLength) {
192 uptr P;
193 for (P = MaxLength - 1; P > 0; P--) {
194 if (Buffer[P] == '\n') {
195 memcpy(LocalBuffer, Buffer, P);
196 LocalBuffer[P] = '\0';
197 async_safe_write_log(AndroidLogInfo, "scudo", LocalBuffer);
198 Buffer = &Buffer[P + 1];
199 break;
200 }
201 }
202 // If no newline was found, just log the buffer.
203 if (P == 0)
204 break;
205 }
Dynamic Tools Team0f811902020-01-28 09:38:59 -0800206 async_safe_write_log(AndroidLogInfo, "scudo", Buffer);
207 } else {
Fangrui Song6712ba92020-10-12 09:57:12 -0700208 (void)write(2, Buffer, strlen(Buffer));
Dynamic Tools Team0f811902020-01-28 09:38:59 -0800209 }
Dynamic Tools Team517193e2019-09-11 14:48:41 +0000210}
211
212extern "C" WEAK void android_set_abort_message(const char *);
213
214void setAbortMessage(const char *Message) {
215 if (&android_set_abort_message)
216 android_set_abort_message(Message);
217}
218
219} // namespace scudo
220
221#endif // SCUDO_LINUX