Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / scudo / standalone / trusty.cpp
blob26b349c6e506e379b96e382c65f19c86f36ad2a8
1 //===-- trusty.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 //===----------------------------------------------------------------------===//
9 #include "platform.h"
11 #if SCUDO_TRUSTY
13 #include "common.h"
14 #include "mutex.h"
15 #include "report_linux.h"
16 #include "trusty.h"
18 #include <errno.h> // for errno
19 #include <lk/err_ptr.h> // for PTR_ERR and IS_ERR
20 #include <stdio.h> // for printf()
21 #include <stdlib.h> // for getenv()
22 #include <sys/auxv.h> // for getauxval()
23 #include <time.h> // for clock_gettime()
24 #include <trusty_err.h> // for lk_err_to_errno()
25 #include <trusty_syscalls.h> // for _trusty_brk()
26 #include <uapi/mm.h> // for MMAP flags
28 namespace scudo {
30 uptr getPageSize() { return getauxval(AT_PAGESZ); }
32 void NORETURN die() { abort(); }
34 void *map(void *Addr, uptr Size, const char *Name, uptr Flags,
35 UNUSED MapPlatformData *Data) {
36 uint32_t MmapFlags =
37 MMAP_FLAG_ANONYMOUS | MMAP_FLAG_PROT_READ | MMAP_FLAG_PROT_WRITE;
39 // If the MAP_NOACCESS flag is set, Scudo tries to reserve
40 // a memory region without mapping physical pages. This corresponds
41 // to MMAP_FLAG_NO_PHYSICAL in Trusty.
42 if (Flags & MAP_NOACCESS)
43 MmapFlags |= MMAP_FLAG_NO_PHYSICAL;
44 if (Addr)
45 MmapFlags |= MMAP_FLAG_FIXED_NOREPLACE;
47 if (Flags & MAP_MEMTAG)
48 MmapFlags |= MMAP_FLAG_PROT_MTE;
50 void *P = (void *)_trusty_mmap(Addr, Size, MmapFlags, 0);
52 if (IS_ERR(P)) {
53 errno = lk_err_to_errno(PTR_ERR(P));
54 if (!(Flags & MAP_ALLOWNOMEM) || errno != ENOMEM)
55 reportMapError(Size);
56 return nullptr;
59 return P;
62 void unmap(UNUSED void *Addr, UNUSED uptr Size, UNUSED uptr Flags,
63 UNUSED MapPlatformData *Data) {
64 if (_trusty_munmap(Addr, Size) != 0)
65 reportUnmapError(reinterpret_cast<uptr>(Addr), Size);
68 void setMemoryPermission(UNUSED uptr Addr, UNUSED uptr Size, UNUSED uptr Flags,
69 UNUSED MapPlatformData *Data) {}
71 void releasePagesToOS(UNUSED uptr BaseAddress, UNUSED uptr Offset,
72 UNUSED uptr Size, UNUSED MapPlatformData *Data) {}
74 const char *getEnv(const char *Name) { return getenv(Name); }
76 // All mutex operations are a no-op since Trusty doesn't currently support
77 // threads.
78 bool HybridMutex::tryLock() { return true; }
80 void HybridMutex::lockSlow() {}
82 void HybridMutex::unlock() {}
84 void HybridMutex::assertHeldImpl() {}
86 u64 getMonotonicTime() {
87 timespec TS;
88 clock_gettime(CLOCK_MONOTONIC, &TS);
89 return static_cast<u64>(TS.tv_sec) * (1000ULL * 1000 * 1000) +
90 static_cast<u64>(TS.tv_nsec);
93 u64 getMonotonicTimeFast() {
94 #if defined(CLOCK_MONOTONIC_COARSE)
95 timespec TS;
96 clock_gettime(CLOCK_MONOTONIC_COARSE, &TS);
97 return static_cast<u64>(TS.tv_sec) * (1000ULL * 1000 * 1000) +
98 static_cast<u64>(TS.tv_nsec);
99 #else
100 return getMonotonicTime();
101 #endif
104 u32 getNumberOfCPUs() { return 0; }
106 u32 getThreadID() { return 0; }
108 bool getRandom(UNUSED void *Buffer, UNUSED uptr Length, UNUSED bool Blocking) {
109 return false;
112 void outputRaw(const char *Buffer) { printf("%s", Buffer); }
114 void setAbortMessage(UNUSED const char *Message) {}
116 } // namespace scudo
118 #endif // SCUDO_TRUSTY