9832fd
commit 7a9368a1174cb15b9f1d6342e0e10dd90dae238d
9832fd
Author: Florian Weimer <fweimer@redhat.com>
9832fd
Date:   Wed Nov 15 11:39:01 2017 +0100
9832fd
9832fd
    malloc: Add missing arena lock in malloc_info [BZ #22408]
9832fd
    
9832fd
    Obtain the size information while the arena lock is acquired, and only
9832fd
    print it later.
9832fd
9832fd
Conflicts:
9832fd
	malloc/Makefile
9832fd
	  (Differences in available tests.)
9832fd
9832fd
diff --git a/malloc/Makefile b/malloc/Makefile
9832fd
index 992cec6b03115a76..6f216f423293dc6c 100644
9832fd
--- a/malloc/Makefile
9832fd
+++ b/malloc/Makefile
9832fd
@@ -31,6 +31,7 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
9832fd
 	 tst-malloc-thread-fail tst-malloc-fork-deadlock \
9832fd
 	 tst-interpose-nothread \
9832fd
 	 tst-interpose-thread \
9832fd
+	 tst-malloc_info \
9832fd
 	 tst-interpose-static-nothread \
9832fd
 	 tst-interpose-static-thread \
9832fd
 	 tst-scratch_buffer \
9832fd
@@ -214,3 +215,5 @@ $(objpfx)tst-dynarray-mem: $(objpfx)tst-dynarray.out
9832fd
 tst-dynarray-fail-ENV = MALLOC_TRACE=$(objpfx)tst-dynarray-fail.mtrace
9832fd
 $(objpfx)tst-dynarray-fail-mem: $(objpfx)tst-dynarray-fail.out
9832fd
 	$(common-objpfx)malloc/mtrace $(objpfx)tst-dynarray-fail.mtrace > $@
9832fd
+
9832fd
+$(objpfx)tst-malloc_info: $(shared-thread-library)
9832fd
diff --git a/malloc/malloc.c b/malloc/malloc.c
9832fd
index d2a5e251da4f1191..035f2167be7019d8 100644
9832fd
--- a/malloc/malloc.c
9832fd
+++ b/malloc/malloc.c
9832fd
@@ -5108,6 +5108,15 @@ __malloc_info (int options, FILE *fp)
9832fd
 	  avail += sizes[NFASTBINS - 1 + i].total;
9832fd
 	}
9832fd
 
9832fd
+      size_t heap_size = 0;
9832fd
+      size_t heap_mprotect_size = 0;
9832fd
+      if (ar_ptr != &main_arena)
9832fd
+	{
9832fd
+	  heap_info *heap = heap_for_ptr (top (ar_ptr));
9832fd
+	  heap_size = heap->size;
9832fd
+	  heap_mprotect_size = heap->mprotect_size;
9832fd
+	}
9832fd
+
9832fd
       __libc_lock_unlock (ar_ptr->mutex);
9832fd
 
9832fd
       total_nfastblocks += nfastblocks;
9832fd
@@ -5141,13 +5150,12 @@ __malloc_info (int options, FILE *fp)
9832fd
 
9832fd
       if (ar_ptr != &main_arena)
9832fd
 	{
9832fd
-	  heap_info *heap = heap_for_ptr (top (ar_ptr));
9832fd
 	  fprintf (fp,
9832fd
 		   "<aspace type=\"total\" size=\"%zu\"/>\n"
9832fd
 		   "<aspace type=\"mprotect\" size=\"%zu\"/>\n",
9832fd
-		   heap->size, heap->mprotect_size);
9832fd
-	  total_aspace += heap->size;
9832fd
-	  total_aspace_mprotect += heap->mprotect_size;
9832fd
+		   heap_size, heap_mprotect_size);
9832fd
+	  total_aspace += heap_size;
9832fd
+	  total_aspace_mprotect += heap_mprotect_size;
9832fd
 	}
9832fd
       else
