是否在环0中运行abort
Does abort run in ring 0?
最近我在一个博客上发现了c语言中提到的abort函数。
下面是abort函数的源代码:http://cristi.indefero.net/p/uClibc-cristi/source/tree/0_9_14/libc/stdlib/abort.c
我发现它使用hlt
指令(我的PC是x86)。
但似乎hlt
必须运行在环0。(参考wiki http://en.wikipedia.org/wiki/HLT)
似乎中止正在用户空间中运行。因此,在abort中使用hlt
指令似乎是非法的。
顺便说一句,我试着在linux和windows中运行hlt
。但是我遇到一个错误。
#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;
}
Windows: cout<<"will run hlg"<<endl;
/*Unhandled exception at 0x0040101d in test_learn.exe: 0xC0000096: Privileged instruction.
*/
__asm{
hlt;
}
abort
函数在发送SIGABRT
失败后才使用hlt
指令。如果您阅读源代码,该函数首先尝试:
raise(SIGABRT);
然后调用无效指令:
/* Still here? Try to suicide with an illegal instruction */
if (been_there_done_that == 2) {
been_there_done_that++;
ABORT_INSTRUCTION;
}
所以你是对的,hlt
需要ring 0特权。这正是使它成为无效指令的原因。执行它将调用一个无效指令处理程序,在您的情况下(我想)是SIGSEGV。
您可能想看一下SIGABRT POSIX信号
例如,如果在调试GDB时碰到abort()
调用,您将看到:
Program received signal SIGABRT, Aborted.
0x0000003c47e352d5 in raise () from /lib64/libc.so.6
(gdb) where
#0 0x0000003c47e352d5 in raise () from /lib64/libc.so.6
#1 0x0000003c47e36beb in abort () from /lib64/libc.so.6
#2 0x0000000000400721 in main (argc=1, argv=0x7fffffffde18) at test.c:27
正如@kbok提到的,abort
函数的反汇编包括hlt
指令:
(gdb) disassemble abort
Dump of assembler code for function abort:
...
0x0000003c47e36b08 <+152>: hlt
...
(但SEGFAULT indeed
)
(gdb) break *0x0000003c47e36b08
Breakpoint 2 at 0x3c47e36b08
(gdb) jump *0x0000003c47e36b08
Breakpoint 2, 0x0000003c47e36b08 in abort () from /lib64/libc.so.6
(gdb) next
Single stepping until exit from function abort,which has no line number information.
Program received signal SIGSEGV, Segmentation fault.
0x0000003c47e36b08 in abort () from /lib64/libc.so.6
相关文章:
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何在运行中期切换GTK CSS style_context
- 如何在MS Visual Studio 2019中运行QT UI
- 如何通过cpp程序运行shell脚本
- IPC使用多个管道和分支进程来运行Python程序
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- c++中的指针和运行时错误
- 在C应用程序中运行C++(带有STL)函数
- 运行程序时出现问题
- 控制允许动态运行c++的并发操作数
- 是否在环0中运行abort