[crash] cxa_pure_virtual 崩溃分析与原理

2023-11-23 08:12

本文主要是介绍[crash] cxa_pure_virtual 崩溃分析与原理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  摘要:工作过程中处理线上的崩溃时发现了一例cxa_pure_virtual相关的crash,直接看堆栈基本山很容易确认是有异步调用导致出发了ABI的异常。但是对于为什么会触发cxa_pure_virtual虽然有大致的猜测但是没有直接的证据,因此本文主要描述触发该类型崩溃的原理。
  关键字:cxxabi,llvm,cxa_pure_virtual,vptr

  首先我们看一下崩溃的现象,线上的崩溃堆栈大概类似于下面形式:

0x********* abort()
0x********* std::terminate()
0x********* cxxabi::__cxa_pure_virtual()
0x********* ******::*******

  上面的崩溃我们看实际的代码基本上能够判断出当前类已经被析构的情况下当前类却尝试访问虚函数导致了cxa_pure_virtual,要修复该问题直接排查哪里导致的异步调用即可。但是为了更加输入的理解,我这边查阅了一些资料,如下。  摘要:工作过程中处理线上的崩溃时发现了一例cxa_pure_virtual相关的crash,直接看堆栈基本山很容易确认是有异步调用导致出发了ABI的异常。但是对于为什么会触发cxa_pure_virtual虽然有大致的猜测但是没有直接的证据,因此本文主要描述触发该类型崩溃的原理。
  关键字:cxxabi,llvm,cxa_pure_virtual,vptr

  首先我们看一下崩溃的现象,线上的崩溃堆栈大概类似于下面形式:

0x********* abort()
0x********* std::terminate()
0x********* cxxabi::__cxa_pure_virtual()
0x********* ******::*******

  上面的崩溃我们看实际的代码基本上能够判断出当前类已经被析构的情况下当前类却尝试访问虚函数导致了cxa_pure_virtual,要修复该问题直接排查哪里导致的异步调用即可。

  __cxa_pure_virtual的描述如下:

The __cxa_pure_virtual function is an error handler that is invoked when a pure virtual function is called.
If you are writing a C++ application that has pure virtual functions you must supply your own __cxa_pure_virtual error handler function.

  当调用一个纯虚函数时被调用,看llvm中cxxabi的实现可以看到该函数被调用时会直接abort。那就比较奇怪,如果我们调用的是一个纯虚函数按理说编译都无法通过,但是查看代码发现对应的函数是被重写的。那我们此时可能怀疑的一个点便是,虚基类的虚函数表构造和销毁问题。可能是因为子类被销毁是基类的虚函数表被改回基类的虚函数表,而基类中对应虚函数指针就是编译器指定的cxa_pure_virtual

_LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void __cxa_pure_virtual(void) {abort_message("Pure virtual function called!");
}

  怀疑到这一点,我这边开始找资料(类似的问题印象中标准中是不管的,那大概率在ABI中定义的,那我们去看ABI的定义)。从ABI的定义中找到如下的描述:

