内存访问回调?

发布于 2024-11-13 06:05:44 字数 155 浏览 1 评论 0原文

当内存被访问(读取或写入)时,是否存在一种方法来分配一些内存并进行某种回调(无论是指向函数还是信号的指针)?

例如,如果我说分配 1mb 内存,我希望有一种方法可以在访问任何 1mb 内存时调用函数。

我正在使用的平台是 x86 Linux,用 C/C++ 编写。

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)?

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.

The platform I'm working on is x86 Linux and writing in C/C++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

转身泪倾城 2024-11-20 06:05:44

是的,有。

使用 mprotect(2) 系统调用(请参阅:http://linux.die.net/man/ 2/mprotect) 在页面上设置只读或不可访问内存保护,并设置一个 SIGEGV 信号处理程序,该处理程序将在内存被访问时调用。

请注意,一旦调用信号处理程序,您将需要在信号处理程序中使用 mprotect 来实际允许内存访问,并且当您这样做时,您会在不知情的情况下打开一个窗口以访问其他任何内容,例如从不同的线程访问内存。根据您的具体使用情况,这可能是也可能不是问题。

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.

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. This may or may not be an issue depending on your specific usage.

绝對不後悔。 2024-11-20 06:05:44

您可以使用自己版本的“安全指针”类,它将包装分配的指针,并且顺便实现取消引用运算符。不过,它需要使用它来进行分配。

这些行中的一些内容:

// 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
    // .
};

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. 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
    // .
};
浮萍、无处依 2024-11-20 06:05:44

我认为没有这样的 API 可以做到这一点,直到您在分配的内存周围创建一个包装器对象,然后通过该包装器对象完成对内存的访问。然后这个包装对象将能够看到对底层内存的所有访问。

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.

夜访吸血鬼 2024-11-20 06:05:44

嗯……你可以设置一个缓冲区。您甚至可以只设置一个带有分配的数组。然后设置一个 if 语句,如果有任何内容篡改该数组部分,例如,如果数组的索引处默认值为 0,而现在不是,则调用您想要执行的任何操作。

如果您希望发生很多事情,然后程序中断并响应该分配被篡改,请设置一个布尔值,当该值更改时,该布尔值变为 true,并发布一个函数来检查该布尔值。

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文