Skip to content

Commit b4ea6af

Browse files
committed
[compiler-rt][rtsan] Reland posix part of llvm#121616 setbuf, setvbuf.
1 parent 24c2ba0 commit b4ea6af

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,27 @@ INTERCEPTOR(FILE *, fmemopen, void *buf, size_t size, const char *mode) {
325325
#define RTSAN_MAYBE_INTERCEPT_FMEMOPEN
326326
#endif
327327

328+
#if SANITIZER_INTERCEPT_SETVBUF
329+
INTERCEPTOR(void, setbuf, FILE *stream, char *buf) {
330+
__rtsan_notify_intercepted_call("setbuf");
331+
return REAL(setbuf)(stream, buf);
332+
}
333+
334+
INTERCEPTOR(int, setvbuf, FILE *stream, char *buf, int mode, size_t size) {
335+
__rtsan_notify_intercepted_call("setvbuf");
336+
return REAL(setvbuf)(stream, buf, mode, size);
337+
}
338+
#define RTSAN_MAYBE_INTERCEPT_SETBUF INTERCEPT_FUNCTION(setbuf)
339+
#define RTSAN_MAYBE_INTERCEPT_SETBUFFER INTERCEPT_FUNCTION(setbuffer)
340+
#define RTSAN_MAYBE_INTERCEPT_SETLINEBUF INTERCEPT_FUNCTION(setlinebuf)
341+
#define RTSAN_MAYBE_INTERCEPT_SETVBUF INTERCEPT_FUNCTION(setvbuf)
342+
#else
343+
#define RTSAN_MAYBE_INTERCEPT_SETBUF
344+
#define RTSAN_MAYBE_INTERCEPT_SETBUFFER
345+
#define RTSAN_MAYBE_INTERCEPT_SETLINEBUF
346+
#define RTSAN_MAYBE_INTERCEPT_SETVBUF
347+
#endif
348+
328349
INTERCEPTOR(int, puts, const char *s) {
329350
__rtsan_notify_intercepted_call("puts");
330351
return REAL(puts)(s);
@@ -986,6 +1007,8 @@ void __rtsan::InitializeInterceptors() {
9861007
RTSAN_MAYBE_INTERCEPT_FOPENCOOKIE;
9871008
RTSAN_MAYBE_INTERCEPT_OPEN_MEMSTREAM;
9881009
RTSAN_MAYBE_INTERCEPT_FMEMOPEN;
1010+
RTSAN_MAYBE_INTERCEPT_SETBUF;
1011+
RTSAN_MAYBE_INTERCEPT_SETVBUF;
9891012
INTERCEPT_FUNCTION(lseek);
9901013
RTSAN_MAYBE_INTERCEPT_LSEEK64;
9911014
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)