gpt4 book ai didi

c - 总线错误: 10 when scanning address space in c

转载 作者:行者123 更新时间:2023-11-30 15:52:31 26 4
gpt4 key购买 nike

我正在尝试扫描地址空间以查找具有读/写权限的内存块。每个页面检查一个地址是可以接受的,因为每个页面具有相同的权限。我知道当我尝试写入一段我不应该能够写入的内存时,我应该得到 Segmentation Failure: 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 在内存空间的下部。

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

最佳答案

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

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

在大多数虚拟内存系统中,非映射空间通常从地址零向上保留一段距离,因此尝试取消引用 NULL 指针或基于 NULL 指针的结构会导致异常。在 20 世纪 80 年代,空白空间通常为 64K 到 256K。在现代架构上,16M 是检测基于 NULL 的访问的合理选择。

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

关于c - 总线错误: 10 when scanning address space in c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14404114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com