C SIGSEGV Handler & Mprotect

前端 未结 3 2185
不知归路
不知归路 2020-12-18 10:34

I\'m constructing a program which uses mprotect() to restrict a block of memory from accessing. When the memory is requested, a SIGSEGV is thrown which I listen for using a

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-18 10:57

    You are looking for libsigsegv http://libsigsegv.sourceforge.net/

    But beware that calling mprotect is only signal-safe in Linux, other POSIX systems may not support this.

    I am afraid that in Linux the only way to get memory protection bits is to read in /proc/$pid/meminfo

    On a side note (Linux only): If you are worried about memory consumption and intend to enable pages of a larger mapping one by one then I would advise to create your mapping using mmap with MAP_NORESERVE in which case you will get a mapping to zero-filled copy-on-write pages which will allocate physical RAM on the first write. MAP_NORESERVE instructs the kernel not to back your memory with swap space allowing you to allocate up to 64TB of virtual address space. Only downside is that if you do run out of memory terrible things can happen (oom-killer).

提交回复
热议问题