[PATCH] Add function gpgrt_annotate_leaked_object.

Peter Wu peter at lekensteyn.nl
Wed Mar 23 17:47:03 CET 2016


* src/gpg-error.h.in: add gpgrt_annotate_leaked_object to support
  marking memory as non-leaked.
--
This annotation can be used to mark objects as explicitly leaked such
that it can be ignored in tools like LeakSanitizer.

The GPGRT_HAVE_LEAK_SANITIZER macro is explicitly not undefined to
support -fsanitize=leak, a user or configure script could then decide to
add this macro when just -fsanitize=leak is given.

Signed-off-by: Peter Wu <peter at lekensteyn.nl>
---
Hi,

This is a follow-up of an earlier version that was proposed last year for
libgcrypt
(https://lists.gnupg.org/pipermail/gcrypt-devel/2015-July/003471.html).

Previously a macro was used, this version proposes an inline function for type
checking and keeps the door open for supporting other tools in the future
(Valgrind's memcheck?).

This patch was tested on libgcrypt by applying the same change to mputil.c as
before.

Other references:
 - GCC post showing that -fsanitize=leak only affects the linker options:
   https://gcc.gnu.org/ml/gcc-patches/2013-11/msg01874.html
 - GCC documentation on __SANITIZE_ADDRESS__:
   https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
 - Clang documentation on __has_feature(address_sanitizer):
   http://clang.llvm.org/docs/AddressSanitizer.html#id10
 - Earlier question on detecting LSan (no good solution unfortunately):
   http://stackoverflow.com/q/31273016

Kind regards,
Peter
---
 src/gpg-error.h.in | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/src/gpg-error.h.in b/src/gpg-error.h.in
index b32b4c4..d031925 100644
--- a/src/gpg-error.h.in
+++ b/src/gpg-error.h.in
@@ -246,10 +246,36 @@ typedef unsigned int gpg_error_t;
 # define GPGRT_HAVE_PRAGMA_GCC_PUSH 1
 #endif
 
+/* Detect LeakSanitizer (LSan) support for GCC and Clang based on
+   whether AddressSanitizer (ASAN) is enabled via -fsanitize=address).
+   Note that -fsanitize=leak just affect the linker options which cannot
+   be detected here. In that case you have to define the
+   GPGRT_HAVE_LEAK_SANITIZER macro manually.  */
+#ifdef __SANITIZE_ADDRESS__
+# define GPGRT_HAVE_LEAK_SANITIZER
+#elif defined(__has_feature)
+# if __has_feature(address_sanitizer)
+#  define GPGRT_HAVE_LEAK_SANITIZER
+# endif
+#endif
+
 
 /* The new name for the inline macro.  */
 #define GPGRT_INLINE GPG_ERR_INLINE
 
+#ifdef GPGRT_HAVE_LEAK_SANITIZER
+# include <sanitizer/lsan_interface.h>
+#endif
+
+/* Mark heap objects as non-leaked memory. */
+static GPGRT_INLINE void
+gpgrt_annotate_leaked_object(const void *p)
+{
+#ifdef GPGRT_HAVE_LEAK_SANITIZER
+    __lsan_ignore_object(p);
+#endif
+}
+
 
 /* Initialization function.  */
 
-- 
2.7.4




More information about the Gnupg-devel mailing list