Skip to content
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

[flang] Fix crash when handling benign USE conflict #121977

Merged
merged 1 commit into from
Jan 8, 2025

Conversation

klausler
Copy link
Contributor

@klausler klausler commented Jan 7, 2025

When the same name is used for distinct derived types in two modules, and at least one of those modules also defines a generic interface of the same name, name resolution crashes when both modules are USE'd into the same scope. The crash is due to some pointers into the symbol table becoming invalid when a symbol is replaced with a UseErrorDetails; set them to null. Also allow for extending a UseErrorDetails in place rather than emitting a spurious error message.

Fixes #121718.

When the same name is used for distinct derived types in two
modules, and at least one of those modules also defines a generic
interface of the same name, name resolution crashes when both
modules are USE'd into the same scope.  The crash is due to
some pointers into the symbol table becoming invalid when
a symbol is replaced with a UseErrorDetails; set them to null.
Also allow for extending a UseErrorDetails in place rather
than emitting a spurious error message.

Fixes llvm#121718.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Jan 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 7, 2025

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

When the same name is used for distinct derived types in two modules, and at least one of those modules also defines a generic interface of the same name, name resolution crashes when both modules are USE'd into the same scope. The crash is due to some pointers into the symbol table becoming invalid when a symbol is replaced with a UseErrorDetails; set them to null. Also allow for extending a UseErrorDetails in place rather than emitting a spurious error message.

Fixes #121718.


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

