From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 From: Stephan Rohr To: gdb-patches@sourceware.org Cc: stephan.rohr@intel.com, tom@tromey.com 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-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_PASS, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org 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: , X-List-Received-Date: Fri, 22 Apr 2022 14:44:36 -0000 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