[v2] limit repeated DIE comparisons

Message ID 20220609111735.1488817-1-gprocida@google.com
State New
Headers
Series [v2] limit repeated DIE comparisons |

Commit Message

Giuliano Procida June 9, 2022, 11:17 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 | 12 ++++++++++++
 1 file changed, 12 insertions(+)
  

Comments

Dodji Seketeli June 9, 2022, 4:14 p.m. UTC | #1
Hello,

Giuliano Procida <gprocida@google.com> a écrit:

I have applied the patch, thanks a lot for it!

I just have one useless comment below, for my own understanding.

> 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 | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
>
> diff --git a/src/abg-dwarf-reader.cc b/src/abg-dwarf-reader.cc
> index 7bf2375d..27dd4faf 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,15 @@ 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;

Interesting.  I'll play with this and see what happens when we hit
this.  I hope I'll find a binary in the testsuite that hits this line,
so that I can understand why the type is compared so many times.

Thanks a lot!

[...]

Cheers,
  

Patch

diff --git a/src/abg-dwarf-reader.cc b/src/abg-dwarf-reader.cc
index 7bf2375d..27dd4faf 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,15 @@  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;
+  else
+    ++visit;
+
   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);