initialize cs_insn.detail by properly zero-out right members for each arch

This commit is contained in:
Nguyen Anh Quynh 2014-06-09 17:50:01 +07:00
parent 29fd0f6405
commit 69582d71ae
10 changed files with 56 additions and 10 deletions

View File

@ -22,6 +22,7 @@
#include <stdlib.h>
#include "../../cs_priv.h"
#include "../../utils.h"
#include "../../MCInst.h"
#include "../../MCInstrDesc.h"
@ -271,6 +272,10 @@ static DecodeStatus _getInstruction(cs_struct *ud, MCInst *MI,
return MCDisassembler_Fail;
}
if (MI->flat_insn->detail) {
memset(&MI->flat_insn->detail->arm64, 0, offset_of(cs_arm64, operands));
}
if (ud->big_endian)
insn = (code[3] << 0) | (code[2] << 8) |
(code[1] << 16) | (code[0] << 24);

View File

@ -26,6 +26,7 @@
#include "../../LEB128.h"
#include "../../MCDisassembler.h"
#include "../../cs_priv.h"
#include "../../utils.h"
#include "ARMDisassembler.h"
@ -443,12 +444,16 @@ static DecodeStatus _ARM_getInstruction(cs_struct *ud, MCInst *MI, const uint8_t
uint8_t bytes[4];
DecodeStatus result;
ud->ITBlock.size = 0;
if (code_len < 4)
// not enough data
return MCDisassembler_Fail;
ud->ITBlock.size = 0;
if (MI->flat_insn->detail) {
memset(&MI->flat_insn->detail->arm, 0, offset_of(cs_arm, operands));
}
memcpy(bytes, code, 4);
if (ud->big_endian)
@ -679,13 +684,18 @@ static DecodeStatus _Thumb_getInstruction(cs_struct *ud, MCInst *MI, const uint8
unsigned Firstcond, Mask;
uint32_t NEONLdStInsn, insn32, NEONDataInsn, NEONCryptoInsn, NEONv8Insn;
ud->ITBlock.size = 0;
// We want to read exactly 2 bytes of data.
if (code_len < 2)
// not enough data
return MCDisassembler_Fail;
ud->ITBlock.size = 0;
if (MI->flat_insn->detail) {
memset(&MI->flat_insn->detail->arm, 0, offset_of(cs_arm, operands));
//MI->flat_insn->detail->arm.op_count = 0;
}
memcpy(bytes, code, 2);
if (ud->big_endian)

View File

@ -254,6 +254,9 @@ static DecodeStatus MipsDisassembler_getInstruction(int mode, MCInst *instr,
// not enough data
return MCDisassembler_Fail;
if (instr->flat_insn->detail)
instr->flat_insn->detail->mips.op_count = 0;
Result = readInstruction32((unsigned char*)code, &Insn, isBigEndian,
mode & CS_MODE_MICRO);
if (Result == MCDisassembler_Fail)
@ -303,6 +306,9 @@ static DecodeStatus Mips64Disassembler_getInstruction(int mode, MCInst *instr,
if (Result == MCDisassembler_Fail)
return MCDisassembler_Fail;
if (instr->flat_insn->detail)
instr->flat_insn->detail->mips.op_count = 0;
// Calling the auto-generated decoder function.
Result = decodeInstruction(DecoderTableMips6432, instr, Insn, Address, MRI, mode);
if (Result != MCDisassembler_Fail) {

View File

@ -14,8 +14,10 @@
#include <stdio.h> // DEBUG
#include <stdlib.h>
#include <string.h>
#include "../../cs_priv.h"
#include "../../utils.h"
#include "../../MCInst.h"
#include "../../MCInstrDesc.h"
@ -289,6 +291,10 @@ static DecodeStatus getInstruction(MCInst *MI,
insn = (code[3] << 24) | (code[2] << 16) |
(code[1] << 8) | (code[0] << 0);
if (MI->flat_insn->detail) {
memset(&MI->flat_insn->detail->ppc, 0, offset_of(cs_ppc, operands));
}
result = decodeInstruction_4(DecoderTable32, MI, insn, Address, 4);
if (result != MCDisassembler_Fail) {
*Size = 4;

View File

@ -17,6 +17,7 @@
#include <string.h>
#include "../../cs_priv.h"
#include "../../utils.h"
#include "../../MCInst.h"
#include "../../MCInstrDesc.h"
@ -230,6 +231,10 @@ bool Sparc_getInstruction(csh ud, const uint8_t *code, size_t code_len, MCInst *
if (Result == MCDisassembler_Fail)
return MCDisassembler_Fail;
if (MI->flat_insn->detail) {
memset(&MI->flat_insn->detail->sparc, 0, offset_of(cs_sparc, operands));
}
Result = decodeInstruction_4(DecoderTableSparc32, MI, Insn, address,
(MCRegisterInfo *)info, 0);
if (Result != MCDisassembler_Fail) {

View File

@ -17,6 +17,7 @@
#include <string.h>
#include "../../cs_priv.h"
#include "../../utils.h"
#include "../../MCInst.h"
#include "../../MCInstrDesc.h"
@ -320,6 +321,10 @@ bool SystemZ_getInstruction(csh ud, const uint8_t *code, size_t code_len, MCInst
// short of input data
return MCDisassembler_Fail;
if (MI->flat_insn->detail) {
memset(&MI->flat_insn->detail->sysz, 0, offset_of(cs_sysz, operands));
}
memcpy(Bytes, code, *size);
// Construct the instruction.

View File

@ -28,6 +28,7 @@
#include "X86DisassemblerDecoderCommon.h"
#include "X86DisassemblerDecoder.h"
#include "../../MCInst.h"
#include "../../utils.h"
#include "X86Mapping.h"
#define GET_REGINFO_ENUM
@ -691,8 +692,6 @@ static void update_pub_insn(cs_insn *pub, InternalInstruction *inter, uint8_t *p
pub->detail->x86.sib_base = x86_map_sib_base(inter->sibBase);
}
#define offsetof(st, member) __builtin_offsetof(st, member)
// Public interface for the disassembler
bool X86_getInstruction(csh ud, const uint8_t *code, size_t code_len,
MCInst *instr, uint16_t *size, uint64_t address, void *_info)
@ -707,7 +706,13 @@ bool X86_getInstruction(csh ud, const uint8_t *code, size_t code_len,
info.size = code_len;
info.offset = address;
memset(&insn, 0, offsetof(InternalInstruction, reader));
memset(&insn, 0, offset_of(InternalInstruction, reader));
if (instr->flat_insn->detail) {
instr->flat_insn->detail->x86.op_count = 0;
memset(instr->flat_insn->detail->x86.prefix, 0, sizeof(instr->flat_insn->detail->x86.prefix));
memset(instr->flat_insn->detail->x86.operands, 0, 4 * sizeof(instr->flat_insn->detail->x86.operands[0]));
}
if (handle->mode & CS_MODE_16)
ret = decodeInstruction(&insn,

View File

@ -17,6 +17,7 @@
#include <string.h>
#include "../../cs_priv.h"
#include "../../utils.h"
#include "../../MCInst.h"
#include "../../MCInstrDesc.h"
@ -736,6 +737,10 @@ bool XCore_getInstruction(csh ud, const uint8_t *code, size_t code_len, MCInst *
return MCDisassembler_Fail;
}
if (MI->flat_insn->detail) {
memset(&MI->flat_insn->detail->xcore, 0, offset_of(cs_xcore, operands));
}
// Calling the auto-generated decoder function.
Result = decodeInstruction_2(DecoderTable16, MI, insn16, address, info, 0);
if (Result != MCDisassembler_Fail) {

3
cs.c
View File

@ -459,9 +459,6 @@ size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset,
if (handle->detail) {
// allocate memory for @detail pointer
insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
insn_cache->detail->x86.op_count = 0;
memset(insn_cache->detail->x86.prefix, 0, sizeof(insn_cache->detail->x86.prefix));
memset(insn_cache->detail->x86.operands, 0, sizeof(insn_cache->detail->x86.operands));
} else {
insn_cache->detail = NULL;
}

View File

@ -47,5 +47,7 @@ char *cs_strdup(const char *str);
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define offset_of(st, member) __builtin_offsetof(st, member)
#endif