about summary refs log tree commit diff homepage
path: root/lib/Module/InstructionOperandTypeCheckPass.cpp
blob: 5f428471c1afbd56336685abce14543c74d99923 (plain) (blame)
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
//===-- InstructionOperandTypeCheckPass.cpp ---------------------*- C++ -*-===//
//
//                     The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "Passes.h"
#include "klee/Config/Version.h"
#include "klee/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;

namespace {

void printOperandWarning(const char *expected, const Instruction *i, Type *ty,
                         unsigned opNum) {
  std::string msg;
  llvm::raw_string_ostream ss(msg);
  ss << "Found unexpected type (" << *ty << ") at operand " << opNum
     << ". Expected " << expected << " in " << *i;
  i->print(ss);
  ss.flush();
  klee::klee_warning("%s", msg.c_str());
}

bool checkOperandTypeIsScalarInt(const Instruction *i, unsigned opNum) {
  assert(opNum < i->getNumOperands());
  llvm::Type *ty = i->getOperand(opNum)->getType();
  if (!(ty->isIntegerTy())) {
    printOperandWarning("scalar integer", i, ty, opNum);
    return false;
  }
  return true;
}

bool checkOperandTypeIsScalarIntOrPointer(const Instruction *i,
                                          unsigned opNum) {
  assert(opNum < i->getNumOperands());
  llvm::Type *ty = i->getOperand(opNum)->getType();
  if (!(ty->isIntegerTy() || ty->isPointerTy())) {
    printOperandWarning("scalar integer or pointer", i, ty, opNum);
    return false;
  }
  return true;
}

bool checkOperandTypeIsScalarPointer(const Instruction *i, unsigned opNum) {
  assert(opNum < i->getNumOperands());
  llvm::Type *ty = i->getOperand(opNum)->getType();
  if (!(ty->isPointerTy())) {
    printOperandWarning("scalar pointer", i, ty, opNum);
    return false;
  }
  return true;
}

bool checkOperandTypeIsScalarFloat(const Instruction *i, unsigned opNum) {
  assert(opNum < i->getNumOperands());
  llvm::Type *ty = i->getOperand(opNum)->getType();
  if (!(ty->isFloatingPointTy())) {
    printOperandWarning("scalar float", i, ty, opNum);
    return false;
  }
  return true;
}

bool checkOperandsHaveSameType(const Instruction *i, unsigned opNum0,
                               unsigned opNum1) {
  assert(opNum0 < i->getNumOperands());
  assert(opNum1 < i->getNumOperands());
  llvm::Type *ty0 = i->getOperand(opNum0)->getType();
  llvm::Type *ty1 = i->getOperand(opNum1)->getType();
  if (!(ty0 == ty1)) {
    std::string msg;
    llvm::raw_string_ostream ss(msg);
    ss << "Found mismatched type (" << *ty0 << " != " << *ty1
       << ") for operands" << opNum0 << " and " << opNum1
       << ". Expected operand types to match in " << *i;
    i->print(ss);
    ss.flush();
    klee::klee_warning("%s", msg.c_str());
    return false;
  }
  return true;
}

bool checkInstruction(const Instruction *i) {
  switch (i->getOpcode()) {
  case Instruction::Select: {
    // Note we do not enforce that operand 1 and 2 are scalar because the
    // scalarizer pass might not remove these. This could be selecting which
    // vector operand to feed to another instruction. The Executor can handle
    // this so case so this is not a problem
    return checkOperandTypeIsScalarInt(i, 0) &
           checkOperandsHaveSameType(i, 1, 2);
  }
  // Integer arithmetic, logical and shifting
  case Instruction::Add:
  case Instruction::Sub:
  case Instruction::Mul:
  case Instruction::UDiv:
  case Instruction::SDiv:
  case Instruction::URem:
  case Instruction::SRem:
  case Instruction::And:
  case Instruction::Or:
  case Instruction::Xor:
  case Instruction::Shl:
  case Instruction::LShr:
  case Instruction::AShr: {
    return checkOperandTypeIsScalarInt(i, 0) &
           checkOperandTypeIsScalarInt(i, 1);
  }
  // Integer comparison
  case Instruction::ICmp: {
    return checkOperandTypeIsScalarIntOrPointer(i, 0) &
           checkOperandTypeIsScalarIntOrPointer(i, 1);
  }
  // Integer Conversion
  case Instruction::Trunc:
  case Instruction::ZExt:
  case Instruction::SExt:
  case Instruction::IntToPtr: {
    return checkOperandTypeIsScalarInt(i, 0);
  }
  case Instruction::PtrToInt: {
    return checkOperandTypeIsScalarPointer(i, 0);
  }
  // TODO: Figure out if Instruction::BitCast needs checking
  // Floating point arithmetic
  case Instruction::FAdd:
  case Instruction::FSub:
  case Instruction::FMul:
  case Instruction::FDiv:
  case Instruction::FRem: {
    return checkOperandTypeIsScalarFloat(i, 0) &
           checkOperandTypeIsScalarFloat(i, 1);
  }
  // Floating point conversion
  case Instruction::FPTrunc:
  case Instruction::FPExt:
  case Instruction::FPToUI:
  case Instruction::FPToSI: {
    return checkOperandTypeIsScalarFloat(i, 0);
  }
  case Instruction::UIToFP:
  case Instruction::SIToFP: {
    return checkOperandTypeIsScalarInt(i, 0);
  }
  // Floating point comparison
  case Instruction::FCmp: {
    return checkOperandTypeIsScalarFloat(i, 0) &
           checkOperandTypeIsScalarFloat(i, 1);
  }
  default:
    // Treat all other instructions as conforming
    return true;
  }
}
}

namespace klee {

char InstructionOperandTypeCheckPass::ID = 0;

bool InstructionOperandTypeCheckPass::runOnModule(Module &M) {
  instructionOperandsConform = true;
  for (Module::iterator fi = M.begin(), fe = M.end(); fi != fe; ++fi) {
    for (Function::iterator bi = fi->begin(), be = fi->end(); bi != be; ++bi) {
      for (BasicBlock::iterator ii = bi->begin(), ie = bi->end(); ii != ie;
           ++ii) {
        Instruction *i = &*ii;
        instructionOperandsConform &= checkInstruction(i);
      }
    }
  }

  return false;
}
}