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
15 static const char *buf1 = "aaaa";
16 static const char *buf2 = "bbbb";
17
callFuzzerInterceptors(const char * prefix)18 static void callFuzzerInterceptors(const char *prefix) {
19 int memcmp_result = memcmp(buf1, buf2, 4);
20 if (memcmp_result != 0) {
21 fprintf(stderr, "%s-MEMCMP\n", prefix);
22 }
23 int strncmp_result = strncmp(buf1, buf2, 4);
24 if (strncmp_result != 0) {
25 fprintf(stderr, "%s-STRNCMP\n", prefix);
26 }
27 int strcmp_result = strcmp(buf1, buf2);
28 if (strcmp_result != 0) {
29 fprintf(stderr, "%s-STRCMP\n", prefix);
30 }
31 const char *strstr_result = strstr(buf1, buf2);
32 if (strstr_result == nullptr) {
33 fprintf(stderr, "%s-STRSTR\n", prefix);
34 }
35 }
36
37 extern "C" void *__libc_calloc(size_t, size_t);
38
calloc(size_t n,size_t elem_size)39 extern "C" void *calloc(size_t n, size_t elem_size) {
40 static bool CalledOnce = false;
41 if (!CalledOnce) {
42 callFuzzerInterceptors("CALLOC");
43 CalledOnce = true;
44 }
45 return __libc_calloc(n, elem_size);
46 }
47