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