Unix 上的 IsBadReadPtr 类似物

发布于 2024-10-10 17:57:34 字数 273 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你在我安 2024-10-17 17:57:34

在 POSIX 系统上执行此操作的常用方法是使用 write() 系统调用。如果无法读取内存,它将在 errno 中返回 EFAULT,而不是引发信号:(

int nullfd = open("/dev/random", O_WRONLY);

if (write(nullfd, pointer, size) < 0)
{
    /* Not OK */
}
close(nullfd);

/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 return EFAULT in errno rather than raising a signal if the memory cannot be read:

int nullfd = open("/dev/random", O_WRONLY);

if (write(nullfd, pointer, size) < 0)
{
    /* Not OK */
}
close(nullfd);

(/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.

旧城烟雨 2024-10-17 17:57:34

你怎么能做到呢?

您尝试这样做,然后处理错误。

为此,首先设置 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.

孤星 2024-10-17 17:57:34

无论是在 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.

素年丶 2024-10-17 17:57:34

在虚拟机中运行 Ubuntu 时,我尝试从帧缓冲区读取“像素”时遇到了同样的问题。似乎没有安全的方法来检查访问而不崩溃或实际挂起 gdb。 StasM 提出的建议暗示我要遵循使用 fork 的“本地”解决方案。

void *some_address;
int pid = fork();
if (pid== 0)
{
    someaddress[0] = some_address[0];
    _exit(123);
}
bool access_ok = true;
int status;
int result = waitpid(pid, &status, 0);
if (result == -1 || WIFEXITED(status) == 0 || WEXITSTATUS(status) != 123)
{
    access_ok = false;
}

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.

void *some_address;
int pid = fork();
if (pid== 0)
{
    someaddress[0] = some_address[0];
    _exit(123);
}
bool access_ok = true;
int status;
int result = waitpid(pid, &status, 0);
if (result == -1 || WIFEXITED(status) == 0 || WEXITSTATUS(status) != 123)
{
    access_ok = false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文