如何检查指针在分配的内存中写入?

时间:2021-01-24 21:16:52

I come across this question during discussion with interviewer:

我在与面试官的讨论中遇到了这个问题:

If I have allocated 4 bytes of memory from malloc.

如果我从malloc分配了4字节的内存。

int *p = (int*) malloc(4);

now if I moved pointer by 4 byte.

如果我把指针移动了4个字节。

p++;

And now pointer is pointing memory which is out of 4 byte of memory allocated by malloc. Let's assume that this memory has permission to read-write.

现在指针指向内存malloc分配的4字节的内存。让我们假设这个内存有读写的权限。

*p=4; 

That means pointer is going out of allocated memory.

这意味着指针将离开分配的内存。

Now consider a case where I allocated some memory but whenever my pointer moves out of allocated and write on that memory then I want to be informed but how??

现在考虑这样一种情况,我分配了一些内存但是当指针移出分配内存并在内存上写入时,我希望得到通知,但是怎么做呢?

I have only malloc and free to use.

我只有malloc,可以免费使用。

2 个解决方案

#1


2  

There's a library called Electric Fence that does what you want.

有一个图书馆叫“电子围栏”,可以做你想做的事。

It intercepts your malloc calls. It works by allocating pages of memory and placing the page boundary exactly at the end of your allocated memory. The page after the allocated memory is unmapped. If the program tries to write past the allocated memory, a SIGSEGV (segmentation fault) is generated.

它拦截你的malloc调用。它的工作方式是分配内存页,并将页面边界准确地放在所分配内存的末尾。分配内存后的页面未被映射。如果程序试图写入已分配的内存,则会生成一个SIGSEGV(分段错误)。

#2


1  

C does not feature what you request nativly, and compilers will in general not detect out of bound arrays on non-static variables.

C本身不支持您请求的内容,编译器一般不会检测到非静态变量上的绑定数组。

In runtime, these can be detected if using tools like valgrind and libefence. These tools however are only meant for debugging and not release, since they increase the memory pressure, CPU-usage and context switches.

在运行时,如果使用像valgrind和libefence这样的工具,就可以检测到这些信息。但是,这些工具只用于调试而不是发布,因为它们增加了内存压力、cpu使用和上下文切换。

At compile time, there exists analyzing tools (static analysis) that simulates what the program does, and try to detect misbehavior like these.

在编译时,存在分析工具(静态分析)来模拟程序所做的事情,并试图发现这些错误行为。

What you can do is to make your own data structure that contains the length information and such, and have your own read/write functions that uses this structure. This is the typical approach that high-level languages like PHP and python do internally.

您可以做的是创建包含长度信息的自己的数据结构,并拥有使用这种结构的自己的读/写函数。这是高级语言(如PHP和python)在内部执行的典型方法。

#1


2  

There's a library called Electric Fence that does what you want.

有一个图书馆叫“电子围栏”,可以做你想做的事。

It intercepts your malloc calls. It works by allocating pages of memory and placing the page boundary exactly at the end of your allocated memory. The page after the allocated memory is unmapped. If the program tries to write past the allocated memory, a SIGSEGV (segmentation fault) is generated.

它拦截你的malloc调用。它的工作方式是分配内存页,并将页面边界准确地放在所分配内存的末尾。分配内存后的页面未被映射。如果程序试图写入已分配的内存,则会生成一个SIGSEGV(分段错误)。

#2


1  

C does not feature what you request nativly, and compilers will in general not detect out of bound arrays on non-static variables.

C本身不支持您请求的内容,编译器一般不会检测到非静态变量上的绑定数组。

In runtime, these can be detected if using tools like valgrind and libefence. These tools however are only meant for debugging and not release, since they increase the memory pressure, CPU-usage and context switches.

在运行时,如果使用像valgrind和libefence这样的工具,就可以检测到这些信息。但是,这些工具只用于调试而不是发布,因为它们增加了内存压力、cpu使用和上下文切换。

At compile time, there exists analyzing tools (static analysis) that simulates what the program does, and try to detect misbehavior like these.

在编译时,存在分析工具(静态分析)来模拟程序所做的事情,并试图发现这些错误行为。

What you can do is to make your own data structure that contains the length information and such, and have your own read/write functions that uses this structure. This is the typical approach that high-level languages like PHP and python do internally.

您可以做的是创建包含长度信息的自己的数据结构,并拥有使用这种结构的自己的读/写函数。这是高级语言(如PHP和python)在内部执行的典型方法。