forked from pascal-lab/Tai-e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptions.java
More file actions
602 lines (534 loc) · 21.4 KB
/
Options.java
File metadata and controls
602 lines (534 loc) · 21.4 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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
* Tai-e: A Static Analysis Framework for Java
*
* Copyright (C) 2022 Tian Tan <tiantan@nju.edu.cn>
* Copyright (C) 2022 Yue Li <yueli@nju.edu.cn>
*
* This file is part of Tai-e.
*
* Tai-e is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Tai-e 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 Lesser General
* Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Tai-e. If not, see <https://www.gnu.org/licenses/>.
*/
package pascal.taie.config;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import pascal.taie.WorldBuilder;
import pascal.taie.analysis.pta.PointerAnalysis;
import pascal.taie.analysis.pta.plugin.reflection.LogItem;
import pascal.taie.language.classes.StringReps;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Path;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;
import java.util.function.Predicate;
/**
* Option class for Tai-e.
* We name this class in the plural to avoid name collision with {@link Option}.
*/
@Command(name = "Options",
description = "Tai-e options",
usageHelpWidth = 120
)
public class Options implements Serializable {
private static final Logger logger = LogManager.getLogger(Options.class);
private static final String OPTIONS_FILE = "options.yml";
private static final String DEFAULT_OUTPUT_DIR = "output";
// ---------- file-based options ----------
@JsonProperty
@Option(names = "--options-file",
description = "The options file")
private File optionsFile;
// ---------- information options ----------
@JsonProperty
@Option(names = {"-h", "--help"},
description = "Display this help message",
defaultValue = "false",
usageHelp = true)
private boolean printHelp;
public boolean isPrintHelp() {
return printHelp;
}
public void printHelp() {
CommandLine cmd = new CommandLine(this);
cmd.setUsageHelpLongOptionsMaxWidth(30);
cmd.usage(System.out);
}
// ---------- program options ----------
@JsonProperty
@JsonSerialize(contentUsing = FilePathSerializer.class)
@Option(names = {"-cp", "--class-path"},
description = "Class path. This option can be repeated"
+ " multiple times to specify multiple paths.",
converter = ClassPathConverter.class)
private List<String> classPath = List.of();
public List<String> getClassPath() {
return classPath;
}
@JsonProperty
@JsonSerialize(contentUsing = FilePathSerializer.class)
@Option(names = {"-acp", "--app-class-path"},
description = "Application class path. This option can be repeated"
+ " multiple times to specify multiple paths.",
converter = ClassPathConverter.class)
private List<String> appClassPath = List.of();
public List<String> getAppClassPath() {
return appClassPath;
}
@JsonProperty
@Option(names = {"-m", "--main-class"}, description = "Main class")
private String mainClass;
public String getMainClass() {
return mainClass;
}
@JsonProperty
@Option(names = {"--input-classes"},
description = "The classes should be included in the World of analyzed program." +
" You can specify class names or paths to input files (.txt)." +
" Multiple entries are split by ','",
split = ",",
paramLabel = "<inputClass|inputFile>")
private List<String> inputClasses = List.of();
public List<String> getInputClasses() {
return inputClasses;
}
@JsonProperty
@Option(names = "-java",
description = "Java version used by the program being analyzed" +
" (default: ${DEFAULT-VALUE})",
defaultValue = "6")
private int javaVersion;
public int getJavaVersion() {
return javaVersion;
}
@JsonProperty
@Option(names = {"-pp", "--prepend-JVM"},
description = "Prepend class path of current JVM to Tai-e's class path" +
" (default: ${DEFAULT-VALUE})",
defaultValue = "false")
private boolean prependJVM;
public boolean isPrependJVM() {
return prependJVM;
}
@JsonProperty
@Option(names = {"-ap", "--allow-phantom"},
description = "Allow Tai-e to process phantom references, i.e.," +
" the referenced classes that are not found in the class paths" +
" (default: ${DEFAULT-VALUE})",
defaultValue = "false")
private boolean allowPhantom;
public boolean isAllowPhantom() {
return allowPhantom;
}
// ---------- general analysis options ----------
@JsonProperty
@Option(names = "--world-builder",
description = "Specify world builder class (default: ${DEFAULT-VALUE})",
defaultValue = "pascal.taie.frontend.soot.SootWorldBuilder")
private Class<? extends WorldBuilder> worldBuilderClass;
public Class<? extends WorldBuilder> getWorldBuilderClass() {
return worldBuilderClass;
}
@JsonProperty
@JsonSerialize(using = OutputDirSerializer.class)
@JsonDeserialize(using = OutputDirDeserializer.class)
@Option(names = "--output-dir",
description = "Specify output directory (default: ${DEFAULT-VALUE})"
+ ", '" + PlaceholderAwareFile.AUTO_GEN + "' can be used as a placeholder"
+ " for an automatically generated timestamp",
defaultValue = DEFAULT_OUTPUT_DIR,
converter = OutputDirConverter.class)
private File outputDir;
public File getOutputDir() {
return outputDir;
}
@JsonProperty
@Option(names = "--pre-build-ir",
description = "Build IR for all available methods before" +
" starting any analysis (default: ${DEFAULT-VALUE})",
defaultValue = "false")
private boolean preBuildIR;
public boolean isPreBuildIR() {
return preBuildIR;
}
@JsonProperty
@Option(names = {"-wc", "--world-cache-mode"},
description = "Enable world cache mode to save build time"
+ " by caching the completed built world to the disk."
+ " When enabled, the '--pre-build-ir' option will be"
+ " enabled automatically. (default: ${DEFAULT-VALUE})",
defaultValue = "false")
private boolean worldCacheMode;
public boolean isWorldCacheMode() {
return worldCacheMode;
}
@JsonProperty
@Option(names = "-scope",
description = "Scope for method/class analyses (default: ${DEFAULT-VALUE}," +
" valid values: ${COMPLETION-CANDIDATES})",
defaultValue = "APP")
private Scope scope;
public Scope getScope() {
return scope;
}
@JsonProperty
@Option(names = "--no-native-model",
description = "Enable native model (default: ${DEFAULT-VALUE})",
defaultValue = "true",
negatable = true)
private boolean nativeModel;
public boolean enableNativeModel() {
return nativeModel;
}
// ---------- specific analysis options ----------
@JsonProperty
@Option(names = {"-p", "--plan-file"},
description = "The analysis plan file")
private File planFile;
public File getPlanFile() {
return planFile;
}
@JsonProperty
@Option(names = {"-a", "--analysis"},
description = "Analyses to be executed",
paramLabel = "<analysisID[=<options>]>",
mapFallbackValue = "")
private Map<String, AnalysisOptions> analyses = Map.of();
public Map<String, AnalysisOptions> getAnalyses() {
return analyses;
}
@JsonProperty
@Option(names = {"-g", "--gen-plan-file"},
description = "Merely generate analysis plan",
defaultValue = "false")
private boolean onlyGenPlan;
public boolean isOnlyGenPlan() {
return onlyGenPlan;
}
@JsonProperty
@Option(names = {"-kr", "--keep-result"},
description = "The analyses whose results are kept" +
" (multiple analyses are split by ',', default: ${DEFAULT-VALUE})",
split = ",", paramLabel = "<analysisID>",
defaultValue = Plan.KEEP_ALL)
private Set<String> keepResult;
public Set<String> getKeepResult() {
return keepResult;
}
/**
* Parses arguments and return the parsed and post-processed Options.
*/
public static Options parse(String... args) {
CommandLine commandLine = new CommandLine(new Options())
.registerConverter(AnalysisOptions.class, new AnalysisOptionsConverter());
Options options = (Options) commandLine.parseArgs(args).commandSpec().userObject();
return postProcess(options);
}
/**
* Validates input options and do some post-process on it.
*
* @return the Options object after post-process.
*/
private static Options postProcess(Options options) {
if (options.optionsFile != null) {
// If options file is given, we ignore other options,
// and instead read options from the file.
options = readRawOptions(options.optionsFile);
}
if (options.prependJVM) {
options.javaVersion = getCurrentJavaVersion();
}
if (options.worldCacheMode) {
options.preBuildIR = true;
}
if (!options.analyses.isEmpty() && options.planFile != null) {
// The user should choose either options or plan file to
// specify analyses to be executed.
throw new ConfigException("Conflict options: " +
"--analysis and --plan-file should not be used simultaneously");
}
if (options.getClassPath() != null
&& options.mainClass == null
&& options.inputClasses.isEmpty()
&& options.getAppClassPath() == null) {
throw new ConfigException("Missing options: " +
"at least one of --main-class, --input-classes " +
"or --app-class-path should be specified");
}
options.addReflectionLogClasses();
// mkdir for output dir
if (!options.outputDir.exists()) {
options.outputDir.mkdirs();
}
logger.info("Output directory: {}",
options.outputDir.getAbsolutePath());
// write options to file for future reviewing and issue submitting
writeOptions(options, new File(options.outputDir, OPTIONS_FILE));
return options;
}
/**
* Reads options from file.
* Note: the returned options have not been post-processed.
*/
private static Options readRawOptions(File file) {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
try {
return mapper.readValue(file, Options.class);
} catch (IOException e) {
throw new ConfigException("Failed to read options from " + file, e);
}
}
static int getCurrentJavaVersion() {
String version = System.getProperty("java.version");
String[] splits = version.split("\\.");
int i0 = Integer.parseInt(splits[0]);
if (i0 == 1) { // format 1.x.y_z (for Java 1-8)
return Integer.parseInt(splits[1]);
} else { // format x.y.z (for Java 9+)
return i0;
}
}
/**
* Writes options to given file.
*/
private static void writeOptions(Options options, File output) {
ObjectMapper mapper = new ObjectMapper(
new YAMLFactory()
.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
.disable(YAMLGenerator.Feature.SPLIT_LINES)
.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES));
try {
logger.info("Writing options to {}", output.getAbsolutePath());
mapper.writeValue(output, options);
} catch (IOException e) {
throw new ConfigException("Failed to write options to "
+ output.getAbsolutePath(), e);
}
}
/**
* Represents a file that supports placeholder and automatically replaces it
* with current timestamp values. This class extends the standard File class.
*/
private static class PlaceholderAwareFile extends File {
/**
* The placeholder for an automatically generated timestamp.
*/
private static final String AUTO_GEN = "$AUTO-GEN";
private final String rawPathname;
public PlaceholderAwareFile(String pathname) {
super(resolvePathname(pathname));
this.rawPathname = pathname;
}
public String getRawPathname() {
return rawPathname;
}
private static String resolvePathname(String pathname) {
if (pathname.contains(AUTO_GEN)) {
String timestamp = DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
.withZone(ZoneId.systemDefault())
.format(Instant.now());
pathname = pathname.replace(AUTO_GEN, timestamp);
// check if the output dir already exists
File file = Path.of(pathname).toAbsolutePath().normalize().toFile();
if (file.exists()) {
throw new RuntimeException("The generated file already exists, "
+ "please wait for a second to start again: " + pathname);
}
}
return Path.of(pathname).toAbsolutePath().normalize().toString();
}
}
/**
* @see #outputDir
*/
private static class OutputDirConverter implements CommandLine.ITypeConverter<File> {
@Override
public File convert(String outputDir) {
return new PlaceholderAwareFile(outputDir);
}
}
/**
* Serializer for raw {@link #outputDir}.
*/
private static class OutputDirSerializer extends JsonSerializer<File> {
@Override
public void serialize(File value, JsonGenerator gen,
SerializerProvider serializers) throws IOException {
if (value instanceof PlaceholderAwareFile file) {
gen.writeString(toSerializedFilePath(file.getRawPathname()));
} else {
throw new RuntimeException("Unexpected type: " + value);
}
}
}
/**
* Deserializer for {@link #outputDir}.
*/
private static class OutputDirDeserializer extends JsonDeserializer<File> {
@Override
public File deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException {
return new PlaceholderAwareFile(p.getValueAsString());
}
}
/**
* Converter for classpath with system path separator.
*/
private static class ClassPathConverter implements CommandLine.ITypeConverter<List<String>> {
@Override
public List<String> convert(String value) {
return Arrays.stream(value.split(File.pathSeparator))
.map(String::trim)
.filter(Predicate.not(String::isEmpty))
.toList();
}
}
/**
* Serializer for file path. Ensures a path is serialized as a relative path
* from the working directory rather than an absolute path, thus
* preserving the portability of the dumped options file.
*/
private static class FilePathSerializer extends JsonSerializer<String> {
@Override
public void serialize(String value, JsonGenerator gen,
SerializerProvider serializers) throws IOException {
gen.writeString(toSerializedFilePath(value));
}
}
/**
* Convert a file to a relative path using the "/" (forward slash)
* from the working directory, thus preserving the portability of
* the dumped options file.
*
* @param file the file to be processed
* @return a relative path from the working directory
*/
private static String toSerializedFilePath(String file) {
Path workingDir = Path.of("").toAbsolutePath();
Path path = Path.of(file).toAbsolutePath().normalize();
return workingDir.relativize(path).toString()
.replace('\\', '/');
}
private static class AnalysisOptionsConverter implements CommandLine.ITypeConverter<AnalysisOptions> {
@Override
public AnalysisOptions convert(String value) {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
JavaType mapType = mapper.getTypeFactory()
.constructMapType(Map.class, String.class, Object.class);
String optStr = toYAMLString(value);
try {
Map<String, Object> optsMap = optStr.isBlank()
? Map.of()
// Leverage Jackson to parse YAML string to Map
: mapper.readValue(optStr, mapType);
return new AnalysisOptions(optsMap);
} catch (JsonProcessingException e) {
throw new ConfigException("Invalid analysis options: " + value, e);
}
}
/**
* Converts option string to a valid YAML string.
* The option string is of format "key1:value1;key2:value2;...".
*/
private static String toYAMLString(String optValue) {
StringJoiner joiner = new StringJoiner("\n");
for (String keyValue : optValue.split(";")) {
if (!keyValue.isBlank()) {
int i = keyValue.indexOf(':'); // split keyValue
joiner.add(keyValue.substring(0, i) + ": "
+ keyValue.substring(i + 1));
}
}
return joiner.toString();
}
}
/**
* Add classes in reflection log to the input classes.
* <p>
* TODO: this is still a tentative solution.
*/
private void addReflectionLogClasses() {
List<String> inputClasses = new ArrayList<>(this.inputClasses);
AnalysisOptions analysisOptions = analyses.get(PointerAnalysis.ID);
if (analysisOptions == null || !analysisOptions.has("reflection-log")) {
return;
}
String path = analysisOptions.getString("reflection-log");
if (path != null) {
LogItem.load(path).forEach(item -> {
// add target class
String target = item.target;
String targetClass;
if (target.startsWith("<")) {
targetClass = StringReps.getClassNameOf(target);
} else {
targetClass = target;
}
if (StringReps.isArrayType(targetClass)) {
targetClass = StringReps.getBaseTypeNameOf(target);
}
inputClasses.add(targetClass);
});
}
this.inputClasses = List.copyOf(inputClasses);
}
@Override
public String toString() {
return "Options{" +
"optionsFile=" + optionsFile +
", printHelp=" + printHelp +
", classPath='" + classPath + '\'' +
", appClassPath='" + appClassPath + '\'' +
", mainClass='" + mainClass + '\'' +
", inputClasses=" + inputClasses +
", javaVersion=" + javaVersion +
", prependJVM=" + prependJVM +
", allowPhantom=" + allowPhantom +
", worldBuilderClass=" + worldBuilderClass +
", outputDir='" + outputDir + '\'' +
", preBuildIR=" + preBuildIR +
", worldCacheMode=" + worldCacheMode +
", scope=" + scope +
", nativeModel=" + nativeModel +
", planFile=" + planFile +
", analyses=" + analyses +
", onlyGenPlan=" + onlyGenPlan +
", keepResult=" + keepResult +
'}';
}
}