An implementation shall provide a standard entry point that a compiler may reference in virtual tables to indicate a pure virtual function. Its interface is:extern "C" void __cxa_pure_virtual ();
This routine will only be called if the user calls a non-overridden pure virtual function, which has undefined behavior according to the C++ Standard. Therefore, this ABI does not specify its behavior, but it is expected that it will terminate the program, possibly with an error message.if C::f is a pure virtual function, no specific requirement is made for the corresponding virtual table entry. It may point to __cxa_pure_virtual (see 3.2.6 Pure Virtual Function API) or to a wrapper function for __cxa_pure_virtual (e.g., to adapt the calling convention). It may also simply be null in such cases.

  上面这一段描述了cxa_pure_virtual实际的意义。下面再看一下CXXABI中关于对象以及虚函数表构造的过程的描述:

     // Sub-VTT for D (embedded in VTT for its derived class X):static vtable *__VTT__1D [1+n+m] ={ D primary vtable,// The sub-VTT for B-in-D in X may have further structure:B-in-D sub-VTT (n elements),// The secondary virtual pointers for D's bases have elements// corresponding to those in the B-in-D sub-VTT,// and possibly others for virtual bases of D:D secondary virtual pointer for B and bases (m elements) }; D ( D *this, vtable **ctorvtbls ){// (The following will be unwound, not a real loop):for ( each base A of D ) {// A "boring" base is one that does not need a ctorvtbl:if ( ! boring(A) ) {// Call subobject constructors with sub-VTT index// if the base needs it -- only B in our example:A ( (A*)this, ctorvtbls + sub-VTT-index(A) ); } else {// Otherwise, just invoke the complete-object constructor:A ( (A*)this );}}// Initialize virtual pointer with primary ctorvtbls address// (first element):this->vptr = ctorvtbls+0;	// primary virtual pointer// (The following will be unwound, not a real loop):for ( each subobject A of D ) {// Initialize virtual pointers of subobjects with ctorvtbls// addresses for the bases if ( ! boring(A) ) {((A*)this)->vptr = ctorvtbls + 1+n + secondary-vptr-index(A);// where n is the number of elements in the sub-VTTs} else {// Otherwise, just use the complete-object vtable:((A *)this)->vptr = &(A-in-D vtable);}}// Code for D constructor....}

  从上面的描述中我们能够看到:

  1. 当前类的虚函数表指针的确定是在执行具体的构造函数代码之前的;
  2. 构建当前类之前会搜索当前类的继承图,找到基类按照继承图的先序序列构造基类;
  3. 基类构造完成后开始调用当前类的构造函数的代码。

  析构函数的顺序相反。对于一个具有直接继承关系的虚基类A和B(B继承自A)的构造顺序为:

class A{
public:virtual void func() = 0;
};class B: public A{
public:virtual void func(){}
};
  1. B构造函数B::B被调用;
  2. 遍历B的基类构造调用基类的构造函数,这里就是A::A();
  3. 调用A的时候先将vfptr指向A的虚函数表,此表项中有基类偏移,typeinfo,__cxa_pure_virtual(因为func是纯虚函数因此该处的虚函数表指针以此填充);
  4. 调用A::A的用户代码,这里没有就不调用;
  5. A构造函数执行完后开始设置B的虚函数指针为B的虚函数表。

  析构顺序:

  1. 调用B::~B析构函数;
  2. 设置虚函数表指针为B的虚函数表;
  3. 执行B析构的用户代码;
  4. 调用基类A::~A(),该过程中先设置虚函数表指针为A的虚函数表再调用A的用户代码。

  从上面的过程中大概也能看出cxa_pure_virtual可能被调用的时机。当类被析构时,基类的析构稍微比较耗时时,第二个线程尝试访问当前类的一个被重写的纯虚函数,由于此时的虚函数表中的纯虚函数已经被修改为cxa_pure_virtual就会直接abort。那我们复现下:

class ClassA {
public:ClassA() {printf("Class A \n");}virtual ~ClassA() {std::this_thread::sleep_for(std::chrono::seconds(5));}virtual void func() = 0;
};class ClassB : public ClassA {
public:virtual ~ClassB() {printf("Class B \n");};virtual void func() override {printf("Class B func\n");}
};void func(ClassA *p) {while (1) {p->func();}
}int main(){std::cout << "Hello World!\n";ClassA* p = new ClassB;auto t = std::thread(func, p);std::this_thread::sleep_for(std::chrono::seconds(1));delete p;t.join();
}

  上面的代码中在析构函数中加了sleep函数来保证对象被析构过程中卡在基类的析构函数,第二个线程尝试访问该纯虚函数。
  再clang/gcc系列编译器上触发的是cxa_purer_virtual,而msvc触发的是_purecall


