pykd

Форк
0
/
typeinfo.py 
414 строк · 19.6 Кб
1
#
2
#
3
#
4

5
import unittest
6
import target
7
import pykd
8

9
typesSourceCode = '''
10

11
typedef struct _LIST_ENTRY {
12
    struct _LIST_ENTRY *Flink;
13
    struct _LIST_ENTRY *Blink;
14
} _LIST_ENTRY;
15
    
16
'''
17

18

19
class TypeInfoTest( unittest.TestCase ):
20

21
    def testCtor( self ):
22
        self.assertEqual( "structTest", pykd.typeInfo( "structTest" ).name() )
23
        self.assertEqual( "structTest", pykd.typeInfo( target.moduleName + "!structTest" ).name() )
24
        self.assertEqual( "structTest", pykd.typeInfo( "g_structTest" ).name() )
25
        self.assertEqual( "structTest", pykd.typeInfo( target.moduleName + "!g_structTest" ).name() )     
26
        self.assertEqual( "Int1B", pykd.typeInfo( "Int1B" ).name() )	
27
 
28
    def testCreateByName( self ):
29
        """ creating typeInfo by the type name """
30
        self.assertEqual( "Int4B*", target.module.type("Int4B*").name() )
31
        self.assertEqual( "structTest", target.module.type( "structTest" ).name() )
32
        self.assertEqual( "structTest**", target.module.type( "structTest**" ).name() )
33
        self.assertEqual( "Int4B[2][3]", target.module.type("Int4B[2][3]").name() )
34
        self.assertEqual( "Int4B(*[4])[2][3]", target.module.type("Int4B(*[4])[2][3]").name() )
35
        self.assertEqual( "Int4B(*)[2][3]", target.module.type("Int4B((*))[2][3]").name() )
36
        self.assertEqual( "Int4B*", pykd.typeInfo("Int4B*").name() )
37

38
    def testCreateBySymbol(self):
39
        """ creating typeInfo by the symbol name """
40
        self.assertEqual( "structTest[2]", target.module.type("g_testArray").name() )
41
        self.assertEqual( "Int4B[2][3]", target.module.type("intMatrix").name() )
42
        self.assertEqual( "structTest*", target.module.type("g_structTestPtr").name() )
43
        self.assertEqual( "structTest**", target.module.type("g_structTestPtrPtr").name() )
44
        self.assertEqual( "Char*[2]", target.module.type("strArray").name() )
45
        self.assertEqual( "Char*(*)[2]", target.module.type("ptrStrArray").name() )
46
        self.assertEqual( "Int4B(*[4])[2][3]", target.module.type("arrIntMatrixPtrs").name() )
47
        self.assertEqual( "Int4B(*)[2][3]", target.module.type("ptrIntMatrix").name() )
48

49
    def testGetField( self ):
50
        """ get field of the complex type """
51
        ti1 = target.module.type( "structTest" )
52
        self.assertTrue( "UInt4B", ti1.m_field0.name() )
53
        self.assertTrue( "m_field0" in ti1 )
54
        self.assertFalse( "not_exist" in ti1) # non-exsisting field
55
        self.assertRaises( AttributeError, lambda t: t.not_exists, ti1) # non-exsisting field
56

57

58
    def testBaseTypes( self ):
59
        self.assertEqual("Int1B", pykd.typeInfo( "Int1B" ).name() )
60
        self.assertEqual("Int2B", pykd.typeInfo( "Int2B" ).name() )
61
        self.assertEqual("Int4B", pykd.typeInfo( "Int4B" ).name() )
62
        self.assertEqual("Int8B", pykd.typeInfo( "Int8B" ).name() )
63
        self.assertEqual("UInt1B", pykd.typeInfo( "UInt1B" ).name() )
64
        self.assertEqual("UInt2B", pykd.typeInfo( "UInt2B" ).name() )
65
        self.assertEqual("UInt4B", pykd.typeInfo( "UInt4B" ).name() )
66
        self.assertEqual("UInt8B", pykd.typeInfo( "UInt8B" ).name() )
67

68
        self.assertEqual("Long", pykd.typeInfo( "Long" ).name() )