9832fd
 	{
9832fd
diff --git a/malloc/tst-malloc_info.c b/malloc/tst-malloc_info.c
9832fd
new file mode 100644
9832fd
index 0000000000000000..a25b8cbeae78e710
9832fd
--- /dev/null
9832fd
+++ b/malloc/tst-malloc_info.c
9832fd
@@ -0,0 +1,101 @@
9832fd
+/* Smoke test for malloc_info.
9832fd
+   Copyright (C) 2017 Free Software Foundation, Inc.
9832fd
+   This file is part of the GNU C Library.
9832fd
+
9832fd
+   The GNU C Library is free software; you can redistribute it and/or
9832fd
+   modify it under the terms of the GNU Lesser General Public
9832fd
+   License as published by the Free Software Foundation; either
9832fd
+   version 2.1 of the License, or (at your option) any later version.
9832fd
+
9832fd
+   The GNU C Library is distributed in the hope that it will be useful,
9832fd
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
9832fd
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9832fd
+   Lesser General Public License for more details.
9832fd
+
9832fd
+   You should have received a copy of the GNU Lesser General Public
9832fd
+   License along with the GNU C Library; if not, see
9832fd
+   <http://www.gnu.org/licenses/>.  */
9832fd
+
9832fd
+/* The purpose of this test is to provide a quick way to run
9832fd
+   malloc_info in a multi-threaded process.  */
9832fd
+
9832fd
+#include <array_length.h>
9832fd
+#include <malloc.h>
9832fd
+#include <stdlib.h>
9832fd
+#include <support/support.h>
9832fd
+#include <support/xthread.h>
9832fd
+
9832fd
+/* This barrier is used to have the main thread wait until the helper
9832fd
+   threads have performed their allocations.  */
9832fd
+static pthread_barrier_t barrier;
9832fd
+
9832fd
+enum
9832fd
+  {
9832fd
+    /* Number of threads performing allocations.  */
9832fd
+    thread_count  = 4,
9832fd
+
9832fd
+    /* Amount of memory allocation per thread.  This should be large
9832fd
+       enough to cause the allocation of multiple heaps per arena.  */
9832fd
+    per_thread_allocations
9832fd
+      = sizeof (void *) == 4 ? 16 * 1024 * 1024 : 128 * 1024 * 1024,
9832fd
+  };
9832fd
+
9832fd
+static void *
9832fd
+allocation_thread_function (void *closure)
9832fd
+{
9832fd
+  struct list
9832fd
+  {
9832fd
+    struct list *next;
9832fd
+    long dummy[4];
9832fd
+  };
9832fd
+
9832fd
+  struct list *head = NULL;
9832fd
+  size_t allocated = 0;
9832fd
+  while (allocated < per_thread_allocations)
9832fd
+    {
9832fd
+      struct list *new_head = xmalloc (sizeof (*new_head));
9832fd
+      allocated += sizeof (*new_head);
9832fd
+      new_head->next = head;
9832fd
+      head = new_head;
9832fd
+    }
9832fd
+
9832fd
+  xpthread_barrier_wait (&barrier);
9832fd
+
9832fd
+  /* Main thread prints first statistics here.  */
9832fd
+
9832fd
+  xpthread_barrier_wait (&barrier);
9832fd
+
9832fd
+  while (head != NULL)
9832fd
+    {
9832fd
+      struct list *next_head = head->next;
9832fd
+      free (head);
9832fd
+      head = next_head;
9832fd
+    }
9832fd
+
9832fd
+  return NULL;
9832fd
+}
9832fd
+
9832fd
+static int
9832fd
+do_test (void)
9832fd
+{
9832fd
+  xpthread_barrier_init (&barrier, NULL, thread_count + 1);
9832fd
+
9832fd
+  pthread_t threads[thread_count];
9832fd
+  for (size_t i = 0; i < array_length (threads); ++i)
9832fd
+    threads[i] = xpthread_create (NULL, allocation_thread_function, NULL);
9832fd
+
9832fd
+  xpthread_barrier_wait (&barrier);
9832fd
+  puts ("info: After allocation:");
9832fd
+  malloc_info (0, stdout);
9832fd
+
9832fd
+  xpthread_barrier_wait (&barrier);
9832fd
+  for (size_t i = 0; i < array_length (threads); ++i)
9832fd
+    xpthread_join (threads[i]);
9832fd
+
9832fd
+  puts ("\ninfo: After deallocation:");
9832fd
+  malloc_info (0, stdout);
9832fd
+
9832fd
+  return 0;
9832fd
+}
9832fd
+
9832fd
+#include <support/test-driver.c>