0

I'm trying to write unit tests for a C lib I've made.
To do so I'm using the lib criterion.

I would like to use valgrind to detect memory leaks and context error, when I run make test it generate a unit_test.o file that I can run throw valgrind and here is the result :

test: clean $(TEST_OBJ)
    $(CC) -o ${TEST_NAME} ${SRC} $(TEST_SRC) --coverage -lcriterion
    ./${TEST_NAME}

enter image description here

As you can see, I have 0 leaks and 0 context error which is not true as the tested code contains both. If I run the same code in the main function It will detect both leaks and contexts.

It looks like criterion execute the unit_tests in a kind of "safe" context and free all the memory itself.
My question is how can I test leaks and contexts from my criterion unit tests ? Or, what are the alternative to test it ?

nem0z
  • 1,060
  • 1
  • 4
  • 17
  • 2
    I'm not familiar with criterion, but I would hazard that it works by running tests in separate subprocesses / forks. Valgrind doesn't monitor subprocesses by default, but I think you can pass the `--trace-children=yes` to do so. – Brian61354270 Apr 03 '23 at 16:22

1 Answers1

2

You'll have to look at the documentation for Criterion — but the chances are high that the default mode launches tests in separate processes.

[…Time passeth…] The source code for the Snaipe/Criterion unit test framework is available on GitHub (at https://github.com/Snaipe/Criterion), and one of the features claimed on the home page is "Test [sic] are isolated in their own process, crashes and signals can be reported and tested".

However, Valgrind has options to help, notably:

  • --trace-children=no|yes Valgrind-ise child processes (follow execve)? [no].
  • --trace-children-skip=patt1,patt2,... specifies a list of executables that --trace-children=yes should not trace into.
  • --trace-children-skip-by-arg=patt1,patt2,... same as --trace-children-skip= but check the argv[] entries for children, rather than the exe name, to make a follow/no-follow decision.

Setting --trace-children=yes as an option to Valgrind should help you.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • This is really interesting, still doesn't detect leaks but it works for context errors. Probleme is that it never stop and I have to close my term to quit.. But this is a nice lead, thx – nem0z Apr 03 '23 at 16:39
  • Fix the errors, it might finish correctly then. – Paul Floyd Apr 04 '23 at 09:52