Skip to content

Commit

Permalink
moving static doing_mem variable into cs_struct to guarantee thread-s…
Browse files Browse the repository at this point in the history
…afe when handling memory operands
  • Loading branch information
aquynh committed Dec 31, 2013
1 parent f2d0ff2 commit 19b0de3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 30 deletions.
18 changes: 8 additions & 10 deletions arch/AArch64/AArch64InstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@
static char *getRegisterName(unsigned RegNo);
static void printOperand(MCInst *MI, unsigned OpNo, SStream *O);

// FIXME: make this status session's specific, not global like this
static bool doing_mem = false;
static void set_mem_access(MCInst *MI, bool status)
{
if (MI->csh->detail != CS_OPT_ON)
return;

doing_mem = status;
MI->csh->doing_mem = status;

if (doing_mem) {
if (status) {
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].type = ARM64_OP_MEM;
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].mem.base = ARM64_REG_INVALID;
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].mem.index = ARM64_REG_INVALID;
Expand Down Expand Up @@ -120,7 +118,7 @@ static void printAddrRegExtendOperand(MCInst *MI, unsigned OpNum,
else
SStream_concat(O, " #%u", ShiftAmt);
if (MI->csh->detail) {
if (doing_mem) {
if (MI->csh->doing_mem) {
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].shift.type = ARM64_SFT_LSL;
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].shift.value = ShiftAmt;
} else {
Expand Down Expand Up @@ -370,7 +368,7 @@ static void printOffsetUImm12Operand(MCInst *MI, unsigned OpNum,
SStream_concat(O, "#%u", Imm);

if (MI->csh->detail) {
if (doing_mem) {
if (MI->csh->doing_mem) {
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].mem.disp = Imm;
} else {
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].type = ARM64_OP_IMM;
Expand Down Expand Up @@ -557,7 +555,7 @@ static void printSImm7ScaledOperand(MCInst *MI, unsigned OpNum,
else
SStream_concat(O, "#%u", Imm * MemScale);
if (MI->csh->detail) {
if (doing_mem) {
if (MI->csh->doing_mem) {
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].mem.disp = Imm * MemScale;
} else {
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].type = ARM64_OP_IMM;
Expand Down Expand Up @@ -589,7 +587,7 @@ static void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
unsigned Reg = MCOperand_getReg(Op);
SStream_concat(O, getRegisterName(Reg));
if (MI->csh->detail) {
if (doing_mem) {
if (MI->csh->doing_mem) {
if (MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].mem.base == ARM64_REG_INVALID) {
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].mem.base = Reg;
} else {
Expand All @@ -608,7 +606,7 @@ static void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
else
SStream_concat(O, "#%"PRIu64, imm);
if (MI->csh->detail) {
if (doing_mem) {
if (MI->csh->doing_mem) {
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].mem.disp = imm;
} else {
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].type = ARM64_OP_IMM;
Expand Down Expand Up @@ -721,7 +719,7 @@ static void printUImmBareOperand(MCInst *MI, unsigned OpNum, SStream *O)
SStream_concat(O, "%u", Imm);

if (MI->csh->detail) {
if (doing_mem) {
if (MI->csh->doing_mem) {
MI->flat_insn.arm64.operands[MI->flat_insn.arm64.op_count].mem.disp = Imm;
} else {
// FIXME: never has false branch??
Expand Down
12 changes: 5 additions & 7 deletions arch/ARM/ARMInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,13 @@ static void printVectorListFourSpaced(MCInst *MI, unsigned OpNum, SStream *O);

static void printInstSyncBOption(MCInst *MI, unsigned OpNum, SStream *O);

// FIXME: make this status session's specific, not global like this
static bool doing_mem = false;
static void set_mem_access(MCInst *MI, bool status)
{
if (MI->csh->detail != CS_OPT_ON)
return;

doing_mem = status;
if (doing_mem) {
MI->csh->doing_mem = status;
if (status) {
MI->flat_insn.arm.operands[MI->flat_insn.arm.op_count].type = ARM_OP_MEM;
MI->flat_insn.arm.operands[MI->flat_insn.arm.op_count].mem.base = ARM_REG_INVALID;
MI->flat_insn.arm.operands[MI->flat_insn.arm.op_count].mem.index = ARM_REG_INVALID;
Expand Down Expand Up @@ -546,7 +544,7 @@ static void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
unsigned Reg = MCOperand_getReg(Op);
printRegName(O, Reg);
if (MI->csh->detail) {
if (doing_mem) {
if (MI->csh->doing_mem) {
if (MI->flat_insn.arm.operands[MI->flat_insn.arm.op_count].mem.base == ARM_REG_INVALID)
MI->flat_insn.arm.operands[MI->flat_insn.arm.op_count].mem.base = Reg;
else
Expand Down Expand Up @@ -587,7 +585,7 @@ static void printOperand(MCInst *MI, unsigned OpNo, SStream *O)

SStream_concat(O, markup(">"));
if (MI->csh->detail) {
if (doing_mem)
if (MI->csh->doing_mem)
MI->flat_insn.arm.operands[MI->flat_insn.arm.op_count].mem.disp = imm;
else {
MI->flat_insn.arm.operands[MI->flat_insn.arm.op_count].type = ARM_OP_IMM;
Expand Down Expand Up @@ -1396,7 +1394,7 @@ static void printNoHashImmediate(MCInst *MI, unsigned OpNum, SStream *O)
else
SStream_concat(O, "%u", tmp);
if (MI->csh->detail) {
if (doing_mem) {
if (MI->csh->doing_mem) {
MI->flat_insn.arm.operands[MI->flat_insn.arm.op_count].mem.disp = tmp;
} else {
MI->flat_insn.arm.operands[MI->flat_insn.arm.op_count].type = ARM_OP_IMM;
Expand Down
10 changes: 4 additions & 6 deletions arch/Mips/MipsInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,14 @@ typedef enum Mips_CondCode {
static char *getRegisterName(unsigned RegNo);
static void printInstruction(MCInst *MI, SStream *O);

// FIXME: make this status session's specific, not global like this
static bool doing_mem = false;
static void set_mem_access(MCInst *MI, bool status)
{
doing_mem = status;
MI->csh->doing_mem = status;

if (MI->csh->detail != CS_OPT_ON)
return;

if (doing_mem) {
if (status) {
MI->flat_insn.mips.operands[MI->flat_insn.mips.op_count].type = MIPS_OP_MEM;
MI->flat_insn.mips.operands[MI->flat_insn.mips.op_count].mem.base = MIPS_REG_INVALID;
MI->flat_insn.mips.operands[MI->flat_insn.mips.op_count].mem.disp = 0;
Expand Down Expand Up @@ -197,7 +195,7 @@ static void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
printRegName(O, reg);
reg = Mips_map_register(reg);
if (MI->csh->detail) {
if (doing_mem) {
if (MI->csh->doing_mem) {
MI->flat_insn.mips.operands[MI->flat_insn.mips.op_count].mem.base = reg;
} else {
MI->flat_insn.mips.operands[MI->flat_insn.mips.op_count].type = MIPS_OP_REG;
Expand All @@ -209,7 +207,7 @@ static void printOperand(MCInst *MI, unsigned OpNo, SStream *O)

if (MCOperand_isImm(Op)) {
int64_t imm = MCOperand_getImm(Op);
if (doing_mem) {
if (MI->csh->doing_mem) {
if (imm) { // only print Imm offset if it is not 0
if (imm >= 0) {
if (imm > HEX_THRESHOLD)
Expand Down
12 changes: 5 additions & 7 deletions arch/PowerPC/PPCInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ static void printOperand(MCInst *MI, unsigned OpNo, SStream *O);
static void printInstruction(MCInst *MI, SStream *O);
static void printAbsBranchOperand(MCInst *MI, unsigned OpNo, SStream *O);

// FIXME: make this status session's specific, not global like this
static bool doing_mem = false;
static void set_mem_access(MCInst *MI, bool status)
{
if (MI->csh->detail != CS_OPT_ON)
return;

doing_mem = status;
MI->csh->doing_mem = status;

if (doing_mem) {
if (status) {
MI->flat_insn.ppc.operands[MI->flat_insn.ppc.op_count].type = PPC_OP_MEM;
MI->flat_insn.ppc.operands[MI->flat_insn.ppc.op_count].mem.base = PPC_REG_INVALID;
MI->flat_insn.ppc.operands[MI->flat_insn.ppc.op_count].mem.disp = 0;
Expand Down Expand Up @@ -327,7 +325,7 @@ static void printS16ImmOperand_Mem(MCInst *MI, unsigned OpNo, SStream *O)
}

if (MI->csh->detail) {
if (doing_mem) {
if (MI->csh->doing_mem) {
MI->flat_insn.ppc.operands[MI->flat_insn.ppc.op_count].mem.disp = Imm;
} else {
MI->flat_insn.ppc.operands[MI->flat_insn.ppc.op_count].type = PPC_OP_IMM;
Expand Down Expand Up @@ -486,7 +484,7 @@ static void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
SStream_concat(O, "%s", RegName);

if (MI->csh->detail) {
if (doing_mem) {
if (MI->csh->doing_mem) {
MI->flat_insn.ppc.operands[MI->flat_insn.ppc.op_count].mem.base = reg;
} else {
MI->flat_insn.ppc.operands[MI->flat_insn.ppc.op_count].type = PPC_OP_REG;
Expand All @@ -513,7 +511,7 @@ static void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
}

if (MI->csh->detail) {
if (doing_mem) {
if (MI->csh->doing_mem) {
MI->flat_insn.ppc.operands[MI->flat_insn.ppc.op_count].mem.disp = imm;
} else {
MI->flat_insn.ppc.operands[MI->flat_insn.ppc.op_count].type = PPC_OP_IMM;
Expand Down
1 change: 1 addition & 0 deletions cs_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct cs_struct {
ARM_ITStatus ITBlock; // for Arm only
cs_opt_value detail;
int syntax; // asm syntax for simple printer such as PPC
bool doing_mem; // handling memory operand in InstPrinter code
};

#define MAX_ARCH 8
Expand Down

0 comments on commit 19b0de3

Please sign in to comment.