From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id MB0HJIG/YmIH5AEAWB0awg (envelope-from ) for ; Fri, 22 Apr 2022 10:45:21 -0400 Received: by simark.ca (Postfix, from userid 112) id 8F6521E058; Fri, 22 Apr 2022 10:45:21 -0400 (EDT) Authentication-Results: simark.ca; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=O0Gv3XfG; dkim-atps=neutral X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from 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 RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id F05A01E00E for ; Fri, 22 Apr 2022 10:45:20 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 84459385627C for ; Fri, 22 Apr 2022 14:45:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 84459385627C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1650638720; bh=coIBK3vmNxFmyj+3tce8uHNR96RoRVRlD1tuoW5D9gg=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=O0Gv3XfGOHPQQGDZ1/MEfQ7WuvS1WI0HLEjfnomeFqkcDW9L3GLZE4baRrxQzEk+D R1eZSg3rK4dhw89uLrGZzAk8JDpe4wD6wTEixPshWYXJCKDZMMmMlfy/jRUzVQeRcN G1Uu22o+QabZYz0ZJvvvvAsrKrszUfYR4lE7Bc1I= Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by sourceware.org (Postfix) with ESMTPS id 7C4A43857C42 for ; Fri, 22 Apr 2022 14:44:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7C4A43857C42 X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="244617675" X-IronPort-AV: E=Sophos;i="5.90,282,1643702400"; d="scan'208";a="244617675" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Apr 2022 07:44:33 -0700 X-IronPort-AV: E=Sophos;i="5.90,282,1643702400"; d="scan'208";a="703576712" Received: from labpcdell3650-004.iul.intel.com (HELO localhost) ([172.28.50.126]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Apr 2022 07:44:32 -0700 To: gdb-patches@sourceware.org Subject: [PATCH 1/1] gdb/dwarf2: Fix 'rw_pieced_value' for values casted to different type. Date: Fri, 22 Apr 2022 16:44:20 +0200 Message-Id: <20220422144420.3545190-2-stephan.rohr@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220422144420.3545190-1-stephan.rohr@intel.com> References: <20220422144420.3545190-1-stephan.rohr@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Stephan Rohr via Gdb-patches Reply-To: Stephan Rohr Cc: tom@tromey.com, stephan.rohr@intel.com Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" From: "Rohr, Stephan" The 'rw_pieced_value' function is executed when fetching a (lazy) variable described by 'DW_OP_piece' or 'DW_OP_bit_piece'. The function checks the 'type' and 'enclosing_type' fields of the value for identity. * The 'type' field describes the type of a value. * In most cases, the 'enclosing_type' field is identical to the 'type' field. * Scenarios where the 'type' and 'enclosing_type' of an object differ are described in 'gdb/value.c'. Possible cases are: * If a value represents a C++ object, then the 'type' field gives the object's compile-time type. If the object actually belongs to some class derived from `type', perhaps with other base classes and additional members, then `type' is just a subobject of the real thing, and the full object is probably larger than `type' would suggest. * If 'type' is a dynamic class (i.e. one with a vtable), then GDB can actually determine the object's run-time type by looking at the run-time type information in the vtable. GDB may then elect to read the entire object. * If the user casts a variable to a different type (e.g. 'print ( []) '), the value's type is updated before reading the value. If a lazy value is fetched, GDB allocates space based on the enclosing type's length and typically reads the 'full' object. This is not implemented for pieced values and causes an internal error if 'type' and 'enclosing_type' of a value are not identical. However, GDB can read the value based on its type. Thus, it should be sufficient to check if the type's length (potentially shifted by 'embedded_offset') does not exceed the enclosing type's length which was used for memory allocation. --- gdb/dwarf2/expr.c | 6 ++---- gdb/testsuite/gdb.dwarf2/shortpiece.exp | 12 ++++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/gdb/dwarf2/expr.c b/gdb/dwarf2/expr.c index 99862583336..6330a5787fc 100644 --- a/gdb/dwarf2/expr.c +++ b/gdb/dwarf2/expr.c @@ -174,10 +174,8 @@ rw_pieced_value (value *v, value *from, bool check_optimized) } else { - if (value_type (v) != value_enclosing_type (v)) - internal_error (__FILE__, __LINE__, - _("Should not be able to create a lazy value with " - "an enclosing type")); + gdb_assert ((TYPE_LENGTH (value_type (v)) + value_embedded_offset (v)) + <= TYPE_LENGTH (value_enclosing_type (v))); if (check_optimized) v_contents = nullptr; else diff --git a/gdb/testsuite/gdb.dwarf2/shortpiece.exp b/gdb/testsuite/gdb.dwarf2/shortpiece.exp index f5a933e521b..19cdec83193 100644 --- a/gdb/testsuite/gdb.dwarf2/shortpiece.exp +++ b/gdb/testsuite/gdb.dwarf2/shortpiece.exp @@ -98,3 +98,15 @@ if { [prepare_for_testing "failed to prepare" ${testfile} \ gdb_test "p s1" " = {a = 1, b = 0}" gdb_test "p s2" \ "access outside bounds of object referenced via synthetic pointer" + +# When fetching a lazy value, GDB typically tries to fetch the 'full' +# object based on the enclosing type. GDB does not support the reading +# of a pieced value with a (possibly larger) enclosing type. However, +# the user may want to print a value casted to a different type, +# e.g. print ( []) . This cast causes an update of the +# value's type. In case of a pieced value, GDB failed to fetch the +# value's content. +# This test verifies that GDB can print a pieced value casted to a +# different type. +gdb_test "p (int \[\]) s1" " = \\{1\\, 0\\}" +gdb_test "p (short \[\]) s1" " = \\{1\\, 0\\, 0\\, \\}" -- 2.25.1