C++ plugin thread crashes if main program doesn't have a thread - c++

I have a main program that uses dlopen to load a library that is written in C++ and call a routine.
handle = dlopen(LIBNAME,RTLD_LAZY);
if(handle == NULL){
fprintf(stderr,"Could not open library %s: %s\n",LIBNAME,dlerror());
exit(1);
}
...
startptr = (int (*)(char*,char*,char*))dlsym(handle,"PluginStart");
if (startptr == NULL){
fprintf(stderr,"Could not find start\n");
exit(1);
}
...
int res = (*startptr)(name,sig,descr);
The start code in the plugin creates an instance of a class,
and spins of a thread to do the startup so that the start code can return
to the main program. The thread runs a loop so that the plugin is running independently from the main program.
Manager(){
std::thread mgrThread(&Manager::threadFunc,this);
mgrThread.detach();
};
void Manager::threadFunc(void)
{
//setup stuff that ends in a loop
}
This worked fine when the main program was written in C. I decided to add a map
to the main program so I changed to C++, and the creation of the thread in the plugin now segfaults.
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) where
#0 0x0000000000000000 in ?? ()
#1 0x00007ffff7e740a9 in std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) () from /lib/x86_64-linux-gnu/libstdc++.so.6
#2 0x00007ffff7fac0c7 in std::thread::thread<void (Manager::*)(), Manager*, void> (this=0x7fffffffddc0, __f=
#0x7fffffffddd0: (void (Manager::*)(class Manager * const)) 0x7ffff7face36 <Manager::threadFunc()>)
at /usr/include/c++/9/thread:130
#3 0x00007ffff7fabf76 in Manager::Manager (this=0x555555580500) at Manager.h:13
#4 0x00007ffff7fabc21 in PluginStart (outName=0x555555561080 <name> "", outSig=0x555555561480 <sig> "",
outDesc=0x555555561880 <descr> "") at testPlugin.cpp:35
#5 0x000055555555b678 in main (argc=1, argv=0x7fffffffdf68) at main.cpp:122
Creating a thread in the main program that does a single printf and exits fixes the failure.
void tstFunc(void){
printf("xyzzy\n");
}
...
std::thread tstThread(tstFunc);
tstThread.detach();
...
So apparently the pthread library is can't dynamically load correctly from the plugin if the C++ library is loaded without pthread in the main program?
I was using -Wl,--dynamic-list=./mexports.txt in the link of the main program to make the callbacks the plugin uses visible to the plugin. I changed it to -Wl,--export-dynamic to see if a more generous symbol export worked, but it only works if I create and use a thread in the main program (for both link options).
Any help would be appreciated.
Ubuntu 20.04 LTS, g++ 9.4.0, no c++ std specified in the makefile.

Related

curl crash on linux C++

I have coded a program that works fine on windows, then I had to use it on linux so I coded the needed things for it to work on linux. What I mainly changed was the thread creation function ;
void thread_create(void *(*threadfunc)(void *), void *u)//linux
{
#ifdef linux
pthread_t id;
pthread_create(&id, NULL, threadfunc, u);
#endif
}
void thread_create(void (*threadfunc)(void *), void *u)//windows
{
#ifndef linux
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadfunc, u, 0, NULL);
#endif
}
The program has a lot of threads. I used gdb to see where it crashes, and the crash always occures at the curl_easy_perform function. Note that I am using this function a lot of times before where I know it will crash, the program never crashes before that point. When I am using them before that point, I'm not sending any data with POST/GET methods, I'm only getting informations from pages without sending any data.
So gdb always tells me :
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffdf65c700 (LWP 26488)]
0x00007ffff6fb0360 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) bt
#0 0x00007ffff6fb0360 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x00000000004a1e70 in readmoredata ()
#2 0x00000000004b33d7 in Curl_fillreadbuffer ()
#3 0x00000000004b40fc in Curl_readwrite ()
#4 0x000000000049b3bc in multi_runsingle ()
#5 0x000000000049c275 in curl_multi_perform ()
#6 0x00000000004972c3 in curl_easy_perform ()
I would like to give you something you can make tests on, but I really can't. The program has thousands of lines.

How do I get more information about that error?

