我正在尝试扫描地址空间以查找具有读/写权限的内存块。每个页面都检查一个地址是可以接受的,因为每个页面都具有相同的权限。我知道我应该得到 Segmentation Fault: 11 当我试图写入一块我不应该能够的内存时。当我试图访问更高的地址时会发生这种情况,但是当我在较低的部分时,比如 0x00000100,我得到总线错误:10。
注意:代码使用 -m32 标志编译,因此它模拟 32 位机器。
另请注意:在调用此函数之前,chunk_list 的内存已被分配。
我复制了下面的代码:
#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 在内存空间的下部。
编辑:我将使用信号处理段错误。我知道如何处理段错误,那么有没有办法处理总线错误:10 也使用信号?