2 Files Affected:

  • (modified) flang/lib/Semantics/resolve-names.cpp (+6)
  • (added) flang/test/Semantics/bug121718.f90 (+31)
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 122c0a2ebb646a..724f1b28078356 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -3162,6 +3162,10 @@ ModuleVisitor::SymbolRename ModuleVisitor::AddUse(
 // Convert it to a UseError with this additional location.
 static bool ConvertToUseError(
     Symbol &symbol, const SourceName &location, const Scope &module) {
+  if (auto *ued{symbol.detailsIf<UseErrorDetails>()}) {
+    ued->add_occurrence(location, module);
+    return true;
+  }
   const auto *useDetails{symbol.detailsIf<UseDetails>()};
   if (!useDetails) {
     if (auto *genericDetails{symbol.detailsIf<GenericDetails>()}) {
@@ -3319,6 +3323,8 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
         combinedDerivedType = CreateLocalUseError();
       } else {
         ConvertToUseError(*localSymbol, location, *useModuleScope_);
+        localDerivedType = nullptr;
+        localGeneric = nullptr;
         combinedDerivedType = localSymbol;
       }
     }
diff --git a/flang/test/Semantics/bug121718.f90 b/flang/test/Semantics/bug121718.f90
new file mode 100644
index 00000000000000..e99391f227d72e
--- /dev/null
+++ b/flang/test/Semantics/bug121718.f90
@@ -0,0 +1,31 @@
+! RUN: %flang_fc1 2>&1 | FileCheck %s --allow-empty
+! CHECK-NOT: error
+! Regression test simplified from LLVM bug 121718.
+! Ensure no crash and no spurious error message.
+module m1
+  type foo
+    integer x
+  end type
+ contains
+  subroutine test
+    print *, foo(123)
+  end
+end
+module m2
+  interface foo
+    procedure f
+  end interface
+  type foo
+    real x
+  end type
+ contains
+  complex function f(x)
+    complex, intent(in) :: x
+    f = x
+  end
+end
+program main
+  use m1
+  use m2
+  call test
+end

@klausler klausler requested a review from vdonaldson January 8, 2025 17:04
@@ -3319,6 +3323,8 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
combinedDerivedType = CreateLocalUseError();
} else {
ConvertToUseError(*localSymbol, location, *useModuleScope_);
localDerivedType = nullptr;
localGeneric = nullptr;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we got to the else clause, then localGeneric should already be null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, but this makes it explicit that the localXXXX pointers into the contents of localSymbol are being invalidated.

@klausler klausler merged commit 9462ce8 into llvm:main Jan 8, 2025
11 checks passed
@klausler klausler deleted the bug121718 branch January 8, 2025 21:15
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jan 10, 2025

LLVM Buildbot has detected a new failure on builder premerge-monolithic-linux running on premerge-linux-1 while building flang at step 7 "test-build-unified-tree-check-all".

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

Here is the relevant piece of the build log for the reference
Step 7 (test-build-unified-tree-check-all) 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 /build/buildbot/premerge-monolithic-linux/build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp && mkdir -p /build/buildbot/premerge-monolithic-linux/build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp
+ rm -rf /build/buildbot/premerge-monolithic-linux/build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp
+ mkdir -p /build/buildbot/premerge-monolithic-linux/build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp
RUN: at line 7: /build/buildbot/premerge-monolithic-linux/build/bin/llvm-mc -triple=x86_64-unknown-linux -position-independent      -filetype=obj -o /build/buildbot/premerge-monolithic-linux/build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp/ELF_x86-64_perf.o /build/buildbot/premerge-monolithic-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/x86-64/ELF_perf.s
+ /build/buildbot/premerge-monolithic-linux/build/bin/llvm-mc -triple=x86_64-unknown-linux -position-independent -filetype=obj -o /build/buildbot/premerge-monolithic-linux/build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp/ELF_x86-64_perf.o /build/buildbot/premerge-monolithic-linux/llvm-project/llvm/test/ExecutionEngine/JITLink/x86-64/ELF_perf.s
RUN: at line 9: env JITDUMPDIR="/build/buildbot/premerge-monolithic-linux/build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp" /build/buildbot/premerge-monolithic-linux/build/bin/llvm-jitlink -perf-support      /build/buildbot/premerge-monolithic-linux/build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp/ELF_x86-64_perf.o
+ env JITDUMPDIR=/build/buildbot/premerge-monolithic-linux/build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp /build/buildbot/premerge-monolithic-linux/build/bin/llvm-jitlink -perf-support /build/buildbot/premerge-monolithic-linux/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: /build/buildbot/premerge-monolithic-linux/llvm-project/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: /build/buildbot/premerge-monolithic-linux/build/bin/llvm-jitlink -perf-support /build/buildbot/premerge-monolithic-linux/build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp/ELF_x86-64_perf.o
 #0 0x00005a0e1d984308 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /build/buildbot/premerge-monolithic-linux/llvm-project/llvm/lib/Support/Unix/Signals.inc:723:13
 #1 0x00005a0e1d981ebe llvm::sys::RunSignalHandlers() /build/buildbot/premerge-monolithic-linux/llvm-project/llvm/lib/Support/Signals.cpp:106:18
 #2 0x00005a0e1d9849b8 SignalHandler(int) /build/buildbot/premerge-monolithic-linux/llvm-project/llvm/lib/Support/Unix/Signals.inc:413:1
 #3 0x00007861c9825520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007861c98799fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x00007861c9825476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x00007861c980b7f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x00007861c980b71b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x00007861c981ce96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x00005a0e1d2010ac (/build/buildbot/premerge-monolithic-linux/build/bin/llvm-jitlink+0x23080ac)
#10 0x00005a0e1d866868 __is_single_threaded /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ext/atomicity.h:52:12
#11 0x00005a0e1d866868 __exchange_and_add_dispatch /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ext/atomicity.h:98:9
#12 0x00005a0e1d866868 _M_release /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/shared_ptr_base.h:180:10
#13 0x00005a0e1d866868 ~__shared_count /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/shared_ptr_base.h:705:11
#14 0x00005a0e1d866868 ~__shared_ptr /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/shared_ptr_base.h:1154:31
#15 0x00005a0e1d866868 llvm::orc::ExecutorProcessControl::~ExecutorProcessControl() /build/buildbot/premerge-monolithic-linux/llvm-project/llvm/lib/ExecutionEngine/Orc/ExecutorProcessControl.cpp:27:49
#16 0x00005a0e1d867fbf llvm::orc::SelfExecutorProcessControl::~SelfExecutorProcessControl() /build/buildbot/premerge-monolithic-linux/llvm-project/llvm/include/llvm/ExecutionEngine/Orc/ExecutorProcessControl.h:467:7
#17 0x00005a0e1d7893ff ~unique_ptr /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/unique_ptr.h:362:8
#18 0x00005a0e1d7893ff llvm::orc::ExecutionSession::~ExecutionSession() /build/buildbot/premerge-monolithic-linux/llvm-project/llvm/lib/ExecutionEngine/Orc/Core.cpp:1600:1
#19 0x00005a0e1d1da4ed llvm::Session::~Session() /build/buildbot/premerge-monolithic-linux/llvm-project/llvm/tools/llvm-jitlink/llvm-jitlink.cpp:1033:1
#20 0x00005a0e1d1e4c08 operator() /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/unique_ptr.h:85:2
#21 0x00005a0e1d1e4c08 reset /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/unique_ptr.h:182:4
#22 0x00005a0e1d1e4c08 reset /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/unique_ptr.h:456:7
#23 0x00005a0e1d1e4c08 main /build/buildbot/premerge-monolithic-linux/llvm-project/llvm/tools/llvm-jitlink/llvm-jitlink.cpp:2584:5
#24 0x00007861c980cd90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#25 0x00007861c980ce40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#26 0x00005a0e1d1d2b65 _start (/build/buildbot/premerge-monolithic-linux/build/bin/llvm-jitlink+0x22d9b65)
/build/buildbot/premerge-monolithic-linux/build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.script: line 4: 2582688 Aborted                 env JITDUMPDIR="/build/buildbot/premerge-monolithic-linux/build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp" /build/buildbot/premerge-monolithic-linux/build/bin/llvm-jitlink -perf-support /build/buildbot/premerge-monolithic-linux/build/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp/ELF_x86-64_perf.o

--

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


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
5 participants