CompiledProgram::CompiledProgram(Program in_program) { mProgSize=0; for (Program::iterator i=in_program.begin(); i!=in_program.end(); i++) { mProgSize+=i->size(); } mpProg=VirtualAlloc( NULL, mProgSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE ); if (mpProg == NULL ) exit(0); size_t progPos=0; for (Program::iterator i=in_program.begin(); i!=in_program.end(); i++) { memcpy((unsigned char*) mpProg+progPos, &(*i)[0], i->size()); progPos+=i->size(); } DWORD flOldProtect; if (!VirtualProtect(mpProg, size(), PAGE_EXECUTE, &flOldProtect)) exit(0); if (!FlushInstructionCache(GetCurrentProcess(), mpProg, size())) exit(0); }
Program MutationCache::mutateProgram(const Program& program, Mutation mutation) { auto start = program.begin(); auto programLen = program.size(); auto instruction = getMutationInstruction(mutation); auto position = getMutationPosition(mutation); switch(getMutationType(mutation)) { case addition: { Program result(start, start + position); result.push_back(instruction); result.insert(result.end(), start + position, program.end()); return result; } break; case removal: { Program result(start, start + position); result.insert(result.end(), start + position + 1, program.end()); return result; } break; case substitution: { Program result = program; result[position] = (result[position] + instruction) % instructionCount; return result; } default: throw; } }