且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Segfault后:有没有办法检查指针是否仍然有效?

更新时间:2022-10-15 14:44:50

当然,如果您依赖的堆栈或其他内存已损坏,那么可能会出现问题,但对于任何代码都是如此。



假设您依赖的堆栈或其他内存没有问题,并且假设您没有调用像 malloc()这不是异步信号安全,并假设你不要试图从你的信号处理程序返回,那么从信号处理程序中读取或写入缓冲区应该没有问题。



如果您试图测试特定的地址是有效的,您可以使用系统调用,例如 mincore() 并检查错误结果。


I plan to create a logging/tracing mechanism, which writes the address (const char*) of string literals to a ring-buffer. These strings are in the read-only data-segment and are created by the preprocessor with __function__ or __file__.

The Question: Is it possible, to analyze this ring-buffer content after a Segfault, if all pointers are valid? With "valid" I mean that they point to a mapped memory area and dereferencing won't cause a segmentation fault.

I'm working with Linux 2.6.3x and GCC 4.4.x.

Best regards,

Charly

Of course if the stack or other memory that you rely upon has been corrupted then there could be problems, but that is true for any code.

Assuming that that there is no problem with the stack or other memory that you rely upon, and assuming that you do not call any functions like malloc() that are not async-signal safe, and assuming that you do not attempt to return from your signal handler, then there should be no problem reading or writing your buffer from within your signal handler.

If you are trying to test whether a particular address is valid, you could use a system call such as mincore() and check for an error result.