summaryrefslogtreecommitdiffstats
path: root/llvm/lib/CodeGen/NoopInsertion.cpp
blob: f2291b8e99cc80d0f46c2287052a483485cf2bf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//===- NoopInsertion.cpp - Noop Insertion ---------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This pass adds fine-grained diversity by displacing code using randomly
// placed (optionally target supplied) Noop instructions.
//
//===----------------------------------------------------------------------===//

#include "llvm/CodeGen/NoopInsertion.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/RandomNumberGenerator.h"
#include "llvm/Target/TargetInstrInfo.h"
using namespace llvm;

#define DEBUG_TYPE "noop-insertion"

static cl::opt<unsigned> NoopInsertionPercentage(
    "noop-insertion-percentage",
    cl::desc("Percentage of instructions that have Noops prepended"),
    cl::init(25)); // Default is a good balance between entropy and
                   // performance impact

static cl::opt<unsigned> MaxNoopsPerInstruction(
    "max-noops-per-instruction",
    llvm::cl::desc("Maximum number of Noops per instruction"),
    llvm::cl::init(1));

STATISTIC(InsertedNoops,
          "Total number of noop type instructions inserted for diversity");

char NoopInsertion::ID = 0;
char &llvm::NoopInsertionID = NoopInsertion::ID;
INITIALIZE_PASS(NoopInsertion, "noop-insertion",
                "Noop Insertion for fine-grained code randomization", false,
                false)

NoopInsertion::NoopInsertion() : MachineFunctionPass(ID) {
  initializeNoopInsertionPass(*PassRegistry::getPassRegistry());

  // clamp percentage to 100
  if (NoopInsertionPercentage > 100)
    NoopInsertionPercentage = 100;
}

void NoopInsertion::getAnalysisUsage(AnalysisUsage &AU) const {
  AU.setPreservesCFG();
  MachineFunctionPass::getAnalysisUsage(AU);
}

bool NoopInsertion::runOnMachineFunction(MachineFunction &Fn) {
  // The RNG must be initialized on first use so we have a Module to
  // construct it from
  if (!RNG)
    RNG.reset(Fn.getFunction()->getParent()->createRNG(this));

  const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo();

  unsigned FnInsertedNoopCount = 0;

  for (auto &BB : Fn) {
    MachineBasicBlock::iterator FirstTerm = BB.getFirstTerminator();

    for (MachineBasicBlock::iterator I = BB.begin(), E = BB.end(); I != E;
         ++I) {
      if (I->isPseudo())
        continue;

      // Insert random number of Noop-like instructions.
      for (unsigned i = 0; i < MaxNoopsPerInstruction; i++) {
        if (Distribution(*RNG) >= NoopInsertionPercentage)
          continue;

        TII->insertNoop(BB, I, *RNG);

        ++FnInsertedNoopCount;
      }

      if (I == FirstTerm)
        break;
    }
  }

  InsertedNoops += FnInsertedNoopCount;

  return FnInsertedNoopCount > 0;
}
OpenPOWER on IntegriCloud