Skip to content

Commit f39ecb7

Browse files
authored
[compiler][rtsan] stream based on memory buffer interception. (llvm#120672)
1 parent adfef2a commit f39ecb7

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-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
@@ -297,6 +297,23 @@ INTERCEPTOR(FILE *, fdopen, int fd, const char *mode) {
297297
return REAL(fdopen)(fd, mode);
298298
}
299299

300+
#if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
301+
INTERCEPTOR(FILE *, open_memstream, char **buf, size_t *size) {
302+
__rtsan_notify_intercepted_call("open_memstream");
303+
return REAL(open_memstream)(buf, size);
304+
}
305+
306+
INTERCEPTOR(FILE *, fmemopen, void *buf, size_t size, const char *mode) {
307+
__rtsan_notify_intercepted_call("fmemopen");
308+
return REAL(fmemopen)(buf, size, mode);
309+
}
310+
#define RTSAN_MAYBE_INTERCEPT_OPEN_MEMSTREAM INTERCEPT_FUNCTION(open_memstream)
311+
#define RTSAN_MAYBE_INTERCEPT_FMEMOPEN INTERCEPT_FUNCTION(fmemopen)
312+
#else
313+
#define RTSAN_MAYBE_INTERCEPT_OPEN_MEMSTREAM
314+
#define RTSAN_MAYBE_INTERCEPT_FMEMOPEN
315+
#endif
316+
300317
INTERCEPTOR(int, puts, const char *s) {
301318
__rtsan_notify_intercepted_call("puts");
302319
return REAL(puts)(s);
@@ -955,6 +972,8 @@ void __rtsan::InitializeInterceptors() {
955972
INTERCEPT_FUNCTION(fputs);
956973
INTERCEPT_FUNCTION(fdopen);
957974
INTERCEPT_FUNCTION(freopen);
975+
RTSAN_MAYBE_INTERCEPT_OPEN_MEMSTREAM;
976+
RTSAN_MAYBE_INTERCEPT_FMEMOPEN;
958977
INTERCEPT_FUNCTION(lseek);
959978
RTSAN_MAYBE_INTERCEPT_LSEEK64;
960979
INTERCEPT_FUNCTION(dup);

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,31 @@ TEST_F(RtsanFileTest, FopenDiesWhenRealtime) {
353353
ExpectNonRealtimeSurvival(Func);
354354
}
355355

356+
#if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
357+
TEST_F(RtsanFileTest, OpenMemstreamDiesWhenRealtime) {
358+
char *buffer;
359+
size_t size;
360+
auto Func = [&buffer, &size]() {
361+
FILE *f = open_memstream(&buffer, &size);
362+
EXPECT_THAT(f, Ne(nullptr));
363+
};
364+
365+
ExpectRealtimeDeath(Func, "open_memstream");
366+
ExpectNonRealtimeSurvival(Func);
367+
}
368+
369+
TEST_F(RtsanFileTest, FmemOpenDiesWhenRealtime) {
370+
char buffer[1024];
371+
auto Func = [&buffer]() {
372+
FILE *f = fmemopen(&buffer, sizeof(buffer), "w");
373+
EXPECT_THAT(f, Ne(nullptr));
374+
};
375+
376+
ExpectRealtimeDeath(Func, "fmemopen");
377+
ExpectNonRealtimeSurvival(Func);
378+
}
379+
#endif
380+
356381
class RtsanOpenedFileTest : public RtsanFileTest {
357382
protected:
358383
void SetUp() override {

0 commit comments

Comments
 (0)