Bug 236344 - [toolchain] gcc-built shared library crashes in static object constructors when dynamically loaded
Summary: [toolchain] gcc-built shared library crashes in static object constructors wh...
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: misc (show other bugs)
Version: 11.2-STABLE
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-toolchain mailing list
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-03-07 04:04 UTC by Yuri Victorovich
Modified: 2020-05-16 12:51 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Yuri Victorovich freebsd_committer 2019-03-07 04:04:12 UTC
Testcase: the port multimedia/webcamoid with added lines:
> WITH_DEBUG=yes
> USE_GCC=yes

When the executable runs, it crashes with this stack while the plugin is dynamically loaded:
> Thread 1 received signal SIGSEGV, Segmentation fault.
> 0x00000008056fed78 in vtable for __cxxabiv1::__si_class_type_info () from /lib/libcxxrt.so.1
> (gdb) bt
> #0  0x00000008056fed78 in vtable for __cxxabiv1::__si_class_type_info () at /lib/libcxxrt.so.1
> #1  0x00000008044ecf86 in __dynamic_cast () at /usr/local/lib/gcc8/libstdc++.so.6
> #2  0x000000080456bdb0 in bool std::has_facet<std::ctype<char> >(std::locale const&) () at /usr/local/lib/gcc8/libstdc++.so.6
> #3  0x000000080455f154 in std::basic_ios<char, std::char_traits<char> >::_M_cache_locale(std::locale const&) () at /usr/local/lib/gcc8/libstdc++.so.6
> #4  0x000000080455f5d0 in std::basic_ios<char, std::char_traits<char> >::init(std::basic_streambuf<char, std::char_traits<char> >*) ()
>     at /usr/local/lib/gcc8/libstdc++.so.6
> #5  0x0000000804500953 in std::ios_base::Init::Init() () at /usr/local/lib/gcc8/libstdc++.so.6
> #6  0x000000082aab4c56 in __static_initialization_and_destruction_0(int, int) (__initialize_p=1, __priority=65535) at /usr/local/lib/gcc8/include/c++/iostream:74
> #7  0x000000082aab4c89 in _GLOBAL__sub_I_ipcbridge.cpp(void) () at src/ipcbridge.cpp:3411
> #8  0x000000080073a7ab in objlist_call_init (list=<optimized out>, lockstate=<optimized out>) at /usr/src/libexec/rtld-elf/rtld.c:2657
> #9  0x000000080073f009 in dlopen_object
>     (name=0x80087cc00 "z\270", <incomplete sequence \325>, fd=<optimized out>, refobj=<optimized out>, lo_flags=10, mode=1, lockstate=0x800000002)
>     at /usr/src/libexec/rtld-elf/rtld.c:3356
> #10 0x000000080073b836 in rtld_dlopen (name=0x80b364e98 "/usr/local/lib/avkys/libVirtualCamera.so", fd=-1, mode=<optimized out>) at /usr/src/libexec/rtld-elf/rtld.c:3231
> #11 0x000000080386a7e5 in  () at /usr/local/lib/qt5/libQt5Core.so.5
> #12 0x00000008038654ea in  () at /usr/local/lib/qt5/libQt5Core.so.5
> #13 0x0000000803865913 in  () at /usr/local/lib/qt5/libQt5Core.so.5
> #14 0x00000008009923b2 in AkElement::createPtr(QString const&, QString const&) (pluginId=..., elementName=...) at src/akelement.cpp:260
> #15 0x0000000800992311 in AkElement::create(QString const&, QString const&) (pluginId=..., elementName=...) at src/akelement.cpp:243
> #16 0x0000000000416753 in MediaTools::MediaTools(QObject*) (this=0x7fffffffe780, parent=0x0) at src/mediatools.cpp:95
> #17 0x0000000000415c00 in main(int, char**) (argc=1, argv=0x7fffffffe808) at src/main.cpp:89
> (gdb) 

The crash occurs during the initialization of this static object:
> 74	  static ios_base::Init __ioinit;

It calls std::ios_base::Init::Init() which has likely been called during the main static constructors invocation, because it generally prints something from the executable.
It looks like the constructor is called twice.
The same code works fine when built with clang.
Comment 1 Fernando Apesteguía freebsd_committer 2020-05-15 15:49:11 UTC
Any updates on this?

cad/openvsp doesn't build with clang anymore due to bug #230888.

It builds with gcc but it then segfaults with similar backtrace:

Program received signal SIGSEGV, Segmentation fault.
0x0000000802efba18 in vtable for __cxxabiv1::__si_class_type_info () from /lib/libcxxrt.so.1
(gdb) bt
#0  0x0000000802efba18 in vtable for __cxxabiv1::__si_class_type_info () from /lib/libcxxrt.so.1
#1  0x0000000801a273e6 in __dynamic_cast () from /usr/local/lib/gcc9/libstdc++.so.6
#2  0x0000000801aa9e8e in bool std::has_facet<std::ctype<char> >(std::locale const&) () from /usr/local/lib/gcc9/libstdc++.so.6
#3  0x0000000801a9bbd4 in std::basic_ios<char, std::char_traits<char> >::_M_cache_locale(std::locale const&) () from /usr/local/lib/gcc9/libstdc++.so.6
#4  0x0000000801a9c070 in std::basic_ios<char, std::char_traits<char> >::init(std::basic_streambuf<char, std::char_traits<char> >*) ()
   from /usr/local/lib/gcc9/libstdc++.so.6
#5  0x0000000801a3b463 in std::ios_base::Init::Init() () from /usr/local/lib/gcc9/libstdc++.so.6
#6  0x000000000049f60d in ?? ()
#7  0x000000080194846e in ?? () from /libexec/ld-elf.so.1
#8  0x00007fffffffc828 in ?? ()
#9  0x0000000801975800 in ?? ()
#10 0x0000000000000000 in ?? ()
Comment 2 Konstantin Belousov freebsd_committer 2020-05-15 16:19:15 UTC
Can you extract the minimal test case ?
Comment 3 Fernando Apesteguía freebsd_committer 2020-05-15 16:22:08 UTC
Yep, it involves a patch for cad/openvsp (to make it build with gcc). Should I open a different PR so I don't hijack this one?
Comment 4 Yuri Victorovich freebsd_committer 2020-05-15 16:38:16 UTC
(In reply to Fernando Apesteguía from comment #3)

Yes, please open a different PR.
Comment 5 Fernando Apesteguía freebsd_committer 2020-05-15 16:54:12 UTC
Done: bug #246488
Comment 6 Dimitry Andric freebsd_committer 2020-05-16 12:51:10 UTC
I think that what Konstantin means with a minimal test case, is not a huge CAD port, but one or two .cpp files which can be independently compiled, to show the issue. That said, this seems like an issue with shared libraries initializing libstdc++'s iostream constructors twice. I guess the vtable pointer shows in the description and comment table is NULL? Did any of the original submitters check that?