[llvm-bugs] [Bug 26586] New: Clang crashes with internal compiler error when compiling a simple program with templates

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Feb 11 17:19:00 PST 2016


https://llvm.org/bugs/show_bug.cgi?id=26586

            Bug ID: 26586
           Summary: Clang crashes with internal compiler error when
                    compiling a simple program with templates
           Product: clang
           Version: 3.6
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++11
          Assignee: unassignedclangbugs at nondot.org
          Reporter: beck.ct at gmail.com
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org
    Classification: Unclassified

Created attachment 15889
  --> https://llvm.org/bugs/attachment.cgi?id=15889&action=edit
cpp file which clang instructed me to attach

Both gcc 4.9 and gcc 5.2 compile and run the following small program correctly
at C++11 standard, however both clang 3.6 and clang 3.7 in Ubuntu 15.10 linux
crash at the same standard, with stacktrace leading into
"clang::ASTContext::getSubstTemplateTypeParmType(clang::TemplateTypeParmType
const*, clang::QualType)".

I also tested on OS X Yosemite with the native clang there, and it crashes
there also in the same fashion. Surprisingly, it didn't crash on coliru though.

--------------------

template <typename T, typename... Args>
using member_callback_t = int (T::*)(void *, Args...);

template <typename T, T t> struct dispatch_helper;

template <typename T, typename ... Args, member_callback_t<T, Args...>
target_func>
struct dispatch_helper<member_callback_t<T, Args...>, target_func> {
  static int dispatch(void *) { return 0; }
};

#define DISPATCH(F) &dispatch_helper<decltype(F), (F)>::dispatch

struct test {
  int callback_one(void *, int) { return 0; }
};

typedef int (*desired_sig_t)(void *);

int main() {
  desired_sig_t ptr{DISPATCH(&test::callback_one)};
}

----------

The contents of the .sh file which clang crash handler instructed me to attach
are:

----------

 "/usr/lib/llvm-3.6/bin/clang" "-cc1" "-triple" "x86_64-pc-linux-gnu"
"-emit-obj" "-mrelax-all" "-disable-free" "-disable-llvm-verifier"
"-main-file-name" "main.cpp" "-mrelocation-model" "static" "-mthread-model"
"posix" "-mdisable-fp-elim" "-fmath-errno" "-masm-verbose"
"-mconstructor-aliases" "-munwind-tables" "-fuse-init-array" "-target-cpu"
"x86-64" "-target-linker-version" "2.25.1" "-dwarf-column-info" "-std=c++11"
"-fdeprecated-macro" "-ferror-limit" "19" "-fmessage-length" "80"
"-mstackrealign" "-fobjc-runtime=gcc" "-fcxx-exceptions" "-fexceptions"
"-fdiagnostics-show-option" "-fcolor-diagnostics" "-x" "c++" "main-0e77ae.cpp"

----------

Here's the complete backtrace which the clang crash handler emits:

----------

