当前位置:网站首页>Handling of legal instruction problems

Handling of legal instruction problems

2022-06-21 10:36:00 Longcheng deficit

Illegal (iLLegal) instruction Intuitively explain ---- Illegal order . On the surface CPU During the execution of instructions , Illegal instruction found , That is, unknown instructions or unauthorized instructions . If you just follow this idea , It's easy to fall into trust in the compiler . Actually , This problem , It may also be caused by the program itself . For example, embedded assembly is used , Then the writer is responsible for the instructions used . Another example is the stack 、 Memory error , here , The instruction accessed may be random data in memory or non instruction data , It can also lead to CPU Can't recognize . I saw a comprehensive analysis article on the Internet , Put it here for reference :

Illegal order (Illegal Instruction) Problem location - ArnoldLu - Blog Garden

I need to add something to this question :

I haven't thought it over before , Made the above conclusion , Then I thought about it , It doesn't feel so simple . For the first case , Use embedded assembly , Actual test , It is found that if the wrong assembly instruction is used , The compiler is not recognized . Wanting is also , Embedded assembly is also a format , There are also many ways to write extensions , The compiler does not follow a macro like definition , Directly trusted and used , Instead, it requires secondary treatment , Keep the basic processing flow , Regenerate assembly instructions that do not affect the context ( The use of registers is considered as a whole ). thus , It is impossible to cheat the machine easily by embedding assembly . About embedded assembly , For simple examples, please refer to the following links :

Is inline compilation terrible ? After reading this article , End it !

continue , For the second case , Modify code instructions , It is not a simple and easy thing . Modern operating systems use the virtual memory mechanism , further , Most of them use the paging mechanism . that , For program code snippets , When the operating system maps memory pages , Will mark such pages as read-only , therefore , Try to modify the code directly , It doesn't work .

For this point , It's not absolute . Can pass mprotect call , Modify the read / write properties of the page , Reference article :

Kernel hotfix , Is it really safe ?

To sum up the above two points , Artificial generation of illegal instructions , We need to think again .

The third supplement : stay X86 After the next attempt fails many times , Finally in the arm The servant successfully generated the above error instruction prompt . The specific process is as follows :

First , The program is simply handled , The following abnormal signal capture is added

void signal_handler(int signum) {
   printf("Signal %d (number) captured \n", signum);
}

signal(SIGILL , signal_handler);


The above code expects that when an illegal instruction is generated , Captured by the application , And further processing .

And then to x86, Because simple error instructions are recognized by the compiler , So I mainly tried call and jmp Instructions . Both instructions are followed by a random address , Can compile through . however , Actual operation , Are similar paragraph errors , That is, it is mainly a memory error .
call Because it is similar to function call , There are operations such as entering and leaving the stack , The paragraph error is understandable , however jmp The order is an unconditional jump , The address you jump to may not contain valid code , For example, unable to decode . however , Multiple attempts , It is still a segment error .x86 Next abandon .

Try ARM platform . Found one with CPU Version dependent instructions ,swpb, This is one that existed in the early days ARM CPU Instructions on the , Interested readers can search for .
Use the following embedded assembly code

asm volatile("swpb %0,%2,[%3]"
             : "=&r"(ret), "=m" (*ptr)
             : "r"(newval), "r"(ptr)
             : "cc", "memory");    //memory == no cache  |  cc == status register update

Compile and pass . Execution procedure , Report illegal instructions .
gdb Trace debugging , When an illegal instruction is found , Yes swpb Command Division . Here's the picture :

Program received signal SIGILL, Illegal instruction.    An illegal instruction was successfully generated .
This explanation , On the embedded platform , Maybe because of the embedded assembly 、 Compiler Version 、 Connection libraries, etc. cause illegal runtime instructions .

原网站

版权声明
本文为[Longcheng deficit]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221440143226.html