1 //===-- linux.cpp -----------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
16 #include "string_utils.h"
20 #include <linux/futex.h>
26 #include <sys/syscall.h>
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
40 uptr getPageSize() { return static_cast<uptr>(sysconf(_SC_PAGESIZE)); }
42 void NORETURN die() { abort(); }
44 void *map(void *Addr, uptr Size, UNUSED const char *Name, uptr Flags,
45 UNUSED MapPlatformData *Data) {
46 int MmapFlags = MAP_PRIVATE | MAP_ANONYMOUS;
48 if (Flags & MAP_NOACCESS) {
49 MmapFlags |= MAP_NORESERVE;
52 MmapProt = PROT_READ | PROT_WRITE;
55 // Currently no scenario for a noaccess mapping with a fixed address.
56 DCHECK_EQ(Flags & MAP_NOACCESS, 0);
57 MmapFlags |= MAP_FIXED;
59 void *P = mmap(Addr, Size, MmapProt, MmapFlags, -1, 0);
60 if (P == MAP_FAILED) {
61 if (!(Flags & MAP_ALLOWNOMEM) || errno != ENOMEM)
62 dieOnMapUnmapError(errno == ENOMEM);
66 if (!(Flags & MAP_NOACCESS))
67 prctl(ANDROID_PR_SET_VMA, ANDROID_PR_SET_VMA_ANON_NAME, P, Size, Name);
72 void unmap(void *Addr, uptr Size, UNUSED uptr Flags,
73 UNUSED MapPlatformData *Data) {
74 if (munmap(Addr, Size) != 0)
78 void releasePagesToOS(uptr BaseAddress, uptr Offset, uptr Size,
79 UNUSED MapPlatformData *Data) {
80 void *Addr = reinterpret_cast<void *>(BaseAddress + Offset);
81 while (madvise(Addr, Size, MADV_DONTNEED) == -1 && errno == EAGAIN) {
85 // Calling getenv should be fine (c)(tm) at any time.
86 const char *getEnv(const char *Name) { return getenv(Name); }
89 enum State : u32 { Unlocked = 0, Locked = 1, Sleeping = 2 };
92 bool HybridMutex::tryLock() {
93 return atomic_compare_exchange(&M, Unlocked, Locked) == Unlocked;
96 // The following is based on https://akkadia.org/drepper/futex.pdf.
97 void HybridMutex::lockSlow() {
98 u32 V = atomic_compare_exchange(&M, Unlocked, Locked);
102 V = atomic_exchange(&M, Sleeping, memory_order_acquire);
103 while (V != Unlocked) {
104 syscall(SYS_futex, reinterpret_cast<uptr>(&M), FUTEX_WAIT_PRIVATE, Sleeping,
105 nullptr, nullptr, 0);
106 V = atomic_exchange(&M, Sleeping, memory_order_acquire);
110 void HybridMutex::unlock() {
111 if (atomic_fetch_sub(&M, 1U, memory_order_release) != Locked) {
112 atomic_store(&M, Unlocked, memory_order_release);
113 syscall(SYS_futex, reinterpret_cast<uptr>(&M), FUTEX_WAKE_PRIVATE, 1,
114 nullptr, nullptr, 0);
118 u64 getMonotonicTime() {
120 clock_gettime(CLOCK_MONOTONIC, &TS);
121 return static_cast<u64>(TS.tv_sec) * (1000ULL * 1000 * 1000) +
122 static_cast<u64>(TS.tv_nsec);
125 u32 getNumberOfCPUs() {
127 CHECK_EQ(sched_getaffinity(0, sizeof(cpu_set_t), &CPUs), 0);
128 return static_cast<u32>(CPU_COUNT(&CPUs));
131 // Blocking is possibly unused if the getrandom block is not compiled in.
132 bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) {
133 if (!Buffer || !Length || Length > MaxRandomLength)
136 #if defined(SYS_getrandom)
137 #if !defined(GRND_NONBLOCK)
138 #define GRND_NONBLOCK 1
140 // Up to 256 bytes, getrandom will not be interrupted.
142 syscall(SYS_getrandom, Buffer, Length, Blocking ? 0 : GRND_NONBLOCK);
143 if (ReadBytes == static_cast<ssize_t>(Length))
145 #endif // defined(SYS_getrandom)
146 // Up to 256 bytes, a read off /dev/urandom will not be interrupted.
147 // Blocking is moot here, O_NONBLOCK has no effect when opening /dev/urandom.
148 const int FileDesc = open("/dev/urandom", O_RDONLY);
151 ReadBytes = read(FileDesc, Buffer, Length);
153 return (ReadBytes == static_cast<ssize_t>(Length));
156 void outputRaw(const char *Buffer) {
157 static HybridMutex Mutex;
159 write(2, Buffer, strlen(Buffer));
162 extern "C" WEAK void android_set_abort_message(const char *);
164 void setAbortMessage(const char *Message) {
165 if (&android_set_abort_message)
166 android_set_abort_message(Message);
171 #endif // SCUDO_LINUX