5

Is it possible - from an application - to explicitly control memory page commits? In a Windows app many years ago, I wrote an "alarm system" heap manager to help me find a random dangling reference/heap corruption bug. I was able to reserve a large area of virtual memory, and then commit/decommit pages at will. The idea at the time was to create a set containing an uncommitted page, followed by one or more committed pages (sufficient to fill the request), followed by another uncommitted page; with a compile-time flag that controlled whether the pointer returned to the caller was at the beginning or end of the committed region (to instantly fire an exception on an underwrite or overwrite, respectively). Windows provides an API for reserving memory regions and committing pages. I have another use for such a book-ended buffer in a Linux program I'm writing, and I'm wondering if the kernel provides the same kind of functionality.

2
  • The short answer is yes, Unix/Linux provides malloc (memory allocate) and free functions.  But it’s complicated, and I don’t really understand your objective, so I don’t know whether this will help you. Nov 21, 2017 at 1:52
  • 2
    @G-Man: malloc and free are totally different from the virtual memory commit in windows. Virtual memory is reserved via mmap in Linux, but to my knowledge there's no explicit commit step in Linux. But I do think one can implement guard pages, e.g. the golang runtime does that. But I don't know details.
    – dirkt
    Nov 21, 2017 at 18:46

2 Answers 2

9

I take it you’re referring to VirtualAlloc and friends. There’s no direct equivalent in Linux as far as I’m aware, but you can achieve the same effect in a variety of ways. If you want to reserve memory, you can use mmap(2); to commit it, you need to write to it; to decommit it, you can use madvise(2)’s MADV_DONTNEED.

To implement guard pages, you might find mprotect(2) to be a better option, along with a SIGSEGV handler. You can also look at the overflow protection provided by your compiler (-fstack-protector etc., and the various -fsanitize options).

0
0

If you simply want to debug your own programs, rather than implementing a complicated memory manager, I recommend you to simply use a program such as valgrind to detect memory leaks and dangling pointers.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .