misc: address sanitizer: fix instrumentation
[vpp.git] / src / vppinfra / mem_dlmalloc.c
index e83e0e8..68901a5 100644 (file)
@@ -19,6 +19,7 @@
 #include <vppinfra/lock.h>
 #include <vppinfra/hash.h>
 #include <vppinfra/elf_clib.h>
+#include <vppinfra/sanitizer.h>
 
 void *clib_per_cpu_mheaps[CLIB_MAX_MHEAPS];
 
@@ -56,6 +57,10 @@ typedef struct
 
   /* Hash table mapping mheap offset to trace index. */
   uword *trace_index_by_offset;
+
+  /* So we can easily shut off current segment trace, if any */
+  void *current_traced_mheap;
+
 } mheap_trace_main_t;
 
 mheap_trace_main_t mheap_trace_main;
@@ -69,7 +74,7 @@ mheap_get_trace (uword offset, uword size)
   mheap_trace_t trace;
   uword save_enabled;
 
-  if (tm->enabled == 0)
+  if (tm->enabled == 0 || (clib_mem_get_heap () != tm->current_traced_mheap))
     return;
 
   /* Spurious Coverity warnings be gone. */
@@ -80,11 +85,6 @@ mheap_get_trace (uword offset, uword size)
   if (n_callers == 0)
     return;
 
-  /* $$$ This looks like dreck to remove... */
-  if (0)
-    for (i = n_callers; i < ARRAY_LEN (trace.callers); i++)
-      trace.callers[i] = 0;
-
   clib_spinlock_lock (&tm->lock);
 
   /* Turn off tracing to avoid embarrassment... */
@@ -215,6 +215,8 @@ clib_mem_init (void *memory, uword memory_size)
   else
     heap = create_mspace (memory_size, 1 /* locked */ );
 
+  CLIB_MEM_POISON (mspace_least_addr (heap), mspace_footprint (heap));
+
   clib_mem_set_heap (heap);
 
   if (mheap_trace_main.lock == 0)
@@ -291,7 +293,8 @@ format_mheap_trace (u8 * s, va_list * va)
   int i;
 
   clib_spinlock_lock (&tm->lock);
-  if (vec_len (tm->traces) > 0)
+  if (vec_len (tm->traces) > 0 &&
+      clib_mem_get_heap () == tm->current_traced_mheap)
     {
       have_traces = 1;
 
@@ -372,7 +375,8 @@ format_mheap (u8 * s, va_list * va)
        format (s, "\n    max total allocated %U", format_msize, mi.usmblks);
     }
 
-  s = format (s, "\n%U", format_mheap_trace, tm, verbose);
+  if (mspace_is_traced (heap))
+    s = format (s, "\n%U", format_mheap_trace, tm, verbose);
   return s;
 }
 
@@ -382,6 +386,21 @@ clib_mem_usage (clib_mem_usage_t * u)
   clib_warning ("unimp");
 }
 
+void
+mheap_usage (void *heap, clib_mem_usage_t * usage)
+{
+  struct dlmallinfo mi = mspace_mallinfo (heap);
+
+  /* TODO: Fill in some more values */
+  usage->object_count = 0;
+  usage->bytes_total = mi.arena;
+  usage->bytes_overhead = 0;
+  usage->bytes_max = 0;
+  usage->bytes_used = mi.uordblks;
+  usage->bytes_free = mi.fordblks;
+  usage->bytes_free_reclaimed = 0;
+}
+
 /* Call serial number for debugger breakpoints. */
 uword clib_mem_validate_serial = 0;
 
@@ -404,9 +423,21 @@ void
 clib_mem_trace (int enable)
 {
   mheap_trace_main_t *tm = &mheap_trace_main;
+  void *current_heap = clib_mem_get_heap ();
 
   tm->enabled = enable;
-  mheap_trace (clib_mem_get_heap (), enable);
+  mheap_trace (current_heap, enable);
+
+  if (enable)
+    tm->current_traced_mheap = current_heap;
+  else
+    tm->current_traced_mheap = 0;
+}
+
+int
+clib_mem_is_traced (void)
+{
+  return mspace_is_traced (clib_mem_get_heap ());
 }
 
 uword