llvm / llvm/llvm-project

[flang] Internal error / crash in Preprocessor::IsIfPredicateTrue when evaluating macro expansion with operators

Open
#209,068 1 comment 0 reactions 0 assignees View on GitHub
crash-on-invalid flang:frontend generated by fuzzer
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/qvdh5dqPd

The following code:

```fortran
#DEFINE WHICH == 2
#if WHICH == 1
```

Resulted in this output:
```
fatal internal error: no case at flang/lib/Parser/preprocessor.cpp(1212)
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0. Program arguments: /usr/lib/llvm-22/bin/flang -fc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu x86-64 -resource-dir /usr/lib/llvm-22/lib/clang/22 -mframe-pointer=all -o /tmp/test-f6b374.o -x f95 /tmp/test.f90
#0 0x00007f6c6e096d5f llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm-22/lib/libLLVM.so.22.1+0x4dc7d5f)
#1 0x00007f6c6e0945d7 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-22/lib/libLLVM.so.22.1+0x4dc55d7)
#2 0x00007f6c6e097b2a (/usr/lib/llvm-22/lib/libLLVM.so.22.1+0x4dc8b2a)
#3 0x00007f6c68d6b330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
#4 0x00007f6c68dc4b2c pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x9eb2c)
#5 0x00007f6c68d6b27e raise (/lib/x86_64-linux-gnu/libc.so.6+0x4527e)
#6 0x00007f6c68d4e8ff abort (/lib/x86_64-linux-gnu/libc.so.6+0x288ff)
#7 0x000056482167061c (/usr/lib/llvm-22/bin/flang+0x2b2061c)
#8 0x000056482134c79e (/usr/lib/llvm-22/bin/flang+0x27fc79e)
#9 0x000056482134941e Fortran::parser::Preprocessor::IsIfPredicateTrue(Fortran::parser::TokenSequence const&, unsigned long, unsigned long, Fortran::parser::Prescanner&) (/usr/lib/llvm-22/bin/flang+0x27f941e)
#10 0x0000564821346650 Fortran::parser::Preprocessor::Directive(Fortran::parser::TokenSequence const&, Fortran::parser::Prescanner&) (/usr/lib/llvm-22/bin/flang+0x27f6650)
#11 0x0000564821358d18 Fortran::parser::Prescanner::SkipCommentLine(bool) (/usr/lib/llvm-22/bin/flang+0x2808d18)
#12 0x0000564821359f29 Fortran::parser::Prescanner::FreeFormContinuation() (/usr/lib/llvm-22/bin/flang+0x2809f29)
#13 0x00005648213554a5 Fortran::parser::Prescanner::SkipToNextSignificantCharacter() (/usr/lib/llvm-22/bin/flang+0x28054a5)
#14 0x00005648213538e8 Fortran::parser::Prescanner::NextToken(Fortran::parser::TokenSequence&) (/usr/lib/llvm-22/bin/flang+0x28038e8)
#15 0x00005648213506eb Fortran::parser::Prescanner::Statement() (/usr/lib/llvm-22/bin/flang+0x28006eb)
#16 0x000056482134f5f8 Fortran::parser::Prescanner::Prescan(Fortran::common::Interval) (/usr/lib/llvm-22/bin/flang+0x27ff5f8)
#17 0x000056482133d052 Fortran::parser::Parsing::Prescan(std::__cxx11::basic_string, std::allocator> const&, Fortran::parser::Options) (/usr/lib/llvm-22/bin/flang+0x27ed052)
#18 0x000056481f71a732 Fortran::frontend::FrontendAction::runPrescan() (/usr/lib/llvm-22/bin/flang+0xbca732)
#19 0x000056481f71f7b5 Fortran::frontend::CodeGenAction::beginSourceFileAction() (/usr/lib/llvm-22/bin/flang+0xbcf7b5)
#20 0x000056481f719f9e Fortran::frontend::FrontendAction::beginSourceFile(Fortran::frontend::CompilerInstance&, Fortran::frontend::FrontendInputFile const&) (/usr/lib/llvm-22/bin/flang+0xbc9f9e)
#21 0x000056481f701e7f Fortran::frontend::CompilerInstance::executeAction(Fortran::frontend::FrontendAction&) (/usr/lib/llvm-22/bin/flang+0xbb1e7f)
#22 0x000056481f71eba0 Fortran::frontend::executeCompilerInvocation(Fortran::frontend::CompilerInstance*) (/usr/lib/llvm-22/bin/flang+0xbceba0)
#23 0x000056481f6ffd34 fc1_main(llvm::ArrayRef, char const*) (/usr/lib/llvm-22/bin/flang+0xbafd34)
#24 0x000056481f6fefa4 main (/usr/lib/llvm-22/bin/flang+0xbaefa4)
#25 0x00007f6c68d501ca (/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
#26 0x00007f6c68d5028b __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
#27 0x000056481f6fdf45 _start (/usr/lib/llvm-22/bin/flang+0xbadf45)
flang-22: error: unable to execute command: Aborted (core dumped)
flang-22: error: flang frontend command failed due to signal (use -v to see invocation)
Ubuntu flang version 22.1.8 (++20260613092238+e80beda6e255-1~exp1~20260613092253.78)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-22/bin
flang-22: error: unable to execute command: Aborted (core dumped)
flang-22: note: diagnostic msg: Error generating preprocessed source(s).
```

To reproduce:
```
flang ./test.f90
```

Compiler version:
```
Ubuntu flang version 22.1.8 (++20260613092238+e80beda6e255-1~exp1~20260613092253.78)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-22/bin
```

*This bug was found by [fusion-fuzz](https://github.com/fusion-fuzz/fusion-fuzz)*

Contributor guide

Open the contributing guide

Research direction

Start with flang/lib/Parser/preprocessor.cpp around line 1212 and trace Preprocessor::IsIfPredicateTrue for the macro and operators in the issue. Run flang ./test.f90 using the supplied reproducer, then add or run a focused regression test showing the input is diagnosed or handled without an internal error or crash.

Written by the indexing model from the issue text.

Assessment

Tech stack
fortran
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.