简体   繁体   English

回调 memory 访问?

[英]Callback on memory access?

Does there exist a way to allocate some memory and have some sort of callback (be it pointer to a function or signal) when the memory is being accessed (either read or written to)?当 memory 被访问(读取或写入)时,是否存在分配一些 memory 并具有某种回调(无论是指向 function 或信号的指针)的方法吗?

For example, if I said allocate 1mb of memory, I would like to have a way to call a function when any of that 1mb is being accessed.例如,如果我说分配 1mb 的 memory,我想有办法在访问任何 1mb 时调用 function。

The platform I'm working on is x86 Linux and writing in C/C++.我正在使用的平台是 x86 Linux 并用 C/C++ 编写。

Yes, there is.就在这里。

Use the mprotect(2) system call (see: http://linux.die.net/man/2/mprotect ) to set read only or no access memory protection on the page and set a SIGEGVsignal handler that will be called when the memory has been accessed.使用 mprotect(2) 系统调用(参见: http://linux.die.net/man/2/mprotect )在页面上设置只读或无访问权限 memory 保护并设置一个 SIGEGVsignal 处理程序,当memory 已被访问。

Note that you will need to use mprotect in your signal handler to actually allow the memory access once your signal handler is called and when you do you open a window to anything else to access the memory without you knowing it, for example from a different thread.请注意,一旦调用信号处理程序,您将需要在信号处理程序中使用 mprotect 以实际允许 memory 访问,当您打开 window 到其他任何东西以访问 ZCD69B4957F06CD818D7ZBF3D61980E291 示例时,您不知道它, . This may or may not be an issue depending on your specific usage.这可能是也可能不是问题,具体取决于您的具体使用情况。

You can use your own version of a "safe-pointer"-like class which will wrap the allocated pointer, and by the way will have an implementation of the dereference operator.您可以使用自己的“安全指针”版本,如 class 将包装分配的指针,顺便说一下,将实现解引用运算符。 It will require using it of cause for allocations, though.但是,它需要使用它进行分配。

Something in these lines:这些行中的一些东西:

// based on pretty standard auto_ptr
template <class T> class auto_ptr
{
    T* ptr;
public:
    explicit auto_ptr(T* p = 0) : ptr(p) {}
    ~auto_ptr()                 {delete ptr;}
    T& operator*()              {return *ptr;}   // <<--- add your stuff here
    T* operator->()             {return ptr;} // <<--- and here
    // .
};

I don't think there is such an API to do so, until you create a wrapper object around the allocated memory and then access to the memory is done through this wrapper object. I don't think there is such an API to do so, until you create a wrapper object around the allocated memory and then access to the memory is done through this wrapper object. Then this wrapper object will be able to see all access to the underlying memory.然后这个包装器 object 将能够看到对底层 memory 的所有访问。

Well....you could set up a buffer.嗯....你可以设置一个缓冲区。 You could even just set up an array with the allocation.您甚至可以使用分配设置一个数组。 Then set up an if statement that if anything tampers with that array section, for instance if the array was defaulted to have a value of 0 at an index, and now its not, call whatever you want to do.然后设置一个 if 语句,如果有任何东西篡改了该数组部分,例如,如果数组在索引处的默认值为 0,而现在它不是,则调用您想要执行的任何操作。

If you want a lot to happen, and then the program break and respond to that allocation being tampered with, set a boolean and when the value is changed, the boolean goes to true, and have a function posted to check that boolean. If you want a lot to happen, and then the program break and respond to that allocation being tampered with, set a boolean and when the value is changed, the boolean goes to true, and have a function posted to check that boolean.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM