xref: /llvm-project/libc/test/UnitTest/HermeticTestUtils.cpp (revision 73799b46072c2241ae32c87f478a7e2a30c0e1a3)
11e8960c7SSiva Chandra Reddy //===-- Implementation of libc death test executors -----------------------===//
21e8960c7SSiva Chandra Reddy //
31e8960c7SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41e8960c7SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
51e8960c7SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61e8960c7SSiva Chandra Reddy //
71e8960c7SSiva Chandra Reddy //===----------------------------------------------------------------------===//
81e8960c7SSiva Chandra Reddy 
92efe3d7fSSchrodinger ZHU Yifan #include "src/__support/common.h"
105ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
111e8960c7SSiva Chandra Reddy #include <stddef.h>
121e8960c7SSiva Chandra Reddy #include <stdint.h>
131e8960c7SSiva Chandra Reddy 
14ffed34e0SJoseph Huber #ifdef LIBC_TARGET_ARCH_IS_AARCH64
15ffed34e0SJoseph Huber #include "src/sys/auxv/getauxval.h"
16ffed34e0SJoseph Huber #endif
17ffed34e0SJoseph Huber 
185ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
191e8960c7SSiva Chandra Reddy 
201e8960c7SSiva Chandra Reddy int bcmp(const void *lhs, const void *rhs, size_t count);
211e8960c7SSiva Chandra Reddy void bzero(void *ptr, size_t count);
221e8960c7SSiva Chandra Reddy int memcmp(const void *lhs, const void *rhs, size_t count);
231e8960c7SSiva Chandra Reddy void *memcpy(void *__restrict, const void *__restrict, size_t);
241e8960c7SSiva Chandra Reddy void *memmove(void *dst, const void *src, size_t count);
251e8960c7SSiva Chandra Reddy void *memset(void *ptr, int value, size_t count);
26f4002c14SJoseph Huber int atexit(void (*func)(void));
271e8960c7SSiva Chandra Reddy 
282efe3d7fSSchrodinger ZHU Yifan // TODO: It seems that some old test frameworks does not use
292efe3d7fSSchrodinger ZHU Yifan // add_libc_hermetic_test properly. Such that they won't get correct linkage
302efe3d7fSSchrodinger ZHU Yifan // against the object containing this function. We create a dummy function that
312efe3d7fSSchrodinger ZHU Yifan // always returns 0 to indicate a failure.
322efe3d7fSSchrodinger ZHU Yifan [[gnu::weak]] unsigned long getauxval(unsigned long id) { return 0; }
332efe3d7fSSchrodinger ZHU Yifan 
345ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
351e8960c7SSiva Chandra Reddy 
361e8960c7SSiva Chandra Reddy namespace {
371e8960c7SSiva Chandra Reddy 
381e8960c7SSiva Chandra Reddy // Integration tests cannot use the SCUDO standalone allocator as SCUDO pulls
391e8960c7SSiva Chandra Reddy // various other parts of the libc. Since SCUDO development does not use
401e8960c7SSiva Chandra Reddy // LLVM libc build rules, it is very hard to keep track or pull all that SCUDO
411e8960c7SSiva Chandra Reddy // requires. Hence, as a work around for this problem, we use a simple allocator
421e8960c7SSiva Chandra Reddy // which just hands out continuous blocks from a statically allocated chunk of
431e8960c7SSiva Chandra Reddy // memory.
44a5d98be5SSiva Chandra Reddy static constexpr uint64_t MEMORY_SIZE = 65336;
459417d9fcSJoseph Huber static uint8_t memory[MEMORY_SIZE];
461e8960c7SSiva Chandra Reddy static uint8_t *ptr = memory;
471e8960c7SSiva Chandra Reddy 
481e8960c7SSiva Chandra Reddy } // anonymous namespace
491e8960c7SSiva Chandra Reddy 
501e8960c7SSiva Chandra Reddy extern "C" {
511e8960c7SSiva Chandra Reddy 
521e8960c7SSiva Chandra Reddy // Hermetic tests rely on the following memory functions. This is because the
531e8960c7SSiva Chandra Reddy // compiler code generation can emit calls to them. We want to map the external
541e8960c7SSiva Chandra Reddy // entrypoint to the internal implementation of the function used for testing.
551e8960c7SSiva Chandra Reddy // This is done manually as not all targets support aliases.
561e8960c7SSiva Chandra Reddy 
571e8960c7SSiva Chandra Reddy int bcmp(const void *lhs, const void *rhs, size_t count) {
58b6bc9d72SGuillaume Chatelet   return LIBC_NAMESPACE::bcmp(lhs, rhs, count);
591e8960c7SSiva Chandra Reddy }
60b6bc9d72SGuillaume Chatelet void bzero(void *ptr, size_t count) { LIBC_NAMESPACE::bzero(ptr, count); }
611e8960c7SSiva Chandra Reddy int memcmp(const void *lhs, const void *rhs, size_t count) {
62b6bc9d72SGuillaume Chatelet   return LIBC_NAMESPACE::memcmp(lhs, rhs, count);
631e8960c7SSiva Chandra Reddy }
641e8960c7SSiva Chandra Reddy void *memcpy(void *__restrict dst, const void *__restrict src, size_t count) {
65b6bc9d72SGuillaume Chatelet   return LIBC_NAMESPACE::memcpy(dst, src, count);
661e8960c7SSiva Chandra Reddy }
671e8960c7SSiva Chandra Reddy void *memmove(void *dst, const void *src, size_t count) {
68b6bc9d72SGuillaume Chatelet   return LIBC_NAMESPACE::memmove(dst, src, count);
691e8960c7SSiva Chandra Reddy }
701e8960c7SSiva Chandra Reddy void *memset(void *ptr, int value, size_t count) {
71b6bc9d72SGuillaume Chatelet   return LIBC_NAMESPACE::memset(ptr, value, count);
721e8960c7SSiva Chandra Reddy }
731e8960c7SSiva Chandra Reddy 
74f4002c14SJoseph Huber // This is needed if the test was compiled with '-fno-use-cxa-atexit'.
75b6bc9d72SGuillaume Chatelet int atexit(void (*func)(void)) { return LIBC_NAMESPACE::atexit(func); }
76f4002c14SJoseph Huber 
77cbcf55d3SJoseph Huber constexpr uint64_t ALIGNMENT = alignof(uintptr_t);
78cbcf55d3SJoseph Huber 
791e8960c7SSiva Chandra Reddy void *malloc(size_t s) {
80cbcf55d3SJoseph Huber   // Keep the bump pointer aligned on an eight byte boundary.
81cbcf55d3SJoseph Huber   s = ((s + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
821e8960c7SSiva Chandra Reddy   void *mem = ptr;
831e8960c7SSiva Chandra Reddy   ptr += s;
849417d9fcSJoseph Huber   return static_cast<uint64_t>(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;
851e8960c7SSiva Chandra Reddy }
861e8960c7SSiva Chandra Reddy 
871e8960c7SSiva Chandra Reddy void free(void *) {}
881e8960c7SSiva Chandra Reddy 
89d3b33042SSiva Chandra Reddy void *realloc(void *mem, size_t s) {
90d3b33042SSiva Chandra Reddy   if (mem == nullptr)
911e8960c7SSiva Chandra Reddy     return malloc(s);
92d3b33042SSiva Chandra Reddy   uint8_t *newmem = reinterpret_cast<uint8_t *>(malloc(s));
93d3b33042SSiva Chandra Reddy   if (newmem == nullptr)
94d3b33042SSiva Chandra Reddy     return nullptr;
95d3b33042SSiva Chandra Reddy   uint8_t *oldmem = reinterpret_cast<uint8_t *>(mem);
96d3b33042SSiva Chandra Reddy   // We use a simple for loop to copy the data over.
97d3b33042SSiva Chandra Reddy   // If |s| is less the previous alloc size, the copy works as expected.
98d3b33042SSiva Chandra Reddy   // If |s| is greater than the previous alloc size, then garbage is copied
99d3b33042SSiva Chandra Reddy   // over to the additional part in the new memory block.
100d3b33042SSiva Chandra Reddy   for (size_t i = 0; i < s; ++i)
101d3b33042SSiva Chandra Reddy     newmem[i] = oldmem[i];
102d3b33042SSiva Chandra Reddy   return newmem;
1031e8960c7SSiva Chandra Reddy }
1041e8960c7SSiva Chandra Reddy 
1051e8960c7SSiva Chandra Reddy // The unit test framework uses pure virtual functions. Since hermetic tests
1061e8960c7SSiva Chandra Reddy // cannot depend C++ runtime libraries, implement dummy functions to support
1071e8960c7SSiva Chandra Reddy // the virtual function runtime.
1081e8960c7SSiva Chandra Reddy void __cxa_pure_virtual() {
1091e8960c7SSiva Chandra Reddy   // A pure virtual being called is an error so we just trap.
1101e8960c7SSiva Chandra Reddy   __builtin_trap();
1111e8960c7SSiva Chandra Reddy }
1121e8960c7SSiva Chandra Reddy 
113f4002c14SJoseph Huber // Hermetic tests are linked with -nostdlib. BFD linker expects
1141e8960c7SSiva Chandra Reddy // __dso_handle when -nostdlib is used.
1151e8960c7SSiva Chandra Reddy void *__dso_handle = nullptr;
1161e8960c7SSiva Chandra Reddy 
1172efe3d7fSSchrodinger ZHU Yifan #ifdef LIBC_TARGET_ARCH_IS_AARCH64
1182efe3d7fSSchrodinger ZHU Yifan // Due to historical reasons, libgcc on aarch64 may expect __getauxval to be
1192efe3d7fSSchrodinger ZHU Yifan // defined. See also https://gcc.gnu.org/pipermail/gcc-cvs/2020-June/300635.html
1202efe3d7fSSchrodinger ZHU Yifan unsigned long __getauxval(unsigned long id) {
1212efe3d7fSSchrodinger ZHU Yifan   return LIBC_NAMESPACE::getauxval(id);
1222efe3d7fSSchrodinger ZHU Yifan }
1232efe3d7fSSchrodinger ZHU Yifan #endif
1242efe3d7fSSchrodinger ZHU Yifan 
1251e8960c7SSiva Chandra Reddy } // extern "C"
1261e8960c7SSiva Chandra Reddy 
127*73799b46SMikhail R. Gadelha void *operator new(size_t size, void *ptr) { return ptr; }
128029bfd63Slntue 
129b932f03bSmichaelrj-google void *operator new(size_t size) { return malloc(size); }
130b932f03bSmichaelrj-google 
131b932f03bSmichaelrj-google void *operator new[](size_t size) { return malloc(size); }
132b932f03bSmichaelrj-google 
1331e8960c7SSiva Chandra Reddy void operator delete(void *) {
1341e8960c7SSiva Chandra Reddy   // The libc runtime should not use the global delete operator. Hence,
1351e8960c7SSiva Chandra Reddy   // we just trap here to catch any such accidental usages.
1361e8960c7SSiva Chandra Reddy   __builtin_trap();
1371e8960c7SSiva Chandra Reddy }
138029bfd63Slntue 
139029bfd63Slntue void operator delete(void *ptr, size_t size) { __builtin_trap(); }
140*73799b46SMikhail R. Gadelha 
141*73799b46SMikhail R. Gadelha // Defining members in the std namespace is not preferred. But, we do it here
142*73799b46SMikhail R. Gadelha // so that we can use it to define the operator new which takes std::align_val_t
143*73799b46SMikhail R. Gadelha // argument.
144*73799b46SMikhail R. Gadelha namespace std {
145*73799b46SMikhail R. Gadelha enum class align_val_t : size_t {};
146*73799b46SMikhail R. Gadelha } // namespace std
147*73799b46SMikhail R. Gadelha 
148*73799b46SMikhail R. Gadelha void operator delete(void *mem, std::align_val_t) noexcept { __builtin_trap(); }
149*73799b46SMikhail R. Gadelha 
150*73799b46SMikhail R. Gadelha void operator delete(void *mem, unsigned int, std::align_val_t) noexcept {
151*73799b46SMikhail R. Gadelha   __builtin_trap();
152*73799b46SMikhail R. Gadelha }
153