From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <3N0A7YQgKCrQajliWcXUaiiafY.WigfcVUVcaUcfmiolWYqUlY.ila@flex--gprocida.bounces.google.com> Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by sourceware.org (Postfix) with ESMTPS id 22CB4384A88A for ; Fri, 10 Sep 2021 11:23:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 22CB4384A88A Received: by mail-qt1-x849.google.com with SMTP id x28-20020ac8701c000000b0029f4b940566so4909315qtm.19 for ; Fri, 10 Sep 2021 04:23:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ER1TOFUJ5OFEYlck3D6dMrV5/bpq+Qz2ApAvpwitnyU=; b=e+kjKBufq7ft0m9ODFmuA6RVtMKJWRz/pzJdsjwsJNBoW2vCRw2y5o+pGi1W8wWva5 ESAQF4IEfGjXtXbEO5u4BcZbrbz68bMSMNuk8NU7F/dUUvimvoXWQ8rZ+0vd5atfugS2 5+B51PAxgMfLhjhl6qBmhzRE6zrTEDjAeHmaEprjNRZOM/j3Wc2XVUb9kBEj6+kamC3W Ev1eVlF36L01AQ0ZNqkg0cdIu0g2H13TOmHo40TKZCDHSSHJCd3D92RgA2wmqdIVdFX1 bEpM3SfF3REaknriO4YPdclJUcAG/XJNY1PLTGNqs6V2tF0HppNbn+q6cCLK2K18vYDd FIdg== X-Gm-Message-State: AOAM531dXeiKj6PpAjPHhjDOQp7A1YJA/kJ+bK7FFLqUEIvYFCPUJ0cJ XptMCNLbWzQ8F6+uy2ASIeSKt6jWJIYQ0NHVcE3shanTJuCSDSnQ+3+bRJR9SmF3SYzxIjlFnUg XfaGjzxMrrdBq9VZGsGNCh1uLmxA84xZKnhv5pz294SFgJ/g4PtaVq0kRPAdaHONVzg3eTOw= X-Google-Smtp-Source: ABdhPJy9x3niRsAQdfneN6TcqrDS/rplyIsySb/8RsW1gm4T9lcMHoCR79qWvDaI2HJwFNa3/kMWg4Tl4D2k9g== X-Received: from tef.lon.corp.google.com ([2a00:79e0:d:210:fe2b:14b7:895d:9020]) (user=gprocida job=sendgmr) by 2002:a05:6214:11e5:: with SMTP id e5mr7892672qvu.55.1631273015693; Fri, 10 Sep 2021 04:23:35 -0700 (PDT) Date: Fri, 10 Sep 2021 12:23:24 +0100 In-Reply-To: <20210910112325.806676-1-gprocida@google.com> Message-Id: <20210910112325.806676-4-gprocida@google.com> Mime-Version: 1.0 References: <20210910112325.806676-1-gprocida@google.com> X-Mailer: git-send-email 2.33.0.309.g3052b89438-goog Subject: [PATCH 3/4] abg-writer: allow debugging of equality / hash issues From: Giuliano Procida To: libabigail@sourceware.org Cc: dodji@seketeli.org, kernel-team@android.com, gprocida@google.com, maennich@google.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-21.9 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL 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: libabigail@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Mailing list of the Libabigail project List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 10 Sep 2021 11:23:37 -0000 Inconsistenencies between equality and hash values can and have caused hard-to-debug issues. There are quite a few unordered sets and maps in the XML writer that use deep_ptr_eq_functor and type_hasher. New ./configure option: --enable-debug-equality-hash-consistency A simple way of tracking down potential issues is to check for inconsistencies any time a type is looked up or inserted into a container. * src/abg-writer.cc (get_id_for_type): Add a debugging stanza that, before a type is looked up or inserted, asserts that if it is equal to an existing mapped type then their hash values must be the same. Signed-off-by: Giuliano Procida --- configure.ac | 16 ++++++++ src/abg-writer.cc | 102 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 98 insertions(+), 20 deletions(-) diff --git a/configure.ac b/configure.ac index 735cc9de..8870a7e7 100644 --- a/configure.ac +++ b/configure.ac @@ -72,6 +72,12 @@ AC_ARG_ENABLE(debug-self-comparison, ENABLE_DEBUG_SELF_COMPARISON=$enableval, ENABLE_DEBUG_SELF_COMPARISON=no) +AC_ARG_ENABLE(debug-equality-hash-consistency, + AS_HELP_STRING([--enable-debug-equality-hash-consistency=yes|no], + [enable debugging of equality / hash value consistency in unordered maps (default is no)]), + ENABLE_DEBUG_EQUALITY_HASH_CONSISTENCY=$enableval, + ENABLE_DEBUG_EQUALITY_HASH_CONSISTENCY=no) + AC_ARG_ENABLE(deb, AS_HELP_STRING([--enable-deb=yes|no|auto], [enable the support of deb in abipkgdiff (default is auto)]), @@ -313,6 +319,16 @@ fi AM_CONDITIONAL(ENABLE_DEBUG_SELF_COMPARISON, test x$ENABLE_DEBUG_SELF_COMPARISON = xyes) +dnl enable the debugging of equality / hash value consistency in the abidw XML writer +if test x$ENABLE_DEBUG_EQUALITY_HASH_CONSISTENCY = xyes; then + AC_DEFINE([WITH_DEBUG_EQUALITY_HASH_CONSISTENCY], 1, [compile equality / hash value consistency checks]) + AC_MSG_NOTICE([support of debugging equality / hash value consistency is enabled]) +else + AC_MSG_NOTICE([support of debugging equality / hash value consistency is disabled]) +fi + +AM_CONDITIONAL(ENABLE_DEBUG_EQUALITY_HASH_CONSISTENCY, test x$ENABLE_DEBUG_EQUALITY_HASH_CONSISTENCY = xyes) + dnl Check for the dpkg program if test x$ENABLE_DEB = xauto -o x$ENABLE_DEB = xyes; then AC_CHECK_PROG(HAS_DPKG, dpkg, yes, no) diff --git a/src/abg-writer.cc b/src/abg-writer.cc index 795ca685..cf664720 100644 --- a/src/abg-writer.cc +++ b/src/abg-writer.cc @@ -115,6 +115,41 @@ struct type_hasher {return hash_type(t);} }; // end struct type_hasher +template +static void check_set_equality_hash_consistency( + const T& haystack, const typename T::key_type& needle) +{ +#ifdef WITH_DEBUG_EQUALITY_HASH_CONSISTENCY + abigail::diff_utils::deep_ptr_eq_functor eq; + type_hasher hasher; + const auto needle_hash = hasher(needle); + for (const auto& key : haystack) + ABG_ASSERT(!eq(needle, key) || needle_hash == hasher(key)); +#else + (void) haystack; + (void) needle; +#endif +} + +template +static void check_map_equality_hash_consistency( + const T& haystack, const typename T::key_type& needle) +{ +#ifdef WITH_DEBUG_EQUALITY_HASH_CONSISTENCY + abigail::diff_utils::deep_ptr_eq_functor eq; + type_hasher hasher; + const auto needle_hash = hasher(needle); + for (const auto& key_value : haystack) + { + const auto& key = key_value.first; + ABG_ASSERT(!eq(key, needle) || needle_hash == hasher(key)); + } +#else + (void) haystack; + (void) needle; +#endif +} + /// A convenience typedef for a map that associates a pointer to type /// to a string. The pointer to type is hashed as fast as possible. typedef unordered_map(t); + check_map_equality_hash_consistency(m_type_id_map, c); auto insertion = m_type_id_map.emplace(c, interned_string()); interned_string& id = insertion.first->second; @@ -544,14 +580,25 @@ public: { // If the type is a function type, record it in a dedicated data // structure. - if (function_type* f = is_function_type(t.get())) - m_referenced_fn_types_set.insert(f); - else if (!t->get_naked_canonical_type()) - // If the type doesn't have a canonical type, record it in a - // dedicated data structure. - m_referenced_non_canonical_types_set.insert(t.get()); + type_base* t_ptr = t.get(); + if (function_type* f = is_function_type(t_ptr)) + { + check_set_equality_hash_consistency(m_referenced_fn_types_set, f); + m_referenced_fn_types_set.insert(f); + } + else if (!t_ptr->get_naked_canonical_type()) + { + // If the type doesn't have a canonical type, record it in a + // dedicated data structure. + check_set_equality_hash_consistency( + m_referenced_non_canonical_types_set, t_ptr); + m_referenced_non_canonical_types_set.insert(t_ptr); + } else - m_referenced_types_set.insert(t.get()); + { + check_set_equality_hash_consistency(m_referenced_types_set, t_ptr); + m_referenced_types_set.insert(t_ptr); + } } /// Test if a given type has been referenced by a pointer, a @@ -564,15 +611,26 @@ public: bool type_is_referenced(const type_base_sptr& t) { - if (function_type *f = is_function_type(t.get())) - return (m_referenced_fn_types_set.find(f) - != m_referenced_fn_types_set.end()); - else if (!t->get_naked_canonical_type()) - return (m_referenced_non_canonical_types_set.find(t.get()) - != m_referenced_non_canonical_types_set.end()); + type_base* t_ptr = t.get(); + if (function_type *f = is_function_type(t_ptr)) + { + check_set_equality_hash_consistency(m_referenced_fn_types_set, f); + return (m_referenced_fn_types_set.find(f) + != m_referenced_fn_types_set.end()); + } + else if (!t_ptr->get_naked_canonical_type()) + { + check_set_equality_hash_consistency( + m_referenced_non_canonical_types_set, t_ptr); + return (m_referenced_non_canonical_types_set.find(t_ptr) + != m_referenced_non_canonical_types_set.end()); + } else - return m_referenced_types_set.find - (t.get()) != m_referenced_types_set.end(); + { + check_set_equality_hash_consistency(m_referenced_types_set, t_ptr); + return (m_referenced_types_set.find(t_ptr) + != m_referenced_types_set.end()); + } } /// A comparison functor to compare pointers to @ref type_base. @@ -727,6 +785,7 @@ public: type_base *c = t->get_naked_canonical_type(); if (c == 0) c = const_cast(t); + check_set_equality_hash_consistency(m_emitted_type_set, c); m_emitted_type_set.insert(c); } @@ -739,6 +798,7 @@ public: bool type_is_emitted(const type_base *t) const { + check_set_equality_hash_consistency(m_emitted_type_set, t); return m_emitted_type_set.find(t) != m_emitted_type_set.end(); } @@ -792,6 +852,7 @@ public: { class_or_union* cl = is_class_or_union_type(t); ABG_ASSERT(cl && cl->get_is_declaration_only()); + check_set_equality_hash_consistency(m_emitted_decl_only_set, t); m_emitted_decl_only_set.insert(t); } @@ -814,10 +875,8 @@ public: bool decl_only_type_is_emitted(const type_base* t) const { - type_ptr_set_type::const_iterator i = m_emitted_decl_only_set.find(t); - if (i == m_emitted_decl_only_set.end()) - return false; - return true; + check_set_equality_hash_consistency(m_emitted_decl_only_set, t); + return m_emitted_decl_only_set.find(t) != m_emitted_decl_only_set.end(); } /// Test if a declaration-only class has been emitted. @@ -2273,7 +2332,10 @@ record_unemitted_type(type_ptr_set_type& types, == tu.get_absolute_path()) && !ctxt.type_is_emitted(t) && !ctxt.decl_only_type_is_emitted(t)) - types.insert(t); + { + check_set_equality_hash_consistency(types, t); + types.insert(t); + } } /// Serialize a translation unit to an output stream. -- 2.33.0.309.g3052b89438-goog