抛出异常的EXC_BAD_ACCESS

时间:2016-05-12 17:08:41

标签: c++ xcode macos

我的某个合作伙伴提供的共享库存在问题,并将其缩小到此测试用例:

我的拥有测试库已编译为 libCrashTestLib.dylib

#include <exception>
#include <iostream>

extern "C" void Test() {
  try {
    throw std::exception{};
  } catch (const std::exception& e) { }
  std::cout << "Success" << std::endl;
}

我的主要可执行文件加载我的库并调用Test函数:

#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);
}

这样可以正常工作并打印Success

但是,当我只是链接(甚至没有调用)我们的合作伙伴提供的库时,它会给我一个EXC_BAD_ACCESS,我抛出异常。

我正在寻找的是两件事:

  • 帮助我摆脱这种行为的编译器或链接器开关。
  • 一种创建我自己的库的方法,当我链接它时会崩溃这个程序,这样我就可以给我们的合作伙伴更多详细信息来修改它。

为了完整起见,我在CMakeLists.txt使用:

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)

1 个答案:

答案 0 :(得分:2)

崩溃的最可能原因是供应商的库使用libstdc++进行编译,而您的代码则使用libc++进行编译。

我可以通过在构建类作为库加载时间的一部分时发出一个cout来轻松地触发崩溃,例如。

#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;
}

加载程序代码:

#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);
}

生成文件:

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

调用:

$ 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

调用失败:

$ 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

您可以使用otool -L检查哪个版本的C ++库链接到二进制文件:

$ 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)
相关问题