Debian Codegraph @

tests/test_language_c.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 from functools import reduce
4 import operator
5
6 from drgn import (
7 AbsenceReason,
8 Object,
9 Qualifiers,
10 Type,
11 TypeEnumerator,
12 TypeMember,
13 TypeParameter,
14 cast,
15 container_of,
16 implicit_convert,
17 )
18 from tests import MockProgramTestCase
19
20
21 class TestPrettyPrintTypeName(MockProgramTestCase):
22 def assertTypeName(self, type, expected, same_as_definition=False):
23 self.assertEqual(type.type_name(), expected)
24 if same_as_definition:
25 self.assertEqual(str(type), expected)
26
27 def assertQualifiedTypeName(self, expected, same_as_definition, constructor, *args):
28 self.assertEqual(constructor(*args).type_name(), expected)
29 qualifiers = [
30 (Qualifiers.CONST, "const"),
31 (Qualifiers.VOLATILE, "volatile"),
32 (Qualifiers.RESTRICT, "restrict"),
33 (Qualifiers.ATOMIC, "_Atomic"),
34 ]
35 for qualifier in qualifiers:
36 t = constructor(*args, qualifiers=qualifier[0])
37 self.assertTypeName(t, qualifier[1] + " " + expected, same_as_definition)
38
39 # All qualifiers.
40 t = constructor(
41 *args,
42 qualifiers=reduce(operator.or_, (qualifier[0] for qualifier in qualifiers)),
43 )
44 self.assertTypeName(
45 t,
46 " ".join(qualifier[1] for qualifier in qualifiers) + " " + expected,
47 same_as_definition,
48 )
49
50 def test_void(self):
51 self.assertQualifiedTypeName("void", True, self.prog.void_type)
52
53 def test_int(self):
54 self.assertQualifiedTypeName("int", True, self.prog.int_type, "int", 4, True)
55
56 def test_bool(self):
57 self.assertQualifiedTypeName("_Bool", True, self.prog.bool_type, "_Bool", 1)
58
59 def test_float(self):
60 self.assertQualifiedTypeName("float", True, self.prog.float_type, "float", 4)
61
62 def test_struct(self):
63 self.assertQualifiedTypeName(
64 "struct point", True, self.prog.struct_type, "point"
65 )
66 self.assertQualifiedTypeName(
67 "struct <anonymous>", False, self.prog.struct_type, None
68 )
69
70 def test_union(self):
71 self.assertQualifiedTypeName(
72 "union option", True, self.prog.union_type, "option"
73 ),
74 self.assertQualifiedTypeName(
75 "union <anonymous>", False, self.prog.union_type, None
76 )
77
78 def test_class(self):
79 self.assertQualifiedTypeName("class coord", True, self.prog.class_type, "coord")
80 self.assertQualifiedTypeName(
81 "class <anonymous>", False, self.prog.class_type, None
82 )
83
84 def test_enum(self):
85 self.assertQualifiedTypeName(
86 "enum color", True, self.prog.enum_type, "color", None, None
87 ),
88 self.assertQualifiedTypeName(
89 "enum <anonymous>", False, self.prog.enum_type, None, None, None
90 )
91
92 def test_typedef(self):
93 self.assertQualifiedTypeName(
94 "bool",
95 False,
96 self.prog.typedef_type,
97 "bool",
98 self.prog.bool_type("_Bool", 1),
99 )
100
101 def test_pointer(self):
102 self.assertTypeName(
103 self.prog.pointer_type(self.prog.void_type()), "void *", True
104 )
105 t = self.prog.pointer_type(self.prog.void_type(qualifiers=Qualifiers.VOLATILE))
106 self.assertTypeName(t, "volatile void *", True)
107 t = self.prog.pointer_type(
108 self.prog.void_type(qualifiers=Qualifiers.VOLATILE),
109 qualifiers=Qualifiers.CONST,
110 )
111 self.assertTypeName(t, "volatile void * const", True)
112 t = self.prog.pointer_type(t)
113 self.assertTypeName(t, "volatile void * const *", True)
114
115 def test_array(self):
116 i = self.prog.int_type("int", 4, True)
117 self.assertTypeName(self.prog.array_type(i), "int []", True)
118 self.assertTypeName(self.prog.array_type(i, 2), "int [2]", True)
119 self.assertTypeName(
120 self.prog.array_type(self.prog.array_type(i, 3), 2), "int [2][3]", True
121 )
122 self.assertTypeName(
123 self.prog.array_type(
124 self.prog.array_type(self.prog.array_type(i, 4), 3), 2
125 ),
126 "int [2][3][4]",
127 True,
128 )
129
130 def test_array_of_pointers(self):
131 self.assertTypeName(
132 self.prog.array_type(
133 self.prog.array_type(
134 self.prog.pointer_type(self.prog.int_type("int", 4, True)), 3
135 ),
136 2,
137 ),
138 "int *[2][3]",
139 True,
140 )
141
142 def test_pointer_to_array(self):
143 self.assertTypeName(
144 self.prog.pointer_type(
145 self.prog.array_type(self.prog.int_type("int", 4, True), 2)
146 ),
147 "int (*)[2]",
148 True,
149 )
150
151 def test_pointer_to_pointer_to_array(self):
152 self.assertTypeName(
153 self.prog.pointer_type(
154 self.prog.pointer_type(
155 self.prog.array_type(self.prog.int_type("int", 4, True), 2)
156 )
157 ),
158 "int (**)[2]",
159 True,
160 )
161
162 def test_pointer_to_array_of_pointers(self):
163 self.assertTypeName(
164 self.prog.pointer_type(
165 self.prog.array_type(
166 self.prog.pointer_type(self.prog.int_type("int", 4, True)), 2
167 )
168 ),
169 "int *(*)[2]",
170 True,
171 )
172
173 def test_array_of_pointers_to_array(self):
174 self.assertTypeName(
175 self.prog.array_type(
176 self.prog.pointer_type(
177 self.prog.array_type(self.prog.int_type("int", 4, True), 3)
178 ),
179 2,
180 ),
181 "int (*[2])[3]",
182 True,
183 )
184
185 def test_pointer_to_function(self):
186 i = self.prog.int_type("int", 4, True)
187 self.assertTypeName(
188 self.prog.pointer_type(
189 self.prog.function_type(i, (TypeParameter(i),), False)
190 ),
191 "int (*)(int)",
192 True,
193 )
194 self.assertTypeName(
195 self.prog.pointer_type(
196 self.prog.function_type(i, (TypeParameter(i, "x"),), False)
197 ),
198 "int (*)(int x)",
199 True,
200 )
201 self.assertTypeName(
202 self.prog.pointer_type(
203 self.prog.function_type(
204 i,
205 (
206 TypeParameter(i),
207 TypeParameter(self.prog.float_type("float", 4)),
208 ),
209 False,
210 ),
211 ),
212 "int (*)(int, float)",
213 True,
214 )
215
216 def test_pointer_to_function_returning_pointer(self):
217 i = self.prog.int_type("int", 4, True)
218 self.assertTypeName(
219 self.prog.pointer_type(
220 self.prog.function_type(
221 self.prog.pointer_type(i), (TypeParameter(i),), False
222 )
223 ),
224 "int *(*)(int)",
225 True,
226 )
227 self.assertTypeName(
228 self.prog.pointer_type(
229 self.prog.function_type(
230 self.prog.pointer_type(i),
231 (TypeParameter(self.prog.pointer_type(i)),),
232 False,
233 ),
234 ),
235 "int *(*)(int *)",
236 True,
237 )
238
239 def test_pointer_to_function_returning_pointer_to_const(self):
240 i = self.prog.int_type("int", 4, True)
241 self.assertTypeName(
242 self.prog.pointer_type(
243 self.prog.function_type(
244 self.prog.pointer_type(
245 self.prog.int_type("int", 4, True, qualifiers=Qualifiers.CONST)
246 ),
247 (TypeParameter(i),),
248 False,
249 ),
250 ),
251 "const int *(*)(int)",
252 True,
253 )
254
255 def test_pointer_to_function_returning_const_pointer(self):
256 i = self.prog.int_type("int", 4, True)
257 self.assertTypeName(
258 self.prog.pointer_type(
259 self.prog.function_type(
260 self.prog.pointer_type(i, qualifiers=Qualifiers.CONST),
261 (TypeParameter(i),),
262 False,
263 ),
264 ),
265 "int * const (*)(int)",
266 True,
267 )
268
269 def test_const_pointer_to_function_returning_pointer(self):
270 i = self.prog.int_type("int", 4, True)
271 self.assertTypeName(
272 self.prog.pointer_type(
273 self.prog.function_type(
274 self.prog.pointer_type(i), (TypeParameter(i),), False
275 ),
276 qualifiers=Qualifiers.CONST,
277 ),
278 "int *(* const)(int)",
279 True,
280 )
281
282 def test_array_of_pointers_to_functions(self):
283 i = self.prog.int_type("int", 4, True)
284 self.assertTypeName(
285 self.prog.array_type(
286 self.prog.pointer_type(
287 self.prog.function_type(i, (TypeParameter(i),), False)
288 ),
289 4,
290 ),
291 "int (*[4])(int)",
292 True,
293 )
294
295 def test_array_of_const_pointers_to_functions(self):
296 i = self.prog.int_type("int", 4, True)
297 self.assertTypeName(
298 self.prog.array_type(
299 self.prog.pointer_type(
300 self.prog.function_type(i, (TypeParameter(i),), False),
301 qualifiers=Qualifiers.CONST,
302 ),
303 ),
304 "int (* const [])(int)",
305 True,
306 )
307
308 def test_pointer_to_variadic_function(self):
309 i = self.prog.int_type("int", 4, True)
310 self.assertTypeName(
311 self.prog.pointer_type(
312 self.prog.function_type(i, (TypeParameter(i),), True)
313 ),
314 "int (*)(int, ...)",
315 True,
316 )
317
318 def test_pointer_to_function_with_no_parameters(self):
319 self.assertTypeName(
320 self.prog.pointer_type(
321 self.prog.function_type(self.prog.int_type("int", 4, True), (), False)
322 ),
323 "int (*)(void)",
324 True,
325 )
326
327 def test_pointer_to_function_with_no_parameter_specification(self):
328 self.assertTypeName(
329 self.prog.pointer_type(
330 self.prog.function_type(self.prog.int_type("int", 4, True), (), True)
331 ),
332 "int (*)()",
333 True,
334 )
335
336 def test_function(self):
337 self.assertTypeName(
338 self.prog.function_type(self.prog.int_type("int", 4, True), (), False),
339 "int (void)",
340 )
341
342 def test_pointer_to_anonymous_struct(self):
343 self.assertTypeName(
344 self.prog.pointer_type(
345 self.prog.struct_type(
346 None, 8, (TypeMember(self.prog.int_type("int", 4, True), "x", 0),)
347 )
348 ),
349 "struct <anonymous> *",
350 )
351
352 def test_array_of_anonymous_struct(self):
353 self.assertTypeName(
354 self.prog.array_type(
355 self.prog.struct_type(
356 None, 8, (TypeMember(self.prog.int_type("int", 4, True), "x", 0),)
357 ),
358 2,
359 ),
360 "struct <anonymous> [2]",
361 )
362
363 def test_function_returning_anonymous_struct(self):
364 self.assertTypeName(
365 self.prog.function_type(
366 self.prog.struct_type(
367 None, 8, (TypeMember(self.prog.int_type("int", 4, True), "x", 0),)
368 ),
369 (),
370 ),
371 "struct <anonymous> (void)",
372 )
373
374 def test_function_of_anonymous_struct(self):
375 self.assertTypeName(
376 self.prog.function_type(
377 self.prog.int_type("int", 4, True),
378 (
379 TypeParameter(
380 self.prog.struct_type(
381 None,
382 8,
383 (TypeMember(self.prog.int_type("int", 4, True), "x", 0),),
384 ),
385 "x",
386 ),
387 ),
388 ),
389 "int (struct <anonymous> x)",
390 )
391
392 def test_typedef_of_anonymous_struct(self):
393 self.assertTypeName(
394 self.prog.typedef_type(
395 "onymous",
396 self.prog.struct_type(
397 None, 8, (TypeMember(self.prog.int_type("int", 4, True), "x", 0),)
398 ),
399 ),
400 "onymous",
401 )
402
403
404 class TestVariableDeclaration(MockProgramTestCase):
405 def assert_variable_declaration(self, type, expected):
406 self.assertEqual(type.variable_declaration("foo"), expected)
407
408 def test_int(self):
409 self.assert_variable_declaration(self.prog.int_type("int", 4, True), "int foo")
410
411 def test_pointer(self):
412 self.assert_variable_declaration(
413 self.prog.pointer_type(self.prog.int_type("int", 4, True)), "int *foo"
414 )
415
416 def test_pointer_to_pointer(self):
417 self.assert_variable_declaration(
418 self.prog.pointer_type(
419 self.prog.pointer_type(self.prog.int_type("int", 4, True))
420 ),
421 "int **foo",
422 )
423
424 def test_array(self):
425 self.assert_variable_declaration(
426 self.prog.array_type(self.prog.int_type("int", 4, True), 3), "int foo[3]"
427 )
428
429 def test_incomplete_array(self):
430 self.assert_variable_declaration(
431 self.prog.array_type(self.prog.int_type("int", 4, True)), "int foo[]"
432 )
433
434 def test_struct(self):
435 self.assert_variable_declaration(self.point_type, "struct point foo")
436
437 def test_anonymous_struct(self):
438 self.assert_variable_declaration(
439 self.prog.struct_type(
440 None,
441 8,
442 (
443 TypeMember(self.prog.int_type("int", 4, True), "x", 0),
444 TypeMember(self.prog.int_type("int", 4, True), "y", 32),
445 ),
446 ),
447 """\
448 struct {
449 int x;
450 int y;
451 } foo""",
452 )
453
454 def test_function(self):
455 self.assert_variable_declaration(
456 self.prog.function_type(
457 self.prog.int_type("unsigned int", 4, True),
458 (TypeParameter(self.prog.int_type("int", 4, True), "j"),),
459 ),
460 "unsigned int foo(int j)",
461 )
462
463
464 class TestPrettyPrintType(MockProgramTestCase):
465 def assertPrettyPrint(self, type, expected):
466 self.assertEqual(str(type), expected)
467
468 def test_struct(self):
469 self.assertPrettyPrint(
470 self.point_type,
471 """\
472 struct point {
473 int x;
474 int y;
475 }""",
476 )
477
478 def test_struct_member(self):
479 self.assertPrettyPrint(
480 self.line_segment_type,
481 """\
482 struct line_segment {
483 struct point a;
484 struct point b;
485 }""",
486 )
487
488 def test_anonymous_struct(self):
489 self.assertPrettyPrint(
490 self.prog.struct_type(
491 None,
492 8,
493 (
494 TypeMember(self.prog.int_type("int", 4, True), "x", 0),
495 TypeMember(self.prog.int_type("int", 4, True), "y", 32),
496 ),
497 ),
498 """\
499 struct {
500 int x;
501 int y;
502 }""",
503 )
504
505 def test_anonymous_struct_member(self):
506 # Member with anonymous struct type.
507 anonymous_struct = self.prog.struct_type(
508 None,
509 8,
510 (
511 TypeMember(self.prog.int_type("int", 4, True), "x", 0),
512 TypeMember(self.prog.int_type("int", 4, True), "y", 32),
513 ),
514 )
515 self.assertPrettyPrint(
516 self.prog.struct_type(
517 "line_segment",
518 16,
519 (
520 TypeMember(anonymous_struct, "a", 0),
521 TypeMember(anonymous_struct, "b", 64),
522 ),
523 ),
524 """\
525 struct line_segment {
526 struct {
527 int x;
528 int y;
529 } a;
530 struct {
531 int x;
532 int y;
533 } b;
534 }""",
535 )
536
537 def test_struct_unnamed_member(self):
538 self.assertPrettyPrint(
539 self.prog.struct_type(
540 "point3",
541 0,
542 (
543 TypeMember(
544 self.prog.struct_type(
545 None,
546 8,
547 (
548 TypeMember(self.prog.int_type("int", 4, True), "x"),
549 TypeMember(self.prog.int_type("int", 4, True), "y", 32),
550 ),
551 )
552 ),
553 TypeMember(self.prog.int_type("int", 4, True), "z", 64),
554 ),
555 ),
556 """\
557 struct point3 {
558 struct {
559 int x;
560 int y;
561 };
562 int z;
563 }""",
564 )
565
566 def test_bit_field(self):
567 self.assertPrettyPrint(
568 self.prog.struct_type(
569 "point",
570 4,
571 (
572 TypeMember(
573 Object(
574 self.prog,
575 self.prog.int_type("int", 4, True),
576 bit_field_size=4,
577 ),
578 "x",
579 0,
580 ),
581 TypeMember(
582 Object(
583 self.prog,
584 self.prog.int_type("int", 4, True),
585 bit_field_size=8,
586 ),
587 "y",
588 4,
589 ),
590 ),
591 ),
592 """\
593 struct point {
594 int x : 4;
595 int y : 8;
596 }""",
597 )
598
599 def test_union(self):
600 self.assertPrettyPrint(
601 self.prog.union_type(
602 "foo",
603 4,
604 (
605 TypeMember(self.prog.int_type("int", 4, True), "i"),
606 TypeMember(
607 self.prog.array_type(
608 self.prog.int_type("unsigned char", 1, False), 4
609 ),
610 "a",
611 ),
612 ),
613 ),
614 """\
615 union foo {
616 int i;
617 unsigned char a[4];
618 }""",
619 )
620
621 def test_union_qualified(self):
622 self.assertPrettyPrint(
623 self.prog.union_type(
624 "foo",
625 4,
626 (
627 TypeMember(self.prog.int_type("int", 4, True), "i"),
628 TypeMember(
629 self.prog.array_type(
630 self.prog.int_type("unsigned char", 1, False), 4
631 ),
632 "a",
633 ),
634 ),
635 qualifiers=Qualifiers.CONST,
636 ),
637 """\
638 const union foo {
639 int i;
640 unsigned char a[4];
641 }""",
642 )
643
644 def test_class(self):
645 self.assertPrettyPrint(
646 self.coord_type,
647 """\
648 class coord {
649 int x;
650 int y;
651 int z;
652 }""",
653 )
654
655 def test_enum(self):
656 self.assertPrettyPrint(
657 self.color_type,
658 """\
659 enum color {
660 RED = 0,
661 GREEN = 1,
662 BLUE = 2,
663 }""",
664 )
665
666 def test_enum_qualified(self):
667 self.assertPrettyPrint(
668 self.color_type.qualified(Qualifiers.CONST),
669 """\
670 const enum color {
671 RED = 0,
672 GREEN = 1,
673 BLUE = 2,
674 }""",
675 )
676
677 def test_enum_anonymous(self):
678 self.assertPrettyPrint(
679 self.prog.enum_type(
680 None,
681 self.prog.int_type("int", 4, True),
682 (
683 TypeEnumerator("RED", 0),
684 TypeEnumerator("GREEN", -1),
685 TypeEnumerator("BLUE", -2),
686 ),
687 ),
688 """\
689 enum {
690 RED = 0,
691 GREEN = -1,
692 BLUE = -2,
693 }""",
694 )
695
696 def test_typedef(self):
697 self.assertPrettyPrint(
698 self.prog.typedef_type("INT", self.prog.int_type("int", 4, True)),
699 "typedef int INT",
700 )
701
702 def test_typedef_const(self):
703 self.assertPrettyPrint(
704 self.prog.typedef_type(
705 "CINT", self.prog.int_type("int", 4, True, qualifiers=Qualifiers.CONST)
706 ),
707 "typedef const int CINT",
708 )
709
710 def test_const_typedef(self):
711 self.assertPrettyPrint(
712 self.prog.typedef_type(
713 "INT", self.prog.int_type("int", 4, True), qualifiers=Qualifiers.CONST
714 ),
715 "const typedef int INT",
716 )
717
718 def test_typedef_pointer(self):
719 self.assertPrettyPrint(
720 self.prog.typedef_type(
721 "string", self.prog.pointer_type(self.prog.int_type("char", 1, True))
722 ),
723 "typedef char *string",
724 )
725
726 def test_typedef_struct(self):
727 self.assertPrettyPrint(
728 self.prog.typedef_type(
729 "Point",
730 self.prog.struct_type(
731 None,
732 8,
733 (
734 TypeMember(self.prog.int_type("int", 4, True), "x", 0),
735 TypeMember(self.prog.int_type("int", 4, True), "y", 32),
736 ),
737 ),
738 ),
739 """\
740 typedef struct {
741 int x;
742 int y;
743 } Point""",
744 )
745
746 def test_typedef_function(self):
747 self.assertPrettyPrint(
748 self.prog.typedef_type(
749 "fn",
750 self.prog.function_type(self.prog.int_type("int", 4, True), (), False),
751 ),
752 "typedef int fn(void)",
753 )
754
755 def test_function_no_name(self):
756 self.assertRaisesRegex(
757 ValueError,
758 "function must have name",
759 str,
760 self.prog.struct_type(
761 "foo",
762 8,
763 (
764 TypeMember(
765 self.prog.function_type(
766 self.prog.int_type("int", 4, True), (), False
767 )
768 ),
769 ),
770 ),
771 )
772
773 def test_function(self):
774 self.assertPrettyPrint(
775 self.prog.function_type(self.prog.int_type("int", 4, True), (), False),
776 "int (void)",
777 )
778
779
780 class TestLiteral(MockProgramTestCase):
781 def test_int(self):
782 self.assertIdentical(
783 Object(self.prog, value=1), Object(self.prog, "int", value=1)
784 )
785 self.assertIdentical(
786 Object(self.prog, value=-1), Object(self.prog, "int", value=-1)
787 )
788 self.assertIdentical(
789 Object(self.prog, value=2**31 - 1),
790 Object(self.prog, "int", value=2**31 - 1),
791 )
792
793 self.assertIdentical(
794 Object(self.prog, value=2**31), Object(self.prog, "long", value=2**31)
795 )
796 # Not int, because this is treated as the negation operator applied to
797 # 2**31.
798 self.assertIdentical(
799 Object(self.prog, value=-(2**31)),
800 Object(self.prog, "long", value=-(2**31)),
801 )
802
803 self.assertIdentical(
804 Object(self.prog, value=2**63),
805 Object(self.prog, "unsigned long long", value=2**63),
806 )
807 self.assertIdentical(
808 Object(self.prog, value=2**64 - 1),
809 Object(self.prog, "unsigned long long", value=2**64 - 1),
810 )
811 self.assertIdentical(
812 Object(self.prog, value=-(2**64 - 1)),
813 Object(self.prog, "unsigned long long", value=1),
814 )
815
816 def test_bool(self):
817 self.assertIdentical(
818 Object(self.prog, value=True), Object(self.prog, "int", value=1)
819 )
820 self.assertIdentical(
821 Object(self.prog, value=False), Object(self.prog, "int", value=0)
822 )
823
824 def test_float(self):
825 self.assertIdentical(
826 Object(self.prog, value=3.14), Object(self.prog, "double", value=3.14)
827 )
828
829 def test_invalid(self):
830 class Foo:
831 pass
832
833 self.assertRaisesRegex(
834 TypeError,
835 "literal must be int, float, or bool, not 'Foo'",
836 Object,
837 self.prog,
838 value=Foo(),
839 )
840
841
842 class TestIntegerPromotion(MockProgramTestCase):
843 def test_conversion_rank_less_than_int(self):
844 self.assertIdentical(+self.bool(False), self.int(0))
845
846 self.assertIdentical(
847 +Object(self.prog, "char", value=1), Object(self.prog, "int", value=1)
848 )
849 self.assertIdentical(
850 +Object(self.prog, "signed char", value=2),
851 Object(self.prog, "int", value=2),
852 )
853 self.assertIdentical(
854 +Object(self.prog, "unsigned char", value=3),
855 Object(self.prog, "int", value=3),
856 )
857
858 self.assertIdentical(
859 +Object(self.prog, "short", value=1), Object(self.prog, "int", value=1)
860 )
861 self.assertIdentical(
862 +Object(self.prog, "unsigned short", value=2),
863 Object(self.prog, "int", value=2),
864 )
865
866 # If short is the same size as int, then int can't represent all of the
867 # values of unsigned short.
868 self.assertIdentical(
869 +Object(self.prog, self.prog.int_type("short", 4, True), value=1),
870 Object(self.prog, "int", value=1),
871 )
872 self.assertIdentical(
873 +Object(self.prog, self.prog.int_type("unsigned short", 4, False), value=2),
874 Object(self.prog, "unsigned int", value=2),
875 )
876
877 def test_int(self):
878 self.assertIdentical(
879 +Object(self.prog, "int", value=-1), Object(self.prog, "int", value=-1)
880 )
881
882 self.assertIdentical(
883 +Object(self.prog, "unsigned int", value=-1),
884 Object(self.prog, "unsigned int", value=-1),
885 )
886
887 def test_conversion_rank_greater_than_int(self):
888 self.assertIdentical(
889 +Object(self.prog, "long", value=-1), Object(self.prog, "long", value=-1)
890 )
891
892 self.assertIdentical(
893 +Object(self.prog, "unsigned long", value=-1),
894 Object(self.prog, "unsigned long", value=-1),
895 )
896
897 self.assertIdentical(
898 +Object(self.prog, "long long", value=-1),
899 Object(self.prog, "long long", value=-1),
900 )
901
902 self.assertIdentical(
903 +Object(self.prog, "unsigned long long", value=-1),
904 Object(self.prog, "unsigned long long", value=-1),
905 )
906
907 def test_extended_integer(self):
908 self.assertIdentical(
909 +Object(self.prog, self.prog.int_type("byte", 1, True), value=1),
910 Object(self.prog, "int", value=1),
911 )
912 self.assertIdentical(
913 +Object(self.prog, self.prog.int_type("ubyte", 1, False), value=-1),
914 Object(self.prog, "int", value=0xFF),
915 )
916 self.assertIdentical(
917 +Object(self.prog, self.prog.int_type("qword", 8, True), value=1),
918 Object(self.prog, self.prog.int_type("qword", 8, True), value=1),
919 )
920 self.assertIdentical(
921 +Object(self.prog, self.prog.int_type("qword", 8, False), value=1),
922 Object(self.prog, self.prog.int_type("qword", 8, False), value=1),
923 )
924
925 def test_bit_field(self):
926 # Bit fields which can be represented by int or unsigned int should be
927 # promoted.
928 self.assertIdentical(
929 +Object(self.prog, "int", value=1, bit_field_size=4),
930 Object(self.prog, "int", value=1),
931 )
932 self.assertIdentical(
933 +Object(self.prog, "long", value=1, bit_field_size=4),
934 Object(self.prog, "int", value=1),
935 )
936 self.assertIdentical(
937 +Object(self.prog, "int", value=1, bit_field_size=32),
938 Object(self.prog, "int", value=1),
939 )
940 self.assertIdentical(
941 +Object(self.prog, "long", value=1, bit_field_size=32),
942 Object(self.prog, "int", value=1),
943 )
944 self.assertIdentical(
945 +Object(self.prog, "unsigned int", value=1, bit_field_size=4),
946 Object(self.prog, "int", value=1),
947 )
948 self.assertIdentical(
949 +Object(self.prog, "unsigned long", value=1, bit_field_size=4),
950 Object(self.prog, "int", value=1),
951 )
952 self.assertIdentical(
953 +Object(self.prog, "unsigned int", value=1, bit_field_size=32),
954 Object(self.prog, "unsigned int", value=1),
955 )
956 self.assertIdentical(
957 +Object(self.prog, "unsigned long", value=1, bit_field_size=32),
958 Object(self.prog, "unsigned int", value=1),
959 )
960
961 # Bit fields which cannot be represented by int or unsigned int should
962 # be preserved.
963 self.assertIdentical(
964 +Object(self.prog, "long", value=1, bit_field_size=40),
965 Object(self.prog, "long", value=1, bit_field_size=40),
966 )
967 self.assertIdentical(
968 +Object(self.prog, "unsigned long", value=1, bit_field_size=40),
969 Object(self.prog, "unsigned long", value=1, bit_field_size=40),
970 )
971
972 def test_enum(self):
973 # Enums should be converted to their compatible type and then promoted.
974 self.assertIdentical(
975 +Object(self.prog, self.color_type, value=1),
976 Object(self.prog, "unsigned int", value=1),
977 )
978
979 type_ = self.prog.enum_type(
980 "color",
981 self.prog.type("unsigned long long"),
982 (
983 TypeEnumerator("RED", 0),
984 TypeEnumerator("GREEN", 1),
985 TypeEnumerator("BLUE", 2),
986 ),
987 )
988 self.assertIdentical(
989 +Object(self.prog, type_, value=1),
990 Object(self.prog, "unsigned long long", value=1),
991 )
992
993 type_ = self.prog.enum_type(
994 "color",
995 self.prog.type("char"),
996 (
997 TypeEnumerator("RED", 0),
998 TypeEnumerator("GREEN", 1),
999 TypeEnumerator("BLUE", 2),
1000 ),
1001 )
1002 self.assertIdentical(
1003 +Object(self.prog, type_, value=1), Object(self.prog, "int", value=1)
1004 )
1005
1006 def test_typedef(self):
1007 type_ = self.prog.typedef_type("SHORT", self.prog.type("short"))
1008 self.assertIdentical(
1009 +Object(self.prog, type_, value=5), Object(self.prog, "int", value=5)
1010 )
1011
1012 # Typedef should be preserved if the type wasn't promoted.
1013 type_ = self.prog.typedef_type("CINT", self.prog.type("int"))
1014 self.assertIdentical(
1015 +Object(self.prog, type_, value=5), Object(self.prog, type_, value=5)
1016 )
1017
1018 def test_non_integer(self):
1019 # Non-integer types should not be affected.
1020 self.assertIdentical(
1021 +Object(self.prog, "double", value=3.14),
1022 Object(self.prog, "double", value=3.14),
1023 )
1024
1025 def test_byte_order(self):
1026 # Types in the opposite byte order should converted to the program's
1027 # byte order.
1028 self.assertIdentical(
1029 +Object(self.prog, self.prog.int_type("int", 4, True, "big"), value=5),
1030 Object(self.prog, self.prog.int_type("int", 4, True, "little"), value=5),
1031 )
1032
1033 def test_byte_order_typedef(self):
1034 self.assertIdentical(
1035 +Object(
1036 self.prog,
1037 self.prog.typedef_type(
1038 "CINT", self.prog.int_type("int", 4, True, "big")
1039 ),
1040 value=5,
1041 ),
1042 Object(
1043 self.prog,
1044 self.prog.typedef_type(
1045 "CINT", self.prog.int_type("int", 4, True, "little")
1046 ),
1047 value=5,
1048 ),
1049 )
1050
1051 def test_byte_order_enum(self):
1052 self.assertIdentical(
1053 +Object(
1054 self.prog,
1055 self.prog.enum_type(
1056 "ENUM", self.prog.int_type("int", 4, True, "big"), ()
1057 ),
1058 value=5,
1059 ),
1060 Object(self.prog, self.prog.int_type("int", 4, True, "little"), value=5),
1061 )
1062
1063
1064 class TestCommonRealType(MockProgramTestCase):
1065 def assertCommonRealType(self, lhs, rhs, expected, commutative=True):
1066 if isinstance(lhs, (str, Type)):
1067 obj1 = Object(self.prog, lhs, value=1)
1068 else:
1069 obj1 = Object(self.prog, lhs[0], value=1, bit_field_size=lhs[1])
1070 if isinstance(rhs, (str, Type)):
1071 obj2 = Object(self.prog, rhs, value=1)
1072 else:
1073 obj2 = Object(self.prog, rhs[0], value=1, bit_field_size=rhs[1])
1074 if isinstance(expected, (str, Type)):
1075 expected_obj = Object(self.prog, expected, value=1)
1076 else:
1077 expected_obj = Object(
1078 self.prog, expected[0], value=1, bit_field_size=expected[1]
1079 )
1080 self.assertIdentical(obj1 * obj2, expected_obj)
1081 if commutative:
1082 self.assertIdentical(obj2 * obj1, expected_obj)
1083
1084 def test_float(self):
1085 self.assertCommonRealType("float", "long long", "float")
1086 self.assertCommonRealType("float", "float", "float")
1087
1088 self.assertCommonRealType("double", "long long", "double")
1089 self.assertCommonRealType("double", "float", "double")
1090 self.assertCommonRealType("double", "double", "double")
1091
1092 # Floating type not in the standard.
1093 float64 = self.prog.float_type("float64", 8)
1094 self.assertCommonRealType(float64, "long long", float64)
1095 self.assertCommonRealType(float64, "float", float64)
1096 self.assertCommonRealType(float64, "double", float64)
1097 self.assertCommonRealType(float64, float64, float64)
1098
1099 def test_bit_field(self):
1100 # Same width and sign.
1101 self.assertCommonRealType(
1102 ("long long", 33), ("long long", 33), ("long long", 33)
1103 )
1104 self.assertCommonRealType(
1105 ("long long", 33), ("long", 33), ("long", 33), commutative=False
1106 )
1107 self.assertCommonRealType(
1108 ("long", 33), ("long long", 33), ("long long", 33), commutative=False
1109 )
1110
1111 # Same width, different sign.
1112 self.assertCommonRealType(
1113 ("long long", 33), ("unsigned long long", 33), ("unsigned long long", 33)
1114 )
1115
1116 # Different width, same sign.
1117 self.assertCommonRealType(
1118 ("long long", 34), ("long long", 33), ("long long", 34)
1119 )
1120
1121 # Different width, different sign.
1122 self.assertCommonRealType(
1123 ("long long", 34), ("unsigned long long", 33), ("long long", 34)
1124 )
1125
1126 def test_same(self):
1127 self.assertCommonRealType("_Bool", "_Bool", "int")
1128 self.assertCommonRealType("int", "int", "int")
1129 self.assertCommonRealType("long", "long", "long")
1130
1131 def test_same_sign(self):
1132 self.assertCommonRealType("long", "int", "long")
1133 self.assertCommonRealType("long long", "int", "long long")
1134 self.assertCommonRealType("long long", "long", "long long")
1135
1136 self.assertCommonRealType("unsigned long", "unsigned int", "unsigned long")
1137 self.assertCommonRealType(
1138 "unsigned long long", "unsigned int", "unsigned long long"
1139 )
1140 self.assertCommonRealType(
1141 "unsigned long long", "unsigned long", "unsigned long long"
1142 )
1143
1144 int64 = self.prog.int_type("int64", 8, True)
1145 qword = self.prog.int_type("qword", 8, True)
1146 self.assertCommonRealType("long", int64, "long")
1147 self.assertCommonRealType(int64, qword, qword, commutative=False)
1148 self.assertCommonRealType(qword, int64, int64, commutative=False)
1149 self.assertCommonRealType("int", int64, int64)
1150
1151 def test_unsigned_greater_rank(self):
1152 self.assertCommonRealType("unsigned long", "int", "unsigned long")
1153 self.assertCommonRealType("unsigned long long", "long", "unsigned long long")
1154 self.assertCommonRealType("unsigned long long", "int", "unsigned long long")
1155
1156 int64 = self.prog.int_type("int64", 8, True)
1157 uint64 = self.prog.int_type("uint64", 8, False)
1158 self.assertCommonRealType(uint64, "int", uint64)
1159 self.assertCommonRealType("unsigned long", int64, "unsigned long")
1160
1161 def test_signed_can_represent_unsigned(self):
1162 self.assertCommonRealType("long", "unsigned int", "long")
1163 self.assertCommonRealType("long long", "unsigned int", "long long")
1164
1165 int64 = self.prog.int_type("int64", 8, True)
1166 weirduint = self.prog.int_type("weirduint", 6, False)
1167 self.assertCommonRealType(int64, "unsigned int", int64)
1168 self.assertCommonRealType("long", weirduint, "long")
1169
1170 def test_corresponding_unsigned(self):
1171 self.assertCommonRealType("long", "unsigned long", "unsigned long")
1172 self.assertCommonRealType("long long", "unsigned long", "unsigned long long")
1173
1174 def test_enum(self):
1175 self.assertCommonRealType(self.color_type, self.color_type, "unsigned int")
1176
1177 def test_typedef(self):
1178 type_ = self.prog.typedef_type("INT", self.prog.type("int"))
1179 self.assertCommonRealType(type_, type_, type_)
1180 self.assertCommonRealType("int", type_, type_, commutative=False)
1181 self.assertCommonRealType(type_, "int", "int", commutative=False)
1182
1183 type_ = self.prog.typedef_type("LONG", self.prog.type("long"))
1184 self.assertCommonRealType(type_, "int", type_)
1185
1186
1187 class TestOperators(MockProgramTestCase):
1188 def test_bool_arrays(self):
1189 self.assertTrue(Object(self.prog, "int [2]", [0, 0]))
1190 self.assertTrue(Object(self.prog, "int [0]", address=0x1234))
1191 self.assertFalse(Object(self.prog, "int [2]", address=0))
1192
1193 def test_bool_functions(self):
1194 self.assertTrue(
1195 Object(
1196 self.prog,
1197 self.prog.function_type(self.prog.type("void"), ()),
1198 address=0x1234,
1199 )
1200 )
1201 self.assertFalse(
1202 Object(
1203 self.prog,
1204 self.prog.function_type(self.prog.type("void"), ()),
1205 address=0,
1206 )
1207 )
1208
1209 def test_cast_array(self):
1210 obj = Object(self.prog, "int []", address=0xFFFF0000)
1211 self.assertIdentical(
1212 cast("int *", obj), Object(self.prog, "int *", value=0xFFFF0000)
1213 )
1214 self.assertIdentical(
1215 cast("void *", obj), Object(self.prog, "void *", value=0xFFFF0000)
1216 )
1217 self.assertIdentical(
1218 cast("unsigned long", obj),
1219 Object(self.prog, "unsigned long", value=0xFFFF0000),
1220 )
1221 self.assertRaisesRegex(
1222 TypeError, r"cannot cast to 'int \[2]'", cast, "int [2]", obj
1223 )
1224
1225 def test_cast_function(self):
1226 func = Object(
1227 self.prog,
1228 self.prog.function_type(self.prog.void_type(), (), False),
1229 address=0xFFFF0000,
1230 )
1231 self.assertIdentical(
1232 cast("void *", func), Object(self.prog, "void *", value=0xFFFF0000)
1233 )
1234
1235 def test_cast_to_void(self):
1236 self.assertIdentical(
1237 cast("void", Object(self.prog, "int", 0)), Object(self.prog, "void")
1238 )
1239 self.assertIdentical(
1240 cast("void", Object(self.prog, "int [2]")), Object(self.prog, "void")
1241 )
1242
1243 def test_cast_to_bool(self):
1244 self.assertIdentical(
1245 cast("_Bool", Object(self.prog, "int", 0)),
1246 Object(self.prog, "_Bool", False),
1247 )
1248 for value in (1, -1, 2, 256, 32767):
1249 with self.subTest(value=value):
1250 self.assertIdentical(
1251 cast("_Bool", Object(self.prog, "int", value)),
1252 Object(self.prog, "_Bool", True),
1253 )
1254 self.assertIdentical(
1255 cast("_Bool", Object(self.prog, "void *", 0)),
1256 Object(self.prog, "_Bool", False),
1257 )
1258 self.assertIdentical(
1259 cast("_Bool", Object(self.prog, "void *", 1)),
1260 Object(self.prog, "_Bool", True),
1261 )
1262
1263 def _test_arithmetic(
1264 self, op, lhs, rhs, result, integral=True, floating_point=False
1265 ):
1266 if integral:
1267 self.assertIdentical(op(self.int(lhs), self.int(rhs)), self.int(result))
1268 self.assertIdentical(op(self.int(lhs), self.long(rhs)), self.long(result))
1269 self.assertIdentical(op(self.long(lhs), self.int(rhs)), self.long(result))
1270 self.assertIdentical(op(self.long(lhs), self.long(rhs)), self.long(result))
1271 self.assertIdentical(op(self.int(lhs), rhs), self.int(result))
1272 self.assertIdentical(op(self.long(lhs), rhs), self.long(result))
1273 self.assertIdentical(op(lhs, self.int(rhs)), self.int(result))
1274 self.assertIdentical(op(lhs, self.long(rhs)), self.long(result))
1275
1276 if floating_point:
1277 self.assertIdentical(
1278 op(self.double(lhs), self.double(rhs)), self.double(result)
1279 )
1280 self.assertIdentical(
1281 op(self.double(lhs), self.int(rhs)), self.double(result)
1282 )
1283 self.assertIdentical(
1284 op(self.int(lhs), self.double(rhs)), self.double(result)
1285 )
1286 self.assertIdentical(op(self.double(lhs), float(rhs)), self.double(result))
1287 self.assertIdentical(op(float(lhs), self.double(rhs)), self.double(result))
1288 self.assertIdentical(op(float(lhs), self.int(rhs)), self.double(result))
1289 self.assertIdentical(op(self.int(lhs), float(rhs)), self.double(result))
1290
1291 def _test_shift(self, op, lhs, rhs, result):
1292 self.assertIdentical(op(self.int(lhs), self.int(rhs)), self.int(result))
1293 self.assertIdentical(op(self.int(lhs), self.long(rhs)), self.int(result))
1294 self.assertIdentical(op(self.long(lhs), self.int(rhs)), self.long(result))
1295 self.assertIdentical(op(self.long(lhs), self.long(rhs)), self.long(result))
1296 self.assertIdentical(op(self.int(lhs), rhs), self.int(result))
1297 self.assertIdentical(op(self.long(lhs), rhs), self.long(result))
1298 self.assertIdentical(op(lhs, self.int(rhs)), self.int(result))
1299 self.assertIdentical(op(lhs, self.long(rhs)), self.int(result))
1300
1301 self._test_pointer_type_errors(op)
1302 self._test_floating_type_errors(op)
1303
1304 def _test_pointer_type_errors(self, op):
1305 def pointer(value):
1306 return Object(self.prog, "int *", value=value)
1307
1308 self.assertRaisesRegex(
1309 TypeError, "invalid operands to binary", op, self.int(1), pointer(1)
1310 )
1311 self.assertRaisesRegex(
1312 TypeError, "invalid operands to binary", op, pointer(1), self.int(1)
1313 )
1314 self.assertRaisesRegex(
1315 TypeError, "invalid operands to binary", op, pointer(1), pointer(1)
1316 )
1317
1318 def _test_floating_type_errors(self, op):
1319 self.assertRaises(TypeError, op, self.int(1), self.double(1))
1320 self.assertRaises(TypeError, op, self.double(1), self.int(1))
1321 self.assertRaises(TypeError, op, self.double(1), self.double(1))
1322
1323 def test_relational(self):
1324 one = self.int(1)
1325 two = self.int(2)
1326 three = self.int(3)
1327
1328 self.assertTrue(one < two)
1329 self.assertFalse(two < two)
1330 self.assertFalse(three < two)
1331
1332 self.assertTrue(one <= two)
1333 self.assertTrue(two <= two)
1334 self.assertFalse(three <= two)
1335
1336 self.assertTrue(one == one)
1337 self.assertFalse(one == two)
1338
1339 self.assertFalse(one != one)
1340 self.assertTrue(one != two)
1341
1342 self.assertFalse(one > two)
1343 self.assertFalse(two > two)
1344 self.assertTrue(three > two)
1345
1346 self.assertFalse(one >= two)
1347 self.assertTrue(two >= two)
1348 self.assertTrue(three >= two)
1349
1350 # The usual arithmetic conversions convert -1 to an unsigned int.
1351 self.assertFalse(self.int(-1) < self.unsigned_int(0))
1352
1353 self.assertTrue(self.int(1) == self.bool(1))
1354
1355 def test_ptr_relational(self):
1356 ptr0 = Object(self.prog, "int *", value=0xFFFF0000)
1357 ptr1 = Object(self.prog, "int *", value=0xFFFF0004)
1358 fptr1 = Object(self.prog, "float *", value=0xFFFF0004)
1359
1360 self.assertTrue(ptr0 < ptr1)
1361 self.assertTrue(ptr0 < fptr1)
1362 self.assertFalse(ptr1 < fptr1)
1363
1364 self.assertTrue(ptr0 <= ptr1)
1365 self.assertTrue(ptr0 <= fptr1)
1366 self.assertTrue(ptr1 <= fptr1)
1367
1368 self.assertFalse(ptr0 == ptr1)
1369 self.assertFalse(ptr0 == fptr1)
1370 self.assertTrue(ptr1 == fptr1)
1371
1372 self.assertTrue(ptr0 != ptr1)
1373 self.assertTrue(ptr0 != fptr1)
1374 self.assertFalse(ptr1 != fptr1)
1375
1376 self.assertFalse(ptr0 > ptr1)
1377 self.assertFalse(ptr0 > fptr1)
1378 self.assertFalse(ptr1 > fptr1)
1379
1380 self.assertFalse(ptr0 >= ptr1)
1381 self.assertFalse(ptr0 >= fptr1)
1382 self.assertTrue(ptr1 >= fptr1)
1383
1384 self.assertRaises(TypeError, operator.lt, ptr0, self.int(1))
1385
1386 func = Object(
1387 self.prog,
1388 self.prog.function_type(self.prog.void_type(), (), False),
1389 address=0xFFFF0000,
1390 )
1391 self.assertTrue(func == func)
1392 self.assertTrue(func == ptr0)
1393
1394 array = Object(self.prog, "int [8]", address=0xFFFF0000)
1395 self.assertTrue(array == array)
1396 self.assertTrue(array != ptr1)
1397
1398 incomplete = Object(self.prog, "int []", address=0xFFFF0000)
1399 self.assertTrue(incomplete == incomplete)
1400 self.assertTrue(incomplete == ptr0)
1401
1402 self.assertRaises(
1403 TypeError,
1404 operator.eq,
1405 Object(
1406 self.prog, self.prog.struct_type("foo", None, None), address=0xFFFF0000
1407 ),
1408 ptr0,
1409 )
1410
1411 def test_add(self):
1412 self._test_arithmetic(operator.add, 1, 2, 3, floating_point=True)
1413
1414 ptr = Object(self.prog, "int *", value=0xFFFF0000)
1415 arr = Object(self.prog, "int [2]", address=0xFFFF0000)
1416 ptr1 = Object(self.prog, "int *", value=0xFFFF0004)
1417 self.assertIdentical(ptr + self.int(1), ptr1)
1418 self.assertIdentical(self.unsigned_int(1) + ptr, ptr1)
1419 self.assertIdentical(arr + self.int(1), ptr1)
1420 self.assertIdentical(ptr1 + self.int(-1), ptr)
1421 self.assertIdentical(self.int(-1) + ptr1, ptr)
1422
1423 self.assertIdentical(ptr + 1, ptr1)
1424 self.assertIdentical(1 + ptr, ptr1)
1425 self.assertRaises(TypeError, operator.add, ptr, ptr)
1426 self.assertRaises(TypeError, operator.add, ptr, 2.0)
1427 self.assertRaises(TypeError, operator.add, 2.0, ptr)
1428
1429 void_ptr = Object(self.prog, "void *", value=0xFFFF0000)
1430 void_ptr1 = Object(self.prog, "void *", value=0xFFFF0001)
1431 self.assertIdentical(void_ptr + self.int(1), void_ptr1)
1432 self.assertIdentical(self.unsigned_int(1) + void_ptr, void_ptr1)
1433 self.assertIdentical(void_ptr + 1, void_ptr1)
1434 self.assertIdentical(1 + void_ptr, void_ptr1)
1435
1436 def test_sub(self):
1437 self._test_arithmetic(operator.sub, 4, 2, 2, floating_point=True)
1438
1439 ptr = Object(self.prog, "int *", value=0xFFFF0000)
1440 arr = Object(self.prog, "int [2]", address=0xFFFF0004)
1441 ptr1 = Object(self.prog, "int *", value=0xFFFF0004)
1442 self.assertIdentical(ptr1 - ptr, Object(self.prog, "ptrdiff_t", value=1))
1443 self.assertIdentical(ptr - ptr1, Object(self.prog, "ptrdiff_t", value=-1))
1444 self.assertIdentical(ptr - self.int(0), ptr)
1445 self.assertIdentical(ptr1 - self.int(1), ptr)
1446 self.assertIdentical(arr - self.int(1), ptr)
1447 self.assertRaises(TypeError, operator.sub, self.int(1), ptr)
1448 self.assertRaises(TypeError, operator.sub, ptr, 1.0)
1449
1450 void_ptr = Object(self.prog, "void *", value=0xFFFF0000)
1451 void_ptr1 = Object(self.prog, "void *", value=0xFFFF0001)
1452 self.assertIdentical(
1453 void_ptr1 - void_ptr, Object(self.prog, "ptrdiff_t", value=1)
1454 )
1455 self.assertIdentical(
1456 void_ptr - void_ptr1, Object(self.prog, "ptrdiff_t", value=-1)
1457 )
1458 self.assertIdentical(void_ptr - self.int(0), void_ptr)
1459 self.assertIdentical(void_ptr1 - self.int(1), void_ptr)
1460
1461 def test_mul(self):
1462 self._test_arithmetic(operator.mul, 2, 3, 6, floating_point=True)
1463 self._test_pointer_type_errors(operator.mul)
1464
1465 # Negative numbers.
1466 self.assertIdentical(self.int(2) * self.int(-3), self.int(-6))
1467 self.assertIdentical(self.int(-2) * self.int(3), self.int(-6))
1468 self.assertIdentical(self.int(-2) * self.int(-3), self.int(6))
1469
1470 # Integer overflow.
1471 self.assertIdentical(self.int(0x8000) * self.int(0x10000), self.int(-(2**31)))
1472
1473 self.assertIdentical(
1474 self.unsigned_int(0x8000) * self.int(0x10000), self.unsigned_int(2**31)
1475 )
1476
1477 self.assertIdentical(
1478 self.unsigned_int(0xFFFFFFFF) * self.unsigned_int(0xFFFFFFFF),
1479 self.unsigned_int(1),
1480 )
1481
1482 self.assertIdentical(
1483 self.unsigned_int(0xFFFFFFFF) * self.int(-1), self.unsigned_int(1)
1484 )
1485
1486 def test_div(self):
1487 self._test_arithmetic(operator.truediv, 6, 3, 2, floating_point=True)
1488
1489 # Make sure we do integer division for integer operands.
1490 self._test_arithmetic(operator.truediv, 3, 2, 1)
1491
1492 # Make sure we truncate towards zero (Python truncates towards negative
1493 # infinity).
1494 self._test_arithmetic(operator.truediv, -1, 2, 0)
1495 self._test_arithmetic(operator.truediv, 1, -2, 0)
1496
1497 self.assertRaises(ZeroDivisionError, operator.truediv, self.int(1), self.int(0))
1498 self.assertRaises(
1499 ZeroDivisionError,
1500 operator.truediv,
1501 self.unsigned_int(1),
1502 self.unsigned_int(0),
1503 )
1504 self.assertRaises(
1505 ZeroDivisionError, operator.truediv, self.double(1), self.double(0)
1506 )
1507
1508 self._test_pointer_type_errors(operator.truediv)
1509
1510 def test_mod(self):
1511 self._test_arithmetic(operator.mod, 4, 2, 0)
1512
1513 # Make sure the modulo result has the sign of the dividend (Python uses
1514 # the sign of the divisor).
1515 self._test_arithmetic(operator.mod, 1, 26, 1)
1516 self._test_arithmetic(operator.mod, 1, -26, 1)
1517 self._test_arithmetic(operator.mod, -1, 26, -1)
1518 self._test_arithmetic(operator.mod, -1, -26, -1)
1519
1520 self.assertRaises(ZeroDivisionError, operator.mod, self.int(1), self.int(0))
1521 self.assertRaises(
1522 ZeroDivisionError, operator.mod, self.unsigned_int(1), self.unsigned_int(0)
1523 )
1524
1525 self._test_pointer_type_errors(operator.mod)
1526 self._test_floating_type_errors(operator.mod)
1527
1528 def test_lshift(self):
1529 self._test_shift(operator.lshift, 2, 3, 16)
1530 self.assertIdentical(self.bool(True) << self.bool(True), self.int(2))
1531 self.assertIdentical(self.int(1) << self.int(32), self.int(0))
1532
1533 def test_rshift(self):
1534 self._test_shift(operator.rshift, 16, 3, 2)
1535 self.assertIdentical(self.int(-2) >> self.int(1), self.int(-1))
1536 self.assertIdentical(self.int(1) >> self.int(32), self.int(0))
1537 self.assertIdentical(self.int(-1) >> self.int(32), self.int(-1))
1538
1539 def test_and(self):
1540 self._test_arithmetic(operator.and_, 1, 3, 1)
1541 self.assertIdentical(self.int(-1) & self.int(2**31), self.int(2**31))
1542 self._test_pointer_type_errors(operator.and_)
1543 self._test_floating_type_errors(operator.and_)
1544
1545 def test_xor(self):
1546 self._test_arithmetic(operator.xor, 1, 3, 2)
1547 self.assertIdentical(self.int(-1) ^ self.int(-(2**31)), self.int(2**31 - 1))
1548 self._test_pointer_type_errors(operator.xor)
1549 self._test_floating_type_errors(operator.xor)
1550
1551 def test_or(self):
1552 self._test_arithmetic(operator.or_, 1, 3, 3)
1553 self.assertIdentical(self.int(-(2**31)) | self.int(2**31 - 1), self.int(-1))
1554 self._test_pointer_type_errors(operator.or_)
1555 self._test_floating_type_errors(operator.or_)
1556
1557 def test_pos(self):
1558 # TestIntegerPromotion covers the other cases.
1559 self.assertRaisesRegex(
1560 TypeError,
1561 r"invalid operand to unary \+",
1562 operator.pos,
1563 Object(self.prog, "int *", value=0),
1564 )
1565
1566 def test_neg(self):
1567 self.assertIdentical(-Object(self.prog, "unsigned char", value=1), self.int(-1))
1568 self.assertIdentical(-self.int(-1), self.int(1))
1569 self.assertIdentical(-self.unsigned_int(1), self.unsigned_int(0xFFFFFFFF))
1570 self.assertIdentical(
1571 -Object(self.prog, "long", value=-0x8000000000000000),
1572 Object(self.prog, "long", value=-0x8000000000000000),
1573 )
1574 self.assertIdentical(-self.double(2.0), self.double(-2.0))
1575 self.assertRaisesRegex(
1576 TypeError,
1577 "invalid operand to unary -",
1578 operator.neg,
1579 Object(self.prog, "int *", value=0),
1580 )
1581
1582 def test_not(self):
1583 self.assertIdentical(~self.int(1), self.int(-2))
1584 self.assertIdentical(
1585 ~Object(self.prog, "unsigned long long", value=-1),
1586 Object(self.prog, "unsigned long long", value=0),
1587 )
1588 self.assertIdentical(
1589 ~Object(self.prog, "unsigned char", value=255), self.int(-256)
1590 )
1591 for type_ in ["int *", "double"]:
1592 self.assertRaisesRegex(
1593 TypeError,
1594 "invalid operand to unary ~",
1595 operator.invert,
1596 Object(self.prog, type_, value=0),
1597 )
1598
1599 def test_container_of(self):
1600 obj = Object(self.prog, "int *", value=0xFFFF000C)
1601 container_of(obj, self.point_type, "x")
1602 self.assertIdentical(
1603 container_of(obj, self.point_type, "x"),
1604 Object(
1605 self.prog, self.prog.pointer_type(self.point_type), value=0xFFFF000C
1606 ),
1607 )
1608 self.assertIdentical(
1609 container_of(obj, self.point_type, "y"),
1610 Object(
1611 self.prog, self.prog.pointer_type(self.point_type), value=0xFFFF0008
1612 ),
1613 )
1614
1615 self.assertIdentical(
1616 container_of(obj, self.line_segment_type, "a.x"),
1617 Object(
1618 self.prog,
1619 self.prog.pointer_type(self.line_segment_type),
1620 value=0xFFFF000C,
1621 ),
1622 )
1623 self.assertIdentical(
1624 container_of(obj, self.line_segment_type, "b.x"),
1625 Object(
1626 self.prog,
1627 self.prog.pointer_type(self.line_segment_type),
1628 value=0xFFFF0004,
1629 ),
1630 )
1631
1632 self.assertRaisesRegex(
1633 LookupError,
1634 "'struct line_segment' has no member 'c'",
1635 container_of,
1636 obj,
1637 self.line_segment_type,
1638 "c.x",
1639 )
1640
1641 polygon_type = self.prog.struct_type(
1642 "polygon", 0, (TypeMember(self.prog.array_type(self.point_type), "points"),)
1643 )
1644 self.assertIdentical(
1645 container_of(obj, polygon_type, "points[3].x"),
1646 Object(self.prog, self.prog.pointer_type(polygon_type), value=0xFFFEFFF4),
1647 )
1648
1649 small_point_type = self.prog.struct_type(
1650 "small_point",
1651 1,
1652 (
1653 TypeMember(
1654 Object(
1655 self.prog, self.prog.int_type("int", 4, True), bit_field_size=4
1656 ),
1657 "x",
1658 0,
1659 ),
1660 TypeMember(
1661 Object(
1662 self.prog, self.prog.int_type("int", 4, True), bit_field_size=4
1663 ),
1664 "y",
1665 4,
1666 ),
1667 ),
1668 )
1669 self.assertRaisesRegex(
1670 ValueError,
1671 "member is not byte-aligned",
1672 container_of,
1673 obj,
1674 small_point_type,
1675 "y",
1676 )
1677
1678 self.assertRaisesRegex(
1679 TypeError,
1680 r"container_of\(\) argument must be a pointer",
1681 container_of,
1682 obj[0],
1683 self.point_type,
1684 "x",
1685 )
1686
1687 self.assertRaisesRegex(
1688 TypeError,
1689 "not a structure, union, or class",
1690 container_of,
1691 obj,
1692 obj.type_,
1693 "x",
1694 ),
1695
1696 type_ = self.prog.struct_type(
1697 "foo",
1698 16,
1699 (
1700 TypeMember(
1701 self.prog.array_type(self.prog.int_type("int", 4, True), 8), "arr"
1702 ),
1703 TypeMember(self.point_type, "point", 256),
1704 ),
1705 )
1706 syntax_errors = [
1707 ("", r"^expected identifier$"),
1708 ("[1]", r"^expected identifier$"),
1709 ("point.", r"^expected identifier after '\.'$"),
1710 ("point(", r"^expected '\.' or '\[' after identifier$"),
1711 ("arr[1](", r"^expected '\.' or '\[' after ']'$"),
1712 ("arr[]", r"^expected number after '\['$"),
1713 ("arr[1)", r"^expected ']' after number$"),
1714 ]
1715 for member_designator, error in syntax_errors:
1716 self.assertRaisesRegex(
1717 SyntaxError, error, container_of, obj, type_, member_designator
1718 )
1719
1720 def test_subobject_member(self):
1721 obj = Object(self.prog, self.point_type, {"x": 1, "y": 2})
1722 self.assertIdentical(
1723 obj.subobject_("x"),
1724 Object(self.prog, self.prog.int_type("int", 4, True), 1),
1725 )
1726
1727 def test_subobject_element(self):
1728 obj = Object(
1729 self.prog,
1730 self.prog.array_type(self.prog.int_type("int", 4, True), 3),
1731 [1, 2, 3],
1732 )
1733 self.assertIdentical(
1734 obj.subobject_("[1]"),
1735 Object(self.prog, self.prog.int_type("int", 4, True), 2),
1736 )
1737
1738 def test_subobject_empty(self):
1739 obj = Object(self.prog, self.point_type, {"x": 1, "y": 2})
1740 self.assertRaisesRegex(
1741 SyntaxError, r"expected identifier or '\['", obj.subobject_, ""
1742 )
1743 self.assertRaisesRegex(
1744 SyntaxError, r"expected identifier or '\['", obj.subobject_, " "
1745 )
1746
1747 def test_subobject_chain(self):
1748 obj = Object(
1749 self.prog,
1750 self.prog.array_type(self.line_segment_type, 2),
1751 [
1752 {
1753 "a": {"x": 1, "y": 2},
1754 "b": {"x": 3, "y": 4},
1755 },
1756 {
1757 "a": {"x": 5, "y": 6},
1758 "b": {"x": 7, "y": 8},
1759 },
1760 ],
1761 )
1762 self.assertIdentical(
1763 obj.subobject_("[1].a.y"),
1764 Object(self.prog, self.prog.int_type("int", 4, True), 6),
1765 )
1766
1767
1768 class TestImplicitConvert(MockProgramTestCase):
1769 def test_to_bool(self):
1770 self.assertIdentical(
1771 implicit_convert("_Bool", Object(self.prog, "int", 0)),
1772 Object(self.prog, "_Bool", False),
1773 )
1774 for value in (1, -1, 2, 256, 32767):
1775 with self.subTest(value=value):
1776 self.assertIdentical(
1777 implicit_convert("_Bool", Object(self.prog, "int", value)),
1778 Object(self.prog, "_Bool", True),
1779 )
1780 self.assertIdentical(
1781 implicit_convert("_Bool", Object(self.prog, "void *", 0)),
1782 Object(self.prog, "_Bool", False),
1783 )
1784 self.assertIdentical(
1785 implicit_convert("_Bool", Object(self.prog, "void *", 1)),
1786 Object(self.prog, "_Bool", True),
1787 )
1788
1789 def test_to_bool_bit_field(self):
1790 self.assertIdentical(
1791 implicit_convert("_Bool", Object(self.prog, "int", 0), bit_field_size=1),
1792 Object(self.prog, "_Bool", False, bit_field_size=1),
1793 )
1794 self.assertIdentical(
1795 implicit_convert("_Bool", Object(self.prog, "int", 4), bit_field_size=1),
1796 Object(self.prog, "_Bool", True, bit_field_size=1),
1797 )
1798
1799 def test_to_int(self):
1800 self.assertIdentical(
1801 implicit_convert("int", Object(self.prog, "unsigned long", 1234)),
1802 Object(self.prog, "int", 1234),
1803 )
1804 self.assertIdentical(
1805 implicit_convert("int", Object(self.prog, "double", 3.5)),
1806 Object(self.prog, "int", 3),
1807 )
1808 self.assertIdentical(
1809 implicit_convert("int", Object(self.prog, "_Bool", False)),
1810 Object(self.prog, "int", 0),
1811 )
1812 self.assertIdentical(
1813 implicit_convert("int", Object(self.prog, self.color_type, 2)),
1814 Object(self.prog, "int", 2),
1815 )
1816
1817 def test_to_int_bit_field(self):
1818 self.assertIdentical(
1819 implicit_convert(
1820 "int", Object(self.prog, "unsigned long", 1234), bit_field_size=3
1821 ),
1822 Object(self.prog, "int", 2, bit_field_size=3),
1823 )
1824 self.assertIdentical(
1825 implicit_convert(
1826 "unsigned int",
1827 Object(self.prog, "unsigned long", 1234),
1828 bit_field_size=2,
1829 ),
1830 Object(self.prog, "unsigned int", 2, bit_field_size=2),
1831 )
1832
1833 def test_to_float(self):
1834 self.assertIdentical(
1835 implicit_convert("double", Object(self.prog, "unsigned long", 1234)),
1836 Object(self.prog, "double", 1234.0),
1837 )
1838 self.assertIdentical(
1839 implicit_convert("double", Object(self.prog, "float", 3.5)),
1840 Object(self.prog, "double", 3.5),
1841 )
1842 self.assertIdentical(
1843 implicit_convert("double", Object(self.prog, "_Bool", False)),
1844 Object(self.prog, "double", 0.0),
1845 )
1846 self.assertIdentical(
1847 implicit_convert("double", Object(self.prog, self.color_type, 2)),
1848 Object(self.prog, "double", 2.0),
1849 )
1850
1851 def test_to_invalid_bit_field(self):
1852 self.assertRaisesRegex(
1853 ValueError,
1854 "bit field must be integer",
1855 implicit_convert,
1856 "float",
1857 Object(self.prog, "unsigned long", 1234),
1858 bit_field_size=3,
1859 )
1860
1861 def test_to_enum(self):
1862 self.assertIdentical(
1863 implicit_convert(self.color_type, Object(self.prog, "unsigned long", 1)),
1864 Object(self.prog, self.color_type, 1),
1865 )
1866 self.assertIdentical(
1867 implicit_convert(self.color_type, Object(self.prog, "double", 3.5)),
1868 Object(self.prog, self.color_type, 3),
1869 )
1870 self.assertIdentical(
1871 implicit_convert(self.color_type, Object(self.prog, "_Bool", False)),
1872 Object(self.prog, self.color_type, 0),
1873 )
1874 self.assertIdentical(
1875 implicit_convert(self.color_type, Object(self.prog, self.color_type, 2)),
1876 Object(self.prog, self.color_type, 2),
1877 )
1878
1879 def test_to_int_typedef(self):
1880 self.assertIdentical(
1881 implicit_convert(
1882 self.prog.typedef_type("INT", self.prog.type("int")),
1883 Object(self.prog, "int", 1234),
1884 ),
1885 Object(
1886 self.prog, self.prog.typedef_type("INT", self.prog.type("int")), 1234
1887 ),
1888 )
1889
1890 def test_struct_to_self(self):
1891 self.assertIdentical(
1892 implicit_convert(
1893 self.prog.struct_type(
1894 "point",
1895 8,
1896 (
1897 TypeMember(self.prog.int_type("int", 4, True), "x", 0),
1898 TypeMember(self.prog.int_type("int", 4, True), "y", 32),
1899 ),
1900 ),
1901 Object(self.prog, self.point_type, {"x": 1, "y": 2}),
1902 ),
1903 Object(self.prog, self.point_type, {"x": 1, "y": 2}),
1904 )
1905 self.assertIdentical(
1906 implicit_convert(
1907 self.point_type.qualified(Qualifiers.CONST),
1908 Object(self.prog, self.point_type, {"x": 1, "y": 2}),
1909 ),
1910 Object(
1911 self.prog, self.point_type.qualified(Qualifiers.CONST), {"x": 1, "y": 2}
1912 ),
1913 )
1914
1915 def test_struct_to_wrong_kind(self):
1916 self.assertRaisesRegex(
1917 TypeError,
1918 "cannot convert.*to incompatible type",
1919 implicit_convert,
1920 self.option_type,
1921 Object(self.prog, self.point_type, {"x": 1, "y": 2}),
1922 )
1923
1924 def test_struct_to_different_type_with_same_name(self):
1925 self.assertRaisesRegex(
1926 TypeError,
1927 "cannot convert.*to incompatible type",
1928 implicit_convert,
1929 self.prog.struct_type(
1930 "point",
1931 4,
1932 (
1933 TypeMember(self.prog.int_type("short", 2, True), "x", 0),
1934 TypeMember(self.prog.int_type("short", 2, True), "y", 16),
1935 ),
1936 ),
1937 Object(self.prog, self.point_type, {"x": 1, "y": 2}),
1938 )
1939
1940 def test_union_to_self(self):
1941 self.assertIdentical(
1942 implicit_convert(
1943 self.prog.union_type(
1944 "option",
1945 4,
1946 (
1947 TypeMember(self.prog.int_type("int", 4, True), "i"),
1948 TypeMember(self.prog.float_type("float", 4), "f"),
1949 ),
1950 ),
1951 Object(self.prog, self.option_type, {"i": 99}),
1952 ),
1953 Object(self.prog, self.option_type, {"i": 99}),
1954 )
1955 self.assertIdentical(
1956 implicit_convert(
1957 self.option_type.qualified(Qualifiers.CONST),
1958 Object(self.prog, self.option_type, {"i": 99}),
1959 ),
1960 Object(self.prog, self.option_type.qualified(Qualifiers.CONST), {"i": 99}),
1961 )
1962
1963 def test_pointer_to_self(self):
1964 self.assertIdentical(
1965 implicit_convert("int *", Object(self.prog, "int *", 0x4)),
1966 Object(self.prog, "int *", 0x4),
1967 )
1968 self.assertIdentical(
1969 implicit_convert("void *", Object(self.prog, "void *", 0x4)),
1970 Object(self.prog, "void *", 0x4),
1971 )
1972
1973 def test_pointer_to_typedef_of_self(self):
1974 self.assertIdentical(
1975 implicit_convert(
1976 "int *",
1977 Object(
1978 self.prog,
1979 self.prog.pointer_type(
1980 self.prog.typedef_type("INT", self.prog.type("int"))
1981 ),
1982 0x4,
1983 ),
1984 ),
1985 Object(self.prog, "int *", 0x4),
1986 )
1987 self.assertIdentical(
1988 implicit_convert(
1989 self.prog.pointer_type(
1990 self.prog.typedef_type("INT", self.prog.type("int"))
1991 ),
1992 Object(self.prog, "int *", 0x4),
1993 ),
1994 Object(
1995 self.prog,
1996 self.prog.pointer_type(
1997 self.prog.typedef_type("INT", self.prog.type("int"))
1998 ),
1999 0x4,
2000 ),
2001 )
2002
2003 def test_pointer_qualifiers(self):
2004 self.assertIdentical(
2005 implicit_convert("const int *", Object(self.prog, "const int *", 0x4)),
2006 Object(self.prog, "const int *", 0x4),
2007 )
2008 self.assertIdentical(
2009 implicit_convert("const int *", Object(self.prog, "int *", 0x4)),
2010 Object(self.prog, "const int *", 0x4),
2011 )
2012 self.assertIdentical(
2013 implicit_convert(
2014 "const volatile int *", Object(self.prog, "volatile int *", 0x4)
2015 ),
2016 Object(self.prog, "const volatile int *", 0x4),
2017 )
2018
2019 def test_void_pointer_qualifiers(self):
2020 self.assertIdentical(
2021 implicit_convert("const void *", Object(self.prog, "const void *", 0x4)),
2022 Object(self.prog, "const void *", 0x4),
2023 )
2024 self.assertIdentical(
2025 implicit_convert("const void *", Object(self.prog, "void *", 0x4)),
2026 Object(self.prog, "const void *", 0x4),
2027 )
2028 self.assertIdentical(
2029 implicit_convert(
2030 "const volatile void *", Object(self.prog, "volatile void *", 0x4)
2031 ),
2032 Object(self.prog, "const volatile void *", 0x4),
2033 )
2034
2035 def test_pointer_missing_qualifiers(self):
2036 self.assertRaisesRegex(
2037 TypeError,
2038 "cannot convert.*to incompatible type",
2039 implicit_convert,
2040 "int *",
2041 Object(self.prog, "const int *", 0x4),
2042 )
2043 self.assertRaisesRegex(
2044 TypeError,
2045 "cannot convert.*to incompatible type",
2046 implicit_convert,
2047 "volatile int *",
2048 Object(self.prog, "const int *", 0x4),
2049 )
2050 self.assertRaisesRegex(
2051 TypeError,
2052 "cannot convert.*to incompatible type",
2053 implicit_convert,
2054 "const int *",
2055 Object(self.prog, "const volatile int *", 0x4),
2056 )
2057
2058 def test_void_pointer_missing_qualifiers(self):
2059 self.assertRaisesRegex(
2060 TypeError,
2061 "cannot convert.*to incompatible type",
2062 implicit_convert,
2063 "void *",
2064 Object(self.prog, "const void *", 0x4),
2065 )
2066 self.assertRaisesRegex(
2067 TypeError,
2068 "cannot convert.*to incompatible type",
2069 implicit_convert,
2070 "volatile void *",
2071 Object(self.prog, "const void *", 0x4),
2072 )
2073 self.assertRaisesRegex(
2074 TypeError,
2075 "cannot convert.*to incompatible type",
2076 implicit_convert,
2077 "const void *",
2078 Object(self.prog, "const volatile void *", 0x4),
2079 )
2080
2081 def test_pointer_to_pointer(self):
2082 self.assertRaisesRegex(
2083 TypeError,
2084 "cannot convert.*to incompatible type",
2085 implicit_convert,
2086 "int **",
2087 Object(self.prog, "void **", 0x4),
2088 )
2089 self.assertRaisesRegex(
2090 TypeError,
2091 "cannot convert.*to incompatible type",
2092 implicit_convert,
2093 "void **",
2094 Object(self.prog, "int **", 0x4),
2095 )
2096
2097 def test_pointer_to_pointer_qualifiers(self):
2098 self.assertIdentical(
2099 implicit_convert("void * const *", Object(self.prog, "void **", 0x4)),
2100 Object(self.prog, "void * const *", 0x4),
2101 )
2102 self.assertRaisesRegex(
2103 TypeError,
2104 "cannot convert.*to incompatible type",
2105 implicit_convert,
2106 "const void **",
2107 Object(self.prog, "void **", 0x4),
2108 )
2109
2110 def test_to_void_pointer(self):
2111 self.assertIdentical(
2112 implicit_convert("void *", Object(self.prog, "int *", 0x4)),
2113 Object(self.prog, "void *", 0x4),
2114 )
2115 self.assertIdentical(
2116 implicit_convert("const void *", Object(self.prog, "int *", 0x4)),
2117 Object(self.prog, "const void *", 0x4),
2118 )
2119
2120 def test_from_void_pointer(self):
2121 self.assertIdentical(
2122 implicit_convert("int *", Object(self.prog, "void *", 0x4)),
2123 Object(self.prog, "int *", 0x4),
2124 )
2125 self.assertIdentical(
2126 implicit_convert("const int *", Object(self.prog, "void *", 0x4)),
2127 Object(self.prog, "const int *", 0x4),
2128 )
2129
2130 def test_pointer_to_different_int_types(self):
2131 self.assertRaisesRegex(
2132 TypeError,
2133 "cannot convert.*to incompatible type",
2134 implicit_convert,
2135 "long *",
2136 Object(self.prog, "long long *", 0x4),
2137 )
2138
2139 def test_pointer_to_same_struct(self):
2140 self.assertIdentical(
2141 implicit_convert(
2142 self.prog.pointer_type(self.point_type),
2143 Object(self.prog, self.prog.pointer_type(self.point_type), 0x4),
2144 ),
2145 Object(self.prog, self.prog.pointer_type(self.point_type), 0x4),
2146 )
2147
2148 def test_pointer_to_incomplete_struct(self):
2149 self.assertIdentical(
2150 implicit_convert(
2151 self.prog.pointer_type(self.point_type),
2152 Object(
2153 self.prog,
2154 self.prog.pointer_type(self.prog.struct_type("point")),
2155 0x4,
2156 ),
2157 ),
2158 Object(self.prog, self.prog.pointer_type(self.point_type), 0x4),
2159 )
2160 self.assertIdentical(
2161 implicit_convert(
2162 self.prog.pointer_type(self.prog.struct_type("point")),
2163 Object(self.prog, self.prog.pointer_type(self.point_type), 0x4),
2164 ),
2165 Object(
2166 self.prog, self.prog.pointer_type(self.prog.struct_type("point")), 0x4
2167 ),
2168 )
2169
2170 def test_pointer_to_different_struct(self):
2171 self.assertRaisesRegex(
2172 TypeError,
2173 "cannot convert.*to incompatible type",
2174 implicit_convert,
2175 self.prog.pointer_type(self.prog.struct_type("foo")),
2176 Object(self.prog, self.prog.pointer_type(self.point_type), 0x4),
2177 )
2178
2179 def test_pointer_to_enum_same(self):
2180 self.assertIdentical(
2181 implicit_convert(
2182 self.prog.pointer_type(self.color_type),
2183 Object(self.prog, self.prog.pointer_type(self.color_type), 0x4),
2184 ),
2185 Object(self.prog, self.prog.pointer_type(self.color_type), 0x4),
2186 )
2187
2188 def test_pointer_to_enum_compatible_type(self):
2189 self.assertIdentical(
2190 implicit_convert(
2191 "unsigned int *",
2192 Object(self.prog, self.prog.pointer_type(self.color_type), 0x4),
2193 ),
2194 Object(self.prog, "unsigned int *", 0x4),
2195 )
2196 self.assertIdentical(
2197 implicit_convert(
2198 self.prog.pointer_type(self.color_type),
2199 Object(self.prog, "unsigned int *", 0x4),
2200 ),
2201 Object(self.prog, self.prog.pointer_type(self.color_type), 0x4),
2202 )
2203
2204 def test_pointer_to_incomplete_enum(self):
2205 incomplete_type = self.prog.pointer_type(self.prog.enum_type("color"))
2206 self.assertIdentical(
2207 implicit_convert(
2208 incomplete_type,
2209 Object(self.prog, self.prog.pointer_type(self.color_type), 0x4),
2210 ),
2211 Object(self.prog, incomplete_type, 0x4),
2212 )
2213 self.assertIdentical(
2214 implicit_convert(
2215 self.prog.pointer_type(self.color_type),
2216 Object(self.prog, incomplete_type, 0x4),
2217 ),
2218 Object(self.prog, self.prog.pointer_type(self.color_type), 0x4),
2219 )
2220
2221 def test_pointer_to_different_enum_with_same_name(self):
2222 other_type = self.prog.pointer_type(
2223 self.prog.enum_type("color", self.prog.type("int"), ())
2224 )
2225 self.assertRaisesRegex(
2226 TypeError,
2227 "cannot convert.*to incompatible type",
2228 implicit_convert,
2229 other_type,
2230 Object(self.prog, self.prog.pointer_type(self.color_type), 0x4),
2231 )
2232 self.assertRaisesRegex(
2233 TypeError,
2234 "cannot convert.*to incompatible type",
2235 implicit_convert,
2236 self.prog.pointer_type(self.color_type),
2237 Object(self.prog, other_type, 0x4),
2238 )
2239
2240 def test_pointer_to_different_enum_with_same_compatible_type(self):
2241 other_type = self.prog.pointer_type(
2242 self.prog.enum_type("foo", self.prog.type("unsigned int"), ())
2243 )
2244 self.assertRaisesRegex(
2245 TypeError,
2246 "cannot convert.*to incompatible type",
2247 implicit_convert,
2248 other_type,
2249 Object(self.prog, self.prog.pointer_type(self.color_type), 0x4),
2250 )
2251 self.assertRaisesRegex(
2252 TypeError,
2253 "cannot convert.*to incompatible type",
2254 implicit_convert,
2255 self.prog.pointer_type(self.color_type),
2256 Object(self.prog, other_type, 0x4),
2257 )
2258
2259 def test_pointer_to_different_incomplete_enum(self):
2260 other_type = self.prog.pointer_type(self.prog.enum_type("foo"))
2261 self.assertRaisesRegex(
2262 TypeError,
2263 "cannot convert.*to incompatible type",
2264 implicit_convert,
2265 other_type,
2266 Object(self.prog, self.prog.pointer_type(self.color_type), 0x4),
2267 )
2268 self.assertRaisesRegex(
2269 TypeError,
2270 "cannot convert.*to incompatible type",
2271 implicit_convert,
2272 self.prog.pointer_type(self.color_type),
2273 Object(self.prog, other_type, 0x4),
2274 )
2275
2276 def test_array_to_pointer(self):
2277 self.assertIdentical(
2278 implicit_convert("int *", Object(self.prog, "int [3]", address=0x8)),
2279 Object(self.prog, "int *", 0x8),
2280 )
2281
2282 def test_pointer_to_array(self):
2283 self.assertIdentical(
2284 implicit_convert("int (*)[3]", Object(self.prog, "int (*)[3]", 0x8)),
2285 Object(self.prog, "int (*)[3]", 0x8),
2286 )
2287
2288 def test_pointer_to_incomplete_array(self):
2289 self.assertIdentical(
2290 implicit_convert("int (*)[]", Object(self.prog, "int (*)[]", 0x8)),
2291 Object(self.prog, "int (*)[]", 0x8),
2292 )
2293
2294 def test_pointer_to_complete_and_incomplete_array(self):
2295 self.assertIdentical(
2296 implicit_convert("int (*)[3]", Object(self.prog, "int (*)[]", 0x8)),
2297 Object(self.prog, "int (*)[3]", 0x8),
2298 )
2299 self.assertIdentical(
2300 implicit_convert("int (*)[]", Object(self.prog, "int (*)[3]", 0x8)),
2301 Object(self.prog, "int (*)[]", 0x8),
2302 )
2303
2304 def test_pointer_to_array_with_different_length(self):
2305 self.assertRaisesRegex(
2306 TypeError,
2307 "cannot convert.*to incompatible type",
2308 implicit_convert,
2309 "int (*)[4]",
2310 Object(self.prog, "int (*)[3]", 0x8),
2311 )
2312
2313 def test_pointer_to_array_with_different_element_type(self):
2314 self.assertRaisesRegex(
2315 TypeError,
2316 "cannot convert.*to incompatible type",
2317 implicit_convert,
2318 "long (*)[3]",
2319 Object(self.prog, "long long (*)[3]", 0x8),
2320 )
2321
2322 def test_function_pointer_to_self(self):
2323 self.assertIdentical(
2324 implicit_convert(
2325 self.prog.pointer_type(
2326 self.prog.function_type(
2327 self.prog.type("int"),
2328 (
2329 TypeParameter(self.prog.type("long")),
2330 TypeParameter(self.prog.type("void *")),
2331 ),
2332 )
2333 ),
2334 Object(
2335 self.prog,
2336 self.prog.pointer_type(
2337 self.prog.function_type(
2338 self.prog.type("int"),
2339 (
2340 # Note: parameter names don't matter.
2341 TypeParameter(self.prog.type("long"), "l"),
2342 TypeParameter(self.prog.type("void *"), "p"),
2343 ),
2344 )
2345 ),
2346 0x8,
2347 ),
2348 ),
2349 Object(
2350 self.prog,
2351 self.prog.pointer_type(
2352 self.prog.function_type(
2353 self.prog.type("int"),
2354 (
2355 TypeParameter(self.prog.type("long")),
2356 TypeParameter(self.prog.type("void *")),
2357 ),
2358 )
2359 ),
2360 0x8,
2361 ),
2362 )
2363
2364 def test_variadic_function_pointer_to_self(self):
2365 self.assertIdentical(
2366 implicit_convert(
2367 self.prog.pointer_type(
2368 self.prog.function_type(self.prog.type("int"), (), is_variadic=True)
2369 ),
2370 Object(
2371 self.prog,
2372 self.prog.pointer_type(
2373 self.prog.function_type(
2374 self.prog.type("int"), (), is_variadic=True
2375 )
2376 ),
2377 0x8,
2378 ),
2379 ),
2380 Object(
2381 self.prog,
2382 self.prog.pointer_type(
2383 self.prog.function_type(self.prog.type("int"), (), is_variadic=True)
2384 ),
2385 0x8,
2386 ),
2387 )
2388
2389 def test_function_to_function_pointer(self):
2390 self.assertIdentical(
2391 implicit_convert(
2392 self.prog.pointer_type(
2393 self.prog.function_type(
2394 self.prog.type("int"),
2395 (
2396 TypeParameter(self.prog.type("long")),
2397 TypeParameter(self.prog.type("void *")),
2398 ),
2399 )
2400 ),
2401 Object(
2402 self.prog,
2403 self.prog.function_type(
2404 self.prog.type("int"),
2405 (
2406 TypeParameter(self.prog.type("long"), "l"),
2407 TypeParameter(self.prog.type("void *"), "p"),
2408 ),
2409 ),
2410 address=0x8,
2411 ),
2412 ),
2413 Object(
2414 self.prog,
2415 self.prog.pointer_type(
2416 self.prog.function_type(
2417 self.prog.type("int"),
2418 (
2419 TypeParameter(self.prog.type("long")),
2420 TypeParameter(self.prog.type("void *")),
2421 ),
2422 )
2423 ),
2424 0x8,
2425 ),
2426 )
2427
2428 def test_function_pointers_with_different_return_types(self):
2429 type1 = self.prog.pointer_type(
2430 self.prog.function_type(self.prog.type("long"), ())
2431 )
2432 type2 = self.prog.pointer_type(
2433 self.prog.function_type(self.prog.type("long long"), ())
2434 )
2435 self.assertRaisesRegex(
2436 TypeError,
2437 "cannot convert.*to incompatible type",
2438 implicit_convert,
2439 type1,
2440 Object(self.prog, type2, 0x8),
2441 )
2442 self.assertRaisesRegex(
2443 TypeError,
2444 "cannot convert.*to incompatible type",
2445 implicit_convert,
2446 type2,
2447 Object(self.prog, type1, 0x8),
2448 )
2449
2450 def test_function_pointers_with_different_number_of_parameters(
2451 self,
2452 ):
2453 type1 = self.prog.pointer_type(
2454 self.prog.function_type(self.prog.type("int"), ())
2455 )
2456 type2 = self.prog.pointer_type(
2457 self.prog.function_type(
2458 self.prog.type("int"), (TypeParameter(self.prog.type("int")),)
2459 )
2460 )
2461 self.assertRaisesRegex(
2462 TypeError,
2463 "cannot convert.*to incompatible type",
2464 implicit_convert,
2465 type1,
2466 Object(self.prog, type2, 0x8),
2467 )
2468 self.assertRaisesRegex(
2469 TypeError,
2470 "cannot convert.*to incompatible type",
2471 implicit_convert,
2472 type2,
2473 Object(self.prog, type1, 0x8),
2474 )
2475
2476 def test_function_pointers_with_incompatible_parameter_types(self):
2477 type1 = self.prog.pointer_type(
2478 self.prog.function_type(
2479 self.prog.type("int"), (TypeParameter(self.prog.type("long")),)
2480 )
2481 )
2482 type2 = self.prog.pointer_type(
2483 self.prog.function_type(
2484 self.prog.type("int"), (TypeParameter(self.prog.type("long long")),)
2485 )
2486 )
2487 self.assertRaisesRegex(
2488 TypeError,
2489 "cannot convert.*to incompatible type",
2490 implicit_convert,
2491 type1,
2492 Object(self.prog, type2, 0x8),
2493 )
2494 self.assertRaisesRegex(
2495 TypeError,
2496 "cannot convert.*to incompatible type",
2497 implicit_convert,
2498 type2,
2499 Object(self.prog, type1, 0x8),
2500 )
2501
2502 def test_function_pointers_with_different_is_variadic(self):
2503 type1 = self.prog.pointer_type(
2504 self.prog.function_type(self.prog.type("int"), (), is_variadic=False)
2505 )
2506 type2 = self.prog.pointer_type(
2507 self.prog.function_type(self.prog.type("int"), (), is_variadic=True)
2508 )
2509 self.assertRaisesRegex(
2510 TypeError,
2511 "cannot convert.*to incompatible type",
2512 implicit_convert,
2513 type1,
2514 Object(self.prog, type2, 0x8),
2515 )
2516 self.assertRaisesRegex(
2517 TypeError,
2518 "cannot convert.*to incompatible type",
2519 implicit_convert,
2520 type2,
2521 Object(self.prog, type1, 0x8),
2522 )
2523
2524 def test_pointer_to_int(self):
2525 self.assertRaisesRegex(
2526 TypeError,
2527 "cannot convert.*to incompatible type",
2528 implicit_convert,
2529 "int",
2530 Object(self.prog, "void *", 0),
2531 )
2532
2533 def test_to_void(self):
2534 self.assertRaisesRegex(
2535 TypeError,
2536 "cannot convert to",
2537 implicit_convert,
2538 "void",
2539 Object(self.prog, "void"),
2540 )
2541
2542 def test_to_array(self):
2543 self.assertRaisesRegex(
2544 TypeError,
2545 "cannot convert to",
2546 implicit_convert,
2547 "int [3]",
2548 Object(self.prog, "int [3]", address=0x8),
2549 )
2550
2551 def test_to_function(self):
2552 self.assertRaisesRegex(
2553 TypeError,
2554 "cannot convert to",
2555 implicit_convert,
2556 self.prog.function_type(self.prog.type("int"), ()),
2557 Object(
2558 self.prog,
2559 self.prog.function_type(self.prog.type("int"), ()),
2560 address=0x8,
2561 ),
2562 )
2563
2564
2565 class TestPrettyPrintObject(MockProgramTestCase):
2566 def test_int(self):
2567 obj = Object(self.prog, "int", value=99)
2568 self.assertEqual(str(obj), "(int)99")
2569 self.assertEqual(obj.format_(type_name=False), "99")
2570 self.assertEqual(
2571 str(Object(self.prog, "const int", value=-99)), "(const int)-99"
2572 )
2573
2574 def test_int_decimal(self):
2575 self.assertEqual(
2576 Object(self.prog, "int", 0).format_(type_name=False, integer_base=10), "0"
2577 )
2578 self.assertEqual(
2579 Object(self.prog, "unsigned int", 0).format_(
2580 type_name=False, integer_base=10
2581 ),
2582 "0",
2583 )
2584 self.assertEqual(
2585 Object(self.prog, "int", 13).format_(type_name=False, integer_base=10), "13"
2586 )
2587 self.assertEqual(
2588 Object(self.prog, "unsigned int", 13).format_(
2589 type_name=False, integer_base=10
2590 ),
2591 "13",
2592 )
2593 self.assertEqual(
2594 Object(self.prog, "int", -13).format_(type_name=False, integer_base=10),
2595 "-13",
2596 )
2597
2598 def test_int_hex(self):
2599 self.assertEqual(
2600 Object(self.prog, "int", 0).format_(type_name=False, integer_base=16), "0x0"
2601 )
2602 self.assertEqual(
2603 Object(self.prog, "unsigned int", 0).format_(
2604 type_name=False, integer_base=16
2605 ),
2606 "0x0",
2607 )
2608 self.assertEqual(
2609 Object(self.prog, "int", 13).format_(type_name=False, integer_base=16),
2610 "0xd",
2611 )
2612 self.assertEqual(
2613 Object(self.prog, "unsigned int", 13).format_(
2614 type_name=False, integer_base=16
2615 ),
2616 "0xd",
2617 )
2618 self.assertEqual(
2619 Object(self.prog, "int", -13).format_(type_name=False, integer_base=16),
2620 "-0xd",
2621 )
2622
2623 def test_int_oct(self):
2624 self.assertEqual(
2625 Object(self.prog, "int", 0).format_(type_name=False, integer_base=8), "0"
2626 )
2627 self.assertEqual(
2628 Object(self.prog, "unsigned int", 0).format_(
2629 type_name=False, integer_base=8
2630 ),
2631 "0",
2632 )
2633 self.assertEqual(
2634 Object(self.prog, "int", 13).format_(type_name=False, integer_base=8), "015"
2635 )
2636 self.assertEqual(
2637 Object(self.prog, "unsigned int", 13).format_(
2638 type_name=False, integer_base=8
2639 ),
2640 "015",
2641 )
2642 self.assertEqual(
2643 Object(self.prog, "int", -13).format_(type_name=False, integer_base=8),
2644 "-015",
2645 )
2646
2647 def test_char(self):
2648 obj = Object(self.prog, "char", value=65)
2649 self.assertEqual(str(obj), "(char)65")
2650 self.assertEqual(obj.format_(char=True), "(char)'A'")
2651 self.assertEqual(
2652 Object(self.prog, "signed char", value=65).format_(char=True),
2653 "(signed char)'A'",
2654 )
2655 self.assertEqual(
2656 Object(self.prog, "unsigned char", value=65).format_(char=True),
2657 "(unsigned char)'A'",
2658 )
2659 self.assertEqual(
2660 Object(
2661 self.prog,
2662 self.prog.typedef_type("uint8_t", self.prog.type("unsigned char")),
2663 value=65,
2664 ).format_(char=True),
2665 "(uint8_t)65",
2666 )
2667
2668 def test_bool(self):
2669 self.assertEqual(str(Object(self.prog, "_Bool", value=False)), "(_Bool)0")
2670 self.assertEqual(
2671 str(Object(self.prog, "const _Bool", value=True)), "(const _Bool)1"
2672 )
2673
2674 def test_float(self):
2675 self.assertEqual(str(Object(self.prog, "double", value=2.0)), "(double)2.0")
2676 self.assertEqual(str(Object(self.prog, "float", value=0.5)), "(float)0.5")
2677
2678 def test_typedef(self):
2679 type_ = self.prog.typedef_type("INT", self.prog.int_type("int", 4, True))
2680 self.assertEqual(str(Object(self.prog, type_, value=99)), "(INT)99")
2681
2682 type_ = self.prog.typedef_type(
2683 "INT", self.prog.int_type("int", 4, True), qualifiers=Qualifiers.CONST
2684 )
2685 self.assertEqual(str(Object(self.prog, type_, value=99)), "(const INT)99")
2686
2687 type_ = self.prog.typedef_type(
2688 "CINT", self.prog.int_type("int", 4, True, qualifiers=Qualifiers.CONST)
2689 )
2690 self.assertEqual(str(Object(self.prog, type_, value=99)), "(CINT)99")
2691
2692 def test_struct(self):
2693 segment = (
2694 (99).to_bytes(4, "little")
2695 + (-1).to_bytes(4, "little", signed=True)
2696 + (12345).to_bytes(4, "little", signed=True)
2697 + (0).to_bytes(4, "little", signed=True)
2698 )
2699 self.add_memory_segment(segment, virt_addr=0xFFFF0000)
2700 self.types.append(self.point_type)
2701
2702 obj = Object(self.prog, "struct point", address=0xFFFF0000)
2703 self.assertEqual(
2704 str(obj),
2705 """\
2706 (struct point){
2707 .x = (int)99,
2708 .y = (int)-1,
2709 }""",
2710 )
2711 self.assertEqual(
2712 obj.format_(member_type_names=False),
2713 """\
2714 (struct point){
2715 .x = 99,
2716 .y = -1,
2717 }""",
2718 )
2719 self.assertEqual(
2720 obj.format_(members_same_line=True),
2721 "(struct point){ .x = (int)99, .y = (int)-1 }",
2722 )
2723 self.assertEqual(
2724 obj.format_(member_names=False),
2725 """\
2726 (struct point){
2727 (int)99,
2728 (int)-1,
2729 }""",
2730 )
2731 self.assertEqual(
2732 obj.format_(members_same_line=True, member_names=False),
2733 "(struct point){ (int)99, (int)-1 }",
2734 )
2735
2736 type_ = self.prog.struct_type(
2737 "foo",
2738 16,
2739 (
2740 TypeMember(self.point_type, "point"),
2741 TypeMember(
2742 self.prog.struct_type(
2743 None,
2744 8,
2745 (
2746 TypeMember(self.prog.int_type("int", 4, True), "bar"),
2747 TypeMember(self.prog.int_type("int", 4, True), "baz", 32),
2748 ),
2749 ),
2750 None,
2751 64,
2752 ),
2753 ),
2754 )
2755 obj = Object(self.prog, type_, address=0xFFFF0000)
2756 expected = """\
2757 (struct foo){
2758 .point = (struct point){
2759 .x = (int)99,
2760 .y = (int)-1,
2761 },
2762 .bar = (int)12345,
2763 .baz = (int)0,
2764 }"""
2765 self.assertEqual(str(obj), expected)
2766 self.assertEqual(str(obj.read_()), expected)
2767
2768 self.add_memory_segment(
2769 (
2770 (99).to_bytes(8, "little")
2771 + (-1).to_bytes(8, "little", signed=True)
2772 + (12345).to_bytes(8, "little", signed=True)
2773 + (0).to_bytes(8, "little", signed=True)
2774 ),
2775 virt_addr=0xFFFF8000,
2776 )
2777
2778 type_ = self.prog.struct_type(
2779 "foo",
2780 32,
2781 (
2782 TypeMember(
2783 self.prog.struct_type(
2784 "long_point",
2785 16,
2786 (
2787 TypeMember(self.prog.int_type("long", 8, True), "x"),
2788 TypeMember(self.prog.int_type("long", 8, True), "y", 64),
2789 ),
2790 ),
2791 "point",
2792 ),
2793 TypeMember(self.prog.int_type("long", 8, True), "bar", 128),
2794 TypeMember(self.prog.int_type("long", 8, True), "baz", 192),
2795 ),
2796 )
2797 obj = Object(self.prog, type_, address=0xFFFF8000)
2798 expected = """\
2799 (struct foo){
2800 .point = (struct long_point){
2801 .x = (long)99,
2802 .y = (long)-1,
2803 },
2804 .bar = (long)12345,
2805 .baz = (long)0,
2806 }"""
2807 self.assertEqual(str(obj), expected)
2808 self.assertEqual(str(obj.read_()), expected)
2809
2810 type_ = self.prog.struct_type("foo", 0, ())
2811 self.assertEqual(str(Object(self.prog, type_, address=0)), "(struct foo){}")
2812
2813 obj = Object(self.prog, self.point_type, value={"x": 1})
2814 self.assertEqual(
2815 obj.format_(implicit_members=False),
2816 """\
2817 (struct point){
2818 .x = (int)1,
2819 }""",
2820 )
2821 self.assertEqual(
2822 obj.format_(member_names=False, implicit_members=False),
2823 """\
2824 (struct point){
2825 (int)1,
2826 }""",
2827 )
2828 obj = Object(self.prog, self.point_type, value={"y": 1})
2829 self.assertEqual(
2830 obj.format_(implicit_members=False),
2831 """\
2832 (struct point){
2833 .y = (int)1,
2834 }""",
2835 )
2836 self.assertEqual(
2837 obj.format_(member_names=False, implicit_members=False),
2838 """\
2839 (struct point){
2840 (int)0,
2841 (int)1,
2842 }""",
2843 )
2844
2845 def test_bit_field(self):
2846 self.add_memory_segment(b"\x07\x10\x5e\x5f\x1f\0\0\0", virt_addr=0xFFFF0000)
2847 type_ = self.prog.struct_type(
2848 "bits",
2849 8,
2850 (
2851 TypeMember(
2852 Object(
2853 self.prog, self.prog.int_type("int", 4, True), bit_field_size=4
2854 ),
2855 "x",
2856 0,
2857 ),
2858 TypeMember(
2859 Object(
2860 self.prog,
2861 self.prog.int_type("int", 4, True, qualifiers=Qualifiers.CONST),
2862 bit_field_size=28,
2863 ),
2864 "y",
2865 4,
2866 ),
2867 TypeMember(
2868 Object(
2869 self.prog, self.prog.int_type("int", 4, True), bit_field_size=5
2870 ),
2871 "z",
2872 32,
2873 ),
2874 ),
2875 )
2876
2877 obj = Object(self.prog, type_, address=0xFFFF0000)
2878 self.assertEqual(
2879 str(obj),
2880 """\
2881 (struct bits){
2882 .x = (int)7,
2883 .y = (const int)100000000,
2884 .z = (int)-1,
2885 }""",
2886 )
2887
2888 self.assertEqual(str(obj.x), "(int)7")
2889 self.assertEqual(str(obj.y), "(const int)100000000")
2890 self.assertEqual(str(obj.z), "(int)-1")
2891
2892 def test_union(self):
2893 self.add_memory_segment(b"\0\0\x80?", virt_addr=0xFFFF0000)
2894 self.types.append(self.option_type)
2895 self.assertEqual(
2896 str(Object(self.prog, "union option", address=0xFFFF0000)),
2897 """\
2898 (union option){
2899 .i = (int)1065353216,
2900 .f = (float)1.0,
2901 }""",
2902 )
2903
2904 def test_enum(self):
2905 self.assertEqual(
2906 str(Object(self.prog, self.color_type, value=0)), "(enum color)RED"
2907 )
2908 self.assertEqual(
2909 str(Object(self.prog, self.color_type, value=1)), "(enum color)GREEN"
2910 )
2911 self.assertEqual(
2912 str(Object(self.prog, self.color_type, value=4)), "(enum color)4"
2913 )
2914 obj = Object(self.prog, self.prog.enum_type("color"), address=0)
2915 self.assertRaisesRegex(TypeError, "cannot format incomplete enum", str, obj)
2916
2917 def test_pointer(self):
2918 self.add_memory_segment((99).to_bytes(4, "little"), virt_addr=0xFFFF0000)
2919 obj = Object(self.prog, "int *", value=0xFFFF0000)
2920 self.assertEqual(str(obj), "*(int *)0xffff0000 = 99")
2921 self.assertEqual(obj.format_(dereference=False), "(int *)0xffff0000")
2922 self.assertEqual(
2923 str(Object(self.prog, "int *", value=0x7FFFFFFF)), "(int *)0x7fffffff"
2924 )
2925
2926 def test_void_pointer(self):
2927 self.add_memory_segment((99).to_bytes(4, "little"), virt_addr=0xFFFF0000)
2928 self.assertEqual(
2929 str(Object(self.prog, "void *", value=0xFFFF0000)), "(void *)0xffff0000"
2930 )
2931
2932 def test_pointer_typedef(self):
2933 self.add_memory_segment(
2934 (0xFFFF00F0).to_bytes(8, "little"), virt_addr=0xFFFF0000
2935 )
2936 type_ = self.prog.typedef_type(
2937 "HANDLE",
2938 self.prog.pointer_type(self.prog.pointer_type(self.prog.void_type())),
2939 )
2940 self.assertEqual(
2941 str(Object(self.prog, type_, value=0xFFFF0000)),
2942 "*(HANDLE)0xffff0000 = 0xffff00f0",
2943 )
2944
2945 # TODO: test symbolize.
2946
2947 def test_c_string(self):
2948 self.add_memory_segment(b"hello\0", virt_addr=0xFFFF0000)
2949 self.add_memory_segment(b"unterminated", virt_addr=0xFFFF0010)
2950 self.add_memory_segment(b'"escape\tme\\\0', virt_addr=0xFFFF0020)
2951
2952 obj = Object(self.prog, "char *", value=0xFFFF0000)
2953 self.assertEqual(str(obj), '(char *)0xffff0000 = "hello"')
2954 self.assertEqual(obj.format_(string=False), "*(char *)0xffff0000 = 104")
2955 self.assertEqual(str(Object(self.prog, "char *", value=0x0)), "(char *)0x0")
2956 self.assertEqual(
2957 str(Object(self.prog, "char *", value=0xFFFF0010)), "(char *)0xffff0010"
2958 )
2959 self.assertEqual(
2960 str(Object(self.prog, "char *", value=0xFFFF0020)),
2961 r'(char *)0xffff0020 = "\"escape\tme\\"',
2962 )
2963
2964 def test_basic_array(self):
2965 segment = bytearray()
2966 for i in range(5):
2967 segment.extend(i.to_bytes(4, "little"))
2968 self.add_memory_segment(segment, virt_addr=0xFFFF0000)
2969 obj = Object(self.prog, "int [5]", address=0xFFFF0000)
2970
2971 self.assertEqual(str(obj), "(int [5]){ 0, 1, 2, 3, 4 }")
2972 self.assertEqual(
2973 obj.format_(type_name=False, element_type_names=True),
2974 "{ (int)0, (int)1, (int)2, (int)3, (int)4 }",
2975 )
2976 self.assertEqual(
2977 obj.format_(element_indices=True),
2978 "(int [5]){ [1] = 1, [2] = 2, [3] = 3, [4] = 4 }",
2979 )
2980 self.assertEqual(
2981 obj.format_(element_indices=True, implicit_elements=True),
2982 "(int [5]){ [0] = 0, [1] = 1, [2] = 2, [3] = 3, [4] = 4 }",
2983 )
2984 self.assertEqual(obj.format_(columns=27), str(obj))
2985
2986 for columns in range(22, 26):
2987 self.assertEqual(
2988 obj.format_(columns=columns),
2989 """\
2990 (int [5]){
2991 0, 1, 2, 3, 4,
2992 }""",
2993 )
2994 for columns in range(19, 22):
2995 self.assertEqual(
2996 obj.format_(columns=columns),
2997 """\
2998 (int [5]){
2999 0, 1, 2, 3,
3000 4,
3001 }""",
3002 )
3003 for columns in range(16, 19):
3004 self.assertEqual(
3005 obj.format_(columns=columns),
3006 """\
3007 (int [5]){
3008 0, 1, 2,
3009 3, 4,
3010 }""",
3011 )
3012 for columns in range(13, 16):
3013 self.assertEqual(
3014 obj.format_(columns=columns),
3015 """\
3016 (int [5]){
3017 0, 1,
3018 2, 3,
3019 4,
3020 }""",
3021 )
3022 for columns in range(13):
3023 self.assertEqual(
3024 obj.format_(columns=columns),
3025 """\
3026 (int [5]){
3027 0,
3028 1,
3029 2,
3030 3,
3031 4,
3032 }""",
3033 )
3034 self.assertEqual(
3035 obj.format_(elements_same_line=False),
3036 """\
3037 (int [5]){
3038 0,
3039 1,
3040 2,
3041 3,
3042 4,
3043 }""",
3044 )
3045
3046 def test_nested_array(self):
3047 segment = bytearray()
3048 for i in range(10):
3049 segment.extend(i.to_bytes(4, "little"))
3050 self.add_memory_segment(segment, virt_addr=0xFFFF0000)
3051 obj = Object(self.prog, "int [2][5]", address=0xFFFF0000)
3052
3053 self.assertEqual(
3054 str(obj), "(int [2][5]){ { 0, 1, 2, 3, 4 }, { 5, 6, 7, 8, 9 } }"
3055 )
3056 self.assertEqual(obj.format_(columns=52), str(obj))
3057 for columns in range(45, 52):
3058 self.assertEqual(
3059 obj.format_(columns=columns),
3060 """\
3061 (int [2][5]){
3062 { 0, 1, 2, 3, 4 }, { 5, 6, 7, 8, 9 },
3063 }""",
3064 )
3065 for columns in range(26, 45):
3066 self.assertEqual(
3067 obj.format_(columns=columns),
3068 """\
3069 (int [2][5]){
3070 { 0, 1, 2, 3, 4 },
3071 { 5, 6, 7, 8, 9 },
3072 }""",
3073 )
3074 for columns in range(24, 26):
3075 self.assertEqual(
3076 obj.format_(columns=columns),
3077 """\
3078 (int [2][5]){
3079 {
3080 0, 1, 2,
3081 3, 4,
3082 },
3083 {
3084 5, 6, 7,
3085 8, 9,
3086 },
3087 }""",
3088 )
3089 for columns in range(21, 24):
3090 self.assertEqual(
3091 obj.format_(columns=columns),
3092 """\
3093 (int [2][5]){
3094 {
3095 0, 1,
3096 2, 3,
3097 4,
3098 },
3099 {
3100 5, 6,
3101 7, 8,
3102 9,
3103 },
3104 }""",
3105 )
3106 for columns in range(21):
3107 self.assertEqual(
3108 obj.format_(columns=columns),
3109 """\
3110 (int [2][5]){
3111 {
3112 0,
3113 1,
3114 2,
3115 3,
3116 4,
3117 },
3118 {
3119 5,
3120 6,
3121 7,
3122 8,
3123 9,
3124 },
3125 }""",
3126 )
3127
3128 def test_array_member(self):
3129 segment = bytearray()
3130 for i in range(5):
3131 segment.extend(i.to_bytes(4, "little"))
3132 self.add_memory_segment(segment, virt_addr=0xFFFF0000)
3133
3134 type_ = self.prog.struct_type(
3135 None,
3136 20,
3137 (
3138 TypeMember(
3139 self.prog.array_type(self.prog.int_type("int", 4, True), 5), "arr"
3140 ),
3141 ),
3142 )
3143 obj = Object(self.prog, type_, address=0xFFFF0000)
3144
3145 self.assertEqual(
3146 str(obj),
3147 """\
3148 (struct <anonymous>){
3149 .arr = (int [5]){ 0, 1, 2, 3, 4 },
3150 }""",
3151 )
3152 self.assertEqual(obj.format_(columns=42), str(obj))
3153
3154 self.assertEqual(
3155 obj.format_(columns=41),
3156 """\
3157 (struct <anonymous>){
3158 .arr = (int [5]){
3159 0, 1, 2, 3, 4,
3160 },
3161 }""",
3162 )
3163
3164 self.assertEqual(
3165 obj.format_(columns=18),
3166 """\
3167 (struct <anonymous>){
3168 .arr = (int [5]){
3169 0,
3170 1,
3171 2,
3172 3,
3173 4,
3174 },
3175 }""",
3176 )
3177
3178 def test_array_of_struct(self):
3179 segment = bytearray()
3180 for i in range(1, 5):
3181 segment.extend(i.to_bytes(4, "little"))
3182 self.add_memory_segment(segment, virt_addr=0xFFFF0000)
3183 self.types.append(self.point_type)
3184
3185 obj = Object(self.prog, "struct point [2]", address=0xFFFF0000)
3186 self.assertEqual(
3187 str(obj),
3188 """\
3189 (struct point [2]){
3190 {
3191 .x = (int)1,
3192 .y = (int)2,
3193 },
3194 {
3195 .x = (int)3,
3196 .y = (int)4,
3197 },
3198 }""",
3199 )
3200
3201 def test_zero_length_array(self):
3202 self.assertEqual(str(Object(self.prog, "int []", address=0)), "(int []){}")
3203 self.assertEqual(str(Object(self.prog, "int [0]", address=0)), "(int [0]){}")
3204
3205 def test_array_zeroes(self):
3206 segment = bytearray(16)
3207 self.add_memory_segment(segment, virt_addr=0xFFFF0000)
3208 self.types.append(self.point_type)
3209 self.types.append(self.prog.struct_type("empty", 0, ()))
3210
3211 obj = Object(self.prog, "int [2]", address=0xFFFF0000)
3212 self.assertEqual(str(obj), "(int [2]){}")
3213 self.assertEqual(obj.format_(implicit_elements=True), "(int [2]){ 0, 0 }")
3214 segment[:4] = (99).to_bytes(4, "little")
3215 self.assertEqual(str(obj), "(int [2]){ 99 }")
3216 segment[:4] = (0).to_bytes(4, "little")
3217 segment[4:8] = (99).to_bytes(4, "little")
3218 self.assertEqual(str(obj), "(int [2]){ 0, 99 }")
3219
3220 obj = Object(self.prog, "struct point [2]", address=0xFFFF0000)
3221 self.assertEqual(
3222 str(obj),
3223 """\
3224 (struct point [2]){
3225 {
3226 .x = (int)0,
3227 .y = (int)99,
3228 },
3229 }""",
3230 )
3231
3232 obj = Object(self.prog, "struct empty [2]", address=0)
3233 self.assertEqual(str(obj), "(struct empty [2]){}")
3234
3235 def test_char_array(self):
3236 segment = bytearray(16)
3237 self.add_memory_segment(segment, virt_addr=0xFFFF0000)
3238
3239 obj = Object(self.prog, "char [4]", address=0xFFFF0000)
3240 segment[:16] = b"hello, world\0\0\0\0"
3241 self.assertEqual(str(obj), '(char [4])"hell"')
3242 self.assertEqual(obj.format_(string=False), "(char [4]){ 104, 101, 108, 108 }")
3243 self.assertEqual(str(obj.read_()), str(obj))
3244 segment[2] = 0
3245 self.assertEqual(str(obj), '(char [4])"he"')
3246 self.assertEqual(str(obj.read_()), str(obj))
3247
3248 self.assertEqual(
3249 str(Object(self.prog, "char [0]", address=0xFFFF0000)), "(char [0]){}"
3250 )
3251 self.assertEqual(
3252 str(Object(self.prog, "char []", address=0xFFFF0000)), "(char []){}"
3253 )
3254
3255 def test_function(self):
3256 obj = Object(
3257 self.prog,
3258 self.prog.function_type(self.prog.void_type(), (), False),
3259 address=0xFFFF0000,
3260 )
3261 self.assertEqual(str(obj), "(void (void))0xffff0000")
3262
3263 def test_absent(self):
3264 for type_ in [
3265 "void",
3266 "int",
3267 "char",
3268 "_Bool",
3269 "double",
3270 self.point_type,
3271 self.option_type,
3272 self.coord_type,
3273 self.color_type,
3274 "size_t",
3275 "void *",
3276 "int [2]",
3277 self.prog.function_type(self.prog.void_type(), ()),
3278 ]:
3279 if isinstance(type_, Type):
3280 type_name = type_.type_name()
3281 else:
3282 type_name = type_
3283 self.assertEqual(str(Object(self.prog, type_)), f"({type_name})<absent>")
3284
3285 def test_optimized_out(self):
3286 self.assertEqual(
3287 str(Object(self.prog, "int", absence_reason=AbsenceReason.OPTIMIZED_OUT)),
3288 "(int)<optimized out>",
3289 )
3290
3291 def test_bigint(self):
3292 segment = bytearray(16)
3293 self.add_memory_segment(segment, virt_addr=0xFFFF0000)
3294
3295 for byteorder in ("little", "big"):
3296 with self.subTest(byteorder=byteorder):
3297 obj = Object(
3298 self.prog,
3299 self.prog.int_type("unsigned __int128", 16, False, byteorder),
3300 address=0xFFFF0000,
3301 )
3302 for value in (
3303 0,
3304 0x100,
3305 0x112233445566778899AABBCCDDEEFF,
3306 0xFEDCBA9876543210,
3307 ):
3308 segment[:] = value.to_bytes(16, byteorder)
3309 self.assertEqual(str(obj), "(unsigned __int128)" + hex(value))
3310
3311 def test_bigint_in_array(self):
3312 segment = bytearray(16 * 3)
3313 self.add_memory_segment(segment, virt_addr=0xFFFF0000)
3314
3315 for byteorder in ("little", "big"):
3316 with self.subTest(byteorder=byteorder):
3317 segment[:16] = (0x112233445566778899AABBCCDDEEFF).to_bytes(
3318 16, byteorder
3319 )
3320 segment[16:32] = (0xDEADBEEF).to_bytes(16, byteorder)
3321 obj = Object(
3322 self.prog,
3323 self.prog.array_type(
3324 self.prog.int_type("unsigned __int128", 16, False, byteorder), 3
3325 ),
3326 address=0xFFFF0000,
3327 )
3328 self.assertEqual(
3329 str(obj),
3330 "(unsigned __int128 [3]){ 0x112233445566778899aabbccddeeff, 0xdeadbeef }",
3331 )
3332 self.assertEqual(
3333 obj.format_(implicit_elements=True),
3334 "(unsigned __int128 [3]){ 0x112233445566778899aabbccddeeff, 0xdeadbeef, 0x0 }",
3335 )
3336
3337 def test_bigint_in_struct(self):
3338 segment = bytearray(16 * 3)
3339 self.add_memory_segment(segment, virt_addr=0xFFFF0000)
3340
3341 for byteorder in ("little", "big"):
3342 with self.subTest(byteorder=byteorder):
3343 segment[:16] = (0x112233445566778899AABBCCDDEEFF).to_bytes(
3344 16, byteorder
3345 )
3346 segment[16:32] = (0xDEADBEEF).to_bytes(16, byteorder)
3347 type = self.prog.int_type("unsigned __int128", 16, False, byteorder)
3348 obj = Object(
3349 self.prog,
3350 self.prog.struct_type(
3351 "foo",
3352 16 * 3,
3353 (
3354 TypeMember(type, "a"),
3355 TypeMember(type, "b", 16 * 8),
3356 TypeMember(type, "c", 16 * 8 * 2),
3357 ),
3358 ),
3359 address=0xFFFF0000,
3360 )
3361 self.assertEqual(
3362 str(obj),
3363 """\
3364 (struct foo){
3365 .a = (unsigned __int128)0x112233445566778899aabbccddeeff,
3366 .b = (unsigned __int128)0xdeadbeef,
3367 .c = (unsigned __int128)0x0,
3368 }""",
3369 )
3370 self.assertEqual(
3371 obj.format_(implicit_members=False),
3372 """\
3373 (struct foo){
3374 .a = (unsigned __int128)0x112233445566778899aabbccddeeff,
3375 .b = (unsigned __int128)0xdeadbeef,
3376 }""",
3377 )
3378
3379 def test_bigint_typedef(self):
3380 self.add_memory_segment(
3381 (0xDEADBEEF).to_bytes(16, "little"), virt_addr=0xFFFF0000
3382 )
3383 type = self.prog.typedef_type(
3384 "__uint128_t", self.prog.int_type("unsigned __int128", 16, False)
3385 )
3386
3387 self.assertEqual(
3388 str(Object(self.prog, type, address=0xFFFF0000)),
3389 "(__uint128_t)0xdeadbeef",
3390 )
3391
3392 self.assertEqual(
3393 str(Object(self.prog, self.prog.array_type(type, 1), address=0xFFFF0000)),
3394 "(__uint128_t [1]){ 0xdeadbeef }",
3395 )
3396
3397 self.assertEqual(
3398 str(
3399 Object(
3400 self.prog,
3401 self.prog.struct_type("foo", 16, (TypeMember(type, "a"),)),
3402 address=0xFFFF0000,
3403 )
3404 ),
3405 """\
3406 (struct foo){
3407 .a = (__uint128_t)0xdeadbeef,
3408 }""",
3409 )
3410
3411 def test_integer_base_struct(self):
3412 self.assertEqual(
3413 Object(self.prog, self.point_type, {"x": 0, "y": 13}).format_(
3414 integer_base=16
3415 ),
3416 """\
3417 (struct point){
3418 .x = (int)0x0,
3419 .y = (int)0xd,
3420 }""",
3421 )
3422
3423 def test_integer_base_array(self):
3424 self.assertEqual(
3425 Object(self.prog, "int [2]", [0, 13]).format_(integer_base=16),
3426 "(int [2]){ 0x0, 0xd }",
3427 )
3428
3429 def test_integer_base_dereference(self):
3430 self.add_memory_segment((13).to_bytes(4, "little"), virt_addr=0xFFFF0000)
3431 obj = Object(self.prog, "int *", value=0xFFFF0000)
3432 self.assertEqual(obj.format_(integer_base=8), "*(int *)0xffff0000 = 015")