| 1 | //===-- Simple malloc and free for use with integration tests -------------===// |
| 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 "src/__support/common.h" |
| 10 | #include "src/__support/macros/config.h" |
| 11 | #include <stddef.h> |
| 12 | #include <stdint.h> |
| 13 | |
| 14 | #ifdef LIBC_TARGET_ARCH_IS_AARCH64 |
| 15 | #include "src/sys/auxv/getauxval.h" |
| 16 | #endif |
| 17 | |
| 18 | // Integration tests rely on the following memory functions. This is because the |
| 19 | // compiler code generation can emit calls to them. We want to map the external |
| 20 | // entrypoint to the internal implementation of the function used for testing. |
| 21 | // This is done manually as not all targets support aliases. |
| 22 | |
| 23 | namespace LIBC_NAMESPACE_DECL { |
| 24 | |
| 25 | int bcmp(const void *lhs, const void *rhs, size_t count); |
| 26 | void bzero(void *ptr, size_t count); |
| 27 | int memcmp(const void *lhs, const void *rhs, size_t count); |
| 28 | void *memcpy(void *__restrict, const void *__restrict, size_t); |
| 29 | void *memmove(void *dst, const void *src, size_t count); |
| 30 | void *memset(void *ptr, int value, size_t count); |
| 31 | int atexit(void (*func)(void)); |
| 32 | |
| 33 | } // namespace LIBC_NAMESPACE_DECL |
| 34 | |
| 35 | extern "C" { |
| 36 | |
| 37 | int bcmp(const void *lhs, const void *rhs, size_t count) { |
| 38 | return LIBC_NAMESPACE::bcmp(lhs, rhs, count); |
| 39 | } |
| 40 | void bzero(void *ptr, size_t count) { LIBC_NAMESPACE::bzero(ptr, count); } |
| 41 | int memcmp(const void *lhs, const void *rhs, size_t count) { |
| 42 | return LIBC_NAMESPACE::memcmp(lhs, rhs, count); |
| 43 | } |
| 44 | void *memcpy(void *__restrict dst, const void *__restrict src, size_t count) { |
| 45 | return LIBC_NAMESPACE::memcpy(dst, src, count); |
| 46 | } |
| 47 | void *memmove(void *dst, const void *src, size_t count) { |
| 48 | return LIBC_NAMESPACE::memmove(dst, src, count); |
| 49 | } |
| 50 | void *memset(void *ptr, int value, size_t count) { |
| 51 | return LIBC_NAMESPACE::memset(ptr, value, count); |
| 52 | } |
| 53 | |
| 54 | // This is needed if the test was compiled with '-fno-use-cxa-atexit'. |
| 55 | int atexit(void (*func)(void)) { return LIBC_NAMESPACE::atexit(func); } |
| 56 | |
| 57 | } // extern "C" |
| 58 | |
| 59 | // Integration tests cannot use the SCUDO standalone allocator as SCUDO pulls |
| 60 | // various other parts of the libc. Since SCUDO development does not use |
| 61 | // LLVM libc build rules, it is very hard to keep track or pull all that SCUDO |
| 62 | // requires. Hence, as a work around for this problem, we use a simple allocator |
| 63 | // which just hands out continuous blocks from a statically allocated chunk of |
| 64 | // memory. |
| 65 | |
| 66 | static constexpr uint64_t MEMORY_SIZE = 16384; |
| 67 | static uint8_t memory[MEMORY_SIZE]; |
| 68 | static uint8_t *ptr = memory; |
| 69 | |
| 70 | extern "C" { |
| 71 | |
| 72 | void *malloc(size_t s) { |
| 73 | void *mem = ptr; |
| 74 | ptr += s; |
| 75 | return static_cast<uint64_t>(ptr - memory) >= MEMORY_SIZE ? nullptr : mem; |
| 76 | } |
| 77 | |
| 78 | void free(void *) {} |
| 79 | |
| 80 | void *realloc(void *ptr, size_t s) { |
| 81 | free(ptr); |
| 82 | return malloc(s); |
| 83 | } |
| 84 | |
| 85 | // Integration tests are linked with -nostdlib. BFD linker expects |
| 86 | // __dso_handle when -nostdlib is used. |
| 87 | void *__dso_handle = nullptr; |
| 88 | |
| 89 | #ifdef LIBC_TARGET_ARCH_IS_AARCH64 |
| 90 | // Due to historical reasons, libgcc on aarch64 may expect __getauxval to be |
| 91 | // defined. See also https://gcc.gnu.org/pipermail/gcc-cvs/2020-June/300635.html |
| 92 | unsigned long __getauxval(unsigned long id) { |
| 93 | return LIBC_NAMESPACE::getauxval(id); |
| 94 | } |
| 95 | #endif |
| 96 | } // extern "C" |
| 97 | |