Skip to content

Commit 3c8818c

Browse files
authored
[rtsan] Add more file descriptor interceptors - dup*, lseek (#116853)
# Why we think these are real-time unsafe They correspond directly to system calls in linux and OSX, they are manipulating a shared resource, which likely takes some operating-system synchronization.
1 parent 7c41b5c commit 3c8818c

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,31 @@ INTERCEPTOR(ssize_t, writev, int fd, const struct iovec *iov, int iovcnt) {
316316
return REAL(writev)(fd, iov, iovcnt);
317317
}
318318

319+
INTERCEPTOR(off_t, lseek, int fd, off_t offset, int whence) {
320+
__rtsan_notify_intercepted_call("lseek");
321+
return REAL(lseek)(fd, offset, whence);
322+
}
323+
324+
#if SANITIZER_INTERCEPT_LSEEK64
325+
INTERCEPTOR(off64_t, lseek64, int fd, off64_t offset, int whence) {
326+
__rtsan_notify_intercepted_call("lseek64");
327+
return REAL(lseek64)(fd, offset, whence);
328+
}
329+
#define RTSAN_MAYBE_INTERCEPT_LSEEK64 INTERCEPT_FUNCTION(lseek64)
330+
#else
331+
#define RTSAN_MAYBE_INTERCEPT_LSEEK64
332+
#endif // SANITIZER_INTERCEPT_LSEEK64
333+
334+
INTERCEPTOR(int, dup, int oldfd) {
335+
__rtsan_notify_intercepted_call("dup");
336+
return REAL(dup)(oldfd);
337+
}
338+
339+
INTERCEPTOR(int, dup2, int oldfd, int newfd) {
340+
__rtsan_notify_intercepted_call("dup2");
341+
return REAL(dup2)(oldfd, newfd);
342+
}
343+
319344
// Concurrency
320345
#if SANITIZER_APPLE
321346
#pragma clang diagnostic push
@@ -757,6 +782,10 @@ void __rtsan::InitializeInterceptors() {
757782
RTSAN_MAYBE_INTERCEPT_CREAT64;
758783
INTERCEPT_FUNCTION(puts);
759784
INTERCEPT_FUNCTION(fputs);
785+
INTERCEPT_FUNCTION(lseek);
786+
RTSAN_MAYBE_INTERCEPT_LSEEK64;
787+
INTERCEPT_FUNCTION(dup);
788+
INTERCEPT_FUNCTION(dup2);
760789

761790
#if SANITIZER_APPLE
762791
INTERCEPT_FUNCTION(OSSpinLockLock);

compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,24 @@ class RtsanOpenedFileTest : public RtsanFileTest {
367367
int fd = -1;
368368
};
369369

370+
TEST_F(RtsanOpenedFileTest, LseekDiesWhenRealtime) {
371+
auto Func = [this]() { lseek(GetOpenFd(), 0, SEEK_SET); };
372+
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("lseek"));
373+
ExpectNonRealtimeSurvival(Func);
374+
}
375+
376+
TEST_F(RtsanOpenedFileTest, DupDiesWhenRealtime) {
377+
auto Func = [this]() { dup(GetOpenFd()); };
378+
ExpectRealtimeDeath(Func, "dup");
379+
ExpectNonRealtimeSurvival(Func);
380+
}
381+
382+
TEST_F(RtsanOpenedFileTest, Dup2DiesWhenRealtime) {
383+
auto Func = [this]() { dup2(GetOpenFd(), 0); };
384+
ExpectRealtimeDeath(Func, "dup2");
385+
ExpectNonRealtimeSurvival(Func);
386+
}
387+
370388
TEST_F(RtsanOpenedFileTest, FreadDiesWhenRealtime) {
371389
auto Func = [this]() {
372390
char c{};

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ SANITIZER_WEAK_IMPORT void *aligned_alloc(__sanitizer::usize __alignment,
210210
#define SANITIZER_INTERCEPT_PREAD64 (SI_GLIBC || SI_SOLARIS32)
211211
#define SANITIZER_INTERCEPT_PWRITE64 (SI_GLIBC || SI_SOLARIS32)
212212

213+
#define SANITIZER_INTERCEPT_LSEEK64 (SI_GLIBC || SI_SOLARIS32)
214+
213215
#define SANITIZER_INTERCEPT_READV SI_POSIX
214216
#define SANITIZER_INTERCEPT_WRITEV SI_POSIX
215217

0 commit comments

Comments
 (0)