[PATCH v2] Add function gpgrt_annotate_leaked_object.

Peter Wu peter at lekensteyn.nl
Wed Mar 23 23:23:06 CET 2016


* src/gpg-error.h.in: add gpgrt_annotate_leaked_object to support
  marking memory as non-leaked for Clang and GCC.
--
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,

I doubt that other compilers set __SANITIZE_ADDRESS__, but have added
the __GNUC__ guard just to be sure.

(This patch is tested with both GCC 5.3 and Clang 3.7.1 against mpitests
from libgcrypt with -fsanitize=address.)

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..5138d0e 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.  */
+#if defined (__GNUC__) && defined( __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