Skip to content

Commit

Permalink
[asan] Use MCInstrInfo in inline asm instrumentation.
Browse files Browse the repository at this point in the history
Patch by Yuri Gorshenin.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@207115 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
eugenis committed Apr 24, 2014
1 parent c80e567 commit 23a4885
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
29 changes: 7 additions & 22 deletions lib/Target/X86/AsmParser/X86AsmInstrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstBuilder.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSubtargetInfo.h"
Expand All @@ -40,8 +41,8 @@ class X86AddressSanitizer : public X86AsmInstrumentation {
// X86AsmInstrumentation implementation:
virtual void InstrumentInstruction(
const MCInst &Inst, SmallVectorImpl<MCParsedAsmOperand *> &Operands,
MCContext &Ctx, MCStreamer &Out) override {
InstrumentMOV(Inst, Operands, Ctx, Out);
MCContext &Ctx, const MCInstrInfo &MII, MCStreamer &Out) override {
InstrumentMOV(Inst, Operands, Ctx, MII, Out);
}

// Should be implemented differently in x86_32 and x86_64 subclasses.
Expand All @@ -53,7 +54,7 @@ class X86AddressSanitizer : public X86AsmInstrumentation {
bool IsWrite, MCContext &Ctx, MCStreamer &Out);
void InstrumentMOV(const MCInst &Inst,
SmallVectorImpl<MCParsedAsmOperand *> &Operands,
MCContext &Ctx, MCStreamer &Out);
MCContext &Ctx, const MCInstrInfo &MII, MCStreamer &Out);
void EmitInstruction(MCStreamer &Out, const MCInst &Inst) {
Out.EmitInstruction(Inst, STI);
}
Expand All @@ -79,50 +80,33 @@ void X86AddressSanitizer::InstrumentMemOperand(

void X86AddressSanitizer::InstrumentMOV(
const MCInst &Inst, SmallVectorImpl<MCParsedAsmOperand *> &Operands,
MCContext &Ctx, MCStreamer &Out) {
MCContext &Ctx, const MCInstrInfo &MII, MCStreamer &Out) {
// Access size in bytes.
unsigned AccessSize = 0;

// FIXME: use MCInstrDesc to get proper value of IsWrite.
bool IsWrite = false;
switch (Inst.getOpcode()) {
case X86::MOV8mi:
case X86::MOV8mr:
AccessSize = 1;
IsWrite = true;
break;
case X86::MOV8rm:
AccessSize = 1;
break;
case X86::MOV16mi:
case X86::MOV16mr:
AccessSize = 2;
IsWrite = true;
break;
case X86::MOV16rm:
AccessSize = 2;
break;
case X86::MOV32mi:
case X86::MOV32mr:
AccessSize = 4;
IsWrite = true;
break;
case X86::MOV32rm:
AccessSize = 4;
break;
case X86::MOV64mi32:
case X86::MOV64mr:
AccessSize = 8;
IsWrite = true;
break;
case X86::MOV64rm:
AccessSize = 8;
break;
case X86::MOVAPDmr:
case X86::MOVAPSmr:
AccessSize = 16;
IsWrite = true;
break;
case X86::MOVAPDrm:
case X86::MOVAPSrm:
AccessSize = 16;
Expand All @@ -131,6 +115,7 @@ void X86AddressSanitizer::InstrumentMOV(
return;
}

const bool IsWrite = MII.get(Inst.getOpcode()).mayStore();
for (unsigned Ix = 0; Ix < Operands.size(); ++Ix) {
MCParsedAsmOperand *Op = Operands[Ix];
if (Op && Op->isMem())
Expand Down Expand Up @@ -219,7 +204,7 @@ X86AsmInstrumentation::~X86AsmInstrumentation() {}

void X86AsmInstrumentation::InstrumentInstruction(
const MCInst &Inst, SmallVectorImpl<MCParsedAsmOperand *> &Operands,
MCContext &Ctx, MCStreamer &Out) {}
MCContext &Ctx, const MCInstrInfo &MII, MCStreamer &Out) {}

X86AsmInstrumentation *
CreateX86AsmInstrumentation(const MCTargetOptions &MCOptions, const MCContext &Ctx,
Expand Down
5 changes: 4 additions & 1 deletion lib/Target/X86/AsmParser/X86AsmInstrumentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace llvm {

class MCContext;
class MCInst;
class MCInstrInfo;
class MCParsedAsmOperand;
class MCStreamer;
class MCSubtargetInfo;
Expand All @@ -35,7 +36,9 @@ class X86AsmInstrumentation {
// instruction is sent to Out.
virtual void InstrumentInstruction(
const MCInst &Inst, SmallVectorImpl<MCParsedAsmOperand *> &Operands,
MCContext &Ctx, MCStreamer &Out);
MCContext &Ctx,
const MCInstrInfo &MII,
MCStreamer &Out);

protected:
friend X86AsmInstrumentation *
Expand Down
9 changes: 6 additions & 3 deletions lib/Target/X86/AsmParser/X86AsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCParser/MCAsmLexer.h"
#include "llvm/MC/MCParser/MCAsmParser.h"
#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Expand Down Expand Up @@ -55,6 +56,7 @@ static const char OpPrecedence[] = {
class X86AsmParser : public MCTargetAsmParser {
MCSubtargetInfo &STI;
MCAsmParser &Parser;
const MCInstrInfo &MII;
ParseInstructionInfo *InstInfo;
std::unique_ptr<X86AsmInstrumentation> Instrumentation;
private:
Expand Down Expand Up @@ -710,9 +712,9 @@ class X86AsmParser : public MCTargetAsmParser {

public:
X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
const MCInstrInfo &MII,
const MCInstrInfo &mii,
const MCTargetOptions &Options)
: MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
: MCTargetAsmParser(), STI(sti), Parser(parser), MII(mii), InstInfo(0) {

// Initialize the set of available features.
setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Expand Down Expand Up @@ -2258,7 +2260,8 @@ static const char *getSubtargetFeatureName(unsigned Val);
void X86AsmParser::EmitInstruction(
MCInst &Inst, SmallVectorImpl<MCParsedAsmOperand *> &Operands,
MCStreamer &Out) {
Instrumentation->InstrumentInstruction(Inst, Operands, getContext(), Out);
Instrumentation->InstrumentInstruction(Inst, Operands, getContext(), MII,
Out);
Out.EmitInstruction(Inst, STI);
}

Expand Down

0 comments on commit 23a4885

Please sign in to comment.