Debian Codegraph @

src/lib/libdwarf/dwarf_harmless.c

raw
1 /*
2 Copyright (C) 2010-2022 David Anderson. All Rights Reserved.
3 Portions Copyright 2012 SN Systems Ltd. All rights reserved.
4
5 This program is free software; you can redistribute it
6 and/or modify it under the terms of version 2.1 of the
7 GNU Lesser General Public License as published by the Free
8 Software Foundation.
9
10 This program is distributed in the hope that it would be
11 useful, but WITHOUT ANY WARRANTY; without even the implied
12 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE.
14
15 Further, this software is distributed without any warranty
16 that it is free of the rightful claim of any third person
17 regarding infringement or the like. Any license provided
18 herein, whether implied or otherwise, applies only to this
19 software file. Patent licenses, if any, provided herein
20 do not apply to combinations of this program with other
21 software, or any other product whatsoever.
22
23 You should have received a copy of the GNU Lesser General
24 Public License along with this program; if not, write the
25 Free Software Foundation, Inc., 51 Franklin Street - Fifth
26 Floor, Boston MA 02110-1301, USA.
27
28 */
29
30 /* This implements _dwarf_insert_harmless_error
31 and related helper functions for recording
32 compiler errors that need not make the input
33 unusable.
34
35 Applications can use dwarf_get_harmless_error_list to
36 find (and possibly print) a warning about such errors.
37
38 The initial error reported here is
39 DW_DLE_DEBUG_FRAME_LENGTH_NOT_MULTIPLE which was a
40 bug in a specific compiler.
41
42 It is a fixed length circular list to constrain
43 the space used for errors.
44
45 The assumption is that these errors are exceedingly
46 rare, and indicate a broken compiler (the one that
47 produced the object getting the error(s)).
48
49 dh_maxcount is recorded internally as 1 greater than
50 requested. Hiding the fact we always leave one
51 slot unused (at least). So a user request for
52 N slots really gives the user N usable slots. */
53
54 #include <config.h>
55
56 #include <stddef.h> /* size_t */
57 #include <stdlib.h> /* free() malloc() */
58 #include <string.h> /* memcpy() strcpy() strlen() */
59
60 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
61 #include "stdafx.h"
62 #endif /* HAVE_STDAFX_H */
63
64 #include "dwarf.h"
65 #include "libdwarf.h"
66 #include "libdwarf_private.h"
67 #include "dwarf_base_types.h"
68 #include "dwarf_safe_strcpy.h"
69 #include "dwarf_opaque.h"
70 #include "dwarf_util.h"
71 #include "dwarf_frame.h"
72 #include "dwarf_harmless.h"
73
74 /* Not user configurable. */
75 #define DW_HARMLESS_ERROR_MSG_STRING_SIZE 300
76
77 /* The pointers returned here through errmsg_ptrs_array
78 become invalidated by any call to libdwarf. Any call.
79 */
80 int
81 dwarf_get_harmless_error_list(Dwarf_Debug dbg,
82 unsigned count,
83 const char ** errmsg_ptrs_array,
84 unsigned * errs_count)
85 {
86
87 struct Dwarf_Harmless_s *dhp = 0;
88
89 if (IS_INVALID_DBG(dbg)) {
90 return DW_DLV_NO_ENTRY;
91 }
92 dhp = &dbg->de_harmless_errors;
93 if (!dhp->dh_errors) {
94 dhp->dh_errs_count = 0;
95 return DW_DLV_NO_ENTRY;
96 }
97 if (dhp->dh_errs_count == 0) {
98 return DW_DLV_NO_ENTRY;
99 }
100 if (errs_count) {
101 *errs_count = dhp->dh_errs_count;
102 }
103 if (count) {
104 /* NULL terminate the array of pointers */
105 --count;
106 errmsg_ptrs_array[count] = 0;
107
108 if (dhp->dh_next_to_use != dhp->dh_first) {
109 unsigned i = 0;
110 unsigned cur = dhp->dh_first;
111 for (i = 0; cur != dhp->dh_next_to_use;
112 ++i, cur = (cur +1) % dhp->dh_maxcount) {
113 if (i >= count ) {
114 /* All output spaces are used. */
115 break;
116 }
117 errmsg_ptrs_array[i] = dhp->dh_errors[cur];
118 }
119 errmsg_ptrs_array[i] = 0;
120 }
121 }
122 dhp->dh_next_to_use = 0;
123 dhp->dh_first = 0;
124 dhp->dh_errs_count = 0;
125 return DW_DLV_OK;
126 }
127
128 /* Insertion made public is only for testing the harmless error code,
129 it is not necessarily useful for libdwarf client code aside
130 from code testing libdwarf. */
131 void
132 dwarf_insert_harmless_error(Dwarf_Debug dbg,
133 char *newerror)
134 {
135 struct Dwarf_Harmless_s *dhp = 0;
136 unsigned next = 0;
137 unsigned cur = 0;
138 char *msgspace = 0;
139
140 if (IS_INVALID_DBG(dbg)) {
141 return;
142 }
143 dhp = &dbg->de_harmless_errors;
144 cur = dhp->dh_next_to_use;
145 if (!dhp->dh_errors) {
146 dhp->dh_errs_count++;
147 return;
148 }
149 msgspace = dhp->dh_errors[cur];
150 _dwarf_safe_strcpy(msgspace,
151 DW_HARMLESS_ERROR_MSG_STRING_SIZE,
152 newerror,
153 strlen(newerror));
154 next = (cur+1) % dhp->dh_maxcount;
155 dhp->dh_errs_count++;
156 dhp->dh_next_to_use = next;
157 if (dhp->dh_next_to_use == dhp->dh_first) {
158 /* Array is full set full invariant. */
159 dhp->dh_first = (dhp->dh_first+1) % dhp->dh_maxcount;
160 }
161 }
162
163 /* The size of the circular list of strings may be set
164 and reset as desired. Returns the previous size of
165 the list. If the list is shortened excess error entries
166 are simply dropped.
167 If the reallocation fails the list size is left unchanged.
168 Do not make this a long list!
169
170 Remember the maxcount we record is 1 > the user count,
171 so we adjust it so it looks like the user count.
172 */
173 unsigned
174 dwarf_set_harmless_error_list_size(Dwarf_Debug dbg,
175 unsigned maxcount )
176 {
177 struct Dwarf_Harmless_s *dhp = 0;
178 unsigned prevcount = 0;
179
180 if (IS_INVALID_DBG(dbg)) {
181 return 0;
182 }
183 dhp = &dbg->de_harmless_errors;
184 prevcount = dhp->dh_maxcount;
185 if (maxcount != 0) {
186 ++maxcount;
187 if (maxcount != dhp->dh_maxcount) {
188 /* Assign transfers 'ownership' of the malloc areas
189 to oldarray. */
190 struct Dwarf_Harmless_s oldarray = *dhp;
191 /* Do not double increment the max, the init() func
192 increments it too. */
193 _dwarf_harmless_init(dhp,maxcount-1);
194 if (oldarray.dh_next_to_use != oldarray.dh_first) {
195 unsigned i = 0;
196 for (i = oldarray.dh_first;
197 i != oldarray.dh_next_to_use;
198 i = (i+1)%oldarray.dh_maxcount) {
199 dwarf_insert_harmless_error(dbg,
200 oldarray.dh_errors[i]);
201 }
202 if (oldarray.dh_errs_count > dhp->dh_errs_count) {
203 dhp->dh_errs_count = oldarray.dh_errs_count;
204 }
205 }
206 _dwarf_harmless_cleanout(&oldarray);
207 }
208 }
209 return prevcount-1;
210 }
211
212 /* Only callable from within libdwarf (as a practical matter)
213 */
214 void
215 _dwarf_harmless_init(struct Dwarf_Harmless_s *dhp,unsigned size)
216 {
217 unsigned i = 0;
218 memset(dhp,0,sizeof(*dhp));
219 dhp->dh_maxcount = size +1;
220 dhp->dh_errors = (char **)calloc(dhp->dh_maxcount,
221 sizeof(char *));
222 if (!dhp->dh_errors) {
223 dhp->dh_maxcount = 0;
224 return;
225 }
226 for (i = 0; i < dhp->dh_maxcount; ++i) {
227 char *newstr =
228 (char *)calloc(1, DW_HARMLESS_ERROR_MSG_STRING_SIZE);
229 dhp->dh_errors[i] = newstr;
230 #if 0 /* Commentary about avoiding leak */
231 /* BAD IDEA. just use the NULL pointer,
232 so we avoid problems later with
233 freeing. */
234 if (!newstr) {
235 dhp->dh_maxcount = 0;
236 /* Let it leak, the leak is a constrained amount. */
237 free(dhp->dh_errors);
238 dhp->dh_errors = 0;
239 return;
240 }
241 #endif /* 0 */
242 dhp->dh_errors[i] = newstr;
243 }
244 }
245
246 void
247 _dwarf_harmless_cleanout(struct Dwarf_Harmless_s *dhp)
248 {
249 unsigned i = 0;
250 if (!dhp->dh_errors) {
251 return;
252 }
253 for (i = 0; i < dhp->dh_maxcount; ++i) {
254 free(dhp->dh_errors[i]);
255 dhp->dh_errors[i] = 0;
256 }
257 free(dhp->dh_errors);
258 dhp->dh_errors = 0;
259 dhp->dh_maxcount = 0;
260 }