2013-01-18 32 views
0

我正在尝试扫描地址空间以查找具有读/写权限的内存块。由于每个页面具有相同的权限,因此每页检查一个地址是可以接受的。我知道我应该得到分段错误:11当试图写入一段内存我不应该能够。当我试图访问更高的地址时,会发生这种情况,但是当我处于较低的地址,比如说0x00000100时,出现总线错误:10.总线错误:10当扫描地址空间在c

注意:代码是使用-m32标志编译的,因此它模拟32 bit机器。

另请注意:在调用此函数之前,chunk_list的内存已经被malloc化了。

我已经复制下面的代码:

#include <signal.h> 
#include <stdio.h> 
#include <unistd.h> 
#include "memchunk.h" 


int get_mem_layout (struct memchunk *chunk_list, int size) 
{ 
//grab the page size 
long page = sysconf(_SC_PAGESIZE); 
printf("The page size for this system is %ld bytes\n", page); 

//page size is 4069 bytes 

//test printing the number of words on a page 
long words = page/4; 
printf("Which works out to %ld words per page\n", words); 

//works out to 1024 words a page 
//1024 = 0x400 

//create the addy pointer 
    //start will be used after bus error: 10 is solved 
void *start; 
char * currAddy; 
currAddy = (char*)0x01000000; 

//someplace to store the addy to write to 
//char * testWrite; 


//looping through the first size pages 
int i; 
for(i = 0; i < size; i++){ 

    //chunk start - wrong addy being written just testing 
    chunk_list[i].start = currAddy; 
    printf("addy is %p\n",currAddy); 
    sleep(1); 

    //try and write to the current addy 
    //testWrite = currAddy; 
    //*testWrite = 'a'; 

    *currAddy = '1'; 


    //+= 0x400 to get to next page 
    currAddy += 0x400; 
} 


//while loop here for all the addys - not there yet because still dealing with bus error: 10 

return 0; 


} 

任何帮助将不胜感激。我还在代码中留下了一些其他的尝试,但仍然会在内存空间的下部产生总线错误:10。

编辑:我将使用信号处理seg故障。我知道如何处理seg故障,那么是否有办法处理总线错误:10也使用信号?

回答

1

读取应该写入未映射的内存会导致总线故障。要发现内存是否存在,请为SEGFAULT安装处理程序以做出相应的反应。

在Linux SE(安全增强)过程中,程序部分被加载到随机位置以阻止病毒依赖稳定的地址。

在大多数虚拟内存系统中,非映射空间通常从地址零开始一直向上,因此尝试对NULL指针或基于NULL指针的结构进行解引用会导致异常。在20世纪80年代,空白区域通常是64K到256K。在现代体系结构中,16M是检测基于NULL的访问的合理选择。

在许多虚拟内存系统上,有系统调用来获取每个进程映射的内存位置。在Linux上,检查/proc/self/maps的内容。

+0

所以你说我执行seg错误处理程序(SIGSEGV)它只会处理总线错误?如果是的话,我会给它一个镜头。 – Kris

+0

@KUSH:那么,处理程序必须处理错误。通常这是通过它的返回值完成的,该值指示是重新运行违规指令还是执行其他操作。 – wallyk

+0

@wallyk只是想知道:是否有可能从用户空间安装这样的处理程序?或者这个过程需要特殊的权限? –