1

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?

Conme
  • 31
  • 4
  • 1
    Might you try building in code to simulate out-of-memory? Maybe the wrapper that allocates your structures maintains an internal counter, and after (say) 500 allocations is starts returning null? – Steve Friedl Nov 30 '19 at 18:55
  • 1
    The problem is that the memory limiters work by replacing the malloc function much like valgrind does. This means that when you run valgrind it then replaces the malloc function again and that malloc function calls straight into the kernel as such bypassing the malloc function of the memory limiter. – txk2048 Nov 30 '19 at 18:57
  • @Toothless204 sure, but from my reading of the question, it's not about behavior of the memory system, but the behavior of the *application*; if the `newBlah` function - which normally calls `malloc` - instead starts simulating failure, then this exercises the applications out-of-memory behavior. Perhaps I misunderstand the question. – Steve Friedl Nov 30 '19 at 19:00
  • @SteveFriedl Thank you for your suggestion. That is indeed a way to test it and this is how I am more or less dealing with it so far. Though I am curious whether there is a more structured global way to do it. – Conme Dec 02 '19 at 14:53

0 Answers0