xref: /minix3/external/bsd/llvm/dist/llvm/lib/Target/X86/AsmParser/X86AsmInstrumentation.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //===-- X86AsmInstrumentation.cpp - Instrument X86 inline assembly C++ -*-===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc 
10*0a6a1f1dSLionel Sambuc #include "MCTargetDesc/X86BaseInfo.h"
11*0a6a1f1dSLionel Sambuc #include "X86AsmInstrumentation.h"
12*0a6a1f1dSLionel Sambuc #include "X86Operand.h"
13*0a6a1f1dSLionel Sambuc #include "X86RegisterInfo.h"
14*0a6a1f1dSLionel Sambuc #include "llvm/ADT/StringExtras.h"
15*0a6a1f1dSLionel Sambuc #include "llvm/ADT/Triple.h"
16*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/MachineValueType.h"
17*0a6a1f1dSLionel Sambuc #include "llvm/IR/Function.h"
18*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCAsmInfo.h"
19*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCContext.h"
20*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCInst.h"
21*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCInstBuilder.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCInstrInfo.h"
23*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
24*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCStreamer.h"
25*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCSubtargetInfo.h"
26*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCTargetAsmParser.h"
27*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCTargetOptions.h"
28*0a6a1f1dSLionel Sambuc #include "llvm/Support/CommandLine.h"
29*0a6a1f1dSLionel Sambuc #include <algorithm>
30*0a6a1f1dSLionel Sambuc #include <cassert>
31*0a6a1f1dSLionel Sambuc #include <vector>
32*0a6a1f1dSLionel Sambuc 
33*0a6a1f1dSLionel Sambuc // Following comment describes how assembly instrumentation works.
34*0a6a1f1dSLionel Sambuc // Currently we have only AddressSanitizer instrumentation, but we're
35*0a6a1f1dSLionel Sambuc // planning to implement MemorySanitizer for inline assembly too. If
36*0a6a1f1dSLionel Sambuc // you're not familiar with AddressSanitizer algorithm, please, read
37*0a6a1f1dSLionel Sambuc // https://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm.
38*0a6a1f1dSLionel Sambuc //
39*0a6a1f1dSLionel Sambuc // When inline assembly is parsed by an instance of X86AsmParser, all
40*0a6a1f1dSLionel Sambuc // instructions are emitted via EmitInstruction method. That's the
41*0a6a1f1dSLionel Sambuc // place where X86AsmInstrumentation analyzes an instruction and
42*0a6a1f1dSLionel Sambuc // decides, whether the instruction should be emitted as is or
43*0a6a1f1dSLionel Sambuc // instrumentation is required. The latter case happens when an
44*0a6a1f1dSLionel Sambuc // instruction reads from or writes to memory. Now instruction opcode
45*0a6a1f1dSLionel Sambuc // is explicitly checked, and if an instruction has a memory operand
46*0a6a1f1dSLionel Sambuc // (for instance, movq (%rsi, %rcx, 8), %rax) - it should be
47*0a6a1f1dSLionel Sambuc // instrumented.  There're also exist instructions that modify
48*0a6a1f1dSLionel Sambuc // memory but don't have an explicit memory operands, for instance,
49*0a6a1f1dSLionel Sambuc // movs.
50*0a6a1f1dSLionel Sambuc //
51*0a6a1f1dSLionel Sambuc // Let's consider at first 8-byte memory accesses when an instruction
52*0a6a1f1dSLionel Sambuc // has an explicit memory operand. In this case we need two registers -
53*0a6a1f1dSLionel Sambuc // AddressReg to compute address of a memory cells which are accessed
54*0a6a1f1dSLionel Sambuc // and ShadowReg to compute corresponding shadow address. So, we need
55*0a6a1f1dSLionel Sambuc // to spill both registers before instrumentation code and restore them
56*0a6a1f1dSLionel Sambuc // after instrumentation. Thus, in general, instrumentation code will
57*0a6a1f1dSLionel Sambuc // look like this:
58*0a6a1f1dSLionel Sambuc // PUSHF  # Store flags, otherwise they will be overwritten
59*0a6a1f1dSLionel Sambuc // PUSH AddressReg  # spill AddressReg
60*0a6a1f1dSLionel Sambuc // PUSH ShadowReg   # spill ShadowReg
61*0a6a1f1dSLionel Sambuc // LEA MemOp, AddressReg  # compute address of the memory operand
62*0a6a1f1dSLionel Sambuc // MOV AddressReg, ShadowReg
63*0a6a1f1dSLionel Sambuc // SHR ShadowReg, 3
64*0a6a1f1dSLionel Sambuc // # ShadowOffset(AddressReg >> 3) contains address of a shadow
65*0a6a1f1dSLionel Sambuc // # corresponding to MemOp.
66*0a6a1f1dSLionel Sambuc // CMP ShadowOffset(ShadowReg), 0  # test shadow value
67*0a6a1f1dSLionel Sambuc // JZ .Done  # when shadow equals to zero, everything is fine
68*0a6a1f1dSLionel Sambuc // MOV AddressReg, RDI
69*0a6a1f1dSLionel Sambuc // # Call __asan_report function with AddressReg as an argument
70*0a6a1f1dSLionel Sambuc // CALL __asan_report
71*0a6a1f1dSLionel Sambuc // .Done:
72*0a6a1f1dSLionel Sambuc // POP ShadowReg  # Restore ShadowReg
73*0a6a1f1dSLionel Sambuc // POP AddressReg  # Restore AddressReg
74*0a6a1f1dSLionel Sambuc // POPF  # Restore flags
75*0a6a1f1dSLionel Sambuc //
76*0a6a1f1dSLionel Sambuc // Memory accesses with different size (1-, 2-, 4- and 16-byte) are
77*0a6a1f1dSLionel Sambuc // handled in a similar manner, but small memory accesses (less than 8
78*0a6a1f1dSLionel Sambuc // byte) require an additional ScratchReg, which is used for shadow value.
79*0a6a1f1dSLionel Sambuc //
80*0a6a1f1dSLionel Sambuc // If, suppose, we're instrumenting an instruction like movs, only
81*0a6a1f1dSLionel Sambuc // contents of RDI, RDI + AccessSize * RCX, RSI, RSI + AccessSize *
82*0a6a1f1dSLionel Sambuc // RCX are checked.  In this case there're no need to spill and restore
83*0a6a1f1dSLionel Sambuc // AddressReg , ShadowReg or flags four times, they're saved on stack
84*0a6a1f1dSLionel Sambuc // just once, before instrumentation of these four addresses, and restored
85*0a6a1f1dSLionel Sambuc // at the end of the instrumentation.
86*0a6a1f1dSLionel Sambuc //
87*0a6a1f1dSLionel Sambuc // There exist several things which complicate this simple algorithm.
88*0a6a1f1dSLionel Sambuc // * Instrumented memory operand can have RSP as a base or an index
89*0a6a1f1dSLionel Sambuc //   register.  So we need to add a constant offset before computation
90*0a6a1f1dSLionel Sambuc //   of memory address, since flags, AddressReg, ShadowReg, etc. were
91*0a6a1f1dSLionel Sambuc //   already stored on stack and RSP was modified.
92*0a6a1f1dSLionel Sambuc // * Debug info (usually, DWARF) should be adjusted, because sometimes
93*0a6a1f1dSLionel Sambuc //   RSP is used as a frame register. So, we need to select some
94*0a6a1f1dSLionel Sambuc //   register as a frame register and temprorary override current CFA
95*0a6a1f1dSLionel Sambuc //   register.
96*0a6a1f1dSLionel Sambuc 
97*0a6a1f1dSLionel Sambuc namespace llvm {
98*0a6a1f1dSLionel Sambuc namespace {
99*0a6a1f1dSLionel Sambuc 
100*0a6a1f1dSLionel Sambuc static cl::opt<bool> ClAsanInstrumentAssembly(
101*0a6a1f1dSLionel Sambuc     "asan-instrument-assembly",
102*0a6a1f1dSLionel Sambuc     cl::desc("instrument assembly with AddressSanitizer checks"), cl::Hidden,
103*0a6a1f1dSLionel Sambuc     cl::init(false));
104*0a6a1f1dSLionel Sambuc 
105*0a6a1f1dSLionel Sambuc const int64_t MinAllowedDisplacement = std::numeric_limits<int32_t>::min();
106*0a6a1f1dSLionel Sambuc const int64_t MaxAllowedDisplacement = std::numeric_limits<int32_t>::max();
107*0a6a1f1dSLionel Sambuc 
ApplyDisplacementBounds(int64_t Displacement)108*0a6a1f1dSLionel Sambuc int64_t ApplyDisplacementBounds(int64_t Displacement) {
109*0a6a1f1dSLionel Sambuc   return std::max(std::min(MaxAllowedDisplacement, Displacement),
110*0a6a1f1dSLionel Sambuc                   MinAllowedDisplacement);
111*0a6a1f1dSLionel Sambuc }
112*0a6a1f1dSLionel Sambuc 
CheckDisplacementBounds(int64_t Displacement)113*0a6a1f1dSLionel Sambuc void CheckDisplacementBounds(int64_t Displacement) {
114*0a6a1f1dSLionel Sambuc   assert(Displacement >= MinAllowedDisplacement &&
115*0a6a1f1dSLionel Sambuc          Displacement <= MaxAllowedDisplacement);
116*0a6a1f1dSLionel Sambuc }
117*0a6a1f1dSLionel Sambuc 
IsStackReg(unsigned Reg)118*0a6a1f1dSLionel Sambuc bool IsStackReg(unsigned Reg) { return Reg == X86::RSP || Reg == X86::ESP; }
119*0a6a1f1dSLionel Sambuc 
IsSmallMemAccess(unsigned AccessSize)120*0a6a1f1dSLionel Sambuc bool IsSmallMemAccess(unsigned AccessSize) { return AccessSize < 8; }
121*0a6a1f1dSLionel Sambuc 
FuncName(unsigned AccessSize,bool IsWrite)122*0a6a1f1dSLionel Sambuc std::string FuncName(unsigned AccessSize, bool IsWrite) {
123*0a6a1f1dSLionel Sambuc   return std::string("__asan_report_") + (IsWrite ? "store" : "load") +
124*0a6a1f1dSLionel Sambuc          utostr(AccessSize);
125*0a6a1f1dSLionel Sambuc }
126*0a6a1f1dSLionel Sambuc 
127*0a6a1f1dSLionel Sambuc class X86AddressSanitizer : public X86AsmInstrumentation {
128*0a6a1f1dSLionel Sambuc public:
129*0a6a1f1dSLionel Sambuc   struct RegisterContext {
130*0a6a1f1dSLionel Sambuc   private:
131*0a6a1f1dSLionel Sambuc     enum RegOffset {
132*0a6a1f1dSLionel Sambuc       REG_OFFSET_ADDRESS = 0,
133*0a6a1f1dSLionel Sambuc       REG_OFFSET_SHADOW,
134*0a6a1f1dSLionel Sambuc       REG_OFFSET_SCRATCH
135*0a6a1f1dSLionel Sambuc     };
136*0a6a1f1dSLionel Sambuc 
137*0a6a1f1dSLionel Sambuc   public:
RegisterContextllvm::__anon4aa9f0860111::X86AddressSanitizer::RegisterContext138*0a6a1f1dSLionel Sambuc     RegisterContext(unsigned AddressReg, unsigned ShadowReg,
139*0a6a1f1dSLionel Sambuc                     unsigned ScratchReg) {
140*0a6a1f1dSLionel Sambuc       BusyRegs.push_back(convReg(AddressReg, MVT::i64));
141*0a6a1f1dSLionel Sambuc       BusyRegs.push_back(convReg(ShadowReg, MVT::i64));
142*0a6a1f1dSLionel Sambuc       BusyRegs.push_back(convReg(ScratchReg, MVT::i64));
143*0a6a1f1dSLionel Sambuc     }
144*0a6a1f1dSLionel Sambuc 
AddressRegllvm::__anon4aa9f0860111::X86AddressSanitizer::RegisterContext145*0a6a1f1dSLionel Sambuc     unsigned AddressReg(MVT::SimpleValueType VT) const {
146*0a6a1f1dSLionel Sambuc       return convReg(BusyRegs[REG_OFFSET_ADDRESS], VT);
147*0a6a1f1dSLionel Sambuc     }
148*0a6a1f1dSLionel Sambuc 
ShadowRegllvm::__anon4aa9f0860111::X86AddressSanitizer::RegisterContext149*0a6a1f1dSLionel Sambuc     unsigned ShadowReg(MVT::SimpleValueType VT) const {
150*0a6a1f1dSLionel Sambuc       return convReg(BusyRegs[REG_OFFSET_SHADOW], VT);
151*0a6a1f1dSLionel Sambuc     }
152*0a6a1f1dSLionel Sambuc 
ScratchRegllvm::__anon4aa9f0860111::X86AddressSanitizer::RegisterContext153*0a6a1f1dSLionel Sambuc     unsigned ScratchReg(MVT::SimpleValueType VT) const {
154*0a6a1f1dSLionel Sambuc       return convReg(BusyRegs[REG_OFFSET_SCRATCH], VT);
155*0a6a1f1dSLionel Sambuc     }
156*0a6a1f1dSLionel Sambuc 
AddBusyRegllvm::__anon4aa9f0860111::X86AddressSanitizer::RegisterContext157*0a6a1f1dSLionel Sambuc     void AddBusyReg(unsigned Reg) {
158*0a6a1f1dSLionel Sambuc       if (Reg != X86::NoRegister)
159*0a6a1f1dSLionel Sambuc         BusyRegs.push_back(convReg(Reg, MVT::i64));
160*0a6a1f1dSLionel Sambuc     }
161*0a6a1f1dSLionel Sambuc 
AddBusyRegsllvm::__anon4aa9f0860111::X86AddressSanitizer::RegisterContext162*0a6a1f1dSLionel Sambuc     void AddBusyRegs(const X86Operand &Op) {
163*0a6a1f1dSLionel Sambuc       AddBusyReg(Op.getMemBaseReg());
164*0a6a1f1dSLionel Sambuc       AddBusyReg(Op.getMemIndexReg());
165*0a6a1f1dSLionel Sambuc     }
166*0a6a1f1dSLionel Sambuc 
ChooseFrameRegllvm::__anon4aa9f0860111::X86AddressSanitizer::RegisterContext167*0a6a1f1dSLionel Sambuc     unsigned ChooseFrameReg(MVT::SimpleValueType VT) const {
168*0a6a1f1dSLionel Sambuc       static const MCPhysReg Candidates[] = { X86::RBP, X86::RAX, X86::RBX,
169*0a6a1f1dSLionel Sambuc                                               X86::RCX, X86::RDX, X86::RDI,
170*0a6a1f1dSLionel Sambuc                                               X86::RSI };
171*0a6a1f1dSLionel Sambuc       for (unsigned Reg : Candidates) {
172*0a6a1f1dSLionel Sambuc         if (!std::count(BusyRegs.begin(), BusyRegs.end(), Reg))
173*0a6a1f1dSLionel Sambuc           return convReg(Reg, VT);
174*0a6a1f1dSLionel Sambuc       }
175*0a6a1f1dSLionel Sambuc       return X86::NoRegister;
176*0a6a1f1dSLionel Sambuc     }
177*0a6a1f1dSLionel Sambuc 
178*0a6a1f1dSLionel Sambuc   private:
convRegllvm::__anon4aa9f0860111::X86AddressSanitizer::RegisterContext179*0a6a1f1dSLionel Sambuc     unsigned convReg(unsigned Reg, MVT::SimpleValueType VT) const {
180*0a6a1f1dSLionel Sambuc       return Reg == X86::NoRegister ? Reg : getX86SubSuperRegister(Reg, VT);
181*0a6a1f1dSLionel Sambuc     }
182*0a6a1f1dSLionel Sambuc 
183*0a6a1f1dSLionel Sambuc     std::vector<unsigned> BusyRegs;
184*0a6a1f1dSLionel Sambuc   };
185*0a6a1f1dSLionel Sambuc 
X86AddressSanitizer(const MCSubtargetInfo & STI)186*0a6a1f1dSLionel Sambuc   X86AddressSanitizer(const MCSubtargetInfo &STI)
187*0a6a1f1dSLionel Sambuc       : X86AsmInstrumentation(STI), RepPrefix(false), OrigSPOffset(0) {}
188*0a6a1f1dSLionel Sambuc 
~X86AddressSanitizer()189*0a6a1f1dSLionel Sambuc   virtual ~X86AddressSanitizer() {}
190*0a6a1f1dSLionel Sambuc 
191*0a6a1f1dSLionel Sambuc   // X86AsmInstrumentation implementation:
InstrumentAndEmitInstruction(const MCInst & Inst,OperandVector & Operands,MCContext & Ctx,const MCInstrInfo & MII,MCStreamer & Out)192*0a6a1f1dSLionel Sambuc   virtual void InstrumentAndEmitInstruction(const MCInst &Inst,
193*0a6a1f1dSLionel Sambuc                                             OperandVector &Operands,
194*0a6a1f1dSLionel Sambuc                                             MCContext &Ctx,
195*0a6a1f1dSLionel Sambuc                                             const MCInstrInfo &MII,
196*0a6a1f1dSLionel Sambuc                                             MCStreamer &Out) override {
197*0a6a1f1dSLionel Sambuc     InstrumentMOVS(Inst, Operands, Ctx, MII, Out);
198*0a6a1f1dSLionel Sambuc     if (RepPrefix)
199*0a6a1f1dSLionel Sambuc       EmitInstruction(Out, MCInstBuilder(X86::REP_PREFIX));
200*0a6a1f1dSLionel Sambuc 
201*0a6a1f1dSLionel Sambuc     InstrumentMOV(Inst, Operands, Ctx, MII, Out);
202*0a6a1f1dSLionel Sambuc 
203*0a6a1f1dSLionel Sambuc     RepPrefix = (Inst.getOpcode() == X86::REP_PREFIX);
204*0a6a1f1dSLionel Sambuc     if (!RepPrefix)
205*0a6a1f1dSLionel Sambuc       EmitInstruction(Out, Inst);
206*0a6a1f1dSLionel Sambuc   }
207*0a6a1f1dSLionel Sambuc 
208*0a6a1f1dSLionel Sambuc   // Adjusts up stack and saves all registers used in instrumentation.
209*0a6a1f1dSLionel Sambuc   virtual void InstrumentMemOperandPrologue(const RegisterContext &RegCtx,
210*0a6a1f1dSLionel Sambuc                                             MCContext &Ctx,
211*0a6a1f1dSLionel Sambuc                                             MCStreamer &Out) = 0;
212*0a6a1f1dSLionel Sambuc 
213*0a6a1f1dSLionel Sambuc   // Restores all registers used in instrumentation and adjusts stack.
214*0a6a1f1dSLionel Sambuc   virtual void InstrumentMemOperandEpilogue(const RegisterContext &RegCtx,
215*0a6a1f1dSLionel Sambuc                                             MCContext &Ctx,
216*0a6a1f1dSLionel Sambuc                                             MCStreamer &Out) = 0;
217*0a6a1f1dSLionel Sambuc 
218*0a6a1f1dSLionel Sambuc   virtual void InstrumentMemOperandSmall(X86Operand &Op, unsigned AccessSize,
219*0a6a1f1dSLionel Sambuc                                          bool IsWrite,
220*0a6a1f1dSLionel Sambuc                                          const RegisterContext &RegCtx,
221*0a6a1f1dSLionel Sambuc                                          MCContext &Ctx, MCStreamer &Out) = 0;
222*0a6a1f1dSLionel Sambuc   virtual void InstrumentMemOperandLarge(X86Operand &Op, unsigned AccessSize,
223*0a6a1f1dSLionel Sambuc                                          bool IsWrite,
224*0a6a1f1dSLionel Sambuc                                          const RegisterContext &RegCtx,
225*0a6a1f1dSLionel Sambuc                                          MCContext &Ctx, MCStreamer &Out) = 0;
226*0a6a1f1dSLionel Sambuc 
227*0a6a1f1dSLionel Sambuc   virtual void InstrumentMOVSImpl(unsigned AccessSize, MCContext &Ctx,
228*0a6a1f1dSLionel Sambuc                                   MCStreamer &Out) = 0;
229*0a6a1f1dSLionel Sambuc 
230*0a6a1f1dSLionel Sambuc   void InstrumentMemOperand(X86Operand &Op, unsigned AccessSize, bool IsWrite,
231*0a6a1f1dSLionel Sambuc                             const RegisterContext &RegCtx, MCContext &Ctx,
232*0a6a1f1dSLionel Sambuc                             MCStreamer &Out);
233*0a6a1f1dSLionel Sambuc   void InstrumentMOVSBase(unsigned DstReg, unsigned SrcReg, unsigned CntReg,
234*0a6a1f1dSLionel Sambuc                           unsigned AccessSize, MCContext &Ctx, MCStreamer &Out);
235*0a6a1f1dSLionel Sambuc 
236*0a6a1f1dSLionel Sambuc   void InstrumentMOVS(const MCInst &Inst, OperandVector &Operands,
237*0a6a1f1dSLionel Sambuc                       MCContext &Ctx, const MCInstrInfo &MII, MCStreamer &Out);
238*0a6a1f1dSLionel Sambuc   void InstrumentMOV(const MCInst &Inst, OperandVector &Operands,
239*0a6a1f1dSLionel Sambuc                      MCContext &Ctx, const MCInstrInfo &MII, MCStreamer &Out);
240*0a6a1f1dSLionel Sambuc 
241*0a6a1f1dSLionel Sambuc protected:
EmitLabel(MCStreamer & Out,MCSymbol * Label)242*0a6a1f1dSLionel Sambuc   void EmitLabel(MCStreamer &Out, MCSymbol *Label) { Out.EmitLabel(Label); }
243*0a6a1f1dSLionel Sambuc 
EmitLEA(X86Operand & Op,MVT::SimpleValueType VT,unsigned Reg,MCStreamer & Out)244*0a6a1f1dSLionel Sambuc   void EmitLEA(X86Operand &Op, MVT::SimpleValueType VT, unsigned Reg,
245*0a6a1f1dSLionel Sambuc                MCStreamer &Out) {
246*0a6a1f1dSLionel Sambuc     assert(VT == MVT::i32 || VT == MVT::i64);
247*0a6a1f1dSLionel Sambuc     MCInst Inst;
248*0a6a1f1dSLionel Sambuc     Inst.setOpcode(VT == MVT::i32 ? X86::LEA32r : X86::LEA64r);
249*0a6a1f1dSLionel Sambuc     Inst.addOperand(MCOperand::CreateReg(getX86SubSuperRegister(Reg, VT)));
250*0a6a1f1dSLionel Sambuc     Op.addMemOperands(Inst, 5);
251*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, Inst);
252*0a6a1f1dSLionel Sambuc   }
253*0a6a1f1dSLionel Sambuc 
254*0a6a1f1dSLionel Sambuc   void ComputeMemOperandAddress(X86Operand &Op, MVT::SimpleValueType VT,
255*0a6a1f1dSLionel Sambuc                                 unsigned Reg, MCContext &Ctx, MCStreamer &Out);
256*0a6a1f1dSLionel Sambuc 
257*0a6a1f1dSLionel Sambuc   // Creates new memory operand with Displacement added to an original
258*0a6a1f1dSLionel Sambuc   // displacement. Residue will contain a residue which could happen when the
259*0a6a1f1dSLionel Sambuc   // total displacement exceeds 32-bit limitation.
260*0a6a1f1dSLionel Sambuc   std::unique_ptr<X86Operand> AddDisplacement(X86Operand &Op,
261*0a6a1f1dSLionel Sambuc                                               int64_t Displacement,
262*0a6a1f1dSLionel Sambuc                                               MCContext &Ctx, int64_t *Residue);
263*0a6a1f1dSLionel Sambuc 
is64BitMode() const264*0a6a1f1dSLionel Sambuc   bool is64BitMode() const {
265*0a6a1f1dSLionel Sambuc     return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
266*0a6a1f1dSLionel Sambuc   }
is32BitMode() const267*0a6a1f1dSLionel Sambuc   bool is32BitMode() const {
268*0a6a1f1dSLionel Sambuc     return (STI.getFeatureBits() & X86::Mode32Bit) != 0;
269*0a6a1f1dSLionel Sambuc   }
is16BitMode() const270*0a6a1f1dSLionel Sambuc   bool is16BitMode() const {
271*0a6a1f1dSLionel Sambuc     return (STI.getFeatureBits() & X86::Mode16Bit) != 0;
272*0a6a1f1dSLionel Sambuc   }
273*0a6a1f1dSLionel Sambuc 
getPointerWidth()274*0a6a1f1dSLionel Sambuc   unsigned getPointerWidth() {
275*0a6a1f1dSLionel Sambuc     if (is16BitMode()) return 16;
276*0a6a1f1dSLionel Sambuc     if (is32BitMode()) return 32;
277*0a6a1f1dSLionel Sambuc     if (is64BitMode()) return 64;
278*0a6a1f1dSLionel Sambuc     llvm_unreachable("invalid mode");
279*0a6a1f1dSLionel Sambuc   }
280*0a6a1f1dSLionel Sambuc 
281*0a6a1f1dSLionel Sambuc   // True when previous instruction was actually REP prefix.
282*0a6a1f1dSLionel Sambuc   bool RepPrefix;
283*0a6a1f1dSLionel Sambuc 
284*0a6a1f1dSLionel Sambuc   // Offset from the original SP register.
285*0a6a1f1dSLionel Sambuc   int64_t OrigSPOffset;
286*0a6a1f1dSLionel Sambuc };
287*0a6a1f1dSLionel Sambuc 
InstrumentMemOperand(X86Operand & Op,unsigned AccessSize,bool IsWrite,const RegisterContext & RegCtx,MCContext & Ctx,MCStreamer & Out)288*0a6a1f1dSLionel Sambuc void X86AddressSanitizer::InstrumentMemOperand(
289*0a6a1f1dSLionel Sambuc     X86Operand &Op, unsigned AccessSize, bool IsWrite,
290*0a6a1f1dSLionel Sambuc     const RegisterContext &RegCtx, MCContext &Ctx, MCStreamer &Out) {
291*0a6a1f1dSLionel Sambuc   assert(Op.isMem() && "Op should be a memory operand.");
292*0a6a1f1dSLionel Sambuc   assert((AccessSize & (AccessSize - 1)) == 0 && AccessSize <= 16 &&
293*0a6a1f1dSLionel Sambuc          "AccessSize should be a power of two, less or equal than 16.");
294*0a6a1f1dSLionel Sambuc   // FIXME: take into account load/store alignment.
295*0a6a1f1dSLionel Sambuc   if (IsSmallMemAccess(AccessSize))
296*0a6a1f1dSLionel Sambuc     InstrumentMemOperandSmall(Op, AccessSize, IsWrite, RegCtx, Ctx, Out);
297*0a6a1f1dSLionel Sambuc   else
298*0a6a1f1dSLionel Sambuc     InstrumentMemOperandLarge(Op, AccessSize, IsWrite, RegCtx, Ctx, Out);
299*0a6a1f1dSLionel Sambuc }
300*0a6a1f1dSLionel Sambuc 
InstrumentMOVSBase(unsigned DstReg,unsigned SrcReg,unsigned CntReg,unsigned AccessSize,MCContext & Ctx,MCStreamer & Out)301*0a6a1f1dSLionel Sambuc void X86AddressSanitizer::InstrumentMOVSBase(unsigned DstReg, unsigned SrcReg,
302*0a6a1f1dSLionel Sambuc                                              unsigned CntReg,
303*0a6a1f1dSLionel Sambuc                                              unsigned AccessSize,
304*0a6a1f1dSLionel Sambuc                                              MCContext &Ctx, MCStreamer &Out) {
305*0a6a1f1dSLionel Sambuc   // FIXME: check whole ranges [DstReg .. DstReg + AccessSize * (CntReg - 1)]
306*0a6a1f1dSLionel Sambuc   // and [SrcReg .. SrcReg + AccessSize * (CntReg - 1)].
307*0a6a1f1dSLionel Sambuc   RegisterContext RegCtx(X86::RDX /* AddressReg */, X86::RAX /* ShadowReg */,
308*0a6a1f1dSLionel Sambuc                          IsSmallMemAccess(AccessSize)
309*0a6a1f1dSLionel Sambuc                              ? X86::RBX
310*0a6a1f1dSLionel Sambuc                              : X86::NoRegister /* ScratchReg */);
311*0a6a1f1dSLionel Sambuc   RegCtx.AddBusyReg(DstReg);
312*0a6a1f1dSLionel Sambuc   RegCtx.AddBusyReg(SrcReg);
313*0a6a1f1dSLionel Sambuc   RegCtx.AddBusyReg(CntReg);
314*0a6a1f1dSLionel Sambuc 
315*0a6a1f1dSLionel Sambuc   InstrumentMemOperandPrologue(RegCtx, Ctx, Out);
316*0a6a1f1dSLionel Sambuc 
317*0a6a1f1dSLionel Sambuc   // Test (%SrcReg)
318*0a6a1f1dSLionel Sambuc   {
319*0a6a1f1dSLionel Sambuc     const MCExpr *Disp = MCConstantExpr::Create(0, Ctx);
320*0a6a1f1dSLionel Sambuc     std::unique_ptr<X86Operand> Op(X86Operand::CreateMem(
321*0a6a1f1dSLionel Sambuc         getPointerWidth(), 0, Disp, SrcReg, 0, AccessSize, SMLoc(), SMLoc()));
322*0a6a1f1dSLionel Sambuc     InstrumentMemOperand(*Op, AccessSize, false /* IsWrite */, RegCtx, Ctx,
323*0a6a1f1dSLionel Sambuc                          Out);
324*0a6a1f1dSLionel Sambuc   }
325*0a6a1f1dSLionel Sambuc 
326*0a6a1f1dSLionel Sambuc   // Test -1(%SrcReg, %CntReg, AccessSize)
327*0a6a1f1dSLionel Sambuc   {
328*0a6a1f1dSLionel Sambuc     const MCExpr *Disp = MCConstantExpr::Create(-1, Ctx);
329*0a6a1f1dSLionel Sambuc     std::unique_ptr<X86Operand> Op(X86Operand::CreateMem(
330*0a6a1f1dSLionel Sambuc         getPointerWidth(), 0, Disp, SrcReg, CntReg, AccessSize, SMLoc(),
331*0a6a1f1dSLionel Sambuc         SMLoc()));
332*0a6a1f1dSLionel Sambuc     InstrumentMemOperand(*Op, AccessSize, false /* IsWrite */, RegCtx, Ctx,
333*0a6a1f1dSLionel Sambuc                          Out);
334*0a6a1f1dSLionel Sambuc   }
335*0a6a1f1dSLionel Sambuc 
336*0a6a1f1dSLionel Sambuc   // Test (%DstReg)
337*0a6a1f1dSLionel Sambuc   {
338*0a6a1f1dSLionel Sambuc     const MCExpr *Disp = MCConstantExpr::Create(0, Ctx);
339*0a6a1f1dSLionel Sambuc     std::unique_ptr<X86Operand> Op(X86Operand::CreateMem(
340*0a6a1f1dSLionel Sambuc         getPointerWidth(), 0, Disp, DstReg, 0, AccessSize, SMLoc(), SMLoc()));
341*0a6a1f1dSLionel Sambuc     InstrumentMemOperand(*Op, AccessSize, true /* IsWrite */, RegCtx, Ctx, Out);
342*0a6a1f1dSLionel Sambuc   }
343*0a6a1f1dSLionel Sambuc 
344*0a6a1f1dSLionel Sambuc   // Test -1(%DstReg, %CntReg, AccessSize)
345*0a6a1f1dSLionel Sambuc   {
346*0a6a1f1dSLionel Sambuc     const MCExpr *Disp = MCConstantExpr::Create(-1, Ctx);
347*0a6a1f1dSLionel Sambuc     std::unique_ptr<X86Operand> Op(X86Operand::CreateMem(
348*0a6a1f1dSLionel Sambuc         getPointerWidth(), 0, Disp, DstReg, CntReg, AccessSize, SMLoc(),
349*0a6a1f1dSLionel Sambuc         SMLoc()));
350*0a6a1f1dSLionel Sambuc     InstrumentMemOperand(*Op, AccessSize, true /* IsWrite */, RegCtx, Ctx, Out);
351*0a6a1f1dSLionel Sambuc   }
352*0a6a1f1dSLionel Sambuc 
353*0a6a1f1dSLionel Sambuc   InstrumentMemOperandEpilogue(RegCtx, Ctx, Out);
354*0a6a1f1dSLionel Sambuc }
355*0a6a1f1dSLionel Sambuc 
InstrumentMOVS(const MCInst & Inst,OperandVector & Operands,MCContext & Ctx,const MCInstrInfo & MII,MCStreamer & Out)356*0a6a1f1dSLionel Sambuc void X86AddressSanitizer::InstrumentMOVS(const MCInst &Inst,
357*0a6a1f1dSLionel Sambuc                                          OperandVector &Operands,
358*0a6a1f1dSLionel Sambuc                                          MCContext &Ctx, const MCInstrInfo &MII,
359*0a6a1f1dSLionel Sambuc                                          MCStreamer &Out) {
360*0a6a1f1dSLionel Sambuc   // Access size in bytes.
361*0a6a1f1dSLionel Sambuc   unsigned AccessSize = 0;
362*0a6a1f1dSLionel Sambuc 
363*0a6a1f1dSLionel Sambuc   switch (Inst.getOpcode()) {
364*0a6a1f1dSLionel Sambuc   case X86::MOVSB:
365*0a6a1f1dSLionel Sambuc     AccessSize = 1;
366*0a6a1f1dSLionel Sambuc     break;
367*0a6a1f1dSLionel Sambuc   case X86::MOVSW:
368*0a6a1f1dSLionel Sambuc     AccessSize = 2;
369*0a6a1f1dSLionel Sambuc     break;
370*0a6a1f1dSLionel Sambuc   case X86::MOVSL:
371*0a6a1f1dSLionel Sambuc     AccessSize = 4;
372*0a6a1f1dSLionel Sambuc     break;
373*0a6a1f1dSLionel Sambuc   case X86::MOVSQ:
374*0a6a1f1dSLionel Sambuc     AccessSize = 8;
375*0a6a1f1dSLionel Sambuc     break;
376*0a6a1f1dSLionel Sambuc   default:
377*0a6a1f1dSLionel Sambuc     return;
378*0a6a1f1dSLionel Sambuc   }
379*0a6a1f1dSLionel Sambuc 
380*0a6a1f1dSLionel Sambuc   InstrumentMOVSImpl(AccessSize, Ctx, Out);
381*0a6a1f1dSLionel Sambuc }
382*0a6a1f1dSLionel Sambuc 
InstrumentMOV(const MCInst & Inst,OperandVector & Operands,MCContext & Ctx,const MCInstrInfo & MII,MCStreamer & Out)383*0a6a1f1dSLionel Sambuc void X86AddressSanitizer::InstrumentMOV(const MCInst &Inst,
384*0a6a1f1dSLionel Sambuc                                         OperandVector &Operands, MCContext &Ctx,
385*0a6a1f1dSLionel Sambuc                                         const MCInstrInfo &MII,
386*0a6a1f1dSLionel Sambuc                                         MCStreamer &Out) {
387*0a6a1f1dSLionel Sambuc   // Access size in bytes.
388*0a6a1f1dSLionel Sambuc   unsigned AccessSize = 0;
389*0a6a1f1dSLionel Sambuc 
390*0a6a1f1dSLionel Sambuc   switch (Inst.getOpcode()) {
391*0a6a1f1dSLionel Sambuc   case X86::MOV8mi:
392*0a6a1f1dSLionel Sambuc   case X86::MOV8mr:
393*0a6a1f1dSLionel Sambuc   case X86::MOV8rm:
394*0a6a1f1dSLionel Sambuc     AccessSize = 1;
395*0a6a1f1dSLionel Sambuc     break;
396*0a6a1f1dSLionel Sambuc   case X86::MOV16mi:
397*0a6a1f1dSLionel Sambuc   case X86::MOV16mr:
398*0a6a1f1dSLionel Sambuc   case X86::MOV16rm:
399*0a6a1f1dSLionel Sambuc     AccessSize = 2;
400*0a6a1f1dSLionel Sambuc     break;
401*0a6a1f1dSLionel Sambuc   case X86::MOV32mi:
402*0a6a1f1dSLionel Sambuc   case X86::MOV32mr:
403*0a6a1f1dSLionel Sambuc   case X86::MOV32rm:
404*0a6a1f1dSLionel Sambuc     AccessSize = 4;
405*0a6a1f1dSLionel Sambuc     break;
406*0a6a1f1dSLionel Sambuc   case X86::MOV64mi32:
407*0a6a1f1dSLionel Sambuc   case X86::MOV64mr:
408*0a6a1f1dSLionel Sambuc   case X86::MOV64rm:
409*0a6a1f1dSLionel Sambuc     AccessSize = 8;
410*0a6a1f1dSLionel Sambuc     break;
411*0a6a1f1dSLionel Sambuc   case X86::MOVAPDmr:
412*0a6a1f1dSLionel Sambuc   case X86::MOVAPSmr:
413*0a6a1f1dSLionel Sambuc   case X86::MOVAPDrm:
414*0a6a1f1dSLionel Sambuc   case X86::MOVAPSrm:
415*0a6a1f1dSLionel Sambuc     AccessSize = 16;
416*0a6a1f1dSLionel Sambuc     break;
417*0a6a1f1dSLionel Sambuc   default:
418*0a6a1f1dSLionel Sambuc     return;
419*0a6a1f1dSLionel Sambuc   }
420*0a6a1f1dSLionel Sambuc 
421*0a6a1f1dSLionel Sambuc   const bool IsWrite = MII.get(Inst.getOpcode()).mayStore();
422*0a6a1f1dSLionel Sambuc 
423*0a6a1f1dSLionel Sambuc   for (unsigned Ix = 0; Ix < Operands.size(); ++Ix) {
424*0a6a1f1dSLionel Sambuc     assert(Operands[Ix]);
425*0a6a1f1dSLionel Sambuc     MCParsedAsmOperand &Op = *Operands[Ix];
426*0a6a1f1dSLionel Sambuc     if (Op.isMem()) {
427*0a6a1f1dSLionel Sambuc       X86Operand &MemOp = static_cast<X86Operand &>(Op);
428*0a6a1f1dSLionel Sambuc       RegisterContext RegCtx(
429*0a6a1f1dSLionel Sambuc           X86::RDI /* AddressReg */, X86::RAX /* ShadowReg */,
430*0a6a1f1dSLionel Sambuc           IsSmallMemAccess(AccessSize) ? X86::RCX
431*0a6a1f1dSLionel Sambuc                                        : X86::NoRegister /* ScratchReg */);
432*0a6a1f1dSLionel Sambuc       RegCtx.AddBusyRegs(MemOp);
433*0a6a1f1dSLionel Sambuc       InstrumentMemOperandPrologue(RegCtx, Ctx, Out);
434*0a6a1f1dSLionel Sambuc       InstrumentMemOperand(MemOp, AccessSize, IsWrite, RegCtx, Ctx, Out);
435*0a6a1f1dSLionel Sambuc       InstrumentMemOperandEpilogue(RegCtx, Ctx, Out);
436*0a6a1f1dSLionel Sambuc     }
437*0a6a1f1dSLionel Sambuc   }
438*0a6a1f1dSLionel Sambuc }
439*0a6a1f1dSLionel Sambuc 
ComputeMemOperandAddress(X86Operand & Op,MVT::SimpleValueType VT,unsigned Reg,MCContext & Ctx,MCStreamer & Out)440*0a6a1f1dSLionel Sambuc void X86AddressSanitizer::ComputeMemOperandAddress(X86Operand &Op,
441*0a6a1f1dSLionel Sambuc                                                    MVT::SimpleValueType VT,
442*0a6a1f1dSLionel Sambuc                                                    unsigned Reg, MCContext &Ctx,
443*0a6a1f1dSLionel Sambuc                                                    MCStreamer &Out) {
444*0a6a1f1dSLionel Sambuc   int64_t Displacement = 0;
445*0a6a1f1dSLionel Sambuc   if (IsStackReg(Op.getMemBaseReg()))
446*0a6a1f1dSLionel Sambuc     Displacement -= OrigSPOffset;
447*0a6a1f1dSLionel Sambuc   if (IsStackReg(Op.getMemIndexReg()))
448*0a6a1f1dSLionel Sambuc     Displacement -= OrigSPOffset * Op.getMemScale();
449*0a6a1f1dSLionel Sambuc 
450*0a6a1f1dSLionel Sambuc   assert(Displacement >= 0);
451*0a6a1f1dSLionel Sambuc 
452*0a6a1f1dSLionel Sambuc   // Emit Op as is.
453*0a6a1f1dSLionel Sambuc   if (Displacement == 0) {
454*0a6a1f1dSLionel Sambuc     EmitLEA(Op, VT, Reg, Out);
455*0a6a1f1dSLionel Sambuc     return;
456*0a6a1f1dSLionel Sambuc   }
457*0a6a1f1dSLionel Sambuc 
458*0a6a1f1dSLionel Sambuc   int64_t Residue;
459*0a6a1f1dSLionel Sambuc   std::unique_ptr<X86Operand> NewOp =
460*0a6a1f1dSLionel Sambuc       AddDisplacement(Op, Displacement, Ctx, &Residue);
461*0a6a1f1dSLionel Sambuc   EmitLEA(*NewOp, VT, Reg, Out);
462*0a6a1f1dSLionel Sambuc 
463*0a6a1f1dSLionel Sambuc   while (Residue != 0) {
464*0a6a1f1dSLionel Sambuc     const MCConstantExpr *Disp =
465*0a6a1f1dSLionel Sambuc         MCConstantExpr::Create(ApplyDisplacementBounds(Residue), Ctx);
466*0a6a1f1dSLionel Sambuc     std::unique_ptr<X86Operand> DispOp =
467*0a6a1f1dSLionel Sambuc         X86Operand::CreateMem(getPointerWidth(), 0, Disp, Reg, 0, 1, SMLoc(),
468*0a6a1f1dSLionel Sambuc                               SMLoc());
469*0a6a1f1dSLionel Sambuc     EmitLEA(*DispOp, VT, Reg, Out);
470*0a6a1f1dSLionel Sambuc     Residue -= Disp->getValue();
471*0a6a1f1dSLionel Sambuc   }
472*0a6a1f1dSLionel Sambuc }
473*0a6a1f1dSLionel Sambuc 
474*0a6a1f1dSLionel Sambuc std::unique_ptr<X86Operand>
AddDisplacement(X86Operand & Op,int64_t Displacement,MCContext & Ctx,int64_t * Residue)475*0a6a1f1dSLionel Sambuc X86AddressSanitizer::AddDisplacement(X86Operand &Op, int64_t Displacement,
476*0a6a1f1dSLionel Sambuc                                      MCContext &Ctx, int64_t *Residue) {
477*0a6a1f1dSLionel Sambuc   assert(Displacement >= 0);
478*0a6a1f1dSLionel Sambuc 
479*0a6a1f1dSLionel Sambuc   if (Displacement == 0 ||
480*0a6a1f1dSLionel Sambuc       (Op.getMemDisp() && Op.getMemDisp()->getKind() != MCExpr::Constant)) {
481*0a6a1f1dSLionel Sambuc     *Residue = Displacement;
482*0a6a1f1dSLionel Sambuc     return X86Operand::CreateMem(Op.getMemModeSize(), Op.getMemSegReg(),
483*0a6a1f1dSLionel Sambuc                                  Op.getMemDisp(), Op.getMemBaseReg(),
484*0a6a1f1dSLionel Sambuc                                  Op.getMemIndexReg(), Op.getMemScale(),
485*0a6a1f1dSLionel Sambuc                                  SMLoc(), SMLoc());
486*0a6a1f1dSLionel Sambuc   }
487*0a6a1f1dSLionel Sambuc 
488*0a6a1f1dSLionel Sambuc   int64_t OrigDisplacement =
489*0a6a1f1dSLionel Sambuc       static_cast<const MCConstantExpr *>(Op.getMemDisp())->getValue();
490*0a6a1f1dSLionel Sambuc   CheckDisplacementBounds(OrigDisplacement);
491*0a6a1f1dSLionel Sambuc   Displacement += OrigDisplacement;
492*0a6a1f1dSLionel Sambuc 
493*0a6a1f1dSLionel Sambuc   int64_t NewDisplacement = ApplyDisplacementBounds(Displacement);
494*0a6a1f1dSLionel Sambuc   CheckDisplacementBounds(NewDisplacement);
495*0a6a1f1dSLionel Sambuc 
496*0a6a1f1dSLionel Sambuc   *Residue = Displacement - NewDisplacement;
497*0a6a1f1dSLionel Sambuc   const MCExpr *Disp = MCConstantExpr::Create(NewDisplacement, Ctx);
498*0a6a1f1dSLionel Sambuc   return X86Operand::CreateMem(Op.getMemModeSize(), Op.getMemSegReg(), Disp,
499*0a6a1f1dSLionel Sambuc                                Op.getMemBaseReg(), Op.getMemIndexReg(),
500*0a6a1f1dSLionel Sambuc                                Op.getMemScale(), SMLoc(), SMLoc());
501*0a6a1f1dSLionel Sambuc }
502*0a6a1f1dSLionel Sambuc 
503*0a6a1f1dSLionel Sambuc class X86AddressSanitizer32 : public X86AddressSanitizer {
504*0a6a1f1dSLionel Sambuc public:
505*0a6a1f1dSLionel Sambuc   static const long kShadowOffset = 0x20000000;
506*0a6a1f1dSLionel Sambuc 
X86AddressSanitizer32(const MCSubtargetInfo & STI)507*0a6a1f1dSLionel Sambuc   X86AddressSanitizer32(const MCSubtargetInfo &STI)
508*0a6a1f1dSLionel Sambuc       : X86AddressSanitizer(STI) {}
509*0a6a1f1dSLionel Sambuc 
~X86AddressSanitizer32()510*0a6a1f1dSLionel Sambuc   virtual ~X86AddressSanitizer32() {}
511*0a6a1f1dSLionel Sambuc 
GetFrameReg(const MCContext & Ctx,MCStreamer & Out)512*0a6a1f1dSLionel Sambuc   unsigned GetFrameReg(const MCContext &Ctx, MCStreamer &Out) {
513*0a6a1f1dSLionel Sambuc     unsigned FrameReg = GetFrameRegGeneric(Ctx, Out);
514*0a6a1f1dSLionel Sambuc     if (FrameReg == X86::NoRegister)
515*0a6a1f1dSLionel Sambuc       return FrameReg;
516*0a6a1f1dSLionel Sambuc     return getX86SubSuperRegister(FrameReg, MVT::i32);
517*0a6a1f1dSLionel Sambuc   }
518*0a6a1f1dSLionel Sambuc 
SpillReg(MCStreamer & Out,unsigned Reg)519*0a6a1f1dSLionel Sambuc   void SpillReg(MCStreamer &Out, unsigned Reg) {
520*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::PUSH32r).addReg(Reg));
521*0a6a1f1dSLionel Sambuc     OrigSPOffset -= 4;
522*0a6a1f1dSLionel Sambuc   }
523*0a6a1f1dSLionel Sambuc 
RestoreReg(MCStreamer & Out,unsigned Reg)524*0a6a1f1dSLionel Sambuc   void RestoreReg(MCStreamer &Out, unsigned Reg) {
525*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::POP32r).addReg(Reg));
526*0a6a1f1dSLionel Sambuc     OrigSPOffset += 4;
527*0a6a1f1dSLionel Sambuc   }
528*0a6a1f1dSLionel Sambuc 
StoreFlags(MCStreamer & Out)529*0a6a1f1dSLionel Sambuc   void StoreFlags(MCStreamer &Out) {
530*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::PUSHF32));
531*0a6a1f1dSLionel Sambuc     OrigSPOffset -= 4;
532*0a6a1f1dSLionel Sambuc   }
533*0a6a1f1dSLionel Sambuc 
RestoreFlags(MCStreamer & Out)534*0a6a1f1dSLionel Sambuc   void RestoreFlags(MCStreamer &Out) {
535*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::POPF32));
536*0a6a1f1dSLionel Sambuc     OrigSPOffset += 4;
537*0a6a1f1dSLionel Sambuc   }
538*0a6a1f1dSLionel Sambuc 
InstrumentMemOperandPrologue(const RegisterContext & RegCtx,MCContext & Ctx,MCStreamer & Out)539*0a6a1f1dSLionel Sambuc   virtual void InstrumentMemOperandPrologue(const RegisterContext &RegCtx,
540*0a6a1f1dSLionel Sambuc                                             MCContext &Ctx,
541*0a6a1f1dSLionel Sambuc                                             MCStreamer &Out) override {
542*0a6a1f1dSLionel Sambuc     unsigned LocalFrameReg = RegCtx.ChooseFrameReg(MVT::i32);
543*0a6a1f1dSLionel Sambuc     assert(LocalFrameReg != X86::NoRegister);
544*0a6a1f1dSLionel Sambuc 
545*0a6a1f1dSLionel Sambuc     const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
546*0a6a1f1dSLionel Sambuc     unsigned FrameReg = GetFrameReg(Ctx, Out);
547*0a6a1f1dSLionel Sambuc     if (MRI && FrameReg != X86::NoRegister) {
548*0a6a1f1dSLionel Sambuc       SpillReg(Out, LocalFrameReg);
549*0a6a1f1dSLionel Sambuc       if (FrameReg == X86::ESP) {
550*0a6a1f1dSLionel Sambuc         Out.EmitCFIAdjustCfaOffset(4 /* byte size of the LocalFrameReg */);
551*0a6a1f1dSLionel Sambuc         Out.EmitCFIRelOffset(
552*0a6a1f1dSLionel Sambuc             MRI->getDwarfRegNum(LocalFrameReg, true /* IsEH */), 0);
553*0a6a1f1dSLionel Sambuc       }
554*0a6a1f1dSLionel Sambuc       EmitInstruction(
555*0a6a1f1dSLionel Sambuc           Out,
556*0a6a1f1dSLionel Sambuc           MCInstBuilder(X86::MOV32rr).addReg(LocalFrameReg).addReg(FrameReg));
557*0a6a1f1dSLionel Sambuc       Out.EmitCFIRememberState();
558*0a6a1f1dSLionel Sambuc       Out.EmitCFIDefCfaRegister(
559*0a6a1f1dSLionel Sambuc           MRI->getDwarfRegNum(LocalFrameReg, true /* IsEH */));
560*0a6a1f1dSLionel Sambuc     }
561*0a6a1f1dSLionel Sambuc 
562*0a6a1f1dSLionel Sambuc     SpillReg(Out, RegCtx.AddressReg(MVT::i32));
563*0a6a1f1dSLionel Sambuc     SpillReg(Out, RegCtx.ShadowReg(MVT::i32));
564*0a6a1f1dSLionel Sambuc     if (RegCtx.ScratchReg(MVT::i32) != X86::NoRegister)
565*0a6a1f1dSLionel Sambuc       SpillReg(Out, RegCtx.ScratchReg(MVT::i32));
566*0a6a1f1dSLionel Sambuc     StoreFlags(Out);
567*0a6a1f1dSLionel Sambuc   }
568*0a6a1f1dSLionel Sambuc 
InstrumentMemOperandEpilogue(const RegisterContext & RegCtx,MCContext & Ctx,MCStreamer & Out)569*0a6a1f1dSLionel Sambuc   virtual void InstrumentMemOperandEpilogue(const RegisterContext &RegCtx,
570*0a6a1f1dSLionel Sambuc                                             MCContext &Ctx,
571*0a6a1f1dSLionel Sambuc                                             MCStreamer &Out) override {
572*0a6a1f1dSLionel Sambuc     unsigned LocalFrameReg = RegCtx.ChooseFrameReg(MVT::i32);
573*0a6a1f1dSLionel Sambuc     assert(LocalFrameReg != X86::NoRegister);
574*0a6a1f1dSLionel Sambuc 
575*0a6a1f1dSLionel Sambuc     RestoreFlags(Out);
576*0a6a1f1dSLionel Sambuc     if (RegCtx.ScratchReg(MVT::i32) != X86::NoRegister)
577*0a6a1f1dSLionel Sambuc       RestoreReg(Out, RegCtx.ScratchReg(MVT::i32));
578*0a6a1f1dSLionel Sambuc     RestoreReg(Out, RegCtx.ShadowReg(MVT::i32));
579*0a6a1f1dSLionel Sambuc     RestoreReg(Out, RegCtx.AddressReg(MVT::i32));
580*0a6a1f1dSLionel Sambuc 
581*0a6a1f1dSLionel Sambuc     unsigned FrameReg = GetFrameReg(Ctx, Out);
582*0a6a1f1dSLionel Sambuc     if (Ctx.getRegisterInfo() && FrameReg != X86::NoRegister) {
583*0a6a1f1dSLionel Sambuc       RestoreReg(Out, LocalFrameReg);
584*0a6a1f1dSLionel Sambuc       Out.EmitCFIRestoreState();
585*0a6a1f1dSLionel Sambuc       if (FrameReg == X86::ESP)
586*0a6a1f1dSLionel Sambuc         Out.EmitCFIAdjustCfaOffset(-4 /* byte size of the LocalFrameReg */);
587*0a6a1f1dSLionel Sambuc     }
588*0a6a1f1dSLionel Sambuc   }
589*0a6a1f1dSLionel Sambuc 
590*0a6a1f1dSLionel Sambuc   virtual void InstrumentMemOperandSmall(X86Operand &Op, unsigned AccessSize,
591*0a6a1f1dSLionel Sambuc                                          bool IsWrite,
592*0a6a1f1dSLionel Sambuc                                          const RegisterContext &RegCtx,
593*0a6a1f1dSLionel Sambuc                                          MCContext &Ctx,
594*0a6a1f1dSLionel Sambuc                                          MCStreamer &Out) override;
595*0a6a1f1dSLionel Sambuc   virtual void InstrumentMemOperandLarge(X86Operand &Op, unsigned AccessSize,
596*0a6a1f1dSLionel Sambuc                                          bool IsWrite,
597*0a6a1f1dSLionel Sambuc                                          const RegisterContext &RegCtx,
598*0a6a1f1dSLionel Sambuc                                          MCContext &Ctx,
599*0a6a1f1dSLionel Sambuc                                          MCStreamer &Out) override;
600*0a6a1f1dSLionel Sambuc   virtual void InstrumentMOVSImpl(unsigned AccessSize, MCContext &Ctx,
601*0a6a1f1dSLionel Sambuc                                   MCStreamer &Out) override;
602*0a6a1f1dSLionel Sambuc 
603*0a6a1f1dSLionel Sambuc private:
EmitCallAsanReport(unsigned AccessSize,bool IsWrite,MCContext & Ctx,MCStreamer & Out,const RegisterContext & RegCtx)604*0a6a1f1dSLionel Sambuc   void EmitCallAsanReport(unsigned AccessSize, bool IsWrite, MCContext &Ctx,
605*0a6a1f1dSLionel Sambuc                           MCStreamer &Out, const RegisterContext &RegCtx) {
606*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::CLD));
607*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::MMX_EMMS));
608*0a6a1f1dSLionel Sambuc 
609*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::AND64ri8)
610*0a6a1f1dSLionel Sambuc                              .addReg(X86::ESP)
611*0a6a1f1dSLionel Sambuc                              .addReg(X86::ESP)
612*0a6a1f1dSLionel Sambuc                              .addImm(-16));
613*0a6a1f1dSLionel Sambuc     EmitInstruction(
614*0a6a1f1dSLionel Sambuc         Out, MCInstBuilder(X86::PUSH32r).addReg(RegCtx.AddressReg(MVT::i32)));
615*0a6a1f1dSLionel Sambuc 
616*0a6a1f1dSLionel Sambuc     const std::string &Fn = FuncName(AccessSize, IsWrite);
617*0a6a1f1dSLionel Sambuc     MCSymbol *FnSym = Ctx.GetOrCreateSymbol(StringRef(Fn));
618*0a6a1f1dSLionel Sambuc     const MCSymbolRefExpr *FnExpr =
619*0a6a1f1dSLionel Sambuc         MCSymbolRefExpr::Create(FnSym, MCSymbolRefExpr::VK_PLT, Ctx);
620*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::CALLpcrel32).addExpr(FnExpr));
621*0a6a1f1dSLionel Sambuc   }
622*0a6a1f1dSLionel Sambuc };
623*0a6a1f1dSLionel Sambuc 
InstrumentMemOperandSmall(X86Operand & Op,unsigned AccessSize,bool IsWrite,const RegisterContext & RegCtx,MCContext & Ctx,MCStreamer & Out)624*0a6a1f1dSLionel Sambuc void X86AddressSanitizer32::InstrumentMemOperandSmall(
625*0a6a1f1dSLionel Sambuc     X86Operand &Op, unsigned AccessSize, bool IsWrite,
626*0a6a1f1dSLionel Sambuc     const RegisterContext &RegCtx, MCContext &Ctx, MCStreamer &Out) {
627*0a6a1f1dSLionel Sambuc   unsigned AddressRegI32 = RegCtx.AddressReg(MVT::i32);
628*0a6a1f1dSLionel Sambuc   unsigned ShadowRegI32 = RegCtx.ShadowReg(MVT::i32);
629*0a6a1f1dSLionel Sambuc   unsigned ShadowRegI8 = RegCtx.ShadowReg(MVT::i8);
630*0a6a1f1dSLionel Sambuc 
631*0a6a1f1dSLionel Sambuc   assert(RegCtx.ScratchReg(MVT::i32) != X86::NoRegister);
632*0a6a1f1dSLionel Sambuc   unsigned ScratchRegI32 = RegCtx.ScratchReg(MVT::i32);
633*0a6a1f1dSLionel Sambuc 
634*0a6a1f1dSLionel Sambuc   ComputeMemOperandAddress(Op, MVT::i32, AddressRegI32, Ctx, Out);
635*0a6a1f1dSLionel Sambuc 
636*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::MOV32rr).addReg(ShadowRegI32).addReg(
637*0a6a1f1dSLionel Sambuc                            AddressRegI32));
638*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::SHR32ri)
639*0a6a1f1dSLionel Sambuc                            .addReg(ShadowRegI32)
640*0a6a1f1dSLionel Sambuc                            .addReg(ShadowRegI32)
641*0a6a1f1dSLionel Sambuc                            .addImm(3));
642*0a6a1f1dSLionel Sambuc 
643*0a6a1f1dSLionel Sambuc   {
644*0a6a1f1dSLionel Sambuc     MCInst Inst;
645*0a6a1f1dSLionel Sambuc     Inst.setOpcode(X86::MOV8rm);
646*0a6a1f1dSLionel Sambuc     Inst.addOperand(MCOperand::CreateReg(ShadowRegI8));
647*0a6a1f1dSLionel Sambuc     const MCExpr *Disp = MCConstantExpr::Create(kShadowOffset, Ctx);
648*0a6a1f1dSLionel Sambuc     std::unique_ptr<X86Operand> Op(
649*0a6a1f1dSLionel Sambuc         X86Operand::CreateMem(getPointerWidth(), 0, Disp, ShadowRegI32, 0, 1,
650*0a6a1f1dSLionel Sambuc                               SMLoc(), SMLoc()));
651*0a6a1f1dSLionel Sambuc     Op->addMemOperands(Inst, 5);
652*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, Inst);
653*0a6a1f1dSLionel Sambuc   }
654*0a6a1f1dSLionel Sambuc 
655*0a6a1f1dSLionel Sambuc   EmitInstruction(
656*0a6a1f1dSLionel Sambuc       Out, MCInstBuilder(X86::TEST8rr).addReg(ShadowRegI8).addReg(ShadowRegI8));
657*0a6a1f1dSLionel Sambuc   MCSymbol *DoneSym = Ctx.CreateTempSymbol();
658*0a6a1f1dSLionel Sambuc   const MCExpr *DoneExpr = MCSymbolRefExpr::Create(DoneSym, Ctx);
659*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::JE_1).addExpr(DoneExpr));
660*0a6a1f1dSLionel Sambuc 
661*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::MOV32rr).addReg(ScratchRegI32).addReg(
662*0a6a1f1dSLionel Sambuc                            AddressRegI32));
663*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::AND32ri)
664*0a6a1f1dSLionel Sambuc                            .addReg(ScratchRegI32)
665*0a6a1f1dSLionel Sambuc                            .addReg(ScratchRegI32)
666*0a6a1f1dSLionel Sambuc                            .addImm(7));
667*0a6a1f1dSLionel Sambuc 
668*0a6a1f1dSLionel Sambuc   switch (AccessSize) {
669*0a6a1f1dSLionel Sambuc   default: llvm_unreachable("Incorrect access size");
670*0a6a1f1dSLionel Sambuc   case 1:
671*0a6a1f1dSLionel Sambuc     break;
672*0a6a1f1dSLionel Sambuc   case 2: {
673*0a6a1f1dSLionel Sambuc     const MCExpr *Disp = MCConstantExpr::Create(1, Ctx);
674*0a6a1f1dSLionel Sambuc     std::unique_ptr<X86Operand> Op(
675*0a6a1f1dSLionel Sambuc         X86Operand::CreateMem(getPointerWidth(), 0, Disp, ScratchRegI32, 0, 1,
676*0a6a1f1dSLionel Sambuc                               SMLoc(), SMLoc()));
677*0a6a1f1dSLionel Sambuc     EmitLEA(*Op, MVT::i32, ScratchRegI32, Out);
678*0a6a1f1dSLionel Sambuc     break;
679*0a6a1f1dSLionel Sambuc   }
680*0a6a1f1dSLionel Sambuc   case 4:
681*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::ADD32ri8)
682*0a6a1f1dSLionel Sambuc                              .addReg(ScratchRegI32)
683*0a6a1f1dSLionel Sambuc                              .addReg(ScratchRegI32)
684*0a6a1f1dSLionel Sambuc                              .addImm(3));
685*0a6a1f1dSLionel Sambuc     break;
686*0a6a1f1dSLionel Sambuc   }
687*0a6a1f1dSLionel Sambuc 
688*0a6a1f1dSLionel Sambuc   EmitInstruction(
689*0a6a1f1dSLionel Sambuc       Out,
690*0a6a1f1dSLionel Sambuc       MCInstBuilder(X86::MOVSX32rr8).addReg(ShadowRegI32).addReg(ShadowRegI8));
691*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::CMP32rr).addReg(ScratchRegI32).addReg(
692*0a6a1f1dSLionel Sambuc                            ShadowRegI32));
693*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::JL_1).addExpr(DoneExpr));
694*0a6a1f1dSLionel Sambuc 
695*0a6a1f1dSLionel Sambuc   EmitCallAsanReport(AccessSize, IsWrite, Ctx, Out, RegCtx);
696*0a6a1f1dSLionel Sambuc   EmitLabel(Out, DoneSym);
697*0a6a1f1dSLionel Sambuc }
698*0a6a1f1dSLionel Sambuc 
InstrumentMemOperandLarge(X86Operand & Op,unsigned AccessSize,bool IsWrite,const RegisterContext & RegCtx,MCContext & Ctx,MCStreamer & Out)699*0a6a1f1dSLionel Sambuc void X86AddressSanitizer32::InstrumentMemOperandLarge(
700*0a6a1f1dSLionel Sambuc     X86Operand &Op, unsigned AccessSize, bool IsWrite,
701*0a6a1f1dSLionel Sambuc     const RegisterContext &RegCtx, MCContext &Ctx, MCStreamer &Out) {
702*0a6a1f1dSLionel Sambuc   unsigned AddressRegI32 = RegCtx.AddressReg(MVT::i32);
703*0a6a1f1dSLionel Sambuc   unsigned ShadowRegI32 = RegCtx.ShadowReg(MVT::i32);
704*0a6a1f1dSLionel Sambuc 
705*0a6a1f1dSLionel Sambuc   ComputeMemOperandAddress(Op, MVT::i32, AddressRegI32, Ctx, Out);
706*0a6a1f1dSLionel Sambuc 
707*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::MOV32rr).addReg(ShadowRegI32).addReg(
708*0a6a1f1dSLionel Sambuc                            AddressRegI32));
709*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::SHR32ri)
710*0a6a1f1dSLionel Sambuc                            .addReg(ShadowRegI32)
711*0a6a1f1dSLionel Sambuc                            .addReg(ShadowRegI32)
712*0a6a1f1dSLionel Sambuc                            .addImm(3));
713*0a6a1f1dSLionel Sambuc   {
714*0a6a1f1dSLionel Sambuc     MCInst Inst;
715*0a6a1f1dSLionel Sambuc     switch (AccessSize) {
716*0a6a1f1dSLionel Sambuc     default: llvm_unreachable("Incorrect access size");
717*0a6a1f1dSLionel Sambuc     case 8:
718*0a6a1f1dSLionel Sambuc       Inst.setOpcode(X86::CMP8mi);
719*0a6a1f1dSLionel Sambuc       break;
720*0a6a1f1dSLionel Sambuc     case 16:
721*0a6a1f1dSLionel Sambuc       Inst.setOpcode(X86::CMP16mi);
722*0a6a1f1dSLionel Sambuc       break;
723*0a6a1f1dSLionel Sambuc     }
724*0a6a1f1dSLionel Sambuc     const MCExpr *Disp = MCConstantExpr::Create(kShadowOffset, Ctx);
725*0a6a1f1dSLionel Sambuc     std::unique_ptr<X86Operand> Op(
726*0a6a1f1dSLionel Sambuc         X86Operand::CreateMem(getPointerWidth(), 0, Disp, ShadowRegI32, 0, 1,
727*0a6a1f1dSLionel Sambuc                               SMLoc(), SMLoc()));
728*0a6a1f1dSLionel Sambuc     Op->addMemOperands(Inst, 5);
729*0a6a1f1dSLionel Sambuc     Inst.addOperand(MCOperand::CreateImm(0));
730*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, Inst);
731*0a6a1f1dSLionel Sambuc   }
732*0a6a1f1dSLionel Sambuc   MCSymbol *DoneSym = Ctx.CreateTempSymbol();
733*0a6a1f1dSLionel Sambuc   const MCExpr *DoneExpr = MCSymbolRefExpr::Create(DoneSym, Ctx);
734*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::JE_1).addExpr(DoneExpr));
735*0a6a1f1dSLionel Sambuc 
736*0a6a1f1dSLionel Sambuc   EmitCallAsanReport(AccessSize, IsWrite, Ctx, Out, RegCtx);
737*0a6a1f1dSLionel Sambuc   EmitLabel(Out, DoneSym);
738*0a6a1f1dSLionel Sambuc }
739*0a6a1f1dSLionel Sambuc 
InstrumentMOVSImpl(unsigned AccessSize,MCContext & Ctx,MCStreamer & Out)740*0a6a1f1dSLionel Sambuc void X86AddressSanitizer32::InstrumentMOVSImpl(unsigned AccessSize,
741*0a6a1f1dSLionel Sambuc                                                MCContext &Ctx,
742*0a6a1f1dSLionel Sambuc                                                MCStreamer &Out) {
743*0a6a1f1dSLionel Sambuc   StoreFlags(Out);
744*0a6a1f1dSLionel Sambuc 
745*0a6a1f1dSLionel Sambuc   // No need to test when ECX is equals to zero.
746*0a6a1f1dSLionel Sambuc   MCSymbol *DoneSym = Ctx.CreateTempSymbol();
747*0a6a1f1dSLionel Sambuc   const MCExpr *DoneExpr = MCSymbolRefExpr::Create(DoneSym, Ctx);
748*0a6a1f1dSLionel Sambuc   EmitInstruction(
749*0a6a1f1dSLionel Sambuc       Out, MCInstBuilder(X86::TEST32rr).addReg(X86::ECX).addReg(X86::ECX));
750*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::JE_1).addExpr(DoneExpr));
751*0a6a1f1dSLionel Sambuc 
752*0a6a1f1dSLionel Sambuc   // Instrument first and last elements in src and dst range.
753*0a6a1f1dSLionel Sambuc   InstrumentMOVSBase(X86::EDI /* DstReg */, X86::ESI /* SrcReg */,
754*0a6a1f1dSLionel Sambuc                      X86::ECX /* CntReg */, AccessSize, Ctx, Out);
755*0a6a1f1dSLionel Sambuc 
756*0a6a1f1dSLionel Sambuc   EmitLabel(Out, DoneSym);
757*0a6a1f1dSLionel Sambuc   RestoreFlags(Out);
758*0a6a1f1dSLionel Sambuc }
759*0a6a1f1dSLionel Sambuc 
760*0a6a1f1dSLionel Sambuc class X86AddressSanitizer64 : public X86AddressSanitizer {
761*0a6a1f1dSLionel Sambuc public:
762*0a6a1f1dSLionel Sambuc   static const long kShadowOffset = 0x7fff8000;
763*0a6a1f1dSLionel Sambuc 
X86AddressSanitizer64(const MCSubtargetInfo & STI)764*0a6a1f1dSLionel Sambuc   X86AddressSanitizer64(const MCSubtargetInfo &STI)
765*0a6a1f1dSLionel Sambuc       : X86AddressSanitizer(STI) {}
766*0a6a1f1dSLionel Sambuc 
~X86AddressSanitizer64()767*0a6a1f1dSLionel Sambuc   virtual ~X86AddressSanitizer64() {}
768*0a6a1f1dSLionel Sambuc 
GetFrameReg(const MCContext & Ctx,MCStreamer & Out)769*0a6a1f1dSLionel Sambuc   unsigned GetFrameReg(const MCContext &Ctx, MCStreamer &Out) {
770*0a6a1f1dSLionel Sambuc     unsigned FrameReg = GetFrameRegGeneric(Ctx, Out);
771*0a6a1f1dSLionel Sambuc     if (FrameReg == X86::NoRegister)
772*0a6a1f1dSLionel Sambuc       return FrameReg;
773*0a6a1f1dSLionel Sambuc     return getX86SubSuperRegister(FrameReg, MVT::i64);
774*0a6a1f1dSLionel Sambuc   }
775*0a6a1f1dSLionel Sambuc 
SpillReg(MCStreamer & Out,unsigned Reg)776*0a6a1f1dSLionel Sambuc   void SpillReg(MCStreamer &Out, unsigned Reg) {
777*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::PUSH64r).addReg(Reg));
778*0a6a1f1dSLionel Sambuc     OrigSPOffset -= 8;
779*0a6a1f1dSLionel Sambuc   }
780*0a6a1f1dSLionel Sambuc 
RestoreReg(MCStreamer & Out,unsigned Reg)781*0a6a1f1dSLionel Sambuc   void RestoreReg(MCStreamer &Out, unsigned Reg) {
782*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::POP64r).addReg(Reg));
783*0a6a1f1dSLionel Sambuc     OrigSPOffset += 8;
784*0a6a1f1dSLionel Sambuc   }
785*0a6a1f1dSLionel Sambuc 
StoreFlags(MCStreamer & Out)786*0a6a1f1dSLionel Sambuc   void StoreFlags(MCStreamer &Out) {
787*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::PUSHF64));
788*0a6a1f1dSLionel Sambuc     OrigSPOffset -= 8;
789*0a6a1f1dSLionel Sambuc   }
790*0a6a1f1dSLionel Sambuc 
RestoreFlags(MCStreamer & Out)791*0a6a1f1dSLionel Sambuc   void RestoreFlags(MCStreamer &Out) {
792*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::POPF64));
793*0a6a1f1dSLionel Sambuc     OrigSPOffset += 8;
794*0a6a1f1dSLionel Sambuc   }
795*0a6a1f1dSLionel Sambuc 
InstrumentMemOperandPrologue(const RegisterContext & RegCtx,MCContext & Ctx,MCStreamer & Out)796*0a6a1f1dSLionel Sambuc   virtual void InstrumentMemOperandPrologue(const RegisterContext &RegCtx,
797*0a6a1f1dSLionel Sambuc                                             MCContext &Ctx,
798*0a6a1f1dSLionel Sambuc                                             MCStreamer &Out) override {
799*0a6a1f1dSLionel Sambuc     unsigned LocalFrameReg = RegCtx.ChooseFrameReg(MVT::i64);
800*0a6a1f1dSLionel Sambuc     assert(LocalFrameReg != X86::NoRegister);
801*0a6a1f1dSLionel Sambuc 
802*0a6a1f1dSLionel Sambuc     const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
803*0a6a1f1dSLionel Sambuc     unsigned FrameReg = GetFrameReg(Ctx, Out);
804*0a6a1f1dSLionel Sambuc     if (MRI && FrameReg != X86::NoRegister) {
805*0a6a1f1dSLionel Sambuc       SpillReg(Out, X86::RBP);
806*0a6a1f1dSLionel Sambuc       if (FrameReg == X86::RSP) {
807*0a6a1f1dSLionel Sambuc         Out.EmitCFIAdjustCfaOffset(8 /* byte size of the LocalFrameReg */);
808*0a6a1f1dSLionel Sambuc         Out.EmitCFIRelOffset(
809*0a6a1f1dSLionel Sambuc             MRI->getDwarfRegNum(LocalFrameReg, true /* IsEH */), 0);
810*0a6a1f1dSLionel Sambuc       }
811*0a6a1f1dSLionel Sambuc       EmitInstruction(
812*0a6a1f1dSLionel Sambuc           Out,
813*0a6a1f1dSLionel Sambuc           MCInstBuilder(X86::MOV64rr).addReg(LocalFrameReg).addReg(FrameReg));
814*0a6a1f1dSLionel Sambuc       Out.EmitCFIRememberState();
815*0a6a1f1dSLionel Sambuc       Out.EmitCFIDefCfaRegister(
816*0a6a1f1dSLionel Sambuc           MRI->getDwarfRegNum(LocalFrameReg, true /* IsEH */));
817*0a6a1f1dSLionel Sambuc     }
818*0a6a1f1dSLionel Sambuc 
819*0a6a1f1dSLionel Sambuc     EmitAdjustRSP(Ctx, Out, -128);
820*0a6a1f1dSLionel Sambuc     SpillReg(Out, RegCtx.ShadowReg(MVT::i64));
821*0a6a1f1dSLionel Sambuc     SpillReg(Out, RegCtx.AddressReg(MVT::i64));
822*0a6a1f1dSLionel Sambuc     if (RegCtx.ScratchReg(MVT::i64) != X86::NoRegister)
823*0a6a1f1dSLionel Sambuc       SpillReg(Out, RegCtx.ScratchReg(MVT::i64));
824*0a6a1f1dSLionel Sambuc     StoreFlags(Out);
825*0a6a1f1dSLionel Sambuc   }
826*0a6a1f1dSLionel Sambuc 
InstrumentMemOperandEpilogue(const RegisterContext & RegCtx,MCContext & Ctx,MCStreamer & Out)827*0a6a1f1dSLionel Sambuc   virtual void InstrumentMemOperandEpilogue(const RegisterContext &RegCtx,
828*0a6a1f1dSLionel Sambuc                                             MCContext &Ctx,
829*0a6a1f1dSLionel Sambuc                                             MCStreamer &Out) override {
830*0a6a1f1dSLionel Sambuc     unsigned LocalFrameReg = RegCtx.ChooseFrameReg(MVT::i64);
831*0a6a1f1dSLionel Sambuc     assert(LocalFrameReg != X86::NoRegister);
832*0a6a1f1dSLionel Sambuc 
833*0a6a1f1dSLionel Sambuc     RestoreFlags(Out);
834*0a6a1f1dSLionel Sambuc     if (RegCtx.ScratchReg(MVT::i64) != X86::NoRegister)
835*0a6a1f1dSLionel Sambuc       RestoreReg(Out, RegCtx.ScratchReg(MVT::i64));
836*0a6a1f1dSLionel Sambuc     RestoreReg(Out, RegCtx.AddressReg(MVT::i64));
837*0a6a1f1dSLionel Sambuc     RestoreReg(Out, RegCtx.ShadowReg(MVT::i64));
838*0a6a1f1dSLionel Sambuc     EmitAdjustRSP(Ctx, Out, 128);
839*0a6a1f1dSLionel Sambuc 
840*0a6a1f1dSLionel Sambuc     unsigned FrameReg = GetFrameReg(Ctx, Out);
841*0a6a1f1dSLionel Sambuc     if (Ctx.getRegisterInfo() && FrameReg != X86::NoRegister) {
842*0a6a1f1dSLionel Sambuc       RestoreReg(Out, LocalFrameReg);
843*0a6a1f1dSLionel Sambuc       Out.EmitCFIRestoreState();
844*0a6a1f1dSLionel Sambuc       if (FrameReg == X86::RSP)
845*0a6a1f1dSLionel Sambuc         Out.EmitCFIAdjustCfaOffset(-8 /* byte size of the LocalFrameReg */);
846*0a6a1f1dSLionel Sambuc     }
847*0a6a1f1dSLionel Sambuc   }
848*0a6a1f1dSLionel Sambuc 
849*0a6a1f1dSLionel Sambuc   virtual void InstrumentMemOperandSmall(X86Operand &Op, unsigned AccessSize,
850*0a6a1f1dSLionel Sambuc                                          bool IsWrite,
851*0a6a1f1dSLionel Sambuc                                          const RegisterContext &RegCtx,
852*0a6a1f1dSLionel Sambuc                                          MCContext &Ctx,
853*0a6a1f1dSLionel Sambuc                                          MCStreamer &Out) override;
854*0a6a1f1dSLionel Sambuc   virtual void InstrumentMemOperandLarge(X86Operand &Op, unsigned AccessSize,
855*0a6a1f1dSLionel Sambuc                                          bool IsWrite,
856*0a6a1f1dSLionel Sambuc                                          const RegisterContext &RegCtx,
857*0a6a1f1dSLionel Sambuc                                          MCContext &Ctx,
858*0a6a1f1dSLionel Sambuc                                          MCStreamer &Out) override;
859*0a6a1f1dSLionel Sambuc   virtual void InstrumentMOVSImpl(unsigned AccessSize, MCContext &Ctx,
860*0a6a1f1dSLionel Sambuc                                   MCStreamer &Out) override;
861*0a6a1f1dSLionel Sambuc 
862*0a6a1f1dSLionel Sambuc private:
EmitAdjustRSP(MCContext & Ctx,MCStreamer & Out,long Offset)863*0a6a1f1dSLionel Sambuc   void EmitAdjustRSP(MCContext &Ctx, MCStreamer &Out, long Offset) {
864*0a6a1f1dSLionel Sambuc     const MCExpr *Disp = MCConstantExpr::Create(Offset, Ctx);
865*0a6a1f1dSLionel Sambuc     std::unique_ptr<X86Operand> Op(
866*0a6a1f1dSLionel Sambuc         X86Operand::CreateMem(getPointerWidth(), 0, Disp, X86::RSP, 0, 1,
867*0a6a1f1dSLionel Sambuc                               SMLoc(), SMLoc()));
868*0a6a1f1dSLionel Sambuc     EmitLEA(*Op, MVT::i64, X86::RSP, Out);
869*0a6a1f1dSLionel Sambuc     OrigSPOffset += Offset;
870*0a6a1f1dSLionel Sambuc   }
871*0a6a1f1dSLionel Sambuc 
EmitCallAsanReport(unsigned AccessSize,bool IsWrite,MCContext & Ctx,MCStreamer & Out,const RegisterContext & RegCtx)872*0a6a1f1dSLionel Sambuc   void EmitCallAsanReport(unsigned AccessSize, bool IsWrite, MCContext &Ctx,
873*0a6a1f1dSLionel Sambuc                           MCStreamer &Out, const RegisterContext &RegCtx) {
874*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::CLD));
875*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::MMX_EMMS));
876*0a6a1f1dSLionel Sambuc 
877*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::AND64ri8)
878*0a6a1f1dSLionel Sambuc                              .addReg(X86::RSP)
879*0a6a1f1dSLionel Sambuc                              .addReg(X86::RSP)
880*0a6a1f1dSLionel Sambuc                              .addImm(-16));
881*0a6a1f1dSLionel Sambuc 
882*0a6a1f1dSLionel Sambuc     if (RegCtx.AddressReg(MVT::i64) != X86::RDI) {
883*0a6a1f1dSLionel Sambuc       EmitInstruction(Out, MCInstBuilder(X86::MOV64rr).addReg(X86::RDI).addReg(
884*0a6a1f1dSLionel Sambuc                                RegCtx.AddressReg(MVT::i64)));
885*0a6a1f1dSLionel Sambuc     }
886*0a6a1f1dSLionel Sambuc     const std::string &Fn = FuncName(AccessSize, IsWrite);
887*0a6a1f1dSLionel Sambuc     MCSymbol *FnSym = Ctx.GetOrCreateSymbol(StringRef(Fn));
888*0a6a1f1dSLionel Sambuc     const MCSymbolRefExpr *FnExpr =
889*0a6a1f1dSLionel Sambuc         MCSymbolRefExpr::Create(FnSym, MCSymbolRefExpr::VK_PLT, Ctx);
890*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::CALL64pcrel32).addExpr(FnExpr));
891*0a6a1f1dSLionel Sambuc   }
892*0a6a1f1dSLionel Sambuc };
893*0a6a1f1dSLionel Sambuc 
InstrumentMemOperandSmall(X86Operand & Op,unsigned AccessSize,bool IsWrite,const RegisterContext & RegCtx,MCContext & Ctx,MCStreamer & Out)894*0a6a1f1dSLionel Sambuc void X86AddressSanitizer64::InstrumentMemOperandSmall(
895*0a6a1f1dSLionel Sambuc     X86Operand &Op, unsigned AccessSize, bool IsWrite,
896*0a6a1f1dSLionel Sambuc     const RegisterContext &RegCtx, MCContext &Ctx, MCStreamer &Out) {
897*0a6a1f1dSLionel Sambuc   unsigned AddressRegI64 = RegCtx.AddressReg(MVT::i64);
898*0a6a1f1dSLionel Sambuc   unsigned AddressRegI32 = RegCtx.AddressReg(MVT::i32);
899*0a6a1f1dSLionel Sambuc   unsigned ShadowRegI64 = RegCtx.ShadowReg(MVT::i64);
900*0a6a1f1dSLionel Sambuc   unsigned ShadowRegI32 = RegCtx.ShadowReg(MVT::i32);
901*0a6a1f1dSLionel Sambuc   unsigned ShadowRegI8 = RegCtx.ShadowReg(MVT::i8);
902*0a6a1f1dSLionel Sambuc 
903*0a6a1f1dSLionel Sambuc   assert(RegCtx.ScratchReg(MVT::i32) != X86::NoRegister);
904*0a6a1f1dSLionel Sambuc   unsigned ScratchRegI32 = RegCtx.ScratchReg(MVT::i32);
905*0a6a1f1dSLionel Sambuc 
906*0a6a1f1dSLionel Sambuc   ComputeMemOperandAddress(Op, MVT::i64, AddressRegI64, Ctx, Out);
907*0a6a1f1dSLionel Sambuc 
908*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::MOV64rr).addReg(ShadowRegI64).addReg(
909*0a6a1f1dSLionel Sambuc                            AddressRegI64));
910*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::SHR64ri)
911*0a6a1f1dSLionel Sambuc                            .addReg(ShadowRegI64)
912*0a6a1f1dSLionel Sambuc                            .addReg(ShadowRegI64)
913*0a6a1f1dSLionel Sambuc                            .addImm(3));
914*0a6a1f1dSLionel Sambuc   {
915*0a6a1f1dSLionel Sambuc     MCInst Inst;
916*0a6a1f1dSLionel Sambuc     Inst.setOpcode(X86::MOV8rm);
917*0a6a1f1dSLionel Sambuc     Inst.addOperand(MCOperand::CreateReg(ShadowRegI8));
918*0a6a1f1dSLionel Sambuc     const MCExpr *Disp = MCConstantExpr::Create(kShadowOffset, Ctx);
919*0a6a1f1dSLionel Sambuc     std::unique_ptr<X86Operand> Op(
920*0a6a1f1dSLionel Sambuc         X86Operand::CreateMem(getPointerWidth(), 0, Disp, ShadowRegI64, 0, 1,
921*0a6a1f1dSLionel Sambuc                               SMLoc(), SMLoc()));
922*0a6a1f1dSLionel Sambuc     Op->addMemOperands(Inst, 5);
923*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, Inst);
924*0a6a1f1dSLionel Sambuc   }
925*0a6a1f1dSLionel Sambuc 
926*0a6a1f1dSLionel Sambuc   EmitInstruction(
927*0a6a1f1dSLionel Sambuc       Out, MCInstBuilder(X86::TEST8rr).addReg(ShadowRegI8).addReg(ShadowRegI8));
928*0a6a1f1dSLionel Sambuc   MCSymbol *DoneSym = Ctx.CreateTempSymbol();
929*0a6a1f1dSLionel Sambuc   const MCExpr *DoneExpr = MCSymbolRefExpr::Create(DoneSym, Ctx);
930*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::JE_1).addExpr(DoneExpr));
931*0a6a1f1dSLionel Sambuc 
932*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::MOV32rr).addReg(ScratchRegI32).addReg(
933*0a6a1f1dSLionel Sambuc                            AddressRegI32));
934*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::AND32ri)
935*0a6a1f1dSLionel Sambuc                            .addReg(ScratchRegI32)
936*0a6a1f1dSLionel Sambuc                            .addReg(ScratchRegI32)
937*0a6a1f1dSLionel Sambuc                            .addImm(7));
938*0a6a1f1dSLionel Sambuc 
939*0a6a1f1dSLionel Sambuc   switch (AccessSize) {
940*0a6a1f1dSLionel Sambuc   default: llvm_unreachable("Incorrect access size");
941*0a6a1f1dSLionel Sambuc   case 1:
942*0a6a1f1dSLionel Sambuc     break;
943*0a6a1f1dSLionel Sambuc   case 2: {
944*0a6a1f1dSLionel Sambuc     const MCExpr *Disp = MCConstantExpr::Create(1, Ctx);
945*0a6a1f1dSLionel Sambuc     std::unique_ptr<X86Operand> Op(
946*0a6a1f1dSLionel Sambuc         X86Operand::CreateMem(getPointerWidth(), 0, Disp, ScratchRegI32, 0, 1,
947*0a6a1f1dSLionel Sambuc                               SMLoc(), SMLoc()));
948*0a6a1f1dSLionel Sambuc     EmitLEA(*Op, MVT::i32, ScratchRegI32, Out);
949*0a6a1f1dSLionel Sambuc     break;
950*0a6a1f1dSLionel Sambuc   }
951*0a6a1f1dSLionel Sambuc   case 4:
952*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, MCInstBuilder(X86::ADD32ri8)
953*0a6a1f1dSLionel Sambuc                              .addReg(ScratchRegI32)
954*0a6a1f1dSLionel Sambuc                              .addReg(ScratchRegI32)
955*0a6a1f1dSLionel Sambuc                              .addImm(3));
956*0a6a1f1dSLionel Sambuc     break;
957*0a6a1f1dSLionel Sambuc   }
958*0a6a1f1dSLionel Sambuc 
959*0a6a1f1dSLionel Sambuc   EmitInstruction(
960*0a6a1f1dSLionel Sambuc       Out,
961*0a6a1f1dSLionel Sambuc       MCInstBuilder(X86::MOVSX32rr8).addReg(ShadowRegI32).addReg(ShadowRegI8));
962*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::CMP32rr).addReg(ScratchRegI32).addReg(
963*0a6a1f1dSLionel Sambuc                            ShadowRegI32));
964*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::JL_1).addExpr(DoneExpr));
965*0a6a1f1dSLionel Sambuc 
966*0a6a1f1dSLionel Sambuc   EmitCallAsanReport(AccessSize, IsWrite, Ctx, Out, RegCtx);
967*0a6a1f1dSLionel Sambuc   EmitLabel(Out, DoneSym);
968*0a6a1f1dSLionel Sambuc }
969*0a6a1f1dSLionel Sambuc 
InstrumentMemOperandLarge(X86Operand & Op,unsigned AccessSize,bool IsWrite,const RegisterContext & RegCtx,MCContext & Ctx,MCStreamer & Out)970*0a6a1f1dSLionel Sambuc void X86AddressSanitizer64::InstrumentMemOperandLarge(
971*0a6a1f1dSLionel Sambuc     X86Operand &Op, unsigned AccessSize, bool IsWrite,
972*0a6a1f1dSLionel Sambuc     const RegisterContext &RegCtx, MCContext &Ctx, MCStreamer &Out) {
973*0a6a1f1dSLionel Sambuc   unsigned AddressRegI64 = RegCtx.AddressReg(MVT::i64);
974*0a6a1f1dSLionel Sambuc   unsigned ShadowRegI64 = RegCtx.ShadowReg(MVT::i64);
975*0a6a1f1dSLionel Sambuc 
976*0a6a1f1dSLionel Sambuc   ComputeMemOperandAddress(Op, MVT::i64, AddressRegI64, Ctx, Out);
977*0a6a1f1dSLionel Sambuc 
978*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::MOV64rr).addReg(ShadowRegI64).addReg(
979*0a6a1f1dSLionel Sambuc                            AddressRegI64));
980*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::SHR64ri)
981*0a6a1f1dSLionel Sambuc                            .addReg(ShadowRegI64)
982*0a6a1f1dSLionel Sambuc                            .addReg(ShadowRegI64)
983*0a6a1f1dSLionel Sambuc                            .addImm(3));
984*0a6a1f1dSLionel Sambuc   {
985*0a6a1f1dSLionel Sambuc     MCInst Inst;
986*0a6a1f1dSLionel Sambuc     switch (AccessSize) {
987*0a6a1f1dSLionel Sambuc     default: llvm_unreachable("Incorrect access size");
988*0a6a1f1dSLionel Sambuc     case 8:
989*0a6a1f1dSLionel Sambuc       Inst.setOpcode(X86::CMP8mi);
990*0a6a1f1dSLionel Sambuc       break;
991*0a6a1f1dSLionel Sambuc     case 16:
992*0a6a1f1dSLionel Sambuc       Inst.setOpcode(X86::CMP16mi);
993*0a6a1f1dSLionel Sambuc       break;
994*0a6a1f1dSLionel Sambuc     }
995*0a6a1f1dSLionel Sambuc     const MCExpr *Disp = MCConstantExpr::Create(kShadowOffset, Ctx);
996*0a6a1f1dSLionel Sambuc     std::unique_ptr<X86Operand> Op(
997*0a6a1f1dSLionel Sambuc         X86Operand::CreateMem(getPointerWidth(), 0, Disp, ShadowRegI64, 0, 1,
998*0a6a1f1dSLionel Sambuc                               SMLoc(), SMLoc()));
999*0a6a1f1dSLionel Sambuc     Op->addMemOperands(Inst, 5);
1000*0a6a1f1dSLionel Sambuc     Inst.addOperand(MCOperand::CreateImm(0));
1001*0a6a1f1dSLionel Sambuc     EmitInstruction(Out, Inst);
1002*0a6a1f1dSLionel Sambuc   }
1003*0a6a1f1dSLionel Sambuc 
1004*0a6a1f1dSLionel Sambuc   MCSymbol *DoneSym = Ctx.CreateTempSymbol();
1005*0a6a1f1dSLionel Sambuc   const MCExpr *DoneExpr = MCSymbolRefExpr::Create(DoneSym, Ctx);
1006*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::JE_1).addExpr(DoneExpr));
1007*0a6a1f1dSLionel Sambuc 
1008*0a6a1f1dSLionel Sambuc   EmitCallAsanReport(AccessSize, IsWrite, Ctx, Out, RegCtx);
1009*0a6a1f1dSLionel Sambuc   EmitLabel(Out, DoneSym);
1010*0a6a1f1dSLionel Sambuc }
1011*0a6a1f1dSLionel Sambuc 
InstrumentMOVSImpl(unsigned AccessSize,MCContext & Ctx,MCStreamer & Out)1012*0a6a1f1dSLionel Sambuc void X86AddressSanitizer64::InstrumentMOVSImpl(unsigned AccessSize,
1013*0a6a1f1dSLionel Sambuc                                                MCContext &Ctx,
1014*0a6a1f1dSLionel Sambuc                                                MCStreamer &Out) {
1015*0a6a1f1dSLionel Sambuc   StoreFlags(Out);
1016*0a6a1f1dSLionel Sambuc 
1017*0a6a1f1dSLionel Sambuc   // No need to test when RCX is equals to zero.
1018*0a6a1f1dSLionel Sambuc   MCSymbol *DoneSym = Ctx.CreateTempSymbol();
1019*0a6a1f1dSLionel Sambuc   const MCExpr *DoneExpr = MCSymbolRefExpr::Create(DoneSym, Ctx);
1020*0a6a1f1dSLionel Sambuc   EmitInstruction(
1021*0a6a1f1dSLionel Sambuc       Out, MCInstBuilder(X86::TEST64rr).addReg(X86::RCX).addReg(X86::RCX));
1022*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, MCInstBuilder(X86::JE_1).addExpr(DoneExpr));
1023*0a6a1f1dSLionel Sambuc 
1024*0a6a1f1dSLionel Sambuc   // Instrument first and last elements in src and dst range.
1025*0a6a1f1dSLionel Sambuc   InstrumentMOVSBase(X86::RDI /* DstReg */, X86::RSI /* SrcReg */,
1026*0a6a1f1dSLionel Sambuc                      X86::RCX /* CntReg */, AccessSize, Ctx, Out);
1027*0a6a1f1dSLionel Sambuc 
1028*0a6a1f1dSLionel Sambuc   EmitLabel(Out, DoneSym);
1029*0a6a1f1dSLionel Sambuc   RestoreFlags(Out);
1030*0a6a1f1dSLionel Sambuc }
1031*0a6a1f1dSLionel Sambuc 
1032*0a6a1f1dSLionel Sambuc } // End anonymous namespace
1033*0a6a1f1dSLionel Sambuc 
X86AsmInstrumentation(const MCSubtargetInfo & STI)1034*0a6a1f1dSLionel Sambuc X86AsmInstrumentation::X86AsmInstrumentation(const MCSubtargetInfo &STI)
1035*0a6a1f1dSLionel Sambuc     : STI(STI), InitialFrameReg(0) {}
1036*0a6a1f1dSLionel Sambuc 
~X86AsmInstrumentation()1037*0a6a1f1dSLionel Sambuc X86AsmInstrumentation::~X86AsmInstrumentation() {}
1038*0a6a1f1dSLionel Sambuc 
InstrumentAndEmitInstruction(const MCInst & Inst,OperandVector & Operands,MCContext & Ctx,const MCInstrInfo & MII,MCStreamer & Out)1039*0a6a1f1dSLionel Sambuc void X86AsmInstrumentation::InstrumentAndEmitInstruction(
1040*0a6a1f1dSLionel Sambuc     const MCInst &Inst, OperandVector &Operands, MCContext &Ctx,
1041*0a6a1f1dSLionel Sambuc     const MCInstrInfo &MII, MCStreamer &Out) {
1042*0a6a1f1dSLionel Sambuc   EmitInstruction(Out, Inst);
1043*0a6a1f1dSLionel Sambuc }
1044*0a6a1f1dSLionel Sambuc 
EmitInstruction(MCStreamer & Out,const MCInst & Inst)1045*0a6a1f1dSLionel Sambuc void X86AsmInstrumentation::EmitInstruction(MCStreamer &Out,
1046*0a6a1f1dSLionel Sambuc                                             const MCInst &Inst) {
1047*0a6a1f1dSLionel Sambuc   Out.EmitInstruction(Inst, STI);
1048*0a6a1f1dSLionel Sambuc }
1049*0a6a1f1dSLionel Sambuc 
GetFrameRegGeneric(const MCContext & Ctx,MCStreamer & Out)1050*0a6a1f1dSLionel Sambuc unsigned X86AsmInstrumentation::GetFrameRegGeneric(const MCContext &Ctx,
1051*0a6a1f1dSLionel Sambuc                                                    MCStreamer &Out) {
1052*0a6a1f1dSLionel Sambuc   if (!Out.getNumFrameInfos()) // No active dwarf frame
1053*0a6a1f1dSLionel Sambuc     return X86::NoRegister;
1054*0a6a1f1dSLionel Sambuc   const MCDwarfFrameInfo &Frame = Out.getDwarfFrameInfos().back();
1055*0a6a1f1dSLionel Sambuc   if (Frame.End) // Active dwarf frame is closed
1056*0a6a1f1dSLionel Sambuc     return X86::NoRegister;
1057*0a6a1f1dSLionel Sambuc   const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
1058*0a6a1f1dSLionel Sambuc   if (!MRI) // No register info
1059*0a6a1f1dSLionel Sambuc     return X86::NoRegister;
1060*0a6a1f1dSLionel Sambuc 
1061*0a6a1f1dSLionel Sambuc   if (InitialFrameReg) {
1062*0a6a1f1dSLionel Sambuc     // FrameReg is set explicitly, we're instrumenting a MachineFunction.
1063*0a6a1f1dSLionel Sambuc     return InitialFrameReg;
1064*0a6a1f1dSLionel Sambuc   }
1065*0a6a1f1dSLionel Sambuc 
1066*0a6a1f1dSLionel Sambuc   return MRI->getLLVMRegNum(Frame.CurrentCfaRegister, true /* IsEH */);
1067*0a6a1f1dSLionel Sambuc }
1068*0a6a1f1dSLionel Sambuc 
1069*0a6a1f1dSLionel Sambuc X86AsmInstrumentation *
CreateX86AsmInstrumentation(const MCTargetOptions & MCOptions,const MCContext & Ctx,const MCSubtargetInfo & STI)1070*0a6a1f1dSLionel Sambuc CreateX86AsmInstrumentation(const MCTargetOptions &MCOptions,
1071*0a6a1f1dSLionel Sambuc                             const MCContext &Ctx, const MCSubtargetInfo &STI) {
1072*0a6a1f1dSLionel Sambuc   Triple T(STI.getTargetTriple());
1073*0a6a1f1dSLionel Sambuc   const bool hasCompilerRTSupport = T.isOSLinux();
1074*0a6a1f1dSLionel Sambuc   if (ClAsanInstrumentAssembly && hasCompilerRTSupport &&
1075*0a6a1f1dSLionel Sambuc       MCOptions.SanitizeAddress) {
1076*0a6a1f1dSLionel Sambuc     if ((STI.getFeatureBits() & X86::Mode32Bit) != 0)
1077*0a6a1f1dSLionel Sambuc       return new X86AddressSanitizer32(STI);
1078*0a6a1f1dSLionel Sambuc     if ((STI.getFeatureBits() & X86::Mode64Bit) != 0)
1079*0a6a1f1dSLionel Sambuc       return new X86AddressSanitizer64(STI);
1080*0a6a1f1dSLionel Sambuc   }
1081*0a6a1f1dSLionel Sambuc   return new X86AsmInstrumentation(STI);
1082*0a6a1f1dSLionel Sambuc }
1083*0a6a1f1dSLionel Sambuc 
1084*0a6a1f1dSLionel Sambuc } // End llvm namespace
1085