Valgind:仅当内存确实丢失时才出现非零退出代码

Vit*_*aev 6 valgrind memory-leaks exit-code

我们将valgrind其用作 CI 流程的一部分。如果存在一些内存问题,valgrind必须返回非零代码,并且报告此事件。这就是我们运行它的方式:

valgrind --error-exitcode=1 --tool=memcheck --leak-check=full \
    --errors-for-leak-kinds=definite --show-leak-kinds=definite \
    --track-origins=yes ./some_cgo_application

(...)

==25182== HEAP SUMMARY:
==25182==     in use at exit: 2,416,970 bytes in 34,296 blocks
==25182==   total heap usage: 83,979 allocs, 49,684 frees, 5,168,335 bytes allocated
==25182== 
==25182== LEAK SUMMARY:
==25182==    definitely lost: 0 bytes in 0 blocks
==25182==    indirectly lost: 0 bytes in 0 blocks
==25182==      possibly lost: 3,024 bytes in 7 blocks
==25182==    still reachable: 2,413,946 bytes in 34,289 blocks
==25182==                       of which reachable via heuristic:
==25182==                         newarray           : 520 bytes in 1 blocks
==25182==         suppressed: 0 bytes in 0 blocks
==25182== Reachable blocks (those to which a pointer was found) are not shown.
==25182== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==25182== 
==25182== For counts of detected and suppressed errors, rerun with: -v
==25182== ERROR SUMMARY: 20 errors from 5 contexts (suppressed: 0 from 0)

Run Code Online (Sandbox Code Playgroud)

目前我们只对内存感兴趣definitly lost。如果没有明确丢失块,valgrind则 的退出代码预计为零。然而,1尽管有选项,它还是会返回--errors-for-leak-kinds=definite --show-leak-kinds=definite

echo $?
1
Run Code Online (Sandbox Code Playgroud)

还有其他选择有助于实现预期结果吗?

Flo*_*mer 5

我怀疑退出状态 1 来自程序本身。我可以用以下方法重现这个:

$ valgrind --error-exitcode=1 --tool=memcheck --leak-check=full \
  --errors-for-leak-kinds=definite --show-leak-kinds=definite \
  --track-origins=yes /bin/false
Run Code Online (Sandbox Code Playgroud)

这看起来不像是可以在当前源中更改的内容:

   case VgSrc_ExitProcess: /* the normal way out (Darwin) */
      /* Change the application return code to user's return code,
         if an error was found */
      if (VG_(clo_error_exitcode) > 0 
          && VG_(get_n_errs_found)() > 0) {
         VG_(client_exit)( VG_(clo_error_exitcode) );
      } else {
         /* otherwise, return the client's exit code, in the normal
            way. */
         VG_(client_exit)( VG_(threads)[tid].os_state.exitcode );
      }
Run Code Online (Sandbox Code Playgroud)

这个exitcode成员是从sys_exit_group中的包装器设置的coregrind/m_syswrap/syswrap-linux.c,没有任何方法可以调整它。

考虑到这一点,我认为最好的选择(不修补 valgrind)是选择与程序可能使用的任何退出状态不同的退出状态,并将其用作 valgrind 错误的指示符。