-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathklp_ccp.cc
386 lines (333 loc) · 9.33 KB
/
klp_ccp.cc
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Copyright (C) 2019 SUSE Software Solutions Germany GmbH
*
* This file is part of klp-ccp.
*
* klp-ccp is free software: you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* klp-ccp is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with klp-ccp. If not, see <https://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <getopt.h>
#include <cstring>
#include <iostream>
#include <vector>
#include "target_x86_64_gcc.hh"
#include "cmdline_except.hh"
#include "header_resolver.hh"
#include "preprocessor.hh"
#include "gnuc_parser_driver.hh"
#include "pp_except.hh"
#include "parse_except.hh"
#include "semantic_except.hh"
#include "lp_except.hh"
#include "lp_creation_python_policy.hh"
#include "output_remarks.hh"
#include "create_lp.hh"
using namespace klp::ccp;
static const char prog_name[] = "klp-ccp";
static const char optstr[] = ":hc:o:i:P:";
static const option longopts[] {
{ "help", 0, nullptr, 'h' },
{ "compiler", 1, nullptr, 'c' },
{ "outfile", 1, nullptr, 'o' },
{ "patched-functions", 1, nullptr, 'i' },
{ "python-policy", 1, nullptr, 'P' },
{ nullptr, 0, nullptr, 0 }
};
static void show_usage(const char * const prog)
{
std::cerr << "See '" << prog << " --help' for usage." << std::endl;
}
static void show_help(const char * const prog)
{
std::cout
<< "Usage: " << prog
<< " OPTIONS"
<< " -- ORIGINAL COMPILER COMMAND LINE"
<< std::endl;
std::cout << "Options:" << std::endl;
std::cout
<< " -c, --compiler=ARCH-COMPILER-VERSION"
<< "\t\tTarget compiler specification."
<< std::endl;
std::cout
<< " -o, --outfile=FILE"
<< "\t\t\t\tOutput file."
<< std::endl;
std::cout
<< " -i, --patched-functions=LIST"
<< "\t\t\tComma separated list of patched" << std::endl
<< "\t\t\t\t\t\tfunctions."
<< std::endl;
std::cout
<< " -P, --python-policy=MOD"
<< "\tFully qualified python class name implementing" << std::endl
<< "\t\t\t\t\t\tlivepatch creation policies." << std::endl
<< std::endl;
std::cout
<< "ORIGINAL COMPILER COMMAND LINE"
<< "\tdenotes the compiler invocation from the live" << std::endl
<< "\t\t\t\tpatching target's original compilation, without" << std::endl
<< "\t\t\t\tthe program name."
<< std::endl << std::endl;
std::cout
<< "For further information, please see"
<< " <https://github.com/suse/klp-ccp>."
<< std::endl;
}
static void show_opt_duplicate(const char * const prog,
const char * const o)
{
std::cerr << "command line error: "
<< " option '" << o << "' specified more than once"
<< std::endl;
show_usage(prog);
}
static void show_opt_missing(const char * const prog,
const char * const o)
{
std::cerr << "command line error: "
<< " option '" << o << "' is required"
<< std::endl;
show_usage(prog);
}
static void split_patched_funs(const char *arg, std::vector<std::string> &funs)
{
while (true) {
const char *delim = std::strchr(arg, ',');
if (!delim) {
delim = arg + std::strlen(arg);
}
const auto& f = std::use_facet<std::ctype<char>>(std::locale());
while (arg != delim &&
f.is(std::ctype_base::blank, *arg)) {
++arg;
}
const char *cur_end = delim;
while (cur_end != arg &&
f.is(std::ctype_base::blank, *(cur_end - 1))) {
--cur_end;
}
if (arg != cur_end)
funs.emplace_back(arg, cur_end);
if (*delim == '\0')
break;
arg = delim + 1;
}
}
int main(int argc, char *argv[], char *envp[])
{
bool do_help = false;
char *o_compiler = nullptr;
const char *o_outfile = nullptr;
std::vector<std::string> patched_funs;
const char *o_python_policy_fq_cls = nullptr;
int o;
int longindex = -1;
while (optind < argc && std::strcmp(argv[optind], "--") &&
(o = getopt_long(argc, argv, optstr, longopts, &longindex)) != -1) {
switch (o) {
case 'h':
do_help = true;
break;
case 'c':
if (o_compiler) {
show_opt_duplicate(prog_name, "--compiler");
return 1;
}
o_compiler = optarg;
break;
case 'o':
if (o_outfile) {
show_opt_duplicate(prog_name, "--outfile");
return 1;
}
o_outfile = optarg;
break;
case 'i':
split_patched_funs(optarg, patched_funs);
break;
case 'P':
if (o_python_policy_fq_cls) {
show_opt_duplicate(prog_name, "--python-policy");
return 1;
}
o_python_policy_fq_cls = optarg;
break;
case '?':
std::cerr << "command line error: invalid option '";
if (optopt)
std::cerr << '-' << static_cast<char>(optopt);
else
std::cerr << argv[optind - 1];
std::cerr << '\'' << std::endl;
show_usage(prog_name);
return 1;
case ':':
std::cerr << "command line error: missing parameter for option '";
if (optopt)
std::cerr << '-' << static_cast<char>(optopt);
else
std::cerr << longopts[longindex].name;
std::cerr << '\'' << std::endl;
show_usage(prog_name);
return 1;
}
}
if (do_help) {
show_help(prog_name);
return 0;
}
if (optind != argc && !std::strcmp(argv[optind], "--"))
++optind;
if (optind == argc) {
std::cerr << "command line error: missing compiler invocation" << std::endl;
show_usage(prog_name);
return 1;
}
if (!o_compiler) {
show_opt_missing(prog_name, "--compiler");
return 1;
}
char *o_compiler_version = std::strrchr(o_compiler, '-');
if (!o_compiler_version || !std::strlen(o_compiler_version + 1)) {
std::cerr
<< "command line error: compiler version specification missing in '"
<< o_compiler << '\'' << std::endl;
show_usage(prog_name);
return 1;
}
*o_compiler_version = '\0';
++o_compiler_version;
if (!o_outfile) {
show_opt_missing(prog_name, "--outfile");
return 1;
}
if (!o_python_policy_fq_cls) {
show_opt_missing(prog_name, "--python-policy");
return 1;
}
const char * o_python_policy_cls =
std::strrchr(o_python_policy_fq_cls, '.');
if (!o_python_policy_cls) {
std::cerr << "command line error: "
<< "expected fully qualified \"MOD.CLS\" for "
<< "--python-policy"
<< std::endl;
return 1;
}
const std::string o_python_policy_mod =
std::string{o_python_policy_fq_cls, o_python_policy_cls};
++o_python_policy_cls;
std::unique_ptr<target> tgt;
if (!strcmp(o_compiler, "x86_64-gcc")) {
try {
tgt.reset(new target_x86_64_gcc{o_compiler_version});
} catch (const cmdline_except &e) {
std::cerr << "command line error: " << e.what() << std::endl;
show_usage(prog_name);
return 1;
}
} else {
std::cerr << "command line error: "
<< "unrecognized compiler specification '"
<< o_compiler << '\'' << std::endl;
show_usage(prog_name);
return 1;
}
header_resolver hr;
preprocessor pp(hr, *tgt);
try {
tgt->parse_command_line(argc - optind,
const_cast<const char **>(&argv[optind]),
hr, pp,
[&](const std::string &warning) {
std::cerr << "compiler command line warning: "
<< warning << std::endl;
});
} catch (const cmdline_except &e) {
std::cerr << "compiler command line error: "
<< e.what() << std::endl;
show_usage(prog_name);
return 1;
}
int r = 0;
yy::gnuc_parser_driver pd{std::move(pp), *tgt};
try {
pd.parse();
} catch (const pp_except &) {
r = 2;
} catch (const parse_except&) {
r = 2;
} catch (const semantic_except&) {
r = 2;
}
if (!pd.get_remarks().empty())
std::cerr << pd.get_remarks();
if (r)
return r;
ast::ast_translation_unit ast(pd.grab_result());
try {
ast.resolve(*tgt);
} catch (const semantic_except&) {
r = 2;
}
if (!ast.get_remarks().empty())
std::cerr << ast.get_remarks();
ast.get_remarks().clear();
if (r)
return r;
try {
ast.evaluate(*tgt);
} catch (const semantic_except&) {
r = 2;
}
if (!ast.get_remarks().empty())
std::cerr << ast.get_remarks();
ast.get_remarks().clear();
if (r)
return r;
code_remarks iremarks;
output_remarks oremarks;
try {
lp_creation_python_policy pol{
ast.get_pp_result(),
std::move(patched_funs),
o_python_policy_mod.c_str(),
o_python_policy_cls
};
create_lp(o_outfile, ast, pol, iremarks, oremarks);
} catch (const lp_except&) {
r = 3;
} catch (const std::system_error &e) {
std::cerr << "error: " << e.what() << std::endl;
r = 4;
} catch (const python_except &e) {
std::cerr << "error: python policy failure: " << e.what() << std::endl;
handle_python_except();
r = 5;
}
// Teardown python only now after exceptions had a chance to print
// their message. It's bad code architecture, but the API for
// embedding Python is what it is, i.e. AFAICT there's no way to
// obtain a formatted error message and print it ourselves.
lp_creation_python_policy::teardown_python();
if (!iremarks.empty())
std::cerr << iremarks;
iremarks.clear();
if (r)
return r;
if (!oremarks.empty())
std::cerr << oremarks;
oremarks.clear();
return r;
}