我正在尝试扫描地址空间以查找具有读/写权限的内存块。由于每个页面具有相同的权限,因此每页检查一个地址是可以接受的。我知道我应该得到分段错误: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也使用信号?
所以你说我执行seg错误处理程序(SIGSEGV)它只会处理总线错误?如果是的话,我会给它一个镜头。 – Kris
@KUSH:那么,处理程序必须处理错误。通常这是通过它的返回值完成的,该值指示是重新运行违规指令还是执行其他操作。 – wallyk
@wallyk只是想知道:是否有可能从用户空间安装这样的处理程序?或者这个过程需要特殊的权限? –