From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id +gzzN/ztjGiExQEAWB0awg (envelope-from ) for ; Fri, 01 Aug 2025 12:40:28 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=avnJs4zt; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id D5F8B1E102; Fri, 1 Aug 2025 12:40:28 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-10.1 required=5.0 tests=ARC_SIGNED,ARC_VALID, BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED, RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE autolearn=ham autolearn_force=no version=4.0.1 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 (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 1DBE01E091 for ; Fri, 1 Aug 2025 12:40:28 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B5B56385842E for ; Fri, 1 Aug 2025 16:40:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B5B56385842E Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=avnJs4zt Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 317233858C40 for ; Fri, 1 Aug 2025 16:39:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 317233858C40 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 317233858C40 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1754066389; cv=none; b=cdQUEnLYzUHy/4qggPNre2HY8Scttj+sokAadZaXj6qB/3+V3aZcmTqYxzCc3M3t++JycIktwrzeBytfsRHr0A73qhWqCG1vwP7Dq0g7ej2waIYbuhziO+wObkdnNLfSTT1kZv2LzUvdcbTcz9ZxGe+NAXjZfucvrUUYdvAFHqI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1754066389; c=relaxed/simple; bh=lsKZvojb4+T6CXqsOx5MuuWoTbInlKyhVXtXfYTNmB8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=pvJpdlKCAxomUB0OFw9SBHzW/mLKcicSjW2GBLyqrQW9Pcc6F17YoLQ3Q9nmnxA/CtOb54GQr8Ve9LMr+alb9mybxjr7yH6XmHHx85aXMhOolkISvvKlrb9mix+QfGE9uk4avznJ5f9hTbO4VJj3QhBpQ18ytyfIRQjCBHMipBw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 317233858C40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1754066388; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=TtQhQYaUII47dindjawGLIZDBJKDCw2LGIDR5abJguE=; b=avnJs4ztBdzPlK8XRpoBq3IWL7XKWkWR3/YddAZjf79/JCZa6Ct2Dc9TT+UQWMnHR37KuG d1tbWp2+KlG8hosfUfV9GnWkgSnqMmVub+5vH2o2GsTkW2fM2wSoEeejpSsvzxgjdN1xog Y3OghWRE0frquj5/n+g3Py4/+g+tp4I= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-224-hwLDSS7XPkuSjOZtUk3HJA-1; Fri, 01 Aug 2025 12:39:47 -0400 X-MC-Unique: hwLDSS7XPkuSjOZtUk3HJA-1 X-Mimecast-MFC-AGG-ID: hwLDSS7XPkuSjOZtUk3HJA_1754066387 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EDD471956094 for ; Fri, 1 Aug 2025 16:39:46 +0000 (UTC) Received: from guittard.redhat.com (unknown [10.22.89.247]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 934DE180035E; Fri, 1 Aug 2025 16:39:45 +0000 (UTC) From: Keith Seitz To: gdb-patches@sourceware.org Subject: [PATCH] should_validate_memtags: Do not dereference references Date: Fri, 1 Aug 2025 09:39:43 -0700 Message-ID: <60afe2cd4b01bc7955fcf2b4e18727de0440801b.1754064717.git.keiths@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: pqkGBN1c10IgtbD6U5_ErirW_UJM_-A2RRHHLsL5oPE_1754066387 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org should_validate_memtags uses value_as_address to evalute whether an address for a value is tagged. The comments for that function simply say, "Extract a value as a C pointer." While that sounds innoncuous, that function calls coerce_array, which will dereference any references. This is not what is desired here. This can be demonstrated on an MTE-enabled host, such as aarch64- based Ampere (example taken from tests introduced in this patch): (gdb) p b.get_foo () Could not validate memory tag: Value can't be converted to integer. $2 = (const foo &) @0xffffffffed88: {m_a = 42} While the command completes, gdb didn't actually attempt to evaluate any memory tags. Fix this by using unpack_pointer instead. Tested on x86_64 Fedora 40 and aarch64 RHEL 9.6. --- gdb/printcmd.c | 4 +- gdb/testsuite/gdb.cp/method-ref-return.cc | 42 +++++++++++++ gdb/testsuite/gdb.cp/method-ref-return.exp | 70 ++++++++++++++++++++++ gdb/value.c | 0 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.cp/method-ref-return.cc create mode 100644 gdb/testsuite/gdb.cp/method-ref-return.exp mode change 100644 => 100755 gdb/value.c diff --git a/gdb/printcmd.c b/gdb/printcmd.c index 19fbc20074e..d1cf72d371d 100644 --- a/gdb/printcmd.c +++ b/gdb/printcmd.c @@ -1290,7 +1290,9 @@ should_validate_memtags (gdbarch *gdbarch, struct value *value) return false; /* We do. Check whether it includes any tags. */ - return target_is_address_tagged (gdbarch, value_as_address (value)); + struct type *val_type = value->type (); + const gdb_byte *data = value->contents ().data (); + return target_is_address_tagged (gdbarch, unpack_pointer (val_type, data)); } /* Helper for parsing arguments for print_command_1. */ diff --git a/gdb/testsuite/gdb.cp/method-ref-return.cc b/gdb/testsuite/gdb.cp/method-ref-return.cc new file mode 100644 index 00000000000..4169bfe6908 --- /dev/null +++ b/gdb/testsuite/gdb.cp/method-ref-return.cc @@ -0,0 +1,42 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2025 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Test that we can access class method/data member via reference. */ + +struct foo +{ + foo () : m_a (42) {} + int get_a () const { return m_a; } + int m_a; +}; + +struct bar +{ + bar () : m_foo () {} + const foo &get_foo () const { return m_foo; } + foo m_foo; +}; + +int +main (int argc, char *argv[]) +{ + bar b; + const foo &ref = b.get_foo (); + int ret = ref.m_a; // breakpoint here + ret += ref.get_a (); + return ret; +} \ No newline at end of file diff --git a/gdb/testsuite/gdb.cp/method-ref-return.exp b/gdb/testsuite/gdb.cp/method-ref-return.exp new file mode 100644 index 00000000000..1ac5ac930f8 --- /dev/null +++ b/gdb/testsuite/gdb.cp/method-ref-return.exp @@ -0,0 +1,70 @@ +# Copyright 2025 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test calling methods and accessing members via reference. + +require allow_cplus_tests + +standard_testfile .cc + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} { + return -1 +} + +if {![runto_main]} { + return -1 +} + +# Set a breakpoint after the bar object is created and the reference is obtained. +gdb_breakpoint [gdb_get_line_number "breakpoint here"] +gdb_continue_to_breakpoint "after reference assignment" + +# Test that we can call the method through reference and get the expected result. +gdb_test_multiple "print b.get_foo()" "print method call returning reference" { + -re "\\$\[0-9\]+ = \\(const foo &\\) @$hex: \\{m_a = 42\\}\r\n$gdb_prompt $" { + pass $gdb_test_name + } + -re "Could not validate memory tag: Value can't be converted to integer\\." { + fail "$gdb_test_name" + } +} + +# Test accessing the member through the reference. +gdb_test "print b.get_foo ().m_a" \ + "\\$\[0-9\]+ = 42" \ + "print member access through reference" + +# Test calling method on the referenced object. +gdb_test "print b.get_foo ().get_a()" \ + "\\$\[0-9\]+ = 42" \ + "print method call on referenced object" + +# Test that the stored reference works correctly. +gdb_test_multiple "print ref" "print stored reference" { + -re "\\$\[0-9\]+ = \\(const foo &\\) @$hex: \\{m_a = 42\\}\r\n$gdb_prompt $" { + pass $gdb_test_name + } + -re "Could not validate memory tag: Value can't be converted to integer\\." { + fail "$gdb_test_name" + } +} + +gdb_test "print ref.m_a" \ + "\\$\[0-9\]+ = 42" \ + "print member through stored reference" + +gdb_test "print ref.get_a()" \ + "\\$\[0-9\]+ = 42" \ + "print method call through stored reference" \ No newline at end of file diff --git a/gdb/value.c b/gdb/value.c old mode 100644 new mode 100755 base-commit: 8a6f3a0462063fec4c16b5ecf98bc54a702ac3e2 -- 2.49.0