An Qt application is crashing and even debugger mode it's all I get:
ASSERT: "!isEmpty()" in file
C:\Qt\Qt5.5.0\5.5\mingw492_32\include\QtCore/qlist.h, line 321
That line in the file points to:
inline void removeLast() { Q_ASSERT(!isEmpty()); erase(--end()); }
But I'd like more information. Like what line exactly in the source code is using it (from search, no direct call to removeLast() done).
Is this possible?
If you run your program in a debugger, it will stop on the assertion and you'll be able to examine the stack trace. For example with this program in GDB :
#include <QList>
int main(int argc,char* argv[])
{
QList<int> my_list;
my_list.append(1);
my_list.pop_back(); // 1
my_list.pop_back(); // 2
return 0;
}
When you run it :
(gdb) r
Starting program: /home/leiaz/tmp/qttest/build/proj
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
ASSERT: "!isEmpty()" in file /usr/include/qt/QtCore/qlist.h, line 321
Program received signal SIGABRT, Aborted.
0x00007ffff61275f8 in raise () from /usr/lib/libc.so.6
The assertion stop the debugger, and you can ask for the stack trace :
(gdb) backtrace
#0 0x00007ffff61275f8 in raise () from /usr/lib/libc.so.6
#1 0x00007ffff6128a7a in abort () from /usr/lib/libc.so.6
#2 0x00007ffff6dc11e1 in QMessageLogger::fatal(char const*, ...) const () from /usr/lib/libQt5Core.so.5
#3 0x00007ffff6dbc34e in qt_assert(char const*, char const*, int) () from /usr/lib/libQt5Core.so.5
#4 0x00000000004060aa in QList<int>::removeLast (this=0x7fffffffe4d0)
at /usr/include/qt/QtCore/qlist.h:321
#5 0x0000000000405de0 in QList<int>::pop_back (this=0x7fffffffe4d0)
at /usr/include/qt/QtCore/qlist.h:337
#6 0x0000000000405ad4 in main (argc=1, argv=0x7fffffffe5d8) at /home/leiaz/tmp/qttest/main.cc:9
You can see removeLast was called by pop_back and my code start at frame 6 :
(gdb) frame 6
#6 0x0000000000405ad4 in main (argc=1, argv=0x7fffffffe5d8) at /home/leiaz/tmp/qttest/main.cc:9
9 my_list.pop_back(); // 2
Here you can examine the values of other variables in that frame.
If you are using Qt Creator see Viewing Call Stack Trace.
Like what line exactly in the source code is using it (from search, no direct call to removeLast() done). Is this possible?
Unfortunately assert() or Q_ASSERT() macros just show that the conditions were wrong, not which code was causing these conditions.
Especially if called many times and/or from many places, assertions aren't very helpful to detect what code actually was causing it.
You can set a conditional breakpoint for the isEmpty() condition, if that's well supported with your debugger.
You can also set a breakpoint in the standard abort() function, if you have access to the debug symbols.
If there's not, and if you have full access to the source code (which you have for a function inlined in a header) you can work around that deficiency. The way I'm usually going, is to change such code temporarily to
void removeLast()
{
if(isEmpty()) { // <<<<<<<<<<< Put an encapsulating if clause here
return; // <<<<<<<<<<<< set breakpoint
}
Q_ASSERT(!isEmpty()); erase(--end());
}
and set a debugger breakpoint. When the breakpoint is hit while running the code from the debugger, I'll examine the call stack to see where this came from.

same piece of C++ code works in g++ 4.6 compiler but crashes with 5.1

The following piece of code works with g++ 4.6 compiler but crashes with segmentation fault when compiled with g++ 5.1 compiler. The variable access gString is causing the segmentation fault.
#define _GLIBCXX_DEBUG 1
#define _GLIBCXX_USE_CXX11_ABI 0
#include<string>
#include<iostream>
#include<vector>
static std::string gString("hello");
static void
__attribute__((constructor))
initialize()
{
gString.assign("hello world");
return;
}
static void
__attribute__((destructor))
finalize()
{
return;
}
int main(int ac, char **av)
{
//std::cerr<<gString;
return 0;
}
GDB output:
Reading symbols from /home/rk/str...done.
(gdb) b initialize
Breakpoint 1 at 0x401419: file str.cc, line 15.
(gdb) r
Starting program: /home/rk/str
Breakpoint 1, initialize() () at str.cc:15
15 gString.assign("hello world");
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
0x00000000004018d6 in std::string::size() const () at /usr/include/c++/5/bits/basic_string.h:3118
3118 { return _M_rep()->_M_length; }
(gdb) bt
#0 0x00000000004018d6 in std::string::size() const () at /usr/include/c++/5/bits/basic_string.h:3118
#1 0x00000000004016ff in std::string::assign(char const*, unsigned long) () at /usr/include/c++/5/bits/basic_string.tcc:706
#2 0x000000000040166e in std::string::assign(char const*) () at /usr/include/c++/5/bits/basic_string.h:3542
#3 0x0000000000401428 in initialize() () at str.cc:15
#4 0x00000000004023dd in __libc_csu_init ()
#5 0x00007ffff71ad700 in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
#6 0x0000000000401289 in _start ()
Why are you using __attribute__((constructor)) in C++ instead of simply a global object with a constructor? Those attributes are useful in C code, but redundant in C++.
The problem is that your constructor runs before the standard iostreams have been initialized, which would not be a problem if you used a global object with a constructor.
You could try adding a priority to your constructor, but I don't think it will help in this case:
__attribute__((constructor(999)))
The runtime error also happens with gcc 4.9.2 (see ideone example).
The problem is related to the iostreams which are not yet initialized. Commenting out the cerr line, and everything works fine
Apparently, it's a known issue.
Edit: Additional remarks
This small workaround seems to work, at least with 4.9: use c stdio instead of iostreams:
fprintf(stderr, "_initialize"); // this works
But I fully agree with Jonathan's suggestion of using a global (singleton ?) object relying solely on well defined standard C++ behaviour, unless you really need the constructor being run exactly at the moment of a dynamic library load.

