xref: /aosp_15_r20/external/llvm-libc/test/UnitTest/HermeticTestUtils.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1*71db0c75SAndroid Build Coastguard Worker //===-- Implementation of libc death test executors -----------------------===//
2*71db0c75SAndroid Build Coastguard Worker //
3*71db0c75SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*71db0c75SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information.
5*71db0c75SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*71db0c75SAndroid Build Coastguard Worker //
7*71db0c75SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
8*71db0c75SAndroid Build Coastguard Worker 
9*71db0c75SAndroid Build Coastguard Worker #include "src/__support/common.h"
10*71db0c75SAndroid Build Coastguard Worker #include "src/__support/macros/config.h"
11*71db0c75SAndroid Build Coastguard Worker #include <stddef.h>
12*71db0c75SAndroid Build Coastguard Worker #include <stdint.h>
13*71db0c75SAndroid Build Coastguard Worker 
14*71db0c75SAndroid Build Coastguard Worker #ifdef LIBC_TARGET_ARCH_IS_AARCH64
15*71db0c75SAndroid Build Coastguard Worker #include "src/sys/auxv/getauxval.h"
16*71db0c75SAndroid Build Coastguard Worker #endif
17*71db0c75SAndroid Build Coastguard Worker 
18*71db0c75SAndroid Build Coastguard Worker namespace LIBC_NAMESPACE_DECL {
19*71db0c75SAndroid Build Coastguard Worker 
20*71db0c75SAndroid Build Coastguard Worker int bcmp(const void *lhs, const void *rhs, size_t count);
21*71db0c75SAndroid Build Coastguard Worker void bzero(void *ptr, size_t count);
22*71db0c75SAndroid Build Coastguard Worker int memcmp(const void *lhs, const void *rhs, size_t count);
23*71db0c75SAndroid Build Coastguard Worker void *memcpy(void *__restrict, const void *__restrict, size_t);
24*71db0c75SAndroid Build Coastguard Worker void *memmove(void *dst, const void *src, size_t count);
25*71db0c75SAndroid Build Coastguard Worker void *memset(void *ptr, int value, size_t count);
26*71db0c75SAndroid Build Coastguard Worker int atexit(void (*func)(void));
27*71db0c75SAndroid Build Coastguard Worker 
28*71db0c75SAndroid Build Coastguard Worker // TODO: It seems that some old test frameworks does not use
29*71db0c75SAndroid Build Coastguard Worker // add_libc_hermetic_test properly. Such that they won't get correct linkage
30*71db0c75SAndroid Build Coastguard Worker // against the object containing this function. We create a dummy function that
31*71db0c75SAndroid Build Coastguard Worker // always returns 0 to indicate a failure.
getauxval(unsigned long id)32*71db0c75SAndroid Build Coastguard Worker [[gnu::weak]] unsigned long getauxval(unsigned long id) { return 0; }
33*71db0c75SAndroid Build Coastguard Worker 
34*71db0c75SAndroid Build Coastguard Worker } // namespace LIBC_NAMESPACE_DECL
35*71db0c75SAndroid Build Coastguard Worker 
36*71db0c75SAndroid Build Coastguard Worker namespace {
37*71db0c75SAndroid Build Coastguard Worker 
38*71db0c75SAndroid Build Coastguard Worker // Integration tests cannot use the SCUDO standalone allocator as SCUDO pulls
39*71db0c75SAndroid Build Coastguard Worker // various other parts of the libc. Since SCUDO development does not use
40*71db0c75SAndroid Build Coastguard Worker // LLVM libc build rules, it is very hard to keep track or pull all that SCUDO
41*71db0c75SAndroid Build Coastguard Worker // requires. Hence, as a work around for this problem, we use a simple allocator
42*71db0c75SAndroid Build Coastguard Worker // which just hands out continuous blocks from a statically allocated chunk of
43*71db0c75SAndroid Build Coastguard Worker // memory.
44*71db0c75SAndroid Build Coastguard Worker static constexpr uint64_t MEMORY_SIZE = 65336;
45*71db0c75SAndroid Build Coastguard Worker static uint8_t memory[MEMORY_SIZE];
46*71db0c75SAndroid Build Coastguard Worker static uint8_t *ptr = memory;
47*71db0c75SAndroid Build Coastguard Worker 
48*71db0c75SAndroid Build Coastguard Worker } // anonymous namespace
49*71db0c75SAndroid Build Coastguard Worker 
50*71db0c75SAndroid Build Coastguard Worker extern "C" {
51*71db0c75SAndroid Build Coastguard Worker 
52*71db0c75SAndroid Build Coastguard Worker // Hermetic tests rely on the following memory functions. This is because the
53*71db0c75SAndroid Build Coastguard Worker // compiler code generation can emit calls to them. We want to map the external
54*71db0c75SAndroid Build Coastguard Worker // entrypoint to the internal implementation of the function used for testing.
55*71db0c75SAndroid Build Coastguard Worker // This is done manually as not all targets support aliases.
56*71db0c75SAndroid Build Coastguard Worker 
bcmp(const void * lhs,const void * rhs,size_t count)57*71db0c75SAndroid Build Coastguard Worker int bcmp(const void *lhs, const void *rhs, size_t count) {
58*71db0c75SAndroid Build Coastguard Worker   return LIBC_NAMESPACE::bcmp(lhs, rhs, count);
59*71db0c75SAndroid Build Coastguard Worker }
bzero(void * ptr,size_t count)60*71db0c75SAndroid Build Coastguard Worker void bzero(void *ptr, size_t count) { LIBC_NAMESPACE::bzero(ptr, count); }
memcmp(const void * lhs,const void * rhs,size_t count)61*71db0c75SAndroid Build Coastguard Worker int memcmp(const void *lhs, const void *rhs, size_t count) {
62*71db0c75SAndroid Build Coastguard Worker   return LIBC_NAMESPACE::memcmp(lhs, rhs, count);
63*71db0c75SAndroid Build Coastguard Worker }
memcpy(void * __restrict dst,const void * __restrict src,size_t count)64*71db0c75SAndroid Build Coastguard Worker void *memcpy(void *__restrict dst, const void *__restrict src, size_t count) {
65*71db0c75SAndroid Build Coastguard Worker   return LIBC_NAMESPACE::memcpy(dst, src, count);
66*71db0c75SAndroid Build Coastguard Worker }
memmove(void * dst,const void * src,size_t count)67*71db0c75SAndroid Build Coastguard Worker void *memmove(void *dst, const void *src, size_t count) {
68*71db0c75SAndroid Build Coastguard Worker   return LIBC_NAMESPACE::memmove(dst, src, count);
69*71db0c75SAndroid Build Coastguard Worker }
memset(void * ptr,int value,size_t count)70*71db0c75SAndroid Build Coastguard Worker void *memset(void *ptr, int value, size_t count) {
71*71db0c75SAndroid Build Coastguard Worker   return LIBC_NAMESPACE::memset(ptr, value, count);
72*71db0c75SAndroid Build Coastguard Worker }
73*71db0c75SAndroid Build Coastguard Worker 
74*71db0c75SAndroid Build Coastguard Worker // This is needed if the test was compiled with '-fno-use-cxa-atexit'.
atexit(void (* func)(void))75*71db0c75SAndroid Build Coastguard Worker int atexit(void (*func)(void)) { return LIBC_NAMESPACE::atexit(func); }
76*71db0c75SAndroid Build Coastguard Worker 
77*71db0c75SAndroid Build Coastguard Worker constexpr uint64_t ALIGNMENT = alignof(uintptr_t);
78*71db0c75SAndroid Build Coastguard Worker 
malloc(size_t s)79*71db0c75SAndroid Build Coastguard Worker void *malloc(size_t s) {
80*71db0c75SAndroid Build Coastguard Worker   // Keep the bump pointer aligned on an eight byte boundary.
81*71db0c75SAndroid Build Coastguard Worker   s = ((s + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
82*71db0c75SAndroid Build Coastguard Worker   void *mem = ptr;
83*71db0c75SAndroid Build Coastguard Worker   ptr += s;
84*71db0c75SAndroid Build Coastguard Worker   return static_cast<uint64_t>(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;
85*71db0c75SAndroid Build Coastguard Worker }
86*71db0c75SAndroid Build Coastguard Worker 
free(void *)87*71db0c75SAndroid Build Coastguard Worker void free(void *) {}
88*71db0c75SAndroid Build Coastguard Worker 
realloc(void * mem,size_t s)89*71db0c75SAndroid Build Coastguard Worker void *realloc(void *mem, size_t s) {
90*71db0c75SAndroid Build Coastguard Worker   if (mem == nullptr)
91*71db0c75SAndroid Build Coastguard Worker     return malloc(s);
92*71db0c75SAndroid Build Coastguard Worker   uint8_t *newmem = reinterpret_cast<uint8_t *>(malloc(s));
93*71db0c75SAndroid Build Coastguard Worker   if (newmem == nullptr)
94*71db0c75SAndroid Build Coastguard Worker     return nullptr;
95*71db0c75SAndroid Build Coastguard Worker   uint8_t *oldmem = reinterpret_cast<uint8_t *>(mem);
96*71db0c75SAndroid Build Coastguard Worker   // We use a simple for loop to copy the data over.
97*71db0c75SAndroid Build Coastguard Worker   // If |s| is less the previous alloc size, the copy works as expected.
98*71db0c75SAndroid Build Coastguard Worker   // If |s| is greater than the previous alloc size, then garbage is copied
99*71db0c75SAndroid Build Coastguard Worker   // over to the additional part in the new memory block.
100*71db0c75SAndroid Build Coastguard Worker   for (size_t i = 0; i < s; ++i)
101*71db0c75SAndroid Build Coastguard Worker     newmem[i] = oldmem[i];
102*71db0c75SAndroid Build Coastguard Worker   return newmem;
103*71db0c75SAndroid Build Coastguard Worker }
104*71db0c75SAndroid Build Coastguard Worker 
105*71db0c75SAndroid Build Coastguard Worker // The unit test framework uses pure virtual functions. Since hermetic tests
106*71db0c75SAndroid Build Coastguard Worker // cannot depend C++ runtime libraries, implement dummy functions to support
107*71db0c75SAndroid Build Coastguard Worker // the virtual function runtime.
__cxa_pure_virtual()108*71db0c75SAndroid Build Coastguard Worker void __cxa_pure_virtual() {
109*71db0c75SAndroid Build Coastguard Worker   // A pure virtual being called is an error so we just trap.
110*71db0c75SAndroid Build Coastguard Worker   __builtin_trap();
111*71db0c75SAndroid Build Coastguard Worker }
112*71db0c75SAndroid Build Coastguard Worker 
113*71db0c75SAndroid Build Coastguard Worker // Hermetic tests are linked with -nostdlib. BFD linker expects
114*71db0c75SAndroid Build Coastguard Worker // __dso_handle when -nostdlib is used.
115*71db0c75SAndroid Build Coastguard Worker void *__dso_handle = nullptr;
116*71db0c75SAndroid Build Coastguard Worker 
117*71db0c75SAndroid Build Coastguard Worker #ifdef LIBC_TARGET_ARCH_IS_AARCH64
118*71db0c75SAndroid Build Coastguard Worker // Due to historical reasons, libgcc on aarch64 may expect __getauxval to be
119*71db0c75SAndroid Build Coastguard Worker // defined. See also https://gcc.gnu.org/pipermail/gcc-cvs/2020-June/300635.html
__getauxval(unsigned long id)120*71db0c75SAndroid Build Coastguard Worker unsigned long __getauxval(unsigned long id) {
121*71db0c75SAndroid Build Coastguard Worker   return LIBC_NAMESPACE::getauxval(id);
122*71db0c75SAndroid Build Coastguard Worker }
123*71db0c75SAndroid Build Coastguard Worker #endif
124*71db0c75SAndroid Build Coastguard Worker 
125*71db0c75SAndroid Build Coastguard Worker } // extern "C"
126*71db0c75SAndroid Build Coastguard Worker 
operator new(size_t size,void * ptr)127*71db0c75SAndroid Build Coastguard Worker void *operator new(size_t size, void *ptr) { return ptr; }
128*71db0c75SAndroid Build Coastguard Worker 
operator new(size_t size)129*71db0c75SAndroid Build Coastguard Worker void *operator new(size_t size) { return malloc(size); }
130*71db0c75SAndroid Build Coastguard Worker 
operator new[](size_t size)131*71db0c75SAndroid Build Coastguard Worker void *operator new[](size_t size) { return malloc(size); }
132*71db0c75SAndroid Build Coastguard Worker 
operator delete(void *)133*71db0c75SAndroid Build Coastguard Worker void operator delete(void *) {
134*71db0c75SAndroid Build Coastguard Worker   // The libc runtime should not use the global delete operator. Hence,
135*71db0c75SAndroid Build Coastguard Worker   // we just trap here to catch any such accidental usages.
136*71db0c75SAndroid Build Coastguard Worker   __builtin_trap();
137*71db0c75SAndroid Build Coastguard Worker }
138*71db0c75SAndroid Build Coastguard Worker 
operator delete(void * ptr,size_t size)139*71db0c75SAndroid Build Coastguard Worker void operator delete(void *ptr, size_t size) { __builtin_trap(); }
140*71db0c75SAndroid Build Coastguard Worker 
141*71db0c75SAndroid Build Coastguard Worker // Defining members in the std namespace is not preferred. But, we do it here
142*71db0c75SAndroid Build Coastguard Worker // so that we can use it to define the operator new which takes std::align_val_t
143*71db0c75SAndroid Build Coastguard Worker // argument.
144*71db0c75SAndroid Build Coastguard Worker namespace std {
145*71db0c75SAndroid Build Coastguard Worker enum class align_val_t : size_t {};
146*71db0c75SAndroid Build Coastguard Worker } // namespace std
147*71db0c75SAndroid Build Coastguard Worker 
operator delete(void * mem,std::align_val_t)148*71db0c75SAndroid Build Coastguard Worker void operator delete(void *mem, std::align_val_t) noexcept { __builtin_trap(); }
149*71db0c75SAndroid Build Coastguard Worker 
operator delete(void * mem,unsigned int,std::align_val_t)150*71db0c75SAndroid Build Coastguard Worker void operator delete(void *mem, unsigned int, std::align_val_t) noexcept {
151*71db0c75SAndroid Build Coastguard Worker   __builtin_trap();
152*71db0c75SAndroid Build Coastguard Worker }
153