Page Checked Heap


Overview

The page-checked heap can be used to help track down bugs caused by incorrect use of memory buffers that have been allocated from the heap. These bugs manifest themselves in obscure ways, often in unrelated parts of the software, and can be difficult to identify.

The page checked heap uses two sets of memory pages for each allocation. The first set of pages is used to allocate the buffer, the second is an inaccesible guard page that is positioned immediately after the end of the memory buffer. Should an attempt be made to read or write beyond the end of the buffer then an exception will be triggered when the kernel detects access to the guard page. Thus KCML will crash at the point the bug occurred, rather than sometime later.


System Requirements & Configuration

The page checked heap is only supported in the 64-bit Linux version of KCML 7.20, or later. It is recommended to run the page checked heap on server that is running at least a Linux 3.8 kernel as the virtual memory management of earlier 2.6 kernels is far less effecient.
Ensure the operating system has been configured to allow a large number of memory mapped regions by setting the kernel limit in /etc/sysctl.conf:

# For page-checked heap
vm.max_map_count = 2097152

And then reboot the server.


Modes of operation

The page-checked heap can be enabled in one of three ways. Command-line flag: A simple program can be run from a command shell using the --heap-pagecheck flag.
For example:-

$ kcml --heap-pagecheck=1 -p myprog.src

An application running under the Connection Manager can be switched, without changing the configuration, to use the page checked heap, by using KClient's --param command-line flag.

$ kclient.exe [email protected] -v MyApp -R --param heap-pagecheck=1

However, there may not be enough resouces available to run complex applications, see Limitations

Alternatively, the $KCML_HEAP_PAGECHECK environment variable can set to a value of "1"

$ KCML_HEAP_PAGECHECK=1
$ export KCML_HEAP_PAGECHECK
$ kcml -p myprog.src

Limitations

The page checked heap is expensive both in memory usage and CPU time, so it is only intended for internal development systems. 64-bit versions of KCML are still restricted to a 32-bit address range for their heap, so applications that make large numbers of allocations may fail as there are not enough memory pages available to fit in the limited address space. The kc & kc6 compilers do make large numbers of allocations so it is not recommended to compile complex applications with the page checked heap enabled.