In order to test a software in limit conditions, I'm trying to create a test case where the provided user buffer is allocated at some very low memory address. Something very close to NULL
, like for example 0x1000h
.
This is proving a tough condition to create.
Actually, I'm unable to generate that with malloc()
on Linux, BSD, Windows, or OS-X.
I'm convinced this situation can happen on other types of devices, but I need a reproducible test case that can be inserted into a CI test suite.
Is there any known method with moderate complexity (and dependencies) to generate such conditions ?
Edit : Selected the solution proposed by Eric Postpischil, using mmap()
. Note that, as underlined by R., it's first necessary to lower the lowest address limit, readable at /proc/sys/vm/mmap_min_addr
(on Linux).
sudo sysctl -w vm.mmap_min_addr="4096"
Then the example code :
#include <stdio.h> /* printf */
#include <sys/mman.h> /* mmap */
int main(int argc, const char** argv)
{
void* lowBuff = mmap((void*)(0x1000), 64<<10,
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
printf("lowBuff starts at %p \n", lowBuff);
}
And the result :
lowBuff starts at 0x1000