當我運行以下C程序(在Ubuntu中使用gcc編譯)時,出現分段錯誤。字節編碼函數時發生分段錯誤?
#include <stdio.h>
char f[] = "\x55\x48\x89\xe5\x48\x89\x7d\xf8\x48\x89\x75\xf0\x48\x8b\x45\xf8\x8b\x10\x48\x8b\x45\xf0\x8b\x00\x89\xd1\x29\xc1\x89\xc8\xc9\xc3";
int main()
{
int (*func)();
func = (int (*)()) f;
int x=3,y=5;
printf("%d\n",(int)(*func)(&x,&y));
return 0;
}
字符串f
包含以下功能的機器代碼。
int f(int*a, int*b)
{
return *a-*b;
}
c.f:
f.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <f>:
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: 48 89 7d f8 mov %rdi,-0x8(%rbp)
8: 48 89 75 f0 mov %rsi,-0x10(%rbp)
c: 48 8b 45 f8 mov -0x8(%rbp),%rax
10: 8b 10 mov (%rax),%edx
12: 48 8b 45 f0 mov -0x10(%rbp),%rax
16: 8b 00 mov (%rax),%eax
18: 89 d1 mov %edx,%ecx
1a: 29 c1 sub %eax,%ecx
1c: 89 c8 mov %ecx,%eax
1e: c9 leaveq
1f: c3 retq
這是使用編譯:
gcc test.c -Wall -Werror
./a.out
Segmentation fault
預期輸出爲-2
- 我怎樣才能得到它的工作?
哪裏是用於'F'函數的代碼? –
更新的f.c內容 – Jo0o0
運行這樣的代碼,你甚至不知道它是如何處理root用戶('sudo'等)是不負責任的。你值得擁有你自己的程序吃你的硬盤。 –