llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.32k stars 11.69k forks source link

Flang crashes when calling procedure dummy argument #104798

Open everythingfunctional opened 1 month ago

everythingfunctional commented 1 month ago

I've documented the issue in another repo, but the following example reproduces the issue.

module example_mod
    abstract interface
        subroutine sub_i
        end subroutine
    end interface
contains
    subroutine call_internal(string)
        character(len=*), intent(in) :: string
        call call_it(print_it)
    contains
        subroutine print_it
            print *, string
        end subroutine
    end subroutine
    subroutine call_it(sub)
        procedure(sub_i) :: sub
        call sub
    end subroutine
end module

program example_prog
    use example_mod
    call call_internal("Hello")
end program
klausler commented 1 month ago

Test works with current llvm-project/main.

everythingfunctional commented 1 month ago

I just did a rebuild of flang and it still crashes for me. Can we reopen this, or is there something I'm doing wrong? I.e.

$ flang-new --version
flang-new version 20.0.0git (git@github.com:llvm/llvm-project.git e8ca306fbefdb8385c53d6cc029251d1d5be7049)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/brad/Repositories/GitHub/BerkeleyLab/llvm-build/bin
Build config: +assertions
$ flang-new -g example.f90
$ lldb ./a.out
(lldb) target create "./a.out"
Current executable set to '/home/brad/Repositories/GitHub/BerkeleyLab/reproducers/compiler/Fortran/flang/mapped-object-invalid-permission/a.out' (x86_64).
(lldb) run
Process 2533326 launched: '/home/brad/Repositories/GitHub/BerkeleyLab/reproducers/compiler/Fortran/flang/mapped-object-invalid-permission/a.out' (x86_64)
Process 2533326 stopped
* thread #1, name = 'a.out', stop reason = signal SIGSEGV: invalid permissions for mapped object (fault address: 0x7fffffffe310)
    frame #0: 0x00007fffffffe310
->  0x7fffffffe310: movabsq $0x555555556530, %r11 ; imm = 0x555555556530 
    0x7fffffffe31a: movabsq $0x7fffffffe300, %r10 ; imm = 0x7FFFFFFFE300 
    0x7fffffffe324: jmpq   *%r11
    0x7fffffffe327: addb   %bh, 0x53(%rdx)
(lldb) bt
* thread #1, name = 'a.out', stop reason = signal SIGSEGV: invalid permissions for mapped object (fault address: 0x7fffffffe310)
  * frame #0: 0x00007fffffffe310
    frame #1: 0x0000555555556586 a.out`_QMexample_modPcall_it at example.f90:17:9
    frame #2: 0x0000555555556522 a.out`_QMexample_modPcall_internal at example.f90:9:9
    frame #3: 0x00005555555565b8 a.out`_QQmain at example.f90:23:5
    frame #4: 0x00005555555565d2 a.out`main at example.f90:24:1
    frame #5: 0x00007ffff7cd5e08 libc.so.6`__libc_start_call_main(main=(a.out`main), argc=1, argv=0x00007fffffffe488) at libc_start_call_main.h:58:16
    frame #6: 0x00007ffff7cd5ecc libc.so.6`__libc_start_main_impl(main=(a.out`main), argc=1, argv=0x00007fffffffe488, init=<unavailable>, fini=<unavailable>, rtld_fini=<unavailable>, stack_end=0x00007fffffffe478) at libc-start.c:360:3
    frame #7: 0x0000555555556405 a.out`_start + 37
klausler commented 1 month ago

Reconfirmed, current llvm-project/main works on this test for me, with and without -g, on x86-64 Linux. Perhaps something is different in your environment; it seems that the trampoline code being called to invoke a dummy procedure is not in an executable page.

everythingfunctional commented 1 month ago

Agreed, but the example above does work if you change print *, string to print *, "string". The key element seems to be the reference to the host associated dummy argument.

klausler commented 1 month ago

Access to a host associated object from an internal procedure is exactly what a trampoline is used for.

everythingfunctional commented 1 month ago

Ok, so do you have any ideas for how I can figure out what's wrong with my environment?

everythingfunctional commented 1 month ago

So I tried out this example on a different system, and it worked. But, in doing so I noticed this warning:

/usr/lib64/gcc/x86_64-suse-linux/12/../../../../x86_64-suse-linux/bin/ld: warning: /tmp/example-a1eb53.o: missing .note.GNU-stack section implies executable stack
/usr/lib64/gcc/x86_64-suse-linux/12/../../../../x86_64-suse-linux/bin/ld: NOTE: This behaviour is deprecated and will be removed in a future version of the linker

And the version of the linker is:

GNU ld (GNU Binutils; SUSE Linux Enterprise 15) 2.41.0.20230908-150100.7.46
Copyright (C) 2023 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.

On my system where this example fails, I do not get that warning, and the version of the linker is:

GNU ld (GNU Binutils) 2.43.0
Copyright (C) 2024 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.

Thus, it looks like the implied executable stack behaviour has finally been removed in the latest version of the GNU linker. So how do we indicate to the linker that we need an executable stack?

everythingfunctional commented 1 month ago

Some quick googling and for now adding -Wl,-z,execstack works. I think the correct linker directive should be added to the generated assembly though (i.e. .section .note.GNU-stack,"x",@progbits), to avoid future user confusion. Here's the stackoverflow answer I found for reference.

rouson commented 3 weeks ago

I just ran the provided reproducer on macOS with an M2 processor and got the following:

