如何抑制来自 gpgme 的 valgrind 警告?
Posted
技术标签:
【中文标题】如何抑制来自 gpgme 的 valgrind 警告?【英文标题】:How to suppress valgrind warnings from gpgme? 【发布时间】:2018-02-09 19:07:56 【问题描述】:我正在尝试在我的代码中使用 GPGME 来验证一些签名。然而,简单地初始化和释放 GPGME 上下文会导致 valgrind 发出大量警告。
例如:
#include <gpgme.h>
int main()
gpgme_check_version(NULL);
gpgme_ctx_t my_ctx;
gpgme_error_t gpg_err;
gpg_err = gpgme_new(&my_ctx);
gpgme_release(my_ctx);
return 0;
用gcc -lgpgme -lassuan -lgpg-error main.c
编译它,然后在valgrind 下运行它。从valgrind -v ./a.out
来看,这段代码似乎启动了 6 个不同的进程。我已经复制了相关的警告信息:
==19158== Warning: invalid file descriptor 1024 in syscall close()
==19158== at 0x558DBC4: close (in /usr/lib/libc-2.26.so)
==19158== by 0x4E65DF8: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E66D65: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E6736B: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E56572: gpgme_get_engine_info (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E56C87: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E6838F: gpgme_new (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x10880B: main (in /home/thedoctor/Programming/gpgmetest/mytest)
==19158== Warning: invalid file descriptor 1025 in syscall close()
==19158== at 0x558DBC4: close (in /usr/lib/libc-2.26.so)
==19158== by 0x4E65DF8: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E66D65: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E6736B: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E56572: gpgme_get_engine_info (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E56C87: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E6838F: gpgme_new (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x10880B: main (in /home/thedoctor/Programming/gpgmetest/mytest)
==19158== Warning: invalid file descriptor 1026 in syscall close()
==19158== at 0x558DBC4: close (in /usr/lib/libc-2.26.so)
==19158== by 0x4E65DF8: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E66D65: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E6736B: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E56572: gpgme_get_engine_info (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E56C87: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E6838F: gpgme_new (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x10880B: main (in /home/thedoctor/Programming/gpgmetest/mytest)
==19158== Warning: invalid file descriptor 1027 in syscall close()
==19158== Use --log-fd=<number> to select an alternative log fd.
==19158== at 0x558DBC4: close (in /usr/lib/libc-2.26.so)
==19158== by 0x4E65DF8: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E66D65: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E6736B: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E56572: gpgme_get_engine_info (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E56C87: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E6838F: gpgme_new (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x10880B: main (in /home/thedoctor/Programming/gpgmetest/mytest)
==19158== Warning: invalid file descriptor 1028 in syscall close()
==19158== at 0x558DBC4: close (in /usr/lib/libc-2.26.so)
==19158== by 0x4E65DF8: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E66D65: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E6736B: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E56572: gpgme_get_engine_info (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E56C87: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E6838F: gpgme_new (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x10880B: main (in /home/thedoctor/Programming/gpgmetest/mytest)
==19158== Warning: invalid file descriptor 1029 in syscall close()
==19158== at 0x558DBC4: close (in /usr/lib/libc-2.26.so)
==19158== by 0x4E65DF8: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E66D65: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E6736B: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E56572: gpgme_get_engine_info (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E56C87: ??? (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x4E6838F: gpgme_new (in /usr/lib/libgpgme.so.11.19.0)
==19158== by 0x10880B: main (in /home/thedoctor/Programming/gpgmetest/mytest)
总共对 6 个不同的进程重复了完全相同的一组警告。似乎 GPGME 试图关闭一些不属于自己的文件描述符。
我的问题有两个:
-
我是否使用了 gpgme API 错误?我还有什么需要做的吗?
如果 API 的使用正确,如何从 valgrind 抑制这些警告?
我似乎无法让 valgrind 对这些警告进行抑制。运行 valgrind --gen-suppressions=all -v ./a.out
不会在输出中显示任何抑制。我想这是因为这些是警告而不是真正的错误。我尝试自己写一个抑制,但我不确定应该是什么类型。通常,当我不得不写抑制时,它们是针对Memcheck:Leak
。但这些警告属于哪一类?
编辑:这是我尝试写的一个抑制:
gpgme-leak-fd
fun:close
...
fun:gpgme_new
编辑 2:我试图提供赏金,但它已过期。如果你对此有一个有效的答案,我提供 100 个代表的赏金。
【问题讨论】:
你可以写你自己的压制。见wiki.wxwidgets.org/Valgrind_Suppression_File_Howto 就像我提到的,我已经尝试过这样做。但 valgrind 似乎并不尊重这些。可能是因为我的语法不正确 【参考方案1】:以下是 Dan McGee 编写的 GPGME 规则,摘自here:
gpgme-static-get-engine-info
Memcheck:Leak
fun:malloc
...
fun:gpgme_get_engine_info
gpgme-static-set-engine-info
Memcheck:Leak
fun:malloc
...
fun:_gpgme_set_engine_info
fun:gpgme_set_engine_info
gpgme-static-set-locale
Memcheck:Leak
fun:malloc
fun:strdup
fun:gpgme_set_locale
【讨论】:
正如我在帖子中提到的,我试图抑制的不是内存泄漏,而是无效的文件描述符警告。以上是关于如何抑制来自 gpgme 的 valgrind 警告?的主要内容,如果未能解决你的问题,请参考以下文章