crash when protocol buffers are used in .so #206

Closed
fkusche opened this Issue Feb 11, 2015 · 4 comments

Comments

Projects
None yet
4 participants

fkusche commented Feb 11, 2015

Consider the following foo.cpp:

#include <dlfcn.h>
#include <stdio.h>
#include "p.pb.h"

int main()
{
    void* soHandle = dlopen( "./bar.so", RTLD_NOW );
    if( !soHandle ) {
        fprintf( stderr, "couldn't load bar.so\n" );
        return 1;
    }
    dlclose( soHandle );
    printf( "shutting down protobuf\n" );
    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}

and bar.cpp:

int bar()
{
    return 0;
}

and p.proto:

package FooCfg;
message Msg {
     required int32 dummy = 2;
}

Compile the proto file: protoc --cpp_out=. p.proto
Compile foo to an executable: g++ -fPIC foo.cpp -ofoo -lprotobuf -ldl
Compile bar to a dll: g++ -fPIC -Wl,--unresolved-symbols=ignore-in-shared-libs -shared -o bar.so bar.cpp p.pb.cc -lprotobuf

running foo will produce a segmentation fault in ShutdownProtobufLibrary():

> gdb ./foo
GNU gdb (Debian 7.7.1+dfsg-5) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./foo...(no debugging symbols found)...done.
(gdb) r
Starting program: /home/pasu/devel/test/foo 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
shutting down protobuf

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6698441 in ?? ()
(gdb) bt
#0  0x00007ffff6698441 in ?? ()
#1  0x00007ffff7af868b in google::protobuf::ShutdownProtobufLibrary() () from /usr/lib/x86_64-linux-gnu/libprotobuf.so.9
#2  0x00000000004008b1 in main ()
(gdb)

Without dlopening bar.so, there will be no segfault.
Also, when p.pb.cc is not compiled into bar.so.

Is this because of global constructors/destructors being executed?
How can this be solved?

@fkusche did you find out what it was? Any work around? I believe a project that I am working on is experiencing a very similar issue.

If we use .so then we see this crash. If we create it as a .a (static) then it doesn't happen.

fkusche commented Aug 28, 2015

@KjellKod sorry for the late answer. No I did not find out anything more, unfortunately.

Member

xfxyjwf commented Jan 20, 2016

As protobuf has used some global variables, linking protobuf dynamically with dlopen across several modules are very problematic. I'm not aware of any good solution here.

@xfxyjwf xfxyjwf closed this Jan 20, 2016

Hello, if I'm correct this is making the vlc build segfault now
https://trac.videolan.org/vlc/ticket/18329

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment