// Return the input group with the specified ID, loading it from the database if necessary. static InputGroup * igroup(int igroup_id) { InputGroups::iterator found = igroup_cache.find(igroup_id); if (found!=igroup_cache.end()) return found->second; InputGroup *igroup = new InputGroup; if (!igroup->load(transaction, igroup_id)) { delete igroup; igroup_cache.insert(std::make_pair(igroup_id, (InputGroup*)0)); return NULL; } igroup_cache.insert(std::make_pair(igroup_id, igroup)); return igroup; }
void operator()() { // Database connections don't survive over fork() according to SqLite and PostgreSQL documentation, so open it again SqlDatabase::TransactionPtr tx = SqlDatabase::Connection::create(databaseUrl)->transaction(); // Use zero for the number of tests ran so that this child process doesn't try to update the semantic_history table. // If two or more processes try to change the same row (which they will if there's a non-zero number of tests) then // they will deadlock with each other. static const size_t NO_TESTS_RAN = 0; NameSet builtin_function_names; add_builtin_functions(builtin_function_names/*out*/); InputGroup igroup; WorkItem prevWorkItem; SgAsmInterpretation *prev_interp = NULL; MemoryMap ro_map; Disassembler::AddressSet whitelist_exports; // dynamic functions that should be called PointerDetectors pointers; InsnCoverage insn_coverage; DynamicCallGraph dynamic_cg; Tracer tracer; ConsumedInputs consumed_inputs; FuncAnalyses funcinfo; OutputGroups ogroups; // do not load from database (that might take a very long time) time_t last_checkpoint = time(NULL); for (size_t workIdx=0; workIdx<work.size(); ++workIdx) { WorkItem &workItem = work[workIdx]; // Load the input group from the database if necessary. if (workItem.igroup_id!=prevWorkItem.igroup_id) { if (!igroup.load(tx, workItem.igroup_id)) { std::cerr <<argv0 <<": input group " <<workItem.igroup_id <<" is empty or does not exist\n"; exit(1); } } // Find the function to test IdFunctionMap::iterator func_found = functions.find(workItem.func_id); assert(func_found!=functions.end()); SgAsmFunction *func = func_found->second; if (opt.verbosity>=LACONIC) { if (opt.verbosity>=EFFUSIVE) std::cerr <<argv0 <<": " <<std::string(100, '=') <<"\n"; std::cerr <<argv0 <<": processing function " <<function_to_str(func, function_ids) <<"\n"; } SgAsmInterpretation *interp = SageInterface::getEnclosingNode<SgAsmInterpretation>(func); assert(interp!=NULL); // Do per-interpretation stuff if (interp!=prev_interp) { prev_interp = interp; assert(interp->get_map()!=NULL); ro_map = *interp->get_map(); ro_map.require(MemoryMap::READABLE).prohibit(MemoryMap::WRITABLE).keep(); Disassembler::AddressSet whitelist_imports = get_import_addresses(interp, builtin_function_names); whitelist_exports.clear(); // imports are addresses of import table slots; exports are functions overmap_dynlink_addresses(interp, *insns, opt.params.follow_calls, &ro_map, GOTPLT_VALUE, whitelist_imports, whitelist_exports/*out*/); if (opt.verbosity>=EFFUSIVE) { std::cerr <<argv0 <<": memory map for SgAsmInterpretation:\n"; interp->get_map()->dump(std::cerr, argv0+": "); } } // Run the test assert(insns!=NULL); assert(entry2id!=NULL); std::cerr <<"process " <<getpid() <<" about to run test " <<workIdx <<"/" <<work.size() <<" " <<workItem <<"\n"; runOneTest(tx, workItem, pointers, func, function_ids, insn_coverage, dynamic_cg, tracer, consumed_inputs, interp, whitelist_exports, cmd_id, igroup, funcinfo, *insns, &ro_map, *entry2id, ogroups); ++ntests_ran; // Checkpoint if (opt.checkpoint>0 && time(NULL)-last_checkpoint > opt.checkpoint) { if (!opt.dry_run) tx = checkpoint(tx, ogroups, tracer, insn_coverage, dynamic_cg, consumed_inputs, NULL, NO_TESTS_RAN, cmd_id); last_checkpoint = time(NULL); } prevWorkItem = workItem; } std::cerr <<"process " <<getpid() <<" is done testing; now finishing up...\n"; if (!tx->is_terminated()) { SqlDatabase::StatementPtr stmt = tx->statement("insert into semantic_funcpartials" " (func_id, ncalls, nretused, ntests, nvoids) values" " (?, ?, ?, ?, ?)"); for (FuncAnalyses::iterator fi=funcinfo.begin(); fi!=funcinfo.end(); ++fi) { stmt->bind(0, fi->first); stmt->bind(1, fi->second.ncalls); stmt->bind(2, fi->second.nretused); stmt->bind(3, fi->second.ntests); stmt->bind(4, fi->second.nvoids); stmt->execute(); } } // Cleanup if (!tx->is_terminated() && !opt.dry_run) { std::cerr <<"process " <<getpid() <<" is doing the final checkpoint\n"; checkpoint(tx, ogroups, tracer, insn_coverage, dynamic_cg, consumed_inputs, NULL, NO_TESTS_RAN, cmd_id); } tx.reset(); std::cerr <<"process " <<getpid() <<" finished\n"; }