From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 0RU+GAQeiWobLzUAWB0awg (envelope-from ) for ; Fri, 21 Aug 2026 23:56:52 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=El6F7RUt; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 5E2F81E0A3; Fri, 21 Aug 2026 23:56:52 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-4.1 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_BL_SPAMCOP_NET,RCVD_IN_DNSWL_MED autolearn=unavailable autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (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 9F2D21E09B for ; Fri, 21 Aug 2026 23:56:51 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 40C284BB24D0 for ; Sat, 22 Aug 2026 03:56:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 40C284BB24D0 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=El6F7RUt Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by sourceware.org (Postfix) with ESMTPS id 7658C4BA2E04 for ; Sat, 22 Aug 2026 03:56:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7658C4BA2E04 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7658C4BA2E04 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::52f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1787370986; cv=none; b=bQp0mfErpyWK6vR+n0Yz3oD8HUXFrLuyHFAdZQfUctE+8wBZIjlli2O88wFMClCQ8xgxwDNGQgw8tbXIlLvczuYdWCtWZaCdPEBHpV0i+XXd7C/dGdWy1FOaoA0eTeL3Ashp2pTU4xRLlg0e2jXoET2yTPtaa8Qs81h3JJxNErY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1787370986; c=relaxed/simple; bh=J72dhjk2yNBhhDWXdqfO4916kWAOQ0sqIAeUAgsIRoM=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=fVMoS9Zm7jXORmC0s0qxH7IM12BCdPWiwRkoO7XJqrhtFNCM9yuGPNN0dkgCMEZH/omwSiiStOhVp25i1TCdHxQqJhIQ4UuN/aqD6ezoIQrJgpcnWgayOzV+k49FLnoRLOCHhQNEPmXxB9BfisY8tetjqIkEdA8V5OJfQygpGfI= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=El6F7RUt DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7658C4BA2E04 Received: by mail-pg1-x52f.google.com with SMTP id 41be03b00d2f7-cbe4c537ad6so194821a12.0 for ; Fri, 21 Aug 2026 20:56:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787370985; x=1787975785; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=FeaKTeIq8Y0SyrKc57iMFCTZL1qR2xF//BBe1Yk5cig=; b=El6F7RUtHIaIMKY9cpzFOkergMfh+FHtkWZtUObfjfrrBs3UzP9J+EOlL4wr9b6Kzo f2ILbEakUV0oRSkVVSVCZyzz7prD1D2LJNXi0f1sCHS87LUMx0LLyPgH5rCdL6iEJSzx 72kZcavfSXlkSd61yLsTlAuIUX+rFbBTDbxb6jNX1IaLpKoQeQAEwFsM2dsknvgxI0tm DaHgcoC4H/8QcuiNbxecoTRKwcXK1o8ei6XHD1oTeHM9AQbJgNXGtwawIoM+XIjQa4XA UR+wVftnyjcH0cX/9qEDMzJvoePqRpkhXc8i9NDY5GUMEz4ogTUpc5PL+mLtcFJhaXng zPgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787370985; x=1787975785; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=FeaKTeIq8Y0SyrKc57iMFCTZL1qR2xF//BBe1Yk5cig=; b=tMQCbsjA7knahuwHy0TyD62mu27WOhrwuvt6ahSRIsrtdgYYqAhmVpAc8THstgA0XZ BktDiOLrXCAIhyDOg4KGB4UCq1ZYIQ0jCRJCrPLxiD+BHSs97AQvMKUVrtW3CLTSllup Gh9iyjQtupPgpYfrMSdT7eCMu7UDm0j0aOvu0F7WOLPcrcr2zCuGcp4r19G4+Ywzg48w 0IxxZIDWTOhYVSyBNfYOJMwvyLbDH+FjrXNpbXI+/AOLBoY8Em1nRhQ5Ud2qjH0zcAoo QjQ7KZOeoLrOdP80kyPf/N03d+5yFvOcoewc+HD/2Hbyx6jwChCb/e5tHvi+EqG849Ld WbUA== X-Gm-Message-State: AFuF++nGm1CRrlF9pq4Jim120kGAC4eFVtnod/CA6eQr3rdlxe3FE1rx 6hYXPr2YOH5pTNm09CpTxrSG5SfX/juskmLtKS0xsYwEm0o4FJhNztWAB6SFQa5T X-Gm-Gg: AR+sD12l0etJIj891TtFmS1OPaaE3Oq/2VnK5QFaHkE20AWNddvDDMHLHkvAK6nSUGu 8oIAEuyxK3PNfNYsmi5T33XlBGkdKBMg7LiTRpo9cFqjnozH/zxXd2AI/yWjOeqwJLdz7EZ/KMM Wmrbz7yqRl9FiGSLQE9nzQ0X2lwmPQWWRmh6CSAD8yuCufI8BpRdaLs8gvNA6LL5CEtmipP4e8j u2v8/QssCV8dfkjEuVVsHbKF3btoqGJuy676Y43LIbfUCUHa0VhXMDk95mIrl2dUgFbB6mpZ8rq kL1L25+HZJe006Ldqp49EeCtFotqc0ijv2TmBvrjFMrLazwYqWbDUGXbuI6K0YgU69HGhmTHhoe +XcJwvTItM/Jt7R15/cszzFi3EMsEaOc8gglTrihsHAzte6GT8UaBjoAyIsFJ6XOb79jCwyvEh/ jSJPovMcaC3bfveMG4sXaBXgl2n9J60cQOHnw9Ss+/q5K2zuaoEbqfGMAVwOcWwrZSCFb+u7NOb 1yM32xEmmWmunRcad+eSs05711fWq/NhfZJsEWwEoY= X-Received: by 2002:a17:903:b43:b0:2d2:a952:2628 with SMTP id d9443c01a7336-2d64b0d7b26mr98667755ad.2.1787370985133; Fri, 21 Aug 2026 20:56:25 -0700 (PDT) Received: from poweredge.r760.114.212.189.33.r760.114.212.189.33 ([155.254.126.227]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-327f923615asm1654067eec.29.2026.08.21.20.56.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Aug 2026 20:56:24 -0700 (PDT) From: Jielun Wu To: gdb-patches@sourceware.org Cc: simark@simark.ca Subject: [PATCH v2] gdb: Check DW_OP_deref_type size against type Date: Sat, 22 Aug 2026 11:56:21 +0800 Message-Id: <20260822035621.694548-1-firmiana402@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 DWARF v5 requires the explicit size operand of DW_OP_deref_type to match the size of the referenced base type. The evaluator currently passes both sizes to dwarf_expr_context::deref without validating the encoding, and the generic dereference helper accepts the mismatch by zero-extending the bytes read from memory. Reject a mismatch after resolving the base type. Name the actual opcode and report both sizes in the diagnostic. Teach the testsuite's DWARF assembler to emit DW_OP_deref_type and DW_OP_GNU_deref_type operands. Add tests covering matching and mismatched sizes for both opcodes. Tested on x86_64-linux. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34277 Signed-off-by: Jielun Wu --- Changes in v2: - Name the actual opcode and report both sizes in the diagnostic. - Rename addr_size to deref_size. - Add DWARF assembler handlers for the standard and GNU opcodes. - Add matching-size coverage for both opcodes. gdb/dwarf2/expr.c | 10 ++- .../gdb.dwarf2/dw2-deref-type-size.c | 22 +++++ .../gdb.dwarf2/dw2-deref-type-size.exp | 88 +++++++++++++++++++ gdb/testsuite/lib/dwarf.exp | 11 +++ 4 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-deref-type-size.c create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-deref-type-size.exp diff --git a/gdb/dwarf2/expr.c b/gdb/dwarf2/expr.c index 3a6b8f58199..7160a73d80b 100644 --- a/gdb/dwarf2/expr.c +++ b/gdb/dwarf2/expr.c @@ -2016,7 +2016,8 @@ dwarf_expr_context::execute_stack_op (gdb::array_view expr) case DW_OP_deref_type: case DW_OP_GNU_deref_type: { - int addr_size = (op == DW_OP_deref ? this->m_addr_size : *op_ptr++); + int deref_size + = (op == DW_OP_deref ? this->m_addr_size : *op_ptr++); CORE_ADDR addr = fetch_address (0); struct type *type; @@ -2027,11 +2028,16 @@ dwarf_expr_context::execute_stack_op (gdb::array_view expr) op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); cu_offset type_die_cu_off = (cu_offset) uoffset; type = get_base_type (type_die_cu_off); + if (type->length () != deref_size) + error (_("%s has dereference size %d, but its " + "type has size %s"), + get_DW_OP_name (op), deref_size, + pulongest (type->length ())); } else type = address_type; - result_val = this->deref (addr, addr_size, type); + result_val = this->deref (addr, deref_size, type); break; } diff --git a/gdb/testsuite/gdb.dwarf2/dw2-deref-type-size.c b/gdb/testsuite/gdb.dwarf2/dw2-deref-type-size.c new file mode 100644 index 00000000000..af01073cd74 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-deref-type-size.c @@ -0,0 +1,22 @@ +/* Copyright 2026 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 . */ + +unsigned char deref_data = 0x5a; + +int +main (void) +{ + return 0; +} diff --git a/gdb/testsuite/gdb.dwarf2/dw2-deref-type-size.exp b/gdb/testsuite/gdb.dwarf2/dw2-deref-type-size.exp new file mode 100644 index 00000000000..82d369f5220 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-deref-type-size.exp @@ -0,0 +1,88 @@ +# Copyright 2026 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 DW_OP_deref_type with explicit dereference sizes that both match +# and differ from the size of the referenced base type. + +load_lib dwarf.exp + +require dwarf2_support + +standard_testfile .c -dw.S + +set asm_file [standard_output_file $srcfile2] + +Dwarf::assemble $asm_file { + cu {version 5} { + compile_unit {} { + declare_labels uint8_label uint64_label + + uint8_label: base_type { + DW_AT_name "uint8_t" + DW_AT_encoding @DW_ATE_unsigned_char + DW_AT_byte_size 1 DW_FORM_sdata + } + + uint64_label: base_type { + DW_AT_name "uint64_t" + DW_AT_encoding @DW_ATE_unsigned + DW_AT_byte_size 8 DW_FORM_sdata + } + + foreach {var op size type_label} [list \ + valid_deref_type DW_OP_deref_type 1 $uint8_label \ + valid_gnu_deref_type DW_OP_GNU_deref_type 1 $uint8_label \ + invalid_deref_type DW_OP_deref_type 4 $uint64_label \ + invalid_gnu_deref_type DW_OP_GNU_deref_type 4 $uint64_label] { + DW_TAG_variable { + DW_AT_name $var + DW_AT_type :$type_label + DW_AT_external 1 DW_FORM_flag + DW_AT_location { + DW_OP_addr [gdb_target_symbol "deref_data"] + $op $size $type_label + DW_OP_stack_value + } SPECIAL_expr + } + } + } + } +} + +if {[prepare_for_testing "failed to prepare" ${testfile} \ + [list $srcfile $asm_file] nodebug]} { + return +} + +if {![runto_main]} { + return +} + +foreach_with_prefix var { + valid_deref_type + valid_gnu_deref_type +} { + gdb_test "print/x $var" " = 0x5a" +} + +foreach {var op} { + invalid_deref_type DW_OP_deref_type + invalid_gnu_deref_type DW_OP_GNU_deref_type +} { + with_test_prefix $var { + gdb_test "print $var" \ + "$op has dereference size 4, but its type has size 8" + } +} diff --git a/gdb/testsuite/lib/dwarf.exp b/gdb/testsuite/lib/dwarf.exp index 839c5174265..54102b1981d 100644 --- a/gdb/testsuite/lib/dwarf.exp +++ b/gdb/testsuite/lib/dwarf.exp @@ -1460,6 +1460,17 @@ namespace eval Dwarf { _op .byte $size } + proc _handle_DW_OP_deref_type {size label} { + variable _cu_label + + _op .byte $size + _op .uleb128 "$label - $_cu_label" + } + + proc _handle_DW_OP_GNU_deref_type {size label} { + _handle_DW_OP_deref_type $size $label + } + proc _handle_DW_OP_bregx {register offset} { _op .uleb128 $register _op .sleb128 $offset -- 2.34.1