簡體   English   中英

回調 memory 訪問?

[英]Callback on memory access?

當 memory 被訪問(讀取或寫入)時,是否存在分配一些 memory 並具有某種回調(無論是指向 function 或信號的指針)的方法嗎?

例如,如果我說分配 1mb 的 memory,我想有辦法在訪問任何 1mb 時調用 function。

我正在使用的平台是 x86 Linux 並用 C/C++ 編寫。

就在這里。

使用 mprotect(2) 系統調用(參見: http://linux.die.net/man/2/mprotect )在頁面上設置只讀或無訪問權限 memory 保護並設置一個 SIGEGVsignal 處理程序,當memory 已被訪問。

請注意,一旦調用信號處理程序,您將需要在信號處理程序中使用 mprotect 以實際允許 memory 訪問,當您打開 window 到其他任何東西以訪問 ZCD69B4957F06CD818D7ZBF3D61980E291 示例時,您不知道它, . 這可能是也可能不是問題,具體取決於您的具體使用情況。

您可以使用自己的“安全指針”版本,如 class 將包裝分配的指針,順便說一下,將實現解引用運算符。 但是,它需要使用它進行分配。

這些行中的一些東西:

// 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. 然后這個包裝器 object 將能夠看到對底層 memory 的所有訪問。

嗯....你可以設置一個緩沖區。 您甚至可以使用分配設置一個數組。 然后設置一個 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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM