Skip to content

Commit 8bd1826

Browse files
committed
[compiler-rt][rtsan] Reland "fopencookie support." (#120864)
1 parent 30b73ed commit 8bd1826

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,17 @@ INTERCEPTOR(FILE *, fdopen, int fd, const char *mode) {
297297
return REAL(fdopen)(fd, mode);
298298
}
299299

300+
#if SANITIZER_INTERCEPT_FOPEN_COOKIE
301+
INTERCEPTOR(FILE *, fopencookie, void *cookie, const char *mode,
302+
cookie_io_functions_t funcs) {
303+
__rtsan_notify_intercepted_call("fopencookie");
304+
return REAL(fopencookie)(cookie, mode, funcs);
305+
}
306+
#define RTSAN_MAYBE_INTERCEPT_FOPENCOOKIE INTERCEPT_FUNCTION(fopencookie)
307+
#else
308+
#define RTSAN_MAYBE_INTERCEPT_FOPENCOOKIE
309+
#endif
310+
300311
#if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
301312
INTERCEPTOR(FILE *, open_memstream, char **buf, size_t *size) {
302313
__rtsan_notify_intercepted_call("open_memstream");
@@ -972,6 +983,7 @@ void __rtsan::InitializeInterceptors() {
972983
INTERCEPT_FUNCTION(fputs);
973984
INTERCEPT_FUNCTION(fdopen);
974985
INTERCEPT_FUNCTION(freopen);
986+
RTSAN_MAYBE_INTERCEPT_FOPENCOOKIE
975987
RTSAN_MAYBE_INTERCEPT_OPEN_MEMSTREAM;
976988
RTSAN_MAYBE_INTERCEPT_FMEMOPEN;
977989
INTERCEPT_FUNCTION(lseek);

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

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

356+
357+
#if SANITIZER_INTERCEPT_FOPENCOOKIE
358+
TEST_F(RtsanFileTest, FopenCookieDieWhenRealtime) {
359+
FILE *f = fopen(GetTemporaryFilePath(), "w");
360+
EXPECT_THAT(f, Ne(nullptr));
361+
struct fholder {
362+
FILE *fp;
363+
size_t read;
364+
} fh = {f, 0};
365+
auto CookieRead = [this](void *cookie, char *buf, size_t size) {
366+
fholder *p = reinterpret_cast<fholder *>(cookie);
367+
p->read = fread(static_cast<void *>(buf), 1, size, p->fp);
368+
EXPECT_NE(0, p->read);
369+
};
370+
cookie_io_functions_t funcs = {(cookie_read_function_t *)&CookieRead, nullptr,
371+
nullptr, nullptr};
372+
auto Func = [&fh, &funcs]() {
373+
FILE *f = fopencookie(&fh, "w", funcs);
374+
EXPECT_THAT(f, Ne(nullptr));
375+
};
376+
377+
ExpectRealtimeDeath(Func, "fopencookie");
378+
ExpectNonRealtimeSurvival(Func);
379+
}
380+
#endif
381+
356382
#if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
357383
TEST_F(RtsanFileTest, OpenMemstreamDiesWhenRealtime) {
358384
char *buffer;

0 commit comments

Comments
 (0)