问题
I want to isolate a custom opcode in my program in order to catch invalid opcode exception if there is any. If I write an empty main, I see an assembly sequence as below:
(gdb) list
1 void main()
2 {
3 }
(gdb) disass /r main
Dump of assembler code for function main:
0x00000000004004ed <+0>: 55 push %rbp
0x00000000004004ee <+1>: 48 89 e5 mov %rsp,%rbp
0x00000000004004f1 <+4>: 5d pop %rbp
0x00000000004004f2 <+5>: c3 retq
End of assembler dump.
If I write a single random byte such as __asm__(".byte 0x01"); I see that it automatically append 5d and c3 to that because it assumes the opcode is ADD and so it needs more than one byte.
0x00000000004004f1 <+4>: 01 5d c3 add %ebx,-0x3d(%rbp)
End of assembler dump.
If I put some NOP (0x90) before and after that, it doesn't also help and it will automatically append some 0x90 to 0x01.
0x00000000004004f1 <+4>: 90 nop
0x00000000004004f2 <+5>: 90 nop
0x00000000004004f3 <+6>: 90 nop
0x00000000004004f4 <+7>: 01 90 90 90 90 90 add %edx,-0x6f6f6f70(%rax)
0x00000000004004fa <+13>: 90 nop
0x00000000004004fb <+14>: 5d pop %rbp
0x00000000004004fc <+15>: c3 retq
By executing that ADD, I get segmentation fault error. Therefore, I can not distinguish seg fault and invalid instruction/opcode. I want to isolate that opcode. What about aligning with different pages and handling page faults?
Any comment?
来源:https://stackoverflow.com/questions/56935238/generating-and-catching-invalid-instruction