Hi,

I ran into some segfaults when using Postgres that was compiled with LLVM 7. 
According to the backtraces these crashes happened during the call to 
llvm_shutdown, during cleanup after another out of memory condition. It seems 
that calls to LLVMOrcDisposeInstance, can crash (at least on LLVM 7) when LLVM 
is left in bad state. I attached the relevant part of the stacktrace to this 
email.

With the attached patch these segfaults went away. The patch turns 
llvm_shutdown into a no-op whenever the backend is exiting with an error. Based 
on my understanding of the code this should be totally fine. No memory should 
be leaked, since all memory will be cleaned up anyway once the backend exits 
shortly after. The only reason this cleanup code even seems to exist at all is 
to get useful LLVM profiling data. To me it seems be acceptable if the 
profiling data is incorrect/missing when the backend exits with an error.

Jelte

Attachment: 0001-Skip-LLVM-shutdown-when-bad-exit.patch
Description: 0001-Skip-LLVM-shutdown-when-bad-exit.patch

#0  notifyFreed (K=<optimized out>, Obj=..., this=<optimized out>) at 
/usr/src/debug/llvm-7.0.1.src/lib/ExecutionEngine/Orc/OrcCBindingsStack.h:485
#1  operator() (K=<optimized out>, Obj=..., __closure=<optimized out>) at 
/usr/src/debug/llvm-7.0.1.src/lib/ExecutionEngine/Orc/OrcCBindingsStack.h:226
#2  std::_Function_handler<void (unsigned long, llvm::object::ObjectFile 
const&), llvm::OrcCBindingsStack::OrcCBindingsStack(llvm::TargetMachine&, 
std::function<std::unique_ptr<llvm::orc::IndirectStubsManager, 
std::default_delete<llvm::orc::IndirectStubsManager> > ()>)::{lambda(unsigned 
long, llvm::object::ObjectFile const&)#3}>::_M_invoke(std::_Any_data const&, 
unsigned long, llvm::object::ObjectFile const&) (__functor=..., 
__args#0=<optimized out>, __args#1=...) at 
/usr/include/c++/4.8.2/functional:2071
#3  0x00007fa1697e1578 in operator() (__args#1=..., __args#0=<optimized out>, 
this=<optimized out>) at /usr/include/c++/4.8.2/functional:2471
#4  ~ConcreteLinkedObject (this=0x2766920, __in_chrg=<optimized out>) at 
/usr/src/debug/llvm-7.0.1.src/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h:179
#5  
llvm::orc::RTDyldObjectLinkingLayer::ConcreteLinkedObject<std::shared_ptr<llvm::RuntimeDyld::MemoryManager>
 >::~ConcreteLinkedObject (this=0x2766920, __in_chrg=<optimized out>) at 
