-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLwjglMethodAudit.java
More file actions
269 lines (243 loc) · 9.35 KB
/
LwjglMethodAudit.java
File metadata and controls
269 lines (243 loc) · 9.35 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
import java.io.*;
import java.util.*;
import java.util.jar.*;
public class LwjglMethodAudit {
static class CpInfo {
int tag;
Object a;
Object b;
}
static class RefUse {
final String owner;
final String name;
final String desc;
final String fromClass;
RefUse(String owner, String name, String desc, String fromClass) {
this.owner = owner; this.name = name; this.desc = desc; this.fromClass = fromClass;
}
String sig() { return owner + "." + name + ":" + desc; }
}
static class ParsedClass {
String thisClass;
List<RefUse> refs = new ArrayList<RefUse>();
Set<String> methods = new HashSet<String>();
}
static int u1(DataInputStream in) throws IOException { return in.readUnsignedByte(); }
static int u2(DataInputStream in) throws IOException { return in.readUnsignedShort(); }
static long u4(DataInputStream in) throws IOException { return in.readInt() & 0xffffffffL; }
static void skipN(DataInputStream in, long n) throws IOException {
while (n > 0) {
long s = in.skip(n);
if (s <= 0) throw new EOFException("skip failed");
n -= s;
}
}
static String cpUtf8(CpInfo[] cp, int idx) {
if (idx <= 0 || idx >= cp.length) return null;
CpInfo c = cp[idx];
if (c == null || c.tag != 1) return null;
return (String) c.a;
}
static String cpClassName(CpInfo[] cp, int idx) {
if (idx <= 0 || idx >= cp.length) return null;
CpInfo c = cp[idx];
if (c == null || c.tag != 7) return null;
return cpUtf8(cp, (Integer) c.a);
}
static int[] cpNameAndType(CpInfo[] cp, int idx) {
if (idx <= 0 || idx >= cp.length) return null;
CpInfo c = cp[idx];
if (c == null || c.tag != 12) return null;
return new int[] { (Integer) c.a, (Integer) c.b };
}
static ParsedClass parseClass(byte[] bytes, Set<String> ownerFilter) throws IOException {
DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
int magic = in.readInt();
if (magic != 0xCAFEBABE) throw new IOException("bad class magic");
u2(in); // minor
u2(in); // major
int cpCount = u2(in);
CpInfo[] cp = new CpInfo[cpCount];
for (int i = 1; i < cpCount; i++) {
int tag = u1(in);
CpInfo c = new CpInfo();
c.tag = tag;
switch (tag) {
case 1:
c.a = in.readUTF();
break;
case 3:
case 4:
in.readInt();
break;
case 5:
case 6:
in.readLong();
cp[i] = c;
i++; // takes two entries
continue;
case 7:
case 8:
case 16:
case 19:
case 20:
c.a = u2(in);
break;
case 9:
case 10:
case 11:
case 12:
case 18:
c.a = u2(in);
c.b = u2(in);
break;
case 15:
c.a = u1(in);
c.b = u2(in);
break;
default:
throw new IOException("unknown cp tag " + tag);
}
cp[i] = c;
}
u2(in); // access
int thisClassIdx = u2(in);
u2(in); // super
String thisClass = cpClassName(cp, thisClassIdx);
int ifaceCount = u2(in);
for (int i = 0; i < ifaceCount; i++) u2(in);
int fields = u2(in);
for (int i = 0; i < fields; i++) {
u2(in); u2(in); u2(in);
int ac = u2(in);
for (int j = 0; j < ac; j++) {
u2(in);
long len = u4(in);
skipN(in, len);
}
}
ParsedClass out = new ParsedClass();
out.thisClass = thisClass;
int methods = u2(in);
for (int i = 0; i < methods; i++) {
u2(in); // access
int nameIdx = u2(in);
int descIdx = u2(in);
String n = cpUtf8(cp, nameIdx);
String d = cpUtf8(cp, descIdx);
if (n != null && d != null) out.methods.add(n + ":" + d);
int ac = u2(in);
for (int j = 0; j < ac; j++) {
u2(in);
long len = u4(in);
skipN(in, len);
}
}
int classAttrs = u2(in);
for (int i = 0; i < classAttrs; i++) {
u2(in);
long len = u4(in);
skipN(in, len);
}
for (int i = 1; i < cp.length; i++) {
CpInfo c = cp[i];
if (c == null) continue;
if (c.tag == 10 || c.tag == 11) {
int classIdx = (Integer) c.a;
int ntIdx = (Integer) c.b;
String owner = cpClassName(cp, classIdx);
int[] nt = cpNameAndType(cp, ntIdx);
if (owner == null || nt == null) continue;
if (!ownerFilter.contains(owner)) continue;
String n = cpUtf8(cp, nt[0]);
String d = cpUtf8(cp, nt[1]);
if (n == null || d == null) continue;
out.refs.add(new RefUse(owner, n, d, thisClass));
}
}
return out;
}
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("usage: java LwjglMethodAudit <bridgeJar> <jar1> [jar2...]");
return;
}
Set<String> owners = new LinkedHashSet<String>();
owners.add("org/lwjgl/opengl/Display");
owners.add("org/lwjgl/opengl/Displa2");
owners.add("org/lwjgl/opengl/GL11");
owners.add("org/lwjgl/opengl/GL1A");
owners.add("org/lwjgl/opengl/GL1D");
owners.add("org/lwjgl/opengl/LinuxContextImplementation");
owners.add("org/lwjgl/Sys");
owners.add("org/lwjgl/Zys");
owners.add("org/lwjgl/MemoryUtil");
Map<String, Set<String>> bridgeMethods = new HashMap<String, Set<String>>();
for (String o : owners) bridgeMethods.put(o, new HashSet<String>());
JarFile bridge = new JarFile(args[0]);
Enumeration<JarEntry> ben = bridge.entries();
while (ben.hasMoreElements()) {
JarEntry je = ben.nextElement();
if (je.isDirectory() || !je.getName().endsWith(".class")) continue;
InputStream is = bridge.getInputStream(je);
byte[] bytes = readAll(is);
ParsedClass pc = parseClass(bytes, owners);
if (pc.thisClass != null && bridgeMethods.containsKey(pc.thisClass)) {
bridgeMethods.get(pc.thisClass).addAll(pc.methods);
}
}
bridge.close();
Map<String, Integer> refCount = new HashMap<String, Integer>();
Map<String, String> sampleFrom = new HashMap<String, String>();
for (int ai = 1; ai < args.length; ai++) {
JarFile jf = new JarFile(args[ai]);
Enumeration<JarEntry> en = jf.entries();
while (en.hasMoreElements()) {
JarEntry je = en.nextElement();
if (je.isDirectory() || !je.getName().endsWith(".class")) continue;
InputStream is = jf.getInputStream(je);
byte[] bytes = readAll(is);
ParsedClass pc;
try {
pc = parseClass(bytes, owners);
} catch (Throwable t) {
continue;
}
for (RefUse r : pc.refs) {
String key = r.owner + "." + r.name + ":" + r.desc;
refCount.put(key, refCount.getOrDefault(key, 0) + 1);
if (!sampleFrom.containsKey(key)) sampleFrom.put(key, r.fromClass + " @ " + args[ai]);
}
}
jf.close();
}
List<String> missing = new ArrayList<String>();
for (String key : refCount.keySet()) {
int dot = key.lastIndexOf('.');
int colon = key.indexOf(':', dot + 1);
String owner = key.substring(0, dot).replace('.', '/');
String sig = key.substring(dot + 1, colon) + key.substring(colon);
Set<String> mset = bridgeMethods.get(owner);
boolean present = mset != null && mset.contains(sig);
if (!present) missing.add(key);
}
Collections.sort(missing, new Comparator<String>() {
public int compare(String a, String b) {
return Integer.compare(refCount.getOrDefault(b,0), refCount.getOrDefault(a,0));
}
});
System.out.println("=== Missing Method Refs In Bridge ===");
for (String m : missing) {
System.out.println(refCount.get(m) + " | " + m + " | sample=" + sampleFrom.get(m));
}
System.out.println("missing_count=" + missing.size());
}
static byte[] readAll(InputStream is) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[8192];
int r;
while ((r = is.read(buf)) != -1) bos.write(buf, 0, r);
is.close();
return bos.toByteArray();
}
}