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 41 namespace scudo { 42 43 uptr getPageSize() { return static_cast<uptr>(sysconf(_SC_PAGESIZE)); } 44 45 void NORETURN die() { abort(); } 46 47 // TODO: Will be deprecated. Use the interfaces in MemMapLinux instead. 48 void *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, Size, MmapProt, MmapFlags, -1, 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. 81 void unmap(void *Addr, uptr Size, UNUSED uptr Flags, 82 UNUSED MapPlatformData *Data) { 83 if (munmap(Addr, Size) != 0) 84 reportUnmapError(reinterpret_cast<uptr>(Addr), Size); 85 } 86 87 // TODO: Will be deprecated. Use the interfaces in MemMapLinux instead. 88 void 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(reinterpret_cast<void *>(Addr), Size, Prot) != 0) 92 reportProtectError(Addr, Size, Prot); 93 } 94 95 // TODO: Will be deprecated. Use the interfaces in MemMapLinux instead. 96 void releasePagesToOS(uptr BaseAddress, uptr Offset, uptr Size, 97 UNUSED MapPlatformData *Data) { 98 void *Addr = reinterpret_cast<void *>(BaseAddress + Offset); 99 100 while (madvise(Addr, Size, MADV_DONTNEED) == -1 && errno == EAGAIN) { 101 } 102 } 103 104 // Calling getenv should be fine (c)(tm) at any time. 105 const char *getEnv(const char *Name) { return getenv(Name); } 106 107 namespace { 108 enum State : u32 { Unlocked = 0, Locked = 1, Sleeping = 2 }; 109 } 110 111 bool HybridMutex::tryLock() { 112 return atomic_compare_exchange_strong(&M, Unlocked, Locked, 113 memory_order_acquire) == Unlocked; 114 } 115 116 // The following is based on https://akkadia.org/drepper/futex.pdf. 117 void HybridMutex::lockSlow() { 118 u32 V = atomic_compare_exchange_strong(&M, Unlocked, Locked, 119 memory_order_acquire); 120 if (V == Unlocked) 121 return; 122 if (V != Sleeping) 123 V = atomic_exchange(&M, Sleeping, 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(&M, Sleeping, memory_order_acquire); 128 } 129 } 130 131 void HybridMutex::unlock() { 132 if (atomic_fetch_sub(&M, 1U, memory_order_release) != Locked) { 133 atomic_store(&M, Unlocked, memory_order_release); 134 syscall(SYS_futex, reinterpret_cast<uptr>(&M), FUTEX_WAKE_PRIVATE, 1, 135 nullptr, nullptr, 0); 136 } 137 } 138 139 void HybridMutex::assertHeldImpl() { 140 CHECK(atomic_load(&M, memory_order_acquire) != Unlocked); 141 } 142 143 u64 getMonotonicTime() { 144 timespec TS; 145 clock_gettime(CLOCK_MONOTONIC, &TS); 146 return static_cast<u64>(TS.tv_sec) * (1000ULL * 1000 * 1000) + 147 static_cast<u64>(TS.tv_nsec); 148 } 149 150 u64 getMonotonicTimeFast() { 151 #if defined(CLOCK_MONOTONIC_COARSE) 152 timespec TS; 153 clock_gettime(CLOCK_MONOTONIC_COARSE, &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 161 u32 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(0, sizeof(cpu_set_t), &CPUs) != 0) 166 return 0; 167 return static_cast<u32>(CPU_COUNT(&CPUs)); 168 } 169 170 u32 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. 179 bool 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("/dev/urandom", O_RDONLY); 196 if (FileDesc == -1) 197 return false; 198 ReadBytes = read(FileDesc, Buffer, Length); 199 close(FileDesc); 200 return (ReadBytes == static_cast<ssize_t>(Length)); 201 } 202 203 // Allocation free syslog-like API. 204 extern "C" WEAK int async_safe_write_log(int pri, const char *tag, 205 const char *msg); 206 207 void 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(Buffer) > MaxLength) { 213 uptr P; 214 for (P = MaxLength - 1; P > 0; P--) { 215 if (Buffer[P] == '\n') { 216 memcpy(LocalBuffer, Buffer, P); 217 LocalBuffer[P] = '\0'; 218 async_safe_write_log(AndroidLogInfo, "scudo", 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(AndroidLogInfo, "scudo", Buffer); 228 } else { 229 (void)write(2, Buffer, strlen(Buffer)); 230 } 231 } 232 233 extern "C" WEAK void android_set_abort_message(const char *); 234 235 void 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