From patchwork Fri Jul 2 02:35:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1499847 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=b0Lw3xu9; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GGK7Y5Xw2z9sV8 for ; Fri, 2 Jul 2021 12:40:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 508A0397302F for ; Fri, 2 Jul 2021 02:40:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 508A0397302F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1625193623; bh=AAl9YrWAqHLdQCOtNNaUU0LQufIhIaBbajjpVr8jNpI=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=b0Lw3xu9R5mmq7hVTYMK6V8OD0wK2uZ0zjQ3S8NNjqXm99mJlmRhDL5N8KKXggDI6 rbUPOQptJOcM2RePwxPi/c82knq31nnYaSRmM2UKuEL+HaubaoMlT2yhitYqq4YtBr db3bPW+EiNMx2dkOcXNFCzNBH0lyy9sdAbZorL8s= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from butterfly.birch.relay.mailchannels.net (butterfly.birch.relay.mailchannels.net [23.83.209.27]) by sourceware.org (Postfix) with ESMTPS id AFD8E397302E for ; Fri, 2 Jul 2021 02:36:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AFD8E397302E X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 9BE50922287; Fri, 2 Jul 2021 02:36:23 +0000 (UTC) Received: from pdx1-sub0-mail-a42.g.dreamhost.com (100-96-18-98.trex.outbound.svc.cluster.local [100.96.18.98]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 2DEB79228C9; Fri, 2 Jul 2021 02:36:23 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a42.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.18.98 (trex/6.3.3); Fri, 02 Jul 2021 02:36:23 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Abortive-Shoe: 509d9186376e2a9b_1625193383453_2286551252 X-MC-Loop-Signature: 1625193383453:1340440266 X-MC-Ingress-Time: 1625193383453 Received: from pdx1-sub0-mail-a42.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a42.g.dreamhost.com (Postfix) with ESMTP id DEE64897DF; Fri, 2 Jul 2021 02:36:22 +0000 (UTC) Received: from rhbox.intra.reserved-bit.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a42.g.dreamhost.com (Postfix) with ESMTPSA id 0F934897E3; Fri, 2 Jul 2021 02:36:20 +0000 (UTC) X-DH-BACKEND: pdx1-sub0-mail-a42 To: libc-alpha@sourceware.org Subject: [PATCH v3 05/10] glibc.malloc.check: Wean away from malloc hooks Date: Fri, 2 Jul 2021 08:05:41 +0530 Message-Id: <20210702023546.3081774-6-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210702023546.3081774-1-siddhesh@sourceware.org> References: <20210702023546.3081774-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-3494.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Cc: fweimer@redhat.com Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Set up a new internal debugging hooks flag variable and migrate glibc.malloc.check to it so that it no longer uses the malloc hooks. Reviewed-by: DJ Delorie --- malloc/arena.c | 4 +-- malloc/hooks.c | 63 ++++++++++++++++++++++++++++++++++++++++++- malloc/malloc-check.c | 30 ++++++--------------- malloc/malloc.c | 9 +------ 4 files changed, 73 insertions(+), 33 deletions(-) diff --git a/malloc/arena.c b/malloc/arena.c index 1861d20006..357a3b0b30 100644 --- a/malloc/arena.c +++ b/malloc/arena.c @@ -210,7 +210,7 @@ TUNABLE_CALLBACK (set_mallopt_check) (tunable_val_t *valp) { int32_t value = (int32_t) valp->numval; if (value != 0) - __malloc_check_init (); + __malloc_debug_enable (MALLOC_CHECK_HOOK); } # define TUNABLE_CALLBACK_FNDECL(__name, __type) \ @@ -400,7 +400,7 @@ ptmalloc_init (void) } } if (s && s[0] != '\0' && s[0] != '0') - __malloc_check_init (); + __malloc_debug_enable (MALLOC_CHECK_HOOK); #endif #if HAVE_MALLOC_INIT_HOOK diff --git a/malloc/hooks.c b/malloc/hooks.c index 4960aafd08..77855801c8 100644 --- a/malloc/hooks.c +++ b/malloc/hooks.c @@ -21,6 +21,8 @@ corrupt pointer is detected: do nothing (0), print an error message (1), or call abort() (2). */ +# include + /* Define and initialize the hook variables. These weak definitions must appear before any use of the variables in a function (arena.c uses one). */ #ifndef weak_variable @@ -29,6 +31,14 @@ # define weak_variable weak_function #endif +/* The internal malloc debugging hooks. */ +enum malloc_debug_hooks +{ + MALLOC_NONE_HOOK = 0, + MALLOC_CHECK_HOOK = 1 << 0, /* MALLOC_CHECK_ or glibc.malloc.check. */ +}; +static unsigned __malloc_debugging_hooks; + /* Forward declarations. */ #if HAVE_MALLOC_INIT_HOOK @@ -48,6 +58,24 @@ void *weak_variable (*__memalign_hook) static void ptmalloc_init (void); +static __always_inline bool +__is_malloc_debug_enabled (enum malloc_debug_hooks flag) +{ + return __malloc_debugging_hooks & flag; +} + +static __always_inline void +__malloc_debug_enable (enum malloc_debug_hooks flag) +{ + __malloc_debugging_hooks |= flag; +} + +static __always_inline void +__malloc_debug_disable (enum malloc_debug_hooks flag) +{ + __malloc_debugging_hooks &= ~flag; +} + #include "malloc-check.c" static __always_inline bool @@ -63,6 +91,12 @@ _malloc_debug_before (size_t bytes, void **victimp, const void *address) *victimp = (*hook)(bytes, address); return true; } + + if (__glibc_unlikely (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))) + { + *victimp = malloc_check (bytes); + return true; + } return false; } @@ -76,6 +110,12 @@ _free_debug_before (void *mem, const void *address) (*hook)(mem, address); return true; } + + if (__glibc_unlikely (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))) + { + free_check (mem); + return true; + } return false; } @@ -91,6 +131,12 @@ _realloc_debug_before (void *oldmem, size_t bytes, void **victimp, return true; } + if (__glibc_unlikely (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))) + { + *victimp = realloc_check (oldmem, bytes); + return true; + } + return false; } @@ -105,6 +151,13 @@ _memalign_debug_before (size_t alignment, size_t bytes, void **victimp, *victimp = (*hook)(alignment, bytes, address); return true; } + + if (__glibc_unlikely (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))) + { + *victimp = memalign_check (alignment, bytes); + return true; + } + return false; } @@ -120,6 +173,14 @@ _calloc_debug_before (size_t bytes, void **victimp, const void *address) memset (*victimp, 0, bytes); return true; } + + if (__glibc_unlikely (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))) + { + *victimp = malloc_check (bytes); + if (*victimp != NULL) + memset (*victimp, 0, bytes); + return true; + } return false; } @@ -195,7 +256,7 @@ malloc_set_state (void *msptr) __realloc_hook = NULL; __free_hook = NULL; __memalign_hook = NULL; - using_malloc_checking = 0; + __malloc_debug_disable (MALLOC_CHECK_HOOK); /* Patch the dumped heap. We no longer try to integrate into the existing heap. Instead, we mark the existing chunks as mmapped. diff --git a/malloc/malloc-check.c b/malloc/malloc-check.c index dcab880510..a85e519498 100644 --- a/malloc/malloc-check.c +++ b/malloc/malloc-check.c @@ -18,20 +18,6 @@ not, see . */ -/* Whether we are using malloc checking. */ -static int using_malloc_checking; - -/* Activate a standard set of debugging hooks. */ -void -__malloc_check_init (void) -{ - using_malloc_checking = 1; - __malloc_hook = malloc_check; - __free_hook = free_check; - __realloc_hook = realloc_check; - __memalign_hook = memalign_check; -} - /* When memory is tagged, the checking data is stored in the user part of the chunk. We can't rely on the user not having modified the tags, so fetch the tag at each location before dereferencing @@ -69,7 +55,7 @@ malloc_check_get_size (mchunkptr p) unsigned char c; unsigned char magic = magicbyte (p); - assert (using_malloc_checking == 1); + assert (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK)); for (size = CHUNK_HDR_SZ + memsize (p) - 1; (c = *SAFE_CHAR_OFFSET (p, size)) != magic; @@ -203,7 +189,7 @@ top_check (void) } static void * -malloc_check (size_t sz, const void *caller) +malloc_check (size_t sz) { void *victim; size_t nb; @@ -222,7 +208,7 @@ malloc_check (size_t sz, const void *caller) } static void -free_check (void *mem, const void *caller) +free_check (void *mem) { mchunkptr p; @@ -256,7 +242,7 @@ free_check (void *mem, const void *caller) } static void * -realloc_check (void *oldmem, size_t bytes, const void *caller) +realloc_check (void *oldmem, size_t bytes) { INTERNAL_SIZE_T chnb; void *newmem = 0; @@ -269,11 +255,11 @@ realloc_check (void *oldmem, size_t bytes, const void *caller) return NULL; } if (oldmem == 0) - return malloc_check (bytes, NULL); + return malloc_check (bytes); if (bytes == 0) { - free_check (oldmem, NULL); + free_check (oldmem); return NULL; } @@ -348,12 +334,12 @@ invert: } static void * -memalign_check (size_t alignment, size_t bytes, const void *caller) +memalign_check (size_t alignment, size_t bytes) { void *mem; if (alignment <= MALLOC_ALIGNMENT) - return malloc_check (bytes, NULL); + return malloc_check (bytes); if (alignment < MINSIZE) alignment = MINSIZE; diff --git a/malloc/malloc.c b/malloc/malloc.c index 75ca6ec3f0..60753446a1 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -1124,13 +1124,6 @@ static void munmap_chunk(mchunkptr p); static mchunkptr mremap_chunk(mchunkptr p, size_t new_size); #endif -static void* malloc_check(size_t sz, const void *caller); -static void free_check(void* mem, const void *caller); -static void* realloc_check(void* oldmem, size_t bytes, - const void *caller); -static void* memalign_check(size_t alignment, size_t bytes, - const void *caller); - /* ------------------ MMAP support ------------------ */ @@ -5078,7 +5071,7 @@ musable (void *mem) p = mem2chunk (mem); - if (__builtin_expect (using_malloc_checking == 1, 0)) + if (__glibc_unlikely (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))) return malloc_check_get_size (p); if (chunk_is_mmapped (p))