extern "C" int __cdecl _purecall()
{_purecall_handler const purecall_handler = _get_purecall_handler();if (purecall_handler){purecall_handler();// The user-registered purecall handler should not return, but if it does,// continue with the default termination behavior.}abort();
}

  __cxa_pure_virtual的描述如下:

The __cxa_pure_virtual function is an error handler that is invoked when a pure virtual function is called.
If you are writing a C++ application that has pure virtual functions you must supply your own __cxa_pure_virtual error handler function.

  当调用一个纯虚函数时被调用,看llvm中cxxabi的实现可以看到该函数被调用时会直接abort。那就比较奇怪,如果我们调用的是一个纯虚函数按理说编译都无法通过,但是查看代码发现对应的函数是被重写的。那我们此时可能怀疑的一个点便是,虚基类的虚函数表构造和销毁问题。可能是因为子类被销毁是基类的虚函数表被改回基类的虚函数表,而基类中对应虚函数指针就是编译器指定的cxa_pure_virtual

_LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void __cxa_pure_virtual(void) {abort_message("Pure virtual function called!");
}

  怀疑到这一点,我这边开始找资料(类似的问题印象中标准中是不管的,那大概率在ABI中定义的,那我们去看ABI的定义)。从ABI的定义中找到如下的描述:

An implementation shall provide a standard entry point that a compiler may reference in virtual tables to indicate a pure virtual function. Its interface is:extern "C" void __cxa_pure_virtual ();
This routine will only be called if the user calls a non-overridden pure virtual function, which has undefined behavior according to the C++ Standard. Therefore, this ABI does not specify its behavior, but it is expected that it will terminate the program, possibly with an error message.if C::f is a pure virtual function, no specific requirement is made for the corresponding virtual table entry. It may point to __cxa_pure_virtual (see 3.2.6 Pure Virtual Function API) or to a wrapper function for __cxa_pure_virtual (e.g., to adapt the calling convention). It may also simply be null in such cases.

  上面这一段描述了cxa_pure_virtual实际的意义。下面再看一下CXXABI中关于对象以及虚函数表构造的过程的描述:

     // Sub-VTT for D (embedded in VTT for its derived class X):static vtable *__VTT__1D [1+n+m] ={ D primary vtable,// The sub-VTT for B-in-D in X may have further structure:B-in-D sub-VTT (n elements),// The secondary virtual pointers for D's bases have elements// corresponding to those in the B-in-D sub-VTT,// and possibly others for virtual bases of D:D secondary virtual pointer for B and bases (m elements) }; D ( D *this, vtable **ctorvtbls ){// (The following will be unwound, not a real loop):for ( each base A of D ) {// A "boring" base is one that does not need a ctorvtbl:if ( ! boring(A) ) {// Call subobject constructors with sub-VTT index// if the base needs it -- only B in our example:A ( (A*)this, ctorvtbls + sub-VTT-index(A) ); } else {// Otherwise, just invoke the complete-object constructor:A ( (A*)this );}}// Initialize virtual pointer with primary ctorvtbls address// (first element):this->vptr = ctorvtbls+0;	// primary virtual pointer// (The following will be unwound, not a real loop):for ( each subobject A of D ) {// Initialize virtual pointers of subobjects with ctorvtbls// addresses for the bases if ( ! boring(A) ) {((A*)this)->vptr = ctorvtbls + 1+n + secondary-vptr-index(A);// where n is the number of elements in the sub-VTTs} else {// Otherwise, just use the complete-object vtable:((A *)this)->vptr = &(A-in-D vtable);}}// Code for D constructor....}

  从上面的描述中我们能够看到:

  1. 当前类的虚函数表指针的确定是在执行具体的构造函数代码之前的;
  2. 构建当前类之前会搜索当前类的继承图,找到基类按照继承图的先序序列构造基类;
  3. 基类构造完成后开始调用当前类的构造函数的代码。

  析构函数的顺序相反。对于一个具有直接继承关系的虚基类A和B(B继承自A)的构造顺序为:

class A{
public:virtual void func() = 0;
};class B: public A{
public:virtual void func(){}
};
  1. B构造函数B::B被调用;
  2. 遍历B的基类构造调用基类的构造函数,这里就是A::A();
  3. 调用A的时候先将vfptr指向A的虚函数表,此表项中有基类偏移,typeinfo,__cxa_pure_virtual(因为func是纯虚函数因此该处的虚函数表指针以此填充);
  4. 调用A::A的用户代码,这里没有就不调用;
  5. A构造函数执行完后开始设置B的虚函数指针为B的虚函数表。

  析构顺序:

  1. 调用B::~B析构函数;
  2. 设置虚函数表指针为B的虚函数表;
  3. 执行B析构的用户代码;
  4. 调用基类A::~A(),该过程中先设置虚函数表指针为A的虚函数表再调用A的用户代码。

  从上面的过程中大概也能看出cxa_pure_virtual可能被调用的时机。当类被析构时,基类的析构稍微比较耗时时,第二个线程尝试访问当前类的一个被重写的纯虚函数,由于此时的虚函数表中的纯虚函数已经被修改为cxa_pure_virtual就会直接abort。那我们复现下:

class ClassA {
public:ClassA() {printf("Class A \n");}virtual ~ClassA() {std::this_thread::sleep_for(std::chrono::seconds(5));}virtual void func() = 0;
};class ClassB : public ClassA {
public:virtual ~ClassB() {printf("Class B \n");};virtual void func() override {printf("Class B func\n");}
};void func(ClassA *p) {while (1) {p->func();}
}int main(){std::cout << "Hello World!\n";ClassA* p = new ClassB;auto t = std::thread(func, p);std::this_thread::sleep_for(std::chrono::seconds(1));delete p;t.join();
}

  上面的代码中在析构函数中加了sleep函数来保证对象被析构过程中卡在基类的析构函数,第二个线程尝试访问该纯虚函数。
  再clang/gcc系列编译器上触发的是cxa_purer_virtual,而msvc触发的是_purecall


extern "C" int __cdecl _purecall()
{_purecall_handler const purecall_handler = _get_purecall_handler();if (purecall_handler){purecall_handler();// The user-registered purecall handler should not return, but if it does,// continue with the default termination behavior.}abort();
}

这篇关于[crash] cxa_pure_virtual 崩溃分析与原理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/416556

相关文章

Spring框架中@Lazy延迟加载原理和使用详解

《Spring框架中@Lazy延迟加载原理和使用详解》:本文主要介绍Spring框架中@Lazy延迟加载原理和使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、@Lazy延迟加载原理1.延迟加载原理1.1 @Lazy三种配置方法1.2 @Component

spring IOC的理解之原理和实现过程

《springIOC的理解之原理和实现过程》:本文主要介绍springIOC的理解之原理和实现过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、IoC 核心概念二、核心原理1. 容器架构2. 核心组件3. 工作流程三、关键实现机制1. Bean生命周期2.

Redis实现分布式锁全解析之从原理到实践过程

《Redis实现分布式锁全解析之从原理到实践过程》:本文主要介绍Redis实现分布式锁全解析之从原理到实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、背景介绍二、解决方案(一)使用 SETNX 命令(二)设置锁的过期时间(三)解决锁的误删问题(四)Re

慢sql提前分析预警和动态sql替换-Mybatis-SQL

《慢sql提前分析预警和动态sql替换-Mybatis-SQL》为防止慢SQL问题而开发的MyBatis组件,该组件能够在开发、测试阶段自动分析SQL语句,并在出现慢SQL问题时通过Ducc配置实现动... 目录背景解决思路开源方案调研设计方案详细设计使用方法1、引入依赖jar包2、配置组件XML3、核心配

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

Golang HashMap实现原理解析

《GolangHashMap实现原理解析》HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持高效的插入、查找和删除操作,:本文主要介绍GolangH... 目录HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持

Java程序进程起来了但是不打印日志的原因分析

《Java程序进程起来了但是不打印日志的原因分析》:本文主要介绍Java程序进程起来了但是不打印日志的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java程序进程起来了但是不打印日志的原因1、日志配置问题2、日志文件权限问题3、日志文件路径问题4、程序