当前位置: 技术问答>linux和unix
请问:linux下有没有类似windows下的IsbadWritePtr的函数啊?
来源: 互联网 发布时间:2015-11-14
本文导语: 就是判断一段内存是否可读可写的函数 | 没有类似的函数或者系统调用。不过你可以自己实现类似的功能,下面的代码实现了函数IsReadBadPtr, IsWriteBadPtr的实现类似。大致原理是在linux下非法指...
就是判断一段内存是否可读可写的函数
|
没有类似的函数或者系统调用。不过你可以自己实现类似的功能,下面的代码实现了函数IsReadBadPtr, IsWriteBadPtr的实现类似。大致原理是在linux下非法指针访问产生SIGSEGV, 并且会产生core dump, 你在测试指针之前设置自己的SIGSEGV信号处理函数就可以不产生core dump。
#include
#include
#include
#include
/*
***************************
* Global Variables
***************************
*/
static volatile sig_atomic_t segv_flag;
static jmp_buf jmp_env;
/*
********************************************
* Pre: none
* Post: restore the original process state
* and return 1 to setjmp
********************************************
*/
static void segv_handler( int dummy )
{
segv_flag = 1;
longjmp( jmp_env , 1);
}
/*
********************************************
* Pre: A pointer and it's length
* Post: Return true is the pointer is bad
* or false if otherwise
********************************************
*/
int IsReadBadPtr(void * ptr_buffer, unsigned long buffer_size)
{
struct sigaction oldaction, newaction;
volatile char dummy;
char *ptr = (char *)ptr_buffer;
if ( !buffer_size )
return 0;
/* storing the old signal environment and trapping SIGSEGV */
newaction.sa_handler = segv_handler;
newaction.sa_flags = 0;
sigemptyset( &newaction.sa_mask);
sigaction( SIGSEGV, &newaction, &oldaction);
segv_flag = 0;
/* Storing the process state so if any failure happens
we can restore it to the original state */
if ( setjmp(jmp_env) == 0 )
{
/* testing the pointer: only the first and the end are needed here since any failure to any of this would indicate there would an error on the entire range */
dummy = ptr[0];
dummy = ptr[buffer_size-1];
}
/* restoring the original signal environment */
sigaction(SIGSEGV, &oldaction, NULL);
return segv_flag;
}
int main()
{
char *testptr,c; // testptr[10],结果将是Ok read Ptr
if ( IsReadBadPtr(testptr, 10))
printf(" Bad read Ptrn");
else
printf(" Ok read Ptrn");
fflush(stdout);
return 0;
}
#include
#include
#include
#include
/*
***************************
* Global Variables
***************************
*/
static volatile sig_atomic_t segv_flag;
static jmp_buf jmp_env;
/*
********************************************
* Pre: none
* Post: restore the original process state
* and return 1 to setjmp
********************************************
*/
static void segv_handler( int dummy )
{
segv_flag = 1;
longjmp( jmp_env , 1);
}
/*
********************************************
* Pre: A pointer and it's length
* Post: Return true is the pointer is bad
* or false if otherwise
********************************************
*/
int IsReadBadPtr(void * ptr_buffer, unsigned long buffer_size)
{
struct sigaction oldaction, newaction;
volatile char dummy;
char *ptr = (char *)ptr_buffer;
if ( !buffer_size )
return 0;
/* storing the old signal environment and trapping SIGSEGV */
newaction.sa_handler = segv_handler;
newaction.sa_flags = 0;
sigemptyset( &newaction.sa_mask);
sigaction( SIGSEGV, &newaction, &oldaction);
segv_flag = 0;
/* Storing the process state so if any failure happens
we can restore it to the original state */
if ( setjmp(jmp_env) == 0 )
{
/* testing the pointer: only the first and the end are needed here since any failure to any of this would indicate there would an error on the entire range */
dummy = ptr[0];
dummy = ptr[buffer_size-1];
}
/* restoring the original signal environment */
sigaction(SIGSEGV, &oldaction, NULL);
return segv_flag;
}
int main()
{
char *testptr,c; // testptr[10],结果将是Ok read Ptr
if ( IsReadBadPtr(testptr, 10))
printf(" Bad read Ptrn");
else
printf(" Ok read Ptrn");
fflush(stdout);
return 0;
}