inline void update_eflag_setnrs(instr_t *instr, t_glob_reg_state *glob_reg_state){ uint flags = instr_get_eflags(instr); uint read_masks[MY_NUM_EFLAGS] = { EFLAGS_READ_CF, EFLAGS_READ_PF, EFLAGS_READ_AF, EFLAGS_READ_ZF, EFLAGS_READ_SF, EFLAGS_READ_TF, EFLAGS_READ_IF, EFLAGS_READ_DF, EFLAGS_READ_OF, EFLAGS_READ_NT, EFLAGS_READ_RF }; uint write_masks[MY_NUM_EFLAGS] = { EFLAGS_WRITE_CF, EFLAGS_WRITE_PF, EFLAGS_WRITE_AF, EFLAGS_WRITE_ZF, EFLAGS_WRITE_SF, EFLAGS_WRITE_TF, EFLAGS_WRITE_IF, EFLAGS_WRITE_DF, EFLAGS_WRITE_OF, EFLAGS_WRITE_NT, EFLAGS_WRITE_RF }; int i; for(i = 0; i < MY_NUM_EFLAGS; i++){ if(TESTALL(read_masks[i], flags)){ //determine set number for rule 1: RaW (WRITTEN TO + 1) glob_reg_state->raw_setnr = MAX(glob_reg_state->raw_setnr, glob_reg_state->my_writtento[MY_EFLAGS_OFFSET+i]+1); } if(TESTALL(write_masks[i], flags)){ //determine set number for rule 2: WaR (READ FROM + 1) glob_reg_state->war_setnr = MAX(glob_reg_state->war_setnr, glob_reg_state->my_readfrom[MY_EFLAGS_OFFSET+i]+1); //determine set number for rule 3: WaW (WRITTEN TO + 1) glob_reg_state->waw_setnr = MAX(glob_reg_state->waw_setnr, glob_reg_state->my_writtento[MY_EFLAGS_OFFSET+i]+1); } } }
/* replaces inc with add 1, dec with sub 1 * returns true if successful, false if not */ static bool replace_inc_with_add(void *drcontext, instr_t *instr, instrlist_t *trace) { instr_t *in; uint eflags; int opcode = instr_get_opcode(instr); bool ok_to_replace = false; DR_ASSERT(opcode == OP_inc || opcode == OP_dec); #ifdef VERBOSE dr_print_instr(drcontext, STDOUT, instr, "in replace_inc_with_add:\n\t"); #endif /* add/sub writes CF, inc/dec does not, make sure that's ok */ for (in = instr; in != NULL; in = instr_get_next(in)) { eflags = instr_get_eflags(in); if ((eflags & EFLAGS_READ_CF) != 0) { #ifdef VERBOSE dr_print_instr(drcontext, STDOUT, in, "\treads CF => cannot replace inc with add: "); #endif return false; } if (instr_is_exit_cti(in)) { /* to be more sophisticated, examine instructions at * target of exit cti (if it is a direct branch). * for this example, we give up if we hit a branch. */ return false; } /* if writes but doesn't read, ok */ if ((eflags & EFLAGS_WRITE_CF) != 0) { ok_to_replace = true; break; } } if (!ok_to_replace) { #ifdef VERBOSE dr_printf("\tno write to CF => cannot replace inc with add\n"); #endif return false; } if (opcode == OP_inc) { #ifdef VERBOSE dr_printf("\treplacing inc with add\n"); #endif in = INSTR_CREATE_add(drcontext, instr_get_dst(instr, 0), OPND_CREATE_INT8(1)); } else { #ifdef VERBOSE dr_printf("\treplacing dec with sub\n"); #endif in = INSTR_CREATE_sub(drcontext, instr_get_dst(instr, 0), OPND_CREATE_INT8(1)); } if (instr_get_prefix_flag(instr, PREFIX_LOCK)) instr_set_prefix_flag(in, PREFIX_LOCK); instr_set_translation(in, instr_get_app_pc(instr)); instrlist_replace(trace, instr, in); instr_destroy(drcontext, instr); return true; }