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