Skip to content

Commit 8584991

Browse files
authored
[compiler-rt][rtsan] Reland "fopencookie support." (llvm#120864) (llvm#121547)
1 parent 2e41489 commit 8584991

File tree

2 files changed

+37
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)