-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcounter_example.cpp
More file actions
184 lines (153 loc) · 5.74 KB
/
Copy pathcounter_example.cpp
File metadata and controls
184 lines (153 loc) · 5.74 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
/**
* Copyright (C) 2014 - 2019 Map2Check tool
* This file is part of the Map2Check tool, and is made available under
* the terms of the GNU General Public License version 2.
*
* SPDX-License-Identifier: (GPL-2.0)
*
**/
#include "counter_example.hpp"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <utility>
#include <vector>
namespace Tools = Map2Check;
// using namespace Map2Check;
using Map2Check::CounterExample;
using Map2Check::SourceCodeHelper;
using std::ios;
CounterExample::CounterExample(std::string path, bool no_source) {
this->noSource = no_source;
if (!no_source)
this->sourceCodeHelper = std::make_unique<Tools::SourceCodeHelper>(
Tools::SourceCodeHelper(path));
this->processKleeLog();
// Log::Debug("started reading list log");
this->processListLog();
this->processProperty();
}
void CounterExample::printCounterExample(bool printListLog) {
// std::sort(this->counterExampleRows.begin(),
// this->counterExampleRows.end());
Log::Info("Counter-example:\n");
int currentState = 0;
for (int i = 0; i < this->counterExampleRows.size(); i++) {
this->counterExampleRows[i]->setState(currentState);
currentState = this->counterExampleRows[i]->getState() + 1;
Log::Info(*this->counterExampleRows[i]);
}
}
void CounterExample::processProperty() {
Tools::CheckViolatedProperty violated;
int step = this->counterExampleRows.size();
int state = 0;
std::string path = violated.path_name;
std::unique_ptr<CounterExampleRow> row =
std::make_unique<CounterExampleProperty>(
step, state, path, 0, violated.propertyViolated, violated.line,
violated.function_name);
this->counterExampleRows.push_back(std::move(row));
this->property = violated.propertyViolated;
}
std::string CounterExample::getViolatedProperty() {
if (this->property == Tools::PropertyViolated::FALSE_FREE) {
return "FALSE-FREE";
}
if (this->property == Tools::PropertyViolated::TARGET_REACHED) {
return "TARGET-REACHED";
}
if (this->property == Tools::PropertyViolated::FALSE_DEREF) {
return "FALSE-DEREF";
}
if (this->property == Tools::PropertyViolated::FALSE_MEMTRACK) {
return "FALSE-MEMTRACK";
}
if (this->property == Tools::PropertyViolated::FALSE_MEMCLEANUP) {
return "FALSE-MEMCLEANUP";
}
if (this->property == Tools::PropertyViolated::NONE) {
return "TRUE";
}
if (this->property == Tools::PropertyViolated::UNKNOWN) {
return "UNKNOWN";
}
if (this->property == Tools::PropertyViolated::FALSE_OVERFLOW) {
return "OVERFLOW";
}
// FIXME: Throw exception
return "";
}
// FIXME: Add Support to other non det type
void CounterExample::processKleeLog() {
kleeLogRows = Tools::KleeLogHelper::getListLogFromCSV();
int ref = 0;
for (int i = 0; i < kleeLogRows.size(); i++) {
int step = std::stoi(kleeLogRows[i].step);
std::string path = noSource ? "" : this->sourceCodeHelper->getFilePath();
int state = 0;
int lineNumber = std::stoi(kleeLogRows[i].line);
std::string lineC = noSource ? ""
: this->sourceCodeHelper->substituteWithResult(
lineNumber, "__VERIFIER_nondet_int()",
kleeLogRows[i].value);
std::unique_ptr<CounterExampleRow> row =
std::make_unique<CounterExampleKleeRow>(kleeLogRows[i], step, state,
path, ref, lineC);
// Log::Info(*row);
this->counterExampleRows.push_back(std::move(row));
ref++;
}
}
void CounterExample::processListLog() {
std::vector<Tools::ListLogRow> listLogRows =
Tools::ListLogHelper::getListLogFromCSV();
// TODO(hbgit): Add flag to map2check to print listlog
int ref = 0;
for (int i = 0; i < listLogRows.size(); i++) {
int step = std::stoi(listLogRows[i].step);
std::string path = noSource ? "" : this->sourceCodeHelper->getFilePath();
int state = 0;
int lineNumber = std::stoi(listLogRows[i].lineNumber);
Log::Debug("Line: " + listLogRows[i].lineNumber);
std::string lineC =
noSource ? "" : this->sourceCodeHelper->getLine(lineNumber);
std::unique_ptr<CounterExampleRow> row =
std::make_unique<CounterExampleListLogRow>(listLogRows[i], step,
state, path, ref, lineC);
this->counterExampleRows.push_back(std::move(row));
ref++;
}
}
void CounterExample::generateTestCase() {
std::string file = this->sourceCodeHelper->getFilePath();
std::string testcase = file + ".testcase";
std::string command = "cp " + file + " " + testcase;
system(command.c_str());
std::ofstream sourceFile;
sourceFile.open(testcase, ios::out | ios::app);
sourceFile << "\n"
<< "unsigned map2check_counter = 0;";
sourceFile << "\n"
<< "long map2check_input_array[] = {";
for (int i = 0; i < (this->kleeLogRows.size()); i++) {
sourceFile << this->kleeLogRows[i].value;
if (i != (this->kleeLogRows.size() - 1)) {
sourceFile << ",";
}
}
sourceFile << "};\n";
sourceFile << "unsigned __VERIFIER_nondet_uint() { return "
"(unsigned)map2check_input_array[map2check_counter++]; }\n";
sourceFile << "int __VERIFIER_nondet_int() { return "
"(int)map2check_input_array[map2check_counter++]; }\n";
sourceFile << "char __VERIFIER_nondet_char() { return "
"(char)map2check_input_array[map2check_counter++]; }\n";
sourceFile << "void* __VERIFIER_nondet_void() { return "
"(void*)(long)map2check_input_array[map2check_counter++]; }\n";
sourceFile << "long __VERIFIER_nondet_long() { return "
"(long)map2check_input_array[map2check_counter++]; }\n";
sourceFile.close();
}