unsigned int designScorer::getDesignSizeInGates(Function* F) { unsigned int totalGateSize = 0; map<string, unsigned int> resourceMap = machineResourceConfig::getResourceTable(); unsigned int mul_count = resourceMap["mul"]; unsigned int div_count = resourceMap["div"]; unsigned int shl_count = resourceMap["shl"]; totalGateSize += mul_count*1088 + div_count*1500 + shl_count*1000; for (inst_iterator i = inst_begin(*F), e = inst_end(*F); i != e; ++i) { totalGateSize += getInstructionSize(&*i); } return totalGateSize; }
static bool patchSearch(Oracle *oracle, const patchStrategy &input, patchQueueT &thingsToTry) { if (input.MustInterpret.empty()) return true; if (debug_declobber_instructions) input.prettyPrint(stdout); unsigned long needed = *input.MustInterpret.begin(); if (debug_declobber_instructions) printf("\tLook at %lx\n", needed); patchStrategy c(input); /* @needed is definitely going to be interpreted after * this. */ c.Cont.insert(needed); c.MustInterpret.erase(needed); /* Decide which maneuver to use here. We need to either patch @needed itself or bring all of its predecessors into the patch. */ /* Figure out which instructions might get clobbered by the * patch */ std::set<unsigned long> clobbered_by_patch; unsigned offset = 0; offset += getInstructionSize(oracle->ms->addressSpace, StaticRip(needed)); while (offset < 5) { clobbered_by_patch.insert(needed + offset); offset += getInstructionSize(oracle->ms->addressSpace, StaticRip(needed + offset)); } /* Can't use patch if that would clobber an external. */ bool can_use_patch = true; for (auto it = clobbered_by_patch.begin(); can_use_patch && it != clobbered_by_patch.end(); it++) { if (oracle->isFunctionHead(StaticRip(*it))) can_use_patch = false; } /* Can't use patch if that would clobber/be clobbered by an existing patch point. */ for (auto it = input.Patch.begin(); can_use_patch && it != input.Patch.end(); it++) { if (needed > *it - 5 && needed < *it + 5) can_use_patch = false; } if (can_use_patch) { /* Try using a patch. */ patchStrategy patched(c); patched.Patch.insert(needed); for (auto it = clobbered_by_patch.begin(); it != clobbered_by_patch.end(); it++) { patched.Cont.insert(*it); } for (auto it = clobbered_by_patch.begin(); it != clobbered_by_patch.end(); it++) { std::set<unsigned long> predecessors; oracle->findPredecessors(*it, predecessors); for (unsigned long y = needed; y < *it; y++) predecessors.erase(y); patched.Cont.insert(*it); patched.MustInterpret.erase(*it); patched.MustInterpret.insert(predecessors.begin(), predecessors.end()); } thingsToTry.push(patched); if (debug_declobber_instructions) { printf("Patch to: "); patched.prettyPrint(stdout); } } /* Try expanding to predecessors. */ std::set<unsigned long> predecessors; oracle->findPredecessors(needed, predecessors); c.MustInterpret.insert(predecessors.begin(), predecessors.end()); thingsToTry.push(c); if (debug_declobber_instructions) { printf("Unpatched: "); c.prettyPrint(stdout); } return false; }