-
Notifications
You must be signed in to change notification settings - Fork 2
/
rewrite-lib.js
90 lines (78 loc) · 2.72 KB
/
rewrite-lib.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
const fs = require('fs');
const copydir = require('copy-dir');
// helpers
function read(filename) { return fs.readFileSync(filename).toString(); }
function readLines(filename) { return read(filename).split('\n'); }
function write(filename, data) { fs.writeFileSync(filename, data); }
function walk(dir, ext) {
var results = [];
var list = fs.readdirSync(dir);
list.forEach(function(file) {
file = dir + '/' + file;
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
results = results.concat(walk(file, ext));
} else if (file.endsWith(ext)) {
/* Is a file */
results.push(file);
}
});
return results;
}
function rewriteLib(dir, language) {
console.log(`[${language}]`);
// Extract relation names, using *.dbscheme.stats file
console.log("extracting relation names..");
let orig_stat = walk(`lib/${language}`, 'stats')[0];
let rels = readLines(orig_stat)
.filter(l=>l.includes("<name>") && !l.includes('sourceLocationPrefix'))
.map(l=>l.substring(6, l.length-7))
const ORIG_LIB_DIR = `lib/${language}/ql/lib`;
const MY_LIB_DIR = `lib-${dir}/${language}`;
const PREFIX = `${language}_`;
// copy library
console.log("copying library..");
fs.mkdirSync(MY_LIB_DIR, { recursive: true })
fs.rmSync(MY_LIB_DIR, { recursive: true, force: true });
copydir.sync(ORIG_LIB_DIR, MY_LIB_DIR);
// rewrite
console.log("rewritting..");
walk(MY_LIB_DIR, ".qll").forEach(file => {
var content = read(file);
var rewritten = rels
.reduce((cur_content, rel) => {
let pattern = RegExp(`(?<!predicate) ${rel}\\((?!\\))`, 'g');
return cur_content.replaceAll(pattern, ` ${PREFIX}${rel}(`);
}, content)
.replaceAll("@", "@"+PREFIX);
if (language == 'java') {
/*
if (file.endsWith('Modifier.qll')) {
rewritten = rewritten.replaceAll("java_hasModifier(\"","hasModifier(\"");
}
if (file.endsWith('DataFlowPrivate.qll')) {
rewritten = rewritten.replace("private class FieldContent ","class FieldContent ");
}
if (file.endsWith('DataFlowUtil.qll')) {
rewritten = rewritten.replace("private class NewExpr ","class NewExpr ");
}
*/
}
if (language == 'python') {
if (file.endsWith('Exprs.qll')) {
rewritten = rewritten.replaceAll("(py_cobjecttypes","(python_py_cobjecttypes");
}
}
write(file, rewritten);
})
console.log();
}
/* JNI */
console.log('==========jni============');
rewriteLib('jni', 'java');
rewriteLib('jni', 'cpp');
/* c-python */
console.log('==========cpython========');
rewriteLib('cpython', 'python');
rewriteLib('cpython', 'cpp');