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 "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;
53  }
54  if (Addr) {
55    // Currently no scenario for a noaccess mapping with a fixed address.
56    DCHECK_EQ(Flags & MAP_NOACCESS, 0);
57    MmapFlags |= MAP_FIXED;
58  }
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);
63    return nullptr;
64  }
65#if SCUDO_ANDROID
66  if (!(Flags & MAP_NOACCESS))
67    prctl(ANDROID_PR_SET_VMA, ANDROID_PR_SET_VMA_ANON_NAME, P, Size, Name);
68#endif
69  return P;
70}
71
72void unmap(void *Addr, uptr Size, UNUSED uptr Flags,
73           UNUSED MapPlatformData *Data) {
74  if (munmap(Addr, Size) != 0)
75    dieOnMapUnmapError();
76}
77
78void 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) {
82  }
83}
84
85// Calling getenv should be fine (c)(tm) at any time.
86const char *getEnv(const char *Name) { return getenv(Name); }
87
88namespace {
89enum State : u32 { Unlocked = 0, Locked = 1, Sleeping = 2 };
90}
91
92bool HybridMutex::tryLock() {
93  return atomic_compare_exchange(&M, Unlocked, Locked) == Unlocked;
94}
95
96// The following is based on https://akkadia.org/drepper/futex.pdf.
97void HybridMutex::lockSlow() {
98  u32 V = atomic_compare_exchange(&M, Unlocked, Locked);
99  if (V == Unlocked)
100    return;
101  if (V != Sleeping)
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);
107  }
108}
109
110void 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);
115  }
116}
117
118u64 getMonotonicTime() {
119  timespec TS;
120  clock_gettime(CLOCK_MONOTONIC, &TS);
121  return static_cast<u64>(TS.tv_sec) * (1000ULL * 1000 * 1000) +
122         static_cast<u64>(TS.tv_nsec);
123}
124
125u32 getNumberOfCPUs() {
126  cpu_set_t CPUs;
127  CHECK_EQ(sched_getaffinity(0, sizeof(cpu_set_t), &CPUs), 0);
128  return static_cast<u32>(CPU_COUNT(&CPUs));
129}
130
131// Blocking is possibly unused if the getrandom block is not compiled in.
132bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) {
133  if (!Buffer || !Length || Length > MaxRandomLength)
134    return false;
135  ssize_t ReadBytes;
136#if defined(SYS_getrandom)
137#if !defined(GRND_NONBLOCK)
138#define GRND_NONBLOCK 1
139#endif
140  // Up to 256 bytes, getrandom will not be interrupted.
141  ReadBytes =
142      syscall(SYS_getrandom, Buffer, Length, Blocking ? 0 : GRND_NONBLOCK);
143  if (ReadBytes == static_cast<ssize_t>(Length))
144    return true;
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);
149  if (FileDesc == -1)
150    return false;
151  ReadBytes = read(FileDesc, Buffer, Length);
152  close(FileDesc);
153  return (ReadBytes == static_cast<ssize_t>(Length));
154}
155
156void outputRaw(const char *Buffer) {
157  static HybridMutex Mutex;
158  ScopedLock L(Mutex);
159  write(2, Buffer, strlen(Buffer));
160}
161
162extern "C" WEAK void android_set_abort_message(const char *);
163
164void setAbortMessage(const char *Message) {
165  if (&android_set_abort_message)
166    android_set_abort_message(Message);
167}
168
169} // namespace scudo
170
171#endif // SCUDO_LINUX
172