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 <stddef.h> 10 #include <stdint.h> 11 12 // Integration tests rely on the following memory functions. This is because the 13 // compiler code generation can emit calls to them. We want to map the external 14 // entrypoint to the internal implementation of the function used for testing. 15 // This is done manually as not all targets support aliases. 16 17 namespace __llvm_libc { 18 19 int bcmp(const void *lhs, const void *rhs, size_t count); 20 void bzero(void *ptr, size_t count); 21 int memcmp(const void *lhs, const void *rhs, size_t count); 22 void *memcpy(void *__restrict, const void *__restrict, size_t); 23 void *memmove(void *dst, const void *src, size_t count); 24 void *memset(void *ptr, int value, size_t count); 25 int atexit(void (*func)(void)); 26 27 } // namespace __llvm_libc 28 29 extern "C" { 30 31 int bcmp(const void *lhs, const void *rhs, size_t count) { 32 return __llvm_libc::bcmp(lhs, rhs, count); 33 } 34 void bzero(void *ptr, size_t count) { __llvm_libc::bzero(ptr, count); } 35 int memcmp(const void *lhs, const void *rhs, size_t count) { 36 return __llvm_libc::memcmp(lhs, rhs, count); 37 } 38 void *memcpy(void *__restrict dst, const void *__restrict src, size_t count) { 39 return __llvm_libc::memcpy(dst, src, count); 40 } 41 void *memmove(void *dst, const void *src, size_t count) { 42 return __llvm_libc::memmove(dst, src, count); 43 } 44 void *memset(void *ptr, int value, size_t count) { 45 return __llvm_libc::memset(ptr, value, count); 46 } 47 48 // This is needed if the test was compiled with '-fno-use-cxa-atexit'. 49 int atexit(void (*func)(void)) { return __llvm_libc::atexit(func); } 50 51 } // extern "C" 52 53 // Integration tests cannot use the SCUDO standalone allocator as SCUDO pulls 54 // various other parts of the libc. Since SCUDO development does not use 55 // LLVM libc build rules, it is very hard to keep track or pull all that SCUDO 56 // requires. Hence, as a work around for this problem, we use a simple allocator 57 // which just hands out continuous blocks from a statically allocated chunk of 58 // memory. 59 60 static uint8_t memory[16384]; 61 static uint8_t *ptr = memory; 62 63 extern "C" { 64 65 void *malloc(size_t s) { 66 void *mem = ptr; 67 ptr += s; 68 return mem; 69 } 70 71 void free(void *) {} 72 73 void *realloc(void *ptr, size_t s) { 74 free(ptr); 75 return malloc(s); 76 } 77 78 // Integration tests are linked with -nostdlib. BFD linker expects 79 // __dso_handle when -nostdlib is used. 80 void *__dso_handle = nullptr; 81 } // extern "C" 82