-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
310 lines (236 loc) · 9.12 KB
/
main.cpp
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <fstream>
// Include fstream before Rinternals.h, otherwise a macro redef error might happen
#include "Rinternals.h"
#include "Rembedded.h"
#include "dirent.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "utils/serializerData.h"
#include "utils/Debug.h"
#include "utils/SerializedDataProcessor.h"
#include "utils/RshBuiltinsMap.h"
#include "utils/UMap.h"
#include "utils/deserializerData.h"
#include <iostream>
#include <stdlib.h>
#include <functional>
#include <sstream>
#include "R/Protect.h"
std::string outputPath;
std::string inputPath;
static void saveDMetaAndCopyFiles(SEXP ddContainer, const std::string & metaFilename) {
std::stringstream outFilePath;
outFilePath << outputPath << "/" << metaFilename << "d";
FILE *fptr;
fptr = fopen(outFilePath.str().c_str(),"w");
if (!fptr) {
for (int i = 0; i < 10; i++) {
sleep(1);
std::cout << "[W]waiting to open: " << outFilePath.str() << std::endl;
fptr = fopen(outFilePath.str().c_str(),"w");
if (fptr) break;
}
if (!fptr) {
std::cout << "[W]unable to open " << outFilePath.str() << std::endl;
Rf_error("[W]unable to open file!");
}
}
R_SaveToFile(ddContainer, fptr, 0);
fclose(fptr);
// Copy all relavant binaries
rir::deserializerData::iterateOverUnits(ddContainer, [&](SEXP ddContainer, SEXP offsetUnitContainer, SEXP contextUnitContainer, SEXP binaryUnitContainer) {
std::stringstream binInPathPrefix, binOutPathPrefix;
binInPathPrefix << inputPath << "/" << CHAR(PRINTNAME(rir::deserializerData::getHast(ddContainer)))
<< "_" << rir::offsetUnit::getOffsetIdxAsInt(offsetUnitContainer)
<< "_" << CHAR(PRINTNAME(rir::binaryUnit::getEpoch(binaryUnitContainer)));
binOutPathPrefix << outputPath << "/" << CHAR(PRINTNAME(rir::deserializerData::getHast(ddContainer)))
<< "_" << rir::offsetUnit::getOffsetIdxAsInt(offsetUnitContainer)
<< "_" << CHAR(PRINTNAME(rir::binaryUnit::getEpoch(binaryUnitContainer)));
{
// Copy BC
std::ifstream src(binInPathPrefix.str() + ".bc", std::ios::binary);
std::ofstream dst(binOutPathPrefix.str() + ".bc", std::ios::binary);
dst << src.rdbuf();
}
{
// Copy BC
std::ifstream src(binInPathPrefix.str() + ".pool", std::ios::binary);
std::ofstream dst(binOutPathPrefix.str() + ".pool", std::ios::binary);
dst << src.rdbuf();
}
});
}
static void testSavedDMeta(const std::string & metaFilename) {
std::stringstream path;
path << outputPath << "/" << metaFilename << "d";
FILE *reader;
reader = fopen(path.str().c_str(),"r");
if (!reader) {
for (int i = 0; i < 10; i++) {
sleep(1);
reader = fopen(path.str().c_str(),"r");
if (reader) break;
}
if (!reader) {
std::cerr << "unable to open " << path.str() << std::endl;
Rf_error("unable to open file!");
}
}
// Initialize the deserializing stream
R_inpstream_st inputStream;
R_InitFileInPStream(&inputStream, reader, R_pstream_binary_format, NULL, R_NilValue);
rir::Protect protecc;
SEXP ddContainer;
protecc(ddContainer= R_LoadFromFile(reader, 0));
fclose(reader);
// rir::deserializerData::print(ddContainer, 2);
}
static void iterateOverMetadatasInDirectory() {
DIR *dir;
struct dirent *ent;
size_t functionsSeen = 0;
size_t functionsWithMoreThanOneContext = 0, functionsMasked = 0;
if ((dir = opendir(inputPath.c_str())) != NULL) {
while ((ent = readdir(dir)) != NULL) {
std::string fName = ent->d_name;
if (fName.find(".meta") != std::string::npos) {
std::stringstream metadataPath;
metadataPath << inputPath << "/" << fName;
FILE *reader;
reader = fopen(metadataPath.str().c_str(),"r");
if (!reader) {
for (int i = 0; i < 10; i++) {
sleep(1);
reader = fopen(metadataPath.str().c_str(),"r");
if (reader) break;
}
if (!reader) {
std::cerr << "unable to open " << metadataPath.str() << std::endl;
Rf_error("unable to open file!");
continue;
}
}
SEXP serDataContainer;
rir::Protect protecc;
protecc(serDataContainer= R_LoadFromFile(reader, 0));
fclose(reader);
// printSpace(0);
// std::cout << "Processing: " << fName << std::endl;
// printSpace(2);
// std::cout << "FunctionName: " << CHAR(PRINTNAME(rir::serializerData::getName(serDataContainer))) << std::endl;
// rir::serializerData::print(serDataContainer, 0);
// Get serialized metadata
REnvHandler offsetMapHandler(rir::serializerData::getBitcodeMap(serDataContainer));
int numOffsets = offsetMapHandler.size();
SEXP ddContainer;
protecc(ddContainer = Rf_allocVector(VECSXP, rir::deserializerData::getContainerSize(numOffsets)));
rir::deserializerData::addHast(ddContainer, rir::serializerData::getHast(serDataContainer));
int ddIdx = rir::deserializerData::offsetsStartingIndex();
offsetMapHandler.iterate([&] (SEXP offsetIndex, SEXP contextMap) {
functionsSeen++;
// printSpace(4);
// std::cout << "Offset: " << CHAR(PRINTNAME(offsetIndex)) << std::endl;
std::stringstream pathPrefix;
pathPrefix << inputPath << "/" << CHAR(PRINTNAME(rir::serializerData::getHast(serDataContainer))) << "_" << CHAR(PRINTNAME(offsetIndex)) << "_";
SerializedDataProcessor p(contextMap, pathPrefix.str());
p.init();
// p.print(6);
if (p.getOrigContextsCount() > 1) {
functionsWithMoreThanOneContext++;
}
//
// Each deserializer data i.e. the parent container vector, contains 'n' unique offsets.
// These offsets are themselves a vector called as offset unit
// Inside each offset unit there are context units
// Inside each context unit there are binary units
//
//
// Create a offset unit that contains the desired number of contexts
//
SEXP ouContainer;
rir::Protect protecc1;
protecc1(ouContainer = Rf_allocVector(VECSXP, rir::offsetUnit::getContainerSize(p.getNumContexts())));
//
// Add Offset idx
//
rir::offsetUnit::addOffsetIdx(ouContainer, std::stoi(CHAR(PRINTNAME(offsetIndex))));
//
// Add mask
//
rir::offsetUnit::addMask(ouContainer, p.getMask().toI());
if (p.getMask().toI() != 0) {
functionsMasked++;
}
//
// Populate the context units in the relavant offsets inside the offset unit
//
p.populateOffsetUnit(ouContainer);
//
// Add the offset unit to the deserializer data.
//
rir::generalUtil::addSEXP(ddContainer, ouContainer, ddIdx);
ddIdx++;
});
// rir::deserializerData::print(ddContainer, 2);
saveDMetaAndCopyFiles(ddContainer, fName);
// testSavedDMeta(fName);
}
}
SerializedDataProcessor::printStats(0);
printSpace(0);
std::cout << "Functions Unique : " << functionsSeen << std::endl;
printSpace(0);
std::cout << "Functions (>1) Contexts : " << functionsWithMoreThanOneContext << std::endl;
printSpace(0);
std::cout << "Functions Masked : " << functionsMasked << std::endl;
} else {
std::cerr << "\"" << inputPath << "\" has no metas, nothing to process" << std::endl;
exit(EXIT_FAILURE);
}
}
// https://stackoverflow.com/questions/46728680/how-to-temporarily-suppress-output-from-printf
int supress_stdout() {
fflush(stdout);
int ret = dup(1);
int nullfd = open("/dev/null", O_WRONLY);
// check nullfd for error omitted
dup2(nullfd, 1);
close(nullfd);
return ret;
}
void resume_stdout(int fd) {
fflush(stdout);
dup2(fd, 1);
close(fd);
}
int main(int argc, char** argv) {
bool rHomeSet = getenv("R_HOME") ? true : false;
if (!rHomeSet) {
std::cerr << "R_HOME not set, set this to the GNUR built using the ./configure --enable-R-shlib flag" << std::endl;
std::cerr << "Usage: ./run.sh [path_to_folder] [output_folder]" << std::endl;
exit(EXIT_FAILURE);
}
if (argc != 4) {
std::cerr << "Usage: ./run.sh [path_to_folder] [output_folder]" << std::endl;
exit(EXIT_FAILURE);
}
inputPath = argv[2];
outputPath = argv[3];
std::cout << "[OBAP Started]" << std::endl;
std::cout << "Raw Bitcodes Folder : " << inputPath << std::endl;
std::cout << "Processed Bitcodes Folder : " << outputPath << std::endl;
std::cout.setstate(std::ios_base::failbit);
int fd = supress_stdout();
int status = Rf_initEmbeddedR(argc, argv);
if (status < 0) {
std::cerr << "R initialization failed." << std::endl;
exit(EXIT_FAILURE);
}
resume_stdout(fd);
std::cout.clear();
RshBuiltinWeights::init();
iterateOverMetadatasInDirectory();
Rf_endEmbeddedR(0);
return 0;
}