这里是 IT 工程专业的学生。我们被要求尝试上下文切换,一项特定的任务让我们实现一个相当粗糙的尝试/抛出系统。这是我们编写的代码:
struct ctx_s {
int esp;
int ebp;
};
struct ctx_s * pctx;
typedef int (func_t)(int); /* a function that returns an int from an int */
int try(func_t *f, int arg)
{
/* saving context by storing values of %esp and %ebp */
asm ("movl %%esp, %0"
: "=r"((*pctx).esp)
:
);
asm ("movl %%ebp, %0"
: "=r"((*pctx).ebp)
:
);
/* calling the function sent to try(), returning whatever it returns */
return f(arg);
}
int throw(int r)
{
printf("MAGIC PRINT\n");
static int my_return = 0;
/* ^ to avoid "an element from initialisation is not a constant" */
my_return = r;
/* restituting context saved in try() */
asm ("movl %0, %%esp"
:
: "r"((*pctx).esp)
);
asm ("movl %0, %%ebp"
:
: "r"((*pctx).ebp)
);
/* this return will go back to main() since we've restored try()'s context
so the return address is whatever called try... */
/* my_return is static (=> stored in the heap) so it's not been corrupted,
unlike r which is now the second parameter received from try()'s context,
and who knows what that might be */
return my_return;
}
pctx 是一个指向包含两个 int 的简单结构的全局指针,f 是一个调用 throw() 的函数,发送一些 #define'd 返回代码到 42,main() 本质上分配 pctx,result=try(f , 0) 并打印结果。我们预计结果为 42。
现在,您可能已经发现了 throw() 中的 MAGIC PRINT。它在这里的原因尚不完全清楚;基本上,大多数(不是全部)学生在 throw() 内出现了段错误;在该函数内调用 printf() 使程序看起来可以正常工作,老师们认为任何系统调用也可以正常工作。
由于我没有真正得到他们的解释,所以我尝试比较两个版本(带和不带 printf())的 gcc -S 生成的汇编代码,但我无法充分理解。在 throw() 的左大括号(第 33 行)处设置断点并使用 gdb 反汇编给了我这个:
没有 printf():
Breakpoint 1, throw (r=42) at main4.c:38
(gdb) disass
Dump of assembler code for function throw:
0x0804845a <throw+0>: push %ebp
0x0804845b <throw+1>: mov %esp,%ebp
0x0804845d <throw+3>: mov 0x8(%ebp),%eax
0x08048460 <throw+6>: mov %eax,0x8049720
0x08048465 <throw+11>: mov 0x8049724,%eax
0x0804846a <throw+16>: mov (%eax),%eax
0x0804846c <throw+18>: mov %eax,%esp
0x0804846e <throw+20>: mov 0x8049724,%eax
0x08048473 <throw+25>: mov 0x4(%eax),%eax
0x08048476 <throw+28>: mov %eax,%ebp
0x08048478 <throw+30>: mov 0x8049720,%eax
0x0804847d <throw+35>: pop %ebp
0x0804847e <throw+36>: ret
End of assembler dump.
(gdb) c
Continuing.
Program received signal SIGSEGV, Segmentation fault.
0xb7e846c0 in ?? ()
使用 printf():
Breakpoint 1, throw (r=42) at main4.c:34
(gdb) disassemble
Dump of assembler code for function throw:
0x0804845a <throw+0>: push %ebp
0x0804845b <throw+1>: mov %esp,%ebp
0x0804845d <throw+3>: sub $0x18,%esp
0x08048460 <throw+6>: movl $0x80485f0,(%esp)
0x08048467 <throw+13>: call 0x8048364 <puts@plt>
0x0804846c <throw+18>: mov 0x8(%ebp),%eax
0x0804846f <throw+21>: mov %eax,0x804973c
0x08048474 <throw+26>: mov 0x8049740,%eax
0x08048479 <throw+31>: mov (%eax),%eax
0x0804847b <throw+33>: mov %eax,%esp
0x0804847d <throw+35>: mov 0x8049740,%eax
0x08048482 <throw+40>: mov 0x4(%eax),%eax
0x08048485 <throw+43>: mov %eax,%ebp
0x08048487 <throw+45>: mov 0x804973c,%eax
0x0804848c <throw+50>: leave
0x0804848d <throw+51>: ret
End of assembler dump.
(gdb) c
Continuing.
MAGIC PRINT
result = 42
Program exited normally.
我真的不知道该怎么办。显然事情正在发生不同的情况,但我发现很难理解这两种情况发生了什么...... 所以我的问题本质上是:调用 printf make 如何抛出而不是段错误?
最佳答案
好吧,这是一个有点松散的分析,因为我看不到 try 部分,但从标准调用约定来看,包含 try 的方法将保存 %esp
至%ebp
,减少%esp
为局部变量腾出空间并运行“尝试”代码来保存 %esp
和%ebp
.
通常,当函数退出时,它会使用 leave
恢复这些更改。返回之前。离开就会恢复%ebp
进入%esp
,流行 %ebp
并做它的返回。这确保 %esp
恢复到保留局部变量空间之前的状态。
没有printf
版本的问题是它不使用 leave
弹出 %ebp
无需先将其内容恢复到 %esp
。 ret
指令将弹出一个局部变量并返回到该变量。这不是最好的结果。
我怀疑,由于你的函数没有局部变量,编译器认为没有理由恢复 %esp
来自%ebp
。自 printf
在堆栈上保留空间,编译器知道该版本中 %esp
返回之前必须恢复。
如果你想测试理论,只需编译为汇编程序,替换即可;
0x0804847d <throw+35>: pop %ebp
带有离开指令并组装结果。它应该也能正常工作。
或者,我怀疑您可以在 asm 指令中向 gcc 指示 %esp
已被破坏,从而使其生成休假。
编辑:显然标记%esp
因为 clobbered 本质上是 gcc 中的 NOOP :-/
关于assembly - 调用 printf 可以防止段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12769160/