]>
git.proxmox.com Git - rustc.git/blob - src/compiler-rt/test/dfsan/custom.cc
1 // RUN: %clang_dfsan %s -o %t && DFSAN_OPTIONS="strict_data_dependencies=0" %run %t
2 // RUN: %clang_dfsan -mllvm -dfsan-args-abi %s -o %t && DFSAN_OPTIONS="strict_data_dependencies=0" %run %t
3 // RUN: %clang_dfsan -DSTRICT_DATA_DEPENDENCIES %s -o %t && %run %t
4 // RUN: %clang_dfsan -DSTRICT_DATA_DEPENDENCIES -mllvm -dfsan-args-abi %s -o %t && %run %t
6 // Tests custom implementations of various glibc functions.
8 #include <sanitizer/dfsan_interface.h>
10 #include <arpa/inet.h>
23 #include <sys/select.h>
24 #include <sys/resource.h>
27 #include <sys/types.h>
31 dfsan_label i_label
= 0;
32 dfsan_label j_label
= 0;
33 dfsan_label k_label
= 0;
34 dfsan_label i_j_label
= 0;
36 #define ASSERT_ZERO_LABEL(data) \
37 assert(0 == dfsan_get_label((long) (data)))
39 #define ASSERT_READ_ZERO_LABEL(ptr, size) \
40 assert(0 == dfsan_read_label(ptr, size))
42 #define ASSERT_LABEL(data, label) \
43 assert(label == dfsan_get_label((long) (data)))
45 #define ASSERT_READ_LABEL(ptr, size, label) \
46 assert(label == dfsan_read_label(ptr, size))
50 dfsan_set_label(i_label
, &i
, sizeof(i
));
54 assert(0 == stat("/", &s
));
55 ASSERT_ZERO_LABEL(s
.st_dev
);
58 assert(-1 == stat("/nonexistent", &s
));
59 ASSERT_LABEL(s
.st_dev
, i_label
);
64 dfsan_set_label(i_label
, &i
, sizeof(i
));
67 int fd
= open("/dev/zero", O_RDONLY
);
69 int rv
= fstat(fd
, &s
);
71 ASSERT_ZERO_LABEL(s
.st_dev
);
75 char str1
[] = "str1", str2
[] = "str2";
76 dfsan_set_label(i_label
, &str1
[3], 1);
77 dfsan_set_label(j_label
, &str2
[3], 1);
79 int rv
= memcmp(str1
, str2
, sizeof(str1
));
81 #ifdef STRICT_DATA_DEPENDENCIES
82 ASSERT_ZERO_LABEL(rv
);
84 ASSERT_LABEL(rv
, i_j_label
);
90 char str2
[sizeof(str1
)];
91 dfsan_set_label(i_label
, &str1
[3], 1);
93 ASSERT_ZERO_LABEL(memcpy(str2
, str1
, sizeof(str1
)));
94 assert(0 == memcmp(str2
, str1
, sizeof(str1
)));
95 ASSERT_ZERO_LABEL(str2
[0]);
96 ASSERT_LABEL(str2
[3], i_label
);
102 dfsan_set_label(j_label
, &j
, sizeof(j
));
104 ASSERT_ZERO_LABEL(memset(&buf
, j
, sizeof(buf
)));
105 for (int i
= 0; i
< 8; ++i
) {
106 ASSERT_LABEL(buf
[i
], j_label
);
107 assert(buf
[i
] == 'a');
112 char str1
[] = "str1", str2
[] = "str2";
113 dfsan_set_label(i_label
, &str1
[3], 1);
114 dfsan_set_label(j_label
, &str2
[3], 1);
116 int rv
= strcmp(str1
, str2
);
118 #ifdef STRICT_DATA_DEPENDENCIES
119 ASSERT_ZERO_LABEL(rv
);
121 ASSERT_LABEL(rv
, i_j_label
);
126 char str1
[] = "str1";
127 dfsan_set_label(i_label
, &str1
[3], 1);
129 int rv
= strlen(str1
);
131 #ifdef STRICT_DATA_DEPENDENCIES
132 ASSERT_ZERO_LABEL(rv
);
134 ASSERT_LABEL(rv
, i_label
);
139 char str1
[] = "str1";
140 dfsan_set_label(i_label
, &str1
[3], 1);
142 char *strd
= strdup(str1
);
143 ASSERT_ZERO_LABEL(strd
[0]);
144 ASSERT_LABEL(strd
[3], i_label
);
148 void test_strncpy() {
149 char str1
[] = "str1";
150 char str2
[sizeof(str1
)];
151 dfsan_set_label(i_label
, &str1
[3], 1);
153 char *strd
= strncpy(str2
, str1
, 5);
154 assert(strd
== str2
);
155 assert(strcmp(str1
, str2
) == 0);
156 ASSERT_ZERO_LABEL(strd
);
157 ASSERT_ZERO_LABEL(strd
[0]);
158 ASSERT_ZERO_LABEL(strd
[1]);
159 ASSERT_ZERO_LABEL(strd
[2]);
160 ASSERT_LABEL(strd
[3], i_label
);
162 strd
= strncpy(str2
, str1
, 3);
163 assert(strd
== str2
);
164 assert(strncmp(str1
, str2
, 3) == 0);
165 ASSERT_ZERO_LABEL(strd
);
166 ASSERT_ZERO_LABEL(strd
[0]);
167 ASSERT_ZERO_LABEL(strd
[1]);
168 ASSERT_ZERO_LABEL(strd
[2]);
171 void test_strncmp() {
172 char str1
[] = "str1", str2
[] = "str2";
173 dfsan_set_label(i_label
, &str1
[3], 1);
174 dfsan_set_label(j_label
, &str2
[3], 1);
176 int rv
= strncmp(str1
, str2
, sizeof(str1
));
178 #ifdef STRICT_DATA_DEPENDENCIES
179 ASSERT_ZERO_LABEL(rv
);
181 ASSERT_LABEL(rv
, dfsan_union(i_label
, j_label
));
184 rv
= strncmp(str1
, str2
, 3);
186 ASSERT_ZERO_LABEL(rv
);
189 void test_strcasecmp() {
190 char str1
[] = "str1", str2
[] = "str2", str3
[] = "Str1";
191 dfsan_set_label(i_label
, &str1
[3], 1);
192 dfsan_set_label(j_label
, &str2
[3], 1);
193 dfsan_set_label(j_label
, &str3
[2], 1);
195 int rv
= strcasecmp(str1
, str2
);
197 #ifdef STRICT_DATA_DEPENDENCIES
198 ASSERT_ZERO_LABEL(rv
);
200 ASSERT_LABEL(rv
, dfsan_union(i_label
, j_label
));
203 rv
= strcasecmp(str1
, str3
);
205 #ifdef STRICT_DATA_DEPENDENCIES
206 ASSERT_ZERO_LABEL(rv
);
208 ASSERT_LABEL(rv
, dfsan_union(i_label
, j_label
));
212 void test_strncasecmp() {
213 char str1
[] = "Str1", str2
[] = "str2";
214 dfsan_set_label(i_label
, &str1
[3], 1);
215 dfsan_set_label(j_label
, &str2
[3], 1);
217 int rv
= strncasecmp(str1
, str2
, sizeof(str1
));
219 #ifdef STRICT_DATA_DEPENDENCIES
220 ASSERT_ZERO_LABEL(rv
);
222 ASSERT_LABEL(rv
, dfsan_union(i_label
, j_label
));
225 rv
= strncasecmp(str1
, str2
, 3);
227 ASSERT_ZERO_LABEL(rv
);
231 char str1
[] = "str1";
232 dfsan_set_label(i_label
, &str1
[3], 1);
234 char *crv
= strchr(str1
, 'r');
235 assert(crv
== &str1
[2]);
236 ASSERT_ZERO_LABEL(crv
);
238 crv
= strchr(str1
, '1');
239 assert(crv
== &str1
[3]);
240 #ifdef STRICT_DATA_DEPENDENCIES
241 ASSERT_ZERO_LABEL(crv
);
243 ASSERT_LABEL(crv
, i_label
);
246 crv
= strchr(str1
, 'x');
248 #ifdef STRICT_DATA_DEPENDENCIES
249 ASSERT_ZERO_LABEL(crv
);
251 ASSERT_LABEL(crv
, i_label
);
256 // With any luck this sequence of calls will cause calloc to return the same
257 // pointer both times. This is probably the best we can do to test this
259 char *crv
= (char *) calloc(4096, 1);
260 ASSERT_ZERO_LABEL(crv
[0]);
261 dfsan_set_label(i_label
, crv
, 100);
264 crv
= (char *) calloc(4096, 1);
265 ASSERT_ZERO_LABEL(crv
[0]);
271 dfsan_set_label(i_label
, buf
, 1);
272 dfsan_set_label(j_label
, buf
+ 15, 1);
274 ASSERT_LABEL(buf
[0], i_label
);
275 ASSERT_LABEL(buf
[15], j_label
);
277 int fd
= open("/dev/zero", O_RDONLY
);
278 int rv
= read(fd
, buf
, sizeof(buf
));
279 assert(rv
== sizeof(buf
));
280 ASSERT_ZERO_LABEL(rv
);
281 ASSERT_ZERO_LABEL(buf
[0]);
282 ASSERT_ZERO_LABEL(buf
[15]);
288 dfsan_set_label(i_label
, buf
, 1);
289 dfsan_set_label(j_label
, buf
+ 15, 1);
291 ASSERT_LABEL(buf
[0], i_label
);
292 ASSERT_LABEL(buf
[15], j_label
);
294 int fd
= open("/bin/sh", O_RDONLY
);
295 int rv
= pread(fd
, buf
, sizeof(buf
), 0);
296 assert(rv
== sizeof(buf
));
297 ASSERT_ZERO_LABEL(rv
);
298 ASSERT_ZERO_LABEL(buf
[0]);
299 ASSERT_ZERO_LABEL(buf
[15]);
304 void *map
= dlopen(NULL
, RTLD_NOW
);
306 ASSERT_ZERO_LABEL(map
);
308 map
= dlopen("/nonexistent", RTLD_NOW
);
310 ASSERT_ZERO_LABEL(map
);
313 void test_clock_gettime() {
315 dfsan_set_label(j_label
, ((char *)&tp
) + 3, 1);
316 int t
= clock_gettime(CLOCK_REALTIME
, &tp
);
318 ASSERT_ZERO_LABEL(t
);
319 ASSERT_ZERO_LABEL(((char *)&tp
)[3]);
322 void test_ctime_r() {
323 char *buf
= (char*) malloc(64);
326 char *ret
= ctime_r(&t
, buf
);
327 ASSERT_ZERO_LABEL(ret
);
329 ASSERT_READ_ZERO_LABEL(buf
, strlen(buf
) + 1);
331 dfsan_set_label(i_label
, &t
, sizeof(t
));
332 ret
= ctime_r(&t
, buf
);
333 ASSERT_ZERO_LABEL(ret
);
334 ASSERT_READ_LABEL(buf
, strlen(buf
) + 1, i_label
);
337 dfsan_set_label(j_label
, &buf
, sizeof(&buf
));
338 ret
= ctime_r(&t
, buf
);
339 ASSERT_LABEL(ret
, j_label
);
340 ASSERT_READ_ZERO_LABEL(buf
, strlen(buf
) + 1);
343 static int write_callback_count
= 0;
345 static const unsigned char *last_buf
;
346 static size_t last_count
;
348 void write_callback(int fd
, const void *buf
, size_t count
) {
349 write_callback_count
++;
352 last_buf
= (const unsigned char*) buf
;
356 void test_dfsan_set_write_callback() {
357 char buf
[] = "Sample chars";
358 int buf_len
= strlen(buf
);
360 int fd
= open("/dev/null", O_WRONLY
);
362 dfsan_set_write_callback(write_callback
);
364 write_callback_count
= 0;
366 // Callback should be invoked on every call to write().
367 int res
= write(fd
, buf
, buf_len
);
368 assert(write_callback_count
== 1);
369 ASSERT_READ_ZERO_LABEL(&res
, sizeof(res
));
370 ASSERT_READ_ZERO_LABEL(&last_fd
, sizeof(last_fd
));
371 ASSERT_READ_ZERO_LABEL(last_buf
, sizeof(last_buf
));
372 ASSERT_READ_ZERO_LABEL(&last_count
, sizeof(last_count
));
374 // Add a label to write() arguments. Check that the labels are readable from
375 // the values passed to the callback.
376 dfsan_set_label(i_label
, &fd
, sizeof(fd
));
377 dfsan_set_label(j_label
, &(buf
[3]), 1);
378 dfsan_set_label(k_label
, &buf_len
, sizeof(buf_len
));
380 res
= write(fd
, buf
, buf_len
);
381 assert(write_callback_count
== 2);
382 ASSERT_READ_ZERO_LABEL(&res
, sizeof(res
));
383 ASSERT_READ_LABEL(&last_fd
, sizeof(last_fd
), i_label
);
384 ASSERT_READ_LABEL(&last_buf
[3], sizeof(last_buf
[3]), j_label
);
385 ASSERT_READ_LABEL(last_buf
, sizeof(last_buf
), j_label
);
386 ASSERT_READ_LABEL(&last_count
, sizeof(last_count
), k_label
);
388 dfsan_set_write_callback(NULL
);
392 char *buf
= (char*) malloc(128);
393 FILE *f
= fopen("/etc/passwd", "r");
394 dfsan_set_label(j_label
, buf
, 1);
395 char *ret
= fgets(buf
, sizeof(buf
), f
);
397 ASSERT_ZERO_LABEL(ret
);
398 ASSERT_READ_ZERO_LABEL(buf
, 128);
399 dfsan_set_label(j_label
, &buf
, sizeof(&buf
));
400 ret
= fgets(buf
, sizeof(buf
), f
);
401 ASSERT_LABEL(ret
, j_label
);
408 dfsan_set_label(i_label
, buf
+ 2, 2);
409 char* ret
= getcwd(buf
, sizeof(buf
));
411 assert(ret
[0] == '/');
412 ASSERT_READ_ZERO_LABEL(buf
+ 2, 2);
413 dfsan_set_label(i_label
, &ptr
, sizeof(ptr
));
414 ret
= getcwd(ptr
, sizeof(buf
));
415 ASSERT_LABEL(ret
, i_label
);
418 void test_get_current_dir_name() {
419 char* ret
= get_current_dir_name();
421 assert(ret
[0] == '/');
422 ASSERT_READ_ZERO_LABEL(ret
, strlen(ret
) + 1);
425 void test_gethostname() {
427 dfsan_set_label(i_label
, buf
+ 2, 2);
428 assert(gethostname(buf
, sizeof(buf
)) == 0);
429 ASSERT_READ_ZERO_LABEL(buf
+ 2, 2);
432 void test_getrlimit() {
434 dfsan_set_label(i_label
, &rlim
, sizeof(rlim
));
435 assert(getrlimit(RLIMIT_CPU
, &rlim
) == 0);
436 ASSERT_READ_ZERO_LABEL(&rlim
, sizeof(rlim
));
439 void test_getrusage() {
441 dfsan_set_label(i_label
, &usage
, sizeof(usage
));
442 assert(getrusage(RUSAGE_SELF
, &usage
) == 0);
443 ASSERT_READ_ZERO_LABEL(&usage
, sizeof(usage
));
447 char src
[] = "hello world";
448 char dst
[sizeof(src
) + 2];
449 dfsan_set_label(0, src
, sizeof(src
));
450 dfsan_set_label(0, dst
, sizeof(dst
));
451 dfsan_set_label(i_label
, src
+ 2, 1);
452 dfsan_set_label(j_label
, src
+ 3, 1);
453 dfsan_set_label(j_label
, dst
+ 4, 1);
454 dfsan_set_label(i_label
, dst
+ 12, 1);
455 char *ret
= strcpy(dst
, src
);
457 assert(strcmp(src
, dst
) == 0);
458 for (int i
= 0; i
< strlen(src
) + 1; ++i
) {
459 assert(dfsan_get_label(dst
[i
]) == dfsan_get_label(src
[i
]));
461 // Note: if strlen(src) + 1 were used instead to compute the first untouched
462 // byte of dest, the label would be I|J. This is because strlen() might
463 // return a non-zero label, and because by default pointer labels are not
465 ASSERT_LABEL(dst
[12], i_label
);
469 char buf
[] = "1234578910";
471 dfsan_set_label(i_label
, buf
+ 1, 1);
472 dfsan_set_label(j_label
, buf
+ 10, 1);
473 long int ret
= strtol(buf
, &endptr
, 10);
474 assert(ret
== 1234578910);
475 assert(endptr
== buf
+ 10);
476 ASSERT_LABEL(ret
, i_j_label
);
479 void test_strtoll() {
480 char buf
[] = "1234578910 ";
482 dfsan_set_label(i_label
, buf
+ 1, 1);
483 dfsan_set_label(j_label
, buf
+ 2, 1);
484 long long int ret
= strtoll(buf
, &endptr
, 10);
485 assert(ret
== 1234578910);
486 assert(endptr
== buf
+ 10);
487 ASSERT_LABEL(ret
, i_j_label
);
490 void test_strtoul() {
491 char buf
[] = "0xffffffffffffaa";
493 dfsan_set_label(i_label
, buf
+ 1, 1);
494 dfsan_set_label(j_label
, buf
+ 2, 1);
495 long unsigned int ret
= strtol(buf
, &endptr
, 16);
496 assert(ret
== 72057594037927850);
497 assert(endptr
== buf
+ 16);
498 ASSERT_LABEL(ret
, i_j_label
);
501 void test_strtoull() {
502 char buf
[] = "0xffffffffffffffaa";
504 dfsan_set_label(i_label
, buf
+ 1, 1);
505 dfsan_set_label(j_label
, buf
+ 2, 1);
506 long long unsigned int ret
= strtoull(buf
, &endptr
, 16);
507 assert(ret
== 0xffffffffffffffaa);
508 assert(endptr
== buf
+ 18);
509 ASSERT_LABEL(ret
, i_j_label
);
513 char buf
[] = "12345.76 foo";
515 dfsan_set_label(i_label
, buf
+ 1, 1);
516 dfsan_set_label(j_label
, buf
+ 6, 1);
517 double ret
= strtod(buf
, &endptr
);
518 assert(ret
== 12345.76);
519 assert(endptr
== buf
+ 8);
520 ASSERT_LABEL(ret
, i_j_label
);
525 dfsan_set_label(i_label
, &t
, 1);
526 time_t ret
= time(&t
);
529 ASSERT_ZERO_LABEL(t
);
532 void test_inet_pton() {
533 char addr4
[] = "127.0.0.1";
534 dfsan_set_label(i_label
, addr4
+ 3, 1);
536 int ret4
= inet_pton(AF_INET
, addr4
, &in4
);
538 ASSERT_READ_LABEL(&in4
, sizeof(in4
), i_label
);
539 assert(in4
.s_addr
== htonl(0x7f000001));
541 char addr6
[] = "::1";
542 dfsan_set_label(j_label
, addr6
+ 3, 1);
544 int ret6
= inet_pton(AF_INET6
, addr6
, &in6
);
546 ASSERT_READ_LABEL(((char *) &in6
) + sizeof(in6
) - 1, 1, j_label
);
549 void test_localtime_r() {
550 time_t t0
= 1384800998;
552 dfsan_set_label(i_label
, &t0
, sizeof(t0
));
553 struct tm
* ret
= localtime_r(&t0
, &t1
);
555 assert(t1
.tm_min
== 56);
556 ASSERT_LABEL(t1
.tm_mon
, i_label
);
559 void test_getpwuid_r() {
562 struct passwd
*result
;
564 dfsan_set_label(i_label
, &pwd
, 4);
565 int ret
= getpwuid_r(0, &pwd
, buf
, sizeof(buf
), &result
);
567 assert(strcmp(pwd
.pw_name
, "root") == 0);
568 assert(result
== &pwd
);
569 ASSERT_READ_ZERO_LABEL(&pwd
, 4);
576 dfsan_set_label(i_label
, &fd
.revents
, sizeof(fd
.revents
));
577 int ret
= poll(&fd
, 1, 1);
578 ASSERT_ZERO_LABEL(fd
.revents
);
587 dfsan_set_label(i_label
, &fds
, sizeof(fds
));
588 dfsan_set_label(j_label
, &t
, sizeof(t
));
589 int ret
= select(1, &fds
, NULL
, NULL
, &t
);
591 ASSERT_ZERO_LABEL(t
.tv_sec
);
592 ASSERT_READ_ZERO_LABEL(&fds
, sizeof(fds
));
595 void test_sched_getaffinity() {
597 dfsan_set_label(j_label
, &mask
, 1);
598 int ret
= sched_getaffinity(0, sizeof(mask
), &mask
);
600 ASSERT_READ_ZERO_LABEL(&mask
, sizeof(mask
));
603 void test_sigemptyset() {
605 dfsan_set_label(j_label
, &set
, 1);
606 int ret
= sigemptyset(&set
);
608 ASSERT_READ_ZERO_LABEL(&set
, sizeof(set
));
611 void test_sigaction() {
612 struct sigaction oldact
;
613 dfsan_set_label(j_label
, &oldact
, 1);
614 int ret
= sigaction(SIGUSR1
, NULL
, &oldact
);
616 ASSERT_READ_ZERO_LABEL(&oldact
, sizeof(oldact
));
619 void test_gettimeofday() {
622 dfsan_set_label(i_label
, &tv
, sizeof(tv
));
623 dfsan_set_label(j_label
, &tz
, sizeof(tz
));
624 int ret
= gettimeofday(&tv
, &tz
);
626 ASSERT_READ_ZERO_LABEL(&tv
, sizeof(tv
));
627 ASSERT_READ_ZERO_LABEL(&tz
, sizeof(tz
));
630 void *pthread_create_test_cb(void *p
) {
631 assert(p
== (void *)1);
632 ASSERT_ZERO_LABEL(p
);
636 void test_pthread_create() {
638 pthread_create(&pt
, 0, pthread_create_test_cb
, (void *)1);
640 pthread_join(pt
, &cbrv
);
641 assert(cbrv
== (void *)2);
644 int dl_iterate_phdr_test_cb(struct dl_phdr_info
*info
, size_t size
,
646 assert(data
== (void *)3);
647 ASSERT_ZERO_LABEL(info
);
648 ASSERT_ZERO_LABEL(size
);
649 ASSERT_ZERO_LABEL(data
);
653 void test_dl_iterate_phdr() {
654 dl_iterate_phdr(dl_iterate_phdr_test_cb
, (void *)3);
657 void test_strrchr() {
658 char str1
[] = "str1str1";
659 dfsan_set_label(i_label
, &str1
[7], 1);
661 char *rv
= strrchr(str1
, 'r');
662 assert(rv
== &str1
[6]);
663 #ifdef STRICT_DATA_DEPENDENCIES
664 ASSERT_ZERO_LABEL(rv
);
666 ASSERT_LABEL(rv
, i_label
);
671 char str1
[] = "str1str1";
672 dfsan_set_label(i_label
, &str1
[3], 1);
673 dfsan_set_label(j_label
, &str1
[5], 1);
675 char *rv
= strstr(str1
, "1s");
676 assert(rv
== &str1
[3]);
677 #ifdef STRICT_DATA_DEPENDENCIES
678 ASSERT_ZERO_LABEL(rv
);
680 ASSERT_LABEL(rv
, i_label
);
683 rv
= strstr(str1
, "2s");
685 #ifdef STRICT_DATA_DEPENDENCIES
686 ASSERT_ZERO_LABEL(rv
);
688 ASSERT_LABEL(rv
, i_j_label
);
693 char str1
[] = "str1";
694 dfsan_set_label(i_label
, &str1
[3], 1);
695 dfsan_set_label(j_label
, &str1
[4], 1);
697 char *crv
= (char *) memchr(str1
, 'r', sizeof(str1
));
698 assert(crv
== &str1
[2]);
699 ASSERT_ZERO_LABEL(crv
);
701 crv
= (char *) memchr(str1
, '1', sizeof(str1
));
702 assert(crv
== &str1
[3]);
703 #ifdef STRICT_DATA_DEPENDENCIES
704 ASSERT_ZERO_LABEL(crv
);
706 ASSERT_LABEL(crv
, i_label
);
709 crv
= (char *) memchr(str1
, 'x', sizeof(str1
));
711 #ifdef STRICT_DATA_DEPENDENCIES
712 ASSERT_ZERO_LABEL(crv
);
714 ASSERT_LABEL(crv
, i_j_label
);
718 void alarm_handler(int unused
) {
722 void test_nanosleep() {
723 struct timespec req
, rem
;
726 dfsan_set_label(i_label
, &rem
, sizeof(rem
));
729 int rv
= nanosleep(&req
, &rem
);
731 ASSERT_ZERO_LABEL(rv
);
732 ASSERT_READ_LABEL(&rem
, 1, i_label
);
734 // interrupted by an alarm
735 signal(SIGALRM
, alarm_handler
);
738 rv
= nanosleep(&req
, &rem
);
740 ASSERT_ZERO_LABEL(rv
);
741 ASSERT_READ_ZERO_LABEL(&rem
, sizeof(rem
));
744 void test_socketpair() {
747 dfsan_set_label(i_label
, fd
, sizeof(fd
));
748 int rv
= socketpair(PF_LOCAL
, SOCK_STREAM
, 0, fd
);
750 ASSERT_ZERO_LABEL(rv
);
751 ASSERT_READ_ZERO_LABEL(fd
, sizeof(fd
));
755 int fd
= open("/dev/null", O_WRONLY
);
757 char buf
[] = "a string";
758 int len
= strlen(buf
);
760 // The result of a write always unlabeled.
761 int res
= write(fd
, buf
, len
);
763 ASSERT_ZERO_LABEL(res
);
765 // Label all arguments to write().
766 dfsan_set_label(i_label
, &(buf
[3]), 1);
767 dfsan_set_label(j_label
, &fd
, sizeof(fd
));
768 dfsan_set_label(i_label
, &len
, sizeof(len
));
770 // The value returned by write() should have no label.
771 res
= write(fd
, buf
, len
);
772 ASSERT_ZERO_LABEL(res
);
778 void test_sprintf_chunk(const char* expected
, const char* format
, T arg
) {
780 memset(buf
, 'a', sizeof(buf
));
782 char padded_expected
[512];
783 strcpy(padded_expected
, "foo ");
784 strcat(padded_expected
, expected
);
785 strcat(padded_expected
, " bar");
787 char padded_format
[512];
788 strcpy(padded_format
, "foo ");
789 strcat(padded_format
, format
);
790 strcat(padded_format
, " bar");
793 assert(sprintf(buf
, padded_format
, arg
) == strlen(padded_expected
));
794 assert(strcmp(buf
, padded_expected
) == 0);
795 ASSERT_READ_LABEL(buf
, strlen(padded_expected
), 0);
796 memset(buf
, 'a', sizeof(buf
));
799 dfsan_set_label(i_label
, &arg
, sizeof(arg
));
800 assert(sprintf(buf
, padded_format
, arg
) == strlen(padded_expected
));
801 assert(strcmp(buf
, padded_expected
) == 0);
802 ASSERT_READ_LABEL(buf
, 4, 0);
803 ASSERT_READ_LABEL(buf
+ 4, strlen(padded_expected
) - 8, i_label
);
804 ASSERT_READ_LABEL(buf
+ (strlen(padded_expected
) - 4), 4, 0);
807 void test_sprintf() {
809 memset(buf
, 'a', sizeof(buf
));
811 // Test formatting (no conversion specifier).
812 assert(sprintf(buf
, "Hello world!") == 12);
813 assert(strcmp(buf
, "Hello world!") == 0);
814 ASSERT_READ_LABEL(buf
, sizeof(buf
), 0);
816 // Test for extra arguments.
817 assert(sprintf(buf
, "Hello world!", 42, "hello") == 12);
818 assert(strcmp(buf
, "Hello world!") == 0);
819 ASSERT_READ_LABEL(buf
, sizeof(buf
), 0);
821 // Test formatting & label propagation (multiple conversion specifiers): %s,
822 // %d, %n, %f, and %%.
823 const char* s
= "world";
826 dfsan_set_label(k_label
, (void *) (s
+ 1), 2);
827 dfsan_set_label(i_label
, &m
, sizeof(m
));
828 dfsan_set_label(j_label
, &d
, sizeof(d
));
830 int r
= sprintf(buf
, "hello %s, %-d/%d/%d %f %% %n%d", s
, 2014, m
, d
,
831 12345.6781234, &n
, 1000);
833 assert(strcmp(buf
, "hello world, 2014/8/27 12345.678123 % 1000") == 0);
834 ASSERT_READ_LABEL(buf
, 7, 0);
835 ASSERT_READ_LABEL(buf
+ 7, 2, k_label
);
836 ASSERT_READ_LABEL(buf
+ 9, 9, 0);
837 ASSERT_READ_LABEL(buf
+ 18, 1, i_label
);
838 ASSERT_READ_LABEL(buf
+ 19, 1, 0);
839 ASSERT_READ_LABEL(buf
+ 20, 2, j_label
);
840 ASSERT_READ_LABEL(buf
+ 22, 15, 0);
844 // Test formatting & label propagation (single conversion specifier, with
845 // additional length and precision modifiers).
846 test_sprintf_chunk("-559038737", "%d", 0xdeadbeef);
847 test_sprintf_chunk("3735928559", "%u", 0xdeadbeef);
848 test_sprintf_chunk("12345", "%i", 12345);
849 test_sprintf_chunk("751", "%o", 0751);
850 test_sprintf_chunk("babe", "%x", 0xbabe);
851 test_sprintf_chunk("0000BABE", "%.8X", 0xbabe);
852 test_sprintf_chunk("-17", "%hhd", 0xdeadbeef);
853 test_sprintf_chunk("-16657", "%hd", 0xdeadbeef);
854 test_sprintf_chunk("deadbeefdeadbeef", "%lx", 0xdeadbeefdeadbeef);
855 test_sprintf_chunk("0xdeadbeefdeadbeef", "%p",
856 (void *) 0xdeadbeefdeadbeef);
857 test_sprintf_chunk("18446744073709551615", "%ju", (intmax_t) -1);
858 test_sprintf_chunk("18446744073709551615", "%zu", (size_t) -1);
859 test_sprintf_chunk("18446744073709551615", "%tu", (size_t) -1);
861 test_sprintf_chunk("0x1.f9acffa7eb6bfp-4", "%a", 0.123456);
862 test_sprintf_chunk("0X1.F9ACFFA7EB6BFP-4", "%A", 0.123456);
863 test_sprintf_chunk("0.12346", "%.5f", 0.123456);
864 test_sprintf_chunk("0.123456", "%g", 0.123456);
865 test_sprintf_chunk("1.234560e-01", "%e", 0.123456);
866 test_sprintf_chunk("1.234560E-01", "%E", 0.123456);
867 test_sprintf_chunk("0.1234567891234560", "%.16Lf",
868 (long double) 0.123456789123456);
870 test_sprintf_chunk("z", "%c", 'z');
872 // %n, %s, %d, %f, and %% already tested
874 // Test formatting with width passed as an argument.
875 r
= sprintf(buf
, "hi %*d my %*s friend %.*f", 3, 1, 6, "dear", 4, 3.14159265359);
877 assert(strcmp(buf
, "hi 1 my dear friend 3.1416") == 0);
880 void test_snprintf() {
882 memset(buf
, 'a', sizeof(buf
));
883 dfsan_set_label(0, buf
, sizeof(buf
));
884 const char* s
= "world";
888 dfsan_set_label(k_label
, (void *) (s
+ 1), 2);
889 dfsan_set_label(i_label
, &y
, sizeof(y
));
890 dfsan_set_label(j_label
, &m
, sizeof(m
));
891 int r
= snprintf(buf
, 19, "hello %s, %-d/%d/%d %f", s
, y
, m
, d
,
893 // The return value is the number of bytes that would have been written to
894 // the final string if enough space had been available.
896 assert(memcmp(buf
, "hello world, 2014/", 19) == 0);
897 ASSERT_READ_LABEL(buf
, 7, 0);
898 ASSERT_READ_LABEL(buf
+ 7, 2, k_label
);
899 ASSERT_READ_LABEL(buf
+ 9, 4, 0);
900 ASSERT_READ_LABEL(buf
+ 13, 4, i_label
);
901 ASSERT_READ_LABEL(buf
+ 17, 2, 0);
906 i_label
= dfsan_create_label("i", 0);
907 j_label
= dfsan_create_label("j", 0);
908 k_label
= dfsan_create_label("k", 0);
909 i_j_label
= dfsan_union(i_label
, j_label
);
912 test_clock_gettime();
914 test_dfsan_set_write_callback();
915 test_dl_iterate_phdr();
919 test_get_current_dir_name();
935 test_pthread_create();
937 test_sched_getaffinity();