1// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2// See https://llvm.org/LICENSE.txt for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5// Test whether calling certain libFuzzer's interceptors inside allocators
6// does not cause an assertion failure.
7#include <assert.h>
8#include <cstddef>
9#include <cstdint>
10#include <cstdlib>
11#include <cstring>
12#include <iostream>
13#include <malloc.h>
14
15static const char *buf1 = "aaaa";
16static const char *buf2 = "bbbb";
17
18static void callFuzzerInterceptors(const char *prefix) {
19 int memcmp_result = memcmp(s1: buf1, s2: buf2, n: 4);
20 if (memcmp_result != 0) {
21 fprintf(stderr, format: "%s-MEMCMP\n", prefix);
22 }
23 int strncmp_result = strncmp(s1: buf1, s2: buf2, n: 4);
24 if (strncmp_result != 0) {
25 fprintf(stderr, format: "%s-STRNCMP\n", prefix);
26 }
27 int strcmp_result = strcmp(s1: buf1, s2: buf2);
28 if (strcmp_result != 0) {
29 fprintf(stderr, format: "%s-STRCMP\n", prefix);
30 }
31 const char *strstr_result = strstr(haystack: buf1, needle: buf2);
32 if (strstr_result == nullptr) {
33 fprintf(stderr, format: "%s-STRSTR\n", prefix);
34 }
35}
36
37extern "C" void *__libc_calloc(size_t, size_t);
38
39extern "C" void *calloc(size_t n, size_t elem_size) {
40 static bool CalledOnce = false;
41 if (!CalledOnce) {
42 callFuzzerInterceptors(prefix: "CALLOC");
43 CalledOnce = true;
44 }
45 return __libc_calloc(n, elem_size);
46}
47

source code of compiler-rt/test/fuzzer/CustomAllocator.cpp