| 1 | // RUN: %clangxx -g %s -o %t |
| 2 | // RUN: %env_tool_opts=decorate_proc_maps=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%tool_name |
| 3 | |
| 4 | // REQUIRES: stable-runtime |
| 5 | // XFAIL: android && asan |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <pthread.h> |
| 10 | #include <stdio.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <unistd.h> |
| 14 | |
| 15 | bool CopyFdToFd(int in_fd, int out_fd) { |
| 16 | const size_t kBufSize = 0x10000; |
| 17 | static char buf[kBufSize]; |
| 18 | while (true) { |
| 19 | ssize_t got = read(fd: in_fd, buf: buf, nbytes: kBufSize); |
| 20 | if (got > 0) { |
| 21 | write(fd: out_fd, buf: buf, n: got); |
| 22 | } else if (got == 0) { |
| 23 | break; |
| 24 | } else if (errno != EAGAIN || errno != EWOULDBLOCK || errno != EINTR) { |
| 25 | fprintf(stderr, format: "error reading file, errno %d\n" , errno); |
| 26 | return false; |
| 27 | } |
| 28 | } |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | void *ThreadFn(void *arg) { |
| 33 | (void)arg; |
| 34 | int fd = open(file: "/proc/self/maps" , O_RDONLY); |
| 35 | bool res = CopyFdToFd(in_fd: fd, out_fd: 2); |
| 36 | close(fd: fd); |
| 37 | return (void *)!res; |
| 38 | } |
| 39 | |
| 40 | int main(void) { |
| 41 | pthread_t t; |
| 42 | void *res; |
| 43 | pthread_create(newthread: &t, attr: 0, start_routine: ThreadFn, arg: 0); |
| 44 | pthread_join(th: t, thread_return: &res); |
| 45 | return (int)(size_t)res; |
| 46 | } |
| 47 | |
| 48 | // CHECK-asan: rw-p {{.*}} [low shadow] |
| 49 | // CHECK-asan: ---p {{.*}} [shadow gap] |
| 50 | // CHECK-asan: rw-p {{.*}} [high shadow] |
| 51 | |
| 52 | // CHECK-hwasan: rw-p {{.*}} [low shadow] |
| 53 | // CHECK-hwasan: ---p {{.*}} [shadow gap] |
| 54 | // CHECK-hwasan: rw-p {{.*}} [high shadow] |
| 55 | |
| 56 | // CHECK-msan: ---p {{.*}} [invalid] |
| 57 | // CHECK-msan: rw-p {{.*}} [shadow{{.*}}] |
| 58 | // CHECK-msan: ---p {{.*}} [origin{{.*}}] |
| 59 | |
| 60 | // CHECK-tsan: rw-p {{.*}} [shadow] |
| 61 | // CHECK-tsan: rw-p {{.*}} [meta shadow] |
| 62 | |
| 63 | // Nothing interesting with standalone LSan and UBSan. |
| 64 | // CHECK-lsan: decorate_proc_maps |
| 65 | // CHECK-ubsan: decorate_proc_maps |
| 66 | |