【问题标题】:how to use asan on a long-time-running server program?如何在长时间运行的服务器程序上使用 asan?
【发布时间】:2021-08-14 17:28:06
【问题描述】:

我有一个长时间运行的服务器程序,我想使用 asan 来检测这个程序的内存泄漏。 我的解决方案:

CXXFLAGS+="-fsanitize=address -fno-omit-frame-pointer -fsanitize-recover=address"
LDFLAGS+="-lasan"
# start the program
LD_PRELOAD=libasan.so.5 ASAN_OPTIONS=halt_on_error=false:alloc_dealloc_mismatch=0 ./bin/server > asan_report 2>&1 &

但似乎只报告一次 我希望它定期报告,怎么做?

【问题讨论】:

  • 你打算如何区分将来不会释放的内存和将来会释放但尚未释放的内存?在程序结束时,释放列表是最终的,您可以看到丢失的内容。但在一个项目中,ASAN 无法窥视未来并预测会发生什么。
  • 您的LDFLAGS 设置不正确,应该是LDFLAGS=-fsanitize=address(原始-lasan 可能有效但不可靠,Asan 开发人员不鼓励使用它)。
  • 而不是LD_PRELOAD=libasan.so.5 更好地做一个更通用和便携的LD_PRELOAD=$(gcc -print-file-name=libasan.so)

标签: c++ memory-leaks g++ address-sanitizer


【解决方案1】:

我希望它定期报告

您可以在调度循环中的适当时间致电__lsan_do_recoverable_leak_check();

来自lsan_interface.h

  // Check for leaks now. Returns zero if no leaks have been found or if leak
  // detection is disabled, non-zero otherwise.
  // This function may be called repeatedly, e.g. to periodically check a
  // long-running process. It prints a leak report if appropriate, but does not
  // terminate the process. It does not affect the behavior of
  // __lsan_do_leak_check() or the end-of-process leak check, and is not
  // affected by them.

这将重复打印相同的泄漏(没有“仅打印自上次调用以来的新泄漏”功能)。

附:既然你的程序已经和libasan链接了,那么LD_PRELOAD没有任何理由,而且有很多缺点。

【讨论】:

    【解决方案2】:

    鉴于您的最终目标是了解您的长期运行的程序是否正在经历不希望的增长,如果是,那么您应该考虑像 https://github.com/vmware/chap 这样的解决方案,它不需要检测您的程序。

    只需使用 gcore 或类似的工具定期获取程序的实时内核,如果您的程序运行多天,则可能每天在程序负载不重的时候获取。

    用 chap 打开每个内核,查看以下命令的结果:

    count leaked
    count writable
    

    如果 leaked 计数不为零,则您的程序存在某种泄漏,动态分配的内存变得无法访问。在这种情况下,https://github.com/vmware/chap/blob/master/USERGUIDE.md#analyzing-memory-leaks 给出了一些关于如何找出泄漏点的指示。

    如果 writable 计数比您预期的要大,或者似乎每次都在变大,那么您正在因为某些其他原因而增长,例如某种标准容器的增长。请参阅https://github.com/vmware/chap/blob/master/USERGUIDE.md#analyzing-memory-growth,了解在这种情况下如何进一步调查的一些想法。

    【讨论】:

      猜你喜欢
      • 2015-02-14
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      相关资源
      最近更新 更多