From 4b577830033066cfd1b2acf4fcf39950678b27bd Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Thu, 2 Jan 2025 22:30:39 +0000 Subject: [PATCH] [compiler-rt][rtsan] fopencookie support. (#120864) --- compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp | 7 +++++++ .../rtsan/tests/rtsan_test_interceptors_posix.cpp | 23 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp index 4e51f464b573..072923ab35ae 100644 --- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp +++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp @@ -297,6 +297,12 @@ INTERCEPTOR(FILE *, fdopen, int fd, const char *mode) { return REAL(fdopen)(fd, mode); } +INTERCEPTOR(FILE *, fopencookie, void *cookie, const char *mode, + cookie_io_functions_t funcs) { + __rtsan_notify_intercepted_call("fopencookie"); + return REAL(fopencookie)(cookie, mode, funcs); +} + #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM INTERCEPTOR(FILE *, open_memstream, char **buf, size_t *size) { __rtsan_notify_intercepted_call("open_memstream"); @@ -972,6 +978,7 @@ void __rtsan::InitializeInterceptors() { INTERCEPT_FUNCTION(fputs); INTERCEPT_FUNCTION(fdopen); INTERCEPT_FUNCTION(freopen); + INTERCEPT_FUNCTION(fopencookie); RTSAN_MAYBE_INTERCEPT_OPEN_MEMSTREAM; RTSAN_MAYBE_INTERCEPT_FMEMOPEN; INTERCEPT_FUNCTION(lseek); diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp index b052dd859dcd..c9c4d7fc4e99 100644 --- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp +++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp @@ -353,6 +353,29 @@ TEST_F(RtsanFileTest, FopenDiesWhenRealtime) { ExpectNonRealtimeSurvival(Func); } +TEST_F(RtsanFileTest, FopenCookieDieWhenRealtime) { + FILE *f = fopen(GetTemporaryFilePath(), "w"); + EXPECT_THAT(f, Ne(nullptr)); + struct fholder { + FILE *fp; + size_t read; + } fh = {f, 0}; + auto CookieRead = [this](void *cookie, char *buf, size_t size) { + fholder *p = reinterpret_cast(cookie); + p->read = fread(static_cast(buf), 1, size, p->fp); + EXPECT_NE(0, p->read); + }; + cookie_io_functions_t funcs = {(cookie_read_function_t *)&CookieRead, nullptr, + nullptr, nullptr}; + auto Func = [&fh, &funcs]() { + FILE *f = fopencookie(&fh, "w", funcs); + EXPECT_THAT(f, Ne(nullptr)); + }; + + ExpectRealtimeDeath(Func, "fopencookie"); + ExpectNonRealtimeSurvival(Func); +} + #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM TEST_F(RtsanFileTest, OpenMemstreamDiesWhenRealtime) { char *buffer; -- 2.11.4.GIT