SEH,访问冲突和堆栈保护页面

时间:2022-09-14 20:30:05

I've posted a question about validating a pointer's accessibility. The conclusion was either to use IsBadReadPtr to check the pointer, or SEH to catch the exception (and preferably to use neither, and debug the application, but that's not the issue here).

我发布了一个关于验证指针可访问性的问题。结论要么是使用IsBadReadPtr来检查指针,要么是SEH来捕获异常(最好不使用它,并调试应用程序,但这不是问题)。

IsBadReadPtr is said to be bad because, among other reasons, it would try to read the pointer, and would catch any exception. It might catch a stack guard page exception, and thus prevent it from reaching the memory manager, which should have enlarged the stack.

IsBadReadPtr据说是坏的,因为除了其他原因,它会尝试读取指针,并会捕获任何异常。它可能会捕获堆栈保护页异常,从而阻止它到达应该扩大堆栈的内存管理器。

If I use SEH and catch only EXCEPTION_ ACCESS_VIOLATION exceptions, would this create the same problem?

如果我使用SEH并仅捕获EXCEPTION_ ACCESS_VIOLATION异常,这会产生同样的问题吗?

Another thing: What are the implications of using SEH? This article suggests that "the compiler can’t perform flow analysis in code protected by SEH". How about if I call a function inside __try block. Would the compiler not optimize the called function at all?

另一件事:使用SEH有什么含义?本文建议“编译器不能在SEH保护的代码中执行流分析”。如果我在__try块中调用一个函数怎么样?编译器根本不会优化被调用函数吗?

1 个解决方案

#1


If I use SEH and catch only EXCEPTION_ ACCESS_VIOLATION exceptions, would this create the same problem?

如果我使用SEH并仅捕获EXCEPTION_ ACCESS_VIOLATION异常,这会产生同样的问题吗?

I think it would. A workaround might be to probe the stack[s] of any thread[s] you know and care about, before you start calling IsBadReadPtr (by "probe the stack" I mean to deliberately touch every memory page in the stack, to ensure every page is pre-allocated).

我想会的。一个解决方法可能是在你开始调用IsBadReadPtr之前探测你知道和关心的任何线程的堆栈[通过“探测堆栈”我的意思是故意触摸堆栈中的每个内存页面,以确保每一个页面已预先分配)。

Would the compiler not optimize the called function at all?

编译器根本不会优化被调用函数吗?

If the function is not inlined, I would expect the compiler to apply the usual optimizations (optimization of the function wouldn't be affected by where the function is called from).

如果函数没有内联,我希望编译器应用通常的优化(函数的优化不会受到调用函数的位置的影响)。

#1


If I use SEH and catch only EXCEPTION_ ACCESS_VIOLATION exceptions, would this create the same problem?

如果我使用SEH并仅捕获EXCEPTION_ ACCESS_VIOLATION异常,这会产生同样的问题吗?

I think it would. A workaround might be to probe the stack[s] of any thread[s] you know and care about, before you start calling IsBadReadPtr (by "probe the stack" I mean to deliberately touch every memory page in the stack, to ensure every page is pre-allocated).

我想会的。一个解决方法可能是在你开始调用IsBadReadPtr之前探测你知道和关心的任何线程的堆栈[通过“探测堆栈”我的意思是故意触摸堆栈中的每个内存页面,以确保每一个页面已预先分配)。

Would the compiler not optimize the called function at all?

编译器根本不会优化被调用函数吗?

If the function is not inlined, I would expect the compiler to apply the usual optimizations (optimization of the function wouldn't be affected by where the function is called from).

如果函数没有内联,我希望编译器应用通常的优化(函数的优化不会受到调用函数的位置的影响)。