Skip to content

Commit f3b4d94

Browse files
authored
[compiler-rt][rtsan] truncate/ftruncate interception. (#128904)
1 parent effd7f0 commit f3b4d94

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,33 @@ INTERCEPTOR(int, unlinkat, int fd, const char *pathname, int flag) {
285285
return REAL(unlinkat)(fd, pathname, flag);
286286
}
287287

288+
INTERCEPTOR(int, truncate, const char *pathname, off_t length) {
289+
__rtsan_notify_intercepted_call("truncate");
290+
return REAL(truncate)(pathname, length);
291+
}
292+
293+
INTERCEPTOR(int, ftruncate, int fd, off_t length) {
294+
__rtsan_notify_intercepted_call("ftruncate");
295+
return REAL(ftruncate)(fd, length);
296+
}
297+
298+
#if SANITIZER_LINUX
299+
INTERCEPTOR(int, truncate64, const char *pathname, off64_t length) {
300+
__rtsan_notify_intercepted_call("truncate64");
301+
return REAL(truncate64)(pathname, length);
302+
}
303+
304+
INTERCEPTOR(int, ftruncate64, int fd, off64_t length) {
305+
__rtsan_notify_intercepted_call("ftruncate64");
306+
return REAL(ftruncate64)(fd, length);
307+
}
308+
#define RTSAN_MAYBE_INTERCEPT_TRUNCATE64 INTERCEPT_FUNCTION(truncate64)
309+
#define RTSAN_MAYBE_INTERCEPT_FTRUNCATE64 INTERCEPT_FUNCTION(ftruncate64)
310+
#else
311+
#define RTSAN_MAYBE_INTERCEPT_TRUNCATE64
312+
#define RTSAN_MAYBE_INTERCEPT_FTRUNCATE64
313+
#endif
314+
288315
// Streams
289316

290317
INTERCEPTOR(FILE *, fopen, const char *path, const char *mode) {
@@ -1437,6 +1464,10 @@ void __rtsan::InitializeInterceptors() {
14371464
RTSAN_MAYBE_INTERCEPT_READLINKAT;
14381465
INTERCEPT_FUNCTION(unlink);
14391466
INTERCEPT_FUNCTION(unlinkat);
1467+
INTERCEPT_FUNCTION(truncate);
1468+
INTERCEPT_FUNCTION(ftruncate);
1469+
RTSAN_MAYBE_INTERCEPT_TRUNCATE64;
1470+
RTSAN_MAYBE_INTERCEPT_FTRUNCATE64;
14401471
INTERCEPT_FUNCTION(fopen);
14411472
RTSAN_MAYBE_INTERCEPT_FOPEN64;
14421473
RTSAN_MAYBE_INTERCEPT_FREOPEN64;

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ TEST_F(RtsanFileTest, FcntlFlockDiesWhenRealtime) {
401401
ASSERT_THAT(fd, Ne(-1));
402402

403403
auto Func = [fd]() {
404-
struct flock lock {};
404+
struct flock lock{};
405405
lock.l_type = F_RDLCK;
406406
lock.l_whence = SEEK_SET;
407407
lock.l_start = 0;
@@ -735,7 +735,7 @@ TEST(TestRtsanInterceptors, IoctlBehavesWithOutputPointer) {
735735
GTEST_SKIP();
736736
}
737737

738-
struct ifreq ifr {};
738+
struct ifreq ifr{};
739739
strncpy(ifr.ifr_name, ifaddr->ifa_name, IFNAMSIZ - 1);
740740

741741
int retval = ioctl(sock, SIOCGIFADDR, &ifr);
@@ -875,6 +875,18 @@ TEST_F(RtsanOpenedFileTest, UnlinkatDiesWhenRealtime) {
875875
ExpectNonRealtimeSurvival(Func);
876876
}
877877

878+
TEST_F(RtsanOpenedFileTest, TruncateDiesWhenRealtime) {
879+
auto Func = [&]() { truncate(GetTemporaryFilePath(), 16); };
880+
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("truncate"));
881+
ExpectNonRealtimeSurvival(Func);
882+
}
883+
884+
TEST_F(RtsanOpenedFileTest, FtruncateDiesWhenRealtime) {
885+
auto Func = [&]() { ftruncate(GetOpenFd(), 16); };
886+
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("ftruncate"));
887+
ExpectNonRealtimeSurvival(Func);
888+
}
889+
878890
TEST_F(RtsanFileTest, FcloseDiesWhenRealtime) {
879891
FILE *f = fopen(GetTemporaryFilePath(), "w");
880892
EXPECT_THAT(f, Ne(nullptr));

0 commit comments

Comments
 (0)