forked from Kishanjay/LacunaV2-evaluator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodomvc_lacuna2.js
288 lines (237 loc) · 9.58 KB
/
todomvc_lacuna2.js
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* @description
* Runs each analyzer of Lacuna only ONCE on each application.
* In contrast with the less efficient procedure where every combination of
* analyzers was run on Lacuna.
*
* The results will be merged in a different script
* This makes it also much easier to spot issues with certain analyzers on
* some frameworks.
*/
require("./prototype_extension");
const path = require("path");
const fs = require("fs-extra");
const lacuna = require("../LacunaV2/lacuna_runner");
const TODOMVC_DIR = "todomvc";
const EXAMPLES_DIR = "examples.normalized.nocl";
const EXAMPLES_OUTPUT_DIR = "examples.lacunized.3"; // different folder to keep things cleen
const ANALYZERS = ["static", "nativecalls", "dynamic", "closure_compiler", "wala", "npm_cg", "tajs", "acg"];
const startTime = Date.now();
mergeLacunaResults();
const endTime = Date.now();
const dateDiff = endTime - startTime;
console.log("Execution time: " + startTime + "-" + endTime + " = " + dateDiff);
// startLacuna().then((result) => {
// console.log("Finished Lacunization");
// // mergeLacunaResults();
// });
function mergeLacunaResults() {
let analyserCombinations = generateAnalyserCombinations(ANALYZERS);
console.log("Analyzer combinations to calculate: " + analyserCombinations.length);
let frameworks = getFrameworks();
frameworks.forEach(framework => {
if (framework.name == 'angular-dart') return;
if (framework.name == 'chaplin-brunch') return;
if (framework.name == 'duel') return;
if (framework.name == 'vanilladart') return;
/**
* Gather all data needed from a framework directory;
* thus creates also the object containg all analyzer data
*/
let directory = generateFrameworkDirectory(framework);
directory = frameworkDirectoryException(framework, { directory, entry: null }).directory;
const analyzerResult = {};
ANALYZERS.forEach(analyzer => {
let analyzerLogfile = generateLogfileName(analyzer);
analyzerResult[analyzer] = loadJSONFile(path.join(directory, analyzerLogfile));
});
const allFunctions = analyzerResult['static'].allFunctions; // shouldn't really matter
analyserCombinations.forEach(analyzerCombination => {
analyzerCombination = analyzerCombination.split(" ");
if (analyzerCombination.length == 1) { return; } // already have these
const aliveFunctions = [];
analyzerCombination.forEach(analyzer => {
analyzerResult[analyzer]['aliveFunctions'].forEach(aliveFunction => {
/* Prevent duplicates from entering the array */
const match = aliveFunctions.some((storedAliveFunction) => {
return aliveFunction.file == storedAliveFunction.file &&
aliveFunction.range[0] == storedAliveFunction.range[0] &&
aliveFunction.range[1] == storedAliveFunction.range[1];
});
if (match) return;
aliveFunctions.push(aliveFunction);
});
});
const deadFunctions = getDeadFunctions(allFunctions, aliveFunctions);
const result = {
deadFunctions: deadFunctions,
aliveFunctions: aliveFunctions,
allFunctions: allFunctions,
}
let resultLogfile = generateLogfileName(analyzerCombination);
writeJSONfile(path.join(directory, resultLogfile), result);
});
});
}
function getDeadFunctions(allFunctions, aliveFunctions) {
const deadFunctions = [];
allFunctions.forEach(func => {
const match = aliveFunctions.some((aliveFunction) => {
return aliveFunction.file == func.file &&
aliveFunction.range[0] == func.range[0] &&
aliveFunction.range[1] == func.range[1];
});
if (match) return;
deadFunctions.push(func);
});
return deadFunctions;
}
async function startLacuna() {
let frameworks = getFrameworks();
// createDestinationFolder(); // creates the destination folder
let lacunaRunOptions = generateLacunaRunOptions(frameworks, ANALYZERS);
console.log("Number of runOptions: " + lacunaRunOptions.length);
for (let i = 0; i < lacunaRunOptions.length; i++) {
let lacunaRunOption = lacunaRunOptions[i];
// skip certain analyzers for now.
if (lacunaRunOption.analyzer.includes("static")) { continue; }
if (lacunaRunOption.analyzer.includes("nativecalls")) { continue; }
if (lacunaRunOption.analyzer.includes("dynamic")) { continue; }
if (lacunaRunOption.analyzer.includes("wala")) { continue; }
if (lacunaRunOption.analyzer.includes("acg")) { continue; }
if (lacunaRunOption.analyzer.includes("tajs")) { continue; }
if (lacunaRunOption.analyzer.includes("closure_compiler")) { continue; }
if (lacunaRunOption.analyzer.includes("npm_cg")) { continue; }
try {
console.log("\n\nRunOption: " + i + "/" + lacunaRunOptions.length);
await runLacuna(lacunaRunOption); // remove await for async **shocker**
} catch (e) { console.log(e); }
// NOTE: should wait for the dynamic analyser to function properly..
}
return;
}
/**
* Fetches the todomvc- frameworks that will be used
*/
function getFrameworks() {
const tests_dir = path.join(TODOMVC_DIR, "tests")
const cwd = process.cwd(); // fixes relative path issue
process.chdir(tests_dir);
const frameworkPathLookup = require("./" + path.join(tests_dir, 'framework-path-lookup'));
const frameworks = frameworkPathLookup();
process.chdir(cwd);
return frameworks;
}
/**
* Will create all combinations of analysers as a space seperated string.
*
* @param {*} analysers array of analysers that will be considered for the
* combinations.
*/
function generateAnalyserCombinations(analysers) {
let result = [];
let f = function (prefix, items) {
for (let i = 0; i < items.length; i++) {
let analyserCombination = (prefix + " " + items[i]).trim();
result.push(analyserCombination);
f(analyserCombination, items.slice(i + 1));
}
}
f('', analysers);
return result;
}
/**
* Generates all runOptions of Lacuna.
* For now that means to gather runoptions for every analyzer
* for every framework
*/
function generateLacunaRunOptions(frameworks, analyzers) {
var lacunaRunOptions = [];
frameworks.forEach(framework => {
let directory = generateFrameworkDirectory(framework);
analyzers.forEach((analyzer) => {
let logfile = generateLogfileName(analyzer);
let lacunaRunOption = {
directory: directory,
analyzer: [analyzer],
logfile: logfile,
assumeNormalization: true, // should be normalized before starting
force: true,
};
frameworkDirectoryException(framework, lacunaRunOption);
lacunaRunOptions.push(lacunaRunOption);
});
});
return lacunaRunOptions;
}
/**
* Lacuna promise required for synchronous execution
*/
function runLacuna(runOption) {
return new Promise((resolve, reject) => {
try { lacuna.run(runOption, (log) => { resolve(log); }); }
catch (e) { console.log(e); reject(e); }
});
}
/**
* Fetches the directory for a framework
*/
function generateFrameworkDirectory({ path: frameworkPath }) {
frameworkPath = frameworkPath.splice(0, 8, EXAMPLES_OUTPUT_DIR); // replaces examples
let pwdFrameworkPath = path.join(TODOMVC_DIR, frameworkPath);
return pwdFrameworkPath;
}
function frameworkDirectoryException(framework, options) {
/* Handle exceptions on the folder structure */
if (framework.name == 'angular-dart') {
options.directory = options.directory.slice(0, -4);
options.entry = 'web/index.html';
}
if (framework.name == 'chaplin-brunch') {
options.directory = options.directory.slice(0, -7);
options.entry = 'public/index.html';
}
if (framework.name == 'duel') {
options.directory = options.directory.slice(0, -4);
options.entry = 'www/index.html';
}
if (framework.name == 'vanilladart') {
options.directory = options.directory.slice(0, -10);
options.entry = 'build/web/index.html';
}
return { directory: options.directory, entry: options.entry };
}
function assert(assertion, msg) {
if (!assertion) {
console.log(msg);
process.exit(1);
}
}
/**
* Generates the name of the log file for a single analyzer, or for
* any combination of analyzers.
*/
function generateLogfileName(analyzer) {
if (Array.isArray(analyzer)) {
return "lacuna_" + analyzer.join(" ").replace(/ /gi, "") + ".log";
}
return "lacuna_" + analyzer + ".log";
}
/**
* Creates and clears the new examples folder where the lacunized results
* will be stored
*/
function createDestinationFolder() {
examplesSource = path.join(TODOMVC_DIR, EXAMPLES_DIR);
examplesDestination = path.join(TODOMVC_DIR, EXAMPLES_OUTPUT_DIR);
if (fs.existsSync(examplesDestination) && fs.lstatSync(examplesDestination).isDirectory()) {
fs.removeSync(examplesDestination);
}
fs.copySync(examplesSource, examplesDestination);
}
function loadJSONFile(filepath) {
return JSON.parse(fs.readFileSync(path.join(__dirname, filepath), 'utf8'));
}
function writeJSONfile(filepath, obj) {
return fs.writeFileSync(path.join(__dirname, filepath), JSON.stringify(obj, null, 4), 'utf8');
}