aboutsummaryrefslogtreecommitdiff
path: root/test/sanitizer_common/TestCases/Posix
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2018-07-28 11:06:48 +0000
committerDimitry Andric <dim@FreeBSD.org>2018-07-28 11:06:48 +0000
commit93c1b73a09a52d4a265f683bf1954b08bb430049 (patch)
tree5543464d74945196cc890e9d9099e5d0660df7eb /test/sanitizer_common/TestCases/Posix
parent0d8e7490d6e8a13a8f0977d9b7771803b9f64ea0 (diff)
downloadsrc-93c1b73a09a52d4a265f683bf1954b08bb430049.tar.gz
src-93c1b73a09a52d4a265f683bf1954b08bb430049.zip
Vendor import of compiler-rt trunk r338150:vendor/compiler-rt/compiler-rt-trunk-r338150
Notes
Notes: svn path=/vendor/compiler-rt/dist/; revision=336817 svn path=/vendor/compiler-rt/compiler-rt-trunk-r338150/; revision=336818; tag=vendor/compiler-rt/compiler-rt-trunk-r338150
Diffstat (limited to 'test/sanitizer_common/TestCases/Posix')
-rw-r--r--test/sanitizer_common/TestCases/Posix/access.cc5
-rw-r--r--test/sanitizer_common/TestCases/Posix/devname.cc23
-rw-r--r--test/sanitizer_common/TestCases/Posix/devname_r.cc28
-rw-r--r--test/sanitizer_common/TestCases/Posix/fgetln.cc24
-rw-r--r--test/sanitizer_common/TestCases/Posix/fgets.cc20
-rw-r--r--test/sanitizer_common/TestCases/Posix/fputs_puts.cc18
-rw-r--r--test/sanitizer_common/TestCases/Posix/getpass.cc8
-rw-r--r--test/sanitizer_common/TestCases/Posix/illegal_read_test.cc15
-rw-r--r--test/sanitizer_common/TestCases/Posix/illegal_write_test.cc14
-rw-r--r--test/sanitizer_common/TestCases/Posix/lstat.cc16
-rw-r--r--test/sanitizer_common/TestCases/Posix/mmap_test.c11
-rw-r--r--test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc47
-rw-r--r--test/sanitizer_common/TestCases/Posix/readlink.c26
-rw-r--r--test/sanitizer_common/TestCases/Posix/readlinkat.c26
-rw-r--r--test/sanitizer_common/TestCases/Posix/sanitizer_set_death_callback_test.cc11
-rw-r--r--test/sanitizer_common/TestCases/Posix/strlcat.cc54
-rw-r--r--test/sanitizer_common/TestCases/Posix/strlcpy.cc54
-rw-r--r--test/sanitizer_common/TestCases/Posix/strxfrm.c20
-rw-r--r--test/sanitizer_common/TestCases/Posix/wcsxfrm.c20
19 files changed, 432 insertions, 8 deletions
diff --git a/test/sanitizer_common/TestCases/Posix/access.cc b/test/sanitizer_common/TestCases/Posix/access.cc
new file mode 100644
index 000000000000..8623645f8bdf
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/access.cc
@@ -0,0 +1,5 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t
+
+#include <unistd.h>
+
+int main(void) { return access("/dev/null", F_OK); }
diff --git a/test/sanitizer_common/TestCases/Posix/devname.cc b/test/sanitizer_common/TestCases/Posix/devname.cc
new file mode 100644
index 000000000000..da4bb8853a12
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/devname.cc
@@ -0,0 +1,23 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
+// UNSUPPORTED: linux, solaris
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+
+int main(void) {
+ struct stat st;
+ char *name;
+
+ if (stat("/dev/null", &st))
+ exit(1);
+
+ if (!(name = devname(st.st_rdev, S_ISCHR(st.st_mode) ? S_IFCHR : S_IFBLK)))
+ exit(1);
+
+ printf("%s\n", name);
+
+ // CHECK: null
+
+ return 0;
+}
diff --git a/test/sanitizer_common/TestCases/Posix/devname_r.cc b/test/sanitizer_common/TestCases/Posix/devname_r.cc
new file mode 100644
index 000000000000..826b7c92ef2f
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/devname_r.cc
@@ -0,0 +1,28 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
+// UNSUPPORTED: linux, solaris
+
+#include <sys/cdefs.h>
+#include <sys/stat.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(void) {
+ struct stat st;
+ char name[100];
+ mode_t type;
+
+ if (stat("/dev/null", &st))
+ exit(1);
+
+ type = S_ISCHR(st.st_mode) ? S_IFCHR : S_IFBLK;
+
+ if (!devname_r(st.st_rdev, type, name, sizeof(name)))
+ exit(1);
+
+ printf("%s\n", name);
+
+ // CHECK: null
+
+ return 0;
+}
diff --git a/test/sanitizer_common/TestCases/Posix/fgetln.cc b/test/sanitizer_common/TestCases/Posix/fgetln.cc
new file mode 100644
index 000000000000..e98cf449a272
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/fgetln.cc
@@ -0,0 +1,24 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t
+// UNSUPPORTED: linux
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(void) {
+ FILE *fp;
+ size_t len;
+ char *s;
+
+ fp = fopen("/etc/hosts", "r");
+ if (!fp)
+ exit(1);
+
+ s = fgetln(fp, &len);
+
+ printf("%.*s\n", (int)len, s);
+
+ if (fclose(fp) == EOF)
+ exit(1);
+
+ return 0;
+}
diff --git a/test/sanitizer_common/TestCases/Posix/fgets.cc b/test/sanitizer_common/TestCases/Posix/fgets.cc
new file mode 100644
index 000000000000..8dde5cd1a84f
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/fgets.cc
@@ -0,0 +1,20 @@
+// RUN: %clangxx -g %s -o %t && %run %t
+
+#include <stdio.h>
+
+int main(int argc, char **argv) {
+ FILE *fp;
+ char buf[2];
+ char *s;
+
+ fp = fopen(argv[0], "r");
+ if (!fp)
+ return 1;
+
+ s = fgets(buf, sizeof(buf), fp);
+ if (!s)
+ return 2;
+
+ fclose(fp);
+ return 0;
+}
diff --git a/test/sanitizer_common/TestCases/Posix/fputs_puts.cc b/test/sanitizer_common/TestCases/Posix/fputs_puts.cc
new file mode 100644
index 000000000000..8e8f7d384e8c
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/fputs_puts.cc
@@ -0,0 +1,18 @@
+// RUN: %clangxx -g %s -o %t && %run %t | FileCheck %s
+// CHECK: {{^foobar$}}
+
+#include <stdio.h>
+
+int main(void) {
+ int r;
+
+ r = fputs("foo", stdout);
+ if (r < 0)
+ return 1;
+
+ r = puts("bar");
+ if (r < 0)
+ return 1;
+
+ return 0;
+}
diff --git a/test/sanitizer_common/TestCases/Posix/getpass.cc b/test/sanitizer_common/TestCases/Posix/getpass.cc
index b91a3d7d5264..bf198eff91d2 100644
--- a/test/sanitizer_common/TestCases/Posix/getpass.cc
+++ b/test/sanitizer_common/TestCases/Posix/getpass.cc
@@ -5,13 +5,19 @@
#include <assert.h>
#include <stdio.h>
-#include <unistd.h>
#include <string.h>
#if __linux__
#include <pty.h>
+#elif defined(__FreeBSD__)
+#include <libutil.h>
+#include <pwd.h>
+#include <sys/ioctl.h>
+#include <sys/termios.h>
+#include <sys/types.h>
#else
#include <util.h>
#endif
+#include <unistd.h>
int
main (int argc, char** argv)
diff --git a/test/sanitizer_common/TestCases/Posix/illegal_read_test.cc b/test/sanitizer_common/TestCases/Posix/illegal_read_test.cc
new file mode 100644
index 000000000000..9615d7132da5
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/illegal_read_test.cc
@@ -0,0 +1,15 @@
+// Test that there was an illegal READ memory access.
+// RUN: %clangxx -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
+
+// REQUIRES: stable-runtime
+// XFAIL: powerpc64, s390x
+
+volatile int *null = 0;
+volatile int a;
+
+int main(int argc, char **argv) {
+ a = *null;
+ return 0;
+}
+
+// CHECK: The signal is caused by a READ memory access.
diff --git a/test/sanitizer_common/TestCases/Posix/illegal_write_test.cc b/test/sanitizer_common/TestCases/Posix/illegal_write_test.cc
new file mode 100644
index 000000000000..13d1c6a06905
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/illegal_write_test.cc
@@ -0,0 +1,14 @@
+// Test that there was an illegal WRITE memory access.
+// RUN: %clangxx -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
+
+// REQUIRES: stable-runtime
+// XFAIL: powerpc64, s390x
+
+volatile int *null = 0;
+
+int main(int argc, char **argv) {
+ *null = 0;
+ return 0;
+}
+
+// CHECK: The signal is caused by a WRITE memory access.
diff --git a/test/sanitizer_common/TestCases/Posix/lstat.cc b/test/sanitizer_common/TestCases/Posix/lstat.cc
new file mode 100644
index 000000000000..37237d82102c
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/lstat.cc
@@ -0,0 +1,16 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t
+
+#include <stdlib.h>
+#include <sys/stat.h>
+
+int main(void) {
+ struct stat st;
+
+ if (lstat("/dev/null", &st))
+ exit(1);
+
+ if (!S_ISCHR(st.st_mode))
+ exit(1);
+
+ return 0;
+}
diff --git a/test/sanitizer_common/TestCases/Posix/mmap_test.c b/test/sanitizer_common/TestCases/Posix/mmap_test.c
new file mode 100644
index 000000000000..3c272f95a015
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/mmap_test.c
@@ -0,0 +1,11 @@
+// RUN: %clang %s -o %t && %run %t
+
+#include <assert.h>
+#include <sys/mman.h>
+
+int main() {
+ char *buf = (char *)mmap(0, 100000, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ assert(buf);
+ munmap(buf, 100000);
+}
diff --git a/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc b/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc
new file mode 100644
index 000000000000..7729057d2deb
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc
@@ -0,0 +1,47 @@
+// RUN: %clangxx %collect_stack_traces -O0 %s -o %t
+
+// Alignment is not a power of two:
+// RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t 17 2>&1 | FileCheck %s
+// Alignment is not a power of two, although is a multiple of sizeof(void*):
+// RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t 24 2>&1 | FileCheck %s
+// Alignment is not a multiple of sizeof(void*), although is a power of 2:
+// RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t 2 2>&1 | FileCheck %s
+// Alignment is 0:
+// RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t 0 2>&1 | FileCheck %s
+
+// The same for allocator_may_return_null=1:
+// RUN: %env_tool_opts=allocator_may_return_null=1 %run %t 17 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
+// RUN: %env_tool_opts=allocator_may_return_null=1 %run %t 24 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
+// RUN: %env_tool_opts=allocator_may_return_null=1 %run %t 2 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
+// RUN: %env_tool_opts=allocator_may_return_null=1 %run %t 0 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
+
+// REQUIRES: stable-runtime
+
+// UNSUPPORTED: ubsan
+
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char **argv) {
+ assert(argc == 2);
+ const int alignment = atoi(argv[1]);
+
+ void* const kInitialPtrValue = reinterpret_cast<void*>(0x2a);
+ void *p = kInitialPtrValue;
+
+ errno = 0;
+ int res = posix_memalign(&p, alignment, 100);
+ // CHECK: {{ERROR: .*Sanitizer: invalid alignment requested in posix_memalign}}
+ // CHECK: {{#0 .*posix_memalign}}
+ // CHECK: {{#1 .*main .*posix_memalign-alignment.cc:}}[[@LINE-3]]
+ // CHECK: {{SUMMARY: .*Sanitizer: invalid-posix-memalign-alignment}}
+
+ // The NULL pointer is printed differently on different systems, while (long)0
+ // is always the same.
+ fprintf(stderr, "errno: %d, res: %d, p: %lx\n", errno, res, (long)p);
+ // CHECK-NULL: errno: 0, res: 22, p: 2a
+
+ return 0;
+}
diff --git a/test/sanitizer_common/TestCases/Posix/readlink.c b/test/sanitizer_common/TestCases/Posix/readlink.c
new file mode 100644
index 000000000000..ef0a4fe358b0
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/readlink.c
@@ -0,0 +1,26 @@
+// RUN: %clang -O0 %s -o %t && %run %t
+
+#include <assert.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+int main(int argc, char **argv) {
+ char symlink_path[PATH_MAX];
+ snprintf(symlink_path, sizeof(symlink_path), "%s_%d.symlink", argv[0],
+ getpid());
+ remove(symlink_path);
+ int res = symlink(argv[0], symlink_path);
+ assert(!res);
+
+ char readlink_path[PATH_MAX];
+ ssize_t res2 = readlink(symlink_path, readlink_path, sizeof(readlink_path));
+ assert(res2 >= 0);
+ readlink_path[res2] = '\0';
+ assert(!strcmp(readlink_path, argv[0]));
+
+ return 0;
+}
diff --git a/test/sanitizer_common/TestCases/Posix/readlinkat.c b/test/sanitizer_common/TestCases/Posix/readlinkat.c
new file mode 100644
index 000000000000..0afb5efe6b5f
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/readlinkat.c
@@ -0,0 +1,26 @@
+// RUN: %clang -O0 %s -o %t && %run %t
+
+#include <assert.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+int main(int argc, char **argv) {
+ char symlink_path[PATH_MAX];
+ snprintf(symlink_path, sizeof(symlink_path), "%s_%d.symlink", argv[0],
+ getpid());
+ remove(symlink_path);
+ int res = symlink(argv[0], symlink_path);
+ assert(!res);
+
+ char readlinkat_path[PATH_MAX];
+ int res2 = readlinkat(AT_FDCWD, symlink_path, readlinkat_path,
+ sizeof(readlinkat_path));
+ assert(res2 >= 0);
+ readlinkat_path[res2] = '\0';
+ assert(!strcmp(readlinkat_path, argv[0]));
+
+ return 0;
+}
diff --git a/test/sanitizer_common/TestCases/Posix/sanitizer_set_death_callback_test.cc b/test/sanitizer_common/TestCases/Posix/sanitizer_set_death_callback_test.cc
index 8d2db364114a..54272b017504 100644
--- a/test/sanitizer_common/TestCases/Posix/sanitizer_set_death_callback_test.cc
+++ b/test/sanitizer_common/TestCases/Posix/sanitizer_set_death_callback_test.cc
@@ -2,12 +2,6 @@
// REQUIRES: stable-runtime
-// For standalone LSan on x86 we have a problem: compiler spills the address
-// of allocated at line 42 memory thus memory block allocated in Leak() function
-// ends up to be classified as reachable despite the fact we zero out 'sink' at
-// the last line of main function. The problem doesn't reproduce with ASan because
-// quarantine prohibits memory block reuse for different allocations.
-// XFAIL: lsan-x86
// XFAIL: ubsan
#include <sanitizer/common_interface_defs.h>
@@ -31,7 +25,10 @@ void MaybeInit(int *uninitialized) {
__attribute__((noinline))
void Leak() {
- sink = new char[100]; // trigger lsan report.
+ // Trigger lsan report. Two attempts in case the address of the first
+ // allocation remained on the stack.
+ sink = new char[100];
+ sink = new char[100];
}
int main(int argc, char **argv) {
diff --git a/test/sanitizer_common/TestCases/Posix/strlcat.cc b/test/sanitizer_common/TestCases/Posix/strlcat.cc
new file mode 100644
index 000000000000..bdabada76aa7
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/strlcat.cc
@@ -0,0 +1,54 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t
+
+// UNSUPPORTED: linux
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void test1() {
+ const char src[] = "abc";
+ char dst[7] = {'x', 'y', 'z', 0};
+ size_t len;
+
+ len = strlcat(dst, src, sizeof(dst));
+ printf("%s %zu ", dst, len);
+}
+
+void test2() {
+ const char src[] = "abc";
+ char dst[7] = {0};
+ size_t len;
+
+ len = strlcat(dst, src, sizeof(dst));
+ printf("%s %zu ", dst, len);
+}
+
+void test3() {
+ const char src[] = "abc";
+ char dst[4] = {'x', 'y', 'z', 0};
+ size_t len;
+
+ len = strlcat(dst, src, sizeof(dst));
+ printf("%s %zu ", dst, len);
+}
+
+void test4() {
+ const char src[] = "";
+ char dst[4] = {'x', 'y', 'z', 0};
+ size_t len;
+
+ len = strlcat(dst, src, sizeof(dst));
+ printf("%s %zu\n", dst, len);
+}
+
+int main(void) {
+ test1();
+ test2();
+ test3();
+ test4();
+
+ // CHECK: xyzabc 6 abc 3 xyz 3 xyz 3
+
+ return 0;
+}
diff --git a/test/sanitizer_common/TestCases/Posix/strlcpy.cc b/test/sanitizer_common/TestCases/Posix/strlcpy.cc
new file mode 100644
index 000000000000..83053911d965
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/strlcpy.cc
@@ -0,0 +1,54 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t
+
+// UNSUPPORTED: linux
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void test1() {
+ const char src[] = "abc";
+ char dst[7] = {'x', 'y', 'z', 0};
+ size_t len;
+
+ len = strlcpy(dst, src, sizeof(dst));
+ printf("%s %zu ", dst, len);
+}
+
+void test2() {
+ const char src[] = "abc";
+ char dst[7] = {0};
+ size_t len;
+
+ len = strlcat(dst, src, sizeof(dst));
+ printf("%s %zu ", dst, len);
+}
+
+void test3() {
+ const char src[] = "abc";
+ char dst[4] = {'x', 'y', 'z', 0};
+ size_t len;
+
+ len = strlcat(dst, src, sizeof(dst));
+ printf("%s %zu ", dst, len);
+}
+
+void test4() {
+ const char src[] = "";
+ char dst[4] = {'x', 'y', 'z', 0};
+ size_t len;
+
+ len = strlcat(dst, src, sizeof(dst));
+ printf("%s %zu\n", dst, len);
+}
+
+int main(void) {
+ test1();
+ test2();
+ test3();
+ test4();
+
+ // CHECK: abc 3 abc 3 xyz 3 0
+
+ return 0;
+}
diff --git a/test/sanitizer_common/TestCases/Posix/strxfrm.c b/test/sanitizer_common/TestCases/Posix/strxfrm.c
new file mode 100644
index 000000000000..c28eb65b7d4f
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/strxfrm.c
@@ -0,0 +1,20 @@
+// RUN: %clang -O0 %s -o %t && %run %t
+// UNSUPPORTED: darwin
+
+#include <assert.h>
+#include <locale.h>
+#include <wchar.h>
+
+int main(int argc, char **argv) {
+ char q[10];
+ size_t n = strxfrm(q, "abcdef", sizeof(q));
+ assert(n < sizeof(q));
+
+ char q2[10];
+ locale_t loc = newlocale(LC_ALL_MASK, "", (locale_t)0);
+ n = strxfrm_l(q2, L"qwerty", sizeof(q), loc);
+ assert(n < sizeof(q2));
+
+ freelocale(loc);
+ return 0;
+}
diff --git a/test/sanitizer_common/TestCases/Posix/wcsxfrm.c b/test/sanitizer_common/TestCases/Posix/wcsxfrm.c
new file mode 100644
index 000000000000..3e349c75f409
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Posix/wcsxfrm.c
@@ -0,0 +1,20 @@
+// RUN: %clang -O0 %s -o %t && %run %t
+// UNSUPPORTED: darwin
+
+#include <assert.h>
+#include <locale.h>
+#include <wchar.h>
+
+int main(int argc, char **argv) {
+ wchar_t q[10];
+ size_t n = wcsxfrm(q, L"abcdef", sizeof(q) / sizeof(wchar_t));
+ assert(n < sizeof(q));
+
+ wchar_t q2[10];
+ locale_t loc = newlocale(LC_ALL_MASK, "", (locale_t)0);
+ n = wcsxfrm_l(q2, L"qwerty", sizeof(q) / sizeof(wchar_t), loc);
+ assert(n < sizeof(q2));
+
+ freelocale(loc);
+ return 0;
+}