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
|
package cuchaz.enigma.analysis;
import cuchaz.enigma.analysis.index.EntryIndex;
import cuchaz.enigma.analysis.index.InheritanceIndex;
import cuchaz.enigma.translation.representation.AccessFlags;
import cuchaz.enigma.translation.representation.entry.ClassDefEntry;
import cuchaz.enigma.translation.representation.entry.ClassEntry;
import cuchaz.enigma.utils.Utils;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.analysis.BasicValue;
import org.objectweb.asm.tree.analysis.SimpleVerifier;
import java.util.Set;
public class IndexSimpleVerifier extends SimpleVerifier {
private static final Type OBJECT_TYPE = Type.getType("Ljava/lang/Object;");
private final EntryIndex entryIndex;
private final InheritanceIndex inheritanceIndex;
public IndexSimpleVerifier(EntryIndex entryIndex, InheritanceIndex inheritanceIndex) {
super(Utils.ASM_VERSION, null, null, null, false);
this.entryIndex = entryIndex;
this.inheritanceIndex = inheritanceIndex;
}
@Override
protected boolean isSubTypeOf(BasicValue value, BasicValue expected) {
Type expectedType = expected.getType();
Type type = value.getType();
switch (expectedType.getSort()) {
case Type.INT:
case Type.FLOAT:
case Type.LONG:
case Type.DOUBLE:
return type.equals(expectedType);
case Type.ARRAY:
case Type.OBJECT:
if (type.equals(NULL_TYPE)) {
return true;
} else if (type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY) {
if (isAssignableFrom(expectedType, type)) {
return true;
} else if (isInterface(expectedType)) {
return isAssignableFrom(OBJECT_TYPE, type);
} else {
return false;
}
} else {
return false;
}
default:
throw new AssertionError();
}
}
@Override
protected boolean isInterface(Type type) {
AccessFlags classAccess = entryIndex.getClassAccess(new ClassEntry(type.getInternalName()));
if (classAccess != null) {
return classAccess.isInterface();
}
Class<?> clazz = getClass(type);
if (clazz != null) {
return clazz.isInterface();
}
return false;
}
@Override
protected Type getSuperClass(Type type) {
ClassDefEntry definition = entryIndex.getDefinition(new ClassEntry(type.getInternalName()));
if (definition != null) {
return Type.getType('L' + definition.getSuperClass().getFullName() + ';');
}
Class<?> clazz = getClass(type);
if (clazz != null) {
return Type.getType(clazz.getSuperclass());
}
return OBJECT_TYPE;
}
@Override
protected boolean isAssignableFrom(Type type1, Type type2) {
if (type1.equals(type2)) {
return true;
}
if (type2.equals(NULL_TYPE)) {
return true;
}
if (type1.getSort() == Type.ARRAY) {
return type2.getSort() == Type.ARRAY && isAssignableFrom(Type.getType(type1.getDescriptor().substring(1)), Type.getType(type2.getDescriptor().substring(1)));
}
if (type2.getSort() == Type.ARRAY) {
return type1.equals(OBJECT_TYPE);
}
if (type1.getSort() == Type.OBJECT && type2.getSort() == Type.OBJECT) {
if (type1.equals(OBJECT_TYPE)) {
return true;
}
ClassEntry class1 = new ClassEntry(type1.getInternalName());
ClassEntry class2 = new ClassEntry(type2.getInternalName());
if (entryIndex.hasClass(class1) && entryIndex.hasClass(class2)) {
return inheritanceIndex.getAncestors(class2).contains(class1);
}
Class<?> class1Class = getClass(Type.getType('L' + class1.getFullName() + ';'));
Class<?> class2Class = getClass(Type.getType('L' + class2.getFullName() + ';'));
if (class1Class == null) {
return true; // missing classes to find out
}
if (class2Class != null) {
return class1Class.isAssignableFrom(class2Class);
}
if (entryIndex.hasClass(class2)) {
Set<ClassEntry> ancestors = inheritanceIndex.getAncestors(class2);
for (ClassEntry ancestorEntry : ancestors) {
Class<?> ancestor = getClass(Type.getType('L' + ancestorEntry.getFullName() + ';'));
if (ancestor == null || class1Class.isAssignableFrom(ancestor)) {
return true; // assignable, or missing classes to find out
}
}
return false;
}
return true; // missing classes to find out
}
return false;
}
@Override
protected final Class<?> getClass(Type type) {
try {
return Class.forName(type.getSort() == Type.ARRAY ? type.getDescriptor().replace('/', '.') : type.getClassName(), false, null);
} catch (ClassNotFoundException e) {
return null;
}
}
}
|