1 //===- implTest.cpp -------------------------------------------------------===//
2 //
3 // The MCLinker Project
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "mcld/Support/LEB128.h"
10 #include "LEB128Test.h"
11
12 #include "mcld/Support/SystemUtils.h"
13 #include <ctime>
14 #include <cstdlib>
15
16 using namespace mcld;
17 using namespace mcldtest;
18
19 // Constructor can do set-up work for all test here.
LEB128Test()20 LEB128Test::LEB128Test() {
21 // Initialize the seed for random number generator using during the tests.
22 sys::SetRandomSeed(::time(NULL));
23 }
24
25 // Destructor can do clean-up work that doesn't throw exceptions here.
~LEB128Test()26 LEB128Test::~LEB128Test() {
27 }
28
29 // SetUp() will be called immediately before each test.
SetUp()30 void LEB128Test::SetUp() {
31 }
32
33 // TearDown() will be called immediately after each test.
TearDown()34 void LEB128Test::TearDown() {
35 }
36
37 //==========================================================================//
38 // Testcases
39 //
40
TEST_F(LEB128Test,EncodeULEB_Example_from_Dwarf3_Figure22_Using_32bits)41 TEST_F(LEB128Test, EncodeULEB_Example_from_Dwarf3_Figure22_Using_32bits) {
42 leb128::ByteType buffer[2];
43 leb128::ByteType* result;
44 size_t size;
45
46 result = buffer;
47 size = leb128::encode<uint32_t>(result, 2);
48 ASSERT_EQ(buffer[0], 2);
49 ASSERT_EQ(result, buffer + 1);
50 ASSERT_TRUE(size == 1);
51
52 result = buffer;
53 size = leb128::encode<uint32_t>(result, 127);
54 ASSERT_EQ(buffer[0], 127);
55 ASSERT_EQ(result, buffer + 1);
56 ASSERT_TRUE(size == 1);
57
58 result = buffer;
59 size = leb128::encode<uint32_t>(result, 128);
60 ASSERT_EQ(buffer[0], 0 + 0x80);
61 ASSERT_EQ(buffer[1], 1);
62 ASSERT_EQ(result, buffer + 2);
63 ASSERT_TRUE(size == 2);
64
65 result = buffer;
66 size = leb128::encode<uint32_t>(result, 129);
67 ASSERT_EQ(buffer[0], 1 + 0x80);
68 ASSERT_EQ(buffer[1], 1);
69 ASSERT_EQ(result, buffer + 2);
70 ASSERT_TRUE(size == 2);
71
72 result = buffer;
73 size = leb128::encode<uint32_t>(result, 130);
74 ASSERT_EQ(buffer[0], 2 + 0x80);
75 ASSERT_EQ(buffer[1], 1);
76 ASSERT_EQ(result, buffer + 2);
77 ASSERT_TRUE(size == 2);
78
79 result = buffer;
80 size = leb128::encode<uint32_t>(result, 12857);
81 ASSERT_EQ(buffer[0], 57 + 0x80);
82 ASSERT_EQ(buffer[1], 100);
83 ASSERT_EQ(result, buffer + 2);
84 ASSERT_TRUE(size == 2);
85 }
86
TEST_F(LEB128Test,EncodeULEB_Example_from_Dwarf3_Figure22_Using_64bits)87 TEST_F(LEB128Test, EncodeULEB_Example_from_Dwarf3_Figure22_Using_64bits) {
88 leb128::ByteType buffer[2];
89 leb128::ByteType* result;
90 size_t size;
91
92 result = buffer;
93 size = leb128::encode<uint64_t>(result, 2);
94 ASSERT_EQ(buffer[0], 2);
95 ASSERT_EQ(result, buffer + 1);
96 ASSERT_TRUE(size == 1);
97
98 result = buffer;
99 size = leb128::encode<uint64_t>(result, 127);
100 ASSERT_EQ(buffer[0], 127);
101 ASSERT_EQ(result, buffer + 1);
102 ASSERT_TRUE(size == 1);
103
104 result = buffer;
105 size = leb128::encode<uint64_t>(result, 128);
106 ASSERT_EQ(buffer[0], 0 + 0x80);
107 ASSERT_EQ(buffer[1], 1);
108 ASSERT_EQ(result, buffer + 2);
109 ASSERT_TRUE(size == 2);
110
111 result = buffer;
112 size = leb128::encode<uint64_t>(result, 129);
113 ASSERT_EQ(buffer[0], 1 + 0x80);
114 ASSERT_EQ(buffer[1], 1);
115 ASSERT_EQ(result, buffer + 2);
116 ASSERT_TRUE(size == 2);
117
118 result = buffer;
119 size = leb128::encode<uint64_t>(result, 130);
120 ASSERT_EQ(buffer[0], 2 + 0x80);
121 ASSERT_EQ(buffer[1], 1);
122 ASSERT_EQ(result, buffer + 2);
123 ASSERT_TRUE(size == 2);
124
125 result = buffer;
126 size = leb128::encode<uint64_t>(result, 12857);
127 ASSERT_EQ(buffer[0], 57 + 0x80);
128 ASSERT_EQ(buffer[1], 100);
129 ASSERT_EQ(result, buffer + 2);
130 ASSERT_TRUE(size == 2);
131 }
132
TEST_F(LEB128Test,EncodeSLEB_Example_from_Dwarf3_Figure22)133 TEST_F(LEB128Test, EncodeSLEB_Example_from_Dwarf3_Figure22) {
134 leb128::ByteType buffer[2];
135 leb128::ByteType* result;
136 size_t size;
137
138 result = buffer;
139 size = leb128::encode<int32_t>(result, 2);
140 ASSERT_EQ(buffer[0], 2);
141 ASSERT_EQ(result, buffer + 1);
142 ASSERT_TRUE(size == 1);
143
144 result = buffer;
145 size = leb128::encode<int32_t>(result, -2);
146 ASSERT_EQ(buffer[0], 0x7e);
147 ASSERT_EQ(result, buffer + 1);
148 ASSERT_TRUE(size == 1);
149
150 result = buffer;
151 size = leb128::encode<int32_t>(result, 127);
152 ASSERT_EQ(buffer[0], 127 + 0x80);
153 ASSERT_EQ(buffer[1], 0);
154 ASSERT_EQ(result, buffer + 2);
155 ASSERT_TRUE(size == 2);
156
157 result = buffer;
158 size = leb128::encode<int32_t>(result, -127);
159 ASSERT_EQ(buffer[0], 1 + 0x80);
160 ASSERT_EQ(buffer[1], 0x7f);
161 ASSERT_EQ(result, buffer + 2);
162 ASSERT_TRUE(size == 2);
163
164 result = buffer;
165 size = leb128::encode<int32_t>(result, 128);
166 ASSERT_EQ(buffer[0], 0 + 0x80);
167 ASSERT_EQ(buffer[1], 1);
168 ASSERT_EQ(result, buffer + 2);
169 ASSERT_TRUE(size == 2);
170
171 result = buffer;
172 size = leb128::encode<int32_t>(result, -128);
173 ASSERT_EQ(buffer[0], 0 + 0x80);
174 ASSERT_EQ(buffer[1], 0x7f);
175 ASSERT_EQ(result, buffer + 2);
176 ASSERT_TRUE(size == 2);
177
178 result = buffer;
179 size = leb128::encode<int32_t>(result, 129);
180 ASSERT_EQ(buffer[0], 1 + 0x80);
181 ASSERT_EQ(buffer[1], 1);
182 ASSERT_EQ(result, buffer + 2);
183 ASSERT_TRUE(size == 2);
184
185 result = buffer;
186 size = leb128::encode<int32_t>(result, -129);
187 ASSERT_EQ(buffer[0], 0x7f + 0x80);
188 ASSERT_EQ(buffer[1], 0x7e);
189 ASSERT_EQ(result, buffer + 2);
190 ASSERT_TRUE(size == 2);
191 }
192
TEST_F(LEB128Test,DecodeULEB_Example_from_Dwarf3_Figure22)193 TEST_F(LEB128Test, DecodeULEB_Example_from_Dwarf3_Figure22) {
194 leb128::ByteType buffer[2];
195 size_t size;
196
197 buffer[0] = 2;
198 buffer[1] = 0;
199 ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 2);
200 ASSERT_TRUE(size == 1);
201
202 buffer[0] = 127;
203 buffer[1] = 0;
204 ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 127);
205 ASSERT_TRUE(size == 1);
206
207 buffer[0] = 0 + 0x80;
208 buffer[1] = 1;
209 ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 128);
210 ASSERT_TRUE(size == 2);
211
212 buffer[0] = 1 + 0x80;
213 buffer[1] = 1;
214 ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 129);
215 ASSERT_TRUE(size == 2);
216
217 buffer[0] = 2 + 0x80;
218 buffer[1] = 1;
219 ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 130);
220 ASSERT_TRUE(size == 2);
221
222 buffer[0] = 57 + 0x80;
223 buffer[1] = 100;
224 ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 12857);
225 ASSERT_TRUE(size == 2);
226 }
227
TEST_F(LEB128Test,DecodeSLEB_Example_from_Dwarf3_Figure22)228 TEST_F(LEB128Test, DecodeSLEB_Example_from_Dwarf3_Figure22) {
229 leb128::ByteType buffer[2];
230 size_t size;
231
232 buffer[0] = 2;
233 buffer[1] = 0;
234 ASSERT_EQ(leb128::decode<int64_t>(buffer, size), 2);
235 ASSERT_TRUE(size == 1);
236
237 buffer[0] = 0x7e;
238 buffer[1] = 0;
239 ASSERT_EQ(leb128::decode<int64_t>(buffer, size), -2);
240 ASSERT_TRUE(size == 1);
241
242 buffer[0] = 127 + 0x80;
243 buffer[1] = 0;
244 ASSERT_EQ(leb128::decode<int64_t>(buffer, size), 127);
245 ASSERT_TRUE(size == 2);
246
247 buffer[0] = 1 + 0x80;
248 buffer[1] = 0x7f;
249 ASSERT_EQ(leb128::decode<int64_t>(buffer, size), -127);
250 ASSERT_TRUE(size == 2);
251
252 buffer[0] = 0 + 0x80;
253 buffer[1] = 1;
254 ASSERT_EQ(leb128::decode<int64_t>(buffer, size), 128);
255 ASSERT_TRUE(size == 2);
256
257 buffer[0] = 0 + 0x80;
258 buffer[1] = 0x7f;
259 ASSERT_EQ(leb128::decode<int64_t>(buffer, size), -128);
260 ASSERT_TRUE(size == 2);
261
262 buffer[0] = 1 + 0x80;
263 buffer[1] = 1;
264 ASSERT_EQ(leb128::decode<int64_t>(buffer, size), 129);
265 ASSERT_TRUE(size == 2);
266
267 buffer[0] = 0x7f + 0x80;
268 buffer[1] = 0x7e;
269 ASSERT_EQ(leb128::decode<int64_t>(buffer, size), -129);
270 ASSERT_TRUE(size == 2);
271 }
272
TEST_F(LEB128Test,DecodeULEB_Tests_Found_in_Android_dalvik_dx)273 TEST_F(LEB128Test, DecodeULEB_Tests_Found_in_Android_dalvik_dx) {
274 leb128::ByteType content[2];
275 const leb128::ByteType* p;
276
277 content[0] = 0;
278 p = content;
279 ASSERT_TRUE(leb128::decode<uint64_t>(p) == 0);
280 ASSERT_EQ(p, content + 1);
281
282 content[0] = 1;
283 p = content;
284 ASSERT_TRUE(leb128::decode<uint64_t>(p) == 1);
285 ASSERT_EQ(p, content + 1);
286
287 content[0] = 0x80;
288 content[1] = 0x7f;
289 p = content;
290 ASSERT_TRUE(leb128::decode<uint64_t>(p) == 16256);
291 ASSERT_EQ(p, content + 2);
292 }
293
TEST_F(LEB128Test,EncodeULEB_Tests_Found_in_Android_dalvik_dx)294 TEST_F(LEB128Test, EncodeULEB_Tests_Found_in_Android_dalvik_dx) {
295 leb128::ByteType buffer[5];
296 leb128::ByteType* result;
297 size_t size;
298
299 result = buffer;
300 size = leb128::encode<uint32_t>(result, 0);
301 ASSERT_EQ(buffer[0], 0);
302 ASSERT_EQ(result, buffer + 1);
303 ASSERT_TRUE(size == 1);
304
305 result = buffer;
306 size = leb128::encode<uint64_t>(result, 0);
307 ASSERT_EQ(buffer[0], 0);
308 ASSERT_EQ(result, buffer + 1);
309 ASSERT_TRUE(size == 1);
310
311 result = buffer;
312 size = leb128::encode<uint32_t>(result, 1);
313 ASSERT_EQ(buffer[0], 1);
314 ASSERT_EQ(result, buffer + 1);
315 ASSERT_TRUE(size == 1);
316
317 result = buffer;
318 size = leb128::encode<uint64_t>(result, 1);
319 ASSERT_EQ(buffer[0], 1);
320 ASSERT_EQ(result, buffer + 1);
321 ASSERT_TRUE(size == 1);
322
323 result = buffer;
324 size = leb128::encode<uint32_t>(result, 16256);
325 ASSERT_EQ(buffer[0], 0x80);
326 ASSERT_EQ(buffer[1], 0x7f);
327 ASSERT_EQ(result, buffer + 2);
328 ASSERT_TRUE(size == 2);
329
330 result = buffer;
331 size = leb128::encode<uint64_t>(result, 16256);
332 ASSERT_EQ(buffer[0], 0x80);
333 ASSERT_EQ(buffer[1], 0x7f);
334 ASSERT_EQ(result, buffer + 2);
335 ASSERT_TRUE(size == 2);
336
337 result = buffer;
338 size = leb128::encode<uint32_t>(result, 0x3b4);
339 ASSERT_EQ(buffer[0], 0xb4);
340 ASSERT_EQ(buffer[1], 0x07);
341 ASSERT_EQ(result, buffer + 2);
342 ASSERT_TRUE(size == 2);
343
344 result = buffer;
345 size = leb128::encode<uint64_t>(result, 0x3b4);
346 ASSERT_EQ(buffer[0], 0xb4);
347 ASSERT_EQ(buffer[1], 0x07);
348 ASSERT_EQ(result, buffer + 2);
349 ASSERT_TRUE(size == 2);
350
351 result = buffer;
352 size = leb128::encode<uint32_t>(result, 0x40c);
353 ASSERT_EQ(buffer[0], 0x8c);
354 ASSERT_EQ(buffer[1], 0x08);
355 ASSERT_EQ(result, buffer + 2);
356 ASSERT_TRUE(size == 2);
357
358 result = buffer;
359 size = leb128::encode<uint64_t>(result, 0x40c);
360 ASSERT_EQ(buffer[0], 0x8c);
361 ASSERT_EQ(buffer[1], 0x08);
362 ASSERT_EQ(result, buffer + 2);
363 ASSERT_TRUE(size == 2);
364
365 result = buffer;
366 size = leb128::encode<uint32_t>(result, 0xffffffff);
367 ASSERT_EQ(buffer[0], 0xff);
368 ASSERT_EQ(buffer[1], 0xff);
369 ASSERT_EQ(buffer[2], 0xff);
370 ASSERT_EQ(buffer[3], 0xff);
371 ASSERT_EQ(buffer[4], 0xf);
372 ASSERT_EQ(result, buffer + 5);
373 ASSERT_TRUE(size == 5);
374
375 result = buffer;
376 size = leb128::encode<uint64_t>(result, 0xffffffff);
377 ASSERT_EQ(buffer[0], 0xff);
378 ASSERT_EQ(buffer[1], 0xff);
379 ASSERT_EQ(buffer[2], 0xff);
380 ASSERT_EQ(buffer[3], 0xff);
381 ASSERT_EQ(buffer[4], 0xf);
382 ASSERT_EQ(result, buffer + 5);
383 ASSERT_TRUE(size == 5);
384 }
385
TEST_F(LEB128Test,DecodeSLEB_Tests_Found_in_Android_dalvik_dx)386 TEST_F(LEB128Test, DecodeSLEB_Tests_Found_in_Android_dalvik_dx) {
387 leb128::ByteType content[2];
388 const leb128::ByteType* p;
389
390 content[0] = 0;
391 p = content;
392 ASSERT_EQ(leb128::decode<int64_t>(p), 0);
393 ASSERT_EQ(p, content + 1);
394
395 content[0] = 1;
396 p = content;
397 ASSERT_EQ(leb128::decode<int64_t>(p), 1);
398 ASSERT_EQ(p, content + 1);
399
400 content[0] = 0x7f;
401 p = content;
402 ASSERT_EQ(leb128::decode<int64_t>(p), -1);
403 ASSERT_EQ(p, content + 1);
404
405 content[0] = 0x3c;
406 p = content;
407 ASSERT_EQ(leb128::decode<int64_t>(p), 0x3c);
408 ASSERT_EQ(p, content + 1);
409 }
410
TEST_F(LEB128Test,EncodeSLEB_Tests_Found_in_Android_dalvik_dx)411 TEST_F(LEB128Test, EncodeSLEB_Tests_Found_in_Android_dalvik_dx) {
412 leb128::ByteType buffer[5];
413 leb128::ByteType* result;
414 size_t size;
415
416 result = buffer;
417 size = leb128::encode<int32_t>(result, 0);
418 ASSERT_EQ(buffer[0], 0);
419 ASSERT_EQ(result, buffer + 1);
420 ASSERT_TRUE(size == 1);
421
422 result = buffer;
423 size = leb128::encode<int64_t>(result, 0);
424 ASSERT_EQ(buffer[0], 0);
425 ASSERT_EQ(result, buffer + 1);
426 ASSERT_TRUE(size == 1);
427
428 result = buffer;
429 size = leb128::encode<int32_t>(result, 1);
430 ASSERT_EQ(buffer[0], 1);
431 ASSERT_EQ(result, buffer + 1);
432 ASSERT_TRUE(size == 1);
433
434 result = buffer;
435 size = leb128::encode<int64_t>(result, 1);
436 ASSERT_EQ(buffer[0], 1);
437 ASSERT_EQ(result, buffer + 1);
438 ASSERT_TRUE(size == 1);
439
440 result = buffer;
441 size = leb128::encode<int32_t>(result, -1);
442 ASSERT_EQ(buffer[0], 0x7f);
443 ASSERT_EQ(result, buffer + 1);
444 ASSERT_TRUE(size == 1);
445
446 result = buffer;
447 size = leb128::encode<int64_t>(result, -1);
448 ASSERT_EQ(buffer[0], 0x7f);
449 ASSERT_EQ(result, buffer + 1);
450 ASSERT_TRUE(size == 1);
451 }
452
TEST_F(LEB128Test,Random_Regression_Test)453 TEST_F(LEB128Test, Random_Regression_Test) {
454 leb128::ByteType buffer[9];
455
456 for (int i = 0; i < 20; i++) {
457 unsigned long int value = sys::GetRandomNum();
458 uint64_t value2 = value * value;
459 int64_t value3 = value * value;
460 leb128::ByteType* result;
461 size_t encode_size, decode_size;
462
463 // Test encode<uint32_t> and decode<uint64_t> on value
464 result = buffer;
465 encode_size = leb128::encode<uint32_t>(result, value);
466 ASSERT_EQ(result, buffer + encode_size);
467 ASSERT_EQ(leb128::decode<uint64_t>(buffer, decode_size), value);
468 ASSERT_EQ(encode_size, decode_size);
469
470 // Test encode<uint64_t> and decode<uint64_t> on (value * value)
471 result = buffer;
472 encode_size = leb128::encode<uint64_t>(result, value2);
473 ASSERT_EQ(result, buffer + encode_size);
474 ASSERT_EQ(leb128::decode<uint64_t>(buffer, decode_size), value2);
475 ASSERT_EQ(encode_size, decode_size);
476
477 // Test encode<uint64_t> and decode<uint64_t> on (value * value)
478 result = buffer;
479 encode_size = leb128::encode<int64_t>(result, value3);
480 ASSERT_EQ(result, buffer + encode_size);
481 ASSERT_EQ(leb128::decode<int64_t>(buffer, decode_size), value3);
482 ASSERT_EQ(encode_size, decode_size);
483
484 // Test encode<uint64_t> and decode<uint64_t> on -(value * value)
485 result = buffer;
486 encode_size = leb128::encode<int64_t>(result, -value3);
487 ASSERT_EQ(result, buffer + encode_size);
488 ASSERT_EQ(leb128::decode<int64_t>(buffer, decode_size), -value3);
489 ASSERT_EQ(encode_size, decode_size);
490 }
491 }
492
TEST_F(LEB128Test,Other_Test)493 TEST_F(LEB128Test, Other_Test) {
494 leb128::ByteType buffer[5];
495 leb128::ByteType* result;
496 size_t size;
497
498 result = buffer;
499 leb128::encode<uint64_t>(result, 154452);
500 ASSERT_EQ(result, buffer + 3);
501 ASSERT_EQ(buffer[0], 0xd4);
502 ASSERT_EQ(buffer[1], 0xb6);
503 ASSERT_EQ(buffer[2], 0x9);
504
505 ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 154452);
506 ASSERT_TRUE(size == 3);
507 }
508
TEST_F(LEB128Test,Type_Conversion_Test)509 TEST_F(LEB128Test, Type_Conversion_Test) {
510 char buffer[5];
511 char* result;
512 size_t size;
513
514 result = buffer;
515 leb128::encode<uint64_t>(result, 154452);
516 ASSERT_EQ(result, buffer + 3);
517 ASSERT_EQ(buffer[0], '\xd4');
518 ASSERT_EQ(buffer[1], '\xb6');
519 ASSERT_EQ(buffer[2], '\x09');
520
521 ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 154452);
522 ASSERT_TRUE(size == 3);
523
524 const char* p = buffer;
525 ASSERT_TRUE(leb128::decode<uint64_t>(p) == 154452);
526 ASSERT_TRUE(p == (buffer + 3));
527 }
528