diff options
| author | Camille Mougey <commial@gmail.com> | 2017-08-09 09:12:40 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-08-09 09:12:40 +0200 |
| commit | 0303602135a98e3bd91dda245f9344f06b293da9 (patch) | |
| tree | f3cbbb72e57bad1a7a3c22f474f6f13e59cd03ad /test | |
| parent | c8f218b0427ae7d42313d11bf5a94b009d4f7f25 (diff) | |
| parent | d2756cb85f4b06280fb38eb32f6322ffbd0e17ca (diff) | |
| download | miasm-0303602135a98e3bd91dda245f9344f06b293da9.tar.gz miasm-0303602135a98e3bd91dda245f9344f06b293da9.zip | |
Merge pull request #596 from serpilliere/fix_ctype
Fix ctype
Diffstat (limited to 'test')
22 files changed, 943 insertions, 2 deletions
diff --git a/test/expr_type/test_chandler.py b/test/expr_type/test_chandler.py new file mode 100644 index 00000000..8070841d --- /dev/null +++ b/test/expr_type/test_chandler.py @@ -0,0 +1,550 @@ +""" +Regression test for objc +* ast parsed C to C Miasm expression +* C Miasm expression to native expression +* Miasm expression to type +""" + +from miasm2.expression.expression import ExprInt, ExprId, ExprMem +from miasm2.expression.simplifications import expr_simp + +from miasm2.core.objc import parse_access +from miasm2.core.objc import ast_get_c_access_expr +from miasm2.core.objc import ExprCToExpr, ExprToAccessC, CHandler + + +from miasm2.core.ctypesmngr import CTypeStruct, CTypeUnion, CAstTypes, CTypePtr, CTypeId +from miasm2.core.objc import CTypesManagerNotPacked + +from miasm2.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 types_mngr.types_ast._types.iteritems(): + 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 types_mngr.types_ast._typedefs.iteritems(): + 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) +exprc2expr = ExprCToExpr(expr_types, types_mngr) +mychandler.updt_expr_types(expr_types) + + +for (expr, result) in tests[4:]: + 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, c_context) + parsed_type = mychandler.c_to_type(out_str, c_context) + 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 + + expr_new1 = expr_simp(parsed_expr) + expr_new2 = expr_simp(expr) + print "\t", expr_new1 + assert expr_new1 == expr_new2 diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_1.S b/test/samples/x86_32/cst_propag/x86_32_sc_1.S new file mode 100644 index 00000000..0fe12e04 --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_1.S @@ -0,0 +1,21 @@ +main: + PUSH EBP + MOV EBP, ESP + MOV ECX, 1 + MOV EDX, 2 + LEA ECX, DWORD PTR [ECX+0x4] + LEA EBX, DWORD PTR [ECX+0x1] + CMP CL, 0x1 + JZ test1 + LEA EBX, DWORD PTR [EBX-1] + JMP end +test1: + LEA EBX, DWORD PTR [EBX-1] +end: + MOV EAX, EBX + LEA EAX, DWORD PTR [EAX + EDX] + MOV EDX, DWORD PTR [EBP+0xC] + LEA EAX, DWORD PTR [EAX + EDX] + MOV ESP, EBP + POP EBP + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_10.S b/test/samples/x86_32/cst_propag/x86_32_sc_10.S new file mode 100644 index 00000000..84eae85b --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_10.S @@ -0,0 +1,17 @@ +main: + PUSH EBP + MOV EBP, ESP + MOV ECX, DWORD PTR [ESP+0x8] + INC EBX + CMP CL, 0x1 + JZ test1 + MOV EAX, 8 + JMP end +test1: + MOV EAX, 4 +end: + LEA EBX, DWORD PTR [EAX+EBX] + MOV EAX, EBX + MOV ESP, EBP + POP EBP + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_11.S b/test/samples/x86_32/cst_propag/x86_32_sc_11.S new file mode 100644 index 00000000..272c76da --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_11.S @@ -0,0 +1,11 @@ +main: + MOV ECX, 10 +loop1: + DEC ECX + JNZ less + JMP goon +less: + DEC ECX +goon: + JNZ loop1 + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_12.S b/test/samples/x86_32/cst_propag/x86_32_sc_12.S new file mode 100644 index 00000000..3420b882 --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_12.S @@ -0,0 +1,12 @@ +main: + MOV EBX, 1 + MOV ECX, 2 + CMP EDX, 3 + JNZ test1 + ADD EBX, 1 + JMP goon +test1: + ADD ECX, 1 +goon: + LEA EAX, DWORD PTR [EBX+ECX] + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_13.S b/test/samples/x86_32/cst_propag/x86_32_sc_13.S new file mode 100644 index 00000000..08b9a891 --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_13.S @@ -0,0 +1,7 @@ +main: + MOV ECX, 10 +loop: + DEC ECX + JNZ loop + MOV EAX, ECX + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_14.S b/test/samples/x86_32/cst_propag/x86_32_sc_14.S new file mode 100644 index 00000000..17cad5d7 --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_14.S @@ -0,0 +1,10 @@ +main: + MOV ECX, 10 + MOV EDX, 10 +loop: + INC EDX + DEC EDX + DEC ECX + JNZ loop + MOV EAX, EDX + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_15.S b/test/samples/x86_32/cst_propag/x86_32_sc_15.S new file mode 100644 index 00000000..03a3c121 --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_15.S @@ -0,0 +1,16 @@ +main: + ADD EDI, 1 + MOV ECX, 10 +loop1: + MOV EDX, 10 + INC EDI +loop2: + ADD EDI, 2 + SUB EDI, 2 + SUB EDX, 1 + JNZ loop2 + DEC EDI + SUB ECX, 1 + JNZ loop1 + MOV EAX, EDI + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_16.S b/test/samples/x86_32/cst_propag/x86_32_sc_16.S new file mode 100644 index 00000000..d54e2657 --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_16.S @@ -0,0 +1,17 @@ +main: + MOV EBX, 1 + ADD EDI, 1 + MOV ECX, 10 +loop1: + MOV EDX, 10 + INC EDI +loop2: + LEA EDI, DWORD PTR [EDI+EBX] + LEA EDI, DWORD PTR [EDI-1] + SUB EDX, 1 + JNZ loop2 + DEC EDI + SUB ECX, 1 + JNZ loop1 + MOV EAX, EDI + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_17.S b/test/samples/x86_32/cst_propag/x86_32_sc_17.S new file mode 100644 index 00000000..576c02e2 --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_17.S @@ -0,0 +1,8 @@ +main: + MOV EBX, 1 + ADD EDI, 1 + SUB EBX, EDI + SUB EDI, 1 + ADD EBX, EDI + MOV EAX, EBX + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_18.S b/test/samples/x86_32/cst_propag/x86_32_sc_18.S new file mode 100644 index 00000000..7d29abdb --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_18.S @@ -0,0 +1,33 @@ +main: + PUSH EBP + MOV EBP, ESP + MOV EAX, DWORD PTR [EBP+0x8] + MOV EBX, DWORD PTR [EBP+0xC] + ADD EAX, EBX + TEST EAX, EAX + JNZ test + PUSH 1 + PUSH 2 + PUSH DWORD PTR [EBP+0x10] + CALL func1 + ADD ESP, 0xC + JMP goon +test: + MOV ECX, 10 +loop: + PUSH 1 + PUSH 2 + CALL func2 + ADD ESP, 0x8 + DEC ECX + JNZ loop +goon: + MOV ESP, EBP + POP EBP + RET + + +func1: + RET +func2: + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_19.S b/test/samples/x86_32/cst_propag/x86_32_sc_19.S new file mode 100644 index 00000000..0b0e0837 --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_19.S @@ -0,0 +1,16 @@ +main: + + MOV EBX, 1 + MOV ECX, 1 + CMP EDX, 10 + JZ test1 + ADD EBX, 1 + ADD ECX, 1 + JMP gogon +test1: + ADD EBX, 2 + ADD ECX, 2 +gogon: + ADD EAX, EBX + ADD EAX, ECX + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_2.S b/test/samples/x86_32/cst_propag/x86_32_sc_2.S new file mode 100644 index 00000000..96e10fa6 --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_2.S @@ -0,0 +1,21 @@ +main: + PUSH EBP + MOV EBP, ESP + MOV ECX, DWORD PTR [ESP+0x8] + MOV EDX, DWORD PTR [EBP+0xC] + LEA ECX, DWORD PTR [ECX+0x4] + LEA EBX, DWORD PTR [EBX+0x1] + CMP CL, 0x1 + JZ test1 + LEA EBX, DWORD PTR [EBX-1] + JMP end +test1: + LEA EBX, DWORD PTR [EBX+0x1] +end: + MOV EAX, EBX + LEA EAX, DWORD PTR [EAX + EDX] + MOV EDX, DWORD PTR [EBP+0xC] + LEA EAX, DWORD PTR [EAX + EDX] + MOV ESP, EBP + POP EBP + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_20.S b/test/samples/x86_32/cst_propag/x86_32_sc_20.S new file mode 100644 index 00000000..4f2b82c6 --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_20.S @@ -0,0 +1,19 @@ +main: + MOV EBX, 0 + MOV ECX, 0 + + CMP EDX, 0 + JNZ test1 + JMP goon +test1: + MOV EDX, 1 + LEA EDX, DWORD PTR [EDX+0xF] + LEA EBX, DWORD PTR [EBX+EDX] + MOV EDX, 2 + LEA EDX, DWORD PTR [EDX+0xE] + MOV ECX, EDX + LEA EBX, DWORD PTR [EBX+ECX] + JNZ test1 +goon: + MOV EAX, EBX + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_3.S b/test/samples/x86_32/cst_propag/x86_32_sc_3.S new file mode 100644 index 00000000..46d2afff --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_3.S @@ -0,0 +1,15 @@ +main: + PUSH EBP + MOV EBP, ESP + MOV ECX, DWORD PTR [EBP+0x8] +loop: + SUB ECX, 1 + JZ end + PUSH EDX + POP ESI + JMP loop +end: + MOV EAX, DWORD PTR [ESP+0xC] + MOV ESP, EBP + POP EBP + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_4.S b/test/samples/x86_32/cst_propag/x86_32_sc_4.S new file mode 100644 index 00000000..1f9e82a3 --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_4.S @@ -0,0 +1,15 @@ +main: + PUSH EBP + MOV EBP, ESP + MOV ECX, DWORD PTR [EBP+0x8] +loop: + PUSH EDX + POP ESI + SUB ECX, 1 + JZ end + JMP loop +end: + MOV EAX, DWORD PTR [ESP+0xC] + MOV ESP, EBP + POP EBP + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_5.S b/test/samples/x86_32/cst_propag/x86_32_sc_5.S new file mode 100644 index 00000000..b9d7e08a --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_5.S @@ -0,0 +1,24 @@ +main: + PUSH EBP + MOV EBP, ESP + MOV ECX, DWORD PTR [EBP+0x8] + + SUB ECX, 1 + JZ test1 + SUB ECX, 1 + JZ test2 + SUB ECX, 1 + JZ test3 + + JMP end +test1: + INC EAX +test2: + INC EAX +test3: + INC EAX +end: + INC EAX + MOV ESP, EBP + POP EBP + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_6.S b/test/samples/x86_32/cst_propag/x86_32_sc_6.S new file mode 100644 index 00000000..65fc2b8b --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_6.S @@ -0,0 +1,37 @@ +main: + PUSH EBP + MOV EBP, ESP + MOV ECX, DWORD PTR [EBP+0x8] + + INC EAX + SUB ECX, 1 + JZ test1 + ADD EAX, 1 + JMP go1 +test1: + ADD EAX, 2 +go1: + + + INC EAX + SUB ECX, 1 + JZ test2 + ADD EAX, 0x10 + JMP go2 +test2: + ADD EAX, 0x20 +go2: + + INC EAX + SUB ECX, 1 + JZ test3 + ADD EAX, 0x30 + JMP go3 +test3: + ADD EAX, 0x40 +go3: + + INC EAX + MOV ESP, EBP + POP EBP + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_7.S b/test/samples/x86_32/cst_propag/x86_32_sc_7.S new file mode 100644 index 00000000..96c5fc6e --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_7.S @@ -0,0 +1,16 @@ +main: + PUSH EBP + MOV EBP, ESP + MOV ECX, DWORD PTR [EBP+0x8] + INC EAX + +loop: + INC EAX + DEC EAX + SUB ECX, 1 + JZ loop + + INC EAX + MOV ESP, EBP + POP EBP + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_8.S b/test/samples/x86_32/cst_propag/x86_32_sc_8.S new file mode 100644 index 00000000..5127c0fa --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_8.S @@ -0,0 +1,18 @@ +main: + PUSH EBP + MOV EBP, ESP + MOV ECX, DWORD PTR [EBP+0x8] + INC EAX + +loop: + MOV EDX, 1 + MOV ESI, 1 + ADD EAX, EDX + SUB EAX, ESI + SUB ECX, 1 + JZ loop + + INC EAX + MOV ESP, EBP + POP EBP + RET diff --git a/test/samples/x86_32/cst_propag/x86_32_sc_9.S b/test/samples/x86_32/cst_propag/x86_32_sc_9.S new file mode 100644 index 00000000..0b01305b --- /dev/null +++ b/test/samples/x86_32/cst_propag/x86_32_sc_9.S @@ -0,0 +1,36 @@ +main: + PUSH EBP + MOV EBP, ESP + MOV ECX, 10 ; DWORD PTR [EBP+0x8] + + MOV EBX, 0x1000 ; + INC EBX ; EBX = 0x1001 + MOV EAX, EBX ; EAX = 0x1001 + MOV EBX, 0x10001 ; EBX = 0x10001 + DEC EBX ; EBX = 0x10000 + MOV ESI, EBX ; ESI = 0x10000 + ; + ADD EDI, EAX ; EDI += 0x1001 + ADD EDI, ESI ; EDI += 0x10000 + ;; EDI = EDI + 0x11001 + +loop: + MOV EBX, 0x1000 ; + MOV EAX, EBX ; + MOV EBX, 0x100001 ; + MOV ESI, EBX ; + MUL ESI ; EAX = 0x1000 + MOV EBX, 0x1 ; + ADD EDI, EBX ; EDI += 1 + MOV EBX, 0x1000 ; + ADD EDI, EBX ; EDI += 0x1000 + SUB EDI, EAX ; EDI -= 0x1000 + DEC EDI ; EDI -= 1 + SUB ECX, 1 + JNZ loop + + INC EDI + MOV EAX, EDI + MOV ESP, EBP + POP EBP + RET diff --git a/test/test_all.py b/test/test_all.py index 3da2dbb5..17193d9f 100755 --- a/test/test_all.py +++ b/test/test_all.py @@ -188,8 +188,9 @@ class SemanticTestAsm(RegressionTest): self.command_line = [self.shellcode_script, arch, input_filename, - output_filename, - self.container_dct.get(container, '')] + output_filename] + if container in self.container_dct: + self.command_line.append(self.container_dct[container]) self.products = [output_filename, "graph.dot"] @@ -247,6 +248,11 @@ for script in ["modint.py", ]: testset += RegressionTest([script], base_dir="expression") +## ObjC/CHandler +testset += RegressionTest(["test_chandler.py"], base_dir="expr_type", + tags=[TAGS["cparser"]]) + + ## IR for script in ["symbexec.py", "ir.py", @@ -299,6 +305,17 @@ testset += RegressionTest(["data_flow.py"], base_dir="analysis", for test_nb in xrange(1, 18)) for fname in fnames]) +for i in xrange(1, 21): + input_name = "cst_propag/x86_32_sc_%d" % i + bin_name = "samples/x86_32/%s.bin" % input_name + test_x86_32_cst = SemanticTestAsm("x86_32", None, [input_name]) + testset+= test_x86_32_cst + testset += RegressionTest(["../example/expression/constant_propagation.py", "-s", bin_name, "0"], + depends=[test_x86_32_cst], + products=["%s.propag.dot" % bin_name]) + + + ## Degraph class TestDepgraph(RegressionTest): """Dependency graph test""" @@ -560,6 +577,11 @@ testset += ExampleExpression(["access_c.py", Example.get_sample("human.bin")], testset += ExampleExpression(["expr_c.py"], tags=[TAGS["cparser"]]) + +testset += ExampleExpression(["constant_propagation.py", + Example.get_sample("simple_test.bin"), "-s", "0"], + products=["%s.propag.dot" % Example.get_sample("simple_test.bin")]) + for script in [["basic_op.py"], ["basic_simplification.py"], ["simplification_tools.py"], |