From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from rock.gnat.com (rock.gnat.com [IPv6:2620:20:4000:0:a9e:1ff:fe9b:1d1]) by sourceware.org (Postfix) with ESMTPS id 5AB113858D33 for ; Wed, 28 Dec 2022 12:46:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5AB113858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 21287116304; Wed, 28 Dec 2022 07:46:33 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id jqewHsGVlnxu; Wed, 28 Dec 2022 07:46:33 -0500 (EST) Received: from free.home (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPS id DD2D5116213; Wed, 28 Dec 2022 07:46:32 -0500 (EST) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 2BSCkLkR743977 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 28 Dec 2022 09:46:22 -0300 From: Alexandre Oliva To: Martin =?utf-8?Q?Li=C5=A1ka?= Cc: gcc-patches@gcc.gnu.org Subject: [16/17] check hash table counts at expand Organization: Free thinker, does not speak for AdaCore References: Errors-To: aoliva@lxoliva.fsfla.org Date: Wed, 28 Dec 2022 09:46:20 -0300 In-Reply-To: ("Martin \=\?utf-8\?Q\?Li\=C5\=A1ka\=22's\?\= message of "Wed, 28 Dec 2022 09:50:11 +0100") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_STATUS,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 List-Id: On Dec 28, 2022, Martin Li=C5=A1ka wrote: > I really like the verification code you added. It's quite similar to what > I added to hash-table.h: *nod* Prompted by your encouragement, I've combined the element count verification code with the verify() loop, and also added it to expand, where it can be done cheaply. Add cheap verification of element and deleted entry counts during expand and hash verify. Regstrapped on x86_64-linux-gnu. Ok to install? for gcc/ChangeLog * hash-table.h (expand): Check elements and deleted counts. (verify): Likewise. --- gcc/hash-table.h | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/gcc/hash-table.h b/gcc/hash-table.h index 53507daae26c3..f4bda6102323e 100644 --- a/gcc/hash-table.h +++ b/gcc/hash-table.h @@ -805,19 +805,28 @@ hash_table::expand () hash_table_usage ().release_instance_overhead (this, sizeof (value_typ= e) * osize); =20 + size_t n_deleted =3D m_n_deleted; + m_entries =3D nentries; m_size =3D nsize; m_size_prime_index =3D nindex; m_n_elements -=3D m_n_deleted; m_n_deleted =3D 0; =20 + size_t n_elements =3D m_n_elements; + value_type *p =3D oentries; do { value_type &x =3D *p; =20 - if (!is_empty (x) && !is_deleted (x)) + if (is_empty (x)) + ; + else if (is_deleted (x)) + n_deleted--; + else { + n_elements--; value_type *q =3D find_empty_slot_for_expand (Descriptor::hash (= x)); new ((void*) q) value_type (std::move (x)); /* After the resources of 'x' have been moved to a new object at 'q', @@ -829,6 +838,8 @@ hash_table::expand () } while (p < olimit); =20 + gcc_checking_assert (!n_elements && !n_deleted); + if (!m_ggc) Allocator ::data_free (oentries); else @@ -1018,8 +1029,9 @@ hash_table return &m_entries[index]; } =20 -/* Verify that all existing elements in th hash table which are - equal to COMPARABLE have an equal HASH value provided as argument. */ +/* Verify that all existing elements in the hash table which are + equal to COMPARABLE have an equal HASH value provided as argument. + Also check that the hash table element counts are correct. */ =20 template class Allocator> @@ -1027,14 +1039,23 @@ void hash_table ::verify (const compare_type &comparable, hashval_t hash) { + size_t n_elements =3D m_n_elements; + size_t n_deleted =3D m_n_deleted; for (size_t i =3D 0; i < MIN (hash_table_sanitize_eq_limit, m_size); i++) { value_type *entry =3D &m_entries[i]; - if (!is_empty (*entry) && !is_deleted (*entry) - && hash !=3D Descriptor::hash (*entry) - && Descriptor::equal (*entry, comparable)) - hashtab_chk_error (); + if (!is_empty (*entry)) + { + n_elements--; + if (is_deleted (*entry)) + n_deleted--; + else if (hash !=3D Descriptor::hash (*entry) + && Descriptor::equal (*entry, comparable)) + hashtab_chk_error (); + } } + if (hash_table_sanitize_eq_limit >=3D m_size) + gcc_checking_assert (!n_elements && !n_deleted); } =20 /* This function deletes an element with the given COMPARABLE value --=20 Alexandre Oliva, happy hacker https://FSFLA.org/blogs/lxo/ Free Software Activist GNU Toolchain Engineer Disinformation flourishes because many people care deeply about injustice but very few check the facts. Ask me about