Skip to content

Clang crash in CheckArrayAccess; division by zero #64564

@bevin-hansson

Description

@bevin-hansson

Clang crashes on the following (very reduced) example:

a[][0] int b = a[217727][0]

Building this with -target avr causes a crash:

 #0 0x00005594ccd5d8df llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x3ba18df)
 #1 0x00005594ccd5b90c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x3b9f90c)
 #2 0x00005594cccae518 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007fdafa17c420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #4 0x00005594ccc8bb35 llvm::APInt::udiv(llvm::APInt const&) const (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x3acfb35)
 #5 0x00005594cf443478 clang::Sema::CheckArrayAccess(clang::Expr const*, clang::Expr const*, clang::ArraySubscriptExpr const*, bool, bool) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x6287478)
 #6 0x00005594cf443bd8 clang::Sema::CheckArrayAccess(clang::Expr const*) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x6287bd8)
 #7 0x00005594cf48df7c clang::Sema::CheckCompletedExpr(clang::Expr*, clang::SourceLocation, bool) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x62d1f7c)
 #8 0x00005594cf8ff3f2 clang::Sema::ActOnFinishFullExpr(clang::Expr*, clang::SourceLocation, bool, bool, bool) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x67433f2)
 #9 0x00005594cf5dccf8 clang::Sema::AddInitializerToDecl(clang::Decl*, clang::Expr*, bool) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x6420cf8)
#10 0x00005594cf29c68d clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x60e068d)
#11 0x00005594cf2b01b6 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x60f41b6)
#12 0x00005594cf279617 clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x60bd617)
#13 0x00005594cf27a75f clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (.part.0) Parser.cpp:0:0
#14 0x00005594cf282194 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x60c6194)
#15 0x00005594cf28372d clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x60c772d)
#16 0x00005594cf272a8a clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x60b6a8a)
#17 0x00005594ce04d865 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x4e91865)
#18 0x00005594cd929671 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x476d671)
#19 0x00005594cd8ad993 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x46f1993)
#20 0x00005594cda0db3b clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x4851b3b)
#21 0x00005594ca53641c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x137a41c)
#22 0x00005594ca531bdc ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#23 0x00005594cd70ece9 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#24 0x00005594cccae977 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x3af2977)
#25 0x00005594cd70ef1c clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#26 0x00005594cd6d73fc clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x451b3fc)
#27 0x00005594cd6d7ded clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x451bded)
#28 0x00005594cd6e223c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x452623c)
#29 0x00005594ca534523 clang_main(int, char**) (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x1378523)
#30 0x00007fdaf9c2a083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#31 0x00005594ca52dd1e _start (/opt/compiler-explorer/clang-16.0.0/bin/clang+0x1371d1e)

See the following Godbolt: https://godbolt.org/z/WdEqPWad9

The Godbolt compiler doesn't give an assertion, but when running with a debug build, you get

clang: ../lib/Support/APInt.cpp:1584: llvm::APInt llvm::APInt::udiv(const llvm::APInt&) const: Assertion `RHS.U.VAL != 0 && "Divide by zero?"' failed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"crashPrefer [crash-on-valid] or [crash-on-invalid]

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions