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
|
//===-- ExecutorUtil.cpp --------------------------------------------------===//
//
// The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "Executor.h"
#include "klee/Expr.h"
#include "klee/Interpreter.h"
#include "klee/Machine.h"
#include "klee/Solver.h"
#include "klee/Internal/Module/KModule.h"
#include "llvm/Constants.h"
#include "llvm/Function.h"
#include "llvm/Instructions.h"
#include "llvm/Module.h"
#include "llvm/ModuleProvider.h"
#include "llvm/Support/CallSite.h"
#include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/Support/Streams.h"
#include "llvm/Target/TargetData.h"
#include <iostream>
#include <cassert>
using namespace klee;
using namespace llvm;
namespace klee {
ref<Expr>
Executor::evalConstantExpr(llvm::ConstantExpr *ce) {
const llvm::Type *type = ce->getType();
ref<Expr> op1(0,Expr::Bool), op2(0,Expr::Bool), op3(0,Expr::Bool);
int numOperands = ce->getNumOperands();
if (numOperands > 0) op1 = evalConstant(ce->getOperand(0));
if (numOperands > 1) op2 = evalConstant(ce->getOperand(1));
if (numOperands > 2) op3 = evalConstant(ce->getOperand(2));
switch (ce->getOpcode()) {
case Instruction::Trunc: return ExtractExpr::createByteOff(op1,
0,
Expr::getWidthForLLVMType(type));
case Instruction::ZExt: return ZExtExpr::create(op1,
Expr::getWidthForLLVMType(type));
case Instruction::SExt: return SExtExpr::create(op1,
Expr::getWidthForLLVMType(type));
case Instruction::Add: return AddExpr::create(op1, op2);
case Instruction::Sub: return SubExpr::create(op1, op2);
case Instruction::Mul: return MulExpr::create(op1, op2);
case Instruction::SDiv: return SDivExpr::create(op1, op2);
case Instruction::UDiv: return UDivExpr::create(op1, op2);
case Instruction::SRem: return SRemExpr::create(op1, op2);
case Instruction::URem: return URemExpr::create(op1, op2);
case Instruction::And: return AndExpr::create(op1, op2);
case Instruction::Or: return OrExpr::create(op1, op2);
case Instruction::Xor: return XorExpr::create(op1, op2);
case Instruction::Shl: return ShlExpr::create(op1, op2);
case Instruction::LShr: return LShrExpr::create(op1, op2);
case Instruction::AShr: return AShrExpr::create(op1, op2);
case Instruction::BitCast: return op1;
case Instruction::IntToPtr: {
return ZExtExpr::create(op1, Expr::getWidthForLLVMType(type));
}
case Instruction::PtrToInt: {
return ZExtExpr::create(op1, Expr::getWidthForLLVMType(type));
}
case Instruction::GetElementPtr: {
ref<Expr> base = op1;
for (gep_type_iterator ii = gep_type_begin(ce), ie = gep_type_end(ce);
ii != ie; ++ii) {
ref<Expr> addend(0, kMachinePointerType);
if (const StructType *st = dyn_cast<StructType>(*ii)) {
const StructLayout *sl = kmodule->targetData->getStructLayout(st);
const ConstantInt *ci = cast<ConstantInt>(ii.getOperand());
addend = Expr::createPointer(sl->getElementOffset((unsigned)
ci->getZExtValue()));
} else {
const SequentialType *st = cast<SequentialType>(*ii);
ref<Expr> index = evalConstant(cast<Constant>(ii.getOperand()));
unsigned elementSize = kmodule->targetData->getTypeStoreSize(st->getElementType());
index = Expr::createCoerceToPointerType(index);
addend = MulExpr::create(index,
Expr::createPointer(elementSize));
}
base = AddExpr::create(base, addend);
}
return base;
}
case Instruction::ICmp: {
switch(ce->getPredicate()) {
case ICmpInst::ICMP_EQ: return EqExpr::create(op1, op2);
case ICmpInst::ICMP_NE: return NeExpr::create(op1, op2);
case ICmpInst::ICMP_UGT: return UgtExpr::create(op1, op2);
case ICmpInst::ICMP_UGE: return UgeExpr::create(op1, op2);
case ICmpInst::ICMP_ULT: return UltExpr::create(op1, op2);
case ICmpInst::ICMP_ULE: return UleExpr::create(op1, op2);
case ICmpInst::ICMP_SGT: return SgtExpr::create(op1, op2);
case ICmpInst::ICMP_SGE: return SgeExpr::create(op1, op2);
case ICmpInst::ICMP_SLT: return SltExpr::create(op1, op2);
case ICmpInst::ICMP_SLE: return SleExpr::create(op1, op2);
default:
assert(0 && "unhandled ICmp predicate");
}
}
case Instruction::Select: {
return SelectExpr::create(op1, op2, op3);
}
case Instruction::FDiv:
case Instruction::FRem:
case Instruction::FPTrunc:
case Instruction::FPExt:
case Instruction::UIToFP:
case Instruction::SIToFP:
case Instruction::FPToUI:
case Instruction::FPToSI:
case Instruction::FCmp:
assert(0 && "floating point ConstantExprs unsupported");
default :
assert(0 && "unknown ConstantExpr type");
}
}
}
|