xref: /aosp_15_r20/external/elfutils/tests/leb128.c (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
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_sleb_safety(void)121 test_sleb_safety (void)
122 {
123   const int64_t expected_error = INT64_MAX;
124   int64_t value;
125   const unsigned char *test = NULL;
126   get_sleb128 (value, test, test);
127   if (value != expected_error)
128     return FAIL;
129 
130   return OK;
131 }
132 
133 static int
test_one_uleb(const unsigned char * data,size_t len,uint64_t expect)134 test_one_uleb (const unsigned char *data, size_t len, uint64_t expect)
135 {
136   uint64_t value;
137   const unsigned char *p;
138 
139   p = data;
140   get_uleb128 (value, p, p + len);
141   if (value != expect || p != data + len)
142     return FAIL;
143 
144   p = data;
145   get_uleb128_unchecked (value, p);
146   if (value != expect || p != data + len)
147     return FAIL;
148 
149   return OK;
150 }
151 
152 static int
test_uleb(void)153 test_uleb (void)
154 {
155 #define TEST(ARRAY, V)				      \
156   if (test_one_uleb (ARRAY, sizeof (ARRAY), V) != OK) \
157     return FAIL;
158 
159   TEST (v0, 0);
160   TEST (v1, 1);
161   TEST (v23, 23);
162   TEST (vm_1, 127);
163   TEST (vm_2, 126);
164   TEST (s127, 127);
165   TEST (v128, 128);
166   TEST (v129, 129);
167   TEST (vm_127, 16257);
168   TEST (vm_128, 16256);
169   TEST (vm_129, 16255);
170   TEST (vhuge, 9223372036854775807ull);
171   TEST (most_positive, 4611686018427387903ull);
172   TEST (most_negative, 4611686018427387904ull);
173   TEST (minus_one, 9223372036854775807ull);
174   TEST (int64_max_m1, INT64_MAX - 1);
175   TEST (int64_min_p1, INT64_MIN + 1);
176 
177 #undef TEST
178 
179   return OK;
180 }
181 
182 static int
test_uleb_safety(void)183 test_uleb_safety (void)
184 {
185   const uint64_t expected_error = UINT64_MAX;
186   uint64_t value;
187   const unsigned char *test = NULL;
188   get_uleb128 (value, test, test);
189   if (value != expected_error)
190     return FAIL;
191 
192   return OK;
193 }
194 
195 int
main(void)196 main (void)
197 {
198   return test_sleb () || test_sleb_safety () || test_uleb ()
199 	 || test_uleb_safety ();
200 }
201