Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / sanitizer_common / TestCases / Posix / weak_hook_test.cpp
blobe95de739fe784ca06212995e928ca8e3078f0c6b
1 // Test the weak hooks.
2 // RUN: %clangxx %s -o %t && %run %t
4 // Must not be implemented, no other reason to install interceptors.
5 // XFAIL: lsan, ubsan
7 // FIXME: Implement.
8 // XFAIL: hwasan
10 #include <assert.h>
11 #include <string.h>
12 #if defined(_GNU_SOURCE)
13 #include <strings.h> // for bcmp
14 #endif
16 bool seen_memcmp, seen_strncmp, seen_strncasecmp, seen_strcmp, seen_strcasecmp,
17 seen_strstr, seen_strcasestr, seen_memmem;
19 extern "C" {
20 void __sanitizer_weak_hook_memcmp(void *called_pc, const void *s1,
21 const void *s2, size_t n, int result) {
22 seen_memcmp = true;
24 void __sanitizer_weak_hook_strncmp(void *called_pc, const char *s1,
25 const char *s2, size_t n, int result) {
26 seen_strncmp = true;
28 void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
29 const char *s2, size_t n, int result){
30 seen_strncasecmp = true;
32 void __sanitizer_weak_hook_strcmp(void *called_pc, const char *s1,
33 const char *s2, int result){
34 seen_strcmp = true;
36 void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
37 const char *s2, int result){
38 seen_strcasecmp = true;
40 void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
41 const char *s2, char *result){
42 seen_strstr = true;
44 void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
45 const char *s2, char *result){
46 seen_strcasestr = true;
48 void __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;
52 } // extern "C"
54 char s1[] = "ABCDEF";
55 char s2[] = "CDE";
57 static volatile int int_sink;
58 static volatile void *ptr_sink;
60 int main() {
61 assert(sizeof(s2) < sizeof(s1));
63 int_sink = memcmp(s1, s2, sizeof(s2));
64 assert(seen_memcmp);
66 #if (defined(__linux__) && !defined(__ANDROID__) && defined(_GNU_SOURCE)) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
67 seen_memcmp = false;
68 int_sink = bcmp(s1, s2, sizeof(s2));
69 assert(seen_memcmp);
70 #endif
72 int_sink = strncmp(s1, s2, sizeof(s2));
73 assert(seen_strncmp);
75 int_sink = strncasecmp(s1, s2, sizeof(s2));
76 assert(seen_strncasecmp);
78 int_sink = strcmp(s1, s2);
79 assert(seen_strcmp);
81 int_sink = strcasecmp(s1, s2);
82 assert(seen_strcasecmp);
84 ptr_sink = strstr(s1, s2);
85 assert(seen_strstr);
87 ptr_sink = strcasestr(s1, s2);
88 assert(seen_strcasestr);
90 ptr_sink = memmem(s1, sizeof(s1), s2, sizeof(s2));
91 assert(seen_memmem);
92 return 0;