These days i found a blog the mentioned abort function in C.
The following is the source code of abort function:
http://cristi.indefero.net/p/uClibc-cristi/source/tree/0_9_14/libc/stdlib/abort.c
I found that it use the hlt instruction (My PC is x86).
But seems that hlt must run in ring 0.
(refer to wiki http://en.wikipedia.org/wiki/HLT)
It seems that the abort is running in the user space. So the usage of the hlt instruction in abort seems illegal.
BTW, I try to run hlt in linux and windows. But i encounter an error.
In linux:
#include <iostream>
using namespace std;
#define HLT_INST asm("hlt")
int main(){
cout<<"whill run halt"<<endl;
HLT_INST; //result in SIGSEGV error
return 0;
}
In Windows:
cout<<"will run hlg"<<endl;
/*Unhandled exception at 0x0040101d in test_learn.exe: 0xC0000096: Privileged instruction.
*/
__asm{
hlt;
}
The
abortfunction only uses thehltinstruction after sendingSIGABRTfails. If you read the source code, the function first tries to:And then calls the invalid instruction :
So you are right,
hltrequires ring 0 privileges. That’s precisely what makes it an invalid instruction. Executing it will call an invalid-instruction handler, which is in your case (I suppose) SIGSEGV.