int darm_disasm(darm_t *d, uint16_t w, uint16_t w2, uint32_t addr) { // if the least significant bit is not set, then this is // an ARMv7 instruction if((addr & 1) == 0) { // disassemble and check for error return values if(darm_armv7_disasm(d, (w2 << 16) | w) < 0) { return 0; } else { return 2; } } // magic table constructed based on section A6.1 of the ARM manual static uint8_t is_thumb2[0x20] = { [b11101] = 1, [b11110] = 1, [b11111] = 1, }; // check whether this is a Thumb or Thumb2 instruction if(is_thumb2[w >> 11] == 0) { // this is a Thumb instruction if(darm_thumb_disasm(d, w) < 0) { return 0; } else { return 1; } } // this is a Thumb2 instruction if(darm_thumb2_disasm(d, w, w2) < 0) { return 0; } else { return 2; } }
/** * Instruction Begin callback. */ void nd_instruction_begin_callback(DECAF_Callback_Params* params){ DEFENSIVE_CHECK0(params == NULL); DEFENSIVE_CHECK0(getCurrentPID() != ND_GLOBAL_TRACING_PID); CPUState* env = params->ib.env; gva_t cur_pc = params->ib.cur_pc; //since for thumb instruction, the last bit is '1' gva_t cur_pc_even = cur_pc & 0xfffffffe; if(!nd_in_blacklist(cur_pc_even)){ return; } //ARM Instruction union _tmpARMInsn{ target_ulong insn; char chars[4]; } tmpARMInsn; //Thumb Instruction union _tmpThumbInsn{ unsigned short insn; char chars[2]; } tmpThumbInsn; //Thumb2 Instruction union _tmpThumb2Insn{ target_ulong insn; char chars[4]; } tmpThumb2Insn; //undefined instruction if(cur_pc == -1){ return; } //the first instruction of target native method SourcePolicy* sourcePolicy = findSourcePolicy(cur_pc_even); if(sourcePolicy != NULL){ DECAF_printf("Step into Native\n"); sourcePolicy->handler(sourcePolicy, env); } //DECAF_printf("%x %x\n", cur_pc_even, lastCallSysLibAddrRet); //return from JNI API calls/system library calls if(cur_pc_even == lastCallJNIAddrRet){ if(lastJniHandler != NULL){ lastJniHandler(env, 0); lastJniHandler = NULL; lastCallJNIAddrRet = -1; } } if(cur_pc_even == lastCallSysLibAddrRet){ if(lastSysLibHandler != NULL){ lastSysLibHandler(env, 0); lastSysLibHandler = NULL; lastCallSysLibAddrRet = -1; } } //Thumb instruction if(env->thumb == 1){ if(DECAF_read_mem(env, cur_pc_even, tmpThumbInsn.chars, 2) != -1){ darm_t d; //darm_str_t str; // magic table constructed based on section A6.1 of the ARM manual static uint8_t is_thumb2[0x20] = { [0x01d] = 1, [0x01e] = 1, [0x01f] = 1, }; if(is_thumb2[tmpThumbInsn.insn >> 11]){ //Thumb2 instruction if(DECAF_read_mem(env, cur_pc_even, tmpThumb2Insn.chars, 4) != -1){ if(darm_thumb2_disasm(&d, tmpThumb2Insn.insn & 0x0000ffff, tmpThumb2Insn.insn >> 16, env) == 0){ //if(darm_str(&d, &str, env) == 0){ //DECAF_printf("T2 %x: %s\n", cur_pc, str.total); //} } } }else{ //Thumb instruction if(darm_thumb_disasm(&d, tmpThumbInsn.insn, env) == 0){ //if(darm_str(&d, &str, env) == 0){ //DECAF_printf("T %x: %s\n", cur_pc, str.total); //} } } }
/** * Instruction Begin callback. */ void nd_instruction_begin_callback(DECAF_Callback_Params* params){ DEFENSIVE_CHECK0(params == NULL); DEFENSIVE_CHECK0(getCurrentPID() != ND_GLOBAL_TRACING_PID); CPUState* env = params->ib.env; gva_t cur_pc = params->ib.cur_pc; //since for thumb instruction, the last bit is '1' gva_t cur_pc_even = cur_pc & 0xfffffffe; //ARM Instruction union _tmpARMInsn{ target_ulong insn; char chars[4]; } tmpARMInsn; //Thumb Instruction union _tmpThumbInsn{ unsigned short insn; char chars[2]; } tmpThumbInsn; //Thumb2 Instruction union _tmpThumb2Insn{ target_ulong insn; char chars[4]; } tmpThumb2Insn; //undefined instruction if(cur_pc == -1){ return; } //the first instruction of target native method SourcePolicy* sourcePolicy = findSourcePolicy(cur_pc_even); if(sourcePolicy != NULL){ sourcePolicy->handler(sourcePolicy, env); } //Thumb instruction if(env->thumb == 1){ if(DECAF_read_mem(env, cur_pc_even, tmpThumbInsn.chars, 2) != -1){ darm_t d; darm_str_t str; // magic table constructed based on section A6.1 of the ARM manual static uint8_t is_thumb2[0x20] = { [0x01d] = 1, [0x01e] = 1, [0x01f] = 1, }; if(is_thumb2[tmpThumbInsn.insn >> 11]){ //Thumb2 instruction if(DECAF_read_mem(env, cur_pc_even, tmpThumb2Insn.chars, 4) != -1){ if(darm_thumb2_disasm(&d, tmpThumb2Insn.insn >> 16, tmpThumb2Insn.insn & 0x0000ffff) == 0){ if(darm_str(&d, &str) == 0){ //DECAF_printf("T2 %x: %s\n", cur_pc, str.total); } } } }else{ //Thumb instruction if(darm_thumb_disasm(&d, tmpThumbInsn.insn) == 0){ if(darm_str(&d, &str) == 0){ //DECAF_printf("T %x: %s\n", cur_pc, str.total); } } } }