有办法写入无效的内存访问吗?

huangapple go评论66阅读模式
英文:

Is there a way to write to an invalid memory access?

问题

我正在定义一个内存区域来存储传入的数据,但由于我是通过指针来定义这些区域的(指针表示起始/结束),所以当我尝试复制数据(写入)到它时,系统会抛出分段错误。注意:我已经检查过我的地址,它指向一个未使用的内存区域。

我已经尝试过在填充内存之前,将内存区域定义到链接器脚本中,但没有任何解决方法可以防止我遇到严重错误。

编辑:我正在嵌入式系统中工作,我知道指针指向的是无效内存(未使用),所以我想要实现的目标要么是在使用之前验证这个内存区域,要么是解决这个错误的方法。

英文:

I'm defining memory region to store incoming data, but this regions are not initialized as I'm defining them through pointers (the pointers represent start/end), so when I'm trying to copy data (write) to it, the system throws a segmentation fault. NOTE: I already checked my address and it's pointing to an unused memory region.

I already tried filling the memory beforehand, defining the memory region into the linker script, but no workaround saves me from getting a hard fault.

Edit: I'm working within an embedded system, I KNOW the pointer is pointing to invalid memory (unused), so the thing I want to achieve is either validate such memory region before using it or a workaround the fault.

答案1

得分: 2

以下是翻译好的部分:

"确保内存未被使用并不足够。[1] 内存管理系统需要知道现在正在使用这段内存。通常通过使用malloccalloc来请求内存来实现这一点(但也有其他特定于系统的获取内存的方法)。

这些函数提供了指向已标记为已分配但未使用的内存的指针。标记为已分配确保将来的分配不会使用该内存(除非在期间已被释放)。它还确保操作系统在您使用该内存时不会引发段错误。


  1. 对我来说,甚至不明显这是否可能。如果可能的话,肯定会比简单使用malloc/calloc更复杂。"
英文:

It's not enough to ensure the memory is unused.<sup>[1]</sup> The memory management system needs to be made aware that the memory is now being used. This is typically done by requesting memory using malloc or calloc (but there are other system-specific means of obtaining memory).

These functions provides a pointer to memory that is unused after marking it as allocated. Marking is as allocated ensures that future allocations won't use that memory (unless it has been freed in the interim). And it makes sure the OS doesn't throw a segfault when you use that memory.


  1. It's not evident to me that this is even possible. And if it is, it would surely be more complicated than simply using malloc/calloc.

huangapple
  • 本文由 发表于 2023年6月15日 07:32:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定