Debian Codegraph @

src/lib/libdwarf/dwarf_leb.c

raw
1 /*
2 Copyright (C) 2000,2004 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright 2011-2020 David Anderson. 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 #include <config.h>
31
32 #include <stddef.h> /* size_t */
33
34 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
35 #include "stdafx.h"
36 #endif /* HAVE_STDAFX_H */
37
38 #include "dwarf.h"
39 #include "libdwarf.h"
40 #include "libdwarf_private.h"
41 #include "dwarf_base_types.h"
42 #include "dwarf_opaque.h"
43 #include "dwarf_error.h"
44 #include "dwarf_util.h"
45
46 #define MORE_BYTES 0x80
47 #define DATA_MASK 0x7f
48 #define DIGIT_WIDTH 7
49 #define SIGN_BIT 0x40
50
51 /* Note that with 'make check')
52 many of the test items
53 only make sense if Dwarf_Unsigned (and Dwarf_Signed)
54 are 64 bits. The encode/decode logic should
55 be fine whether those types are 64 or 32 bits.
56 See runtests.sh */
57
58 /* The encode/decode functions here are public */
59
60 /* 10 bytes of leb, 7 bits each part of the number, gives
61 room for a 64bit number.
62 While any number of leading zeroes would be legal, so
63 no max is really truly required here, why would a
64 compiler generate leading zeros (for unsigned leb)?
65 That would seem strange except in rare circumstances
66 a compiler may want, for overall alignment, to
67 add extra bytes..
68
69 So we allow more than 10 as it is legal for a compiler to
70 generate an leb with correct but useless trailing
71 zero bytes (note the interaction with sign in the signed case).
72 The value of BYTESLEBMAX is arbitrary but allows catching
73 corrupt data in a short time.
74 Before April 2021 BYTESLEBMAX was 10 as it was assumed
75 there were no 'useless' 0x80 high-order bytes in an LEB.
76 (0x80 meaning the 7 bits 'value' is zero)
77 However in DWARF6 (and at least one compiler before
78 DWARF6) a few high order bytes are allowed as padding.
79
80 */
81 #define BYTESLEBMAX 24
82 #define BITSPERBYTE 8
83
84 /* When an leb value needs to reveal its length,
85 but the value is not needed */
86 int
87 _dwarf_skip_leb128(char * leb128,
88 Dwarf_Unsigned * leb128_length,
89 char * endptr)
90 {
91 unsigned byte = 0;
92 /* The byte_length value will be a small non-negative integer. */
93 unsigned byte_length = 1;
94
95 if (leb128 >=endptr) {
96 return DW_DLV_ERROR;
97 }
98
99 byte = *(unsigned char *)leb128;
100 if ((byte & 0x80) == 0) {
101 *leb128_length = 1;
102 return DW_DLV_OK;
103 } else {
104 unsigned byte2 = 0;
105 if ((leb128+1) >=endptr) {
106 return DW_DLV_ERROR;
107 }
108 byte2 = *(unsigned char *)(leb128 + 1);
109 if ((byte2 & 0x80) == 0) {
110 *leb128_length = 2;
111 return DW_DLV_OK;
112 }
113 /* Gets messy to hand-inline more byte checking.
114 One or two byte leb is very frequent. */
115 }
116
117 ++byte_length;
118 ++leb128;
119 /* Validity of leb128+1 checked above */
120 for (;;byte_length++,leb128++) {
121 if (leb128 >= endptr) {
122 /* Off end of available space. */
123 return DW_DLV_ERROR;
124 }
125 byte = *(unsigned char *)leb128;
126 if (byte & 0x80) {
127 if (byte_length >= BYTESLEBMAX) {
128 /* Too long. Not sane length. */
129 return DW_DLV_ERROR;
130 }
131 continue;
132 }
133 break;
134 }
135 *leb128_length = byte_length;
136 return DW_DLV_OK;
137 }
138 /* Decode ULEB with checking.
139 Casting leb128 to (unsigned char *) as
140 the signedness of char * is unpredictable in C */
141 int
142 dwarf_decode_leb128(char * leb128,
143 Dwarf_Unsigned * leb128_length,
144 Dwarf_Unsigned *outval,
145 char * endptr)
146 {
147 unsigned long byte = 0;
148 Dwarf_Unsigned word_number = 0;
149 Dwarf_Unsigned number = 0;
150 unsigned long shift = 0; /* at least 32 bits, even Win32 */
151 /* The byte_length value will be a small non-negative integer. */
152 unsigned int byte_length = 0;
153
154 if (leb128 >=endptr) {
155 return DW_DLV_ERROR;
156 }
157 /* The following unrolls-the-loop for the first two bytes and
158 unpacks into 32 bits to make this as fast as possible.
159 word_number is assumed big enough that the shift has a defined
160 result. */
161 byte = *(unsigned char *)leb128;
162 if ((byte & 0x80) == 0) {
163 if (leb128_length) {
164 *leb128_length = 1;
165 }
166 if (outval) {
167 *outval = byte;
168 }
169 return DW_DLV_OK;
170 } else {
171 unsigned long byte2 = 0;
172 if ((leb128+1) >=endptr) {
173 return DW_DLV_ERROR;
174 }
175 byte2 = *(unsigned char *)(leb128 + 1);
176 if ((byte2 & 0x80) == 0) {
177 if (leb128_length) {
178 *leb128_length = 2;
179 }
180 word_number = byte & 0x7f;
181 word_number |= (byte2 & 0x7f) << 7;
182 if (outval) {
183 *outval = word_number;
184 }
185 return DW_DLV_OK;
186 }
187 /* Gets messy to hand-inline more byte checking. */
188 }
189
190 /* The rest handles long numbers. Because the 'number'
191 may be larger than the default int/unsigned,
192 we must cast the 'byte' before
193 the shift for the shift to have a defined result. */
194 number = 0;
195 shift = 0;
196 byte_length = 1;
197 for (;;) {
198 unsigned int b = byte & 0x7f;
199 if (shift >= (sizeof(number)*BITSPERBYTE)) {
200 /* Shift is large. Maybe corrupt value,
201 maybe some padding high-end byte zeroes
202 that we can ignore. */
203 if (!b) {
204 if (byte_length >= BYTESLEBMAX) {
205 /* Erroneous input. */
206 if (leb128_length) {
207 *leb128_length = BYTESLEBMAX;
208 }
209 return DW_DLV_ERROR;
210 }
211 ++leb128;
212 /* shift cannot overflow as
213 BYTESLEBMAX is not a large value */
214 shift += 7;
215 if (leb128 >=endptr ) {
216 if (leb128 == endptr && !byte) {
217 /* Meaning zero bits a padding byte */
218 if (leb128_length) {
219 *leb128_length = byte_length;
220 }
221 if (outval) {
222 *outval = number;
223 }
224 return DW_DLV_OK;
225 }
226 return DW_DLV_ERROR;
227 }
228 ++byte_length;
229 byte = *(unsigned char *)leb128;
230 continue;
231 }
232 /* Too big, corrupt data given the non-zero
233 byte content */
234 return DW_DLV_ERROR;
235 }
236 number |= ((Dwarf_Unsigned)b << shift);
237 if ((byte & 0x80) == 0) {
238 if (leb128_length) {
239 *leb128_length = byte_length;
240 }
241 if (outval) {
242 *outval = number;
243 }
244 return DW_DLV_OK;
245 }
246 shift += 7;
247 byte_length++;
248 if (byte_length > BYTESLEBMAX) {
249 /* Erroneous input. */
250 if (leb128_length) {
251 *leb128_length = BYTESLEBMAX;
252 }
253 break;
254 }
255 ++leb128;
256 if (leb128 >= endptr) {
257 return DW_DLV_ERROR;
258 }
259 byte = *(unsigned char *)leb128;
260 }
261 return DW_DLV_ERROR;
262 }
263
264 /* Decode SLEB with checking
265 Casting leb128 to (unsigned char *) as
266 the signedness of char * is unpredictable
267 in C */
268 int
269 dwarf_decode_signed_leb128(char * leb128,
270 Dwarf_Unsigned * leb128_length,
271 Dwarf_Signed *outval,char * endptr)
272 {
273 Dwarf_Unsigned byte = 0;
274 unsigned int b = 0;
275 Dwarf_Signed number = 0;
276 unsigned long shift = 0;
277 int sign = FALSE;
278 /* The byte_length value will be a small non-negative integer. */
279 unsigned int byte_length = 1;
280
281 /* byte_length being the number of bytes
282 of data absorbed so far in
283 turning the leb into a Dwarf_Signed. */
284 if (!outval) {
285 return DW_DLV_ERROR;
286 }
287 if (leb128 >= endptr) {
288 return DW_DLV_ERROR;
289 }
290 byte = *(unsigned char *)leb128;
291 for (;;) {
292 b = byte & 0x7f;
293 if (shift >= (sizeof(number)*BITSPERBYTE)) {
294 /* Shift is large. Maybe corrupt value,
295 maybe some padding high-end byte zeroes
296 that we can ignore (but notice sign bit
297 from the last usable byte). */
298 sign = b & 0x40;
299 if (!byte || byte == 0x40) {
300 /* The value is complete. */
301 break;
302 }
303 if (b == 0) {
304 ++byte_length;
305 if (byte_length > BYTESLEBMAX) {
306 /* Erroneous input. */
307 if (leb128_length) {
308 *leb128_length = BYTESLEBMAX;
309 }
310 return DW_DLV_ERROR;
311 }
312 ++leb128;
313 /* shift cannot overflow as
314 BYTESLEBMAX is not a large value */
315 shift += 7;
316 if (leb128 >= endptr) {
317 return DW_DLV_ERROR;
318 }
319 byte = *(unsigned char *)leb128;
320 continue;
321 }
322 /* Too big, corrupt data given the non-zero
323 byte content */
324 return DW_DLV_ERROR;
325 }
326 /* This bit of the last byte indicates sign */
327 sign = b & 0x40;
328 number |= ((Dwarf_Unsigned)b) << shift;
329 shift += 7;
330 if ((byte & 0x80) == 0) {
331 break;
332 }
333 ++leb128;
334 if (leb128 >= endptr) {
335 return DW_DLV_ERROR;
336 }
337 byte = *(unsigned char *)leb128;
338 byte_length++;
339 if (byte_length > BYTESLEBMAX) {
340 /* Erroneous input. */
341 if (leb128_length) {
342 *leb128_length = BYTESLEBMAX;
343 }
344 return DW_DLV_ERROR;
345 }
346 }
347 if (sign) {
348 /* The following avoids undefined behavior. */
349 unsigned int shiftlim = sizeof(Dwarf_Signed) * BITSPERBYTE -1;
350 if (shift < shiftlim) {
351 Dwarf_Signed y = (Dwarf_Signed)
352 (((Dwarf_Unsigned)1) << shift);
353 Dwarf_Signed x = -y;
354 number |= x;
355 } else if (shift == shiftlim) {
356 Dwarf_Signed x= (((Dwarf_Unsigned)1) << shift);
357 number |= x;
358 } else {
359 /* trailing zeroes case */
360 Dwarf_Signed x= (((Dwarf_Unsigned)1) << shiftlim);
361 number |= x;
362 }
363 }
364 if (leb128_length) {
365 *leb128_length = byte_length;
366 }
367 *outval = number;
368 return DW_DLV_OK;
369 }
370
371 /* Encode val as a uleb128. This encodes it as an unsigned
372 number.
373 Return DW_DLV_ERROR or DW_DLV_OK.
374 space to write leb number is provided by caller, with caller
375 passing length.
376 number of bytes used returned thru nbytes arg.
377 This never emits padding, it emits the minimum
378 number of bytes that can hold the value. */
379 int dwarf_encode_leb128(Dwarf_Unsigned val, int *nbytes,
380 char *space, int splen)
381 {
382 char *a;
383 char *end = space + splen;
384
385 a = space;
386 do {
387 unsigned char uc;
388
389 if (a >= end) {
390 return DW_DLV_ERROR;
391 }
392 uc = val & DATA_MASK;
393 val >>= DIGIT_WIDTH;
394 if (val != 0) {
395 uc |= MORE_BYTES;
396 }
397 *a = uc;
398 a++;
399 } while (val);
400 *nbytes = (int)(a - space);
401 return DW_DLV_OK;
402 }
403
404 /* This never emits padding at the end, so it
405 says nothing about what such would look like
406 for a negative value. */
407 int dwarf_encode_signed_leb128(Dwarf_Signed value, int *nbytes,
408 char *space, int splen)
409 {
410 char *str;
411 Dwarf_Signed sign = -(value < 0);
412 int more = 1;
413 char *end = space + splen;
414
415 str = space;
416
417 do {
418 unsigned char byte = value & DATA_MASK;
419
420 value >>= DIGIT_WIDTH;
421
422 if (str >= end) {
423 return DW_DLV_ERROR;
424 }
425 /* Remaining chunks would just contain the sign
426 bit, and this chunk
427 has already captured at least one sign bit. */
428 if (value == sign &&
429 ((byte & SIGN_BIT) == (sign & SIGN_BIT))) {
430 more = 0;
431 } else {
432 byte |= MORE_BYTES;
433 }
434 *str = byte;
435 str++;
436 } while (more);
437 *nbytes = (int)(str - space);
438 return DW_DLV_OK;
439 }