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