/usr/src/debug/llvm-7.0.1.src/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h:182
#6  0x00007fa1697e17aa in operator() (this=<optimized out>, __ptr=<optimized 
out>) at /usr/include/c++/4.8.2/bits/unique_ptr.h:67
#7  ~unique_ptr (this=0x27a9848, __in_chrg=<optimized out>) at 
/usr/include/c++/4.8.2/bits/unique_ptr.h:184
#8  ~pair (this=0x27a9840, __in_chrg=<optimized out>) at 
/usr/include/c++/4.8.2/bits/stl_pair.h:96
#9  ~_Rb_tree_node (this=0x27a9820, __in_chrg=<optimized out>) at 
/usr/include/c++/4.8.2/bits/stl_tree.h:131
#10 destroy<std::_Rb_tree_node<std::pair<unsigned long const, 
std::unique_ptr<llvm::orc::RTDyldObjectLinkingLayerBase::LinkedObject> > > > 
(this=<optimized out>, __p=0x27a9820) at 
/usr/include/c++/4.8.2/ext/new_allocator.h:124
#11 _M_destroy_node (this=0x25611b0, __p=0x27a9820) at 
/usr/include/c++/4.8.2/bits/stl_tree.h:421
#12 std::_Rb_tree<unsigned long, std::pair<unsigned long const, 
std::unique_ptr<llvm::orc::RTDyldObjectLinkingLayerBase::LinkedObject, 
std::default_delete<llvm::orc::RTDyldObjectLinkingLayerBase::LinkedObject> > >, 
std::_Select1st<std::pair<unsigned long const, 
std::unique_ptr<llvm::orc::RTDyldObjectLinkingLayerBase::LinkedObject, 
std::default_delete<llvm::orc::RTDyldObjectLinkingLayerBase::LinkedObject> > > 
>, std::less<unsigned long>, std::allocator<std::pair<unsigned long const, 
std::unique_ptr<llvm::orc::RTDyldObjectLinkingLayerBase::LinkedObject, 
std::default_delete<llvm::orc::RTDyldObjectLinkingLayerBase::LinkedObject> > > 
> >::_M_erase (this=this@entry=0x25611b0, __x=0x27a9820) at 
/usr/include/c++/4.8.2/bits/stl_tree.h:1127
#13 0x00007fa1697edc91 in ~_Rb_tree (this=0x25611b0, __in_chrg=<optimized out>) 
at /usr/include/c++/4.8.2/bits/stl_tree.h:671
#14 ~map (this=0x25611b0, __in_chrg=<optimized out>) at 
/usr/include/c++/4.8.2/bits/stl_map.h:96
#15 ~RTDyldObjectLinkingLayer (this=0x25611a8, __in_chrg=<optimized out>) at 
/usr/src/debug/llvm-7.0.1.src/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h:140
#16 llvm::OrcCBindingsStack::~OrcCBindingsStack (this=0x2560380, 
__in_chrg=<optimized out>) at 
/usr/src/debug/llvm-7.0.1.src/lib/ExecutionEngine/Orc/OrcCBindingsStack.h:106
#17 0x00007fa1697edfaa in LLVMOrcDisposeInstance (JITStack=0x2560380) at 
/usr/src/debug/llvm-7.0.1.src/lib/ExecutionEngine/Orc/OrcCBindings.cpp:126
#18 0x00007fa16b7bb1a1 in llvm_shutdown (code=<optimized out>, arg=<optimized 
out>) at llvmjit.c:885
#19 0x000000000076bf75 in shmem_exit (code=code@entry=1) at ipc.c:239
#20 0x000000000076c0a7 in proc_exit_prepare (code=code@entry=1) at ipc.c:194
#21 0x000000000076c128 in proc_exit (code=code@entry=1) at ipc.c:107
#22 0x00000000008ab16c in errfinish (filename=<optimized out>, 
lineno=<optimized out>, funcname=0x7fa16b7ca9f0 
<fatal_llvm_error_handler(void*, std::string const&, bool)::__func__> 
"fatal_llvm_error_handler") at elog.c:578
#23 0x00007fa16b7bc4c3 in fatal_llvm_error_handler(void*, std::string const&, 
bool) () from /usr/pgsql-13/lib/llvmjit.so
#24 0x00007fa168666e85 in llvm::report_fatal_error (Reason=..., 
GenCrashDiag=<optimized out>) at 
/usr/src/debug/llvm-7.0.1.src/lib/Support/ErrorHandling.cpp:108
#25 0x00007fa168666fd8 in llvm::report_fatal_error 
(Reason=Reason@entry=0x7fa16a3b05f0 "Unable to allocate section memory!", 
GenCrashDiag=GenCrashDiag@entry=true) at 
/usr/src/debug/llvm-7.0.1.src/lib/Support/ErrorHandling.cpp:83
#26 0x00007fa1697fcc9f in llvm::RuntimeDyldImpl::emitSection 
(this=this@entry=0x25b6e50, Obj=..., Section=..., IsCode=IsCode@entry=true) at 
/usr/src/debug/llvm-7.0.1.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp:764
#27 0x00007fa1697fe64d in llvm::RuntimeDyldImpl::findOrEmitSection 
(this=this@entry=0x25b6e50, Obj=..., Section=..., IsCode=<optimized out>, 
LocalSections=std::map with 0 elements) at 
/usr/src/debug/llvm-7.0.1.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp:826
#28 0x00007fa1697ff267 in llvm::RuntimeDyldImpl::loadObjectImpl 
(this=this@entry=0x25b6e50, Obj=...) at 
/usr/src/debug/llvm-7.0.1.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp:322
#29 0x00007fa16981140a in llvm::RuntimeDyldELF::loadObject (this=0x25b6e50, 
O=...) at 
/usr/src/debug/llvm-7.0.1.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp:253
#30 0x00007fa1697faf2f in llvm::RuntimeDyld::loadObject (this=0x25bc080, 
Obj=...) at 
/usr/src/debug/llvm-7.0.1.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp:1185
#31 0x00007fa1697ea005 in 
llvm::orc::RTDyldObjectLinkingLayer::ConcreteLinkedObject<std::shared_ptr<llvm::RuntimeDyld::MemoryManager>
 >::finalize (this=0x2766920) at 
/usr/src/debug/llvm-7.0.1.src/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h:195

Reply via email to