Skip to content

Commit 4b57783

Browse files
authored
[compiler-rt][rtsan] fopencookie support. (#120864)
1 parent 06b6161 commit 4b57783

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

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

300+
INTERCEPTOR(FILE *, fopencookie, void *cookie, const char *mode,
301+
cookie_io_functions_t funcs) {
302+
__rtsan_notify_intercepted_call("fopencookie");
303+
return REAL(fopencookie)(cookie, mode, funcs);
304+
}
305+
300306
#if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
301307
INTERCEPTOR(FILE *, open_memstream, char **buf, size_t *size) {
302308
__rtsan_notify_intercepted_call("open_memstream");
@@ -972,6 +978,7 @@ void __rtsan::InitializeInterceptors() {
972978
INTERCEPT_FUNCTION(fputs);
973979
INTERCEPT_FUNCTION(fdopen);
974980
INTERCEPT_FUNCTION(freopen);
981+
INTERCEPT_FUNCTION(fopencookie);
975982
RTSAN_MAYBE_INTERCEPT_OPEN_MEMSTREAM;
976983
RTSAN_MAYBE_INTERCEPT_FMEMOPEN;
977984
INTERCEPT_FUNCTION(lseek);

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

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

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

0 commit comments

Comments
 (0)