Changeset 1551
- Timestamp:
- 02/14/08 13:54:47 (8 months ago)
- Files:
-
- libemu/trunk/include/emu/environment/emu_profile.h (modified) (4 diffs)
- libemu/trunk/src/environment/emu_profile.c (modified) (16 diffs)
- libemu/trunk/src/environment/win32/env_w32_dll_export_ws2_32_hooks.c (modified) (2 diffs)
- libemu/trunk/testsuite/Makefile.am (modified) (2 diffs)
- libemu/trunk/testsuite/scprofiler.c (added)
- libemu/trunk/testsuite/sctest.c (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
libemu/trunk/include/emu/environment/emu_profile.h
r1531 r1551 39 39 render_ptr, 40 40 render_int, 41 render_short, 41 42 render_struct, 42 43 render_string, … … 59 60 union 60 61 { 61 int tint; 62 int32_t tint; 63 int16_t tshort; 64 62 65 char *tchar; 63 66 struct … … 120 123 void emu_profile_function_debug(struct emu_profile_function *function); 121 124 125 void emu_profile_argument_debug(struct emu_profile_argument *argument, int indent); 126 122 127 void emu_profile_argument_add_none(struct emu_profile *profile); 123 128 void emu_profile_argument_add_int(struct emu_profile *profile, char *argtype, char *argname, int value); 129 void emu_profile_argument_add_short(struct emu_profile *profile, char *argtype, char *argname, int16_t value); 124 130 void emu_profile_argument_add_string(struct emu_profile *profile, char *argtype, char *argname, char *value); 125 131 void emu_profile_argument_add_ptr(struct emu_profile *profile, char *argtype, char *argname, uint32_t value); … … 136 142 void emu_profile_function_returnvalue_int_set(struct emu_profile *profile, const char *type, int value); 137 143 void emu_profile_function_returnvalue_ptr_set(struct emu_profile *profile, const char *type, int value); 144 145 146 void *emu_profile_function_argument_get(struct emu_profile_function *function, int argc); 138 147 #endif libemu/trunk/src/environment/emu_profile.c
r1531 r1551 37 37 #include "emu/environment/emu_profile.h" 38 38 39 /* 39 40 static char *renderings[] = 40 41 { … … 49 50 "render_array" 50 51 }; 51 52 */ 52 53 typedef unsigned char byte; 53 54 … … 153 154 } 154 155 156 void emu_profile_argument_add_short(struct emu_profile *profile, char *argtype, char *argname, int16_t value) 157 { 158 struct emu_profile_argument *argument = emu_profile_argument_new(render_short, argtype, argname); 159 argument->value.tshort = value; 160 emu_profile_argument_add(profile, argument); 161 } 162 163 155 164 void emu_profile_argument_add_string(struct emu_profile *profile, char *argtype, char *argname, char *value) 156 165 { … … 231 240 } 232 241 242 uint32_t measure_size(struct emu_profile_argument *argument, bool followptr) 243 { 244 uint32_t size = 0; 245 struct emu_profile_argument *argumentit; 246 247 switch(argument->render) 248 { 249 case render_struct: 250 for (argumentit = emu_profile_arguments_first(argument->value.tstruct.arguments); 251 !emu_profile_arguments_istail(argumentit); 252 argumentit = emu_profile_arguments_next(argumentit)) 253 { 254 size += measure_size(argumentit, followptr); 255 } 256 break; 257 258 case render_array: 259 260 for (argumentit = emu_profile_arguments_first(argument->value.tstruct.arguments); 261 !emu_profile_arguments_istail(argumentit); 262 argumentit = emu_profile_arguments_next(argumentit)) 263 { 264 size += measure_size(argumentit, followptr); 265 266 } 267 break; 268 269 case render_int: 270 size += 4; 271 break; 272 273 case render_short: 274 size += 2; 275 break; 276 277 case render_string: 278 size += strlen(argument->value.tchar) +1; 279 break; 280 281 case render_ptr: 282 { 283 size += 4; 284 if (followptr) 285 size += measure_size(argument->value.tptr.ptr, followptr); 286 } 287 break; 288 289 case render_ip: 290 size += 4; 291 break; 292 293 case render_port: 294 size += 2; 295 break; 296 297 case render_none: 298 size += 4; 299 break; 300 } 301 302 return size; 303 } 304 305 int copy_data(struct emu_profile_argument *argument, uint8_t *addr, uint8_t **next) 306 { 307 printf("%s : %i \n", __PRETTY_FUNCTION__, __LINE__); 308 309 uint32_t *addr32 = (uint32_t *)addr; 310 uint16_t *addr16 = (uint16_t *)addr; 311 312 int size = measure_size(argument, false); 313 int offset = 0; 314 315 struct emu_profile_argument *argumentit; 316 317 if (addr == *next) 318 { 319 *next = addr + size; 320 } 321 322 switch(argument->render) 323 { 324 case render_struct: 325 for (argumentit = emu_profile_arguments_first(argument->value.tstruct.arguments); 326 !emu_profile_arguments_istail(argumentit); 327 argumentit = emu_profile_arguments_next(argumentit)) 328 { 329 // size += measure_size(argumentit, false); 330 offset += copy_data(argumentit, addr+offset, next); 331 } 332 break; 333 334 case render_array: 335 336 for (argumentit = emu_profile_arguments_first(argument->value.tstruct.arguments); 337 !emu_profile_arguments_istail(argumentit); 338 argumentit = emu_profile_arguments_next(argumentit)) 339 { 340 // size += measure_size(argumentit, level - 1); 341 offset += copy_data(argumentit, addr+offset, next); 342 } 343 break; 344 345 case render_int: 346 // size += 4; 347 *addr32 = argument->value.tint; 348 break; 349 350 case render_short: 351 *addr16 = argument->value.tshort; 352 break; 353 354 case render_string: 355 // size += strlen(argument->value.tchar) +1; 356 strcpy((char *)addr, argument->value.tchar); 357 break; 358 359 case render_ptr: 360 { 361 // size += 4; 362 // size += measure_size(argument->value.tptr.ptr, false); 363 *addr32 = (uint32_t)*next; 364 365 copy_data(argument->value.tptr.ptr, *next, next); 366 } 367 break; 368 369 case render_ip: 370 // size += 4; 371 *addr32 = argument->value.tint; 372 break; 373 374 case render_port: 375 // size += 2; 376 *addr16 = (uint16_t)argument->value.tint; 377 378 break; 379 380 case render_none: 381 // size += 4; 382 break; 383 } 384 385 return size; 386 } 387 388 void *emu_profile_function_argument_get(struct emu_profile_function *function, int argc) 389 { 390 int i = 0; 391 struct emu_profile_argument *argument = emu_profile_arguments_first(function->arguments); 392 393 while (i < argc) 394 { 395 argument = emu_profile_arguments_next(argument); 396 i++; 397 398 if (emu_profile_arguments_istail(argument)) 399 return NULL; 400 } 401 402 uint32_t size = 0; 403 size = measure_size(argument, true); 404 405 printf("%s size is %i\n", argument->argname, size); 406 407 uint8_t *data = malloc(size); 408 uint8_t *next = data; 409 memset(data, 0x90, size); 410 copy_data(argument, data, &next); 411 412 return data; 413 } 414 415 233 416 struct emu_profile_argument *emu_profile_argument_new(enum emu_profile_argument_render render, const char *type, const char *name) 234 417 { … … 266 449 case render_none: 267 450 case render_int: 451 case render_short: 268 452 break; 269 453 … … 337 521 printf("%s %s %s = %i;\n", indents(indent), argument->argtype, argument->argname, argument->value.tint); 338 522 break; 523 524 case render_short: 525 printf("%s %s %s = %i;\n", indents(indent), argument->argtype, argument->argname, argument->value.tshort); 526 break; 527 339 528 340 529 case render_string: … … 419 608 int emu_profile_dump_byte_write(FILE *f, byte value) 420 609 { 421 if (fwrite(&value, 1, 1, f) == 4)610 if (fwrite(&value, 1, 1, f) == 1) 422 611 return 0; 423 612 return -1; … … 432 621 return -1; 433 622 } 623 624 int emu_profile_dump_short_write(FILE *f, int16_t value) 625 { 626 int16_t nval = htons(value); 627 628 if (fwrite(&nval, 2, 1, f) == 2) 629 return 0; 630 return -1; 631 } 632 434 633 435 634 int emu_profile_dump_string_write(FILE *f, const char *string) … … 473 672 break; 474 673 674 case render_short: 675 emu_profile_dump_short_write(f, argument->value.tshort); 676 break; 677 678 475 679 case render_string: 476 680 emu_profile_dump_string_write(f, argument->value.tchar); … … 578 782 return -1; 579 783 } 784 785 int emu_profile_dump_short_read(FILE *f, int16_t *i) 786 { 787 if (fread(i, 1, 2, f) == 2) 788 { 789 *i = ntohs(*i); 790 return 0; 791 } 792 return -1; 793 } 794 795 580 796 581 797 int emu_profile_dump_string_read(FILE *f, char **string) … … 605 821 return 0; 606 822 607 printf("%i %s %s %s\n",render , renderings[render], argtype, argname);823 // printf("%i %s %s %s\n",render , renderings[render], argtype, argname); 608 824 609 825 switch ( render ) … … 616 832 int argcount=0; 617 833 emu_profile_dump_int_read(f, &argcount); 618 printf("parsing %i struct arguments\n", argcount);834 // printf("parsing %i struct arguments\n", argcount); 619 835 while ( argcount > 0 ) 620 836 { … … 632 848 int argcount=0; 633 849 emu_profile_dump_int_read(f, &argcount); 634 printf("parsing %i array arguments\n", argcount);850 // printf("parsing %i array arguments\n", argcount); 635 851 while ( argcount > 0 ) 636 852 { … … 650 866 break; 651 867 868 case render_short: 869 { 870 int16_t value = 0; 871 emu_profile_dump_short_read(f,&value); 872 emu_profile_argument_add_short(profile, argtype, argname, value); 873 } 874 break; 875 652 876 case render_string: 653 877 { … … 702 926 int argcount = 0; 703 927 emu_profile_dump_int_read(f, &argcount); 704 printf("parsing %i function arguments\n", argcount);928 // printf("parsing %i function arguments\n", argcount); 705 929 while (argcount > 0) 706 930 { … … 726 950 int functions = 0; 727 951 emu_profile_dump_int_read(f, &functions); 728 printf("parsing %i functions\n", functions);952 // printf("parsing %i functions\n", functions); 729 953 while (functions > 0) 730 954 { libemu/trunk/src/environment/win32/env_w32_dll_export_ws2_32_hooks.c
r1531 r1551 138 138 emu_profile_argument_add_ptr(env->profile, "sockaddr_in *", "name", name); 139 139 emu_profile_argument_struct_start(env->profile, "", ""); 140 emu_profile_argument_add_ int(env->profile, "short", "sin_family", si->sin_family);140 emu_profile_argument_add_short(env->profile, "short", "sin_family", si->sin_family); 141 141 emu_profile_argument_add_port(env->profile, "unsigned short", "sin_port", si->sin_port); 142 142 emu_profile_argument_struct_start(env->profile, "in_addr", "sin_addr"); … … 240 240 emu_profile_argument_add_ptr(env->profile, "sockaddr_in *", "name", name); 241 241 emu_profile_argument_struct_start(env->profile, "", ""); 242 emu_profile_argument_add_ int(env->profile, "short", "sin_family", si->sin_family);242 emu_profile_argument_add_short(env->profile, "short", "sin_family", si->sin_family); 243 243 emu_profile_argument_add_port(env->profile, "unsigned short", "sin_port", si->sin_port); 244 244 emu_profile_argument_struct_start(env->profile, "in_addr", "sin_addr"); libemu/trunk/testsuite/Makefile.am
r1509 r1551 4 4 AM_LDFLAGS = -lemu -L../src 5 5 6 bin_PROGRAMS = sctest 7 noinst_PROGRAMS = testsuite cpurun instrtest instrtree hashtest memtest 6 bin_PROGRAMS = sctest scprofiler 7 noinst_PROGRAMS = testsuite cpurun instrtest instrtree hashtest memtest 8 8 9 9 testsuite_LDADD = ../src/libemu.la … … 31 31 memtest_SOURCES = memtest.c 32 32 33 scprofiler_LDADD = ../src/libemu.la 34 scprofiler_SOURCES = scprofiler.c 35 33 36 EXTRA_DIST = emunids.c libemu/trunk/testsuite/sctest.c
r1531 r1551 1514 1514 }; 1515 1515 1516 1516 1517 struct instr_vertex 1517 1518 { … … 1748 1749 emu_hashtable_insert(eh, (void *)eipsave, ev); 1749 1750 } 1750 1751 if ( last_vertex != NULL )1752 {1753 struct emu_edge *ee = emu_vertex_edge_add(last_vertex, ev);1754 struct emu_cpu *cpu = emu_cpu_get(e);1755 if ( cpu->instr.is_fpu == 0 && cpu->instr.source.cond_pos == eipsave && cpu->instr.source.has_cond_pos == 1 )1756 ee->data = (void *)0x1;1757 }1758 1759 last_vertex = ev;1760 1751 } 1761 1752 … … 1764 1755 if ( dllhook != NULL ) 1765 1756 { 1766 if ( opts.graphfile != NULL && ev->data == NULL ) 1757 1758 if ( opts.graphfile != NULL ) 1767 1759 { 1760 if ( ev->data != NULL && strcmp(dllhook->fnname, "CreateProcessA") == 0) 1761 { 1762 ev = emu_vertex_new(); 1763 emu_graph_vertex_add(graph, ev); 1764 } 1765 1766 // fnname_from_profile(env->profile, dllhook->fnname); 1768 1767 iv = instr_vertex_new(eipsave,dllhook->fnname); 1769 1768 emu_vertex_data_set(ev, iv); … … 1865 1864 } 1866 1865 } 1866 if ( opts.graphfile != NULL ) 1867 { 1868 if ( last_vertex != NULL ) 1869 { 1870 struct emu_edge *ee = emu_vertex_edge_add(last_vertex, ev); 1871 struct emu_cpu *cpu = emu_cpu_get(e); 1872 if ( cpu->instr.is_fpu == 0 && cpu->instr.source.cond_pos == eipsave && cpu->instr.source.has_cond_pos == 1 ) 1873 ee->data = (void *)0x1; 1874 } 1875 1876 last_vertex = ev; 1877 } 1867 1878 1868 1879 // printf("\n"); … … 1923 1934 nev->data = niv; 1924 1935 1925 emu_hashtable_insert(ht, (void *)iv ->eip, nev);1936 emu_hashtable_insert(ht, (void *)iv, nev); 1926 1937 ev->color = white; 1927 1938 } … … 1956 1967 1957 1968 // create the new vertex 1958 nev = (struct emu_vertex *)emu_hashtable_search(ht, (void *)iv ->eip)->value;1969 nev = (struct emu_vertex *)emu_hashtable_search(ht, (void *)iv)->value; 1959 1970 niv = (struct instr_vertex *)nev->data; 1960 1971 … … 1986 1997 { 1987 1998 struct instr_vertex *ivto = emu_vertex_data_get(ee->destination); 1988 struct emu_hashtable_item *ehi = emu_hashtable_search(ht, (void *)ivto ->eip);1999 struct emu_hashtable_item *ehi = emu_hashtable_search(ht, (void *)ivto); 1989 2000 struct emu_vertex *to = (struct emu_vertex *)ehi->value; 1990 2001 if ( 1 )// nev != to )//&& to->color != black ) … … 2042 2053 #endif // 0 2043 2054 if ( iv->dll != NULL || iv->syscall != NULL ) 2044 fprintf(f, "\t %i [shape=box, style=filled, color=\".7 .3 1.0\", label = \"%s\"]\n", iv->eip, emu_string_char(iv->instr_string));2055 fprintf(f, "\t %i [shape=box, style=filled, color=\".7 .3 1.0\", label = \"%s\"]\n",(unsigned int)iv, emu_string_char(iv->instr_string)); 2045 2056 else 2046 fprintf(f, "\t %i [shape=box, label = \"%s\"]\n", iv->eip, emu_string_char(iv->instr_string));2057 fprintf(f, "\t %i [shape=box, label = \"%s\"]\n",(unsigned int)iv, emu_string_char(iv->instr_string)); 2047 2058 } 2048 2059 … … 2058 2069 2059 2070 if ( ee->data != (void *)0x0 ) 2060 emu_string_append_format(fs, "\t %i -> %i [style = dashed", ivfrom->eip, ivto->eip);2071 emu_string_append_format(fs, "\t %i -> %i [style = dashed", (unsigned int)ivfrom, (unsigned int)ivto); 2061 2072 else 2062 emu_string_append_format(fs, "\t %i -> %i [style = bold", ivfrom->eip, ivto->eip);2073 emu_string_append_format(fs, "\t %i -> %i [style = bold", (unsigned int)ivfrom, (unsigned int)ivto); 2063 2074 2064 2075 if ( ee->count > 100 ) … … 2095 2106 } 2096 2107 2097 /* 2098 int getpctest(int n) 2099 { 2100 int i=0; 2108 2109 int getpctest() 2110 { 2101 2111 struct emu *e = emu_new(); 2102 2112 2103 if ( opts.verbose ==1 )2113 if ( opts.verbose > 1 ) 2104 2114 { 2105 2115 emu_log_level_set(emu_logging_get(e),EMU_LOG_DEBUG); 2106 2116 } 2107 2117 2108 for ( i=0;i<sizeof(tests)/sizeof(struct instr_test);i++ ) 2109 { 2110 if ( n != -1 && i != n && !opts.from_stdin ) 2111 continue; 2112 2113 if( !opts.from_stdin ) 2114 { 2115 printf("testing (#%d) '%s' \t", i, tests[i].instr); 2116 if ( emu_shellcode_test(e, (uint8_t *)tests[i].code, tests[i].codesize) >= 0 ) 2117 printf(SUCCESS"\n"); 2118 else 2119 printf(FAILED"\n"); 2120 }else 2121 { 2122 int32_t off; 2123 if ( (off = emu_shellcode_test(e, (uint8_t *)opts.scode, opts.size)) >= 0 ) 2124 { 2125 printf(SUCCESS" (offset %i (0x%x))\n",off,(unsigned int)off); 2126 opts.offset = off; 2127 test(n); 2128 } 2129 else 2130 printf(FAILED"\n"); 2131 } 2132 2133 emu_memory_clear(emu_memory_get(e)); 2134 2135 if (opts.from_stdin) 2136 break; 2137 } 2118 if ( (opts.offset = emu_shellcode_test(e, (uint8_t *)opts.scode, opts.size)) >= 0 ) 2119 printf(SUCCESS"\n"); 2120 else 2121 printf(FAILED"\n"); 2122 2138 2123 emu_free(e); 2124 2139 2125 return 0; 2140 2126 } 2141 */ 2127 2142 2128 2143 2129 void dump(int n) … … 2210 2196 } 2211 2197 2212 2213 int prepare_from_stdin(struct emu *e) 2198 int prepare_from_stdin_read() 2214 2199 { 2215 2200 unsigned buffer[BUFSIZ]; … … 2262 2247 opts.size = len; 2263 2248 2264 2249 return 0; 2250 } 2251 2252 2253 int prepare_from_stdin_write(struct emu *e) 2254 { 2265 2255 /* set the registers to the initial values */ 2266 2256 struct emu_cpu *cpu = emu_cpu_get(e); … … 2281 2271 int static_offset = CODE_OFFSET; 2282 2272 emu_memory_write_block(mem, static_offset, opts.scode, opts.size); 2283 2273 2284 2274 2285 2275 … … 2290 2280 2291 2281 free(opts.scode); 2282 2292 2283 return 0; 2293 2294 2284 } 2285 2286 int prepare_from_stdin(struct emu *e) 2287 { 2288 if (opts.size != 0) 2289 prepare_from_stdin_read(); 2290 2291 prepare_from_stdin_write(e); 2292 2293 2294 return 0; 2295 2296 } 2297 2295 2298 2296 2299 … … 2329 2332 /* set eip to the code */ 2330 2333 emu_cpu_eip_set(emu_cpu_get(e), static_offset + opts.offset); 2334 2335 opts.scode = (unsigned char *) tests[i].code; 2336 opts.size = tests[i].codesize; 2331 2337 return 0; 2332 2338 } … … 2519 2525 struct emu *e = emu_new(); 2520 2526 if ( prepare(e) == 0 ) 2527 { 2528 if (opts.getpc == 1) 2529 { 2530 getpctest(); 2531 2532 emu_free(e); 2533 e = emu_new(); 2534 prepare(e); 2535 } 2536 2521 2537 test(e); 2522 2538 } 2523 2539 2524 2540 emu_free(e);
