1// Test the weak hooks.
2// RUN: %clangxx %s -o %t && %run %t
3
4// Must not be implemented, no other reason to install interceptors.
5// XFAIL: lsan, ubsan
6
7// FIXME: Implement.
8// XFAIL: hwasan
9
10#include <assert.h>
11#include <string.h>
12#if defined(_GNU_SOURCE)
13#include <strings.h> // for bcmp
14#endif
15
16bool seen_memcmp, seen_strncmp, seen_strncasecmp, seen_strcmp, seen_strcasecmp,
17 seen_strstr, seen_strcasestr, seen_memmem;
18
19extern "C" {
20void __sanitizer_weak_hook_memcmp(void *called_pc, const void *s1,
21 const void *s2, size_t n, int result) {
22 seen_memcmp = true;
23}
24void __sanitizer_weak_hook_strncmp(void *called_pc, const char *s1,
25 const char *s2, size_t n, int result) {
26 seen_strncmp = true;
27}
28void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
29 const char *s2, size_t n, int result){
30 seen_strncasecmp = true;
31}
32void __sanitizer_weak_hook_strcmp(void *called_pc, const char *s1,
33 const char *s2, int result){
34 seen_strcmp = true;
35}
36void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
37 const char *s2, int result){
38 seen_strcasecmp = true;
39}
40void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
41 const char *s2, char *result){
42 seen_strstr = true;
43}
44void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
45 const char *s2, char *result){
46 seen_strcasestr = true;
47}
48void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
49 const void *s2, size_t len2, void *result){
50 seen_memmem = true;
51}
52} // extern "C"
53
54char s1[] = "ABCDEF";
55char s2[] = "CDE";
56
57static volatile int int_sink;
58static volatile void *ptr_sink;
59
60int main() {
61 assert(sizeof(s2) < sizeof(s1));
62
63 int_sink = memcmp(s1: s1, s2: s2, n: sizeof(s2));
64 assert(seen_memcmp);
65
66#if (defined(__linux__) && !defined(__ANDROID__) && defined(_GNU_SOURCE)) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
67 seen_memcmp = false;
68 int_sink = bcmp(s1: s1, s2: s2, n: sizeof(s2));
69 assert(seen_memcmp);
70#endif
71
72 int_sink = strncmp(s1: s1, s2: s2, n: sizeof(s2));
73 assert(seen_strncmp);
74
75 int_sink = strncasecmp(s1: s1, s2: s2, n: sizeof(s2));
76 assert(seen_strncasecmp);
77
78 int_sink = strcmp(s1: s1, s2: s2);
79 assert(seen_strcmp);
80
81 int_sink = strcasecmp(s1: s1, s2: s2);
82 assert(seen_strcasecmp);
83
84 ptr_sink = strstr(haystack: s1, needle: s2);
85 assert(seen_strstr);
86
87 ptr_sink = strcasestr(haystack: s1, needle: s2);
88 assert(seen_strcasestr);
89
90 ptr_sink = memmem(haystack: s1, haystacklen: sizeof(s1), needle: s2, needlelen: sizeof(s2));
91 assert(seen_memmem);
92 return 0;
93}
94

source code of compiler-rt/test/sanitizer_common/TestCases/Posix/weak_hook_test.cpp