• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Test program for leb128
2    Copyright (C) 2020 Tom Tromey
3    This file is part of elfutils.
4 
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    elfutils is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #include <config.h>
19 #include <stddef.h>
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <libdw.h>
23 #include "../libdw/libdwP.h"
24 #include "../libdw/memory-access.h"
25 
26 #define OK 0
27 #define FAIL 1
28 
29 static const unsigned char v0[] = { 0x0 };
30 static const unsigned char v1[] = { 0x1 };
31 static const unsigned char v23[] = { 23 };
32 static const unsigned char vm_1[] = { 0x7f };
33 static const unsigned char vm_2[] = { 0x7e };
34 static const unsigned char s127[] = { 0xff, 0x00 };
35 static const unsigned char v128[] = { 0x80, 0x01 };
36 static const unsigned char v129[] = { 0x81, 0x01 };
37 static const unsigned char vm_127[] = { 0x81, 0x7f };
38 static const unsigned char vm_128[] = { 0x80, 0x7f };
39 static const unsigned char vm_129[] = { 0xff, 0x7e };
40 static const unsigned char vhuge[] =
41   {
42     0xff, 0xff, 0xff, 0xff, 0xff,
43     0xff, 0xff, 0xff, 0xff, 0x0
44   };
45 static const unsigned char most_positive[] =
46   {
47     0xff, 0xff, 0xff, 0xff, 0xff,
48     0xff, 0xff, 0xff, 0x3f
49   };
50 static const unsigned char most_negative[] =
51   {
52     0x80, 0x80, 0x80, 0x80, 0x80,
53     0x80, 0x80, 0x80, 0x40
54   };
55 static const unsigned char minus_one[] =
56   {
57     0xff, 0xff, 0xff, 0xff, 0xff,
58     0xff, 0xff, 0xff, 0x7f
59   };
60 static const unsigned char int64_max_m1[] =
61   {
62     0xfe, 0xff, 0xff, 0xff, 0xff,
63     0xff, 0xff, 0xff, 0xff, 0x00
64   };
65 static const unsigned char int64_min_p1[] =
66   {
67     0x81, 0x80, 0x80, 0x80, 0x80,
68     0x80, 0x80, 0x80, 0x80, 0x7f
69   };
70 
71 static int
test_one_sleb(const unsigned char * data,size_t len,int64_t expect)72 test_one_sleb (const unsigned char *data, size_t len, int64_t expect)
73 {
74   int64_t value;
75   const unsigned char *p;
76 
77   p = data;
78   get_sleb128 (value, p, p + len);
79   if (value != expect || p != data + len)
80     return FAIL;
81 
82   p = data;
83   get_sleb128_unchecked (value, p);
84   if (value != expect || p != data + len)
85     return FAIL;
86 
87   return OK;
88 }
89 
90 static int
test_sleb(void)91 test_sleb (void)
92 {
93 #define TEST(ARRAY, V)				      \
94   if (test_one_sleb (ARRAY, sizeof (ARRAY), V) != OK) \
95     return FAIL;
96 
97   TEST (v0, 0);
98   TEST (v1, 1);
99   TEST (v23, 23);
100   TEST (vm_1, -1);
101   TEST (vm_2, -2);
102   TEST (s127, 127);
103   TEST (v128, 128);
104   TEST (v129, 129);
105   TEST (vm_127, -127);
106   TEST (vm_128, -128);
107   TEST (vm_129, -129);
108   TEST (vhuge, 9223372036854775807ll);
109   TEST (most_positive, 4611686018427387903ll);
110   TEST (most_negative, -4611686018427387904ll);
111   TEST (minus_one, -1);
112   TEST (int64_max_m1, INT64_MAX - 1);
113   TEST (int64_min_p1, INT64_MIN + 1);
114 
115 #undef TEST
116 
117   return OK;
118 }
119 
120 static int
test_one_uleb(const unsigned char * data,size_t len,uint64_t expect)121 test_one_uleb (const unsigned char *data, size_t len, uint64_t expect)
122 {
123   uint64_t value;
124   const unsigned char *p;
125 
126   p = data;
127   get_uleb128 (value, p, p + len);
128   if (value != expect || p != data + len)
129     return FAIL;
130 
131   p = data;
132   get_uleb128_unchecked (value, p);
133   if (value != expect || p != data + len)
134     return FAIL;
135 
136   return OK;
137 }
138 
139 static int
test_uleb(void)140 test_uleb (void)
141 {
142 #define TEST(ARRAY, V)				      \
143   if (test_one_uleb (ARRAY, sizeof (ARRAY), V) != OK) \
144     return FAIL;
145 
146   TEST (v0, 0);
147   TEST (v1, 1);
148   TEST (v23, 23);
149   TEST (vm_1, 127);
150   TEST (vm_2, 126);
151   TEST (s127, 127);
152   TEST (v128, 128);
153   TEST (v129, 129);
154   TEST (vm_127, 16257);
155   TEST (vm_128, 16256);
156   TEST (vm_129, 16255);
157   TEST (vhuge, 9223372036854775807ull);
158   TEST (most_positive, 4611686018427387903ull);
159   TEST (most_negative, 4611686018427387904ull);
160   TEST (minus_one, 9223372036854775807ull);
161   TEST (int64_max_m1, INT64_MAX - 1);
162   TEST (int64_min_p1, INT64_MIN + 1);
163 
164 #undef TEST
165 
166   return OK;
167 }
168 
169 int
main(void)170 main (void)
171 {
172   return test_sleb () || test_uleb ();
173 }
174