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
23namespace LIBC_NAMESPACE_DECL {
24
25int bcmp(const void *lhs, const void *rhs, size_t count);
26void bzero(void *ptr, size_t count);
27int memcmp(const void *lhs, const void *rhs, size_t count);
28void *memcpy(void *__restrict, const void *__restrict, size_t);
29void *memmove(void *dst, const void *src, size_t count);
30void *memset(void *ptr, int value, size_t count);
31int atexit(void (*func)(void));
32
33} // namespace LIBC_NAMESPACE_DECL
34
35extern "C" {
36
37int bcmp(const void *lhs, const void *rhs, size_t count) {
38 return LIBC_NAMESPACE::bcmp(lhs, rhs, count);
39}
40void bzero(void *ptr, size_t count) { LIBC_NAMESPACE::bzero(ptr, count); }
41int memcmp(const void *lhs, const void *rhs, size_t count) {
42 return LIBC_NAMESPACE::memcmp(lhs, rhs, count);
43}
44void *memcpy(void *__restrict dst, const void *__restrict src, size_t count) {
45 return LIBC_NAMESPACE::memcpy(dst, src, count);
46}
47void *memmove(void *dst, const void *src, size_t count) {
48 return LIBC_NAMESPACE::memmove(dst, src, count);
49}
50void *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'.
55int 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
66static constexpr uint64_t MEMORY_SIZE = 16384;
67static uint8_t memory[MEMORY_SIZE];
68static uint8_t *ptr = memory;
69
70extern "C" {
71
72void *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
78void free(void *) {}
79
80void *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.
87void *__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
92unsigned long __getauxval(unsigned long id) {
93 return LIBC_NAMESPACE::getauxval(id);
94}
95#endif
96} // extern "C"
97

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of libc/test/IntegrationTest/test.cpp