LLVM ERROR: INIT_TRAMPOLINE operation is only supported on Linux.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.  Program arguments: /Users/rouson/Repositories/llvm-project/build/bin/flang-new -fc1 -triple arm64-apple-macosx14.0.0 -emit-obj -fcolor-diagnostics -mrelocation-model pic -pic-level 2 -target-cpu apple-m1 -target-feature +zcm -target-feature +zcz -target-feature +v8.4a -target-feature +aes -target-feature +altnzcv -target-feature +ccdp -target-feature +complxnum -target-feature +crc -target-feature +dotprod -target-feature +fp-armv8 -target-feature +fp16fml -target-feature +fptoint -target-feature +fullfp16 -target-feature +jsconv -target-feature +lse -target-feature +neon -target-feature +pauth -target-feature +perfmon -target-feature +predres -target-feature +ras -target-feature +rcpc -target-feature +rdm -target-feature +sb -target-feature +sha2 -target-feature +sha3 -target-feature +specrestrict -target-feature +ssbs -resource-dir /Users/rouson/Repositories/llvm-project/build/lib/clang/20 -mframe-pointer=non-leaf -o /var/folders/kb/8rtthbvd4xbddz_l8hmm1xnw0000gp/T/reproducer-39e4cb.o -x f95-cpp-input reproducer.f90
1.  Running pass 'Function Pass Manager' on module 'FIRModule'.
2.  Running pass 'AArch64 Instruction Selection' on function '@_QMexample_modPcall_internal'
 #0 0x00000001004db494 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x10033f494)
 #1 0x00000001004d96a8 llvm::sys::RunSignalHandlers() (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x10033d6a8)
 #2 0x00000001004dbb50 SignalHandler(int) (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x10033fb50)
 #3 0x0000000194afa584 (/usr/lib/system/libsystem_platform.dylib+0x18047a584)
 #4 0x0000000194ac9c20 (/usr/lib/system/libsystem_pthread.dylib+0x180449c20)
 #5 0x00000001949d6a30 (/usr/lib/system/libsystem_c.dylib+0x180356a30)
 #6 0x0000000100441a94 llvm::report_fatal_error(llvm::Twine const&, bool) (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x1002a5a94)
 #7 0x00000001004418cc llvm::report_fatal_error(llvm::Twine const&, bool) (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x1002a58cc)
 #8 0x00000001002ba86c llvm::AArch64TargetLowering::LowerINIT_TRAMPOLINE(llvm::SDValue, llvm::SelectionDAG&) const (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x10011e86c)
 #9 0x00000001002bb438 llvm::AArch64TargetLowering::LowerOperation(llvm::SDValue, llvm::SelectionDAG&) const (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x10011f438)
#10 0x000000010096e104 (anonymous namespace)::SelectionDAGLegalize::LegalizeOp(llvm::SDNode*) (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x1007d2104)
#11 0x000000010096d8a4 llvm::SelectionDAG::Legalize() (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x1007d18a4)
#12 0x0000000100aa13d0 llvm::SelectionDAGISel::CodeGenAndEmitDAG() (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x1009053d0)
#13 0x0000000100aa0b10 llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x100904b10)
#14 0x0000000100a9ef38 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x100902f38)
#15 0x0000000100a9db5c llvm::SelectionDAGISelLegacy::runOnMachineFunction(llvm::MachineFunction&) (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x100901b5c)
#16 0x000000010319346c llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x102ff746c)
#17 0x000000010444d450 llvm::FPPassManager::runOnFunction(llvm::Function&) (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x1042b1450)
#18 0x0000000104453f48 llvm::FPPassManager::runOnModule(llvm::Module&) (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x1042b7f48)
#19 0x000000010444df78 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x1042b1f78)
#20 0x000000010052482c Fortran::frontend::CodeGenAction::executeAction() (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x10038882c)
#21 0x000000010051c0b0 Fortran::frontend::FrontendAction::execute() (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x1003800b0)
#22 0x000000010050ab2c Fortran::frontend::CompilerInstance::executeAction(Fortran::frontend::FrontendAction&) (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x10036eb2c)
#23 0x00000001006420f8 Fortran::frontend::executeCompilerInvocation(Fortran::frontend::CompilerInstance*) (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x1004a60f8)
#24 0x00000001001a2924 fc1_main(llvm::ArrayRef<char const*>, char const*) (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x100006924)
#25 0x00000001001a15c0 main (/Users/rouson/Repositories/llvm-project/build/bin/flang-new+0x1000055c0)
#26 0x000000019473f154 
flang-new: error: unable to execute command: Abort trap: 6
flang-new: error: flang frontend command failed due to signal (use -v to see invocation)
flang-new version 20.0.0git (git@github.com:llvm/llvm-project fe5d1f901a709bc6a2180b7a77b9d5948c6c3482)
Target: arm64-apple-darwin23.6.0
Thread model: posix
InstalledDir: /Users/rouson/Repositories/llvm-project/build/bin
flang-new: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
flang-new: note: diagnostic msg: /var/folders/kb/8rtthbvd4xbddz_l8hmm1xnw0000gp/T/reproducer-32aaa9
flang-new: note: diagnostic msg: /var/folders/kb/8rtthbvd4xbddz_l8hmm1xnw0000gp/T/reproducer-32aaa9.sh
flang-new: note: diagnostic msg: Crash backtrace is located in
flang-new: note: diagnostic msg: /Users/rouson/Library/Logs/DiagnosticReports/flang-new_<YYYY-MM-DD-HHMMSS>_<hostname>.crash
flang-new: note: diagnostic msg: (choose the .crash file that corresponds to your crash)
flang-new: note: diagnostic msg: 

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