segfault using SWIG converted code for tcl

I'm having a segmentation fault with my program.
In fact I write a library in C++ and convert it for tcl using SWIG.
The segfault occurs here:
return Tcl_NewIntObj(static_cast< int >(value));
where value=0
the gdb back trace shows:
(gdb) bt
#0 0x000054b6 in ?? ()
#1 0xb6650d5d in SWIG_From_long (value=0) at mntdisplay_wrap.cc:1712
#2 SWIG_From_int (value=0) at mntdisplay_wrap.cc:1722
#3 Testguimnt_Init (interp=0x9714e28) at mntdisplay_wrap.cc:3774
#4 0xb76748fe in Tcl_LoadObjCmd () from /opt/ActiveTcl-8.6/lib/libtcl8.6.so
#5 0xb75d02af in TclNREvalObjv () from /opt/ActiveTcl-8.6/lib/libtcl8.6.so
#6 0xb75d0859 in Tcl_EvalObjv () from /opt/ActiveTcl-8.6/lib/libtcl8.6.so
#7 0xb75d0d99 in TclEvalEx () from /opt/ActiveTcl-8.6/lib/libtcl8.6.so
#8 0xb7670045 in Tcl_FSEvalFileEx () from /opt/ActiveTcl-8.6/lib/libtcl8.6.so
#9 0xb767645f in Tcl_MainEx () from /opt/ActiveTcl-8.6/lib/libtcl8.6.so
#10 0x0804885c in main ()
In the wrapper:
line 1712:
SWIGINTERNINLINE Tcl_Obj*
SWIG_From_long (long value)
{
if (((long) INT_MIN <= value) && (value <= (long) INT_MAX)) {
return Tcl_NewIntObj(static_cast< int >(value)); //1712
} else {
return Tcl_NewLongObj(value);
}
}
1722:
SWIGINTERNINLINE Tcl_Obj *
SWIG_From_int (int value)
{
return SWIG_From_long (value); //1722
}
3774:
SWIG_Tcl_SetConstantObj(interp, "MESSAGE_NEW", SWIG_From_int(static_cast< int >(MESSAGE_NEW)));
where MESSAGE_NEW is defined in a enum and is 0.
Please, if you have any idea, please help me. Thank you!
EDIT:
I found the cause of the problem: it's an linking error.
I created a new thread for this issue:
C++: linked library disappears and gives segfault during execution
I found the problem.
Please see my other post:
C++: linked library disappears and gives segfault during execution
There was an undefined symbol of my library. I defined it and problem solved!
The confusion was, my program crashed in the middle of tcl wrapper functions (where my undefined symbol was not involved at all). I don't really know why but that's it..
Hope it will help others!

segfault when using boost::signal with -D_GLIBCXX_DEBUG compiler flag

