如何检测某个地址是否会导致访问冲突?

发布于 09-10 22:50 字数 388 浏览 8 评论 0原文

我正在为 Lua 绑定创建一个类,它包含一个指针并且可以由脚本编写者更改。它将包括一些函数,例如 :ReadString 和 :ReadBool,但如果我知道它们提供的地址将导致访问冲突,我不希望应用程序崩溃。

检测地址是否在可读/可写内存之外的好方法是?谢谢!

可能有用的函数库是“虚拟”函数库,例如 VirtualQuery

我并不是真的在寻找一个万无一失的设计,我只是想省略明显的(空指针,可能的内存位置之外的指针)

我了解这个库是多么不安全,我不是在寻求安全,只是在寻求理智。

I'm creating a class for a Lua binding which holds a pointer and can be changed by the scripter. It will include a few functions such as :ReadString and :ReadBool, but I don't want the application to crash if I can tell that the address they supplied will cause an access violation.

Is the a good way to detect if an address is outside of the readable/writable memory? Thanks!

A function library that may be useful is the "Virtual" function libraries, for example VirtualQuery

I'm not really looking for a foolproof design, I just want to omit the obvious (null pointers, pointers way outside the possible memory location)

I understand how unsafe this library is, and I'm not looking for safety, just sanity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

聊慰2024-09-17 22:50:09

方法是有的,但它们并不能达到您想要的目的。那是;是的,您可以确定某个地址在当前时刻是否有效。但;不,您无法确定该地址在几个时钟周期后是否有效。另一个线程可能会更改虚拟内存映射,并且以前有效的地址将变得无效。

正确处理访问可疑指针的可能性的唯一方法是使用平台上可用的任何本机异常处理。这可能涉及处理信号SIG_BUS,或者可能涉及使用专有的__try__catch扩展。

要使用的习惯用法是尝试访问并显式处理所产生的异常(如果确实发生了任何异常)。

您还存在确保返回的指针指向您的内存或某些其他内存的问题。为此,您可以创建自己的数据结构,我会想到一棵树,它存储您的“指针”可以实现的有效地址范围。否则,脚本代码可以为您提供一些绝对地址,并且您将通过操作系统更改进程环境的内存结构。

您编写的应用程序非常可疑,您可能应该从爆炸性较小的设计开始。但我想如果你真的想的话我会告诉你如何玩火。

There are ways, but they do not serve the purpose you intend. That is; yes, you can determine whether an address appears to be valid at the current moment in time. But; no, you cannot determine whether that address will be valid a few clock cycles from now. Another thread could change the virtual memory map and a formerly valid address would become invalid.

The only way to properly handle the possibility of accessing suspect pointers is using whatever native exception handling is available on your platform. This may involve handling the signal SIG_BUS or it may involve using the proprietary __try and __catch extensions.

The idiom to use is the one wherein you attempt the access, and explicitly handle the resulting exception, if any does happen to occur.

You also have the problem of ensuring that the pointers you return point to your memory or to some other memory. For that, you could make your own data structure, a tree springs to mind, which stores the valid address ranges your "pointers" can achieve. Otherwise, the script code can hand you some absolute addresses and you will change memory structures by the operating system for the process environment.

The application you write about is highly suspect and you should probably start over with a less explosive design. But I thought I would tell you how to play with fire if you really want to.

话少情深2024-09-17 22:50:09

请查看 Raymond Chen 的博客文章,其中更深入地探讨了原因这种做法很糟糕。最有趣的是,他指出,一旦某个页面通过 IsBadReadPtr 测试,进一步访问该页面将不会引发异常!

Check out Raymond Chen's blog post, which goes more deeply into why this practice is bad. Most interestingly, he points out that, once a page is tested by IsBadReadPtr, further accesses to that page will not raise exceptions!

我三岁2024-09-17 22:50:09

不存在,这就是为什么你永远不应该做这样的事情。

There is no, and that's why you should never do things like this.

我是有多爱你2024-09-17 22:50:09

也许尝试使用 segvcatch,它可以将段错误转换为 C++ 异常。

Perhaps try using segvcatch, which can convert segfaults into C++ exceptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文