<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/121201>121201</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[analyzer] loop unrolling crash
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
shenjunjiekoda
</td>
</tr>
</table>
<pre>
Found a crash when using analyzer from project `libfmt` 's file `test/std-test.cc`.
After reduce the case:
```cpp
template <bool, typename T, typename> using conditional_t = T;
class basic_format_arg;
template <typename> struct formatter;
template <typename Context> struct value {
template <typename T> value(T) {
using value_type = T;
format_custom_arg<value_type,
typename Context::template formatter_type<value_type>>;
}
template <typename, typename Formatter> static void format_custom_arg() {
Context ctx;
auto f = Formatter();
f.format(0, ctx);
}
};
struct context {
template <typename T> using formatter_type = formatter<T>;
};
enum { max_packed_args };
template <typename Context, long>
using arg_t = conditional_t<max_packed_args, value<Context>, basic_format_arg>;
template <int NUM_ARGS> struct format_arg_store {
arg_t<context, NUM_ARGS> args;
};
template <typename... T, long NUM_ARGS = sizeof...(T)>
auto make_format_args(T... args) -> format_arg_store<NUM_ARGS> {
return {args...};
}
template <typename F> void write_padded(context, F write) { write(0); };
template <typename... T> void format(T... args) { make_format_args(args...); }
template <int> struct bitset {
bitset(long);
};
template <long N> struct formatter<bitset<N>> {
struct writer {
bitset<N> bs;
template <typename OutputIt> void operator()(OutputIt) {
for (auto pos = N; pos > 0; --pos)
;
}
};
template <typename FormatContext>
void format(bitset<N> bs, FormatContext ctx) {
write_padded(ctx, writer{bs});
}
};
bitset<6> TestBody_bs = (2);
void TestBody() { format(TestBody_bs); }
```
```shell
$ clang-tidy "-checks=-*,clang-analyzer-core.*" "/path/to/file" "--" "-Xclang" "-analyzer-config" "-Xclang" "unroll-loops=true"
```
crash msg
```
1. <eof> parser at end of file
2. While analyzing stack:
#0 Calling formatter<bitset<6>>::writer::operator()(int)
#0 0x00007f58617c81d8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/local/bin/../lib/../lib/libLLVMSupport.so.16+0x1ce1d8)
#1 0x00007f58617c5c2c SignalHandler(int) Signals.cpp:0:0
#2 0x00007f586580edd0 (/lib/x86_64-linux-gnu/libc.so.6+0x38dd0)
#3 0x00007f586580ed51 raise (/lib/x86_64-linux-gnu/libc.so.6+0x38d51)
#4 0x00007f58657f8537 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22537)
#5 0x00007f58657f840f (/lib/x86_64-linux-gnu/libc.so.6+0x2240f)
#6 0x00007f58658076d2 (/lib/x86_64-linux-gnu/libc.so.6+0x316d2)
#7 0x00007f58616766b2 (/usr/local/bin/../lib/../lib/libLLVMSupport.so.16+0x7c6b2)
#8 0x00007f585b12fa7a clang::ento::shouldCompletelyUnroll(clang::Stmt const*, clang::ASTContext&, clang::ento::ExplodedNode*, unsigned int&) (.part.0) LoopUnrolling.cpp:0:0
#9 0x00007f585b130ff3 clang::ento::updateLoopStack(clang::Stmt const*, clang::ASTContext&, clang::ento::ExplodedNode*, unsigned int) (/usr/local/bin/../lib/../lib/../lib/../lib/libclangStaticAnalyzerCore.so.16+0x16aff3)
#10 0x00007f585b0dc770 clang::ento::ExprEngine::processCFGBlockEntrance(clang::BlockEdge const&, clang::ento::NodeBuilderWithSinks&, clang::ento::ExplodedNode*) (/usr/local/bin/../lib/../lib/../lib/../lib/libclangStaticAnalyzerCore.so.16+0x116770)
#11 0x00007f585b0abdc6 clang::ento::CoreEngine::HandleBlockEdge(clang::BlockEdge const&, clang::ento::ExplodedNode*) (/usr/local/bin/../lib/../lib/../lib/../lib/libclangStaticAnalyzerCore.so.16+0xe5dc6)
#12 0x00007f585b0ace5a clang::ento::CoreEngine::dispatchWorkItem(clang::ento::ExplodedNode*, clang::ProgramPoint, clang::ento::WorkListUnit const&) (/usr/local/bin/../lib/../lib/../lib/../lib/libclangStaticAnalyzerCore.so.16+0xe6e5a)
#13 0x00007f585b0ad0aa clang::ento::CoreEngine::ExecuteWorkList(clang::LocationContext const*, unsigned int, llvm::IntrusiveRefCntPtr<clang::ento::ProgramState const>) (/usr/local/bin/../lib/../lib/../lib/../lib/libclangStaticAnalyzerCore.so.16+0xe70aa)
#14 0x00007f585f7e13a2 (anonymous namespace)::AnalysisConsumer::RunPathSensitiveChecks(clang::Decl*, clang::ento::ExprEngine::InliningModes, llvm::DenseSet<clang::Decl const*, llvm::DenseMapInfo<clang::Decl const*, void>>*) AnalysisConsumer.cpp:0:0
#15 0x00007f585f7fcc3a (anonymous namespace)::AnalysisConsumer::HandleDeclsCallGraph(unsigned int) AnalysisConsumer.cpp:0:0
#16 0x00007f585f7fdd53 (anonymous namespace)::AnalysisConsumer::HandleTranslationUnit(clang::ASTContext&) AnalysisConsumer.cpp:0:0
#17 0x00007f585f5b6468 clang::MultiplexConsumer::HandleTranslationUnit(clang::ASTContext&) (/usr/local/bin/../lib/../lib/../lib/libclangFrontend.so.16+0x15a468)
#18 0x00007f585a3762c5 clang::ParseAST(clang::Sema&, bool, bool) (/usr/local/bin/../lib/../lib/../lib/../lib/libclangParse.so.16+0x352c5)
#19 0x00007f585f57fbb9 clang::FrontendAction::Execute() (/usr/local/bin/../lib/../lib/../lib/libclangFrontend.so.16+0x123bb9)
#20 0x00007f585f4f62c6 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/local/bin/../lib/../lib/../lib/libclangFrontend.so.16+0x9a2c6)
#21 0x00007f586222de7d clang::tooling::FrontendActionFactory::runInvocation(std::shared_ptr<clang::CompilerInvocation>, clang::FileManager*, std::shared_ptr<clang::PCHContainerOperations>, clang::DiagnosticConsumer*) (/usr/local/bin/../lib/../lib/libclangTooling.so.16+0x6fe7d)
#22 0x00007f58622ee1ca clang::tidy::(anonymous namespace)::ActionFactory::runInvocation(std::shared_ptr<clang::CompilerInvocation>, clang::FileManager*, std::shared_ptr<clang::PCHContainerOperations>, clang::DiagnosticConsumer*) ClangTidyStandaloneRunner.cpp:0:0
#23 0x00007f58622248fc clang::tooling::ToolInvocation::runInvocation(char const*, clang::driver::Compilation*, std::shared_ptr<clang::CompilerInvocation>, std::shared_ptr<clang::PCHContainerOperations>) (/usr/local/bin/../lib/../lib/libclangTooling.so.16+0x668fc)
#24 0x00007f5862229de8 clang::tooling::ToolInvocation::run() (/usr/local/bin/../lib/../lib/libclangTooling.so.16+0x6bde8)
#25 0x00007f586222bc71 clang::tooling::ClangTool::runCompileCommands(clang::tooling::ToolAction*) (/usr/local/bin/../lib/../lib/libclangTooling.so.16+0x6dc71)
#26 0x00007f586222ccae clang::tooling::ClangTool::run(clang::tooling::ToolAction*) (/usr/local/bin/../lib/../lib/libclangTooling.so.16+0x6ecae)
#27 0x00007f58622ef2dd clang::tidy::runClangTidy(clang::tidy::ClangTidyContext&, clang::tooling::CompilationDatabase const&, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>, bool, bool, llvm::StringRef) (/usr/local/bin/../lib/../lib/libclangTidy.so.16+0x9f2dd)
#28 0x00007f58659d850e clang::tidy::clangTidyMain(int, char const**) (/usr/local/bin/../lib/libclangTidyMain.so.16+0x1850e)
#29 0x00007f58657f9d7a __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x23d7a)
#30 0x000055f1e8a4e08a _start (/usr/local/bin/clang-tidy+0x108a)
```
I see the reason may be in the `clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp`
```cpp
static bool shouldCompletelyUnroll(const Stmt *LoopStmt, ASTContext &ASTCtx,
ExplodedNode *Pred, unsigned &maxStep) {
// ....
if (InitNum.getBitWidth() != BoundNum.getBitWidth()) {
InitNum = InitNum.zext(BoundNum.getBitWidth());
BoundNum = BoundNum.zext(InitNum.getBitWidth());
}
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzcWt1z26gW_2vICxOPhKwPP-TBUeLdzLTdT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