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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
|
"""
Regression test for objc
* ast parsed C to C Miasm expression
* C Miasm expression to native expression
* Miasm expression to type
"""
from __future__ import print_function
from future.utils import viewitems
from past.builtins import cmp
from builtins import str
from miasm.expression.expression import ExprInt, ExprId, ExprMem
from miasm.expression.simplifications import expr_simp
from miasm.core.objc import parse_access
from miasm.core.objc import ast_get_c_access_expr
from miasm.core.objc import ExprCToExpr, ExprToAccessC, CHandler
from miasm.core.ctypesmngr import CTypeStruct, CTypeUnion, CAstTypes, CTypePtr, CTypeId
from miasm.core.objc import CTypesManagerNotPacked
from miasm.arch.x86.ctype import CTypeAMD64_unk
text_1 = """
# 1 "test.h"
typedef enum {
TMP0,
TMP1,
TMP2,
TMP3,
} MyEnum;
typedef struct mini_st {
int x;
int y;
short z;
} Mini;
typedef union mini_un {
int x;
int y;
short z;
} MiniUnion;
typedef unsigned char block[8];
typedef struct mini_st mini_st_struct;
typedef mini_st_struct mini_st_struct2;
typedef struct test_st {
int a;
int b;
int** ptr;
short tab1[4*sizeof(int)];
short* tab2[2*2+4*4-16/4];
int* xptr;
int tab3[0x10][0x20];
Mini f_mini;
Mini minitab[0x10];
Mini *minitabptr[0x10];
int (*(tab4[0x20]))[0x10];
MyEnum myenum;
block chunk;
union testU{
int myint;
char mychars[4];
} myunion;
union testV{
int myint;
char mychars[4];
struct tutu {
int a;
unsigned int b;
} mystruct_x;
} myunion_x;
union testW{
union testX{
int a;
unsigned int b;
char c;
} u0;
union testX{
int a;
char b;
} u1;
} myunion_y;
union {
int a1;
};
struct {
int a2;
};
Mini (*(tab5[0x20]))[0x10];
int *tab6[4][4][4][4];
} Test;
typedef int (*func)(int, char);
typedef func ( *(xxx[5]) )[4];
typedef union char_int_st {
char a;
int b;
} Char_int;
typedef int array1[4];
typedef int array2[4];
typedef unsigned int array3[4];
typedef Char_int array4[4];
typedef char dummy[((sizeof(char)<<3) >> 1)*4/2 - 1 + 2];
struct recurse {
struct recurse* next;
int a;
};
int strlen(const char *s);
"""
text_2 = """
struct test_context {
int a;
struct test_st test;
int b;
};
"""
base_types = CTypeAMD64_unk()
types_ast = CAstTypes()
# Add C types definition
types_ast.add_c_decl(text_1)
types_ast.add_c_decl(text_2)
types_mngr = CTypesManagerNotPacked(types_ast, base_types)
for type_id, type_desc in viewitems(types_mngr.types_ast._types):
print(type_id)
obj = types_mngr.get_objc(type_id)
print(obj)
print(repr(obj))
types_mngr.check_objc(obj)
for type_id, type_desc in viewitems(types_mngr.types_ast._typedefs):
print(type_id)
obj = types_mngr.get_objc(type_id)
print(obj)
print(repr(obj))
types_mngr.check_objc(obj)
void_ptr = types_mngr.void_ptr
obj_dummy = types_mngr.get_objc(CTypeId("dummy"))
obj_int = types_mngr.get_objc(CTypeId("int"))
obj_uint = types_mngr.get_objc(CTypeId("unsigned", "int"))
obj_long = types_mngr.get_objc(CTypeId("long"))
obj_array1 = types_mngr.get_objc(CTypeId("array1"))
obj_array2 = types_mngr.get_objc(CTypeId("array2"))
obj_array3 = types_mngr.get_objc(CTypeId("array3"))
obj_array4 = types_mngr.get_objc(CTypeId("array4"))
obj_charint = types_mngr.get_objc(CTypeUnion("char_int"))
assert cmp(obj_int, obj_uint) != 0
assert cmp(obj_int, obj_long) != 0
assert cmp(obj_array1, obj_array1) == 0
assert cmp(obj_array1, obj_array2) == 0
assert cmp(obj_array1, obj_array3) != 0
assert cmp(obj_array1, obj_array4) != 0
assert cmp(obj_charint, obj_charint) == 0
assert cmp(obj_charint, obj_uint) != 0
obj_test = types_mngr.get_objc(CTypePtr(CTypeId("Test")))
ptr_test = ExprId("ptr_Test", 64)
obj_recurse = types_mngr.get_objc(CTypePtr(CTypeStruct("recurse")))
# Test cmp same recursive object
obj_recurse_bis = types_mngr.get_objc(CTypePtr(CTypeStruct("recurse")))
assert cmp(obj_recurse, obj_recurse_bis) == 0
set_test = set([obj_recurse, obj_recurse_bis])
assert len(set_test) == 1
ptr_recurse = ExprId("ptr_recurse", 64)
obj_test_st = types_mngr.get_objc(CTypeStruct("test_st"))
print(repr(obj_test_st))
obj_test_context = types_mngr.get_objc(CTypeStruct("test_context"))
print(repr(obj_test_context))
assert obj_test_context.size > obj_test_st.size
assert cmp(obj_test_st, obj_recurse) != 0
expr_types = {ptr_test: set([obj_test]),
ptr_recurse: set([obj_recurse])}
c_context = {ptr_test.name: obj_test,
ptr_recurse.name: obj_recurse}
tests = [
(
ExprMem(ptr_test, 32),
[("int", "(ptr_Test)->a")]
),
(
ptr_test,
[('struct test_st *', "ptr_Test")]
),
(
ExprMem(ptr_test + ExprInt(0, 64), 32),
[("int", "(ptr_Test)->a")]
),
(
ExprMem(ptr_test + ExprInt(8, 64), 64),
[("int **", "(ptr_Test)->ptr")]
),
(
ExprMem(ptr_test + ExprInt(8, 64), 64) + ExprInt(8 * 3, 64),
[("int **", "&(((ptr_Test)->ptr)[3])")]
),
(
ExprMem(ExprMem(ptr_test + ExprInt(8, 64), 64) +
ExprInt(8 * 3, 64), 64),
[("int *", "((ptr_Test)->ptr)[3]")]
),
(
ExprMem(
ExprMem(
ExprMem(
ptr_test + ExprInt(8, 64),
64) + ExprInt(8 * 3, 64),
64) + ExprInt(4 * 9, 64),
32),
[("int", "(((ptr_Test)->ptr)[3])[9]")]
),
(
ptr_test + ExprInt(0x10, 64),
[("short [16]", "(ptr_Test)->tab1")]
),
(
ptr_test + ExprInt(0x12, 64),
[("short *", "&(((ptr_Test)->tab1)[1])")]
),
(
ExprMem(ptr_test + ExprInt(0x10, 64), 16),
[("short", "*((ptr_Test)->tab1)")]
),
(
ExprMem(ptr_test + ExprInt(0x10 + 2 * 3, 64), 16),
[("short", "((ptr_Test)->tab1)[3]")]
),
(
ExprMem(ptr_test + ExprInt(0xb8 + 4, 64), 32),
[("int", "(((ptr_Test)->tab3)[0])[1]")]
),
(
ExprMem(ptr_test + ExprInt(0xb8 + 32 * 4 * 3 + 4 * 7, 64), 32),
[("int", "(((ptr_Test)->tab3)[3])[7]")]
),
(
ptr_test + ExprInt(0xb8 + 4, 64),
[("int *", "&((((ptr_Test)->tab3)[0])[1])")]
),
# struct of struct
(
ptr_test + ExprInt(0x8b8, 64),
[("struct mini_st *", '&((ptr_Test)->f_mini)')]
),
(
ptr_test + ExprInt(0x8bc, 64),
[("int *", "&(((ptr_Test)->f_mini).y)")]
),
(
ExprMem(ptr_test + ExprInt(0x8bc, 64), 32),
[("int", "((ptr_Test)->f_mini).y")]
),
# struct of array of struct
(
ptr_test + ExprInt(0x8c4, 64),
[('struct mini_st [16]', '(ptr_Test)->minitab')]
),
(
ptr_test + ExprInt(0x8c4 + 3 * 4, 64),
[('struct mini_st *', '&(((ptr_Test)->minitab)[1])')]
),
(
ExprMem(ptr_test + ExprInt(0x8c4, 64), 32),
[("int", "((ptr_Test)->minitab)->x")]
),
(
ExprMem(ptr_test + ExprInt(0x8c4 + 12 * 4, 64), 32),
[("int", "(((ptr_Test)->minitab)[4]).x")]
),
(
ExprMem(ptr_test + ExprInt(0x8c4 + 4, 64), 32),
[("int", "((ptr_Test)->minitab)->y")]
),
(
ExprMem(ptr_test + ExprInt(0x8c4 + 12 * 4 + 4, 64), 32),
[("int", "(((ptr_Test)->minitab)[4]).y")]
),
# struct of array of ptr of struct
(
ExprMem(ptr_test + ExprInt(0x988 + 8 * 4, 64), 64),
[('struct mini_st *', "((ptr_Test)->minitabptr)[4]")]
),
(
ExprMem(
(ExprMem(ptr_test + ExprInt(0x988 + 8 * 4, 64), 64) +
ExprInt(8, 64)),
16),
[("short", "(((ptr_Test)->minitabptr)[4])->z")]
),
# tab4
(
ptr_test + ExprInt(0xa08, 64),
[("int (*[32])[16]", "(ptr_Test)->tab4")]
),
(
ExprMem(ptr_test + ExprInt(0xa08 + 0x8 * 2, 64), 64),
[("int (*)[16]", "((ptr_Test)->tab4)[2]")]
),
(
ExprMem(ExprMem(ptr_test + ExprInt(0xa08 + 0x8 * 2, 64), 64), 64),
[("int [16]", "*(((ptr_Test)->tab4)[2])")]
),
(
ExprMem(ExprMem(ptr_test + ExprInt(0xa08 + 0x8 * 2, 64), 64), 64) + ExprInt(4 * 5, 64),
[("int *", "&((*(((ptr_Test)->tab4)[2]))[5])")]
),
# enum
(
ExprMem(ptr_test + ExprInt(2824, 64), 32),
[("int", "(ptr_Test)->myenum")]
),
# typedef array
(
ExprMem(ptr_test + ExprInt(2828 + 1, 64), 8),
[("uchar", "((ptr_Test)->chunk)[1]")]
),
# union
(
ptr_test + ExprInt(2836, 64),
[("union testU *", '&((ptr_Test)->myunion)')]
),
(
ExprMem(ptr_test + ExprInt(2836, 64), 8),
[("char", "*(((ptr_Test)->myunion).mychars)")]
),
(
ExprMem(ptr_test + ExprInt(2836 + 1, 64), 8),
[("char", "(((ptr_Test)->myunion).mychars)[1]")]
),
(
ExprMem(ptr_test + ExprInt(2836, 64), 32),
[("int", "((ptr_Test)->myunion).myint")]
),
# union struct
(
ExprMem(ptr_test + ExprInt(2840, 64), 8),
[("char", "*(((ptr_Test)->myunion_x).mychars)")]
),
(
ExprMem(ptr_test + ExprInt(2840 + 1, 64), 8),
[("char", "(((ptr_Test)->myunion_x).mychars)[1]")]
),
(
ExprMem(ptr_test + ExprInt(2840, 64), 32),
[("int", "((ptr_Test)->myunion_x).myint"),
("int", "(((ptr_Test)->myunion_x).mystruct_x).a")]
),
(
ptr_test + ExprInt(2840, 64),
[('union testV *', '&((ptr_Test)->myunion_x)')]
),
# union union
(
ptr_test + ExprInt(2848, 64),
[('union testW *', '&((ptr_Test)->myunion_y)')]
),
(
ExprMem(ptr_test + ExprInt(2848, 64), 32),
[('int', '(((ptr_Test)->myunion_y).u0).a'),
('uint', '(((ptr_Test)->myunion_y).u0).b'),
('int', '(((ptr_Test)->myunion_y).u1).a')]
),
# recurse
(
ptr_recurse,
[('struct recurse *', 'ptr_recurse')]
),
(
ptr_recurse + ExprInt(8, 64),
[('int *', '&((ptr_recurse)->a)')]
),
(
ExprMem(ptr_recurse, 64),
[('struct recurse *', '(ptr_recurse)->next')]
),
(
ExprMem(ExprMem(ptr_recurse, 64), 64),
[('struct recurse *', '((ptr_recurse)->next)->next')]
),
(
ExprMem(ExprMem(ExprMem(ptr_recurse, 64), 64) + ExprInt(8, 64), 32),
[('int', '(((ptr_recurse)->next)->next)->a')]
),
# tab5
(
ptr_test + ExprInt(0xb30, 64),
[("struct mini_st (*[32])[16]", "(ptr_Test)->tab5")]
),
(
ExprMem(ptr_test + ExprInt(0xb30 + 0x8 * 2, 64), 64),
[("struct mini_st (*)[16]", "((ptr_Test)->tab5)[2]")]
),
(
ExprMem(ExprMem(ptr_test + ExprInt(0xb30 + 0x8 * 2, 64), 64), 64),
[("struct mini_st [16]", "*(((ptr_Test)->tab5)[2])")]
),
(
ExprMem(ExprMem(ptr_test + ExprInt(0xb30 + 0x8 * 2, 64), 64), 64) + ExprInt(12*3 + 8, 64),
[("short *", "&(((*(((ptr_Test)->tab5)[2]))[3]).z)")]
),
(
ExprMem(ExprMem(ExprMem(ptr_test + ExprInt(0xb30 + 0x8 * 2, 64), 64), 64) +
ExprInt(12*3 + 8, 64), 16),
[("short", "((*(((ptr_Test)->tab5)[2]))[3]).z")]
),
# tab 6
(
ExprMem(ptr_test + ExprInt(0xc30 + ((((3) * 4 + 2)*4 + 0)*4 + 1)*8, 64), 64),
[("int *", "(((((ptr_Test)->tab6)[3])[2])[0])[1]")]
),
(
ExprMem(ExprMem(ptr_test + ExprInt(0xc30 + ((((3) * 4 + 2)*4 + 0)*4 + 1)*8, 64), 64), 32),
[("int", "*((((((ptr_Test)->tab6)[3])[2])[0])[1])")]
),
]
mychandler = CHandler(types_mngr, expr_types=expr_types, C_types=c_context)
exprc2expr = ExprCToExpr(expr_types, types_mngr)
mychandler.updt_expr_types(expr_types)
for (expr, result) in tests:
print("*" * 80)
print("Native expr:", expr)
result = set(result)
expr_c = mychandler.expr_to_c(expr)
types = mychandler.expr_to_types(expr)
target_type = mychandler.expr_to_types(expr)
access_c_gen = ExprToAccessC(expr_types, types_mngr)
computed = set()
for c_str, ctype in mychandler.expr_to_c_and_types(expr):
print(c_str, ctype)
computed.add((str(ctype), c_str))
assert computed == result
for out_type, out_str in computed:
parsed_expr = mychandler.c_to_expr(out_str)
parsed_type = mychandler.c_to_type(out_str)
print("Access expr:", parsed_expr)
print("Access type:", parsed_type)
ast = parse_access(out_str)
access_c = ast_get_c_access_expr(ast, c_context)
print("Generated access:", access_c)
parsed_expr_bis, parsed_type_bis = mychandler.exprc2expr.get_expr(access_c, c_context)
assert parsed_expr_bis is not None
assert parsed_expr == parsed_expr_bis
assert parsed_type == parsed_type_bis
parsed_expr_3, parsed_type_3 = mychandler.c_to_expr_and_type(out_str)
assert parsed_expr_3 is not None
assert parsed_expr == parsed_expr_3
assert parsed_type == parsed_type_3
expr_new1 = expr_simp(parsed_expr)
expr_new2 = expr_simp(expr)
print("\t", expr_new1)
assert expr_new1 == expr_new2
|