[PATCH 5/6] Add LSan annotation to ignore a memory leak

Peter Wu peter at lekensteyn.nl
Thu Jul 9 17:11:35 CEST 2015


* src/g10lib.h: Add annotate_leaked_object macro that ignores leaked
  objects. This avoids LSan from reporting deliberately leaked memory.
* mpi/mpiutil.c: Mark "constant" MPIs as leaked.

Signed-off-by: Peter Wu <peter at lekensteyn.nl>
---
I am not happy with the method to detect LSan availability, but here I
hope to solicit for some feedback. Should the code really be added to
mpiutil? Or let the caller (tests/mpitests.c) handle leaks?
---
 mpi/mpiutil.c |  5 ++++-
 src/g10lib.h  | 17 +++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/mpi/mpiutil.c b/mpi/mpiutil.c
index 71b3f1c..9a796c6 100644
--- a/mpi/mpiutil.c
+++ b/mpi/mpiutil.c
@@ -211,7 +211,10 @@ _gcry_mpi_free( gcry_mpi_t a )
   if (!a )
     return;
   if ((a->flags & 32))
-    return; /* Never release a constant. */
+    {
+      annotate_leaked_object(a);
+      return; /* Never release a constant. */
+    }
   if ((a->flags & 4))
     xfree( a->d );
   else
diff --git a/src/g10lib.h b/src/g10lib.h
index 50a08ec..5793f8c 100644
--- a/src/g10lib.h
+++ b/src/g10lib.h
@@ -88,6 +88,16 @@
 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
 #define DIMof(type,member)   DIM(((type *)0)->member)
 
+/* Detect LeakSanitizer (LSan) support for GCC and Clang based on the
+   availability of AddressSanitizer (ASAN).  */
+#ifdef __SANITIZE_ADDRESS__
+# define LEAK_SANITIZER
+#elif defined(__has_feature)
+# if __has_feature(address_sanitizer)
+#  define LEAK_SANITIZER
+# endif
+#endif
+
 
 
 /*-- src/global.c -*/
@@ -126,6 +136,13 @@ int   _gcry_is_secure (const void *a) _GCRY_GCC_ATTR_PURE;
 #define xstrdup(a)       _gcry_xstrdup ((a))
 #define xfree(a)         _gcry_free ((a))
 
+/* Allows "constant" MPIs to be annotated as memory leak.  */
+#ifdef LEAK_SANITIZER
+# include <sanitizer/lsan_interface.h>
+# define annotate_leaked_object(a)  __lsan_ignore_object((a))
+#else
+# define annotate_leaked_object(a)  do { } while (0)
+#endif
 
 /*-- src/misc.c --*/
 
-- 
2.4.4




More information about the Gcrypt-devel mailing list