Skip to content

Conversation

@devnexen
Copy link
Member

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Dec 22, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: David CARLIER (devnexen)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/120864.diff

2 Files Affected:

  • (modified) compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp (+7)
  • (modified) compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp (+25-2)
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 4e51f464b57304..072923ab35ae0d 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 b052dd859dcdf6..8fa396fb415ac5 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -299,7 +299,7 @@ TEST_F(RtsanFileTest, FcntlFlockDiesWhenRealtime) {
   ASSERT_THAT(fd, Ne(-1));
 
   auto Func = [fd]() {
-    struct flock lock {};
+    struct flock lock{};
     lock.l_type = F_RDLCK;
     lock.l_whence = SEEK_SET;
     lock.l_start = 0;
@@ -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<fholder *>(cookie);
+    p->read = fread(static_cast<void *>(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;
@@ -453,7 +476,7 @@ TEST(TestRtsanInterceptors, IoctlBehavesWithOutputPointer) {
     GTEST_SKIP();
   }
 
-  struct ifreq ifr {};
+  struct ifreq ifr{};
   strncpy(ifr.ifr_name, ifaddr->ifa_name, IFNAMSIZ - 1);
 
   int retval = ioctl(sock, SIOCGIFADDR, &ifr);

@github-actions
Copy link

github-actions bot commented Dec 22, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@devnexen devnexen merged commit 4b57783 into llvm:main Jan 2, 2025
7 checks passed
@devnexen devnexen deleted the rtsan_fopencookie branch January 2, 2025 22:30
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jan 2, 2025

LLVM Buildbot has detected a new failure on builder openmp-offload-sles-build-only running on rocm-worker-hw-04-sles while building compiler-rt at step 8 "Add check check-llvm".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/140/builds/13915

Here is the relevant piece of the build log for the reference
Step 8 (Add check check-llvm) failure: test (failure)
******************** TEST 'LLVM :: ExecutionEngine/JITLink/x86-64/ELF_perf.s' FAILED ********************
Exit Code: 134

Command Output (stderr):
--
RUN: at line 6: rm -rf /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp && mkdir -p /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp
+ rm -rf /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp
+ mkdir -p /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp
RUN: at line 7: /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/llvm-mc -triple=x86_64-unknown-linux -position-independent      -filetype=obj -o /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp/ELF_x86-64_perf.o /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.src/llvm/test/ExecutionEngine/JITLink/x86-64/ELF_perf.s
+ /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/llvm-mc -triple=x86_64-unknown-linux -position-independent -filetype=obj -o /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp/ELF_x86-64_perf.o /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.src/llvm/test/ExecutionEngine/JITLink/x86-64/ELF_perf.s
RUN: at line 9: env JITDUMPDIR="/home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp" /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/llvm-jitlink -perf-support      /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp/ELF_x86-64_perf.o
+ env JITDUMPDIR=/home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/llvm-jitlink -perf-support /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp/ELF_x86-64_perf.o
Writing unwind record with unwind data size 104 and EH frame header size 12 and mapped size 0
llvm-jitlink: /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.src/llvm/include/llvm/ExecutionEngine/Orc/SymbolStringPool.h:285: llvm::orc::SymbolStringPool::~SymbolStringPool(): Assertion `Pool.empty() && "Dangling references at pool destruction time"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/llvm-jitlink -perf-support /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp/ELF_x86-64_perf.o
 #0 0x0000000000d18308 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/llvm-jitlink+0xd18308)
 #1 0x0000000000d1585c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007fe6ed092910 __restore_rt (/lib64/libpthread.so.0+0x16910)
 #3 0x00007fe6ec9c0d2b raise (/lib64/libc.so.6+0x4ad2b)
 #4 0x00007fe6ec9c23e5 abort (/lib64/libc.so.6+0x4c3e5)
 #5 0x00007fe6ec9b8c6a __assert_fail_base (/lib64/libc.so.6+0x42c6a)
 #6 0x00007fe6ec9b8cf2 (/lib64/libc.so.6+0x42cf2)
 #7 0x000000000066affb (/home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/llvm-jitlink+0x66affb)
 #8 0x0000000000be3ae1 llvm::orc::ExecutorProcessControl::~ExecutorProcessControl() (/home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/llvm-jitlink+0xbe3ae1)
 #9 0x0000000000be3e03 llvm::orc::SelfExecutorProcessControl::~SelfExecutorProcessControl() (/home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/llvm-jitlink+0xbe3e03)
#10 0x0000000000b0dae9 llvm::orc::ExecutionSession::~ExecutionSession() (/home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/llvm-jitlink+0xb0dae9)
#11 0x0000000000667a93 llvm::Session::~Session() (/home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/llvm-jitlink+0x667a93)
#12 0x000000000062e4c9 main (/home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/llvm-jitlink+0x62e4c9)
#13 0x00007fe6ec9ab24d __libc_start_main (/lib64/libc.so.6+0x3524d)
#14 0x000000000065cc8a _start /home/abuild/rpmbuild/BUILD/glibc-2.31/csu/../sysdeps/x86_64/start.S:122:0
/home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.script: line 4: 1357291 Aborted                 (core dumped) env JITDUMPDIR="/home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp" /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/llvm-jitlink -perf-support /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp/ELF_x86-64_perf.o

--

********************


@gulfemsavrun
Copy link
Contributor

This is just heads up that we started seeing this failure on our side after this patch:

[2554/2754](33) Building CXX object compiler-rt/lib/rtsan/CMakeFiles/RTRtsan.osx.dir/rtsan_interceptors_posix.cpp.o
FAILED: compiler-rt/lib/rtsan/CMakeFiles/RTRtsan.osx.dir/rtsan_interceptors_posix.cpp.o 
/Volumes/Work/s/w/ir/x/w/llvm_build/./bin/clang++ --target=x86_64-apple-darwin22.6.0 --sysroot=/Volumes/Work/s/w/ir/cache/macos_sdk/XCode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -D_DEBUG -D_GLIBCXX_ASSERTIONS -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/Volumes/Work/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/rtsan/.. -fPIC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffile-prefix-map=/Volumes/Work/s/w/ir/x/w/llvm_build/runtimes/runtimes-bins=../../../llvm-llvm-project -ffile-prefix-map=/Volumes/Work/s/w/ir/x/w/llvm-llvm-project/= -no-canonical-prefixes -Wall -Wno-unused-parameter -O3 -DNDEBUG -std=c++17 -arch arm64 -arch x86_64 -isysroot /Volumes/Work/s/w/ir/cache/macos_sdk/XCode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -stdlib=libc++ -mmacosx-version-min=10.13 -isysroot /Volumes/Work/s/w/ir/cache/macos_sdk/XCode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -fPIC -fno-builtin -fno-exceptions -funwind-tables -fno-stack-protector -fno-sanitize=safe-stack -fvisibility=hidden -fno-lto -Wthread-safety -Wthread-safety-reference -Wthread-safety-beta -O3 -g -Wno-gnu -Wno-variadic-macros -Wno-c99-extensions -ftrivial-auto-var-init=pattern -DSANITIZER_COMMON_NO_REDEFINE_BUILTINS -fno-rtti -MD -MT compiler-rt/lib/rtsan/CMakeFiles/RTRtsan.osx.dir/rtsan_interceptors_posix.cpp.o -MF compiler-rt/lib/rtsan/CMakeFiles/RTRtsan.osx.dir/rtsan_interceptors_posix.cpp.o.d -o compiler-rt/lib/rtsan/CMakeFiles/RTRtsan.osx.dir/rtsan_interceptors_posix.cpp.o -c /Volumes/Work/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
/Volumes/Work/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp:301:13: error: unknown type name 'cookie_io_functions_t'
  301 |             cookie_io_functions_t funcs) {
      |             ^
/Volumes/Work/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp:301:13: error: unknown type name 'cookie_io_functions_t'
/Volumes/Work/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp:300:1: error: 'substitution_fopencookie' causes a section type conflict with 'substitution_open'
  300 | INTERCEPTOR(FILE *, fopencookie, void *cookie, const char *mode,
      | ^
/Volumes/Work/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/rtsan/../interception/interception.h:332:3: note: expanded from macro 'INTERCEPTOR'
  332 |   INTERCEPTOR_ZZZ(/*no symbol variants*/, ret_type, func, __VA_ARGS__)
      |   ^
/Volumes/Work/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/rtsan/../interception/interception.h:328:3: note: expanded from macro 'INTERCEPTOR_ZZZ'
  328 |   INTERPOSER(func);                                   \
      |   ^
/Volumes/Work/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/rtsan/../interception/interception.h:142:30: note: expanded from macro 'INTERPOSER'
  142 | const interpose_substitution substitution_##func_name[] \
      |                              ^
<scratch space>:74:1: note: expanded from here
   74 | substitution_fopencookie
      | ^
/Volumes/Work/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp:65:1: note: declared here
   65 | INTERCEPTOR(int, open, const char *path, int oflag, ...) {
      | ^
/Volumes/Work/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/rtsan/../interception/interception.h:332:3: note: expanded from macro 'INTERCEPTOR'
  332 |   INTERCEPTOR_ZZZ(/*no symbol variants*/, ret_type, func, __VA_ARGS__)
      |   ^
/Volumes/Work/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/rtsan/../interception/interception.h:328:3: note: expanded from macro 'INTERCEPTOR_ZZZ'
  328 |   INTERPOSER(func);                                   \
      |   ^
/Volumes/Work/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/rtsan/../interception/interception.h:142:30: note: expanded from macro 'INTERPOSER'
  142 | const interpose_substitution substitution_##func_name[] \
      |                              ^
<scratch space>:22:1: note: expanded from here
   22 | substitution_open
      | ^
/Volumes/Work/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp:301:13: error: unknown type name 'cookie_io_functions_t'
  301 |             cookie_io_functions_t funcs) {
      |             ^
4 errors generated.

https://logs.chromium.org/logs/fuchsia/buildbucket/cr-buildbucket/8726813137214075521/+/u/clang/build/stdout

@devnexen
Copy link
Member Author

devnexen commented Jan 3, 2025

Oh right macos dos not support it I see. Seems easy I just forgot to guard it with SANITIZER_INTERCEPT_FOPENCOOKIE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants