How to determine if a program is running from valgrind?

Is there a way to identify at runtime an executable that runs from valgrind? I have a set of unit tests in C ++, and one of them expects std::vector::reserveto quit std::bad_alloc. When I run this under valgrind, it is completely freed up, preventing me from testing memory leak (using valgrind) and behavior (expecting an exception).

Here is a minimal example that reproduces it:

#include <vector>
int main()
{
    size_t uint_max = static_cast<size_t>(-1);
    std::vector<char> v;
    v.reserve(uint_max);
}

Running valgrind, I get this output:

Warning: silly arg (-1) to __builtin_new()
new/new[] failed and should throw an exception, but Valgrind
   cannot throw exceptions and so is aborting instead.  Sorry.
   at 0x40192BC: VALGRIND_PRINTF_BACKTRACE (valgrind.h:319)
   by 0x401C823: operator new(unsigned) (vg_replace_malloc.c:164)
   by 0x80487BF: std::vector<char, std::allocator<char> >::reserve(unsigned) new_allocator.h:92)
   by 0x804874D: main (vg.cxx:6)

I would like to modify my unit test to just skip the violation code when it is run from valgrind. Is it possible?

+5
source share
2 answers

Valgrind, RUNNING_ON_VALGRIND ( valgrind.h), , .

+16

valgrind . , :

  • , valgrind .

  • , , ( ) :

    MYAPP_UNIT_TESTS_DISABLED="NEW_MINUS_ONE,FLY_TO_MOON,DEREF_NULL" valgrind myapp
    

bool unit_test_enabled(const char *testname);

unit test , getenv (3).

0

All Articles