Reputation: 3325
I'm having a problem with a shared library provided by one of our partners and narrowed it down to this test case:
My own test library compiled to libCrashTestLib.dylib
:
#include <exception>
#include <iostream>
extern "C" void Test() {
try {
throw std::exception{};
} catch (const std::exception& e) { }
std::cout << "Success" << std::endl;
}
My main executable loads my library and calls the Test
function:
#include <string>
#include <dlfcn.h>
int main(int argc, char** argv) {
std::string lib_path = "/path/to/libCrashTestLib.dylib";
void* handle = dlopen(lib_path.c_str(), RTLD_NOW | RTLD_GLOBAL);
void (*test_pointer)() = reinterpret_cast<void(*)()>(dlsym(handle, "Test"));
test_pointer();
dlclose(handle);
}
This works fine and prints Success
.
However, when I just link (not even call) the library provided by our partner it gives me an EXC_BAD_ACCESS
where I throw the exception.
What I'm looking for is either of two things:
For completeness, here is the CMakeLists.txt
I'm using:
cmake_minimum_required(VERSION 3.0)
project(CrashTest CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED on)
find_library(VendorApi NAMES vendorapi PATHS ${CMAKE_SOURCE_DIR})
add_library(CrashTestLib SHARED lib.cpp)
# Uncomment this line to make it crash:
#target_link_libraries(CrashTestLib VendorApi)
add_executable(CrashTestMain main.cpp)
Upvotes: 1
Views: 721
Reputation: 94654
The most likely reason for the crash is that the vendor's library is compiled with libstdc++
, while your code is compliled with libc++
.
I can trigger a crash quite easily by issuing a cout while constructing a class as part of the library load-time e.g.
#include <exception>
#include <iostream>
#include <string>
class bongo {
public:
bongo() {
std::cout << "Log something" << std::endl;
}
};
static class bongo *boing;
void __attribute__((constructor)) start_something(void) {
boing = new bongo;
}
extern "C" void Test() {
try {
throw std::exception{};
} catch (const std::exception& e) { }
std::cout << "Success" << std::endl;
}
loader code:
#include <string>
#include <dlfcn.h>
int main(int argc, char** argv) {
std::string lib_path = "./libCrashTestLib.dylib";
void* handle = dlopen(lib_path.c_str(), RTLD_NOW | RTLD_GLOBAL);
void (*test_pointer)() = reinterpret_cast<void(*)()>(dlsym(handle, "Test"));
test_pointer();
dlclose(handle);
}
Makefile:
CXXFLAGS += -std=c++11
STDLIB = -stdlib=libstdc++
all: libCrashTestLib.dylib testLib
libCrashTestLib.dylib: CrashTest.cpp
$(CXX) $(CXXFLAGS) $(STDLIB) -shared -o $@ $<
testLib: testLib.cpp
$(CXX) $(CXXFLAGS) -o $@ $<
clean:
rm -f *.dylib testLib
Invocation:
$ make clean && make STDLIB= && ./testLib
rm -f *.dylib testLib
c++ -std=c++11 -shared -o libCrashTestLib.dylib CrashTest.cpp
c++ -std=c++11 -o testLib testLib.cpp
Log something
Success
Failing invocation:
$ make clean && make && ./testLib
rm -f *.dylib testLib
c++ -std=c++11 -stdlib=libstdc++ -shared -o libCrashTestLib.dylib CrashTest.cpp
c++ -std=c++11 -o testLib testLib.cpp
Segmentation fault: 11
You can check what version of the C++ library is linked to the binary using otool -L
:
$ otool -L libCrashTestLib.dylib
libCrashTestLib.dylib:
libCrashTestLib.dylib (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 104.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
Upvotes: 2