compiler-research / CppInterOp

A Clang-based C++ Interoperability Library
Other
43 stars 21 forks source link

Segmentation fault when calling InstantiateTemplate #36

Open vgvassilev opened 1 year ago

vgvassilev commented 1 year ago

We instantiate a template named T2 from test test_advancedcpp.py::test04_template_types with a vector of template arguments of 1 element which is a nullptr.

#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  0x00007ffff7dd9859 in __GI_abort () at abort.c:79
#2  0x00007ffff7dd9729 in __assert_fail_base (fmt=0x7ffff7f6f588 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", 
    assertion=0x7fffef055768 "!isNull() && \"Cannot retrieve a NULL type pointer\"", 
    file=0x7fffef055700 "/home/vvassilev/workspace/builds/scratch/cling-build/cling-src/tools/clang/include/clang/AST/Type.h", line=684, function=<optimized out>)
    at assert.c:92
#3  0x00007ffff7deaf36 in __GI___assert_fail (assertion=0x7fffef055768 "!isNull() && \"Cannot retrieve a NULL type pointer\"", 
    file=0x7fffef055700 "/home/vvassilev/workspace/builds/scratch/cling-build/cling-src/tools/clang/include/clang/AST/Type.h", line=684, 
    function=0x7fffef0556b0 "const clang::ExtQualsTypeCommonBase* clang::QualType::getCommonPtr() const") at assert.c:101
#4  0x00007fffe960ab66 in clang::QualType::getCommonPtr (this=0x7fffffff6e50)
    at /home/vvassilev/workspace/builds/scratch/cling-build/cling-src/tools/clang/include/clang/AST/Type.h:684
#5  0x00007fffe960c108 in clang::QualType::getTypePtr (this=0x7fffffff6e50)
    at /home/vvassilev/workspace/builds/scratch/cling-build/cling-src/tools/clang/include/clang/AST/Type.h:6430
#6  0x00007fffe960acde in clang::QualType::operator-> (this=0x7fffffff6e50)
    at /home/vvassilev/workspace/builds/scratch/cling-build/cling-src/tools/clang/include/clang/AST/Type.h:726
#7  0x00007fffe9611fc0 in clang::TypeLoc::getTypeLocClass (this=0x7fffffff6e70)
    at /home/vvassilev/workspace/builds/scratch/cling-build/cling-src/tools/clang/include/clang/AST/TypeLoc.h:117
#8  0x00007fffeb6949d7 in clang::TypeLoc::initializeImpl (Context=..., TL=..., Loc=...)
    at /home/vvassilev/workspace/builds/scratch/cling-build/cling-src/tools/clang/lib/AST/TypeLoc.cpp:134
#9  0x00007fffeae3eb39 in clang::TypeLoc::initialize (this=0x7fffffff6f20, Context=..., Loc=...)
    at /home/vvassilev/workspace/builds/scratch/cling-build/cling-src/tools/clang/include/clang/AST/TypeLoc.h:197
#10 0x00007fffeb043652 in clang::ASTContext::getTrivialTypeSourceInfo (this=0xfb83b0, T=..., L=...)
    at /home/vvassilev/workspace/builds/scratch/cling-build/cling-src/tools/clang/lib/AST/ASTContext.cpp:2908
#11 0x00007fffe5dfd887 in InterOp::InstantiateTemplate (ClassDecl=0x2ef63f8, TemplateArgs=..., S=...)
    at /home/vvassilev/workspace/builds/scratch/cppyy/InterOp/lib/Interpreter/InterOp.cpp:2105
#12 0x00007fffe5dfda41 in InterOp::InstantiateClassTemplate (interp=0xbe1e90, tmpl=0x2ef63f8, types=0x3005070, type_size=1)
    at /home/vvassilev/workspace/builds/scratch/cppyy/InterOp/lib/Interpreter/InterOp.cpp:2125
#13 0x00007fffe5df2351 in Cppyy::InstantiateTemplateClass (tmpl=0x2ef63f8, types=0x3005070, types_size=1)
    at /home/vvassilev/workspace/builds/scratch/cppyy/cppyy-backend/src/clingwrapper.cxx:1888
#14 0x00007fffe5955323 in (anonymous namespace)::MakeCppTemplateClass (args=0x7fffe54d5ec8)
    at /home/vvassilev/workspace/builds/scratch/cppyy/CPyCppyy/src/CPyCppyyModule.cxx:438
aaronj0 commented 3 months ago

Minimal reproducable example:

template class T1<int>;
template class T2<T1<int> >;

and calling cppyy.gbl.T2

vgvassilev commented 3 months ago

Do you mean:

template <typename T> class T1{};
template class T1<int>;

template class T1<int>; is a class ClassTemplateSpecializationDecl you can't instantiate it. I guess the calling code needs to be protected for this case?