Skip to content

Commit 7004d68

Browse files
authored
[compiler-rt][rtsan] adding setlinebuf/setbuffer interception. (llvm#122018)
catering to platform differences as those calls are not posix.
1 parent 645c1ee commit 7004d68

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,33 @@ INTERCEPTOR(int, setvbuf, FILE *stream, char *buf, int mode, size_t size) {
347347
__rtsan_notify_intercepted_call("setvbuf");
348348
return REAL(setvbuf)(stream, buf, mode, size);
349349
}
350+
351+
#if SANITIZER_LINUX
352+
INTERCEPTOR(void, setlinebuf, FILE *stream) {
353+
#else
354+
INTERCEPTOR(int, setlinebuf, FILE *stream) {
355+
#endif
356+
__rtsan_notify_intercepted_call("setlinebuf");
357+
return REAL(setlinebuf)(stream);
358+
}
359+
360+
#if SANITIZER_LINUX
361+
INTERCEPTOR(void, setbuffer, FILE *stream, char *buf, size_t size) {
362+
#else
363+
INTERCEPTOR(void, setbuffer, FILE *stream, char *buf, int size) {
364+
#endif
365+
__rtsan_notify_intercepted_call("setbuffer");
366+
return REAL(setbuffer)(stream, buf, size);
367+
}
350368
#define RTSAN_MAYBE_INTERCEPT_SETBUF INTERCEPT_FUNCTION(setbuf)
351369
#define RTSAN_MAYBE_INTERCEPT_SETVBUF INTERCEPT_FUNCTION(setvbuf)
370+
#define RTSAN_MAYBE_INTERCEPT_SETLINEBUF INTERCEPT_FUNCTION(setlinebuf)
371+
#define RTSAN_MAYBE_INTERCEPT_SETBUFFER INTERCEPT_FUNCTION(setbuffer)
352372
#else
353373
#define RTSAN_MAYBE_INTERCEPT_SETBUF
354374
#define RTSAN_MAYBE_INTERCEPT_SETVBUF
375+
#define RTSAN_MAYBE_INTERCEPT_SETLINEBUF
376+
#define RTSAN_MAYBE_INTERCEPT_SETBUFFER
355377
#endif
356378

357379
INTERCEPTOR(int, puts, const char *s) {
@@ -1018,6 +1040,8 @@ void __rtsan::InitializeInterceptors() {
10181040
RTSAN_MAYBE_INTERCEPT_FMEMOPEN;
10191041
RTSAN_MAYBE_INTERCEPT_SETBUF;
10201042
RTSAN_MAYBE_INTERCEPT_SETVBUF;
1043+
RTSAN_MAYBE_INTERCEPT_SETLINEBUF;
1044+
RTSAN_MAYBE_INTERCEPT_SETBUFFER;
10211045
INTERCEPT_FUNCTION(lseek);
10221046
RTSAN_MAYBE_INTERCEPT_LSEEK64;
10231047
INTERCEPT_FUNCTION(dup);

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ TEST_F(RtsanFileTest, SetbufDieWhenRealtime) {
409409
FILE *f = fopen(GetTemporaryFilePath(), "w");
410410
EXPECT_THAT(f, Ne(nullptr));
411411

412-
auto Func = [&f, &buffer]() { setbuf(f, buffer); };
412+
auto Func = [f, &buffer]() { setbuf(f, buffer); };
413413

414414
ExpectRealtimeDeath(Func, "setbuf");
415415
ExpectNonRealtimeSurvival(Func);
@@ -421,14 +421,36 @@ TEST_F(RtsanFileTest, SetvbufDieWhenRealtime) {
421421
FILE *f = fopen(GetTemporaryFilePath(), "w");
422422
EXPECT_THAT(f, Ne(nullptr));
423423

424-
auto Func = [&f, &buffer, &size]() {
424+
auto Func = [f, &buffer, size]() {
425425
int r = setvbuf(f, buffer, _IOFBF, size);
426426
EXPECT_THAT(r, Eq(0));
427427
};
428428

429429
ExpectRealtimeDeath(Func, "setvbuf");
430430
ExpectNonRealtimeSurvival(Func);
431431
}
432+
433+
TEST_F(RtsanFileTest, SetlinebufDieWhenRealtime) {
434+
FILE *f = fopen(GetTemporaryFilePath(), "w");
435+
EXPECT_THAT(f, Ne(nullptr));
436+
437+
auto Func = [f]() { setlinebuf(f); };
438+
439+
ExpectRealtimeDeath(Func, "setlinebuf");
440+
ExpectNonRealtimeSurvival(Func);
441+
}
442+
443+
TEST_F(RtsanFileTest, SetbufferDieWhenRealtime) {
444+
char buffer[1024];
445+
size_t size = sizeof(buffer);
446+
FILE *f = fopen(GetTemporaryFilePath(), "w");
447+
EXPECT_THAT(f, Ne(nullptr));
448+
449+
auto Func = [f, &buffer, size]() { setbuffer(f, buffer, size); };
450+
451+
ExpectRealtimeDeath(Func, "setbuffer");
452+
ExpectNonRealtimeSurvival(Func);
453+
}
432454
#endif
433455

434456
class RtsanOpenedFileTest : public RtsanFileTest {

0 commit comments

Comments
 (0)