1 //===-- sanitizer_rtems.cpp -----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is shared between various sanitizers' runtime libraries and
10 // implements RTEMS-specific functions.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_rtems.h"
16 #define posix_memalign __real_posix_memalign
17 #define free __real_free
18 #define memset __real_memset
20 #include "sanitizer_file.h"
21 #include "sanitizer_symbolizer.h"
31 // There is no mmap on RTEMS. Use memalign, etc.
32 #define __mmap_alloc_aligned posix_memalign
33 #define __mmap_free free
34 #define __mmap_memset memset
36 namespace __sanitizer
{
38 #include "sanitizer_syscall_generic.inc"
40 void NORETURN
internal__exit(int exitcode
) {
44 uptr
internal_sched_yield() {
48 uptr
internal_getpid() {
52 int internal_dlinfo(void *handle
, int request
, void *p
) {
56 bool FileExists(const char *filename
) {
58 if (stat(filename
, &st
))
60 // Sanity check: filename is a regular file.
61 return S_ISREG(st
.st_mode
);
64 uptr
GetThreadSelf() { return static_cast<uptr
>(pthread_self()); }
66 tid_t
GetTid() { return GetThreadSelf(); }
68 void Abort() { abort(); }
70 int Atexit(void (*function
)(void)) { return atexit(function
); }
72 void SleepForSeconds(int seconds
) { sleep(seconds
); }
74 void SleepForMillis(int millis
) { usleep(millis
* 1000); }
76 bool SupportsColoredOutput(fd_t fd
) { return false; }
78 void GetThreadStackTopAndBottom(bool at_initialization
,
79 uptr
*stack_top
, uptr
*stack_bottom
) {
81 pthread_attr_init(&attr
);
82 CHECK_EQ(pthread_getattr_np(pthread_self(), &attr
), 0);
85 CHECK_EQ(pthread_attr_getstack(&attr
, &base
, &size
), 0);
86 CHECK_EQ(pthread_attr_destroy(&attr
), 0);
88 *stack_bottom
= reinterpret_cast<uptr
>(base
);
89 *stack_top
= *stack_bottom
+ size
;
92 void GetThreadStackAndTls(bool main
, uptr
*stk_addr
, uptr
*stk_size
,
93 uptr
*tls_addr
, uptr
*tls_size
) {
94 uptr stack_top
, stack_bottom
;
95 GetThreadStackTopAndBottom(main
, &stack_top
, &stack_bottom
);
96 *stk_addr
= stack_bottom
;
97 *stk_size
= stack_top
- stack_bottom
;
98 *tls_addr
= *tls_size
= 0;
101 void InitializePlatformEarly() {}
102 void MaybeReexec() {}
104 void CheckMPROTECT() {}
105 void DisableCoreDumperIfNecessary() {}
106 void InstallDeadlySignalHandlers(SignalHandlerType handler
) {}
107 void SetAlternateSignalStack() {}
108 void UnsetAlternateSignalStack() {}
109 void InitTlsSize() {}
111 void SignalContext::DumpAllRegisters(void *context
) {}
112 const char *DescribeSignalOrException(int signo
) { UNIMPLEMENTED(); }
114 enum MutexState
{ MtxUnlocked
= 0, MtxLocked
= 1, MtxSleeping
= 2 };
116 BlockingMutex::BlockingMutex() {
117 internal_memset(this, 0, sizeof(*this));
120 void BlockingMutex::Lock() {
122 atomic_uint32_t
*m
= reinterpret_cast<atomic_uint32_t
*>(&opaque_storage_
);
123 if (atomic_exchange(m
, MtxLocked
, memory_order_acquire
) == MtxUnlocked
)
125 while (atomic_exchange(m
, MtxSleeping
, memory_order_acquire
) != MtxUnlocked
) {
126 internal_sched_yield();
130 void BlockingMutex::Unlock() {
131 atomic_uint32_t
*m
= reinterpret_cast<atomic_uint32_t
*>(&opaque_storage_
);
132 u32 v
= atomic_exchange(m
, MtxUnlocked
, memory_order_release
);
133 CHECK_NE(v
, MtxUnlocked
);
136 void BlockingMutex::CheckLocked() {
137 atomic_uint32_t
*m
= reinterpret_cast<atomic_uint32_t
*>(&opaque_storage_
);
138 CHECK_NE(MtxUnlocked
, atomic_load(m
, memory_order_relaxed
));
141 uptr
GetPageSize() { return getpagesize(); }
143 uptr
GetMmapGranularity() { return GetPageSize(); }
145 uptr
GetMaxVirtualAddress() {
146 return (1ULL << 32) - 1; // 0xffffffff
149 void *MmapOrDie(uptr size
, const char *mem_type
, bool raw_report
) {
151 int res
= __mmap_alloc_aligned(&ptr
, GetPageSize(), size
);
153 ReportMmapFailureAndDie(size
, mem_type
, "allocate", res
, raw_report
);
154 __mmap_memset(ptr
, 0, size
);
155 IncreaseTotalMmap(size
);
159 void *MmapOrDieOnFatalError(uptr size
, const char *mem_type
) {
161 int res
= __mmap_alloc_aligned(&ptr
, GetPageSize(), size
);
165 ReportMmapFailureAndDie(size
, mem_type
, "allocate", false);
167 __mmap_memset(ptr
, 0, size
);
168 IncreaseTotalMmap(size
);
172 void *MmapAlignedOrDieOnFatalError(uptr size
, uptr alignment
,
173 const char *mem_type
) {
174 CHECK(IsPowerOfTwo(size
));
175 CHECK(IsPowerOfTwo(alignment
));
177 int res
= __mmap_alloc_aligned(&ptr
, alignment
, size
);
179 ReportMmapFailureAndDie(size
, mem_type
, "align allocate", res
, false);
180 __mmap_memset(ptr
, 0, size
);
181 IncreaseTotalMmap(size
);
185 void *MmapNoReserveOrDie(uptr size
, const char *mem_type
) {
186 return MmapOrDie(size
, mem_type
, false);
189 void UnmapOrDie(void *addr
, uptr size
) {
190 if (!addr
|| !size
) return;
192 DecreaseTotalMmap(size
);
195 fd_t
OpenFile(const char *filename
, FileAccessMode mode
, error_t
*errno_p
) {
198 case RdOnly
: flags
= O_RDONLY
; break;
199 case WrOnly
: flags
= O_WRONLY
| O_CREAT
| O_TRUNC
; break;
200 case RdWr
: flags
= O_RDWR
| O_CREAT
; break;
202 fd_t res
= open(filename
, flags
, 0660);
203 if (internal_iserror(res
, errno_p
))
208 void CloseFile(fd_t fd
) {
212 bool ReadFromFile(fd_t fd
, void *buff
, uptr buff_size
, uptr
*bytes_read
,
214 uptr res
= read(fd
, buff
, buff_size
);
215 if (internal_iserror(res
, error_p
))
222 bool WriteToFile(fd_t fd
, const void *buff
, uptr buff_size
, uptr
*bytes_written
,
224 uptr res
= write(fd
, buff
, buff_size
);
225 if (internal_iserror(res
, error_p
))
228 *bytes_written
= res
;
232 void ReleaseMemoryPagesToOS(uptr beg
, uptr end
) {}
233 void DumpProcessMap() {}
235 // There is no page protection so everything is "accessible."
236 bool IsAccessibleMemoryRange(uptr beg
, uptr size
) {
240 char **GetArgv() { return nullptr; }
241 char **GetEnviron() { return nullptr; }
243 const char *GetEnv(const char *name
) {
247 uptr
ReadBinaryName(/*out*/char *buf
, uptr buf_len
) {
248 internal_strncpy(buf
, "StubBinaryName", buf_len
);
249 return internal_strlen(buf
);
252 uptr
ReadLongProcessName(/*out*/ char *buf
, uptr buf_len
) {
253 internal_strncpy(buf
, "StubProcessName", buf_len
);
254 return internal_strlen(buf
);
257 bool IsPathSeparator(const char c
) {
261 bool IsAbsolutePath(const char *path
) {
262 return path
!= nullptr && IsPathSeparator(path
[0]);
265 void ReportFile::Write(const char *buffer
, uptr length
) {
267 static const char *kWriteError
=
268 "ReportFile::Write() can't output requested buffer!\n";
270 if (length
!= write(fd
, buffer
, length
)) {
271 write(fd
, kWriteError
, internal_strlen(kWriteError
));
276 uptr MainThreadStackBase
, MainThreadStackSize
;
277 uptr MainThreadTlsBase
, MainThreadTlsSize
;
279 } // namespace __sanitizer
281 #endif // SANITIZER_RTEMS