daxiguagg 发表于 2013-09-09 19:28

linux有没有测试内存可读写的函数。

WINDOWS上有IsBadReadPtr和IsBadWritePtr函数测试内存的可读可写?LINUX有类式的函数吗?

myworkstation 发表于 2013-09-09 22:17

本帖最后由 myworkstation 于 2013-09-09 22:18 编辑

回复 1# daxiguagg


    没有,linux下要自己实现相应的函数,通常都是处理SIGSEGV信号来实现。examples:
#include <setjmp.h>
#include <signal.h>

static jmp_buf hndbuf;

static void handler(int ignored)
{
(void)ignored;
longjmp(hndbuf, 1);
}

static BOOL IsBadWritePtr(const void *lp, size_t ucb)
{
struct sigaction act;
struct sigaction oact;
char *base = (void*)lp;
char *over = base + ucb;
volatile char *test;

act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags |= SA_RESTART;

if (sigaction(SIGSEGV, &act, &oact) < 0) {
return FALSE; /* Debatable action */
}

switch (setjmp(hndbuf)) {
case 0:
for (test = base; test < over; test++) {
*test = *test;
}

(void)sigaction(SIGSEGV, &oact, NULL);
return FALSE;
default:
break;
}

(void)sigaction(SIGSEGV, &oact, NULL);
return TRUE;
}可以参考文章 :
http://www.360doc.com/content/12/0717/16/9484405_224759271.shtml
页: [1]
查看完整版本: linux有没有测试内存可读写的函数。