69
        self.assertEqual("ULong", pykd.typeInfo( "ULong" ).name() )
70
        self.assertEqual("Bool", pykd.typeInfo( "Bool" ).name() )
71
        self.assertEqual("Char", pykd.typeInfo("Char").name() )
72
        self.assertEqual("WChar", pykd.typeInfo("WChar").name() )
73

74
        self.assertEqual( 1, pykd.typeInfo("Int1B").size() )
75
        self.assertEqual( 1, pykd.typeInfo("UInt1B").size() )
76
        self.assertEqual( 2, pykd.typeInfo("Int2B").size() )
77
        self.assertEqual( 2, pykd.typeInfo("UInt2B").size() )
78
        self.assertEqual( 4, pykd.typeInfo("Int4B").size() )
79
        self.assertEqual( 4, pykd.typeInfo("UInt4B").size() )
80
        self.assertEqual( 8, pykd.typeInfo("Int8B").size() )
81
        self.assertEqual( 8, pykd.typeInfo("UInt8B").size() )
82

83
        self.assertEqual( 4, pykd.typeInfo("Long" ).size() )
84
        self.assertEqual( 4, pykd.typeInfo("ULong" ).size() )
85
        self.assertEqual( 1, pykd.typeInfo("Bool" ).size() )
86
        self.assertEqual( 1, pykd.typeInfo("Char").size() )
87
        self.assertEqual( 2, pykd.typeInfo("WChar").size() )
88

89
        try:
90
            self.assertEqual("Int9B", pykd.typeInfo( "Int9B" ).name() )
91
        except pykd.SymbolException:
92
            pass
93
            
94
    def testBaseTypePtr(self):
95
        self.assertEqual("Int1B*", pykd.typeInfo( "Int1B*" ).name() )
96
        self.assertEqual("Int1B", pykd.typeInfo( "Int1B*" ).deref().name() )
97
        
98
    def testBaseTypeArray(self):
99
        self.assertEqual("Int4B[20]", pykd.typeInfo( "Int4B[20]" ).name() )
100

101
    def testName( self ):
102
        ti1 = target.module.type( "classChild" )
103
        self.assertEqual( "classChild", ti1.name() )
104
        self.assertEqual( "Int4B", ti1.m_childField.name() )
105
        self.assertEqual( "structTest", ti1.m_childField3.name() )
106
        self.assertEqual( "structTest", target.module.type("g_structTest").name() )
107
        
108
    def testVarName( self ):
109
        self.assertEqual( "structTest", target.module.type( "g_structTest").name() )
110
        self.assertRaises( pykd.TypeException, target.module.type, "g_testArray[0]" )
111
        self.assertRaises( pykd.TypeException, target.module.type, "*g_structTestPtr" )
112

113
    def testOffset( self ):
114
        ti1 = target.module.type( "structTest" )
115
        self.assertEqual( 0, ti1.fieldOffset("m_field0") )
116
        self.assertEqual( 4, ti1.fieldOffset("m_field1") )
117
        self.assertEqual( 12, ti1.fieldOffset("m_field2") )
118
        self.assertEqual( 14, ti1.fieldOffset("m_field3") )
119

120
        ti2 = target.module.type( "unionTest" )
121
        self.assertEqual( 0, ti2.fieldOffset("m_value") )
122
        self.assertEqual( 0, ti2.fieldOffset("m_structValue") )
123

124
        ti3 = target.module.type( "structWithNested" )
125
        self.assertEqual( ti3.fieldOffset( "m_unnameStruct"), ti3.fieldOffset( "m_unnameStruct.m_field2" ) )
126

127
    def testSize( self ):
128
        ti1 = target.module.type( "structTest" )
129
        self.assertEqual( 16 + pykd.ptrSize(), ti1.size() )
130
        self.assertEqual( pykd.ptrSize(), target.module.type("structTest**").size() )
131
        self.assertEqual( pykd.sizeof("structTest"), target.module.type("structTest").size() )
132
        self.assertEqual( pykd.sizeof("structTest**"), target.module.type("structTest**").size() )
