From patchwork Thu Aug 22 02:59:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Wangyang" X-Patchwork-Id: 1975217 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=OB6O6+Gn; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wq7Lz6Y4Cz1ybW for ; Thu, 22 Aug 2024 13:03:39 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2E8F53870C06 for ; Thu, 22 Aug 2024 03:03:38 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) by sourceware.org (Postfix) with ESMTPS id D79AC38708E8 for ; Thu, 22 Aug 2024 03:02:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D79AC38708E8 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D79AC38708E8 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.13 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724295760; cv=none; b=JlRUqSplGV96bFxlx0ZtOaAMVqclTRhcoZZ/JozEUnAasB74Pzxj1PDWYZgK8UnQmuCKOhj+Gh64GY6htH9SVxlo5LmQGzCq2txxGS3MVzFjH7aI/AMBiPC09fifWnYTegDMFRjVm1plnmNyYnGWfZRIesD4O9W0XXPCb9cFNBU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724295760; c=relaxed/simple; bh=qCQmdR5VtkceZps2nOaI9JWENe7XhR1cKy3ICZ0ChJw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=mBVH9DVdEVegTA1CoBeIOgmP9vzwzlePtj0kt1tledsoh3OR+0wdIectDdyIaHBejHGVelU6Y3ylxQB+bxzjmEgsXCsmm7EXbC65gVDdWYMMa1Fm11q82zF0lj6/wNtcUP3xBuCq48wN97SXs118T2kbQbzAJoj++qbrmWzMphw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724295759; x=1755831759; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qCQmdR5VtkceZps2nOaI9JWENe7XhR1cKy3ICZ0ChJw=; b=OB6O6+Gnv7LeOgHei9IyVrRGJ/5IgFcjbMB71kSpnKsCM81blW+BvbQS +jK5LiR7d9vM/xzIbDd/PLuMgyXbVEGBjME1Ii36qwTMxrBKsRGTBBDsW u4WqwyNWDGU1bAP5jc9mRrm5G44m0mdC+iKLoTqils2iPbXwjl+/KgpOs EQexNb36W4cIygPpShmVFVaSNrunXUeLi4aYl9VguSR8d7dVI2BWIVKDb eAWlvR0U+AKstuCbA7mH+KAei3AjyWqrj1PGZecf8FDKjehjvQyrtjW6I VUwErQkZsFFVmm4eb70IOC0A7dVaRaqknfgOm967dzC3hzeGCglXAe8ZR g==; X-CSE-ConnectionGUID: pOHJJyaXSiOzZT87C2xr2g== X-CSE-MsgGUID: TEFRcqThQXKCbEEjPj5vhg== X-IronPort-AV: E=McAfee;i="6700,10204,11171"; a="25581840" X-IronPort-AV: E=Sophos;i="6.10,165,1719903600"; d="scan'208";a="25581840" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Aug 2024 20:02:38 -0700 X-CSE-ConnectionGUID: y294rvrzQG+rD1nclYO7uw== X-CSE-MsgGUID: S0+ZpDoKT2i2x/RvlJ7m+g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,165,1719903600"; d="scan'208";a="66181846" Received: from linux-pnp-server-11.sh.intel.com ([10.239.176.178]) by orviesa005.jf.intel.com with ESMTP; 21 Aug 2024 20:02:37 -0700 From: Wangyang Guo To: libc-alpha@sourceware.org Cc: Noah Goldstein , Tianyou Li , Wangyang Guo Subject: [PATCH 6/6] malloc: Fix tst-safe-linking failure after enable tcache Date: Thu, 22 Aug 2024 10:59:21 +0800 Message-ID: <20240822025921.3120998-7-wangyang.guo@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240822025921.3120998-1-wangyang.guo@intel.com> References: <20240822025921.3120998-1-wangyang.guo@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org In previous, calloc() is used as a way to by-pass tcache in memory allocation and trigger safe-linking check in fastbins path. With tcache enabled in calloc(), it needs extra workarounds to bypass tcache. Signed-off-by: Wangyang Guo --- malloc/tst-safe-linking.c | 81 ++++++++++++++++++++++++++++++++------- 1 file changed, 68 insertions(+), 13 deletions(-) diff --git a/malloc/tst-safe-linking.c b/malloc/tst-safe-linking.c index 01dd07004d..5302575ad1 100644 --- a/malloc/tst-safe-linking.c +++ b/malloc/tst-safe-linking.c @@ -111,22 +111,37 @@ test_fastbin (void *closure) int i; int mask = ((int *)closure)[0]; size_t size = TCACHE_ALLOC_SIZE; + void * ps[TCACHE_FILL_COUNT]; + void * pps[TCACHE_FILL_COUNT]; printf ("++ fastbin ++\n"); + /* Populate the fastbin list. */ + void * volatile a = calloc (1, size); + void * volatile b = calloc (1, size); + void * volatile c = calloc (1, size); + printf ("a=%p, b=%p, c=%p\n", a, b, c); + + /* Chunks for later tcache filling from fastbins. */ + for (i = 0; i < TCACHE_FILL_COUNT; ++i) + { + void * volatile p = calloc (1, size); + pps[i] = p; + } + /* Take the tcache out of the game. */ for (i = 0; i < TCACHE_FILL_COUNT; ++i) { void * volatile p = calloc (1, size); - printf ("p=%p\n", p); - free (p); + ps[i] = p; } - /* Populate the fastbin list. */ - void * volatile a = calloc (1, size); - void * volatile b = calloc (1, size); - void * volatile c = calloc (1, size); - printf ("a=%p, b=%p, c=%p\n", a, b, c); + for (i = 0; i < TCACHE_FILL_COUNT; ++i) + { + free (ps[i]); + } + + /* Free abc will return to fastbin in FIFO order. */ free (a); free (b); free (c); @@ -136,11 +151,43 @@ test_fastbin (void *closure) memset (c, mask & 0xFF, size); printf ("After: c=%p, c[0]=%p\n", c, ((void **)c)[0]); + /* Filling fastbins, will be copied to tcache later. */ + for (i = 0; i < TCACHE_FILL_COUNT; ++i) + { + free (pps[i]); + } + + /* Drain out tcache to make sure later alloc from fastbins. */ + for (i = 0; i < TCACHE_FILL_COUNT; ++i) + { + void * volatile p = calloc (1, size); + ps[i] = p; + } + + /* This line will also filling tcache with remain pps and c. */ + pps[TCACHE_FILL_COUNT - 1] = calloc (1, size); + + /* Tcache is FILO, now the first one is c, take it out. */ c = calloc (1, size); printf ("Allocated: c=%p\n", c); + + /* Drain out remain pps from tcache. */ + for (i = 0; i < TCACHE_FILL_COUNT - 1; ++i) + { + void * volatile p = calloc (1, size); + pps[i] = p; + } + /* This line will trigger the Safe-Linking check. */ b = calloc (1, size); printf ("b=%p\n", b); + + /* Free previous pointers. */ + for (i = 0; i < TCACHE_FILL_COUNT; ++i) + { + free (ps[i]); + free (pps[i]); + } } /* Try corrupting the fastbin list and trigger a consolidate. */ @@ -150,21 +197,29 @@ test_fastbin_consolidate (void *closure) int i; int mask = ((int*)closure)[0]; size_t size = TCACHE_ALLOC_SIZE; + void * ps[TCACHE_FILL_COUNT]; printf ("++ fastbin consolidate ++\n"); + /* Populate the fastbin list. */ + void * volatile a = calloc (1, size); + void * volatile b = calloc (1, size); + void * volatile c = calloc (1, size); + printf ("a=%p, b=%p, c=%p\n", a, b, c); + /* Take the tcache out of the game. */ for (i = 0; i < TCACHE_FILL_COUNT; ++i) { void * volatile p = calloc (1, size); - free (p); + ps[i] = p; } - /* Populate the fastbin list. */ - void * volatile a = calloc (1, size); - void * volatile b = calloc (1, size); - void * volatile c = calloc (1, size); - printf ("a=%p, b=%p, c=%p\n", a, b, c); + for (i = 0; i < TCACHE_FILL_COUNT; ++i) + { + free (ps[i]); + } + + /* Free abc will return to fastbin. */ free (a); free (b); free (c);