$ ./build.sh clang++
0  libLLVM-3.6.so.1 0x00007fcd7f11f572 llvm::sys::PrintStackTrace(_IO_FILE*) +
50
1  libLLVM-3.6.so.1 0x00007fcd7f11dcb9
2  libpthread.so.0  0x00007fcd7e1f0d10
3  clang            0x00000000011da4f9
clang::ASTContext::getSubstTemplateTypeParmType(clang::TemplateTypeParmType
const*, clang::QualType) const + 249
4  clang            0x0000000000dc379f
5  clang            0x0000000000dc3f8a
6  clang            0x0000000000dc83d5
7  clang            0x0000000000dce25c
8  clang            0x0000000000dc3050
9  clang            0x0000000000dc3f8a
10 clang            0x0000000000dc410e
11 clang            0x0000000000dc4ec3 clang::Sema::SubstType(clang::QualType,
clang::MultiLevelTemplateArgumentList const&, clang::SourceLocation,
clang::DeclarationName) + 99
12 clang            0x0000000000d93b44
13 clang            0x0000000000db34bc
clang::Sema::DeduceTemplateArguments(clang::ClassTemplatePartialSpecializationDecl*,
clang::TemplateArgumentList const&, clang::sema::TemplateDeductionInfo&) + 636
14 clang            0x0000000000dd9951
clang::Sema::InstantiateClassTemplateSpecialization(clang::SourceLocation,
clang::ClassTemplateSpecializationDecl*, clang::TemplateSpecializationKind,
bool) + 705
15 clang            0x0000000000e00f2e
clang::Sema::RequireCompleteTypeImpl(clang::SourceLocation, clang::QualType,
clang::Sema::TypeDiagnoser&) + 1902
16 clang            0x0000000000e01565
clang::Sema::RequireCompleteType(clang::SourceLocation, clang::QualType,
clang::Sema::TypeDiagnoser&) + 21
17 clang            0x0000000000ae0f8d
clang::Sema::RequireCompleteDeclContext(clang::CXXScopeSpec&,
clang::DeclContext*) + 237
18 clang            0x0000000000b71cac
clang::Sema::getTypeName(clang::IdentifierInfo const&, clang::SourceLocation,
clang::Scope*, clang::CXXScopeSpec*, bool, bool,
clang::OpaquePtr<clang::QualType>, bool, bool, clang::IdentifierInfo**) + 2284
19 clang            0x0000000000a24ba9
clang::Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec(bool, bool,
clang::CXXScopeSpec&, bool) + 297
20 clang            0x0000000000a26afe
clang::Parser::TryAnnotateTypeOrScopeToken(bool, bool) + 158
21 clang            0x0000000000a5b6b0 clang::Parser::ParseCastExpression(bool,
bool, bool&, clang::Parser::TypeCastState) + 816
22 clang            0x0000000000a5e4fd clang::Parser::ParseCastExpression(bool,
bool, clang::Parser::TypeCastState) + 45
23 clang            0x0000000000a5b4d7 clang::Parser::ParseCastExpression(bool,
bool, bool&, clang::Parser::TypeCastState) + 343
24 clang            0x0000000000a5e4fd clang::Parser::ParseCastExpression(bool,
bool, clang::Parser::TypeCastState) + 45
25 clang            0x0000000000a5e58f
clang::Parser::ParseAssignmentExpression(clang::Parser::TypeCastState) + 31
26 clang            0x0000000000a750f4 clang::Parser::ParseBraceInitializer() +
468
27 clang            0x0000000000a31b6d
clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&,
clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) + 2429
28 clang            0x0000000000a40e7a
clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, unsigned int,
clang::SourceLocation*, clang::Parser::ForRangeInit*) + 1898
29 clang            0x0000000000a41d79
clang::Parser::ParseSimpleDeclaration(unsigned int, clang::SourceLocation&,
clang::Parser::ParsedAttributesWithRange&, bool, clang::Parser::ForRangeInit*)
+ 857
30 clang            0x0000000000a42056 clang::Parser::ParseDeclaration(unsigned
int, clang::SourceLocation&, clang::Parser::ParsedAttributesWithRange&) + 214
31 clang            0x0000000000a94437
clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector<clang::Stmt*,
32u>&, bool, clang::SourceLocation*, clang::Parser::ParsedAttributesWithRange&)
+ 3015
32 clang            0x0000000000a9468b
clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector<clang::Stmt*,
32u>&, bool, clang::SourceLocation*) + 139
33 clang            0x0000000000a96f21
clang::Parser::ParseCompoundStatementBody(bool) + 1985
34 clang            0x0000000000a98db6
clang::Parser::ParseFunctionStatementBody(clang::Decl*,
clang::Parser::ParseScope&) + 198
35 clang            0x0000000000a29f09
clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&,
clang::Parser::ParsedTemplateInfo const&, clang::Parser::LateParsedAttrList*) +
1177
36 clang            0x0000000000a413f3
clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, unsigned int,
clang::SourceLocation*, clang::Parser::ForRangeInit*) + 3299
37 clang            0x0000000000a25ab2
clang::Parser::ParseDeclOrFunctionDefInternal(clang::Parser::ParsedAttributesWithRange&,
clang::ParsingDeclSpec&, clang::AccessSpecifier) + 290
38 clang            0x0000000000a26171
39 clang            0x0000000000a2619f
clang::Parser::ParseDeclarationOrFunctionDefinition(clang::Parser::ParsedAttributesWithRange&,
clang::ParsingDeclSpec*, clang::AccessSpecifier) + 31
40 clang            0x0000000000a2b3c3
clang::Parser::ParseExternalDeclaration(clang::Parser::ParsedAttributesWithRange&,
clang::ParsingDeclSpec*) + 291
41 clang            0x0000000000a2bcd8
clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&) + 520
42 clang            0x0000000000a218e3 clang::ParseAST(clang::Sema&, bool,
bool) + 499
43 clang            0x00000000006dc1c6 clang::FrontendAction::Execute() + 118
44 clang            0x00000000006b5669
clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 313
45 clang            0x000000000069c613
clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 1939
46 clang            0x0000000000697ae8 cc1_main(llvm::ArrayRef<char const*>,
char const*, void*) + 1304
47 clang            0x0000000000694b62 main + 962
48 libc.so.6        0x00007fcd7d89da40 __libc_start_main + 240
49 clang            0x00000000006970b9 _start + 41
Stack dump:
0.    Program arguments: /usr/lib/llvm-3.6/bin/clang -cc1 -triple
x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier
-main-file-name main.cpp -mrelocation-model static -mthread-model posix
-mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases
-munwind-tables -fuse-init-array -target-cpu x86-64 -target-linker-version
2.25.1 -dwarf-column-info -resource-dir
/usr/lib/llvm-3.6/bin/../lib/clang/3.6.2 -I src/ -internal-isystem
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.2.1/../../../../include/c++/5.2.1
-internal-isystem
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.2.1/../../../../include/x86_64-linux-gnu/c++/5.2.1
-internal-isystem
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.2.1/../../../../include/x86_64-linux-gnu/c++/5.2.1
-internal-isystem
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.2.1/../../../../include/c++/5.2.1/backward
-internal-isystem /usr/local/include -internal-isystem
/usr/lib/llvm-3.6/bin/../lib/clang/3.6.2/include -internal-externc-isystem
/usr/include/x86_64-linux-gnu -internal-externc-isystem /include
-internal-externc-isystem /usr/include -std=c++11 -fdeprecated-macro
-fdebug-compilation-dir /home/chris/cpp/mock_lua -ferror-limit 19
-fmessage-length 80 -mstackrealign -fobjc-runtime=gcc -fcxx-exceptions
-fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o
/tmp/main-da35f6.o -x c++ src/main.cpp 
1.    src/main.cpp:20:21 <Spelling=src/main.cpp:11:57>: current parser token
'dispatch'
2.    src/main.cpp:19:12: parsing function body 'main'
3.    src/main.cpp:19:12: in compound statement ('{}')
clang: error: unable to execute command: Segmentation fault (core dumped)
clang: error: clang frontend command failed due to signal (use -v to see
invocation)
Ubuntu clang version 3.6.2-1 (tags/RELEASE_362/final) (based on LLVM 3.6.2)
Target: x86_64-pc-linux-gnu
Thread model: posix
clang: note: diagnostic msg: PLEASE submit a bug report to
http://bugs.debian.org/ and include the crash backtrace, preprocessed source,
and associated run script.
clang: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/main-0e77ae.cpp
clang: note: diagnostic msg: /tmp/main-0e77ae.sh
clang: note: diagnostic msg: 

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

----------

This test case seems to be close to minimal -- if the 'dispatch' member
function is changed from a function to e.g. a static class member `int`
variable, then I don't get a crash anymore, and instead a get an error
"implicit instantiation of undefined template". While better than a crash, I
*think* clang is wrong in failing to compile it.

Some things could still be changed to make the test case smaller, like the
macro could be eliminated for instance, but I think it makes the test case
potentially easier to understand so I left it.

- Is this program correct?
- Is there anything I can do to massage the templates used here so that clang
will be happier about them? I was hoping to use the code this was reduced from
in a project, and I need to use be able to use clang to compile that project.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160212/630ec9b9/attachment.html>


More information about the llvm-bugs mailing list