160 lines · cpp
1//===-- Implementation of libc death test executors -----------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "hdr/stdint_proxy.h"10#include "src/__support/common.h"11#include "src/__support/macros/config.h"12#include <stddef.h>13 14#ifdef LIBC_TARGET_ARCH_IS_AARCH6415#include "src/sys/auxv/getauxval.h"16#endif17 18namespace LIBC_NAMESPACE_DECL {19 20int bcmp(const void *lhs, const void *rhs, size_t count);21void bzero(void *ptr, size_t count);22int memcmp(const void *lhs, const void *rhs, size_t count);23void *memcpy(void *__restrict, const void *__restrict, size_t);24void *memmove(void *dst, const void *src, size_t count);25void *memset(void *ptr, int value, size_t count);26int atexit(void (*func)(void));27 28// TODO: It seems that some old test frameworks does not use29// add_libc_hermetic_test properly. Such that they won't get correct linkage30// against the object containing this function. We create a dummy function that31// always returns 0 to indicate a failure.32[[gnu::weak]] unsigned long getauxval([[maybe_unused]] unsigned long id) {33 return 0;34}35 36} // namespace LIBC_NAMESPACE_DECL37 38constexpr uint64_t ALIGNMENT = alignof(uintptr_t);39 40namespace {41 42// Integration tests cannot use the SCUDO standalone allocator as SCUDO pulls43// various other parts of the libc. Since SCUDO development does not use44// LLVM libc build rules, it is very hard to keep track or pull all that SCUDO45// requires. Hence, as a work around for this problem, we use a simple allocator46// which just hands out continuous blocks from a statically allocated chunk of47// memory.48static constexpr uint64_t MEMORY_SIZE = 65336;49alignas(ALIGNMENT) static uint8_t memory[MEMORY_SIZE];50static uint8_t *ptr = memory;51 52} // anonymous namespace53 54extern "C" {55 56// Hermetic tests rely on the following memory functions. This is because the57// compiler code generation can emit calls to them. We want to map the external58// entrypoint to the internal implementation of the function used for testing.59// This is done manually as not all targets support aliases.60 61int bcmp(const void *lhs, const void *rhs, size_t count) {62 return LIBC_NAMESPACE::bcmp(lhs, rhs, count);63}64void bzero(void *ptr, size_t count) { LIBC_NAMESPACE::bzero(ptr, count); }65int memcmp(const void *lhs, const void *rhs, size_t count) {66 return LIBC_NAMESPACE::memcmp(lhs, rhs, count);67}68void *memcpy(void *__restrict dst, const void *__restrict src, size_t count) {69 return LIBC_NAMESPACE::memcpy(dst, src, count);70}71void *memmove(void *dst, const void *src, size_t count) {72 return LIBC_NAMESPACE::memmove(dst, src, count);73}74void *memset(void *ptr, int value, size_t count) {75 return LIBC_NAMESPACE::memset(ptr, value, count);76}77 78// This is needed if the test was compiled with '-fno-use-cxa-atexit'.79int atexit(void (*func)(void)) { return LIBC_NAMESPACE::atexit(func); }80 81void *malloc(size_t s) {82 // Keep the bump pointer aligned on an eight byte boundary.83 s = ((s + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;84 void *mem = ptr;85 ptr += s;86 return static_cast<uint64_t>(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;87}88 89void free(void *) {}90 91void *realloc(void *mem, size_t s) {92 if (mem == nullptr)93 return malloc(s);94 uint8_t *newmem = reinterpret_cast<uint8_t *>(malloc(s));95 if (newmem == nullptr)96 return nullptr;97 uint8_t *oldmem = reinterpret_cast<uint8_t *>(mem);98 // We use a simple for loop to copy the data over.99 // If |s| is less the previous alloc size, the copy works as expected.100 // If |s| is greater than the previous alloc size, then garbage is copied101 // over to the additional part in the new memory block.102 for (size_t i = 0; i < s; ++i)103 newmem[i] = oldmem[i];104 return newmem;105}106 107// The unit test framework uses pure virtual functions. Since hermetic tests108// cannot depend C++ runtime libraries, implement dummy functions to support109// the virtual function runtime.110void __cxa_pure_virtual() {111 // A pure virtual being called is an error so we just trap.112 __builtin_trap();113}114 115// Hermetic tests are linked with -nostdlib. BFD linker expects116// __dso_handle when -nostdlib is used.117void *__dso_handle = nullptr;118 119#ifdef LIBC_TARGET_ARCH_IS_AARCH64120// Due to historical reasons, libgcc on aarch64 may expect __getauxval to be121// defined. See also https://gcc.gnu.org/pipermail/gcc-cvs/2020-June/300635.html122unsigned long __getauxval(unsigned long id) {123 return LIBC_NAMESPACE::getauxval(id);124}125#endif126 127} // extern "C"128 129void *operator new([[maybe_unused]] size_t size, void *ptr) { return ptr; }130 131void *operator new(size_t size) { return malloc(size); }132 133void *operator new[](size_t size) { return malloc(size); }134 135void operator delete(void *) {136 // The libc runtime should not use the global delete operator. Hence,137 // we just trap here to catch any such accidental usages.138 __builtin_trap();139}140 141void operator delete([[maybe_unused]] void *ptr, [[maybe_unused]] size_t size) {142 __builtin_trap();143}144 145// Defining members in the std namespace is not preferred. But, we do it here146// so that we can use it to define the operator new which takes std::align_val_t147// argument.148namespace std {149enum class align_val_t : size_t {};150} // namespace std151 152void operator delete([[maybe_unused]] void *mem, std::align_val_t) noexcept {153 __builtin_trap();154}155 156void operator delete([[maybe_unused]] void *mem, unsigned int,157 std::align_val_t) noexcept {158 __builtin_trap();159}160