133
        self.assertEqual( pykd.sizeof("Int1B"), pykd.typeInfo("Int1B").size() )
134

135
    def testBitField( self ):
136
        ti = target.module.type( "g_structWithBits" )
137
        self.assertEqual( 0, ti.fieldOffset("m_bit6_8") )
138
        self.assertEqual( 4, ti.m_bit6_8.size() )
139
        self.assertEqual( "UInt4B:3", ti.m_bit6_8.name() )
140
        self.assertEqual( 3, ti.m_bit6_8.bitWidth() )
141
        self.assertEqual( 6, ti.m_bit6_8.bitOffset() )
142

143
    def testEnum(self):
144
        ti = target.module.type("enumType")
145
        self.assertTrue( hasattr( ti, "TWO" ) )
146
        self.assertEqual( 4, ti.TWO.size() )
147

148
        ti = target.module.type("classChild")
149
        self.assertEqual( "enumType", ti.m_enumField.name() )
150

151
    def testPtr(self):
152
        self.assertEqual( "UInt8B*", target.module.type( "pbigValue" ).name() )
153
        self.assertEqual( "structTest*", target.module.type( "structTest*" ).name() )
154
        self.assertEqual( "UInt2B*", pykd.typeInfo( "UInt2B*" ).name() )
155
        self.assertEqual( "Void*",  target.module.type( "voidPtr" ).name() )
156
        self.assertEqual( "Void*[3]", target.module.type( "voidPtrArray" ).name())
157
        #self.assertEqual( "<function>*", target.module.type( "g_ptrToFunction" ).name())
158
        #self.assertEqual( "<function>*[4]", target.module.type( "g_arrOfPtrToFunc" ).name())
159

160
    def testUnion(self):
161
        ti = target.module.type("unionTest")
162
        self.assertEqual( 0, ti.fieldOffset("m_doubleValue") )
163
        self.assertEqual( 0, ti.fieldOffset("m_value") )
164
        self.assertEqual( 0, ti.fieldOffset("m_structValue") )
165
        self.assertEqual( ti.size(), ti.m_structValue.size() )
166

167
    def testDeref(self):
168
        ti = pykd.typeInfo("Int1B*")
169
        self.assertEqual( "Int1B", ti.deref().name() )
170

171
        ti = target.module.type("structTest*")
172
        self.assertEqual( "structTest", ti.deref().name() )
173

174
        ti =  pykd.typeInfo("structTest[2]")
175
        self.assertEqual( "structTest", ti.deref().name() )
176

177
        ti = target.module.type("classChild")
178
        self.assertRaises( pykd.TypeException, ti.deref )
179

180
    def testNestedStruct( self ):
181
        ti = target.module.type("structWithNested")
182
        self.assertTrue( ti.hasField("m_field"))
183
        self.assertTrue( ti.hasField("m_field3"))
184
        self.assertTrue( ti.hasField("m_unnameStruct"))
185
        self.assertTrue( ti.m_unnameStruct.hasField("m_field2"))
186
        self.assertFalse( ti.hasField( "m_field2" ) )
187
        self.assertFalse( ti.hasField( "m_nestedFiled" ) )
188
        ti = target.module.type("structWithNested::Nested")
189
        self.assertTrue( ti.hasField( "m_nestedFiled" ) )
190

191
    def testPrint(self):
192
        self.assertTrue( str(target.module.type( "ucharVar" ) ) )
193
        self.assertTrue( str(target.module.type( "ushortVar" ) ) )
194
        self.assertTrue( str(target.module.type( "ulongVar" ) ) )
195
        self.assertTrue( str(target.module.type( "ulonglongVar" ) ) )
196
        self.assertTrue( str(target.module.type( "g_structWithBits" ) ) )
197
        self.assertTrue( str(target.module.type( "g_structTest" ) ) )
198
        self.assertTrue( str(target.module.type( "g_structTest1" ) ) )
199
        self.assertTrue( str(target.module.type( "g_testArray" ) ) )
200
        self.assertTrue( str(target.module.type( "g_structTestPtr" ) ) )
201
        self.assertTrue( str(target.module.type( "g_structTestPtrPtr" ) ) )
