aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/backtrace/patches/010-avoid-recursion.patch
blob: 7efaf1b660ffb271397fe7fc9e44854c3b496352 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
commit 8d7954369c2f1ef62fee65057da9af4dee17032d (HEAD -> pr-8661314, arcadia/users/mikailbag/submit-ac578483-dd24a51-10986d8a-96572213)
merge: ec02af38d2b9facf29be0e6cb9130bd73b2eccf9 1ba32821f8f7452cb6c8b354d82ffb7e2b2f74aa
author: mikailbag
date: 2025-05-05T19:22:33+03:00

    submit for PR 8661314
    
    arc up from 1ba32821f8

diff --git a/dwarf.c b/dwarf.c
--- a/dwarf.c
+++ b/dwarf.c
@@ -1615,8 +1615,7 @@ unit_addrs_search (const void *vkey, const void *ventry)
 
 static int
 resolve_unit_addrs_overlap_walk (struct backtrace_state *state,
-				 size_t *pfrom, size_t *pto,
-				 struct unit_addrs *enclosing,
+				 struct backtrace_vector *enclosing,
 				 struct unit_addrs_vector *old_vec,
 				 backtrace_error_callback error_callback,
 				 void *data,
@@ -1627,25 +1626,36 @@ resolve_unit_addrs_overlap_walk (struct backtrace_state *state,
   struct unit_addrs *new_addrs;
   size_t from;
   size_t to;
+  size_t enclosing_count;
 
   old_addrs = (struct unit_addrs *) old_vec->vec.base;
   old_count = old_vec->count;
   new_addrs = (struct unit_addrs *) new_vec->vec.base;
 
-  for (from = *pfrom, to = *pto; from < old_count; from++, to++)
+  enclosing_count = 0;
+
+  to = 0;
+  for (from = 0; from < old_count; from++)
     {
-      /* If we are in the scope of a larger range that can no longer
-	 cover any further ranges, return back to the caller.  */
+      struct unit_addrs *current_enclosing;
+      new_addrs[to] = old_addrs[from];
+      to++;
+
+      /* While we are in the scope of a larger range that can no longer
+      	 cover any further ranges, pop it from the enclosing stack.  */
+      while (enclosing_count > 0
+        && ((struct unit_addrs**)enclosing->base)[enclosing_count-1]->high <= old_addrs[from].low)
+        {
+          enclosing_count--;
+          enclosing->alc += sizeof (struct unit_addrs*);
+        }
+      if (enclosing_count > 0) {
+        current_enclosing = ((struct unit_addrs**)enclosing->base)[enclosing_count-1];
+      } else {
+        current_enclosing = NULL;
+      }
 
-      if (enclosing != NULL
-	  && enclosing->high <= old_addrs[from].low)
-	{
-	  *pfrom = from;
-	  *pto = to;
-	  return 1;
-	}
 
-      new_addrs[to] = old_addrs[from];
 
       /* If we are in scope of a larger range, fill in any gaps
 	 between this entry and the next one.
@@ -1653,65 +1663,63 @@ resolve_unit_addrs_overlap_walk (struct backtrace_state *state,
 	 There is an extra entry at the end of the vector, so it's
 	 always OK to refer to from + 1.  */
 
-      if (enclosing != NULL
-	  && enclosing->high > old_addrs[from].high
-	  && old_addrs[from].high < old_addrs[from + 1].low)
-	{
-	  void *grew;
-	  size_t new_high;
+      if (current_enclosing != NULL
+	      && current_enclosing->high > old_addrs[from].high
+	      && old_addrs[from].high < old_addrs[from + 1].low)
+	      {
+	        void *grew;
+	        size_t new_high;
 
-	  grew = backtrace_vector_grow (state, sizeof (struct unit_addrs),
+	        grew = backtrace_vector_grow (state, sizeof (struct unit_addrs),
 					error_callback, data, &new_vec->vec);
-	  if (grew == NULL)
-	    return 0;
-	  new_addrs = (struct unit_addrs *) new_vec->vec.base;
-	  to++;
-	  new_addrs[to].low = old_addrs[from].high;
-	  new_high = old_addrs[from + 1].low;
-	  if (enclosing->high < new_high)
-	    new_high = enclosing->high;
-	  new_addrs[to].high = new_high;
-	  new_addrs[to].u = enclosing->u;
-	}
+	        if (grew == NULL)
+	          return 0;
+	        new_addrs = (struct unit_addrs *) new_vec->vec.base;
+	        new_addrs[to].low = old_addrs[from].high;
+	        new_high = old_addrs[from + 1].low;
+	        if (current_enclosing->high < new_high)
+	          new_high = current_enclosing->high;
+	        new_addrs[to].high = new_high;
+	        new_addrs[to].u = current_enclosing->u;
+          to++;
+	      }
 
       /* If this range has a larger scope than the next one, use it to
-	 fill in any gaps.  */
+	     fill in any gaps.  */
 
       if (old_addrs[from].high > old_addrs[from + 1].high)
-	{
-	  *pfrom = from + 1;
-	  *pto = to + 1;
-	  if (!resolve_unit_addrs_overlap_walk (state, pfrom, pto,
-						&old_addrs[from], old_vec,
-						error_callback, data, new_vec))
-	    return 0;
-	  from = *pfrom;
-	  to = *pto;
-
-	  /* Undo the increment the loop is about to do.  */
-	  from--;
-	  to--;
-	}
+	      {
+	        void* grew;
+          struct unit_addrs **enclosing_top;
+
+          grew = backtrace_vector_grow (state, sizeof (struct unit_addrs *),
+                error_callback, data, enclosing);
+          if (grew == NULL)
+            return 0;
+          enclosing_top = ((struct unit_addrs **) (enclosing->base)) + enclosing_count;
+    
+          *enclosing_top = &old_addrs[from];
+	      }
     }
 
-  if (enclosing == NULL)
-    {
-      struct unit_addrs *pa;
+  
+  
+  struct unit_addrs *pa;
 
-      /* Add trailing entry.  */
+  /* Add trailing entry.  */
 
-      pa = ((struct unit_addrs *)
-	    backtrace_vector_grow (state, sizeof (struct unit_addrs),
-				   error_callback, data, &new_vec->vec));
-      if (pa == NULL)
-	return 0;
-      pa->low = 0;
-      --pa->low;
-      pa->high = pa->low;
-      pa->u = NULL;
+  pa = ((struct unit_addrs *)
+	backtrace_vector_grow (state, sizeof (struct unit_addrs),
+				error_callback, data, &new_vec->vec));
+  if (pa == NULL)
+	  return 0;
+  pa->low = 0;
+  --pa->low;
+  pa->high = pa->low;
+  pa->u = NULL;
 
-      new_vec->count = to;
-    }
+  new_vec->count = to;
+  
 
   return 1;
 }
@@ -1756,8 +1764,8 @@ resolve_unit_addrs_overlap (struct backtrace_state *state,
   size_t i;
   struct unit_addrs_vector new_vec;
   void *grew;
-  size_t from;
-  size_t to;
+  int walk_ok;
+  struct backtrace_vector enclosing;
 
   addrs = (struct unit_addrs *) addrs_vec->vec.base;
   count = addrs_vec->count;
@@ -1787,15 +1795,16 @@ resolve_unit_addrs_overlap (struct backtrace_state *state,
 				error_callback, data, &new_vec.vec);
   if (grew == NULL)
     return 0;
+  memset (&enclosing, 0, sizeof enclosing);
 
-  from = 0;
-  to = 0;
-  resolve_unit_addrs_overlap_walk (state, &from, &to, NULL, addrs_vec,
+  walk_ok = resolve_unit_addrs_overlap_walk (state, &enclosing, addrs_vec,
 				   error_callback, data, &new_vec);
   backtrace_vector_free (state, &addrs_vec->vec, error_callback, data);
-  *addrs_vec = new_vec;
+  backtrace_vector_free (state, &enclosing, error_callback, data);
+  if (walk_ok)
+    *addrs_vec = new_vec;
 
-  return 1;
+  return walk_ok;
 }
 
 /* Sort the line vector by PC.  We want a stable sort here to maintain