I have written a piece of C code, which dynamically allocates some structures, the one after the other. When any of the allocations fails, some of the previously allocated ones have to be freed. While I am able to successfully memcheck the application with no issues when every allocation succeeds, I have not been able to figure out how to use valgrind's memcheck to check the scenarios when any of the allocations fails.
When trying to impose a memory limit externally for the allocations to fail, using ulimit or a virtual machine for example, the limit is imposed to valgrind as well, with it failing as a result. Using tools like qemu make the memory behavior of the executable invisible to valgrind and only the tool's behavior is profiled.
When trying to impose it inside the code using rlimit structs, while it is successfully imposed when the program is executed in isolation and the allocations fail as expected, when run by valgrind it is ignored and the allocations succeed regardless of its value.
The ideal scenario would be for valgrind to allow for a memory restriction, but such an option does not seem to exist. Any proposals on how to memcheck for such memory limited scenarios?