Unix 上的 IsBadReadPtr 类似物
Unix 中是否有类似于 IsBadReadPtr 的函数?至少 IsBadReadPtr 的一些功能? 我想编写一个过程,如果进程发生问题(例如 SIGSEGV),该过程会做出反应并恢复一些信息。但我想检查指针以确保数据没有损坏,并查看是否可以安全地访问它们。否则崩溃处理程序本身就会崩溃,从而变得毫无用处。
有什么建议吗?
Is there a function analogous to IsBadReadPtr in Unix? At least some functionalities of IsBadReadPtr?
I want to write a procedure which would react if something bad happens to a process (like SIGSEGV
) and recover some information. But I want to check the pointers to make sure that the data is not corrupt and see if they can be accessed safely. Otherwise the crash handling procedure itself will crash, thus becoming useless.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 POSIX 系统上执行此操作的常用方法是使用 write() 系统调用。如果无法读取内存,它将在
errno
中返回EFAULT
,而不是引发信号:(/dev/random
是一个很好用的设备在 Linux 上,因为它可以由任何用户写入,并且实际上会尝试读取给定的内存,在没有/dev/random
或不可写入的操作系统上,请尝试/。 dev/null
)。另一种选择是匿名管道,但如果您想测试较大区域,则需要定期清除管道的读取端。The usual way to do this on POSIX systems is to use the
write()
system call. It will returnEFAULT
inerrno
rather than raising a signal if the memory cannot be read:(
/dev/random
is a good device to use for this on Linux, because it can be written by any user and will actually try to read the memory given. On OSes without/dev/random
or where it isn't writeable, try/dev/null
). Another alternative would be an anonymous pipe, but if you want to test a large region you'll need to regularly clear the reading end of the pipe.你怎么能做到呢?
您尝试这样做,然后处理错误。
为此,首先设置 sigsetjmp 和 SIGSEGV 信号处理程序。然后尝试使用指针。如果它是一个坏指针,则调用 SIGSEGV 处理程序,您可以跳转到安全并向用户报告错误。
How can you do it?
You try to do it and then handle the error.
To do this, first you set up a sigsetjmp and a SIGSEGV signal handler. Then attempt to use the pointer. If it was a bad pointer then the SIGSEGV handler is called and you can jump to safety and report the error to the user.
无论是在 Windows 还是 Unix 上,您永远无法判断“是否可以安全地访问指针”。但对于某些 UNIX 平台上的一些类似信息,请查看
cat /proc/self/maps
。You can never tell "whether a pointer can be accessed safely", on Windows or on Unix. But for some similar information on some unix platforms, check out
cat /proc/self/maps
.在虚拟机中运行 Ubuntu 时,我尝试从帧缓冲区读取“像素”时遇到了同样的问题。似乎没有安全的方法来检查访问而不崩溃或实际挂起 gdb。 StasM 提出的建议暗示我要遵循使用 fork 的“本地”解决方案。
I ran into the same issue trying to read a 'pixel' from a framebuffer while running Ubuntu from within a virtualbox. There seemed to be no secure way to check access without crashing or acutally hanging gdb. The suggestion made by StasM hinted me towards to following working 'local' solution using fork.