I'm building with g++, and yesterday a helpful person on SO told me to compile with the -D_GLIBCXX_DEBUG and -D_GLIBCXX_DEBUG_PEDANTIC flags. I did so, and I spent most of yesterday tweaking my code to conform to these flags. Now it's complaining about my use of boost::signal, and I'm not sure where the problem is.
I have a class Yarl that has a function refresh() that I want to bind to a signal sigRefresh in another class EventHandler:
class Yarl
{
private:
void refresh();
(...)
};
class EventHandler
{
public:
boost::signal<void()> sigRefresh;
(...)
}
Then, in a member function of Yarl, I have this bit of code:
EventHandler eventHandler;
eventHandler.sigRefresh.connect(boost::bind(&Yarl::refresh, this));
Before I started compiling with those flags, this code ran fine. Now that I'm using them, my program segfaults at the second line.
Here's the backtrace from gdb:
#0 0x001eeee6 in __gnu_debug::_Safe_iterator_base::_M_detach_single() ()
from /usr/lib/libstdc++.so.6
#1 0x001f0555 in __gnu_debug::_Safe_sequence_base::_M_detach_all() ()
from /usr/lib/libstdc++.so.6
#2 0x0804e8a3 in ~_Safe_sequence_base (this=0x812cda4,
__in_chrg=<value optimized out>)
at /usr/include/c++/4.4/debug/safe_base.h:180
#3 0x08085af9 in __gnu_debug::_Safe_sequence<std::__debug::vector<boost::signals::trackable const*, std::allocator<boost::signals::trackable const*> > >::~_Safe_sequence() ()
#4 0x08085b44 in std::__debug::vector<boost::signals::trackable const*, std::allocator<boost::signals::trackable const*> >::~vector() ()
#5 0x080873ab in boost::signals::detail::slot_base::data_t::~data_t() ()
#6 0x080873e3 in void boost::checked_delete<boost::signals::detail::slot_base::data_t>(boost::signals::detail::slot_base::data_t*) ()
#7 0x0808802e in boost::detail::sp_counted_impl_p<boost::signals::detail::slot_base::data_t>::dispose() ()
#8 0x08083d04 in boost::detail::sp_counted_base::release (this=0x812ce30)
at /usr/local/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp:145
#9 0x08083d76 in ~shared_count (this=0xbffff358,
__in_chrg=<value optimized out>)
at /usr/local/boost/smart_ptr/detail/shared_count.hpp:217
#10 0x08083f70 in ~shared_ptr (this=0xbffff354,
__in_chrg=<value optimized out>)
at /usr/local/boost/smart_ptr/shared_ptr.hpp:169
#11 0x080847f1 in ~slot_base (this=0xbffff354, __in_chrg=<value optimized out>)
at /usr/local/boost/signals/slot.hpp:27
#12 0x08084829 in ~slot (this=0xbffff354, __in_chrg=<value optimized out>)
at /usr/local/boost/signals/slot.hpp:105
#13 0x0808390f in yarl::Yarl::mainLoop (this=0xbffff3dc) at src/Yarl.cpp:408
#14 0x08083a96 in yarl::Yarl::startGame (this=0xbffff3dc) at src/Yarl.cpp:452
#15 0x08083abe in main () at src/Yarl.cpp:461
Anyone see what I should fix?
EDIT: I have a small sample program that illustrates the problem, as suggested by Daniel Trebbien.
Here's the header file (test.hpp):
#include <boost/bind.hpp>
#include <boost/signal.hpp>
#include <iostream>
#include <tr1/memory>
namespace yarl
{
class Yarl
{
private:
void refresh();
public:
void hookSignal();
};
namespace events
{
class EventHandler
{
public:
boost::signal<void()> sigRefresh;
};
}
}
and here's the implementation:
#include "test.hpp"
using namespace std;
namespace yarl
{
void Yarl::refresh()
{
cout << "in refresh" << endl;
}
void Yarl::hookSignal()
{
events::EventHandler eventHandler;
eventHandler.sigRefresh.connect(boost::bind(&Yarl::refresh, this));
eventHandler.sigRefresh();
}
}
int main()
{
yarl::Yarl y;
y.hookSignal();
}
As before, this sample program works fine when compiled in g++ with only a -g flag, but if I add -D_GLIBCXX_DEBUG and -D_GLIBCXX_DEBUG_PEDANTIC, it segfaults on the eventHandler.sigRefresh.connect line.
I recompiled boost with -D_GLIBCXX_DEBUG and -D_GLIBCXX_DEBUG_PEDANTIC, and it didn't fix the problem, but while it was compiling I noticed it was doing something odd. I compiled with bjam using this command (according to this boost tutorial):
sudo bjam --build-dir=. --toolset=gcc --variant=debug --cxxflags=-D_GLIBCXX_DEBUG,-D_GLIBCXX_DEBUG_PEDANTIC --layout=tagged stage
despite the --variant=debug tag, it was still compiling the release versions. I also didn't see any mention of my debug flags anywhere in the output. Is it possible I compiled it wrong?
Do I have to have differently compiled versions of boost for release code and debug code?
I'm afraid you do. From personal experience, boost is extremely sensitive to changes in compiler flags. A few years ago a free software project I was hacking on had to stop using boost::system and boost::filesystem just because those modules have shared libraries that weren't reliably compiled (by the Linux distributors) with exactly the same flags as our code. The symptoms were just the same - inexplicable crashes on correct code.
Because of this I have to recommend not using any Boost module that ships a shared library. Ever. It's sad.