File: weak_hook_test.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (93 lines) | stat: -rw-r--r-- 2,712 bytes parent folder | download | duplicates (17)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Test the weak hooks.
// RUN: %clangxx %s -o %t && %run %t

// Must not be implemented, no other reason to install interceptors.
// XFAIL: lsan, ubsan

// FIXME: Implement.
// XFAIL: hwasan

#include <assert.h>
#include <string.h>
#if defined(_GNU_SOURCE)
#include <strings.h> // for bcmp
#endif

bool seen_memcmp, seen_strncmp, seen_strncasecmp, seen_strcmp, seen_strcasecmp,
    seen_strstr, seen_strcasestr, seen_memmem;

extern "C" {
void __sanitizer_weak_hook_memcmp(void *called_pc, const void *s1,
                                  const void *s2, size_t n, int result) {
  seen_memcmp = true;
}
void __sanitizer_weak_hook_strncmp(void *called_pc, const char *s1,
                                   const char *s2, size_t n, int result) {
  seen_strncmp = true;
}
void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
                                       const char *s2, size_t n, int result){
  seen_strncasecmp = true;
}
void __sanitizer_weak_hook_strcmp(void *called_pc, const char *s1,
                                  const char *s2, int result){
  seen_strcmp = true;
}
void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
                                      const char *s2, int result){
  seen_strcasecmp = true;
}
void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
                                  const char *s2, char *result){
  seen_strstr = true;
}
void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
                                      const char *s2, char *result){
  seen_strcasestr = true;
}
void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
                                  const void *s2, size_t len2, void *result){
  seen_memmem = true;
}
} // extern "C"

char s1[] = "ABCDEF";
char s2[] = "CDE";

static volatile int int_sink;
static volatile void *ptr_sink;

int main() {
  assert(sizeof(s2) < sizeof(s1));

  int_sink = memcmp(s1, s2, sizeof(s2));
  assert(seen_memcmp);

#if (defined(__linux__) && !defined(__ANDROID__) && defined(_GNU_SOURCE)) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  seen_memcmp = false;
  int_sink = bcmp(s1, s2, sizeof(s2));
  assert(seen_memcmp);
#endif

  int_sink = strncmp(s1, s2, sizeof(s2));
  assert(seen_strncmp);

  int_sink = strncasecmp(s1, s2, sizeof(s2));
  assert(seen_strncasecmp);

  int_sink = strcmp(s1, s2);
  assert(seen_strcmp);

  int_sink = strcasecmp(s1, s2);
  assert(seen_strcasecmp);

  ptr_sink = strstr(s1, s2);
  assert(seen_strstr);

  ptr_sink = strcasestr(s1, s2);
  assert(seen_strcasestr);

  ptr_sink = memmem(s1, sizeof(s1), s2, sizeof(s2));
  assert(seen_memmem);
  return 0;
}