aboutsummaryrefslogtreecommitdiff
path: root/lib/Target/Mips/MipsNaClRewritePass.cpp
blob: f675e5663a9a85cb7bba68b1fd927b5407d6b62b (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//===-- MipsNaClRewritePass.cpp - Native Client Rewrite Pass  -----*- C++ -*-=//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Native Client Rewrite Pass
// This final pass inserts the sandboxing instructions needed to run inside
// the Native Client sandbox. Native Client requires certain software fault
// isolation (SFI) constructions to be put in place, to prevent escape from
// the sandbox. Native Client refuses to execute binaries without the correct
// SFI sequences.
//
// Potentially dangerous operations which are protected include:
// * Stores
// * Branches
// * Changes to SP
//
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "mips-sfi"
#include "Mips.h"
#include "MipsInstrInfo.h"
#include "MipsNaClRewritePass.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineJumpTableInfo.h"
#include "llvm/Function.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/CommandLine.h"

using namespace llvm;

unsigned Mips::IndirectBranchMaskReg = Mips::T6;
unsigned Mips::LoadStoreStackMaskReg = Mips::T7;

namespace {
  class MipsNaClRewritePass : public MachineFunctionPass {
  public:
    static char ID;
    MipsNaClRewritePass() : MachineFunctionPass(ID) {}

    const MipsInstrInfo *TII;
    const TargetRegisterInfo *TRI;
    virtual bool runOnMachineFunction(MachineFunction &Fn);

    virtual const char *getPassName() const {
      return "Mips Native Client Rewrite Pass";
    }

  private:

    bool SandboxLoadsInBlock(MachineBasicBlock &MBB);
    bool SandboxStoresInBlock(MachineBasicBlock &MBB);
    void SandboxLoadStore(MachineBasicBlock &MBB,
                      MachineBasicBlock::iterator MBBI,
                      MachineInstr &MI,
                      int AddrIdx);

    bool SandboxBranchesInBlock(MachineBasicBlock &MBB);
    bool SandboxStackChangesInBlock(MachineBasicBlock &MBB);

    void SandboxStackChange(MachineBasicBlock &MBB,
                              MachineBasicBlock::iterator MBBI);
    void AlignAllJumpTargets(MachineFunction &MF);
  };
  char MipsNaClRewritePass::ID = 0;
}

static bool IsReturn(const MachineInstr &MI) {
  return (MI.getOpcode() == Mips::RET);
}

static bool IsIndirectJump(const MachineInstr &MI) {
  return (MI.getOpcode() == Mips::JR);
}

static bool IsIndirectCall(const MachineInstr &MI) {
  return (MI.getOpcode() == Mips::JALR);
}

static bool IsDirectCall(const MachineInstr &MI) {
  return ((MI.getOpcode() == Mips::JAL) || (MI.getOpcode() == Mips::BGEZAL)
       || (MI.getOpcode() == Mips::BLTZAL));
;
}

static bool IsStackMask(const MachineInstr &MI) {
  return (MI.getOpcode() == Mips::SFI_DATA_MASK);
}

static bool NeedSandboxStackChange(const MachineInstr &MI,
                                   const TargetRegisterInfo *TRI) {
  if (IsDirectCall(MI) || IsIndirectCall(MI)) {
    // We check this first because method modifiesRegister
    // returns true for calls.
    return false;
  }
  return (MI.modifiesRegister(Mips::SP, TRI) && !IsStackMask(MI));
}

void MipsNaClRewritePass::SandboxStackChange(MachineBasicBlock &MBB,
                                       MachineBasicBlock::iterator MBBI) {
  MachineInstr &MI = *MBBI;

  BuildMI(MBB, MBBI, MI.getDebugLoc(),
          TII->get(Mips::SFI_NOP_IF_AT_BUNDLE_END));

  // Get to next instr (one + to get the original, and one more + to get past).
  MachineBasicBlock::iterator MBBINext = (MBBI++);
  MachineBasicBlock::iterator MBBINext2 = (MBBI++);

  BuildMI(MBB, MBBINext2, MI.getDebugLoc(),
          TII->get(Mips::SFI_DATA_MASK), Mips::SP)
          .addReg(Mips::SP)
          .addReg(Mips::LoadStoreStackMaskReg);
  return;
}

bool MipsNaClRewritePass::SandboxStackChangesInBlock(MachineBasicBlock &MBB) {
  bool Modified = false;
  for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
       MBBI != E; ++MBBI) {
    MachineInstr &MI = *MBBI;
    if (NeedSandboxStackChange(MI, TRI)) {
      SandboxStackChange(MBB, MBBI);
      Modified = true;
    }
  }
  return Modified;
}

