26 changes: 13 additions & 13 deletions llvm/lib/Target/X86/AsmParser/X86AsmInstrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,18 @@
#include "X86AsmInstrumentation.h"
#include "X86Operand.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/IR/Function.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstBuilder.h"
#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
#include "llvm/MC/MCTargetOptions.h"

namespace llvm {
namespace {

static cl::opt<bool> ClAsanInstrumentInlineAssembly(
"asan-instrument-inline-assembly", cl::desc("instrument inline assembly"),
cl::Hidden, cl::init(false));

bool IsStackReg(unsigned Reg) {
return Reg == X86::RSP || Reg == X86::ESP || Reg == X86::SP;
}
Expand All @@ -38,7 +34,7 @@ std::string FuncName(unsigned AccessSize, bool IsWrite) {

class X86AddressSanitizer : public X86AsmInstrumentation {
public:
X86AddressSanitizer(MCSubtargetInfo &sti) : STI(sti) {}
X86AddressSanitizer(const MCSubtargetInfo &STI) : STI(STI) {}
virtual ~X86AddressSanitizer() {}

// X86AsmInstrumentation implementation:
Expand All @@ -63,7 +59,7 @@ class X86AddressSanitizer : public X86AsmInstrumentation {
}

protected:
MCSubtargetInfo &STI;
const MCSubtargetInfo &STI;
};

void X86AddressSanitizer::InstrumentMemOperand(
Expand Down Expand Up @@ -144,7 +140,8 @@ void X86AddressSanitizer::InstrumentMOV(

class X86AddressSanitizer32 : public X86AddressSanitizer {
public:
X86AddressSanitizer32(MCSubtargetInfo &sti) : X86AddressSanitizer(sti) {}
X86AddressSanitizer32(const MCSubtargetInfo &STI)
: X86AddressSanitizer(STI) {}
virtual ~X86AddressSanitizer32() {}

virtual void InstrumentMemOperandImpl(X86Operand *Op, unsigned AccessSize,
Expand Down Expand Up @@ -179,7 +176,8 @@ void X86AddressSanitizer32::InstrumentMemOperandImpl(

class X86AddressSanitizer64 : public X86AddressSanitizer {
public:
X86AddressSanitizer64(MCSubtargetInfo &sti) : X86AddressSanitizer(sti) {}
X86AddressSanitizer64(const MCSubtargetInfo &STI)
: X86AddressSanitizer(STI) {}
virtual ~X86AddressSanitizer64() {}

virtual void InstrumentMemOperandImpl(X86Operand *Op, unsigned AccessSize,
Expand Down Expand Up @@ -223,8 +221,10 @@ void X86AsmInstrumentation::InstrumentInstruction(
const MCInst &Inst, SmallVectorImpl<MCParsedAsmOperand *> &Operands,
MCContext &Ctx, MCStreamer &Out) {}

X86AsmInstrumentation *CreateX86AsmInstrumentation(MCSubtargetInfo &STI) {
if (ClAsanInstrumentInlineAssembly) {
X86AsmInstrumentation *
CreateX86AsmInstrumentation(const MCTargetOptions &MCOptions, const MCContext &Ctx,
const MCSubtargetInfo &STI) {
if (MCOptions.SanitizeAddress) {
if ((STI.getFeatureBits() & X86::Mode32Bit) != 0)
return new X86AddressSanitizer32(STI);
if ((STI.getFeatureBits() & X86::Mode64Bit) != 0)
Expand Down
12 changes: 8 additions & 4 deletions llvm/lib/Target/X86/AsmParser/X86AsmInstrumentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ class MCInst;
class MCParsedAsmOperand;
class MCStreamer;
class MCSubtargetInfo;
class MCTargetOptions;

class X86AsmInstrumentation;

X86AsmInstrumentation *CreateX86AsmInstrumentation(MCSubtargetInfo &STI);
X86AsmInstrumentation *
CreateX86AsmInstrumentation(const MCTargetOptions &MCOptions,
const MCContext &Ctx, const MCSubtargetInfo &STI);

class X86AsmInstrumentation {
public:
Expand All @@ -36,11 +39,12 @@ class X86AsmInstrumentation {

protected:
friend X86AsmInstrumentation *
CreateX86AsmInstrumentation(MCSubtargetInfo &STI);
CreateX86AsmInstrumentation(const MCTargetOptions &MCOptions,
const MCContext &Ctx, const MCSubtargetInfo &STI);

X86AsmInstrumentation();
};

} // End llvm namespace
} // End llvm namespace

#endif // X86_ASM_INSTRUMENTATION_H
#endif // X86_ASM_INSTRUMENTATION_H
7 changes: 5 additions & 2 deletions llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,13 +710,16 @@ 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) {

// Initialize the set of available features.
setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Instrumentation.reset(CreateX86AsmInstrumentation(STI));
Instrumentation.reset(
CreateX86AsmInstrumentation(Options, Parser.getContext(), STI));
}

bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;

bool
Expand Down
20 changes: 20 additions & 0 deletions llvm/test/Instrumentation/AddressSanitizer/X86/asm_attr.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mcpu=corei7 -mattr=+sse2 -asm-instrumentation=address | FileCheck %s

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; CHECK-LABEL: mov_no_attr
; CHECK-NOT: callq __sanitizer_sanitize_load8@PLT
; CHECK-NOT: callq __sanitizer_sanitize_store8@PLT
define void @mov_no_attr(i64* %dst, i64* %src) {
tail call void asm sideeffect "movq ($1), %rax \0A\09movq %rax, ($0) \0A\09", "r,r,~{memory},~{rax},~{dirflag},~{fpsr},~{flags}"(i64* %dst, i64* %src)
ret void
}

; CHECK-LABEL: mov_sanitize
; CHECK: callq __sanitizer_sanitize_load8@PLT
; CHECK: callq __sanitizer_sanitize_store8@PLT
define void @mov_sanitize(i64* %dst, i64* %src) sanitize_address {
tail call void asm sideeffect "movq ($1), %rax \0A\09movq %rax, ($0) \0A\09", "r,r,~{memory},~{rax},~{dirflag},~{fpsr},~{flags}"(i64* %dst, i64* %src)
ret void
}
4 changes: 2 additions & 2 deletions llvm/test/Instrumentation/AddressSanitizer/X86/asm_mov.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mcpu=corei7 -mattr=+sse2 -asan-instrument-inline-assembly | FileCheck %s
; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mcpu=corei7 -mattr=+sse2 -asm-instrumentation=address | FileCheck %s

target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
Expand Down Expand Up @@ -113,7 +113,7 @@ entry:
ret void
}

attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-frame-pointer-elim-non-leaf"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #0 = { nounwind uwtable sanitize_address "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-frame-pointer-elim-non-leaf"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { nounwind }

!0 = metadata !{i32 98, i32 122, i32 160}
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Instrumentation/AddressSanitizer/X86/asm_mov.s
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RUN: llvm-mc %s -triple=x86_64-unknown-linux-gnu -mcpu=corei7 -mattr=+sse2 -asan-instrument-inline-assembly | FileCheck %s
# RUN: llvm-mc %s -triple=x86_64-unknown-linux-gnu -mcpu=corei7 -mattr=+sse2 -asm-instrumentation=address | FileCheck %s

.text
.globl mov1b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,5 @@ mov1b: # @mov1b
.size mov1b, .Ltmp0-mov1b
.cfi_endproc

.globl mov16b
.align 16, 0x90
.type mov16b,@function
# CHECK-LABEL: mov16b
# CHECK-NOT: callq __sanitizer_sanitize_load16@PLT
# CHECK-NOT: callq __sanitizer_sanitize_store16@PLT
mov16b: # @mov16b
.cfi_startproc
# BB#0:
#APP
movaps (%rsi), %xmm0
movaps %xmm0, (%rdi)

#NO_APP
retq
.Ltmp1:
.size mov16b, .Ltmp1-mov16b
.cfi_endproc


.ident "clang version 3.5 "
.section ".note.GNU-stack","",@progbits
7 changes: 5 additions & 2 deletions llvm/tools/llvm-mc/llvm-mc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCTargetAsmParser.h"
#include "llvm/MC/MCTargetOptionsCommandFlags.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compression.h"
#include "llvm/Support/FileUtilities.h"
Expand Down Expand Up @@ -320,9 +321,11 @@ static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI, tool_output_file *Out)
static int AssembleInput(const char *ProgName, const Target *TheTarget,
SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str,
MCAsmInfo &MAI, MCSubtargetInfo &STI, MCInstrInfo &MCII) {
std::unique_ptr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx, Str, MAI));
std::unique_ptr<MCAsmParser> Parser(
createMCAsmParser(SrcMgr, Ctx, Str, MAI));
std::unique_ptr<MCTargetAsmParser> TAP(
TheTarget->createMCAsmParser(STI, *Parser, MCII));
TheTarget->createMCAsmParser(STI, *Parser, MCII,
InitMCTargetOptionsFromFlags()));
if (!TAP) {
errs() << ProgName
<< ": error: this target does not support assembly parsing.\n";
Expand Down