mi-malloc 3.6/2.5/1.15
Loading...
Searching...
No Matches
Guarded Mode

mimalloc can be built in guarded mode using -DMI_GUARDED=ON with CMake. This is enabled by default in debug builds. Guarded mode places OS guard pages behind selected allocations to catch buffer overflows as they occur. On hardware supporting MTE, tagging can detect many such overflows more efficiently.

This can be invaluable to catch buffer-overflow bugs in large programs. However, it also means that any object allocated with a guard page takes at least 8 KiB memory for the guard page and its alignment. As such, allocating a guard page for every allocation may be too expensive both in terms of memory, and in terms of performance with many system calls. Therefore, there are various environment variables (and options) to tune this. When environment options are restricted (see Environment Options), use mi_option_set() instead.

  • MIMALLOC_GUARDED_SAMPLE_RATE=N: Set the sample rate to N (by default 0). This mode places a guard page behind a suitable object about every N requested bytes (per thread). Since the performance in guarded mode without placing guard pages is close to release mode, this can be used to enable guard pages even in production to catch latent buffer overflow bugs, for example with 16KiB or higher for a small performance impact. Set the sample rate to 1 to guard every object, and to 0 to place no guard pages at all.
  • MIMALLOC_GUARDED_SAMPLE_SEED=N: Start sampling at N (by default random). Can be used to reproduce a buffer overflow if needed.
  • MIMALLOC_GUARDED_MIN=N, MIMALLOC_GUARDED_MAX=N: Minimal and maximal rounded object sizes for which a guard page is considered (0 and 1GiB respectively). If you suspect a buffer overflow occurs with an object of size 141, set the minimum and maximum to 148 and the sample rate to 1 to have all of those guarded.