summaryrefslogtreecommitdiff
path: root/tests/mangling/itanium.py
blob: a04c590ca99fc29dc95af89ea77399a088dfd08d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/python3-dbg
# -*- coding: utf-8 -*-


# Tests validant le décodage des types et des routines pour le format Itanium


from chrysacase import ChrysalideTestCase
from pychrysalide.mangling import ItaniumDemangler


class TestItaniumMangling(ChrysalideTestCase):
    """TestCase for pychrysalide.mangling.ItaniumDemangler."""

    def check_demangling(self, got, expected):
        """Check a given demangling result."""

        self.assertEqual(str(got), expected)


    def testItaniumTypeMangling(self):
        """Check Itanium type demangling with specifications samples."""

        # https://itanium-cxx-abi.github.io/cxx-abi/abi-examples.html#mangling

        demangler = ItaniumDemangler()

        demangled = demangler.decode_type('_ZN3FooIA4_iE3barE')
        self.check_demangling(demangled, 'Foo<int[4]>::bar')

        demangled = demangler.decode_type('_ZN1N1fE')
        self.check_demangling(demangled, 'N::f')

        demangled = demangler.decode_type('_ZN5Arena5levelE')
        self.check_demangling(demangled, 'Arena::level')

        demangled = demangler.decode_type('_ZN5StackIiiE5levelE')
        self.check_demangling(demangled, 'Stack<int, int>::level')


    def testItaniumRoutineMangling(self):
        """Check Itanium routine demangling with specifications samples."""

        # https://itanium-cxx-abi.github.io/cxx-abi/abi-examples.html#mangling

        demangler = ItaniumDemangler()

        demangled = demangler.decode_routine('_Z1fv')
        self.check_demangling(demangled, '??? f(void)')

        demangled = demangler.decode_routine('_Z1fi')
        self.check_demangling(demangled, '??? f(int)')

        demangled = demangler.decode_routine('_Z3foo3bar')
        self.check_demangling(demangled, '??? foo(bar)')

        demangled = demangler.decode_routine('_Zrm1XS_')
        self.check_demangling(demangled, '??? operator%(X, X)')

        demangled = demangler.decode_routine('_ZplR1XS0_')
        self.check_demangling(demangled, '??? operator+(X &, X &)')

        demangled = demangler.decode_routine('_ZlsRK1XS1_')
        self.check_demangling(demangled, '??? operator<<(const X &, const X &)')

        demangled = demangler.decode_routine('_Z1fIiEvi')
        self.check_demangling(demangled, 'void f<int>(int)')

        demangled = demangler.decode_routine('_Z5firstI3DuoEvS0_')
        self.check_demangling(demangled, 'void first<Duo>(Duo)')

        demangled = demangler.decode_routine('_Z5firstI3DuoEvT_')
        self.check_demangling(demangled, 'void first<Duo>(Duo)')

        demangled = demangler.decode_routine('_Z3fooIiPFidEiEvv')
        self.check_demangling(demangled, 'void foo<int, int (*) (double), int>(void)')

        demangled = demangler.decode_routine('_ZN6System5Sound4beepEv')
        self.check_demangling(demangled, '??? System::Sound::beep(void)')

        demangled = demangler.decode_routine('_Z1fI1XEvPVN1AIT_E1TE')
        self.check_demangling(demangled, 'void f<X>(volatile A<X>::T *)')

        demangled = demangler.decode_routine('_ZngILi42EEvN1AIXplT_Li2EEE1TE')
        self.check_demangling(demangled, 'void operator-<42>(A<42+2>::T)')

        demangled = demangler.decode_routine('_Z4makeI7FactoryiET_IT0_Ev')
        self.check_demangling(demangled, 'Factory<int> make<Factory, int>(void)')

        demangled = demangler.decode_routine('_Z3foo5Hello5WorldS0_S_')
        self.check_demangling(demangled, '??? foo(Hello, World, World, Hello)')

        demangled = demangler.decode_routine('_ZlsRSoRKSs')
        self.check_demangling(demangled, '??? operator<<(std::ostream &, const std::string &)')


    def testItaniumRoutineManglingInside(self):
        """Check Itanium routine demangling examples within the specifications."""

        # http://refspecs.linuxbase.org/cxxabi-1.83.html#linkage

        demangler = ItaniumDemangler()

        demangled = demangler.decode_routine('_Z1fM1AKFvvE')
        self.check_demangling(demangled, '??? f(const void (A::*) (void))')

        demangled = demangler.decode_routine('_Z1fPFvvEM1SFvvE')
        self.check_demangling(demangled, '??? f(void (*) (void), void (S::*) (void))')

        demangled = demangler.decode_routine('_ZN1N1TIiiE2mfES0_IddE')
        self.check_demangling(demangled, '??? N::T<int, int>::mf(N::T<double, double>)')


    def testItaniumRoutineManglingExtra(self):
        """Check extra Itanium routine demangling cases."""

        # http://refspecs.linuxbase.org/cxxabi-1.83.html#linkage

        demangler = ItaniumDemangler()

        # A la lecture, il s'agit d'une référence sur un tableau, et non
        # d'un tableau de références.
        demangled = demangler.decode_routine('_Z3fooILi2EEvRAplT_Li1E_i')
        self.check_demangling(demangled, 'void foo<2>(int[2+1] &)')


    def testAFL(self):
        """Tests from AFL."""

        demangler = ItaniumDemangler()

        demangled = demangler.decode_routine('_Z4makeI7FactoryiET_IT')
        self.assertIsNone(demangled)

        demangled = demangler.decode_routine('_Z4makeN7FactoryiET_IT0_Ev')
        self.assertIsNone(demangled)

        demangled = demangler.decode_routine('_Z4makeI7FactoryiET_I4makeIMGaptoryiET_T0_Ev')
        self.assertIsNone(demangled)

        demangled = demangler.decode_routine('_Z4maktoryiaS_ILNd')
        self.assertIsNone(demangled)

        # ?!
        demangled = demangler.decode_routine('_Z4makeMVFactoryiES_')
        self.assertIsNotNone(demangled)


    def testOldRealWorldDemanglings(self):
        """Check real world demangling cases from previous code."""

        demangler = ItaniumDemangler()

        demangled = demangler.decode_routine('_ZNSt6vectorItSaItEE6insertEN9__gnu_cxx17__normal_iteratorIPtS1_EERKt')
        self.check_demangling(demangled, '??? std::vector<unsigned short, std::allocator<unsigned short>>::insert(__gnu_cxx::__normal_iterator<unsigned short *, std::vector<unsigned short, std::allocator<unsigned short>>>, const unsigned short &)')

        demangled = demangler.decode_routine('_ZSt26__uninitialized_fill_n_auxIP15CProfStringListiS0_ET_S2_T0_RKT1_12__false_type')
        self.check_demangling(demangled, 'CProfStringList *std::__uninitialized_fill_n_aux<CProfStringList *, int, CProfStringList>(CProfStringList *, int, const CProfStringList &, __false_type)')


        demangled = demangler.decode_routine('_ZN21IUDFSettingsValidator15IdNotIllegalStdEN13UDFParameters12UDF_STANDARDES1_')
        self.check_demangling(demangled, '??? IUDFSettingsValidator::IdNotIllegalStd(UDFParameters::UDF_STANDARD, UDFParameters::UDF_STANDARD)')

        demangled = demangler.decode_routine('_ZNSbI26NeroMediumFeatureSpecifierSt11char_traitsIS_ESaIS_EE4_Rep10_M_destroyERKS2_')
        self.check_demangling(demangled, '??? std::basic_string<NeroMediumFeatureSpecifier, std::char_traits<NeroMediumFeatureSpecifier>, std::allocator<NeroMediumFeatureSpecifier>>::_Rep::_M_destroy(const std::allocator<NeroMediumFeatureSpecifier> &)')

        demangled = demangler.decode_routine('_ZNSt6vectorIlSaIlEE6insertEN9__gnu_cxx17__normal_iteratorIPlS1_EERKl')
        self.check_demangling(demangled, '??? std::vector<long, std::allocator<long>>::insert(__gnu_cxx::__normal_iterator<long *, std::vector<long, std::allocator<long>>>, const long &)')

        demangled = demangler.decode_routine('_ZSt22__merge_without_bufferIN9__gnu_cxx17__normal_iteratorIP15CProfStringListSt6vectorIS2_SaIS2_EEEEiEvT_S8_S8_T0_S9_')
        self.check_demangling(demangled, 'void std::__merge_without_buffer<__gnu_cxx::__normal_iterator<CProfStringList *, std::vector<CProfStringList, std::allocator<CProfStringList>>>, int>(__gnu_cxx::__normal_iterator<CProfStringList *, std::vector<CProfStringList, std::allocator<CProfStringList>>>, __gnu_cxx::__normal_iterator<CProfStringList *, std::vector<CProfStringList, std::allocator<CProfStringList>>>, __gnu_cxx::__normal_iterator<CProfStringList *, std::vector<CProfStringList, std::allocator<CProfStringList>>>, int, int)')

        demangled = demangler.decode_routine('_ZSt11__push_heapIN9__gnu_cxx17__normal_iteratorIP8DRIVE_IDSt6vectorIS2_SaIS2_EEEEiS2_EvT_T0_S9_T1_')
        self.check_demangling(demangled, 'void std::__push_heap<__gnu_cxx::__normal_iterator<DRIVE_ID *, std::vector<DRIVE_ID, std::allocator<DRIVE_ID>>>, int, DRIVE_ID>(__gnu_cxx::__normal_iterator<DRIVE_ID *, std::vector<DRIVE_ID, std::allocator<DRIVE_ID>>>, int, int, DRIVE_ID)')

        demangled = demangler.decode_routine('_ZSt12partial_sortIN9__gnu_cxx17__normal_iteratorIP28CPR_MAI_ADPTY_SectorSequenceSt6vectorIS2_SaIS2_EEEEEvT_S8_S8_')
        self.check_demangling(demangled, 'void std::partial_sort<__gnu_cxx::__normal_iterator<CPR_MAI_ADPTY_SectorSequence *, std::vector<CPR_MAI_ADPTY_SectorSequence, std::allocator<CPR_MAI_ADPTY_SectorSequence>>>>(__gnu_cxx::__normal_iterator<CPR_MAI_ADPTY_SectorSequence *, std::vector<CPR_MAI_ADPTY_SectorSequence, std::allocator<CPR_MAI_ADPTY_SectorSequence>>>, __gnu_cxx::__normal_iterator<CPR_MAI_ADPTY_SectorSequence *, std::vector<CPR_MAI_ADPTY_SectorSequence, std::allocator<CPR_MAI_ADPTY_SectorSequence>>>, __gnu_cxx::__normal_iterator<CPR_MAI_ADPTY_SectorSequence *, std::vector<CPR_MAI_ADPTY_SectorSequence, std::allocator<CPR_MAI_ADPTY_SectorSequence>>>)')