Index: include/llvm/CodeGen/CommandFlags.h =================================================================== --- include/llvm/CodeGen/CommandFlags.h +++ include/llvm/CodeGen/CommandFlags.h @@ -231,6 +231,9 @@ Options.PositionIndependentExecutable = EnablePIE; Options.EnableSegmentedStacks = SegmentedStacks; Options.UseInitArray = UseInitArray; + + Options.MCOptions.InitFromFlags(); + return Options; } Index: include/llvm/MC/MCTargetAsmParser.h =================================================================== --- include/llvm/MC/MCTargetAsmParser.h +++ include/llvm/MC/MCTargetAsmParser.h @@ -14,12 +14,13 @@ #include "llvm/MC/MCParser/MCAsmParserExtension.h" namespace llvm { -class MCStreamer; -class StringRef; -class SMLoc; class AsmToken; -class MCParsedAsmOperand; class MCInst; +class MCParsedAsmOperand; +class MCStreamer; +class MachineFunction; +class SMLoc; +class StringRef; template class SmallVectorImpl; enum AsmRewriteKind { @@ -97,6 +98,9 @@ /// ms-style inline assembly. MCAsmParserSemaCallback *SemaCallback; + /// Current MachineFunction. + const MachineFunction *MF; + public: virtual ~MCTargetAsmParser(); @@ -110,6 +114,13 @@ SemaCallback = Callback; } + void setMachineFunction(const MachineFunction *MF) { + this->MF = MF; + onMachineFunctionSet(); + } + + virtual void onMachineFunctionSet() {} + virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) = 0; Index: include/llvm/MC/MCTargetOptions.h =================================================================== --- /dev/null +++ include/llvm/MC/MCTargetOptions.h @@ -0,0 +1,47 @@ +//===- MCTargetOptions.h - MC Target Options -------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_MC_MCTARGETOPTIONS_H +#define LLVM_MC_MCTARGETOPTIONS_H + +namespace llvm { + +namespace AsmInstrumentation { +enum AsmInstrumentationMode { + None, // No instrumentation at all. + Address // Instrument instructions with memory arguments. +}; +} + +class MCTargetOptions { +public: + /// AsmInstrumentationMode - This flag is sed by the + /// asm-instrumentation=mode option is specified on the command + /// line. This settings my either be None or Address. None selects + /// no instrumentation at all. Address selects AddressSanitizer + /// instrumentation. + AsmInstrumentation::AsmInstrumentationMode AsmInstrumentationMode; + + MCTargetOptions(); + void InitFromFlags(); +}; + +inline bool operator==(const MCTargetOptions &LHS, const MCTargetOptions &RHS) { +#define ARE_EQUAL(X) LHS.X == RHS.X + return ARE_EQUAL(AsmInstrumentationMode); +#undef ARE_EQUAL +} + +inline bool operator!=(const MCTargetOptions &LHS, const MCTargetOptions &RHS) { + return !(LHS == RHS); +} + +} // end namespace llvm + +#endif Index: include/llvm/Target/TargetMachine.h =================================================================== --- include/llvm/Target/TargetMachine.h +++ include/llvm/Target/TargetMachine.h @@ -108,6 +108,7 @@ /// \brief Reset the target options based on the function's attributes. void resetTargetOptions(const MachineFunction *MF) const; + const TargetOptions &getTargetOptions() const { return Options; } // Interfaces to the major aspects of target machine information: // Index: include/llvm/Target/TargetOptions.h =================================================================== --- include/llvm/Target/TargetOptions.h +++ include/llvm/Target/TargetOptions.h @@ -15,6 +15,7 @@ #ifndef LLVM_TARGET_TARGETOPTIONS_H #define LLVM_TARGET_TARGETOPTIONS_H +#include "llvm/MC/MCTargetOptions.h" #include namespace llvm { @@ -195,6 +196,9 @@ /// via the llvm.fma.* intrinsic) will always be honored, regardless of /// the value of this option. FPOpFusion::FPOpFusionMode AllowFPOpFusion; + + /// Machine level options. + MCTargetOptions MCOptions; }; // Comparison operators: @@ -221,7 +225,8 @@ ARE_EQUAL(UseInitArray) && ARE_EQUAL(TrapFuncName) && ARE_EQUAL(FloatABIType) && - ARE_EQUAL(AllowFPOpFusion); + ARE_EQUAL(AllowFPOpFusion) && + ARE_EQUAL(MCOptions); #undef ARE_EQUAL } Index: include/llvm/Transforms/Utils/SpecialCaseList.h =================================================================== --- include/llvm/Transforms/Utils/SpecialCaseList.h +++ include/llvm/Transforms/Utils/SpecialCaseList.h @@ -94,6 +94,8 @@ /// omitted to search the empty category. bool isIn(const Module &M, const StringRef Category = StringRef()) const; + bool isSourceFileIn(const StringRef FileName) const; + private: SpecialCaseList(SpecialCaseList const &) LLVM_DELETED_FUNCTION; SpecialCaseList &operator=(SpecialCaseList const &) LLVM_DELETED_FUNCTION; Index: lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp =================================================================== --- lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp +++ lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp @@ -139,6 +139,7 @@ if (!TAP) report_fatal_error("Inline asm not supported by this streamer because" " we don't have an asm parser for this target\n"); + TAP->setMachineFunction(MF); Parser->setAssemblerDialect(Dialect); Parser->setTargetParser(*TAP.get()); Index: lib/MC/CMakeLists.txt =================================================================== --- lib/MC/CMakeLists.txt +++ lib/MC/CMakeLists.txt @@ -44,6 +44,7 @@ MCSubtargetInfo.cpp MCSymbol.cpp MCSymbolizer.cpp + MCTargetOptions.cpp MCValue.cpp MCWin64EH.cpp MachObjectWriter.cpp Index: lib/MC/MCParser/MCTargetAsmParser.cpp =================================================================== --- lib/MC/MCParser/MCTargetAsmParser.cpp +++ lib/MC/MCParser/MCTargetAsmParser.cpp @@ -11,7 +11,7 @@ using namespace llvm; MCTargetAsmParser::MCTargetAsmParser() - : AvailableFeatures(0), ParsingInlineAsm(false) + : AvailableFeatures(0), ParsingInlineAsm(false), MF(NULL) { } Index: lib/Target/TargetMachine.cpp =================================================================== --- lib/Target/TargetMachine.cpp +++ lib/Target/TargetMachine.cpp @@ -21,6 +21,7 @@ #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCCodeGenInfo.h" #include "llvm/MC/MCContext.h" +#include "llvm/MC/MCTargetOptions.h" #include "llvm/MC/SectionKind.h" #include "llvm/Support/CommandLine.h" #include "llvm/Target/TargetLowering.h" @@ -89,6 +90,11 @@ RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math"); RESET_OPTION(UseSoftFloat, "use-soft-float"); RESET_OPTION(DisableTailCalls, "disable-tail-calls"); + + if (F->hasFnAttribute(Attribute::SanitizeAddress)) + TO.MCOptions.AsmInstrumentationMode = AsmInstrumentation::Address; + else + TO.MCOptions.AsmInstrumentationMode = AsmInstrumentation::None; } /// getRelocationModel - Returns the code generation relocation model. The Index: lib/Target/X86/AsmParser/X86AsmInstrumentation.h =================================================================== --- lib/Target/X86/AsmParser/X86AsmInstrumentation.h +++ lib/Target/X86/AsmParser/X86AsmInstrumentation.h @@ -14,6 +14,7 @@ namespace llvm { +class MachineFunction; class MCContext; class MCInst; class MCParsedAsmOperand; @@ -22,7 +23,9 @@ class X86AsmInstrumentation; -X86AsmInstrumentation *CreateX86AsmInstrumentation(MCSubtargetInfo &STI); +X86AsmInstrumentation * +CreateX86AsmInstrumentation(const MachineFunction *MF, const MCContext &Ctx, + const MCSubtargetInfo &STI); class X86AsmInstrumentation { public: @@ -36,11 +39,12 @@ protected: friend X86AsmInstrumentation * - CreateX86AsmInstrumentation(MCSubtargetInfo &STI); + CreateX86AsmInstrumentation(const MachineFunction *MF, const MCContext &Ctx, + const MCSubtargetInfo &STI); X86AsmInstrumentation(); }; -} // End llvm namespace +} // End llvm namespace -#endif // X86_ASM_INSTRUMENTATION_H +#endif // X86_ASM_INSTRUMENTATION_H Index: lib/Target/X86/AsmParser/X86AsmInstrumentation.cpp =================================================================== --- lib/Target/X86/AsmParser/X86AsmInstrumentation.cpp +++ lib/Target/X86/AsmParser/X86AsmInstrumentation.cpp @@ -11,21 +11,30 @@ #include "X86AsmInstrumentation.h" #include "X86Operand.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/CodeGen/MachineFunction.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/MC/MCTargetOptions.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Compiler.h" -#include "llvm/MC/MCParser/MCParsedAsmOperand.h" +#include "llvm/Target/TargetMachine.h" +#include "llvm/Target/TargetOptions.h" +#include "llvm/Transforms/Utils/SpecialCaseList.h" +#include namespace llvm { namespace { -static cl::opt ClAsanInstrumentInlineAssembly( - "asan-instrument-inline-assembly", cl::desc("instrument inline assembly"), - cl::Hidden, cl::init(false)); +static cl::opt +ClAsanAsmBlacklist("asan-asm-blacklist", + cl::desc("File containing the list of objects to ignore " + "during asan instrumentation"), + cl::Hidden); bool IsStackReg(unsigned Reg) { return Reg == X86::RSP || Reg == X86::ESP || Reg == X86::SP; @@ -38,7 +47,7 @@ class X86AddressSanitizer : public X86AsmInstrumentation { public: - X86AddressSanitizer(MCSubtargetInfo &sti) : STI(sti) {} + X86AddressSanitizer(const MCSubtargetInfo &STI) : STI(STI) {} virtual ~X86AddressSanitizer() {} // X86AsmInstrumentation implementation: @@ -63,7 +72,7 @@ } protected: - MCSubtargetInfo &STI; + const MCSubtargetInfo &STI; }; void X86AddressSanitizer::InstrumentMemOperand( @@ -144,7 +153,8 @@ 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, @@ -179,7 +189,8 @@ 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, @@ -223,12 +234,28 @@ const MCInst &Inst, SmallVectorImpl &Operands, MCContext &Ctx, MCStreamer &Out) {} -X86AsmInstrumentation *CreateX86AsmInstrumentation(MCSubtargetInfo &STI) { - if (ClAsanInstrumentInlineAssembly) { - if ((STI.getFeatureBits() & X86::Mode32Bit) != 0) - return new X86AddressSanitizer32(STI); - if ((STI.getFeatureBits() & X86::Mode64Bit) != 0) - return new X86AddressSanitizer64(STI); +X86AsmInstrumentation * +CreateX86AsmInstrumentation(const MachineFunction *MF, const MCContext &Ctx, + const MCSubtargetInfo &STI) { + MCTargetOptions Options; + if (MF) + Options = MF->getTarget().getTargetOptions().MCOptions; + else + Options.InitFromFlags(); + const Function *F = MF ? MF->getFunction() : 0; + if (Options.AsmInstrumentationMode == AsmInstrumentation::Address) { + std::unique_ptr BL( + SpecialCaseList::createOrDie(ClAsanAsmBlacklist)); + if (BL->isSourceFileIn(Ctx.getMainFileName())) + ; + else if (F && BL->isIn(*F)) + ; + else { + if ((STI.getFeatureBits() & X86::Mode32Bit) != 0) + return new X86AddressSanitizer32(STI); + if ((STI.getFeatureBits() & X86::Mode64Bit) != 0) + return new X86AddressSanitizer64(STI); + } } return new X86AsmInstrumentation(); } Index: lib/Target/X86/AsmParser/X86AsmParser.cpp =================================================================== --- lib/Target/X86/AsmParser/X86AsmParser.cpp +++ lib/Target/X86/AsmParser/X86AsmParser.cpp @@ -715,8 +715,14 @@ // Initialize the set of available features. setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits())); - Instrumentation.reset(CreateX86AsmInstrumentation(STI)); + Instrumentation.reset( + CreateX86AsmInstrumentation(MF, Parser.getContext(), STI)); } + + void onMachineFunctionSet() override { + Instrumentation.reset(CreateX86AsmInstrumentation(MF, Parser.getContext(), STI)); + } + bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override; bool Index: lib/Transforms/Utils/SpecialCaseList.cpp =================================================================== --- lib/Transforms/Utils/SpecialCaseList.cpp +++ lib/Transforms/Utils/SpecialCaseList.cpp @@ -207,6 +207,10 @@ return inSectionCategory("src", M.getModuleIdentifier(), Category); } +bool SpecialCaseList::isSourceFileIn(const StringRef FileName) const { + return inSectionCategory("src", FileName, StringRef()); +} + bool SpecialCaseList::inSectionCategory(const StringRef Section, const StringRef Query, const StringRef Category) const { Index: test/Instrumentation/AddressSanitizer/X86/asm_attr.ll =================================================================== --- /dev/null +++ test/Instrumentation/AddressSanitizer/X86/asm_attr.ll @@ -0,0 +1,29 @@ +; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mcpu=corei7 -mattr=+sse2 -asm-instrumentation=address -asan-asm-blacklist=%s.blacklist | 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_blacklisted +; CHECK-NOT: callq __sanitizer_sanitize_load8@PLT +; CHECK-NOT: callq __sanitizer_sanitize_store8@PLT +define void @mov_blacklisted(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 +} + +; 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 +} + Index: test/Instrumentation/AddressSanitizer/X86/asm_attr.ll.blacklist =================================================================== --- /dev/null +++ test/Instrumentation/AddressSanitizer/X86/asm_attr.ll.blacklist @@ -0,0 +1 @@ +fun:*_blacklisted Index: test/Instrumentation/AddressSanitizer/X86/asm_blacklisted.s =================================================================== --- test/Instrumentation/AddressSanitizer/X86/asm_blacklisted.s +++ test/Instrumentation/AddressSanitizer/X86/asm_blacklisted.s @@ -1,4 +1,4 @@ -# RUN: llvm-mc %s -triple=x86_64-unknown-linux-gnu -mcpu=corei7 -mattr=+sse2 | FileCheck %s +# RUN: llvm-mc %s -triple=x86_64-unknown-linux-gnu -mcpu=corei7 -mattr=+sse2 -asm-instrumentation=address -asan-asm-blacklist=%s.blacklist | FileCheck %s .text .globl mov1b @@ -20,25 +20,5 @@ .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 Index: test/Instrumentation/AddressSanitizer/X86/asm_blacklisted.s.blacklist =================================================================== --- /dev/null +++ test/Instrumentation/AddressSanitizer/X86/asm_blacklisted.s.blacklist @@ -0,0 +1 @@ +src:*_blacklisted.s Index: test/Instrumentation/AddressSanitizer/X86/asm_mov.ll =================================================================== --- test/Instrumentation/AddressSanitizer/X86/asm_mov.ll +++ test/Instrumentation/AddressSanitizer/X86/asm_mov.ll @@ -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" @@ -113,7 +113,7 @@ 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} Index: test/Instrumentation/AddressSanitizer/X86/asm_mov.s =================================================================== --- test/Instrumentation/AddressSanitizer/X86/asm_mov.s +++ test/Instrumentation/AddressSanitizer/X86/asm_mov.s @@ -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 Index: test/Instrumentation/AddressSanitizer/X86/asm_mov_no_instrumentation.s =================================================================== --- test/Instrumentation/AddressSanitizer/X86/asm_mov_no_instrumentation.s +++ test/Instrumentation/AddressSanitizer/X86/asm_mov_no_instrumentation.s @@ -20,25 +20,5 @@ .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