1 // RUN: %clang_dfsan -mllvm -dfsan-combine-pointer-labels-on-load=false %s -o %t && %run %t
2 // RUN: %clang_dfsan -DORIGIN_TRACKING -mllvm -dfsan-track-origins=1 -mllvm -dfsan-combine-pointer-labels-on-load=false %s -o %t && %run %t
4 // Tests custom implementations of various glibc functions.
6 // REQUIRES: x86_64-target-arch
8 #include <sanitizer/dfsan_interface.h>
16 #define ASSERT_ZERO_LABEL(data) \
17 assert(0 == dfsan_get_label((long) (data)))
19 #define ASSERT_READ_ZERO_LABEL(ptr, size) \
20 assert(0 == dfsan_read_label(ptr, size))
22 const int kAlignment
= 8;
25 void test_aligned_alloc() {
26 char *p
= (char *) aligned_alloc(kAlignment
, kSize
);
28 ASSERT_READ_ZERO_LABEL(p
, kSize
);
33 char *p
= (char *) calloc(kSize
, 1);
35 ASSERT_READ_ZERO_LABEL(p
, kSize
);
40 // The current glibc does not support cfree.
44 char *p
= (char *) malloc(kSize
);
45 dfsan_set_label(1, p
, kSize
);
47 ASSERT_READ_ZERO_LABEL(p
, kSize
);
50 void test_mallinfo() {
51 struct mallinfo mi
= mallinfo();
52 for (int i
= 0; i
< sizeof(struct mallinfo
); ++i
) {
53 char c
= ((char *)(&mi
))[i
];
60 char *p
= (char *) malloc(kSize
);
62 ASSERT_READ_ZERO_LABEL(p
, kSize
);
66 void test_malloc_stats() {
67 // Only ensures it does not crash. Our interceptor of malloc_stats is empty.
71 void test_malloc_usable_size() {
72 char *p
= (char *) malloc(kSize
);
73 size_t s
= malloc_usable_size(p
);
80 int r
= mallopt(0, 0);
85 void test_memalign() {
86 char *p
= (char *) memalign(kAlignment
, kSize
);
88 ASSERT_READ_ZERO_LABEL(p
, kSize
);
93 char *p
= mmap(NULL
, kSize
, PROT_READ
| PROT_WRITE
,
94 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
95 ASSERT_READ_ZERO_LABEL(p
, kSize
);
97 dfsan_set_label(1, &val
, sizeof(val
));
98 memset(p
, val
, kSize
);
99 p
= mmap(p
, kSize
, PROT_READ
| PROT_WRITE
,
100 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
101 ASSERT_READ_ZERO_LABEL(p
, kSize
);
106 // The current glibc does not support mmap64.
110 char *p
= mmap(NULL
, kSize
, PROT_READ
| PROT_WRITE
,
111 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
113 dfsan_set_label(1, &val
, sizeof(val
));
114 memset(p
, val
, kSize
);
116 ASSERT_READ_ZERO_LABEL(p
, kSize
);
119 void test_posix_memalign() {
121 dfsan_set_label(1, &p
, sizeof(p
));
122 int r
= posix_memalign((void **)&p
, kAlignment
, kSize
);
124 ASSERT_ZERO_LABEL(p
);
125 ASSERT_READ_ZERO_LABEL(p
, kSize
);
129 void test_pvalloc() {
130 char *p
= (char *) pvalloc(kSize
);
131 ASSERT_ZERO_LABEL(p
);
132 ASSERT_READ_ZERO_LABEL(p
, kSize
);
136 void test_realloc() {
137 char *p
= (char *) malloc(kSize
);
139 char *q
= (char *) realloc(p
, kSize
* 2);
140 ASSERT_ZERO_LABEL(q
);
141 ASSERT_READ_ZERO_LABEL(q
, kSize
* 2);
143 char *x
= (char *) realloc(q
, kSize
);
144 ASSERT_ZERO_LABEL(x
);
145 ASSERT_READ_ZERO_LABEL(x
, kSize
);
150 void test_reallocarray() {
151 // The current glibc does not support reallocarray.
155 char *p
= (char *) valloc(kSize
);
156 ASSERT_ZERO_LABEL(p
);
157 ASSERT_READ_ZERO_LABEL(p
, kSize
);
161 void test___libc_memalign() {
162 // The current glibc does not support __libc_memalign.
165 void test___tls_get_addr() {
166 // The current glibc does not support __tls_get_addr.
170 // With any luck this sequence of calls will cause allocators to return the
171 // same pointer. This is probably the best we can do to test these functions.
172 test_aligned_alloc();
179 test_malloc_usable_size();
185 test_posix_memalign();
190 test___libc_memalign();
191 test___tls_get_addr();