/
PDFControlDependenceGraph.java
163 lines (144 loc) · 6.05 KB
/
PDFControlDependenceGraph.java
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
/*******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.wala.examples.drivers;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import com.ibm.wala.cfg.cdg.ControlDependenceGraph;
import com.ibm.wala.classLoader.IMethod;
import com.ibm.wala.core.tests.callGraph.CallGraphTestUtil;
import com.ibm.wala.examples.properties.WalaExamplesProperties;
import com.ibm.wala.ipa.callgraph.AnalysisCache;
import com.ibm.wala.ipa.callgraph.AnalysisOptions;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
import com.ibm.wala.ipa.callgraph.impl.Everywhere;
import com.ibm.wala.ipa.cha.ClassHierarchy;
import com.ibm.wala.properties.WalaProperties;
import com.ibm.wala.ssa.IR;
import com.ibm.wala.ssa.ISSABasicBlock;
import com.ibm.wala.ssa.SSAInstruction;
import com.ibm.wala.ssa.SSAOptions;
import com.ibm.wala.types.MethodReference;
import com.ibm.wala.util.WalaException;
import com.ibm.wala.util.config.AnalysisScopeReader;
import com.ibm.wala.util.debug.Assertions;
import com.ibm.wala.util.io.FileProvider;
import com.ibm.wala.util.strings.StringStuff;
import com.ibm.wala.viz.DotUtil;
import com.ibm.wala.viz.PDFViewUtil;
/**
*
* This simple example application builds a WALA CDG and fires off ghostview
* to viz a DOT representation.
*
* @author sfink
*/
public class PDFControlDependenceGraph {
final public static boolean SANITIZE_CFG = false;
final public static String PDF_FILE = "cdg.pdf";
/**
* Usage: GVControlDependenceGraph -appJar [jar file name] -sig [method signature] The "jar
* file name" should be something like "c:/temp/testdata/java_cup.jar" The
* signature should be something like "java_cup.lexer.advance()V"
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
run(args);
}
/**
* @param args
* -appJar [jar file name] -sig [method signature] The "jar file
* name" should be something like "c:/temp/testdata/java_cup.jar" The
* signature should be something like "java_cup.lexer.advance()V"
* @throws IOException
*/
public static Process run(String[] args) throws IOException {
validateCommandLine(args);
return run(args[1], args[3]);
}
/**
* @param appJar
* should be something like "c:/temp/testdata/java_cup.jar"
* @param methodSig
* should be something like "java_cup.lexer.advance()V"
* @throws IOException
*/
public static Process run(String appJar, String methodSig) throws IOException {
try {
if (PDFCallGraph.isDirectory(appJar)) {
appJar = PDFCallGraph.findJarFiles(new String[] { appJar });
}
AnalysisScope scope = AnalysisScopeReader.makeJavaBinaryAnalysisScope(appJar, (new FileProvider()).getFile(CallGraphTestUtil.REGRESSION_EXCLUSIONS));
ClassHierarchy cha = ClassHierarchy.make(scope);
MethodReference mr = StringStuff.makeMethodReference(methodSig);
IMethod m = cha.resolveMethod(mr);
if (m == null) {
System.err.println("could not resolve " + mr);
throw new RuntimeException();
}
AnalysisOptions options = new AnalysisOptions();
options.getSSAOptions().setPiNodePolicy(SSAOptions.getAllBuiltInPiNodes());
AnalysisCache cache = new AnalysisCache();
IR ir = cache.getSSACache().findOrCreateIR(m, Everywhere.EVERYWHERE, options.getSSAOptions() );
if (ir == null) {
Assertions.UNREACHABLE("Null IR for " + m);
}
System.err.println(ir.toString());
ControlDependenceGraph<SSAInstruction, ISSABasicBlock> cdg = new ControlDependenceGraph<SSAInstruction, ISSABasicBlock>(ir.getControlFlowGraph());
Properties wp = null;
try {
wp = WalaProperties.loadProperties();
wp.putAll(WalaExamplesProperties.loadProperties());
} catch (WalaException e) {
e.printStackTrace();
Assertions.UNREACHABLE();
}
String psFile = wp.getProperty(WalaProperties.OUTPUT_DIR) + File.separatorChar + PDFControlDependenceGraph.PDF_FILE;
String dotFile = wp.getProperty(WalaProperties.OUTPUT_DIR) + File.separatorChar
+ PDFTypeHierarchy.DOT_FILE;
String dotExe = wp.getProperty(WalaExamplesProperties.DOT_EXE);
String gvExe = wp.getProperty(WalaExamplesProperties.PDFVIEW_EXE);
DotUtil.dotify(cdg, PDFViewUtil.makeIRDecorator(ir), dotFile, psFile, dotExe);
return PDFViewUtil.launchPDFView(psFile, gvExe);
} catch (WalaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
* Validate that the command-line arguments obey the expected usage.
*
* Usage:
* <ul>
* <li> args[0] : "-appJar"
* <li> args[1] : something like "c:/temp/testdata/java_cup.jar"
* <li> args[2] : "-sig"
* <li> args[3] : a method signature like "java_cup.lexer.advance()V" </ul?
*
* @param args
* @throws UnsupportedOperationException
* if command-line is malformed.
*/
static void validateCommandLine(String[] args) {
if (args.length != 4) {
throw new UnsupportedOperationException("must have at exactly 4 command-line arguments");
}
if (!args[0].equals("-appJar")) {
throw new UnsupportedOperationException("invalid command-line, args[0] should be -appJar, but is " + args[0]);
}
if (!args[2].equals("-sig")) {
throw new UnsupportedOperationException("invalid command-line, args[2] should be -sig, but is " + args[0]);
}
}
}