bool MipsNaClRewritePass::SandboxBranchesInBlock(MachineBasicBlock &MBB) {
  bool Modified = false;

  for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
      MBBI != E; ++MBBI) {
    MachineInstr &MI = *MBBI;

    if (IsReturn(MI)) {
      unsigned AddrReg = MI.getOperand(0).getReg();
      BuildMI(MBB, MBBI, MI.getDebugLoc(),
              TII->get(Mips::SFI_GUARD_RETURN), AddrReg)
          .addReg(AddrReg)
          .addReg(Mips::IndirectBranchMaskReg);
      Modified = true;
    } else if (IsIndirectJump(MI)) {
      unsigned AddrReg = MI.getOperand(0).getReg();
      BuildMI(MBB, MBBI, MI.getDebugLoc(),
              TII->get(Mips::SFI_GUARD_INDIRECT_JMP), AddrReg)
          .addReg(AddrReg)
          .addReg(Mips::IndirectBranchMaskReg);
      Modified = true;
    } else if (IsDirectCall(MI)) {
      BuildMI(MBB, MBBI, MI.getDebugLoc(),
              TII->get(Mips::SFI_GUARD_CALL));
      Modified = true;
    } else if (IsIndirectCall(MI)) {
      unsigned AddrReg = MI.getOperand(0).getReg();
      BuildMI(MBB, MBBI, MI.getDebugLoc(),
              TII->get(Mips::SFI_GUARD_INDIRECT_CALL), AddrReg)
          .addReg(AddrReg)
          .addReg(Mips::IndirectBranchMaskReg);
      Modified = true;
    }
  }

  return Modified;
}

/*
 * Sandboxes a load or store instruction by inserting an appropriate mask
 * operation before it.
 */
void MipsNaClRewritePass::SandboxLoadStore(MachineBasicBlock &MBB,
                                      MachineBasicBlock::iterator MBBI,
                                      MachineInstr &MI,
                                      int AddrIdx) {
  unsigned BaseReg = MI.getOperand(AddrIdx).getReg();

  BuildMI(MBB, MBBI, MI.getDebugLoc(),
          TII->get(Mips::SFI_GUARD_LOADSTORE), BaseReg)
      .addReg(BaseReg)
      .addReg(Mips::LoadStoreStackMaskReg);
  return;
}

bool IsDangerousLoad(const MachineInstr &MI, int *AddrIdx) {
  unsigned Opcode = MI.getOpcode();
  switch (Opcode) {
  default: return false;

  // Instructions with base address register in position 1
  case Mips::LB:
  case Mips::LBu:
  case Mips::LH:
  case Mips::LHu:
  case Mips::LW:
  case Mips::LWC1:
  case Mips::LDC1:
  case Mips::LL:
  case Mips::LWL:
  case Mips::LWR:
    *AddrIdx = 1;
    break;
  }

  if (MI.getOperand(*AddrIdx).getReg() == Mips::SP) {
    // The contents of SP do not require masking.
    return false;
  }

  return true;
}

bool IsDangerousStore(const MachineInstr &MI, int *AddrIdx) {
  unsigned Opcode = MI.getOpcode();
  switch (Opcode) {
  default: return false;

  // Instructions with base address register in position 1
  case Mips::SB:
  case Mips::SH:
  case Mips::SW:
  case Mips::SWC1:
  case Mips::SDC1:
  case Mips::SWL:
  case Mips::SWR:
    *AddrIdx = 1;
    break;

  case Mips::SC:
    *AddrIdx = 2;
    break;
  }

  if (MI.getOperand(*AddrIdx).getReg() == Mips::SP) {
    // The contents of SP do not require masking.
    return false;
  }

  return true;
}

bool MipsNaClRewritePass::SandboxLoadsInBlock(MachineBasicBlock &MBB) {
  bool Modified = false;
  for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
       MBBI != E;
       ++MBBI) {
    MachineInstr &MI = *MBBI;
    int AddrIdx;

    if (IsDangerousLoad(MI, &