202
        self.assertTrue( str(target.module.type( "longlongArray" ) ) )
203
        self.assertTrue( str(target.module.type( "intMatrix4" ) ) )
204
        self.assertTrue( str(target.module.type( "ptrIntMatrix" ) ) )
205
        self.assertTrue( str(target.module.type( "g_classChild" ) ) )
206
#        self.assertTrue( str(target.module.type( "g_listHead" ) ) )
207
#        self.assertTrue( str(target.module.type( "g_voidPtr" ) ) )
208
#        self.assertTrue( str(target.module.type( "g_arrOfPtrToFunc" ) ) )
209
#        self.assertTrue( str(target.module.type( "g_unTypedPtrToFunction" ) ) )
210

211
    def testTypedef(self):
212
        self.assertEqual( "structTest", pykd.typeInfo( "g_structTypeDef" ).name() )
213
        self.assertEqual( "structTest", pykd.typeInfo( "structTestTypeDef" ).name() )
214

215
    def testStaticField(self):
216
        ti = pykd.typeInfo( "g_classChild" )
217
        self.assertNotEqual( 0, ti.staticOffset( "m_staticField" ) )
218
        self.assertNotEqual( 0, ti.staticOffset("m_stdstr") )
219
        if not ti.staticOffset("m_staticConst"):
220
            self.assertFalse( "MS DIA bug: https://connect.microsoft.com/VisualStudio/feedback/details/737430" )
221

222
    def testVfnTable(self):
223
        ti = pykd.typeInfo( "g_classChild" )
224
        self.assertTrue( hasattr( ti, "__VFN_table" ) )
225

226
    def testUdtSubscribe(self):
227
        ti = pykd.typeInfo( "g_virtChild" )
228
        self.assertEqual( 6, len(ti) )
229
        for field in ti:
230
             str( field )
231

232
    def testEnumSubscribe(self):
233
        ti = pykd.typeInfo( "enumType" )
234
        self.assertEqual( 3, len(ti) )
235
        self.assertEqual( [ 1, 2, 3], [ int(field) for field in ti ] )
236
        self.assertEqual( [ ( "ONE", 1), ("TWO", 2), ("THREE", 3) ], ti.fields() )
237

238
    def testStructNullSize(self):
239
        ti = target.module.type("structNullSize")
240
        self.assertEqual( 0, len(ti) )
241

242
    def testDerefName(self):
243
        entry = pykd.typedVar("g_listHead").flink
244
        self.assertEqual( "listEntry*", entry.type().name() )
245

246
    def testPtrTo(self):
247
        ti = pykd.typeInfo("UInt8B").ptrTo()
248
        self.assertTrue( "UInt8B*", ti.name() )
249
        self.assertNotEqual( 0, ti.size() )
250

251
    def testArrayOf(self):
252
        ti = pykd.typeInfo("UInt8B").arrayOf(10)
253
        self.assertTrue( "UInt8B[10]", ti.name() )
254
        
255
    def testCompareWithNone(self):
256
        ti = pykd.typeInfo("UInt8B")
257
        self.assertFalse(ti == None)
258
        self.assertTrue(ti != None)
259
        self.assertFalse(not ti)
260
        self.assertTrue(ti)
261

262
    def testFunction(self):
263
        functype = target.module.typedVar( "CdeclFuncPtr" ).type().deref()
264
        self.assertTrue( functype.isFunction() )
265

266
        functype = target.module.typedVar( "g_variadicFuncPtr" ).type().deref()
267
        self.assertTrue( functype.isFunction() )
268

269
    def testFunctionArgs(self):
270
        functype = target.module.typedVar( "CdeclFuncPtr" ).type().deref()
271
        self.assertEqual( [ arg.name() for arg in functype ], ["Int4B", "Float"] )
272

273
        functype = target.module.typedVar( "g_variadicFuncPtr" ).type().deref()
274
        self.assertEqual( [ arg.name() for arg in functype ], ["Int4B", "NoType"] )
275

276
    def testFunctionCallConv(self):
277
        functype = target.module.typedVar( "CdeclFuncPtr" ).type().deref()
