Skip to content

Commit c4443a1

Browse files
authored
[compiler-rt][rtsan] fseek api interception. (#122163)
1 parent 2bc422d commit c4443a1

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,95 @@ INTERCEPTOR(void, setbuffer, FILE *stream, char *buf, int size) {
376376
#define RTSAN_MAYBE_INTERCEPT_SETBUFFER
377377
#endif
378378

379+
#if SANITIZER_INTERCEPT_FSEEK
380+
INTERCEPTOR(int, fgetpos, FILE *stream, fpos_t *pos) {
381+
__rtsan_notify_intercepted_call("fgetpos");
382+
return REAL(fgetpos)(stream, pos);
383+
}
384+
385+
INTERCEPTOR(int, fseek, FILE *stream, long offset, int whence) {
386+
__rtsan_notify_intercepted_call("fseek");
387+
return REAL(fseek)(stream, offset, whence);
388+
}
389+
390+
INTERCEPTOR(int, fseeko, FILE *stream, off_t offset, int whence) {
391+
__rtsan_notify_intercepted_call("fseeko");
392+
return REAL(fseeko)(stream, offset, whence);
393+
}
394+
395+
INTERCEPTOR(int, fsetpos, FILE *stream, const fpos_t *pos) {
396+
__rtsan_notify_intercepted_call("fsetpos");
397+
return REAL(fsetpos)(stream, pos);
398+
}
399+
400+
INTERCEPTOR(long, ftell, FILE *stream) {
401+
__rtsan_notify_intercepted_call("ftell");
402+
return REAL(ftell)(stream);
403+
}
404+
405+
INTERCEPTOR(off_t, ftello, FILE *stream) {
406+
__rtsan_notify_intercepted_call("ftello");
407+
return REAL(ftello)(stream);
408+
}
409+
410+
#if SANITIZER_LINUX && !SANITIZER_MUSL
411+
INTERCEPTOR(int, fgetpos64, FILE *stream, fpos64_t *pos) {
412+
__rtsan_notify_intercepted_call("fgetpos64");
413+
return REAL(fgetpos64)(stream, pos);
414+
}
415+
416+
INTERCEPTOR(int, fseeko64, FILE *stream, off64_t offset, int whence) {
417+
__rtsan_notify_intercepted_call("fseeko64");
418+
return REAL(fseeko64)(stream, offset, whence);
419+
}
420+
421+
INTERCEPTOR(int, fsetpos64, FILE *stream, const fpos64_t *pos) {
422+
__rtsan_notify_intercepted_call("fsetpos64");
423+
return REAL(fsetpos64)(stream, pos);
424+
}
425+
426+
INTERCEPTOR(off64_t, ftello64, FILE *stream) {
427+
__rtsan_notify_intercepted_call("ftello64");
428+
return REAL(ftello64)(stream);
429+
}
430+
#endif
431+
432+
INTERCEPTOR(void, rewind, FILE *stream) {
433+
__rtsan_notify_intercepted_call("rewind");
434+
return REAL(rewind)(stream);
435+
}
436+
#define RTSAN_MAYBE_INTERCEPT_FGETPOS INTERCEPT_FUNCTION(fgetpos)
437+
#define RTSAN_MAYBE_INTERCEPT_FSEEK INTERCEPT_FUNCTION(fseek)
438+
#define RTSAN_MAYBE_INTERCEPT_FSEEKO INTERCEPT_FUNCTION(fseeko)
439+
#define RTSAN_MAYBE_INTERCEPT_FSETPOS INTERCEPT_FUNCTION(fsetpos)
440+
#define RTSAN_MAYBE_INTERCEPT_FTELL INTERCEPT_FUNCTION(ftell)
441+
#define RTSAN_MAYBE_INTERCEPT_FTELLO INTERCEPT_FUNCTION(ftello)
442+
#define RTSAN_MAYBE_INTERCEPT_REWIND INTERCEPT_FUNCTION(rewind)
443+
#if SANITIZER_LINUX && !SANITIZER_MUSL
444+
#define RTSAN_MAYBE_INTERCEPT_FGETPOS64 INTERCEPT_FUNCTION(fgetpos64)
445+
#define RTSAN_MAYBE_INTERCEPT_FSEEKO64 INTERCEPT_FUNCTION(fseeko64)
446+
#define RTSAN_MAYBE_INTERCEPT_FSETPOS64 INTERCEPT_FUNCTION(fsetpos64)
447+
#define RTSAN_MAYBE_INTERCEPT_FTELLO64 INTERCEPT_FUNCTION(ftello64)
448+
#else
449+
#define RTSAN_MAYBE_INTERCEPT_FGETPOS64
450+
#define RTSAN_MAYBE_INTERCEPT_FSEEKO64
451+
#define RTSAN_MAYBE_INTERCEPT_FSETPOS64
452+
#define RTSAN_MAYBE_INTERCEPT_FTELLO64
453+
#endif
454+
#else
455+
#define RTSAN_MAYBE_INTERCEPT_FGETPOS
456+
#define RTSAN_MAYBE_INTERCEPT_FSEEK
457+
#define RTSAN_MAYBE_INTERCEPT_FSEEKO
458+
#define RTSAN_MAYBE_INTERCEPT_FSETPOS
459+
#define RTSAN_MAYBE_INTERCEPT_FTELL
460+
#define RTSAN_MAYBE_INTERCEPT_FTELLO
461+
#define RTSAN_MAYBE_INTERCEPT_REWIND
462+
#define RTSAN_MAYBE_INTERCEPT_FGETPOS64
463+
#define RTSAN_MAYBE_INTERCEPT_FSEEKO64
464+
#define RTSAN_MAYBE_INTERCEPT_FSETPOS64
465+
#define RTSAN_MAYBE_INTERCEPT_FTELLO64
466+
#endif
467+
379468
INTERCEPTOR(int, puts, const char *s) {
380469
__rtsan_notify_intercepted_call("puts");
381470
return REAL(puts)(s);
@@ -1042,6 +1131,17 @@ void __rtsan::InitializeInterceptors() {
10421131
RTSAN_MAYBE_INTERCEPT_SETVBUF;
10431132
RTSAN_MAYBE_INTERCEPT_SETLINEBUF;
10441133
RTSAN_MAYBE_INTERCEPT_SETBUFFER;
1134+
RTSAN_MAYBE_INTERCEPT_FGETPOS;
1135+
RTSAN_MAYBE_INTERCEPT_FSEEK;
1136+
RTSAN_MAYBE_INTERCEPT_FSEEKO;
1137+
RTSAN_MAYBE_INTERCEPT_FSETPOS;
1138+
RTSAN_MAYBE_INTERCEPT_FTELL;
1139+
RTSAN_MAYBE_INTERCEPT_FTELLO;
1140+
RTSAN_MAYBE_INTERCEPT_REWIND;
1141+
RTSAN_MAYBE_INTERCEPT_FGETPOS64;
1142+
RTSAN_MAYBE_INTERCEPT_FSEEKO64;
1143+
RTSAN_MAYBE_INTERCEPT_FSETPOS64;
1144+
RTSAN_MAYBE_INTERCEPT_FTELLO64;
10451145
INTERCEPT_FUNCTION(lseek);
10461146
RTSAN_MAYBE_INTERCEPT_LSEEK64;
10471147
INTERCEPT_FUNCTION(dup);

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,80 @@ class RtsanOpenedFileTest : public RtsanFileTest {
478478
int fd = -1;
479479
};
480480

481+
#if SANITIZER_INTERCEPT_FSEEK
482+
TEST_F(RtsanOpenedFileTest, FgetposDieWhenRealtime) {
483+
auto Func = [this]() {
484+
fpos_t pos;
485+
int ret = fgetpos(GetOpenFile(), &pos);
486+
ASSERT_THAT(ret, Eq(0));
487+
};
488+
489+
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fgetpos"));
490+
ExpectNonRealtimeSurvival(Func);
491+
}
492+
493+
TEST_F(RtsanOpenedFileTest, FsetposDieWhenRealtime) {
494+
fpos_t pos;
495+
int ret = fgetpos(GetOpenFile(), &pos);
496+
ASSERT_THAT(ret, Eq(0));
497+
auto Func = [this, pos]() {
498+
int ret = fsetpos(GetOpenFile(), &pos);
499+
ASSERT_THAT(ret, Eq(0));
500+
};
501+
502+
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fsetpos"));
503+
ExpectNonRealtimeSurvival(Func);
504+
}
505+
506+
TEST_F(RtsanOpenedFileTest, FseekDieWhenRealtime) {
507+
auto Func = [this]() {
508+
int ret = fseek(GetOpenFile(), 0, SEEK_CUR);
509+
ASSERT_THAT(ret, Eq(0));
510+
};
511+
512+
ExpectRealtimeDeath(Func, "fseek");
513+
ExpectNonRealtimeSurvival(Func);
514+
}
515+
516+
TEST_F(RtsanOpenedFileTest, FseekoDieWhenRealtime) {
517+
auto Func = [this]() {
518+
int ret = fseeko(GetOpenFile(), 0, SEEK_CUR);
519+
ASSERT_THAT(ret, Eq(0));
520+
};
521+
522+
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fseeko"));
523+
ExpectNonRealtimeSurvival(Func);
524+
}
525+
526+
TEST_F(RtsanOpenedFileTest, FtellDieWhenRealtime) {
527+
auto Func = [this]() {
528+
long ret = ftell(GetOpenFile());
529+
ASSERT_THAT(ret, Eq(0));
530+
};
531+
532+
ExpectRealtimeDeath(Func, "ftell");
533+
ExpectNonRealtimeSurvival(Func);
534+
}
535+
536+
TEST_F(RtsanOpenedFileTest, FtelloDieWhenRealtime) {
537+
auto Func = [this]() {
538+
off_t ret = ftello(GetOpenFile());
539+
ASSERT_THAT(ret, Eq(0));
540+
};
541+
542+
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("ftello"));
543+
ExpectNonRealtimeSurvival(Func);
544+
}
545+
546+
TEST_F(RtsanOpenedFileTest, RewindDieWhenRealtime) {
547+
int end = fseek(GetOpenFile(), 0, SEEK_END);
548+
auto Func = [this]() { rewind(GetOpenFile()); };
549+
550+
ExpectRealtimeDeath(Func, "rewind");
551+
ExpectNonRealtimeSurvival(Func);
552+
}
553+
#endif
554+
481555
TEST(TestRtsanInterceptors, IoctlDiesWhenRealtime) {
482556
auto Func = []() { ioctl(0, FIONREAD); };
483557
ExpectRealtimeDeath(Func, "ioctl");

0 commit comments

Comments
 (0)