-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbmc-golf.cpp
More file actions
270 lines (228 loc) · 8.44 KB
/
Copy pathcbmc-golf.cpp
File metadata and controls
270 lines (228 loc) · 8.44 KB
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
/*
* cbmc-golf.cpp - a single-source file wrapper for golfing applications of CBMC
* Copyright (C) 2018 DevJPM / SEJPM (cryptopotato@posteo.de)
* Distributed under the MIT License.
* For further information, see the enclosed file LICENSE.txt or
* visit https://opensource.org/licenses/mit-license.html
*/
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <deque>
#include <sstream>
#include "ProgramOptions/ProgramOptions.hxx"
static inline bool stringContains(const std::string& container, const std::string& toBeContained);
static std::pair<unsigned int,std::vector<std::string>> parseOptions(int argc, char** argv);
static std::deque<std::string> parseInput();
static void preProcessFile(const std::string& filename, std::deque<std::string>& definitions);
static std::string invokeCBMC(const std::vector<std::string>& filenames, unsigned int unwindCount);
static std::string trimOutput(const std::string& verboseOutput);
static inline void ReplaceAll(std::string& str, const std::string& from, const std::string& to);
int main( int argc, char** argv )
{
auto filesAndUnwinds = parseOptions(argc,argv);
auto& files = filesAndUnwinds.second;
auto& unwinds = filesAndUnwinds.first;
if(files.empty())
return 0;
auto inputDefinitions = parseInput();
for(const auto& file : files)
preProcessFile(file,inputDefinitions);
if(!inputDefinitions.empty())
{
std::cerr<<"didn't bind all the input parameters, error!"<<std::endl;
return -1;
}
auto output = invokeCBMC(files,unwinds);
std::cout << trimOutput(output);
return 0;
}
static std::pair<unsigned int,std::vector<std::string>> parseOptions(int argc, char** argv)
{
po::parser options;
options["help"]
.abbreviation('?')
.description("prints this help screen")
.callback([&](){std::cout<<options<<std::endl;});
options["unwind"]
.abbreviation('u')
.type(po::u32)
.description("specifies the number of loop-unwinds cbmc should perform")
.fallback(100);
options[""]
.type(po::string)
.multi();
if(!options(argc,argv)) {
std::cerr<<"error parsing program options"<<std::endl;
return std::make_pair(0,std::vector<std::string>());
}
if(options["help"].size())
return std::make_pair(0,std::vector<std::string>());
std::vector<std::string> files;
for(auto&& file : options[""])
files.push_back(file.string);
if(files.empty())
std::cerr << "please specify at least one source file"<<std::endl;
return std::make_pair(options["unwind"].get().u32,std::move(files));
}
static std::deque<std::string> parseInput()
{
std::deque<std::string> toReturn;
while(!std::cin.eof())
{
std::string buf;
std::getline(std::cin,buf);
if(buf.size()>0 && buf[buf.size()-1]=='\r')
buf.erase(buf.size()-1);
toReturn.push_back(std::move(buf));
}
return std::move(toReturn);
}
static void preProcessFile(const std::string& filename, std::deque<std::string>& definitions)
{
std::ifstream inputStream(filename);
if(!inputStream)
{
std::cerr<<"failed to open "<<filename<<std::endl;
return;
}
std::ofstream outputStream(filename+".cbmc.c");
if(!outputStream)
{
std::cerr<<"failed to open "<<filename<<".cbmc.c for writing"<<std::endl;
return;
}
outputStream<<"#include <stdio.h>"<<std::endl;
outputStream<<"#include <assert.h>"<<std::endl;
std::string lineBuffer;
while(std::getline(inputStream,lineBuffer) &&
lineBuffer.size()>=1 &&
lineBuffer[0]!='#' &&
lineBuffer.find("f()")==std::string::npos)
{
outputStream<<"const "<<lineBuffer<<" = "<<definitions.front()<<";"<<std::endl;
if(stringContains(lineBuffer,"[]"))
{
auto rightBeforeFirstBracket = lineBuffer.find("[");
auto firstWhitespaceToLeftOfBracket = lineBuffer.find_last_of(" ",rightBeforeFirstBracket)+1;
auto varName = lineBuffer.substr(firstWhitespaceToLeftOfBracket,rightBeforeFirstBracket-firstWhitespaceToLeftOfBracket);
outputStream<<"const int "<< varName << "l = sizeof("<< varName << ")/sizeof("<<varName<<"[0]);"<<std::endl;
}
definitions.pop_front();
}
while(!inputStream.eof())
{
// replace p( with printf(
ReplaceAll(lineBuffer,"p(\"","printf(\"\\n");
// replace a( with assert(
ReplaceAll(lineBuffer,"a(","assert(");
if(lineBuffer=="f()" || lineBuffer=="f()\r")
outputStream<<"int main() {";
else
outputStream<<lineBuffer<<std::endl;
std::getline(inputStream,lineBuffer);
}
outputStream<<"}";
}
static inline std::string buildOutPutFileName(const std::vector<std::string>& filenames)
{
std::string outPutFile = "";
for(const auto& file : filenames)
outPutFile += file + "_";
outPutFile += "out";
return std::move(outPutFile);
}
static std::string invokeCBMC(const std::vector<std::string>& filenames, unsigned int unwindCount)
{
std::string command = "cbmc --beautify --trace --unwind ";
command += std::to_string(unwindCount);
for(const auto& file : filenames)
command += " " + file + ".cbmc.c";
auto outPutFile = buildOutPutFileName(filenames);
command += " > " + outPutFile;
command += " 2> /dev/null";
std::system(command.c_str());
std::ifstream cbmcOutput(outPutFile);
if(!cbmcOutput)
{
std::cerr<<"failed to read the CBMC output"<<std::endl;
return "";
}
std::stringstream sstr;
sstr << cbmcOutput.rdbuf();
return std::move(sstr.str());
}
static inline bool stringContains(const std::string& container, const std::string& toBeContained)
{
return container.find(toBeContained)!=std::string::npos;
}
const std::string removedLinesSubStrings[] = {
"CBMC version",
"Parsing ",
"Unwinding loop ",
"Not unwinding loop ",
"size of program expression: ",
"simple slicing removed ",
"Generated ",
"Passing problem to propositional reduction",
"converting SSA",
"Running propositional reduction",
"Post-processing",
"Solving with MiniSAT",
"variables",
"SAT checker: instance is ",
"Runtime decision procedure:",
" assertion ",
"** Results",
"Trace for ",
"**",
"VERIFICATION FAILED",
"VERIFICATION SUCCESSFUL",
"file <command-line> ",
"<built-in>:",
"Converting",
"Type-checking ",
"Generating GOTO Program",
"Adding CPROVER library",
"Removal of function pointers and virtual functions",
"Partial Inlining",
"Generic Property Instrumentation",
"Starting Bounded Model Checking"
};
static std::string trimOutput(const std::string& verboseOutput)
{
std::string outputBuffer;
std::string lineBuffer;
std::istringstream stream(verboseOutput);
while(std::getline(stream,lineBuffer))
{
if(std::any_of(std::begin(removedLinesSubStrings),
std::end(removedLinesSubStrings),
[&lineBuffer](const std::string& bad){return stringContains(lineBuffer,bad);}))
continue;
if(stringContains(lineBuffer,"State ") || stringContains(lineBuffer,"Violated property:"))
{
while(lineBuffer!="" && lineBuffer!="\r" && !stream.eof())
std::getline(stream,lineBuffer);
continue;
}
if(lineBuffer=="" || lineBuffer=="\r")
continue;
outputBuffer+=lineBuffer;
outputBuffer+="\n";
}
return std::move(outputBuffer);
}
// from stackoverflow.com
// question: https://stackoverflow.com/q/2896600
// asker: https://stackoverflow.com/users/194510/big-z
// answer: https://stackoverflow.com/a/24315631
// answerer: https://stackoverflow.com/users/1715716/gauthier-boaglio
static inline void ReplaceAll(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
}