From patchwork Fri Jun 21 08:58:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 1950656 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; secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=kxx5z83h; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W5BJm3sRzz20X6 for ; Fri, 21 Jun 2024 19:05:12 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C643E38983A0 for ; Fri, 21 Jun 2024 09:05:10 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by sourceware.org (Postfix) with ESMTPS id 319CE3896C09 for ; Fri, 21 Jun 2024 08:58:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 319CE3896C09 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 319CE3896C09 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718960331; cv=none; b=icf5Y688I11SUM7wxaw1+UV4NTTJxjOMM04hQgeQS0snOZeJMQATyUA4dAqGBUPOcBSU7trvIHPYC84nRL9McSikX8e9/h24Oy2Q8ZJQY0d84CJQ38bbn6+pMl7h+0rlpqHjmVQgfBGqHMnRBjezKRf8SjtfTcpyCdN0lcccDO8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718960331; c=relaxed/simple; bh=/eVODcI7OMJ9Ozv0L8X45TyekjouRDwFv61MX83Xk7w=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=o5ltvIs+OE4+PgC71MWCH7AsEfvr/Zf32O6Zl7j9inzg0eTkiZbVMHk8yMX/n8emoGtj1NriTG9xttqc+6zTSBxwMO69YliHfRWOROq452a1TNR9fXPNaTHtDjLBSQ87RHboVxpJwfiv5w3v18PP7vqy9gGbqCaAv5oYEy3UGWQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-42249a4f9e4so13150095e9.2 for ; Fri, 21 Jun 2024 01:58:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1718960326; x=1719565126; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=AsK+iyVmzDoqpnGL5y9ZQAav4vk60gAmSuJCCcK+Q90=; b=kxx5z83hwYGEy8frOsrPqo3TvUiholVv5Dp6SuLzDcvFDynwV1iM8zGwcQTv5S7lJ3 UeEIvbminplXDKzr7s4bPs0pKwJ5m3GR6VjadqhxQ5UviJ+2whYkVaJ1WJitMN3d/HYy iKOXfAIZ5KGp8/fW7S8b9R9R0K12zIfqVZuiRc9JI3DmMU9H/56XN344LK1UGKVaUZpd 3dsXElttjErVRwjm92ChIVJ850BtYKgb//R4nSkeT0cSQTG8fCBonPbyCbSlXDPU0OwV nadSHnGk7qdb32zRQy2A0GYFEuCPSmIjOhJZFa4/XTWz7HTKwLZzl2PE3yxyDdZr0yhe rC/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718960326; x=1719565126; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AsK+iyVmzDoqpnGL5y9ZQAav4vk60gAmSuJCCcK+Q90=; b=rDLjyIgtqjwEzN4T+lRpK7kRBcu3KfNvX2B+NVcygZPtHKtQHVG8EdYKjDP1I2B/jT f0trCMGH7Z+XDXjhux/P2srdHUp2MvoSvkt1+AgDyrDaFgH1zj1ez2GEmg8tQ2uuZzrI cEyaqDxZwAJXo3XH27xAig3hX1w2UevmUWL+CKscfl7J1XMbF2NwVQOeZAj7+cQiPHXn JFniY2pp0YXSpKIv90R9eZ3z7z+g7e3+5ftWXKTVeA8Lbf8CLexvjLuNEBbobij5eTlk FB2Y3DJdP6wziCGk9bpyMSSu6n+wKXsPGVRxP8r7/LZei/bvHKxked1G5mjI4EyK0eLI 8+9A== X-Gm-Message-State: AOJu0YyagVDp6VLihpl1Dfn4YGqXTbV9jXMmkjI9t/kYCwJyogxAYFZN 64JTGREpUVj2f1kodXfaC1kI6Kp7q5HATDZdI/9NEvB+OSx/eEjHH2EI79SgJT0Fmjg8pINOgsU = X-Google-Smtp-Source: AGHT+IHKepJKuyxQ0KwKmvedSKI9OLZt4m5I42VC97pbWiJojh4AtdnhLx5wCJtgjRwH6VVvCSgIQQ== X-Received: by 2002:a05:600c:2d93:b0:421:182d:9232 with SMTP id 5b1f17b1804b1-42475185a23mr57072695e9.18.1718960325920; Fri, 21 Jun 2024 01:58:45 -0700 (PDT) Received: from poulhies-Precision-5550.lan ([2001:861:3382:1a90:a589:2704:bfe1:5d92]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4247d0c5485sm55322375e9.21.2024.06.21.01.58.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Jun 2024 01:58:45 -0700 (PDT) From: =?utf-8?q?Marc_Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED 20/22] ada: Fix bogus Address Sanitizer stack-buffer-overflow on packed record equality Date: Fri, 21 Jun 2024 10:58:16 +0200 Message-ID: <20240621085819.2485987-20-poulhies@adacore.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240621085819.2485987-1-poulhies@adacore.com> References: <20240621085819.2485987-1-poulhies@adacore.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org From: Eric Botcazou We set DECL_BIT_FIELD optimistically during the translation of record types and clear it afterward if needed, but fail to clear other attributes in the latter case, which fools the logic of the Address Sanitizer. gcc/ada/ * gcc-interface/utils.cc (clear_decl_bit_field): New function. (finish_record_type): Call clear_decl_bit_field instead of clearing DECL_BIT_FIELD manually. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/gcc-interface/utils.cc | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/gcc/ada/gcc-interface/utils.cc b/gcc/ada/gcc-interface/utils.cc index 771cb1a17ca..0eb9af8d4a2 100644 --- a/gcc/ada/gcc-interface/utils.cc +++ b/gcc/ada/gcc-interface/utils.cc @@ -2002,6 +2002,21 @@ finish_fat_pointer_type (tree record_type, tree field_list) TYPE_CONTAINS_PLACEHOLDER_INTERNAL (record_type) = 2; } +/* Clear DECL_BIT_FIELD flag and associated markers on FIELD, which is a field + of aggregate type TYPE. */ + +static void +clear_decl_bit_field (tree field, tree type) +{ + DECL_BIT_FIELD (field) = 0; + DECL_BIT_FIELD_TYPE (field) = NULL_TREE; + + /* DECL_BIT_FIELD_REPRESENTATIVE is not defined for QUAL_UNION_TYPE since + it uses the same slot as DECL_QUALIFIER. */ + if (TREE_CODE (type) != QUAL_UNION_TYPE) + DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE; +} + /* Given a record type RECORD_TYPE and a list of FIELD_DECL nodes FIELD_LIST, finish constructing the record or union type. If REP_LEVEL is zero, this record has no representation clause and so will be entirely laid out here. @@ -2112,7 +2127,7 @@ finish_record_type (tree record_type, tree field_list, int rep_level, if (TYPE_ALIGN (record_type) >= align) { SET_DECL_ALIGN (field, MAX (DECL_ALIGN (field), align)); - DECL_BIT_FIELD (field) = 0; + clear_decl_bit_field (field, record_type); } else if (!had_align && rep_level == 0 @@ -2122,7 +2137,7 @@ finish_record_type (tree record_type, tree field_list, int rep_level, { SET_TYPE_ALIGN (record_type, align); SET_DECL_ALIGN (field, MAX (DECL_ALIGN (field), align)); - DECL_BIT_FIELD (field) = 0; + clear_decl_bit_field (field, record_type); } } @@ -2130,7 +2145,7 @@ finish_record_type (tree record_type, tree field_list, int rep_level, if (!STRICT_ALIGNMENT && DECL_BIT_FIELD (field) && value_factor_p (pos, BITS_PER_UNIT)) - DECL_BIT_FIELD (field) = 0; + clear_decl_bit_field (field, record_type); } /* Clear DECL_BIT_FIELD_TYPE for a variant part at offset 0, it's simply @@ -2453,10 +2468,7 @@ rest_of_record_type_compilation (tree record_type) avoid generating useless attributes for the field in DWARF. */ if (DECL_SIZE (old_field) == TYPE_SIZE (field_type) && value_factor_p (pos, BITS_PER_UNIT)) - { - DECL_BIT_FIELD (new_field) = 0; - DECL_BIT_FIELD_TYPE (new_field) = NULL_TREE; - } + clear_decl_bit_field (new_field, new_record_type); DECL_CHAIN (new_field) = TYPE_FIELDS (new_record_type); TYPE_FIELDS (new_record_type) = new_field;