Skip to content

Commit 9374453

Browse files
authored
[compiler-rt][rtsan] Reland posix part of #121616 setbuf, setvbuf. (#121658)
1 parent dd1e8aa commit 9374453

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,23 @@ INTERCEPTOR(FILE *, fmemopen, void *buf, size_t size, const char *mode) {
337337
#define RTSAN_MAYBE_INTERCEPT_FMEMOPEN
338338
#endif
339339

340+
#if SANITIZER_INTERCEPT_SETVBUF
341+
INTERCEPTOR(void, setbuf, FILE *stream, char *buf) {
342+
__rtsan_notify_intercepted_call("setbuf");
343+
return REAL(setbuf)(stream, buf);
344+
}
345+
346+
INTERCEPTOR(int, setvbuf, FILE *stream, char *buf, int mode, size_t size) {
347+
__rtsan_notify_intercepted_call("setvbuf");
348+
return REAL(setvbuf)(stream, buf, mode, size);
349+
}
350+
#define RTSAN_MAYBE_INTERCEPT_SETBUF INTERCEPT_FUNCTION(setbuf)
351+
#define RTSAN_MAYBE_INTERCEPT_SETVBUF INTERCEPT_FUNCTION(setvbuf)
352+
#else
353+
#define RTSAN_MAYBE_INTERCEPT_SETBUF
354+
#define RTSAN_MAYBE_INTERCEPT_SETVBUF
355+
#endif
356+
340357
INTERCEPTOR(int, puts, const char *s) {
341358
__rtsan_notify_intercepted_call("puts");
342359
return REAL(puts)(s);
@@ -999,6 +1016,8 @@ void __rtsan::InitializeInterceptors() {
9991016
RTSAN_MAYBE_INTERCEPT_FOPENCOOKIE;
10001017
RTSAN_MAYBE_INTERCEPT_OPEN_MEMSTREAM;
10011018
RTSAN_MAYBE_INTERCEPT_FMEMOPEN;
1019+
RTSAN_MAYBE_INTERCEPT_SETBUF;
1020+
RTSAN_MAYBE_INTERCEPT_SETVBUF;
10021021
INTERCEPT_FUNCTION(lseek);
10031022
RTSAN_MAYBE_INTERCEPT_LSEEK64;
10041023
INTERCEPT_FUNCTION(dup);

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,34 @@ TEST_F(RtsanFileTest, FmemOpenDiesWhenRealtime) {
403403
}
404404
#endif
405405

406+
#if SANITIZER_INTERCEPT_SETVBUF
407+
TEST_F(RtsanFileTest, SetbufDieWhenRealtime) {
408+
char buffer[BUFSIZ];
409+
FILE *f = fopen(GetTemporaryFilePath(), "w");
410+
EXPECT_THAT(f, Ne(nullptr));
411+
412+
auto Func = [&f, &buffer]() { setbuf(f, buffer); };
413+
414+
ExpectRealtimeDeath(Func, "setbuf");
415+
ExpectNonRealtimeSurvival(Func);
416+
}
417+
418+
TEST_F(RtsanFileTest, SetvbufDieWhenRealtime) {
419+
char buffer[1024];
420+
size_t size = sizeof(buffer);
421+
FILE *f = fopen(GetTemporaryFilePath(), "w");
422+
EXPECT_THAT(f, Ne(nullptr));
423+
424+
auto Func = [&f, &buffer, &size]() {
425+
int r = setvbuf(f, buffer, _IOFBF, size);
426+
EXPECT_THAT(r, Eq(0));
427+
};
428+
429+
ExpectRealtimeDeath(Func, "setvbuf");
430+
ExpectNonRealtimeSurvival(Func);
431+
}
432+
#endif
433+
406434
class RtsanOpenedFileTest : public RtsanFileTest {
407435
protected:
408436
void SetUp() override {

0 commit comments

Comments
 (0)