summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--compiler-rt/lib/asan/lit_tests/TestCases/readv.cc32
-rw-r--r--compiler-rt/lib/msan/tests/msan_test.cc46
-rw-r--r--compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc131
-rw-r--r--compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h13
-rw-r--r--compiler-rt/lib/tsan/rtl/tsan_interceptors.cc38
-rw-r--r--compiler-rt/lib/tsan/rtl/tsan_stat.cc2
-rw-r--r--compiler-rt/lib/tsan/rtl/tsan_stat.h2
7 files changed, 217 insertions, 47 deletions
diff --git a/compiler-rt/lib/asan/lit_tests/TestCases/readv.cc b/compiler-rt/lib/asan/lit_tests/TestCases/readv.cc
new file mode 100644
index 00000000000..5120b77e768
--- /dev/null
+++ b/compiler-rt/lib/asan/lit_tests/TestCases/readv.cc
@@ -0,0 +1,32 @@
+// RUN: %clangxx_asan -O0 %s -o %t && %t
+// RUN: %clangxx_asan -O0 %s -DPOSITIVE -o %t && %t 2>&1 | %symbolize | FileCheck %s
+
+// Test the readv() interceptor.
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/uio.h>
+#include <time.h>
+
+int main() {
+ char buf[2011];
+ struct iovec iov[2];
+#ifdef POSITIVE
+ char * volatile buf_ = buf;
+ iov[0].iov_base = buf_ - 1;
+#else
+ iov[0].iov_base = buf + 1;
+#endif
+ iov[0].iov_len = 5;
+ iov[1].iov_base = buf + 10;
+ iov[1].iov_len = 2000;
+ int fd = open("/etc/hosts", O_RDONLY);
+ assert(fd > 0);
+ readv(fd, iov, 2);
+ // CHECK: WRITE of size 5 at
+ close(fd);
+ return 0;
+}
diff --git a/compiler-rt/lib/msan/tests/msan_test.cc b/compiler-rt/lib/msan/tests/msan_test.cc
index 53106ac9e89..6f8ac955258 100644
--- a/compiler-rt/lib/msan/tests/msan_test.cc
+++ b/compiler-rt/lib/msan/tests/msan_test.cc
@@ -566,6 +566,52 @@ TEST(MemorySanitizer, pread) {
delete x;
}
+TEST(MemorySanitizer, readv) {
+ char buf[2011];
+ struct iovec iov[2];
+ iov[0].iov_base = buf + 1;
+ iov[0].iov_len = 5;
+ iov[1].iov_base = buf + 10;
+ iov[1].iov_len = 2000;
+ int fd = open("/proc/self/stat", O_RDONLY);
+ assert(fd > 0);
+ int sz = readv(fd, iov, 2);
+ ASSERT_LT(sz, 5 + 2000);
+ ASSERT_GT(sz, iov[0].iov_len);
+ EXPECT_POISONED(buf[0]);
+ EXPECT_NOT_POISONED(buf[1]);
+ EXPECT_NOT_POISONED(buf[5]);
+ EXPECT_POISONED(buf[6]);
+ EXPECT_POISONED(buf[9]);
+ EXPECT_NOT_POISONED(buf[10]);
+ EXPECT_NOT_POISONED(buf[10 + (sz - 1) - 5]);
+ EXPECT_POISONED(buf[11 + (sz - 1) - 5]);
+ close(fd);
+}
+
+TEST(MemorySanitizer, preadv) {
+ char buf[2011];
+ struct iovec iov[2];
+ iov[0].iov_base = buf + 1;
+ iov[0].iov_len = 5;
+ iov[1].iov_base = buf + 10;
+ iov[1].iov_len = 2000;
+ int fd = open("/proc/self/stat", O_RDONLY);
+ assert(fd > 0);
+ int sz = preadv(fd, iov, 2, 3);
+ ASSERT_LT(sz, 5 + 2000);
+ ASSERT_GT(sz, iov[0].iov_len);
+ EXPECT_POISONED(buf[0]);
+ EXPECT_NOT_POISONED(buf[1]);
+ EXPECT_NOT_POISONED(buf[5]);
+ EXPECT_POISONED(buf[6]);
+ EXPECT_POISONED(buf[9]);
+ EXPECT_NOT_POISONED(buf[10]);
+ EXPECT_NOT_POISONED(buf[10 + (sz - 1) - 5]);
+ EXPECT_POISONED(buf[11 + (sz - 1) - 5]);
+ close(fd);
+}
+
// FIXME: fails now.
TEST(MemorySanitizer, DISABLED_ioctl) {
struct winsize ws;
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 61d09ce384b..37dac0f62b7 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -112,6 +112,27 @@ INTERCEPTOR(long double, frexpl, long double x, int *exp) {
#define INIT_FREXPF_FREXPL
#endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
+#if SI_NOT_WINDOWS
+static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
+ SIZE_T iovlen, SIZE_T maxlen) {
+ for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
+ SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
+ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
+ maxlen -= sz;
+ }
+}
+
+static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
+ SIZE_T iovlen, SIZE_T maxlen) {
+ COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
+ for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
+ SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
+ COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
+ maxlen -= sz;
+ }
+}
+#endif
+
#if SANITIZER_INTERCEPT_READ
INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
void *ctx;
@@ -160,6 +181,51 @@ INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
#define INIT_PREAD64
#endif
+#if SANITIZER_INTERCEPT_READV
+INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
+ int iovcnt) {
+ void *ctx;
+ COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
+ SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
+ if (res > 0) write_iovec(ctx, iov, iovcnt, res);
+ if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
+ return res;
+}
+#define INIT_READV INTERCEPT_FUNCTION(readv)
+#else
+#define INIT_READV
+#endif
+
+#if SANITIZER_INTERCEPT_PREADV
+INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
+ OFF_T offset) {
+ void *ctx;
+ COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
+ SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
+ if (res > 0) write_iovec(ctx, iov, iovcnt, res);
+ if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
+ return res;
+}
+#define INIT_PREADV INTERCEPT_FUNCTION(preadv)
+#else
+#define INIT_PREADV
+#endif
+
+#if SANITIZER_INTERCEPT_PREADV64
+INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
+ OFF64_T offset) {
+ void *ctx;
+ COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
+ SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
+ if (res > 0) write_iovec(ctx, iov, iovcnt, res);
+ if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
+ return res;
+}
+#define INIT_PREADV64 INTERCEPT_FUNCTION(preadv64)
+#else
+#define INIT_PREADV64
+#endif
+
#if SANITIZER_INTERCEPT_WRITE
INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
void *ctx;
@@ -167,6 +233,7 @@ INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
if (fd >= 0)
COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
SSIZE_T res = REAL(write)(fd, ptr, count);
+ // FIXME: this check should be _before_ the call to REAL(write), not after
if (res > 0)
COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
return res;
@@ -209,6 +276,51 @@ INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
#define INIT_PWRITE64
#endif
+#if SANITIZER_INTERCEPT_WRITEV
+INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
+ int iovcnt) {
+ void *ctx;
+ COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
+ if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
+ SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
+ if (res > 0) read_iovec(ctx, iov, iovcnt, res);
+ return res;
+}
+#define INIT_WRITEV INTERCEPT_FUNCTION(writev)
+#else
+#define INIT_WRITEV
+#endif
+
+#if SANITIZER_INTERCEPT_PWRITEV
+INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
+ OFF_T offset) {
+ void *ctx;
+ COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
+ if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
+ SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
+ if (res > 0) read_iovec(ctx, iov, iovcnt, res);
+ return res;
+}
+#define INIT_PWRITEV INTERCEPT_FUNCTION(pwritev)
+#else
+#define INIT_PWRITEV
+#endif
+
+#if SANITIZER_INTERCEPT_PWRITEV64
+INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
+ OFF64_T offset) {
+ void *ctx;
+ COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
+ if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
+ SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
+ if (res > 0) read_iovec(ctx, iov, iovcnt, res);
+ return res;
+}
+#define INIT_PWRITEV64 INTERCEPT_FUNCTION(pwritev64)
+#else
+#define INIT_PWRITEV64
+#endif
+
#if SANITIZER_INTERCEPT_PRCTL
INTERCEPTOR(int, prctl, int option,
unsigned long arg2, unsigned long arg3, // NOLINT
@@ -1079,16 +1191,15 @@ INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
#endif
#if SANITIZER_INTERCEPT_RECVMSG
-static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg) {
+static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg, SSIZE_T maxlen) {
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
if (msg->msg_name)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name,
REAL(strlen)((char *)msg->msg_name) + 1);
if (msg->msg_iov)
- COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov, msg->msg_iovlen);
- for (SIZE_T i = 0; i < msg->msg_iovlen; ++i)
- COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov[i].iov_base,
- msg->msg_iov[i].iov_len);
+ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
+ sizeof(*msg->msg_iov) * msg->msg_iovlen);
+ write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
if (msg->msg_control)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
}
@@ -1100,7 +1211,7 @@ INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
if (res >= 0) {
if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
- if (msg) write_msghdr(ctx, msg);
+ if (msg) write_msghdr(ctx, msg, res);
}
return res;
}
@@ -1131,10 +1242,16 @@ INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
INIT_READ; \
INIT_PREAD; \
INIT_PREAD64; \
- INIT_PRCTL; \
+ INIT_READV; \
+ INIT_PREADV; \
+ INIT_PREADV64; \
INIT_WRITE; \
INIT_PWRITE; \
INIT_PWRITE64; \
+ INIT_WRITEV; \
+ INIT_PWRITEV; \
+ INIT_PWRITEV64; \
+ INIT_PRCTL; \
INIT_LOCALTIME_AND_FRIENDS; \
INIT_SCANF; \
INIT_FREXP; \
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
index ab16ddba5c5..d68abed1b30 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -48,8 +48,17 @@
# define SANITIZER_INTERCEPT_WRITE SI_NOT_WINDOWS
# define SANITIZER_INTERCEPT_PWRITE SI_NOT_WINDOWS
-# define SANITIZER_INTERCEPT_PREAD64 SI_LINUX_NOT_ANDROID
-# define SANITIZER_INTERCEPT_PWRITE64 SI_LINUX_NOT_ANDROID
+#define SANITIZER_INTERCEPT_PREAD64 SI_LINUX_NOT_ANDROID
+#define SANITIZER_INTERCEPT_PWRITE64 SI_LINUX_NOT_ANDROID
+
+#define SANITIZER_INTERCEPT_READV SI_NOT_WINDOWS
+#define SANITIZER_INTERCEPT_WRITEV SI_NOT_WINDOWS
+
+#define SANITIZER_INTERCEPT_PREADV SI_LINUX_NOT_ANDROID
+#define SANITIZER_INTERCEPT_PWRITEV SI_LINUX_NOT_ANDROID
+#define SANITIZER_INTERCEPT_PREADV64 SI_LINUX_NOT_ANDROID
+#define SANITIZER_INTERCEPT_PWRITEV64 SI_LINUX_NOT_ANDROID
+
# define SANITIZER_INTERCEPT_PRCTL SI_LINUX
# define SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS SI_NOT_WINDOWS
diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc b/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc
index ae7ad6dc67b..96c64d8be53 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc
+++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc
@@ -1449,40 +1449,6 @@ TSAN_INTERCEPTOR(int, pipe2, int *pipefd, int flags) {
return res;
}
-TSAN_INTERCEPTOR(long_t, readv, int fd, void *vec, int cnt) {
- SCOPED_TSAN_INTERCEPTOR(readv, fd, vec, cnt);
- int res = REAL(readv)(fd, vec, cnt);
- if (res >= 0 && fd >= 0) {
- FdAcquire(thr, pc, fd);
- }
- return res;
-}
-
-TSAN_INTERCEPTOR(long_t, preadv64, int fd, void *vec, int cnt, u64 off) {
- SCOPED_TSAN_INTERCEPTOR(preadv64, fd, vec, cnt, off);
- int res = REAL(preadv64)(fd, vec, cnt, off);
- if (res >= 0 && fd >= 0) {
- FdAcquire(thr, pc, fd);
- }
- return res;
-}
-
-TSAN_INTERCEPTOR(long_t, writev, int fd, void *vec, int cnt) {
- SCOPED_TSAN_INTERCEPTOR(writev, fd, vec, cnt);
- if (fd >= 0)
- FdRelease(thr, pc, fd);
- int res = REAL(writev)(fd, vec, cnt);
- return res;
-}
-
-TSAN_INTERCEPTOR(long_t, pwritev64, int fd, void *vec, int cnt, u64 off) {
- SCOPED_TSAN_INTERCEPTOR(pwritev64, fd, vec, cnt, off);
- if (fd >= 0)
- FdRelease(thr, pc, fd);
- int res = REAL(pwritev64)(fd, vec, cnt, off);
- return res;
-}
-
TSAN_INTERCEPTOR(long_t, send, int fd, void *buf, long_t len, int flags) {
SCOPED_TSAN_INTERCEPTOR(send, fd, buf, len, flags);
if (fd >= 0)
@@ -2049,10 +2015,6 @@ void InitializeInterceptors() {
TSAN_INTERCEPT(pipe);
TSAN_INTERCEPT(pipe2);
- TSAN_INTERCEPT(readv);
- TSAN_INTERCEPT(preadv64);
- TSAN_INTERCEPT(writev);
- TSAN_INTERCEPT(pwritev64);
TSAN_INTERCEPT(send);
TSAN_INTERCEPT(sendmsg);
TSAN_INTERCEPT(recv);
diff --git a/compiler-rt/lib/tsan/rtl/tsan_stat.cc b/compiler-rt/lib/tsan/rtl/tsan_stat.cc
index e64cac4b911..0c7d8dae994 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_stat.cc
+++ b/compiler-rt/lib/tsan/rtl/tsan_stat.cc
@@ -229,11 +229,13 @@ void StatOutput(u64 *stat) {
name[StatInt_pread] = " pread ";
name[StatInt_pread64] = " pread64 ";
name[StatInt_readv] = " readv ";
+ name[StatInt_preadv] = " preadv ";
name[StatInt_preadv64] = " preadv64 ";
name[StatInt_write] = " write ";
name[StatInt_pwrite] = " pwrite ";
name[StatInt_pwrite64] = " pwrite64 ";
name[StatInt_writev] = " writev ";
+ name[StatInt_pwritev] = " pwritev ";
name[StatInt_pwritev64] = " pwritev64 ";
name[StatInt_send] = " send ";
name[StatInt_sendmsg] = " sendmsg ";
diff --git a/compiler-rt/lib/tsan/rtl/tsan_stat.h b/compiler-rt/lib/tsan/rtl/tsan_stat.h
index e30d91d0b1b..0b7df4472c5 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_stat.h
+++ b/compiler-rt/lib/tsan/rtl/tsan_stat.h
@@ -224,11 +224,13 @@ enum StatType {
StatInt_pread,
StatInt_pread64,
StatInt_readv,
+ StatInt_preadv,
StatInt_preadv64,
StatInt_write,
StatInt_pwrite,
StatInt_pwrite64,
StatInt_writev,
+ StatInt_pwritev,
StatInt_pwritev64,
StatInt_send,
StatInt_sendmsg,
OpenPOWER on IntegriCloud