From patchwork Fri Mar 19 13:26:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Szabolcs Nagy X-Patchwork-Id: 1455853 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@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=EqsQVEa1; 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 4F24S36Cj6z9sRf for ; Sat, 20 Mar 2021 00:26:59 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5B940386F464; Fri, 19 Mar 2021 13:26:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5B940386F464 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1616160414; bh=xgcsNrd2GfBa6ArB28NhvEu7fvKfgVBCJd7TUeOvEg4=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=EqsQVEa1IyS15JdqmLy265yKxXMM6mdUzks+dm7C/uMHhpfuWdYcsktA1sGAntcvo M8DRjMXAPqZV19Ky1sNgVOaq1PPvHosZeaGySV6fLgGGSrp7aqfC2U89aU4JuoVzsP xyJbVCcmuFc2tBMydTeDCPn6O6u4/r8IJFEP80ig= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-eopbgr70059.outbound.protection.outlook.com [40.107.7.59]) by sourceware.org (Postfix) with ESMTPS id 44F1D385142A for ; Fri, 19 Mar 2021 13:26:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 44F1D385142A Received: from AM5PR1001CA0072.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:206:15::49) by PAXPR08MB6512.eurprd08.prod.outlook.com (2603:10a6:102:15a::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.18; Fri, 19 Mar 2021 13:26:42 +0000 Received: from AM5EUR03FT039.eop-EUR03.prod.protection.outlook.com (2603:10a6:206:15:cafe::98) by AM5PR1001CA0072.outlook.office365.com (2603:10a6:206:15::49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.18 via Frontend Transport; Fri, 19 Mar 2021 13:26:42 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; sourceware.org; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;sourceware.org; dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM5EUR03FT039.mail.protection.outlook.com (10.152.17.185) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.18 via Frontend Transport; Fri, 19 Mar 2021 13:26:41 +0000 Received: ("Tessian outbound 04b74cf98e3c:v87"); Fri, 19 Mar 2021 13:26:41 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 7dba612785a6d8ef X-CR-MTA-TID: 64aa7808 Received: from e01bfd2c6870.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 51E054B5-6D25-4127-BB9B-F3D3F197EE79.1; Fri, 19 Mar 2021 13:26:22 +0000 Received: from EUR02-AM5-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id e01bfd2c6870.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Fri, 19 Mar 2021 13:26:22 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=B3R/KCGnMemJgTZI52oa4K/u51I3/RUIV37pocCRWUvFmtZqs7168KYgHPpLHFZiYnPiKC+0aKRqg/4QlEVbiNSCW9kM+yYU8m+C4+ZztbkF0I7nMsndX5ntHVzZuIteyg9rAcN+ZL0xsJcM9O5leEE71v7fM9UI0vAmbK6hV3CdHEs/ls8uLfFJFOojmIRZl9dHZ5Bl4iZBWe1N9mZhoIzNmN1k5q0TgZ0Lp+k9I5KocmzzZQUaXu0bUwQGv2lug30X/h/ZEj2hGg4QjcjmgQ8CF57BAPSKJE7mlvYJ0+T6JcPm7fpsoeedgiew/VoQyITTegwzaA2KlPUyffQpPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=xgcsNrd2GfBa6ArB28NhvEu7fvKfgVBCJd7TUeOvEg4=; b=M7UcAeGWmvpHqNYmSosl4YbNI6mvn0zDeQP105p8H+ZVwWL6OPJXqDrYokSDOnKEWxaR2b3t/WpluUPVYjURQNtJNSrZsyB7532sp/CW+7/7czjOCG/w7Xvd8X/lysklOSdrVzHxTFMKPA8wDdmevKt/TKS6GGlMI6LbFYnx+C2tYqWOGRKmXE0wYb2ITk2s0UBcEP5SaX2bWOha7c8ieCXfmqVofRqLGeCoXy63zdKKjaPRYP8zEDgjNZ+M+1qVWp+MCPwjasf+s9JuSgLcqFZ3c/xgc36eMi2i1fluWGmu1nOGQBGYOU/VPF0zQBsjYVTDIj2zF9l3XrZLr/aIWQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none Authentication-Results-Original: sourceware.org; dkim=none (message not signed) header.d=none;sourceware.org; dmarc=none action=none header.from=arm.com; Received: from PA4PR08MB6320.eurprd08.prod.outlook.com (2603:10a6:102:e5::9) by PA4PR08MB5966.eurprd08.prod.outlook.com (2603:10a6:102:ee::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.18; Fri, 19 Mar 2021 13:26:21 +0000 Received: from PA4PR08MB6320.eurprd08.prod.outlook.com ([fe80::60f0:3773:69b8:e336]) by PA4PR08MB6320.eurprd08.prod.outlook.com ([fe80::60f0:3773:69b8:e336%2]) with mapi id 15.20.3955.018; Fri, 19 Mar 2021 13:26:21 +0000 To: libc-alpha@sourceware.org, DJ Delorie Subject: [PATCH 1/6] malloc: Use memsize instead of CHUNK_AVAILABLE_SIZE Date: Fri, 19 Mar 2021 13:26:09 +0000 Message-Id: <4af59f688a0c79487187441c3257466037164221.1616155129.git.szabolcs.nagy@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-Originating-IP: [217.140.106.49] X-ClientProxiedBy: SA0PR11CA0126.namprd11.prod.outlook.com (2603:10b6:806:131::11) To PA4PR08MB6320.eurprd08.prod.outlook.com (2603:10a6:102:e5::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (217.140.106.49) by SA0PR11CA0126.namprd11.prod.outlook.com (2603:10b6:806:131::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.18 via Frontend Transport; Fri, 19 Mar 2021 13:26:19 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 22fb0bdf-1f54-4ea2-6c8a-08d8eadaa255 X-MS-TrafficTypeDiagnostic: PA4PR08MB5966:|PAXPR08MB6512: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:9508;OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: SnAhNYV9ANrSm1vv++Jag9Nqmkl6YadYwStvYH2+hjGnxHoE2p20DFjgwvc/Nje2ahzuo1EzgOZVAkU7bza714IIcRJwJ88lSAGfR6au65C56efqagCVbLVfGTXuKwXRrkGFRBrS/gDlEAedTegvlZblbQlvUDRsjZ9y0a1J2p1xBnuKstOE6b+SsS+wkWGXzfWo9q2ao+DPBRiQE43k/zF9OOtOk7e9o8koQ3kLJtpGyhTiWMjes+td4GQUCSf6Tj3hRkXpSKTiNDYTV5yXflF2Efw7MvYA6msQG2NI0Z6NYXkp6O6Mf6S/mZio6hxUMIs+IIp+1n5nnjh2GsqIAD8HhgZ5Jn60hRVwSJ7Hp01MMNVY0a2OYcdyTV9Y6JSCUMoPTBvhle9y6G78lePFae4BMOImdj34gqJY6TfH8GAzeSqwr40M2BF+J7Eiq494oDzrhCYHgZmGFnwIfzHiV8EW8imbo0GcDb076oaiorohNnvmZxYbYfY3SjI9LqyrnqwYmGl83x9rO1ck7B5U7gfzts4nd/9TXeibHl/NIt6+k/UqZ/RBIARpwvrlEaGwAeXN7lag3Bg2in5zTeGV+QTRRoPFlh35PiJE4w051ugEhDsCYAlfF05GBZx9yRpt+fhbve4DFYOb/D2dN9t7ua3+1gkgq4EBmeM9mG/7IwOm/lL2hM+pgDE+ETE9IIoT X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PA4PR08MB6320.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(39860400002)(136003)(376002)(366004)(346002)(2616005)(6916009)(26005)(316002)(36756003)(8936002)(6512007)(44832011)(5660300002)(8676002)(66556008)(66476007)(186003)(2906002)(6666004)(6506007)(66946007)(69590400012)(16526019)(86362001)(6486002)(83380400001)(52116002)(478600001)(38100700001)(956004); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: rRumdxmtuz9DLhmcxOKdYvPSTQf6bJykP4H9jNsslcr9lcCDji49pqx0ENmnobc9QUE3Nn4YUWzhtBwniuDAmpGl7dlbZzeEc9Dsy9U4r7pT7bgkfXDM9ouulZwqm8zK5A6U02AOtZbwC4dq8AtBlAMwmZcb7eA2vd2ZyDdeWr1p9StxfYwjuqyluWNa0kT3ym7IBDWy1THz1KHILL9XxId1fjE2gTWfwx/lkn/WvElyKghaBGvq6l0Wfw6HFyTZvVCa5HWhxpvlQj9uVtBpQ/NwP6KCUdEXUGFNuxxkkMHNyxT79ODGlcJ6IJCYHZwtaevGGubylHO0cTwRg3bSfp4RdjEfNh7UD+2Zo7yM/7n+sA12QThhvj+692jxO1T5JhVzHAdaY8zXTsvmg7PRJjPtk22iE6tpf2ipzPcE4Q1x6CDTvQNfNfH94Kpt1/O/h6nxX/jSdYhqnx5OSEC1EnyeV6IvO8eY5HSZGHyhYFUo9wik7AHTI7v8C4BZzudM478K80EnaLk34LnRa52/x3/61rcXDWJxSeMGOR/6jj+pxhJtRFIBD+Wx0Io2ZONUDgKMwV11PyBgoVeJrhKqc7VUwRW1PWoiTNtwk77PKndGEdPHG8Jmula2WzTcfPs8aH1cjvJN2P1IGx6JuiEpZUTYFEUH7NxMPvHwjpF106W8gv2NQU2pEhX/QADLrZ9v3HS5Byw0GJivO2PHk7oy9gCRd+ZdoFWQnOtPmAEvQDEkp3aMje4CRV8q55UT1RpIr5dWJmtw1Qg0GZQDM7NFYS+TPwJI0ePoCA21N4gmwkKhre6nW3ELXDHoheFrmEs2v7l1qfXDYWRZLWXNA7ZdNuMvQudW1y1LmszeEZPNL6cXj37MHOGdfwScNZpaHXfoL9UGFHrY50cpXIWhGqApGqe97VRy01RS7WRcr9PBnNSTF0WtEPvTldolT8fswcyMSL75Nxs48kL8CZwSLSdWunp3TR8mDA+cpXsqB1mfMwdYJUtY9LqRmhgU5Z52sqgqSKEpt74v7cZcfRLuuuNJCecAYu1ERFwXohvC6gVmKdjwUzNVdc57YVUTXhHS1woss1foNKmxd8zPkw8Yg90lKOaZQTFj9iwU/Yj9WE0JhffSE1Nv40tt3aQ9YEuIMRnaBuNC6873Gjq+mmNoU+lstXiAcX7La22Pe4n8daUVlktET+hp+M7qklFEvkM8gY0YmNWZ1js3DaC0nQMK0ITjQy90iRE2JaQ4j+x/0prwwPNHX8E2JcCFbN6/rOG7XEiD7jKwSDuLomHHSOzq1fH7pQq4CmUtie91aeqsx9Rtsed54Xr/mJGpGYvCpr7+g04M X-MS-Exchange-Transport-CrossTenantHeadersStamped: PA4PR08MB5966 Original-Authentication-Results: sourceware.org; dkim=none (message not signed) header.d=none; sourceware.org; dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM5EUR03FT039.eop-EUR03.prod.protection.outlook.com X-MS-Office365-Filtering-Correlation-Id-Prvs: dab18c8d-2d97-4193-5896-08d8eada9595 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: VnuGB9bR8+V6DulHCwf8Ar5MUkEeeycRWIDOtdk91+wm0MupLIaOp0q1yb1Y2FJxG+AFZfj4v8tFpVIdafCAYcEDFIxbvNV5kgZDE0PbE1SBMwDUI1niu9MOnk7krIqDBqWPucMda6iRYrp0NZmSIhfOQNIXNIx1BzF4deNi4j8y3rp/OwQO8wh3qdENA4Tc3cUsq7U2YpGY053wZFWODc0X7nkPEdBT1diGiquCk37UbylfyrHJcMUOB7mOE3MkbPoQsgSdHE5R1Noi37lPVYMzKbMzbtKZn+Keyjz4TEOZnWsjDcUd8dKopx5mJOGbIkeOOr0zt2bbh7T0UHAr0S+oakXjOGrQ+Uj1vshPJR2rpCWQgCrVFVXae27F7hlKEQ2lGC+sitse7vEuPm0U5r66JZwQ89Uey+h/P/iyhxFD5UVWapySJw6kamUiNONjaXxhkT11XNfDPaLjeVrOBPGjLx7IqkMl2eqxX+tAhGKtZAdFkg5MPNBXSje5bLttg02b+bI2kG4LxyZOM3QS2rk7DyayfdQaGflBgR+PYPs7hR+W8Mw/cogGKRolh4WEvcTC/SubQMD0Lh4REVupM97C4hD/av/qaFvIiJr/8TS6hl8BCZp+Mbltskj4Ej91sVqVhQ5Lu24VTldWtWZ90uXGEmSJT84yPRojD8yoj4JL2DlwYaC2ZxvnCQMy6v97 X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFS:(4636009)(346002)(376002)(136003)(39860400002)(396003)(46966006)(36840700001)(6862004)(44832011)(956004)(47076005)(8676002)(6506007)(70206006)(69590400012)(83380400001)(186003)(16526019)(336012)(2616005)(6666004)(36860700001)(6486002)(86362001)(2906002)(316002)(478600001)(81166007)(82740400003)(5660300002)(36756003)(8936002)(356005)(6512007)(70586007)(26005)(82310400003); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Mar 2021 13:26:41.9452 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 22fb0bdf-1f54-4ea2-6c8a-08d8eadaa255 X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: AM5EUR03FT039.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR08MB6512 X-Spam-Status: No, score=-14.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, MSGID_FROM_MTA_HEADER, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Szabolcs Nagy via Libc-alpha From: Szabolcs Nagy Reply-To: Szabolcs Nagy Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" This is a pure refactoring change that does not affect behaviour. The CHUNK_AVAILABLE_SIZE name was unclear, the memsize name tries to follow the existing convention of mem denoting the allocation that is handed out to the user, while chunk is its internally used container. The user owned memory for a given chunk starts at chunk2mem(p) and the size is memsize(p). It is not valid to use on dumped heap chunks. Moved the definition next to other chunk and mem related macros. --- malloc/hooks.c | 11 +++++------ malloc/malloc.c | 39 +++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/malloc/hooks.c b/malloc/hooks.c index 9474e199c3..b82ff5781b 100644 --- a/malloc/hooks.c +++ b/malloc/hooks.c @@ -102,7 +102,7 @@ malloc_check_get_size (mchunkptr p) assert (using_malloc_checking == 1); - for (size = CHUNK_AVAILABLE_SIZE (p) - 1; + for (size = CHUNK_HDR_SZ + memsize (p) - 1; (c = *SAFE_CHAR_OFFSET (p, size)) != magic; size -= c) { @@ -130,7 +130,7 @@ mem2mem_check (void *ptr, size_t req_sz) p = mem2chunk (ptr); magic = magicbyte (p); - max_sz = CHUNK_AVAILABLE_SIZE (p) - CHUNK_HDR_SZ; + max_sz = memsize (p); for (i = max_sz - 1; i > req_sz; i -= block_sz) { @@ -175,7 +175,7 @@ mem2chunk_check (void *mem, unsigned char **magic_p) next_chunk (prev_chunk (p)) != p))) return NULL; - for (sz = CHUNK_AVAILABLE_SIZE (p) - 1; + for (sz = CHUNK_HDR_SZ + memsize (p) - 1; (c = *SAFE_CHAR_OFFSET (p, sz)) != magic; sz -= c) { @@ -200,7 +200,7 @@ mem2chunk_check (void *mem, unsigned char **magic_p) ((prev_size (p) + sz) & page_mask) != 0) return NULL; - for (sz = CHUNK_AVAILABLE_SIZE (p) - 1; + for (sz = CHUNK_HDR_SZ + memsize (p) - 1; (c = *SAFE_CHAR_OFFSET (p, sz)) != magic; sz -= c) { @@ -279,8 +279,7 @@ free_check (void *mem, const void *caller) else { /* Mark the chunk as belonging to the library again. */ - (void)tag_region (chunk2rawmem (p), CHUNK_AVAILABLE_SIZE (p) - - CHUNK_HDR_SZ); + (void)tag_region (chunk2rawmem (p), memsize (p)); _int_free (&main_arena, p, 1); __libc_lock_unlock (main_arena.mutex); } diff --git a/malloc/malloc.c b/malloc/malloc.c index 36583120ce..03eb0f40fa 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -1331,18 +1331,6 @@ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ MINSIZE : \ ((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK) -/* Available size of chunk. This is the size of the real usable data - in the chunk, plus the chunk header. Note: If memory tagging is - enabled the layout changes to accomodate the granule size, this is - wasteful for small allocations so not done by default. The logic - does not work if chunk headers are not granule aligned. */ -_Static_assert (__MTAG_GRANULE_SIZE <= CHUNK_HDR_SZ, - "memory tagging is not supported with large granule."); -#define CHUNK_AVAILABLE_SIZE(p) \ - (__MTAG_GRANULE_SIZE > SIZE_SZ && __glibc_unlikely (mtag_enabled) ? \ - chunksize (p) : \ - chunksize (p) + (chunk_is_mmapped (p) ? 0 : SIZE_SZ)) - /* Check if REQ overflows when padded and aligned and if the resulting value is less than PTRDIFF_T. Returns TRUE and the requested size or MINSIZE in case the value is less than MINSIZE on SZ or false if any of the previous @@ -1465,14 +1453,26 @@ checked_request2size (size_t req, size_t *sz) __nonnull (1) #pragma GCC poison mchunk_size #pragma GCC poison mchunk_prev_size +/* This is the size of the real usable data in the chunk. Not valid for + dumped heap chunks. */ +#define memsize(p) \ + (__MTAG_GRANULE_SIZE > SIZE_SZ && __glibc_unlikely (mtag_enabled) ? \ + chunksize (p) - CHUNK_HDR_SZ : \ + chunksize (p) - CHUNK_HDR_SZ + (chunk_is_mmapped (p) ? 0 : SIZE_SZ)) + +/* If memory tagging is enabled the layout changes to accomodate the granule + size, this is wasteful for small allocations so not done by default. + Both the chunk header and user data has to be granule aligned. */ +_Static_assert (__MTAG_GRANULE_SIZE <= CHUNK_HDR_SZ, + "memory tagging is not supported with large granule."); + static __always_inline void * tag_new_usable (void *ptr) { if (__glibc_unlikely (mtag_enabled) && ptr) { mchunkptr cp = mem2chunk(ptr); - ptr = __libc_mtag_tag_region (__libc_mtag_new_tag (ptr), - CHUNK_AVAILABLE_SIZE (cp) - CHUNK_HDR_SZ); + ptr = __libc_mtag_tag_region (__libc_mtag_new_tag (ptr), memsize (cp)); } return ptr; } @@ -3316,8 +3316,7 @@ __libc_free (void *mem) MAYBE_INIT_TCACHE (); /* Mark the chunk as belonging to the library again. */ - (void)tag_region (chunk2rawmem (p), - CHUNK_AVAILABLE_SIZE (p) - CHUNK_HDR_SZ); + (void)tag_region (chunk2rawmem (p), memsize (p)); ar_ptr = arena_for_chunk (p); _int_free (ar_ptr, p, 0); @@ -3459,7 +3458,7 @@ __libc_realloc (void *oldmem, size_t bytes) newp = __libc_malloc (bytes); if (newp != NULL) { - size_t sz = CHUNK_AVAILABLE_SIZE (oldp) - CHUNK_HDR_SZ; + size_t sz = memsize (oldp); memcpy (newp, oldmem, sz); (void) tag_region (chunk2rawmem (oldp), sz); _int_free (ar_ptr, oldp, 0); @@ -3675,7 +3674,7 @@ __libc_calloc (size_t n, size_t elem_size) regardless of MORECORE_CLEARS, so we zero the whole block while doing so. */ if (__glibc_unlikely (mtag_enabled)) - return tag_new_zero_region (mem, CHUNK_AVAILABLE_SIZE (p) - CHUNK_HDR_SZ); + return tag_new_zero_region (mem, memsize (p)); INTERNAL_SIZE_T csz = chunksize (p); @@ -4863,7 +4862,7 @@ _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize, else { void *oldmem = chunk2rawmem (oldp); - size_t sz = CHUNK_AVAILABLE_SIZE (oldp) - CHUNK_HDR_SZ; + size_t sz = memsize (oldp); (void) tag_region (oldmem, sz); newmem = tag_new_usable (newmem); memcpy (newmem, oldmem, sz); @@ -5110,7 +5109,7 @@ musable (void *mem) result = chunksize (p) - CHUNK_HDR_SZ; } else if (inuse (p)) - result = CHUNK_AVAILABLE_SIZE (p) - CHUNK_HDR_SZ; + result = memsize (p); return result; }