From patchwork Fri Jan 27 15:11:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 63792 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6DC1C3857BA4 for ; Fri, 27 Jan 2023 15:11:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6DC1C3857BA4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1674832293; bh=aT0tauVcozNks7pLn6L3Sfzsg46eVo7oOSzb1n1fcuA=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Z3Jykg6ONoSsDoOYeTd2bG9Je2tnBtqevqy+79EaBnYa2eAPDRzZ1IOHhQ2Uz9H/y Q1O0kgcwkrVMz2YiwbOoqg9mnDPoejaDNVX3n6wXLOgcoyLmWxnCt0RZXoH8l9jw7f IOvI4G9X0bcsw265eyrsaryGGCla6ITDHjkcTgdA= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2e.google.com (mail-io1-xd2e.google.com [IPv6:2607:f8b0:4864:20::d2e]) by sourceware.org (Postfix) with ESMTPS id 405623858C00 for ; Fri, 27 Jan 2023 15:11:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 405623858C00 Received: by mail-io1-xd2e.google.com with SMTP id s26so2010326ioa.11 for ; Fri, 27 Jan 2023 07:11:09 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=aT0tauVcozNks7pLn6L3Sfzsg46eVo7oOSzb1n1fcuA=; b=ionah7trFn7nDvHSrKZEt6GHndGEFYUp0Xh465kF/tBTyEKBHC5mSM9jjKig9OIVf9 IJfCUh2v2U+EBAxhjh+mgndnhc3zA8pTSIz9DclAtpkxow23CLcp6686LhmY1WJhWh0q Sr6Qlhz2MkZ13TtGT80FGOfmKX+vWW2pShNDuuPLsfyYo6hitC28BaBB/UJ3ph/ravPb hPdZ/VEkPgyaYsHaev+qdPmFk9x7g26MjLlrqulPKfcn1PJ5ZP5K5/MiDlu3vozF/PKH 2RsOaTxVcMxTE/581MuONFIPk/FdpWMVhg87U6pecBTx7BHoSt1xyAnv6n61ce97HFfS 97xw== X-Gm-Message-State: AFqh2kosUrbNJyRqwgzgaKViwbBoTVbQEtpyJCpF4EYpVxr5mn/YXI7Q n0aBLMMCbmGOxDevX3/q8n15xf5kKGid+d/+ X-Google-Smtp-Source: AMrXdXvUhdnHZ2oKI2rmkrnF89UjdRoTwNdjQuYRnXhIat0LFAhf8ZKO06NQ84cXLU7nrdl9pyImrg== X-Received: by 2002:a5e:8b46:0:b0:6e3:203a:715c with SMTP id z6-20020a5e8b46000000b006e3203a715cmr25779885iom.11.1674832268261; Fri, 27 Jan 2023 07:11:08 -0800 (PST) Received: from localhost.localdomain (75-166-146-144.hlrn.qwest.net. [75.166.146.144]) by smtp.gmail.com with ESMTPSA id l35-20020a026663000000b0039e5418fb01sm1506880jaf.73.2023.01.27.07.11.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Jan 2023 07:11:07 -0800 (PST) To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2] Increase size of main_type::nfields Date: Fri, 27 Jan 2023 08:11:01 -0700 Message-Id: <20230127151101.4070956-1-tromey@adacore.com> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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: 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-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" main_type::nfields is a 'short', and has been for many years. PR c++/29985 points out that 'short' is too narrow for an enum that contains more than 2^15 constants. This patch bumps the size of 'nfields'. To verify that the field isn't directly used, it is also renamed. Note that this does not affect the size of main_type on x86-64 Fedora 36. And, if it does have a negative effect somewhere, it's worth considering that types could be shrunk more drastically by using subclasses for the different codes. This is v2 of this patch, which has these changes: * I changed nfields to 'unsigned', per Simon's request. I looked at changing all the uses, but this quickly fans out into a very large patch. (One additional tweak was needed, though.) * I wrote a test case. I discovered that GCC cannot compile a large enough C test case, so I resorted to using the DWARF assembler. This test doesn't reproduce the crash, but it does fail without the patch. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29985 --- gdb/compile/compile-cplus-types.c | 2 +- gdb/gdb-gdb.py.in | 4 +- gdb/gdbtypes.h | 10 ++-- gdb/testsuite/gdb.dwarf2/mega-enum.exp | 67 ++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/mega-enum.exp diff --git a/gdb/compile/compile-cplus-types.c b/gdb/compile/compile-cplus-types.c index 55661156b86..d48aa61b4bc 100644 --- a/gdb/compile/compile-cplus-types.c +++ b/gdb/compile/compile-cplus-types.c @@ -981,7 +981,7 @@ compile_cplus_convert_func (compile_cplus_instance *instance, gcc_type return_type = instance->convert_type (target_type); std::vector elements (type->num_fields ()); - struct gcc_type_array array = { type->num_fields (), elements.data () }; + struct gcc_type_array array = { (int) type->num_fields (), elements.data () }; int artificials = 0; for (int i = 0; i < type->num_fields (); ++i) { diff --git a/gdb/gdb-gdb.py.in b/gdb/gdb-gdb.py.in index dbc4d773e0b..95b7d84966f 100644 --- a/gdb/gdb-gdb.py.in +++ b/gdb/gdb-gdb.py.in @@ -261,8 +261,8 @@ class StructMainTypePrettyPrinter: fields.append("flags = [%s]" % self.flags_to_string()) fields.append("owner = %s" % self.owner_to_string()) fields.append("target_type = %s" % self.val["m_target_type"]) - if self.val["nfields"] > 0: - for fieldno in range(self.val["nfields"]): + if self.val["m_nfields"] > 0: + for fieldno in range(self.val["m_nfields"]): fields.append(self.struct_field_img(fieldno)) if self.val["code"] == gdb.TYPE_CODE_RANGE: fields.append(self.bounds_img()) diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h index 18ea1bca19c..aaed12f97ee 100644 --- a/gdb/gdbtypes.h +++ b/gdb/gdbtypes.h @@ -818,7 +818,7 @@ struct main_type /* * Number of fields described for this type. This field appears at this location because it packs nicely here. */ - short nfields; + unsigned int m_nfields; /* * Name of this type, or NULL if none. @@ -944,15 +944,15 @@ struct type } /* Get the number of fields of this type. */ - int num_fields () const + unsigned int num_fields () const { - return this->main_type->nfields; + return this->main_type->m_nfields; } /* Set the number of fields of this type. */ - void set_num_fields (int num_fields) + void set_num_fields (unsigned int num_fields) { - this->main_type->nfields = num_fields; + this->main_type->m_nfields = num_fields; } /* Get the fields array of this type. */ diff --git a/gdb/testsuite/gdb.dwarf2/mega-enum.exp b/gdb/testsuite/gdb.dwarf2/mega-enum.exp new file mode 100644 index 00000000000..9d47931eb76 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/mega-enum.exp @@ -0,0 +1,67 @@ +# Copyright 2023 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 a huge enum. A colossal enum. If you will, a mega enum. + +load_lib dwarf.exp +require dwarf2_support + +standard_testfile main.c mega-enum.S + +set asm_file [standard_output_file $srcfile2] +Dwarf::assemble $asm_file { + global srcfile + + cu {} { + DW_TAG_compile_unit { + {DW_AT_language @DW_LANG_C} + {DW_AT_name $srcfile} + {DW_AT_comp_dir /tmp} + } { + declare_labels integer_label + + integer_label: DW_TAG_base_type { + {DW_AT_byte_size 4 DW_FORM_sdata} + {DW_AT_encoding @DW_ATE_signed} + {DW_AT_name int} + } + + DW_TAG_enumeration_type { + {DW_AT_name mega_enum} + {DW_AT_type :$integer_label} + } { + # In the past gdb used a 'short' for the field count. + # But this fails if there are too many fields. If the + # field count here is too high, gas takes a long time; + # so use a number that's just big enough. + for {set i 0} {$i < 65538} {incr i} { + DW_TAG_enumerator { + {DW_AT_name DEI_$i} + {DW_AT_const_value $i DW_FORM_sdata} + } + } + } + } + } +} + +if {[prepare_for_testing "failed to prepare" ${testfile} \ + [list $srcfile $asm_file] {nodebug}]} { + return -1 +} + +# The largest known Fermat prime. +gdb_test "print/d DEI_65537" " = 65537" +gdb_test "print (enum mega_enum) 65537" " = DEI_65537"