278
        self.assertEqual( functype.getCallingConvention(), pykd.callingConvention.NearC )
279

280
    def testFunctionThis(self):
281
        functype = target.module.typedVar( "MethodPtr" ).type().deref()
282
        self.assertEqual( [ arg.name() for arg in functype ], ["FuncTestClass*"] )
283

284
        functype = target.module.typedVar( "CdeclStaticMethodPtr" ).type().deref()
285
        self.assertEqual( [ arg.name() for arg in functype ], [] )
286

287
    def testFunctionName(self):
288
        functype = target.module.typedVar( "CdeclFuncPtr" ).type().deref()
289
        self.assertEqual(functype.name(), "Void(__cdecl)(Int4B, Float)")
290

291
        functype = target.module.typedVar( "MethodPtr" ).type().deref()
292
        self.assertEqual(functype.name(), "Void(__thiscall FuncTestClass::)()")
293

294
        functype = target.module.typedVar( "ArrayOfCdeclFuncPtr" ).type()[0].deref()
295
        self.assertEqual(functype.name(), "Void(__cdecl)(Int4B, Float)")
296

297
        functype = target.module.typedVar( "ArrayOfMethodPtr" ).type()[0].deref()
298
        self.assertEqual(functype.name(), "Void(__thiscall FuncTestClass::)()")
299

300
        functype = target.module.typedVar( "g_variadicFuncPtr" ).type().deref()
301
        self.assertEqual(functype.name(), "Void(__cdecl)(Int4B, ...)")
302

303
    def testFunctionPtrName(self):
304
        funcptrtype = target.module.typedVar( "CdeclFuncPtr" ).type()
305
        self.assertEqual(funcptrtype.name(), "Void(__cdecl*)(Int4B, Float)")
306

307
        functype = target.module.typedVar( "MethodPtr" ).type()
308
        self.assertEqual(functype.name(), "Void(__thiscall FuncTestClass::*)()")
309

310
        funcptrtype = target.module.typedVar( "g_variadicFuncPtr" ).type()
311
        self.assertEqual(funcptrtype.name(), "Void(__cdecl*)(Int4B, ...)")
312

313
    def testFunctionArrName(self):
314
        funcptrtype = target.module.typedVar( "ArrayOfCdeclFuncPtr" ).type()
315
        self.assertEqual(funcptrtype.name(), "Void(__cdecl*[3])(Int4B, Float)")
316

317
        functype = target.module.typedVar( "ArrayOfMethodPtr" ).type()
318
        self.assertEqual(functype.name(), "Void(__thiscall FuncTestClass::*[2])()")
319

320
    def testDir(self):
321
        ti = target.module.type("structTest")
322
        self.assertEqual(5, len(dir(ti)))
323
        self.assertTrue("m_field3" in dir(ti))
324
        self.assertFalse("m_field33" in dir(ti))
325

326
    def testEnumTypes(self):
327
        lst = target.module.enumTypes()
328
        self.assertNotEqual([], lst)
329
        lst = target.module.enumTypes("structTest")
330
        self.assertEqual(["structTest"], lst)
331
        lst = target.module.enumTypes("NonExsistType")
332
        self.assertEqual([],lst)
333

334
    def testArrayOverflow(self):
335
        self.assertRaises(pykd.TypeException, pykd.baseTypes.UInt8B.arrayOf, 0xFFFFFFFFFFFFFFFF)
336

337
    def testMethodCount(self):
338
        self.assertEqual( 14, target.module.type("classChild").getNumberMethods() )
339
        
340
    def testGetMethod(self):
341
        self.assertEqual( "Int4B(__thiscall classChild::)(Int4B)", target.module.type("classChild").method("childMethod").name() )
342
        self.assertEqual( "Int4B(__thiscall classChild::)(Int4B)", target.module.type("classChild").childMethod.name() )
343
        self.assertEqual( "Int4B(__thiscall classChild::)(Int4B)", target.module.type("classChild").method(1).name() )
344

345
    def testMethods(self):
346
        self.assertEqual( 14, len(target.module.type("classChild").methods()))
347

