加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 抛出异常导致OSX 10.11.4 clang上的SIGSEGV

发布时间:2020-12-16 07:16:19 所属栏目:百科 来源:网络整理
导读:给出以下代码: #include stdexcept#include stringusing namespace std;class exception_base : public runtime_error {public: exception_base() : runtime_error(string()) { }};class my_exception : public exception_base {public:};int main() { thro
给出以下代码:

#include <stdexcept>
#include <string>

using namespace std;

class exception_base : public runtime_error {
public:
    exception_base() 
    : runtime_error(string()) { }
};

class my_exception : public exception_base {
public:

};

int main() {
    throw my_exception();
}

这在GNU / Linux和Windows上运行良好,并且在最新更新到版本10.11.4之前用于在OSX上正常工作.罚款我的意思是因为没有捕获异常,所以调用std :: terminate.

但是,在使用clang(LLVM 7.3.0)的OSX 10.11.4上,程序崩溃并出现分段错误.堆栈跟踪没有帮助:

Program received signal SIGSEGV,Segmentation fault.
0x0000000100000ad1 in main () at test.cpp:17
17      throw my_exception();
(gdb) bt
#0  0x0000000100000ad1 in main () at test.cpp:17
(gdb)

valgrind对此也不怎么说:

==6500== Process terminating with default action of signal 11 (SIGSEGV)
==6500==  General Protection Fault
==6500==    at 0x100000AD1: main (test.cpp:17)

我认为代码不会以任何方式违反标准.我在这里错过了什么吗?

请注意,即使我在throw周围添加了try-catch,代码仍会因SIGSEGV而崩溃.

解决方法

如果查看反汇编,您将看到SSE movaps指令上发生了一般保护(GP)异常:

a.out`main:
    0x100000ad0 :   pushq  %rbp
    0x100000ad1 :   movq   %rsp,%rbp
    0x100000ad4 :   subq   $0x20,%rsp
    0x100000ad8 :   movl   $0x0,-0x4(%rbp)
    0x100000adf :  movl   $0x10,%eax
    0x100000ae4 :  movl   %eax,%edi
    0x100000ae6 :  callq  0x100000dea               ; symbol stub for: __cxa_allocate_exception
    0x100000aeb :  movq   %rax,%rdi
    0x100000aee :  xorps  %xmm0,%xmm0
->  0x100000af1 :  movaps %xmm0,(%rax)
    0x100000af4 :  movq   %rdi,-0x20(%rbp)
    0x100000af8 :  movq   %rax,%rdi
    0x100000afb :  callq  0x100000b40               ; my_exception::my_exception
...

在调用my_exception :: my_exception()构造函数之前,使用movaps指令将__cxa_allocate_exception(size_t)返回的内存块清零.但是,此指针(在我的情况下为0x0000000100103498)不保证是16字节对齐.当movaps指令的源操作数或目标操作数是内存操作数时,操作数必须在16字节边界上对齐,否则将生成GP异常.

暂时解决问题的一种方法是在没有SSE指令的情况下编译(-mno-sse).它不是理想的解决方案,因为SSE指令可以提高性能.

我认为这与http://reviews.llvm.org/D18479有关:

07001 made changes to give a higher alignment for exception objects on the grounds that Itanium says _Unwind_Exception should be “double-word” aligned and the structure is normally declared with __attribute__((aligned)) guaranteeing 16-byte alignment. It turns out that libc++abi doesn’t declare the structure with __attribute__((aligned)) and therefore only guarantees 8-byte alignment on 32-bit and 64-bit platforms. This caused a crash in some cases when the backend emitted SIMD store instructions that requires 16-byte alignment (such as movaps).

This patch makes ItaniumCXXABI::getAlignmentOfExnObject return an 8-byte alignment on Darwin to fix the crash.

..哪个补丁在2016年3月31日作为r264998提交.

还有https://llvm.org/bugs/show_bug.cgi?id=24604和https://llvm.org/bugs/show_bug.cgi?id=27208似乎相关.

更新我安装了Xcode 7.3.1(昨天发布),问题似乎已得到解决;生成的程序集现在是:

a.out`main:
    0x100000ac0 :   pushq  %rbp
    0x100000ac1 :   movq   %rsp,%rbp
    0x100000ac4 :   subq   $0x20,%rsp
    0x100000ac8 :   movl   $0x0,-0x4(%rbp)
    0x100000acf :  movl   $0x10,%eax
    0x100000ad4 :  movl   %eax,%edi
    0x100000ad6 :  callq  0x100000dea               ; symbol stub for: __cxa_allocate_exception
    0x100000adb :  movq   %rax,%rdi
    0x100000ade :  movq   $0x0,0x8(%rax)
    0x100000ae6 :  movq   $0x0,(%rax)
    0x100000aed :  movq   %rdi,-0x20(%rbp)
    0x100000af1 :  movq   %rax,%rdi
    0x100000af4 :  callq  0x100000b40               ; my_exception::my_exception
...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读