summaryrefslogtreecommitdiffstats
path: root/compiler-rt/test/sanitizer_common/TestCases/Posix/weak_hook_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'compiler-rt/test/sanitizer_common/TestCases/Posix/weak_hook_test.cpp')
-rw-r--r--compiler-rt/test/sanitizer_common/TestCases/Posix/weak_hook_test.cpp92
1 files changed, 92 insertions, 0 deletions
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/weak_hook_test.cpp b/compiler-rt/test/sanitizer_common/TestCases/Posix/weak_hook_test.cpp
new file mode 100644
index 00000000000..ba98f47b35e
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/weak_hook_test.cpp
@@ -0,0 +1,92 @@
+// Test the weak hooks.
+// RUN: %clangxx %s -o %t
+// RUN: %run %t
+
+// Hooks are not implemented for lsan.
+// XFAIL: lsan
+// XFAIL: ubsan
+
+#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;
+}
OpenPOWER on IntegriCloud