forked from HexHive/FuzzGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagic.h
211 lines (172 loc) · 7.26 KB
/
magic.h
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
// ------------------------------------------------------------------------------------------------
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* ___ ___ ___ ___ ___ ___ ___
* /\__\ /\ \ /\__\ /\__\ /\__\ /\__\ /\ \
* /:/ _/_ \:\ \ /::| | /::| | /:/ _/_ /:/ _/_ \:\ \
* /:/ /\__\ \:\ \ /:/:| | /:/:| | /:/ /\ \ /:/ /\__\ \:\ \
* /:/ /:/ / ___ \:\ \ /:/|:| |__ /:/|:| |__ /:/ /::\ \ /:/ /:/ _/_ _____\:\ \
* /:/_/:/ / /\ \ \:\__\ /:/ |:| /\__\ /:/ |:| /\__\ /:/__\/\:\__\ /:/_/:/ /\__\ /::::::::\__\
* \:\/:/ / \:\ \ /:/ / \/__|:|/:/ / \/__|:|/:/ / \:\ \ /:/ / \:\/:/ /:/ / \:\~~\~~\/__/
* \::/__/ \:\ /:/ / |:/:/ / |:/:/ / \:\ /:/ / \::/_/:/ / \:\ \
* \:\ \ \:\/:/ / |::/ / |::/ / \:\/:/ / \:\/:/ / \:\ \
* \:\__\ \::/ / |:/ / |:/ / \::/ / \::/ / \:\__\
* \/__/ \/__/ |/__/ |/__/ \/__/ \/__/ \/__/
*
* FuzzGen - Automatic Fuzzer Generation
*
*
*
* magic.h
*
* Header file for magic.cpp.
*
*/
// ------------------------------------------------------------------------------------------------
#ifndef LIBRARY_MAGIC_H
#define LIBRARY_MAGIC_H
#include "common.h" // local includes
#include "interwork.h"
#include "llvm/Pass.h" // llvm includes
#include "llvm/IR/Module.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/CallSite.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Analysis/Passes.h"
#include <typeinfo> // c++ includes
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <list>
#include <map>
#include <stack>
#include <deque>
#define DISABLE_MAGIC false
#define ENABLE_MAGIC true
/* Set-attribute modes */
enum SetAttrMode {
SET_ATTR_MODE_DISABLED = 0, // never update attributes
SET_ATTR_MODE_OFF, // temporarily not update attributes
SET_ATTR_MODE_ON // update attributes on every instruction
};
// ------------------------------------------------------------------------------------------------
// * Stack Frame *
//
// Node object that is used for DFS
//
class StackFrame {
public:
const Value *inst; // current instruction
StackFrame *parent; // parent instruction
unsigned n, // current struct "depth"
depth; // current tree depth (DEBUG only)
bool read; // set when branch ends with a load after a GEP
int mode;
/* class constructors */
StackFrame(const Value *v) :
inst(v), parent(nullptr), n(0), depth(0), read(false), mode(SET_ATTR_MODE_DISABLED) { }
StackFrame(const Value *v, int m) :
StackFrame(v) { mode = m; }
StackFrame(const Value *v, StackFrame *p) :
inst(v), parent(p), n(p->n), depth(p->depth), read(p->read), mode(p->mode) { }
};
// ------------------------------------------------------------------------------------------------
// * Magic Data *
//
// A magic function, produces magic data :P
//
template<typename T>
class MagicData {
public:
int attr; // attributes
int mode;
list<T> predefined; // values of predefined set (optional)
/* class constructor */
MagicData() : attr(0), mode(SET_ATTR_MODE_ON) { }
/* add (logic OR) an attribute */
void addAttr(int a) {
if (mode == SET_ATTR_MODE_ON) {
attr |= a;
}
}
/* set an attribute (clear previous ones) */
void setAttr(int a) {
if (mode == SET_ATTR_MODE_ON) {
attr = a;
}
}
/* add a predefined value */
void addPredefined(T p) {
if (mode == SET_ATTR_MODE_ON) {
predefined.push_back(p);
}
}
/* check whether a predefined value is in the set */
bool inPredefined(T p) {
return find(predefined.begin(), predefined.end(), p) != predefined.end();
}
};
// ------------------------------------------------------------------------------------------------
// * Magic module *
//
// Perform the internal analysis for each function API (I kept the name "magic" for legacy
// reasons).
//
class Magic {
public:
/* Class constructor */
Magic(Context *, deque<unsigned> &);
/* do the magic (i.e., internal analysis) */
interwork::BaseAttr *do_magic(Argument &, Type *, string);
/* clear visited node (needed for recursion */
void clear();
private:
Context *ctx; // execution context
static const set<string> sizeNames; // common size names
deque<unsigned> &structOff; // element offsets within struct
// (>1 for nested structs)
int analysisTy; // magic mode (analysis type)
Type *origTy; // original argument type
int setAttrMode; // set-attribute mode
map<const Argument*, int> funcVisited; // visited functions (needed by DF Analysis)
map<const Value *, bool> visited; // visited instructions
map<const StoreInst *, bool> skippedStores; // store instructions that should be avoided
/* check whether an argument represents an array */
inline bool isArray(const Argument &);
/* get the preceding argument of a given argument */
inline const Argument &getPreceding(const Argument &) const;
/* coalesce two magic data objects into one */
template<typename T>
inline void coalesce(MagicData<T> *, MagicData<T> *);
/* cast magic data to interwork objects */
template <typename T>
interwork::BaseAttr *magicToInterwork(MagicData<T> *, string);
/* basic data flow analysis */
template<typename T>
MagicData<T> *dataflowAnalysis(const Argument &, const AllocaInst *, int);
/* the "magic" function */
template<typename T>
MagicData<T> *argSpaceInference(const Argument &, int=0);
};
// ------------------------------------------------------------------------------------------------
#endif