348
    def testGetBaseClass(self):
349
        classChild = target.module.type("classChild")
350
        self.assertEqual( ["classBase1", "classBase2"], [ classChild.baseClass(i).name() for i in range(classChild.getNumberBaseClasses()) ] )
351
        self.assertEqual( ["classBase1", "classBase2"], [ name for name, _, _ in classChild.baseClasses()] )
352

353
    def testGetBaseClassOffset(self):
354
        classChild = target.module.type("classChild")
355
        self.assertEqual( classChild.baseClassOffset(0), classChild.baseClassOffset('classBase1'))
356
        self.assertEqual(classChild.baseClassOffset(1), classChild.baseClassOffset('classBase2'))
357

358
    def  testPdbTypeProvider(self):
359
        pdb = target.module.symfile()
360
        typeProvider = pykd.getTypeInfoProviderFromPdb(pdb)
361
        self.assertEqual("structTest", typeProvider.getTypeByName("structTest").name())
362
        self.assertEqual("structTest", typeProvider.structTest.name())
363
        self.assertEqual(15, len(list(typeProvider.typeIterator("*struct*"))))
364

365
    def testScopeName(self):
366
        self.assertEqual( target.module.name(), pykd.typeInfo( "structTest" ).scopeName() )
367
        self.assertEqual( target.module.name(), pykd.typeInfo( "structWithNested::Nested" ).scopeName() )
368
 
369
    def testContain(self):
370
        ti = target.module.type( "structTest" )
371
        self.assertTrue("m_field1" in ti)
372
        self.assertFalse("NotExist" in ti)
373
        self.assertRaises(Exception, lambda t : 2 in t, ti)
374

375
    def testTemplateType(self):
376
        ti = target.module.type("g_testTemplateTwoTypes")
377
        self.assertTrue(ti.isTemplate)
378
        self.assertEqual(['int', 'TestClassTemplate<int>'], ti.getTemplateArgs() )
379

380
    def testMembers(self):
381
        self.assertEqual(
382
           ['__VFN_table', '__VFN_table', 'm_staticConst', 'm_staticField', 'm_childField',
383
           'm_childField2', 'm_childField3', 'm_enumField'],
384
           [ member[0] for member in pykd.typeInfo( "classChild" ).members() ])
385

386
    def testIsStaticField(self):
387
        ti = pykd.typeInfo("classChild")
388
        self.assertTrue(ti.isStaticField("m_staticField"))
389
        self.assertFalse(ti.isStaticField("m_baseField"))
390

391
    def testIsConstField(self):
392
        ti = pykd.typeInfo("classChild")
393
        self.assertTrue(ti.isStaticField("m_staticConst"))
394
        self.assertFalse(ti.isConstField("m_staticConst"))
395

396
    #def testClangCompile(self):
397
    #    src_code = '#include <windows.h>\r\n'
398
    #    include_path = '"C:/Program Files (x86)/Windows Kits/8.1/Include/um";"C:/Program Files (x86)/Windows Kits/8.1/Include/shared'
399
    #    compile_opts = '-I%s -w' % include_path
400
    #    symbolEnum = pykd.getSymbolProviderFromSource(src_code, compile_opts)
401
    #    for symName, _, symType in symbolEnum :
402
    #        print( symName, symType.name() )
403

404
    def testClangCompile(self):
405
        typesProvider = pykd.getTypeInfoProviderFromSource(typesSourceCode)
406
        ti = typesProvider.getTypeByName('_LIST_ENTRY')
407
        self.assertEqual (ti.name(), '_LIST_ENTRY')
408
        self.assertEqual (ti.Flink.name(), '_LIST_ENTRY*')
409
        self.assertEqual (ti.Flink.deref().name(), '_LIST_ENTRY')
410
        self.assertEqual (ti.Blink.name(), '_LIST_ENTRY*')
411
        self.assertEqual (ti.Blink.deref().name(), '_LIST_ENTRY')
412

413
        (typesProvider, outMsg) = pykd.getTypeInfoProviderFromSourceEx('1; abc; ', "-w")
414
        self.assertTrue ('abc' in outMsg)
415

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.