Changeset 1551

Show
Ignore:
Timestamp:
02/14/08 13:54:47 (8 months ago)
Author:
common
Message:

libemu

  • void *emu_profile_function_argument_get(struct emu_profile_function *function, int argc);
    added, expected to work
  • emu_profile supports shorts
  • sctest, minor changes, try to reestablisch getpctest()
  • scprofiler, testdummy for emu_profile_function_argument_get()
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libemu/trunk/include/emu/environment/emu_profile.h

    r1531 r1551  
    3939        render_ptr, 
    4040        render_int, 
     41        render_short, 
    4142        render_struct, 
    4243        render_string, 
     
    5960        union 
    6061        { 
    61                 int tint; 
     62                int32_t tint; 
     63                int16_t tshort; 
     64 
    6265                char *tchar; 
    6366                struct  
     
    120123void emu_profile_function_debug(struct emu_profile_function *function); 
    121124 
     125void emu_profile_argument_debug(struct emu_profile_argument *argument, int indent); 
     126 
    122127void emu_profile_argument_add_none(struct emu_profile *profile); 
    123128void emu_profile_argument_add_int(struct emu_profile *profile, char *argtype, char *argname, int value); 
     129void emu_profile_argument_add_short(struct emu_profile *profile, char *argtype, char *argname, int16_t value); 
    124130void emu_profile_argument_add_string(struct emu_profile *profile, char *argtype,  char *argname, char *value); 
    125131void emu_profile_argument_add_ptr(struct emu_profile *profile,  char *argtype,  char *argname, uint32_t value); 
     
    136142void emu_profile_function_returnvalue_int_set(struct emu_profile *profile, const char *type, int value); 
    137143void emu_profile_function_returnvalue_ptr_set(struct emu_profile *profile, const char *type, int value); 
     144 
     145 
     146void *emu_profile_function_argument_get(struct emu_profile_function *function, int argc); 
    138147#endif 
  • libemu/trunk/src/environment/emu_profile.c

    r1531 r1551  
    3737#include "emu/environment/emu_profile.h" 
    3838 
     39/* 
    3940static char *renderings[] = 
    4041{ 
     
    4950        "render_array" 
    5051}; 
    51  
     52*/ 
    5253typedef unsigned char byte; 
    5354 
     
    153154} 
    154155 
     156void 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 
    155164void emu_profile_argument_add_string(struct emu_profile *profile, char *argtype, char *argname, char *value) 
    156165{ 
     
    231240} 
    232241 
     242uint32_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 
     305int 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 
     388void *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 
    233416struct emu_profile_argument *emu_profile_argument_new(enum emu_profile_argument_render render, const char *type, const char *name) 
    234417{ 
     
    266449        case render_none: 
    267450        case render_int: 
     451        case render_short: 
    268452                break; 
    269453 
     
    337521                printf("%s %s %s = %i;\n", indents(indent), argument->argtype, argument->argname, argument->value.tint); 
    338522                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 
    339528 
    340529        case render_string: 
     
    419608int emu_profile_dump_byte_write(FILE *f, byte value) 
    420609{ 
    421         if (fwrite(&value, 1, 1, f) == 4
     610        if (fwrite(&value, 1, 1, f) == 1
    422611                return 0; 
    423612        return -1; 
     
    432621        return -1; 
    433622} 
     623 
     624int 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 
    434633 
    435634int emu_profile_dump_string_write(FILE *f, const char *string) 
     
    473672                break; 
    474673 
     674        case render_short: 
     675                emu_profile_dump_short_write(f, argument->value.tshort); 
     676                break; 
     677 
     678 
    475679        case render_string: 
    476680                emu_profile_dump_string_write(f, argument->value.tchar); 
     
    578782        return -1; 
    579783} 
     784 
     785int 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 
    580796 
    581797int emu_profile_dump_string_read(FILE *f, char **string) 
     
    605821                return 0; 
    606822 
    607       printf("%i %s %s %s\n",render , renderings[render], argtype, argname); 
     823//    printf("%i %s %s %s\n",render , renderings[render], argtype, argname); 
    608824 
    609825        switch ( render ) 
     
    616832                        int argcount=0; 
    617833                        emu_profile_dump_int_read(f, &argcount); 
    618                       printf("parsing %i struct arguments\n", argcount); 
     834//                    printf("parsing %i struct arguments\n", argcount); 
    619835                        while ( argcount > 0 ) 
    620836                        { 
     
    632848                        int argcount=0; 
    633849                        emu_profile_dump_int_read(f, &argcount); 
    634                       printf("parsing %i array arguments\n", argcount); 
     850//                    printf("parsing %i array arguments\n", argcount); 
    635851                        while ( argcount > 0 ) 
    636852                        { 
     
    650866                break; 
    651867 
     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 
    652876        case render_string: 
    653877                { 
     
    702926        int argcount = 0; 
    703927        emu_profile_dump_int_read(f, &argcount); 
    704       printf("parsing %i function arguments\n", argcount); 
     928//    printf("parsing %i function arguments\n", argcount); 
    705929        while (argcount > 0) 
    706930        { 
     
    726950        int functions = 0; 
    727951        emu_profile_dump_int_read(f, &functions); 
    728       printf("parsing %i functions\n", functions); 
     952//    printf("parsing %i functions\n", functions); 
    729953        while (functions > 0) 
    730954        { 
  • libemu/trunk/src/environment/win32/env_w32_dll_export_ws2_32_hooks.c

    r1531 r1551  
    138138                emu_profile_argument_add_ptr(env->profile, "sockaddr_in *", "name", name); 
    139139                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); 
    141141                emu_profile_argument_add_port(env->profile, "unsigned short", "sin_port", si->sin_port); 
    142142                emu_profile_argument_struct_start(env->profile, "in_addr", "sin_addr"); 
     
    240240                emu_profile_argument_add_ptr(env->profile, "sockaddr_in *", "name", name); 
    241241                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); 
    243243                emu_profile_argument_add_port(env->profile, "unsigned short", "sin_port", si->sin_port); 
    244244                emu_profile_argument_struct_start(env->profile, "in_addr", "sin_addr"); 
  • libemu/trunk/testsuite/Makefile.am

    r1509 r1551  
    44AM_LDFLAGS = -lemu -L../src  
    55 
    6 bin_PROGRAMS = sctest 
    7 noinst_PROGRAMS = testsuite cpurun instrtest instrtree hashtest memtest 
     6bin_PROGRAMS = sctest scprofiler 
     7noinst_PROGRAMS = testsuite cpurun instrtest instrtree hashtest memtest  
    88 
    99testsuite_LDADD = ../src/libemu.la 
     
    3131memtest_SOURCES = memtest.c 
    3232 
     33scprofiler_LDADD = ../src/libemu.la 
     34scprofiler_SOURCES = scprofiler.c 
     35 
    3336EXTRA_DIST = emunids.c 
  • libemu/trunk/testsuite/sctest.c

    r1531 r1551  
    15141514}; 
    15151515 
     1516 
    15161517struct instr_vertex 
    15171518{ 
     
    17481749                                emu_hashtable_insert(eh, (void *)eipsave, ev); 
    17491750                        } 
    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; 
    17601751                } 
    17611752 
     
    17641755                if ( dllhook != NULL ) 
    17651756                { 
    1766                         if ( opts.graphfile != NULL && ev->data == NULL ) 
     1757                          
     1758                        if ( opts.graphfile != NULL ) 
    17671759                        { 
     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); 
    17681767                                iv = instr_vertex_new(eipsave,dllhook->fnname); 
    17691768                                emu_vertex_data_set(ev, iv); 
     
    18651864                        } 
    18661865                } 
     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                } 
    18671878 
    18681879//                      printf("\n"); 
     
    19231934                nev->data = niv; 
    19241935 
    1925                 emu_hashtable_insert(ht, (void *)iv->eip, nev); 
     1936                emu_hashtable_insert(ht, (void *)iv, nev); 
    19261937                ev->color = white; 
    19271938        } 
     
    19561967 
    19571968                // 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; 
    19591970                niv = (struct instr_vertex *)nev->data; 
    19601971 
     
    19861997                { 
    19871998                        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); 
    19892000                        struct emu_vertex *to = (struct emu_vertex *)ehi->value; 
    19902001                        if ( 1 )// nev != to )//&& to->color != black ) 
     
    20422053#endif // 0 
    20432054                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)); 
    20452056                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)); 
    20472058        } 
    20482059 
     
    20582069 
    20592070                        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); 
    20612072                        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); 
    20632074 
    20642075                        if ( ee->count > 100 ) 
     
    20952106} 
    20962107 
    2097 /* 
    2098 int getpctest(int n) 
    2099 
    2100         int i=0; 
     2108 
     2109int getpctest() 
     2110
    21012111        struct emu *e = emu_new(); 
    21022112 
    2103         if ( opts.verbose == 1 ) 
     2113        if ( opts.verbose > 1 ) 
    21042114        { 
    21052115                emu_log_level_set(emu_logging_get(e),EMU_LOG_DEBUG); 
    21062116        } 
    21072117 
    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 
    21382123        emu_free(e); 
     2124 
    21392125        return 0; 
    21402126} 
    2141 */ 
     2127 
    21422128 
    21432129void dump(int n) 
     
    22102196} 
    22112197 
    2212  
    2213 int prepare_from_stdin(struct emu *e) 
     2198int prepare_from_stdin_read() 
    22142199{ 
    22152200        unsigned buffer[BUFSIZ]; 
     
    22622247        opts.size = len; 
    22632248 
    2264  
     2249        return 0; 
     2250
     2251 
     2252 
     2253int prepare_from_stdin_write(struct emu *e) 
     2254
    22652255        /* set the registers to the initial values */ 
    22662256        struct emu_cpu *cpu = emu_cpu_get(e); 
     
    22812271        int static_offset = CODE_OFFSET; 
    22822272        emu_memory_write_block(mem, static_offset, opts.scode,  opts.size); 
    2283          
     2273 
    22842274 
    22852275 
     
    22902280 
    22912281        free(opts.scode); 
     2282 
    22922283        return 0; 
    2293  
    22942284} 
     2285 
     2286int 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 
    22952298 
    22962299 
     
    23292332                /* set eip to the code */ 
    23302333                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; 
    23312337                return 0; 
    23322338        } 
     
    25192525        struct emu *e = emu_new(); 
    25202526        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 
    25212537                test(e); 
    2522          
     2538        } 
    25232539 
    25242540        emu_free(e);