[译]C++异常的幕后17:异常类型的发射以及读.gcc_except_table

80 篇文章 0 订阅
80 篇文章 0 订阅

目前为止我们知道在抛出一个异常时,通过阅读本地储存区,即.gcc_except_table,我们可以得到许多反射信息;读这个表我们已经能够实现能决定在抛出一个异常时运行哪个着陆垫的personality函数。我们还知道如何读LSDA的活动表部分,因此我们应该能够修改personality函数在带有多个catch的着陆垫内选择正确的catch语句。

上次我们留下了我们的ABI实现,并把一些时间用于分析.gcc_except_table的汇编来发掘我们如何找出catch可以处理的类型。我们发现这张表的部分确实保存了一个类型列表,可以找到这个消息。让我们尝试在清理阶段读它,但首先让我们回忆LSDA头的定义:

1

2

3

4

5

6

7

struct LSDA_Header {

    uint8_t start_encoding;

    uint8_t type_encoding;

 

    // This is the offset, from the end of the header, to the types table

    uint8_t type_table_offset;

};

最后一个域是新的(对我们):它向我们给出了类型表的一个偏移。让我们回忆一下每个调用点的定义:

1

2

3

4

5

6

7

8

9

10

struct LSDA_CS {

    // Offset into function from which we could handle a throw

    uint8_t start;

    // Length of the block that might throw

    uint8_t len;

    // Landing pad

    uint8_t lp;

    // Offset into action table + 1 (0 means no action)

    uint8_t action;

};

检查最后这个域“action”。这向我们给出活动表的一个偏移。这意味着我们可以对一个特定CS找到该活动。这里的技巧是对存在一个catch的着陆垫,这个活动将保存该类型表的偏移;我们可以使用类型表指针的偏移,这我们可以从头部获得。相当拗口:让我们更好地讨论代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

// Pointer to the beginning of the raw LSDA

LSDA_ptr lsda = (uint8_t*)_Unwind_GetLanguageSpecificData(context);

 

// Read LSDA headerfor the LSDA

LSDA_Header header(&lsda);

 

const LSDA_ptr types_table_start = lsda + header.type_table_offset;

 

// Read the LSDA CS header

LSDA_CS_Header cs_header(&lsda);

 

// Calculate where the end of the LSDA CS table is

const LSDA_ptr lsda_cs_table_end = lsda + cs_header.length;

 

// Get the start of action tables

const LSDA_ptr action_tbl_start = lsda_cs_table_end;

 

// Get the first call site

LSDA_CS cs(&lsda);

 

// cs.action is the offset + 1; that way cs.action == 0

// means there is no associated entry in the action table

const size_t action_offset = cs.action - 1;

const LSDA_ptr action = action_tbl_start + action_offset;

 

// For a landing pad with a catch the action table will

// hold an index to a list of types

int type_index = action[0];

 

// types_table_start actually points to the end of the table, so

// we need to invert the type_index. There we'll find a ptr to

// the std::type_info for the specification in our catch

const void* catch_type_info = types_table_start[ -1 * type_index ];

const std::type_info *catch_ti = (const std::type_info *) catch_type_info;

 

// If everything went OK, this should print something like Fake_Exception

printf("%s\n", catch_ti->name());

代码看起来复杂,因为在到达结构体type_info之前有几层间接层,不过这并不复杂:它只是读我们在汇编里找到的.gcc_except_table。

打印类型名是正确方向上前进的一大步。同样,我们的personality函数变得有点混乱。读LSDA的大部分复杂性可以隐藏在底下,几乎没有代价。

下次我们将看看是否能把新发现的类型匹配到原有的异常。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值