Skip to content

[rtsan] Fix issue where close test would lead to crash #144017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2025

Conversation

cjappl
Copy link
Contributor

@cjappl cjappl commented Jun 13, 2025

fixes: #138311

See that ticket for the steps that led to the crash.

Changes in this PR

  1. Change so that we always try to close an open fd by making it use our OpenFile fixture
  2. Ensure we never close the fd twice by adjusting the fixture
  3. Out of an abundance of caution, split the test in two so we never double close.

@llvmbot
Copy link
Member

llvmbot commented Jun 13, 2025

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

Author: Chris Apple (cjappl)

Changes

fixes: #138311

See that ticket for the steps that led to the crash.

Changes in this PR

  1. Change so that we always try to close an open fd by making it use our OpenFile fixture
  2. Ensure we never close the fd twice by adjusting the fixture
  3. Out of an abundance of caution, split the test in two so we never double close.

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

1 Files Affected:

  • (modified) compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp (+13-7)
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 048da5858d665..aa4ad6477e6e1 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -449,12 +449,6 @@ TEST_F(RtsanFileTest, FcntlSetFdDiesWhenRealtime) {
   close(fd);
 }
 
-TEST(TestRtsanInterceptors, CloseDiesWhenRealtime) {
-  auto Func = []() { close(0); };
-  ExpectRealtimeDeath(Func, "close");
-  ExpectNonRealtimeSurvival(Func);
-}
-
 TEST(TestRtsanInterceptors, ChdirDiesWhenRealtime) {
   auto Func = []() { chdir("."); };
   ExpectRealtimeDeath(Func, "chdir");
@@ -606,8 +600,10 @@ class RtsanOpenedFileTest : public RtsanFileTest {
   }
 
   void TearDown() override {
-    if (file != nullptr)
+    const bool is_open = fcntl(fd, F_GETFD) != -1;
+    if (is_open && file != nullptr)
       fclose(file);
+
     RtsanFileTest::TearDown();
   }
 
@@ -620,6 +616,16 @@ class RtsanOpenedFileTest : public RtsanFileTest {
   int fd = -1;
 };
 
+TEST_F(RtsanOpenedFileTest, CloseDiesWhenRealtime) {
+  auto Func = [this]() { close(GetOpenFd()); };
+  ExpectRealtimeDeath(Func, "close");
+}
+
+TEST_F(RtsanOpenedFileTest, CloseSurvivesWhenNotRealtime) {
+  auto Func = [this]() { close(GetOpenFd()); };
+  ExpectNonRealtimeSurvival(Func);
+}
+
 #if SANITIZER_INTERCEPT_FSEEK
 TEST_F(RtsanOpenedFileTest, FgetposDieWhenRealtime) {
   auto Func = [this]() {

@cjappl cjappl merged commit 147a4c7 into llvm:main Jun 15, 2025
10 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jun 15, 2025

LLVM Buildbot has detected a new failure on builder clang-aarch64-sve2-vla-2stage running on linaro-g4-02 while building compiler-rt at step 12 "ninja check 2".

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

Here is the relevant piece of the build log for the reference
Step 12 (ninja check 2) failure: stage 2 checked (failure)
...
[637/1391] Linking CXX executable tools/clang/unittests/AllClangUnitTests
clang++: warning: argument unused during compilation: '-mllvm -scalable-vectorization=preferred' [-Wunused-command-line-argument]
clang++: warning: argument unused during compilation: '-mllvm -treat-scalable-fixed-error-as-warning=false' [-Wunused-command-line-argument]
[641/1391] Linking CXX executable tools/clang/unittests/Format/FormatTests
clang++: warning: argument unused during compilation: '-mllvm -scalable-vectorization=preferred' [-Wunused-command-line-argument]
clang++: warning: argument unused during compilation: '-mllvm -treat-scalable-fixed-error-as-warning=false' [-Wunused-command-line-argument]
[763/1391] Linking CXX executable unittests/BinaryFormat/BinaryFormatTests
clang++: warning: argument unused during compilation: '-mllvm -scalable-vectorization=preferred' [-Wunused-command-line-argument]
clang++: warning: argument unused during compilation: '-mllvm -treat-scalable-fixed-error-as-warning=false' [-Wunused-command-line-argument]
[770/1391] Linking CXX executable unittests/AsmParser/AsmParserTests
FAILED: unittests/AsmParser/AsmParserTests 
: && /home/tcwg-buildbot/worker/clang-aarch64-sve2-vla-2stage/stage1.install/bin/clang++ -mcpu=neoverse-v2 -mllvm -scalable-vectorization=preferred -mllvm -treat-scalable-fixed-error-as-warning=false -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -O3 -DNDEBUG -fuse-ld=lld -Wl,--color-diagnostics    -Wl,--gc-sections unittests/AsmParser/CMakeFiles/AsmParserTests.dir/AsmParserTest.cpp.o -o unittests/AsmParser/AsmParserTests  lib/libLLVMAsmParser.a  lib/libLLVMCore.a  lib/libLLVMSupport.a  lib/libLLVMSupport.a  lib/libllvm_gtest_main.a  lib/libllvm_gtest.a  lib/libLLVMRemarks.a  lib/libLLVMBitstreamReader.a  lib/libLLVMBinaryFormat.a  lib/libLLVMTargetParser.a  lib/libLLVMSupport.a  lib/libLLVMDemangle.a  -lrt  -ldl  -lm  /usr/lib/aarch64-linux-gnu/libz.so  -lpthread && :
clang++: warning: argument unused during compilation: '-mllvm -scalable-vectorization=preferred' [-Wunused-command-line-argument]
clang++: warning: argument unused during compilation: '-mllvm -treat-scalable-fixed-error-as-warning=false' [-Wunused-command-line-argument]
ld.lld: error: cannot open lib/libllvm_gtest_main.a: No such file or directory
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
[797/1391] Building CXX object unittests/CodeGen/CMakeFiles/CodeGenTests.dir/CCStateTest.cpp.o
[799/1391] Building CXX object unittests/CodeGen/CMakeFiles/CodeGenTests.dir/MachineBasicBlockTest.cpp.o
[800/1391] Building CXX object unittests/CodeGen/CMakeFiles/CodeGenTests.dir/DIETest.cpp.o
[801/1391] Building CXX object unittests/CodeGen/CMakeFiles/CodeGenTests.dir/MachineDomTreeUpdaterTest.cpp.o
[802/1391] Building CXX object unittests/CodeGen/CMakeFiles/CodeGenTests.dir/LexicalScopesTest.cpp.o
[803/1391] Building CXX object unittests/CodeGen/CMakeFiles/CodeGenTests.dir/MachineInstrTest.cpp.o
[804/1391] Building CXX object unittests/CodeGen/CMakeFiles/CodeGenTests.dir/AsmPrinterDwarfTest.cpp.o
[805/1391] Building CXX object unittests/ADT/CMakeFiles/ADTTests.dir/SmallVectorTest.cpp.o
[806/1391] Building CXX object unittests/CodeGen/CMakeFiles/CodeGenTests.dir/InstrRefLDVTest.cpp.o
ninja: build stopped: subcommand failed.

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.

RTSan unit-tests can crash due to use of hard-coded file descirptors
4 participants