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 //===----------------------------------------------------------------------===//
8
9 #include "platform.h"
10
11 #if SCUDO_TRUSTY
12
13 #include "common.h"
14 #include "mutex.h"
15 #include "string_utils.h"
16 #include "trusty.h"
17
18 #include <errno.h> // for errno
19 #include <stdio.h> // for printf()
20 #include <stdlib.h> // for getenv()
21 #include <sys/auxv.h> // for getauxval()
22 #include <time.h> // for clock_gettime()
23 #include <trusty_syscalls.h> // for _trusty_brk()
24
25 #define SBRK_ALIGN 32
26
27 namespace scudo {
28
getPageSize()29 uptr getPageSize() { return getauxval(AT_PAGESZ); }
30
die()31 void NORETURN die() { abort(); }
32
map(UNUSED void * Addr,uptr Size,UNUSED const char * Name,uptr Flags,UNUSED MapPlatformData * Data)33 void *map(UNUSED void *Addr, uptr Size, UNUSED const char *Name, uptr Flags,
34 UNUSED MapPlatformData *Data) {
35 // Calling _trusty_brk(0) returns the current program break.
36 uptr ProgramBreak = reinterpret_cast<uptr>(_trusty_brk(0));
37 uptr Start;
38 uptr End;
39
40 Start = roundUpTo(ProgramBreak, SBRK_ALIGN);
41 // Don't actually extend the heap if MAP_NOACCESS flag is set since this is
42 // the case where Scudo tries to reserve a memory region without mapping
43 // physical pages.
44 if (Flags & MAP_NOACCESS)
45 return reinterpret_cast<void *>(Start);
46
47 // Attempt to extend the heap by Size bytes using _trusty_brk.
48 End = roundUpTo(Start + Size, SBRK_ALIGN);
49 ProgramBreak =
50 reinterpret_cast<uptr>(_trusty_brk(reinterpret_cast<void *>(End)));
51 if (ProgramBreak < End) {
52 errno = ENOMEM;
53 dieOnMapUnmapError(Size);
54 return nullptr;
55 }
56 return reinterpret_cast<void *>(Start); // Base of new reserved region.
57 }
58
59 // Unmap is a no-op since Trusty uses sbrk instead of memory mapping.
unmap(UNUSED void * Addr,UNUSED uptr Size,UNUSED uptr Flags,UNUSED MapPlatformData * Data)60 void unmap(UNUSED void *Addr, UNUSED uptr Size, UNUSED uptr Flags,
61 UNUSED MapPlatformData *Data) {}
62
setMemoryPermission(UNUSED uptr Addr,UNUSED uptr Size,UNUSED uptr Flags,UNUSED MapPlatformData * Data)63 void setMemoryPermission(UNUSED uptr Addr, UNUSED uptr Size, UNUSED uptr Flags,
64 UNUSED MapPlatformData *Data) {}
65
releasePagesToOS(UNUSED uptr BaseAddress,UNUSED uptr Offset,UNUSED uptr Size,UNUSED MapPlatformData * Data)66 void releasePagesToOS(UNUSED uptr BaseAddress, UNUSED uptr Offset,
67 UNUSED uptr Size, UNUSED MapPlatformData *Data) {}
68
getEnv(const char * Name)69 const char *getEnv(const char *Name) { return getenv(Name); }
70
71 // All mutex operations are a no-op since Trusty doesn't currently support
72 // threads.
tryLock()73 bool HybridMutex::tryLock() { return true; }
74
lockSlow()75 void HybridMutex::lockSlow() {}
76
unlock()77 void HybridMutex::unlock() {}
78
getMonotonicTime()79 u64 getMonotonicTime() {
80 timespec TS;
81 clock_gettime(CLOCK_MONOTONIC, &TS);
82 return static_cast<u64>(TS.tv_sec) * (1000ULL * 1000 * 1000) +
83 static_cast<u64>(TS.tv_nsec);
84 }
85
getNumberOfCPUs()86 u32 getNumberOfCPUs() { return 0; }
87
getThreadID()88 u32 getThreadID() { return 0; }
89
getRandom(UNUSED void * Buffer,UNUSED uptr Length,UNUSED bool Blocking)90 bool getRandom(UNUSED void *Buffer, UNUSED uptr Length, UNUSED bool Blocking) {
91 return false;
92 }
93
outputRaw(const char * Buffer)94 void outputRaw(const char *Buffer) { printf("%s", Buffer); }
95
setAbortMessage(UNUSED const char * Message)96 void setAbortMessage(UNUSED const char *Message) {}
97
98 } // namespace scudo
99
100 #endif // SCUDO_TRUSTY
101