Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/gen Switch atform allocations from malloc()+free() ...



details:   https://anonhg.NetBSD.org/src/rev/51048310251f
branches:  trunk
changeset: 744444:51048310251f
user:      kamil <kamil%NetBSD.org@localhost>
date:      Sat Feb 01 15:38:46 2020 +0000

description:
Switch atform allocations from malloc()+free() to mmap()+munmap()

This avoid bootstrapping malloc too early when libc+libpthread are not
ready. It is called through pthread__init() -> _pthread_atfork().

This also helps LLVM Leak Sanitizer to pacify false positive reports.

diffstat:

 lib/libc/gen/pthread_atfork.c |  23 ++++++++++++++++++-----
 1 files changed, 18 insertions(+), 5 deletions(-)

diffs (62 lines):

diff -r 75f5552919ea -r 51048310251f lib/libc/gen/pthread_atfork.c
--- a/lib/libc/gen/pthread_atfork.c     Sat Feb 01 15:33:48 2020 +0000
+++ b/lib/libc/gen/pthread_atfork.c     Sat Feb 01 15:38:46 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_atfork.c,v 1.10 2015/01/20 18:31:25 christos Exp $     */
+/*     $NetBSD: pthread_atfork.c,v 1.11 2020/02/01 15:38:46 kamil Exp $        */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -31,15 +31,17 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: pthread_atfork.c,v 1.10 2015/01/20 18:31:25 christos Exp $");
+__RCSID("$NetBSD: pthread_atfork.c,v 1.11 2020/02/01 15:38:46 kamil Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "namespace.h"
 
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/queue.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <sys/queue.h>
 #include "reentrant.h"
 
 #ifdef __weak_alias
@@ -73,11 +75,22 @@
 static struct atfork_callback *
 af_alloc(void)
 {
+       void *arena;
+       size_t sz;
 
        if (atfork_builtin.fn == NULL)
                return &atfork_builtin;
 
-       return malloc(sizeof(atfork_builtin));
+       /*
+        * Avoid using here malloc() as this function is used on early init
+        * and can prematuraly initialize the malloc library.
+        * malloc() allocations here also confuse the LLVM Leak Sanitizer.
+        */
+       sz = sizeof(atfork_builtin);
+       arena = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
+       if (arena == MAP_FAILED)
+               return NULL;
+       return arena;
 }
 
 static void
@@ -85,7 +98,7 @@
 {
 
        if (af != &atfork_builtin)
-               free(af);
+               munmap(af, sizeof(atfork_builtin));
 }
 
 int



Home | Main Index | Thread Index | Old Index