limit repeated DIE comparisons

Message ID 20220609110627.1469252-1-gprocida@google.com
State New
Headers
Series limit repeated DIE comparisons |

Commit Message

Giuliano Procida June 9, 2022, 11:06 a.m. UTC
  Exponential explosion of DIE comparison has been possible since the
limit of at most 5 pending struct/union DIE comparison pairs was
lifted.

This commit adds two things to control this (with a negligible chance of
falsely finding that two DIEs are equivalent when they are not).

- DIE self-comparisons immediately return true
- once a DIE pair has been compared 10000 times, always return true

	* src/abg-dwarf-reader.cc (read_context): Add mutable
	die_comparison_visits_ member.
	(compare_dies): Return true if this is a self-comparison.
	Return true if we have visited this comparison 10000 times.

Signed-off-by: Giuliano Procida <gprocida@google.com>
---
 src/abg-dwarf-reader.cc | 10 ++++++++++
 1 file changed, 10 insertions(+)
  

Patch

diff --git a/src/abg-dwarf-reader.cc b/src/abg-dwarf-reader.cc
index 7bf2375d..11436bb0 100644
--- a/src/abg-dwarf-reader.cc
+++ b/src/abg-dwarf-reader.cc
@@ -2094,6 +2094,9 @@  public:
   /// A map that associates a function type representations to
   /// function types, inside a translation unit.
   mutable istring_fn_type_map_type per_tu_repr_to_fn_type_maps_;
+  mutable std::unordered_map<std::pair<Dwarf_Off, Dwarf_Off>,
+			     size_t,
+			     dwarf_offset_pair_hash> die_comparison_visits_;
 
   die_class_or_union_map_type	die_wip_classes_map_;
   die_class_or_union_map_type	alternate_die_wip_classes_map_;
@@ -10216,6 +10219,13 @@  compare_dies(const read_context& ctxt,
 
   Dwarf_Off l_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(l)),
     r_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(r));
+
+  if (l_offset == r_offset)
+    return true;
+  auto& visit = ctxt.die_comparison_visits_[std::make_pair(l_offset, r_offset)];
+  if (++visit == 10000)
+    return true;
+
   Dwarf_Off l_canonical_die_offset = 0, r_canonical_die_offset = 0;
   const die_source l_die_source = ctxt.get_die_source(l);
   const die_source r_die_source = ctxt.get_die_source(r);