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