void initialize(int argc, char **argv) { SET_PROGNAME(); bi_vars_init(); /* load the builtin variables */ bi_funct_init(); /* load the builtin functions */ kw_init(); /* load the keywords */ field_init(); #if USE_BINMODE { char *p = getenv("MAWKBINMODE"); if (p) set_binmode(atoi(p)); } #endif process_cmdline(argc, argv); code_init(); fpe_init(); set_stdio(); #if USE_BINMODE stdout_init(); #endif }
Hsp3::Hsp3() { // 初期化 // memset( &hspctx, 0, sizeof(HSPCTX) ); code_setctx( &hspctx ); code_init(); hspctx.mem_mcs = NULL; axfile = NULL; axname = NULL; }
int main(void) { irqInit(); irqEnable(IRQ_VBLANK); code_init(); AgbMain(); // SoundDriverInit(); // SoundDriverMode(); while (1) { VBlankIntrWait(); // SoundDriverVsync(); } }
int code(int inst) { Inst *new_machine; int i; if (!machine || !machine_size) { code_init(); } if (progi >= machine_size) { new_machine = MALLOC(Inst, machine_size *= 2); for (i = 0; i < progi; i++) { new_machine[i] = machine[i]; } FREE(machine); machine = new_machine; } machine[progi++] = inst; return progi - 1; }
/* traverse page by page */ void findReadOnlyPages(Mem * mem) { int i; int pageSize = 4 * 1024; //4k int totalPageNumber = mem->mem_size / (4 * 1024); //assume that every page has 4k int calledPages[totalPageNumber]; int dsmPages[totalPageNumber]; //record virtual address unsigned virtualAddrs[totalPageNumber]; for (i = 0; i < totalPageNumber; i++) { calledPages[i] = 0; dsmPages[i] = 0; virtualAddrs[i] = 0; } unsigned cr3Pages[100]; for (i = 0; i < 100; i++) { cr3Pages[i] = 0; } //start address unsigned startVirtualAddr = 0x80000000; //step 1. kdi int kdi_time =0; int allPages=0; int cluster_index= kdi(startVirtualAddr, mem,pageSize,cr3Pages, &kdi_time, &allPages); //step 2. signature generation struct timeval earlier; struct timeval later; if (gettimeofday(&earlier, NULL)) { perror("gettimeofday() error"); exit(1); } int cr3ClusterNo = 0; int cr3PageNo = 0; ranges[0].end = 0; //find the cluster which has max cr3 number int maxcr3Index = findClusterHasMaxCr3(cluster_index, clusters, cr3Pages, &cr3ClusterNo); if (maxcr3Index == -1) { puts("Cannot find clusters have cr3."); // return ; } unsigned codePageNo = 0; newstart = 1; unsigned vAddr; for (vAddr = clusters[maxcr3Index].start; vAddr < clusters[maxcr3Index].end; vAddr += 0x1000) { cr3PageNo++; unsigned pAddr = vtop(mem->mem, mem->mem_size, mem->pgd, vAddr); if (vAddr == out_pc) code_init(mem, vAddr, pageSize, dsmPages, virtualAddrs, 1, calledPages, &codePageNo); else code_init(mem, vAddr, pageSize, dsmPages, virtualAddrs, 0, calledPages, &codePageNo); } ranges[range_index].end = clusters[maxcr3Index].end; ranges[range_index].len = ranges[range_index].end - ranges[range_index].start; //find the max range int max_len = 0, max_index = 0; for (i = 1; i <= range_index; i++) { if (containKernelAddresForRange(ranges[i], cr3Pages) != 0) { continue; } printf("start:%x, end:%x: len:%x kernel\n", ranges[i].start, ranges[i].end, ranges[i].len); if (ranges[i].len > max_len) { max_index = i; max_len = ranges[i].len; } } unsigned pAddr = vtop(mem->mem, mem->mem_size, mem->pgd, ranges[max_index].start); int pageIndex = pAddr / pageSize; char *page = (char*) ((unsigned) mem->mem + pAddr); code_preprocess(mem, page, ranges[max_index].len, 0x1000, ranges + max_index, dsmPages + pageIndex); printf("step2: cluster: %d\n", range_index); //print md5 of pages that can be disassembled startVirtualAddr = ranges[max_index].start; unsigned disasPageNo = 0; unsigned totalPageNo = 0; for (; startVirtualAddr <= ranges[max_index].end; startVirtualAddr += 0x1000) { totalPageNo++; unsigned pAddr = vtop(mem->mem, mem->mem_size, mem->pgd, startVirtualAddr); if (pAddr == -1 || pAddr > mem->mem_size) continue; int pageIndex = pAddr / pageSize; if (dsmPages[pageIndex] == 1) { unsigned offset = startVirtualAddr - ranges[max_index].start; void *startAdress = (void*) ((unsigned) mem->mem + pageIndex * pageSize); genMd5WithOffset(startAdress, pageSize, startVirtualAddr, offset); disasPageNo++; } } if (gettimeofday(&later, NULL)) { perror("gettimeofday() error"); exit(1); } int sigGen_time = timeval_diff(NULL, &later, &earlier) / 1000; printf("step2: time cost is %d milliseconds\n", sigGen_time); float byerate = (float) ranges[max_index].disasBytes / (4096 * disasPageNo); printf("Success pages: %u/%u disassembled bytes rate: %f, page rate: %f\n", disasPageNo, totalPageNo, (float) ranges[max_index].disasBytes / (4096 * disasPageNo), (float) disasPageNo / totalPageNo); //record data; recordData(allPages, cluster_index, cr3ClusterNo, cr3PageNo, totalPageNo, disasPageNo, byerate); //begin match int match_time = 0; sigMatch(ranges[max_index], mem, pageSize, dsmPages, &match_time); //record performance recordPerformance(kdi_time, sigGen_time, match_time); return; }
/*************************************************************************** Initialize the emulated machine (load the roms, initialize the various subsystems...). Returns 0 if successful. ***************************************************************************/ int init_machine(void) { int i; /* LBO 042400 start */ if (uistring_init (options.language_file) != 0) goto out; /* LBO 042400 end */ if (code_init() != 0) goto out; for (i = 0;i < MAX_MEMORY_REGIONS;i++) { Machine->memory_region[i] = 0; Machine->memory_region_length[i] = 0; Machine->memory_region_type[i] = 0; } if (gamedrv->input_ports) { Machine->input_ports = input_port_allocate(gamedrv->input_ports); if (!Machine->input_ports) goto out_code; Machine->input_ports_default = input_port_allocate(gamedrv->input_ports); if (!Machine->input_ports_default) { input_port_free(Machine->input_ports); Machine->input_ports = 0; goto out_code; } } #ifdef MESS if (!gamedrv->rom) { logerror("Going to load_next tag\n"); goto load_next; } #endif if (readroms() != 0) goto out_free; #ifdef MESS load_next: if (init_devices(gamedrv)) goto out_free; #endif /* Mish: Multi-session safety - set spriteram size to zero before memory map is set up */ spriteram_size=spriteram_2_size=0; /* first of all initialize the memory handlers, which could be used by the */ /* other initialization routines */ cpu_init(); /* load input ports settings (keys, dip switches, and so on) */ settingsloaded = load_input_port_settings(); if( !memory_init() ) goto out_free; if (gamedrv->driver_init) (*gamedrv->driver_init)(); return 0; out_free: input_port_free(Machine->input_ports); Machine->input_ports = 0; input_port_free(Machine->input_ports_default); Machine->input_ports_default = 0; out_code: code_close(); out: return 1; }
void eval_block(code *block, map* vars) { while (true) { code_skip_whitespace(block); if (block->source[block->pos] == '?') { uint8_t brackets = 0; size_t start, length = 0; mpz_t value; mpz_init(value); block->pos++; code_skip_whitespace(block); _parse_value(block, value, vars); code_skip_whitespace(block); start = block->pos + 1; while (true) { length++; if (block->source[block->pos] == '{') { brackets++; block->pos++; } else if (block->source[block->pos] == '}') { brackets--; block->pos++; if (brackets == 0) break; } else { block->pos++; } } if (mpz_sgn(value) == 0) { code subblock; code_init(&subblock); code_append(&subblock, block->source + start * sizeof(char), length - 2); eval_block(&subblock, vars); code_free(&subblock); } mpz_clear(value); } else if (block->source[block->pos] == '!') { mpz_t value; mpz_init(value); block->pos++; code_skip_whitespace(block); _parse_value(block, value, vars); mpz_out_str(stdout, 10, value); printf("\n"); mpz_clear(value); } else { char *var = malloc(1024 * sizeof(char)); mpz_t value; mpz_init(value); _parse_variable_name(block, var); code_skip_whitespace(block); switch (block->source[block->pos]) { case '=': block->pos++; code_skip_whitespace(block); _parse_value(block, value, vars); map_set(vars, var, value, false); break; case '+': block->pos += 2; code_skip_whitespace(block); _parse_value(block, value, vars); map_set(vars, var, value, true); break; case '-': block->pos += 2; code_skip_whitespace(block); _parse_value(block, value, vars); mpz_neg(value, value); map_set(vars, var, value, true); break; default: fprintf(stderr, "Parse error\n"); } free(var); mpz_clear(value); } code_skip_whitespace(block); if (block->pos >= block->length) return; if (block->source[block->pos] == ';') { block->pos++; } else { fprintf(stderr, "Missing ;\n"); return; } } }
/* determine version of OS by mem * 1.To get signature of multiply versions of os, which is the md5 of kernel code * 2.Compared by a decision tree. * 3.Done!*/ void determineOsVersion(Mem * mem) { int i; int pageSize = 4 * 1024; //4k int totalPageNumber = mem->mem_size / (4 * 1024); //assume that every page has 4k unsigned codePageNo = 0; //record when two page have different page index and the same sharp int calledPages[totalPageNumber]; int dsmPages[totalPageNumber]; //record virtual address unsigned virtualAddrs[totalPageNumber]; for (i = 0; i < totalPageNumber; i++) { calledPages[i] = 0; dsmPages[i] = 0; virtualAddrs[i] = 0; } //start address unsigned startVirtualAddr = KERNEL_START_ADDRESS; //with dissemble or not int withDissemble = 1; int cr3PageIndex = 0; unsigned cr3Pages[20]; for (i = 0; i < 20; i++) { cr3Pages[i] = 0; } struct timeval earlier; struct timeval later; if (gettimeofday(&earlier, NULL)) { perror("gettimeofday() error"); exit(1); } //generate step 1 clusters clusters[0].end = 0; int pre_rw = -1; //read or write int pre_us = -1; //use or system int pre_g = -1; //global, no move out of TLB int pre_ps = -1; //page size unsigned cluster_index = 0; newstart = 1; unsigned vAddr = startVirtualAddr; for (; vAddr > KERNEL_START_ADDRESS - 1; vAddr += 0x1000) { //printf("startvirtual %x:\n",startVirtualAddr); int rw = 0; //read or write int us = 0; //use or system int g = 0; //global, no move out of TLB int ps = 0; //page size 4M or 4k unsigned pAddr = vtopPageProperty(mem->mem, mem->mem_size, mem->pgd, vAddr, &rw, &us, &g, &ps); //if PHYSICAL ADDRESS is not VALID, then start a new cluster if (pAddr < 0 || pAddr > mem->mem_size || us != 0 || g != 256) { if (newstart == 0) { clusters[cluster_index].end = vAddr - 1; //printf("err address end is %x %x\n", vAddr, ranges[range_index].end); newstart = 1; } continue; } //if any property changes, then start a new cluster if (rw != pre_rw || us != pre_us || g != pre_g || ps != pre_ps) { if (newstart == 0) { clusters[cluster_index].end = vAddr - 1; //printf("property change end is %x %x\n", vAddr, ranges[range_index].end); newstart = 1; } } //update pre properties pre_rw = rw; pre_us = us; pre_g = g; pre_ps = ps; //collect pages with continuous properties; if (newstart) { clusters[++cluster_index].start = vAddr; clusters[cluster_index].end = vAddr + pageSize - 1; newstart = 0; } else clusters[cluster_index].end = vAddr + pageSize - 1; } if (gettimeofday(&later, NULL)) { perror("gettimeofday() error"); exit(1); } printf("step1, cluster: %d,time cost is %d milliseconds\n", cluster_index, timeval_diff(NULL, &later, &earlier) / 1000); //step2. kernel code page clusters with cr3 if (gettimeofday(&earlier, NULL)) { perror("gettimeofday() error"); exit(1); } unsigned startVirtual = startVirtualAddr; for (; startVirtual > startVirtualAddr - 1; startVirtual += 0x1000) { // for (; startVirtual < 0x818f0000; startVirtual += 0x1000) { unsigned vAddr = startVirtual; int rw = 0; //read or write int us = 0; //use or system int g = 0; //global(no move out of TLB) or not global int ps = 0; //page size unsigned pAddr = vtopPageProperty(mem->mem, mem->mem_size, mem->pgd, vAddr, &rw, &us, &g, &ps); // IS PHYSICAL ADDRESS VALID? if (pAddr == -1 || pAddr > mem->mem_size) continue; //collect pages which are system access, and global pages if (us == 0 && g == 256) { // printf("r only page %x\n", vAddr); if (find_kernel(mem, vAddr, pageSize) == 0) { //record kernel address cr3Pages[cr3PageIndex++] = vAddr; printf("kernel start at %x\n", vAddr); } } } if (gettimeofday(&later, NULL)) { perror("gettimeofday() error"); exit(1); } printf("step2 time cost is %d milliseconds\n", timeval_diff(NULL, &later, &earlier) / 1000); //step 3. clusters if (gettimeofday(&earlier, NULL)) { perror("gettimeofday() error"); exit(1); } int cr3PagesNo = 0; ranges[0].end = 0; newstart = 1; for (i = 1; i <= cluster_index; i++) { //:w printf("%x %x\n", clusters[i].start, clusters[i].end); if (containKernelAddres(clusters[i], cr3Pages) == -1) { continue; } cr3PagesNo++; unsigned vAddr = clusters[i].start; // printf("%x %x\n", clusters[i].start, clusters[i].end); newstart = 1; for (; vAddr < clusters[i].end; vAddr += 0x1000) { unsigned pAddr = vtop(mem->mem, mem->mem_size, mem->pgd, vAddr); if (vAddr == out_pc) code_init(mem, vAddr, pageSize, dsmPages, virtualAddrs, 1, calledPages, &codePageNo); else code_init(mem, vAddr, pageSize, dsmPages, virtualAddrs, 0, calledPages, &codePageNo); } ranges[range_index].end = clusters[i].end; } if (gettimeofday(&later, NULL)) { perror("gettimeofday() error"); exit(1); } printf("step2, cluster: %d\n", cr3PagesNo); printf("step3, cluster: %d,time cost is %d milliseconds\n", range_index, timeval_diff(NULL, &later, &earlier) / 1000); //3.find the kernel core code page cluster, and print it int osNumber = initDb(); if (gettimeofday(&earlier, NULL)) { perror("gettimeofday() error"); exit(1); } int max_len = 0, max_index = 0; for (i = 1; i <= range_index; i++) { // printf("start:%x, end:%x: len:%x kernel\n", ranges[i].start, ranges[i].end, ranges[i].len); if (ranges[i].len > max_len) { max_index = i; max_len = ranges[i].len; } } //4.print md5 of pages that can be disassembled int availableOs[FINGERPRINT_NO], matchCounts[FINGERPRINT_NO]; for (i = 0; i < FINGERPRINT_NO; i++) { availableOs[i] = 1; matchCounts[i] = 0; } startVirtualAddr = ranges[max_index].start; unsigned disasPageNo = 0; unsigned totalPageNo = 0; for (; startVirtualAddr <= ranges[max_index].end; startVirtualAddr += 0x1000) { totalPageNo++; unsigned pAddr = vtop(mem->mem, mem->mem_size, mem->pgd, startVirtualAddr); if (pAddr == -1 || pAddr > mem->mem_size) continue; int pageIndex = pAddr / pageSize; if (dsmPages[pageIndex] == 1) { int offset = (startVirtualAddr - ranges[max_index].start) / 4096; void *startAdress = (void *) ((unsigned) mem->mem + pageIndex * pageSize); unsigned char md5digest[16]; MDMem(startAdress, pageSize, md5digest); // printf("%x ", vaddr); //print vaddr MDPrint(md5digest); printf("\n"); //search hash table int ret = matchByIndex(osNumber, md5digest, offset, availableOs, matchCounts); // genMd5WithOffset(startAdress, pageSize, startVirtualAddr, offset); disasPageNo++; } } if (gettimeofday(&later, NULL)) { perror("gettimeofday() error"); exit(1); } printf("step4.time cost is %d milliseconds\n", timeval_diff(NULL, &later, &earlier) / 1000); int maxIndex = -1; int maxMatch = 0; for (i = 0; i < FINGERPRINT_NO; i++) { if (matchCounts[i] > maxMatch) { maxIndex = i; maxMatch = matchCounts[i]; } } if (maxMatch > 0) printf("Os is %s, match count is %d\n", fingerprints[maxIndex].osVersion, maxMatch); else puts("Unknown OS!"); return; }
static void init_machine(running_machine *machine) { mame_private *mame = machine->mame_data; int num; /* initialize basic can't-fail systems here */ cpuintrf_init(machine); sndintrf_init(machine); fileio_init(machine); config_init(machine); output_init(machine); state_init(machine); state_save_allow_registration(TRUE); drawgfx_init(machine); palette_init(machine); render_init(machine); ui_init(machine); generic_machine_init(machine); generic_video_init(machine); mame->rand_seed = 0x9d14abd7; /* initialize the base time (if not doing record/playback) */ if (!Machine->record_file && !Machine->playback_file) time(&mame->base_time); else mame->base_time = 0; /* init the osd layer */ if (osd_init(machine) != 0) fatalerror("osd_init failed"); /* initialize the input system */ /* this must be done before the input ports are initialized */ if (code_init(machine) != 0) fatalerror("code_init failed"); /* initialize the input ports for the game */ /* this must be done before memory_init in order to allow specifying */ /* callbacks based on input port tags */ if (input_port_init(machine, machine->gamedrv->ipt) != 0) fatalerror("input_port_init failed"); /* load the ROMs if we have some */ /* this must be done before memory_init in order to allocate memory regions */ rom_init(machine, machine->gamedrv->rom); /* initialize the timers and allocate a soft_reset timer */ /* this must be done before cpu_init so that CPU's can allocate timers */ timer_init(machine); mame->soft_reset_timer = timer_alloc(soft_reset); /* initialize the memory system for this game */ /* this must be done before cpu_init so that set_context can look up the opcode base */ if (memory_init(machine) != 0) fatalerror("memory_init failed"); /* now set up all the CPUs */ if (cpuexec_init(machine) != 0) fatalerror("cpuexec_init failed"); if (cpuint_init(machine) != 0) fatalerror("cpuint_init failed"); #ifdef MESS /* initialize the devices */ devices_init(machine); #endif /* start the save/load system */ saveload_init(machine); /* call the game driver's init function */ /* this is where decryption is done and memory maps are altered */ /* so this location in the init order is important */ ui_set_startup_text("Initializing...", TRUE); if (machine->gamedrv->driver_init != NULL) (*machine->gamedrv->driver_init)(machine); /* start the audio system */ if (sound_init(machine) != 0) fatalerror("sound_init failed"); /* start the video hardware */ if (video_init(machine) != 0) fatalerror("video_init failed"); /* start the cheat engine */ if (options.cheat) cheat_init(machine); /* call the driver's _START callbacks */ if (machine->drv->machine_start != NULL && (*machine->drv->machine_start)(machine) != 0) fatalerror("Unable to start machine emulation"); if (machine->drv->sound_start != NULL && (*machine->drv->sound_start)(machine) != 0) fatalerror("Unable to start sound emulation"); if (machine->drv->video_start != NULL && (*machine->drv->video_start)(machine) != 0) fatalerror("Unable to start video emulation"); /* free memory regions allocated with REGIONFLAG_DISPOSE (typically gfx roms) */ for (num = 0; num < MAX_MEMORY_REGIONS; num++) if (mame->mem_region[num].flags & ROMREGION_DISPOSE) free_memory_region(machine, num); #ifdef MAME_DEBUG /* initialize the debugger */ if (machine->debug_mode) mame_debug_init(machine); #endif }
int main(int argc,char **argv) { int binary = 0,hex = 0; const char *output = NULL; const char *symbols = NULL; const char *flash_security = NULL; int c,i; error_init(); id_init(); code_init(); cpp_option = alloc_type_n(char *,argc*2); while ((c = getopt(argc,argv,"bef:hD:I:m:o:U:V")) != EOF) { char opt[] = "-?"; switch (c) { case 'b': binary = 1; break; case 'e': allow_extensions = 1; break; case 'f': if (flash_security) usage(*argv); flash_security = optarg; break; case 'h': hex = 1; break; case 'o': if (output) usage(*argv); output = optarg; break; case 'm': symbols = optarg; break; case 'D': case 'I': case 'U': opt[1] = c; cpp_option[cpp_options*2] = stralloc(opt); cpp_option[cpp_options*2+1] = stralloc(optarg); cpp_options++; break; case 'V': printf("m8cas from m8cutils version %s\n",VERSION); exit(0); default: usage(*argv); } } if (binary && hex) usage(*argv); if (cpp_options && !allow_extensions) { fprintf(stderr,"CPP options are only supported if using CPP (-e)\n"); return 1; } read_protection(flash_security); /* move stdin to a safe place, because we may open other files before */ fd0 = dup(0); if (fd0 < 0) { perror("dup"); exit(1); } (void) close(0); if (optind == argc) do_file(NULL); else { for (i = optind; i != argc; i++) do_file(argv[i]); } resolve(); if (symbols) write_symbols(symbols); program_size = text->highest_pc; id_cleanup(); code_cleanup(); error_cleanup(); if (!hex) for (i = 0; i != security_size; i++) if (security[i]) { fprintf(stderr, "output must be Intel HEX for non-zero flash protection\n"); exit(1); } write_file(output ? output : "-",binary,hex); return 0; }