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