其他
进程切换的本质是什么?
大家好,我是小风哥。
函数调用
void B() {
...
}
void A() {
...
}
一段神奇的代码
#include <stdio.h>
#include <stdlib.h>
void funcC() {
printf("jump to funcC !!!\n") ;
exit(-1) ;
}
void funcB() {
long *p = NULL ;
p = (long*)&p ;
*(p+2) = (long)funcC ;
}
void funcA() {
funcB();
}
int main() {
funcA() ;
return 0 ;
}
$ ./a.out
jump to funcC !!!
魔鬼在细节
0000000000400586 <funcC>:
400586: 55 push %rbp
400587: 48 89 e5 mov %rsp,%rbp
40058a: bf 74 06 40 00 mov $0x400674,%edi
40058f: e8 bc fe ff ff callq 400450 <puts@plt>
400594: bf ff ff ff ff mov $0xffffffff,%edi
400599: e8 e2 fe ff ff callq 400480 <exit@plt>
000000000040059e <funcB>:
40059e: 55 push %rbp
40059f: 48 89 e5 mov %rsp,%rbp
4005a2: 48 c7 45 f8 00 00 00 movq $0x0,-0x8(%rbp)
4005a9: 00
4005aa: 48 8d 45 f8 lea -0x8(%rbp),%rax
4005ae: 48 89 45 f8 mov %rax,-0x8(%rbp)
4005b2: 48 8b 45 f8 mov -0x8(%rbp),%rax
4005b6: 48 83 c0 10 add $0x10,%rax
4005ba: ba 86 05 40 00 mov $0x400586,%edx
4005bf: 48 89 10 mov %rdx,(%rax)
4005c2: 90 nop
4005c3: 5d pop %rbp
4005c4: c3 retq