setjmp和longjmp的实现原理与应用

在讨论setjmp的实现原理之前,我们先看一个setjmp和longjmp的例子:
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>

static jmp_buf jmpbuf_1;

int g_a = 0;

void test


int main



ret = setjmp;

if

else if

else if


test;

return 0;
}


下面是测试代码在不同的编译优化条件下的执行结果:
wangyao@wangyao-laptop:~/Test/sys$ gcc longjmp.c -O2
wangyao@wangyao-laptop:~/Test/sys$ ./a.out 1
Orig setjmp
Return From longjmp 1
Global_a: 1111
Local_a: 1
wangyao@wangyao-laptop:~/Test/sys$ ./a.out 2
Orig setjmp
Return From longjmp 2
Global_a: 1111
Local_a: 1
wangyao@wangyao-laptop:~/Test/sys$ gcc longjmp.c
wangyao@wangyao-laptop:~/Test/sys$ ./a.out 1
Orig setjmp
Return From longjmp 1
Global_a: 1111
Local_a: 2222
wangyao@wangyao-laptop:~/Test/sys$ ./a.out 2
Orig setjmp
Return From longjmp 2
Global_a: 1111
Local_a: 2222

man 3 longjmp中提到:
The values of automatic variables are unspecified after a call to longjmp if they meet all the following criteria:
  they are local to the function that made the corresponding setjmp call;
  their values are changed between the calls to setjmp and longjmp; and
  they are not declared as volatile.

posix中没有定义自动变量在调用longjmp后的动作。

在调用longjmp后,自动变量、全局变脸、寄存器变量、静态变量和易失变量的变化是不同的。全局变量、静态变量和易失变量的分配要么是在data段,要么是在bss段中,上下文切换对它们基本上没有什么影响;但是对于栈和寄存器来讲,上下文切换就有很大的影响。因此对于一些自动变量、寄存器变量就可能会在longjmp的时候发生变化,当然这个还与编译时候的优化条件有关。


下面再来讨论setjmp和longjmp的实现原理,这里我们只关注x86架构的实现,具体来讲是i386的实现。

首先,回想一下x86架构下面,函数调用过程栈的变化情况:
在x86架构下,调用函数的时候,首先自右向左将压栈,(早期的gcc版本是采用push $变量的方法;后期gcc使用的是sub $num,esp 然后mov $变量,esp+$num的方法)
接下来就是call func了,call func的动作是,将下一指令地址压栈,jmp到func的地址执行。
func刚开始就执行一个push ebp,将原先的栈基址保存下来。

setjmp具体的实现在:
glibc-2.7/sysdeps/i386/setjmp.S
将寄存器中的值保存到jmp_buf中。
ENTRY )
ENTER

movl JMPBUF, %eax
CHECK_BOUNDS_BOTH_WIDE , $JB_SIZE)

/ Save registers. /
movl %ebx,
movl %esi,
movl %edi,
leal JMPBUF, %ecx / Save SP as it will be after we return. /
#ifdef PTR_MANGLE
PTR_MANGLE
#endif
movl %ecx,
movl PCOFF, %ecx / Save PC we are returning to now. /
#ifdef PTR_MANGLE
PTR_MANGLE
#endif
movl %ecx,
LEAVE / pop frame pointer to prepare for tail-call. /
movl %ebp,  / Save caller‘s frame pointer. /

#if defined NOT_IN_libc && defined IS_IN_rtld
/ In ld.so we never save the signal mask. /
xorl %eax, %eax
ret
#else
/ Make a tail call to __sigjmp_save; it takes the same args. /
jmp __sigjmp_save
#endif


longjmp的具体实现在:
glibc-2.7/sysdeps/i386/__longjmp.S
将jmp_buf中的值恢复到相应的寄存器中,并将longjmp的第2个参数作为返回值返回,由于longjmp中将寄存器的eip设置回setjmp时候的值。longjmp的返回值在程序逻辑上就是setjmp的返回值了。
#ifdef PTR_DEMANGLE
movl JBUF, %eax / User‘s jmp_buf in %eax. /
CHECK_BOUNDS_BOTH_WIDE , $JB_SIZE)

/ Save the return address now. /
movl , %edx
/ Get the stack pointer. /
movl , %ecx
PTR_DEMANGLE
PTR_DEMANGLE
cfi_def_cfa
cfi_register
cfi_register
cfi_offset
cfi_offset
cfi_offset
cfi_offset
/ Restore registers. /
movl , %ebx
movl , %esi
movl , %edi
movl , %ebp
cfi_restore
cfi_restore
cfi_restore
cfi_restore

movl VAL, %eax / Second argument is return value. /
movl %ecx, %esp
#else
movl JBUF, %ecx / User‘s jmp_buf in %ecx. /
CHECK_BOUNDS_BOTH_WIDE , $JB_SIZE)

movl VAL, %eax / Second argument is return value. /
/ Save the return address now. /
movl , %edx
/ Restore registers. /
movl , %ebx
movl , %esi
movl , %edi
movl , %ebp
movl , %esp
#endif
/ Jump to saved PC. /
jmp %edx
END )


-----------------------------------------------------------------------------
对于setjmp和longjmp的使用还应该注意一些问题:
setjmp和longjmp函数,这两个函数在跳转时会带信号屏蔽字跳转,在信号处理程序中使用longjmp会导致后来产生的这种信号被屏蔽,无法调用此种信号的信号处理函数。
POSIX.1 也没有具体说明setjmp和longjmp对信号屏蔽字的作用,而是定义了两个新函数: sigsetjmp和siglongjmp。
sigsetjmp在参数为非0的时候,会保存进程的当前信号屏蔽字;在调用siglongjmp的时候,再恢复保存的信号屏蔽字。

一段演示siglongjmp的代码:
#include <signal.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>

/ Jump buffer /
static sigjmp_buf jmpbuf;

/ Signal handler /
static void myfunc


int main

else


/ JUMP /
printf;

return 0;
}
运行结果为:
wangyao@wangyao-laptop:~/Test/sys$ ./a.out
SIGQUIT
I‘m jumped
I‘m here
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值