Skip to content

Commit

Permalink
Index more class references (#531)
Browse files Browse the repository at this point in the history
* Index more class references

* fix checkstyle

* index checkcast as well
  • Loading branch information
YanisBft authored Sep 25, 2023
1 parent 5792e2f commit 936aafb
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.InvokeDynamicInsnNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.TypeInsnNode;
import org.objectweb.asm.tree.analysis.Analyzer;
import org.objectweb.asm.tree.analysis.AnalyzerException;
import org.objectweb.asm.tree.analysis.BasicValue;
Expand Down Expand Up @@ -65,7 +67,7 @@ public MethodVisitor visitMethod(int access, String name, String desc, String si

private static class MethodInterpreter extends InterpreterPair<BasicValue, SourceValue> {
private final MethodDefEntry callerEntry;
private JarIndexer indexer;
private final JarIndexer indexer;

MethodInterpreter(MethodDefEntry callerEntry, JarIndexer indexer, EntryIndex entryIndex, InheritanceIndex inheritanceIndex) {
super(new IndexSimpleVerifier(entryIndex, inheritanceIndex), new SourceInterpreter());
Expand All @@ -80,6 +82,15 @@ public PairValue<BasicValue, SourceValue> newOperation(AbstractInsnNode insn) th
indexer.indexFieldReference(callerEntry, FieldEntry.parse(field.owner, field.name, field.desc), ReferenceTargetType.none());
}

if (insn.getOpcode() == Opcodes.LDC) {
LdcInsnNode ldc = (LdcInsnNode) insn;

if (ldc.getType() == Type.ARRAY && ldc.cst instanceof Type type) {
String className = type.getClassName().replace(".", "/");
indexer.indexClassReference(callerEntry, ClassEntry.parse(className), ReferenceTargetType.none());
}
}

return super.newOperation(insn);
}

Expand All @@ -95,6 +106,17 @@ public PairValue<BasicValue, SourceValue> unaryOperation(AbstractInsnNode insn,
indexer.indexFieldReference(callerEntry, FieldEntry.parse(field.owner, field.name, field.desc), getReferenceTargetType(value, insn));
}

if (insn.getOpcode() == Opcodes.INSTANCEOF) {
TypeInsnNode type = (TypeInsnNode) insn;
// Note: type.desc is actually the name
indexer.indexClassReference(callerEntry, ClassEntry.parse(type.desc), ReferenceTargetType.none());
}

if (insn.getOpcode() == Opcodes.CHECKCAST) {
TypeInsnNode type = (TypeInsnNode) insn;
indexer.indexClassReference(callerEntry, ClassEntry.parse(type.desc), ReferenceTargetType.none());
}

return super.unaryOperation(insn, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ public void indexMethod(MethodDefEntry methodEntry) {
}
}

@Override
public void indexClassReference(MethodDefEntry callerEntry, ClassEntry referencedEntry, ReferenceTargetType targetType) {
if (callerEntry.getParent().isJre()) {
return;
}

indexers.forEach(indexer -> indexer.indexClassReference(callerEntry, referencedEntry, targetType));
}

@Override
public void indexMethodReference(MethodDefEntry callerEntry, MethodEntry referencedEntry, ReferenceTargetType targetType) {
if (callerEntry.getParent().isJre()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cuchaz.enigma.analysis.ReferenceTargetType;
import cuchaz.enigma.translation.representation.Lambda;
import cuchaz.enigma.translation.representation.entry.ClassDefEntry;
import cuchaz.enigma.translation.representation.entry.ClassEntry;
import cuchaz.enigma.translation.representation.entry.FieldDefEntry;
import cuchaz.enigma.translation.representation.entry.FieldEntry;
import cuchaz.enigma.translation.representation.entry.MethodDefEntry;
Expand All @@ -18,6 +19,9 @@ default void indexField(FieldDefEntry fieldEntry) {
default void indexMethod(MethodDefEntry methodEntry) {
}

default void indexClassReference(MethodDefEntry callerEntry, ClassEntry referencedEntry, ReferenceTargetType targetType) {
}

default void indexMethodReference(MethodDefEntry callerEntry, MethodEntry referencedEntry, ReferenceTargetType targetType) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ private void indexFieldTypeDescriptor(FieldDefEntry field, TypeDescriptor typeDe
}
}

@Override
public void indexClassReference(MethodDefEntry callerEntry, ClassEntry referencedEntry, ReferenceTargetType targetType) {
referencesToClasses.put(referencedEntry, new EntryReference<>(referencedEntry, referencedEntry.getName(), callerEntry, targetType));
}

@Override
public void indexMethodReference(MethodDefEntry callerEntry, MethodEntry referencedEntry, ReferenceTargetType targetType) {
referencesToMethods.put(referencedEntry, new EntryReference<>(referencedEntry, referencedEntry.getName(), callerEntry, targetType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public ClassEntry(@Nullable ClassEntry parent, String className, @Nullable Strin
}
}

public static ClassEntry parse(String name) {
return new ClassEntry(name);
}

@Override
public Class<ClassEntry> getParentType() {
return ClassEntry.class;
Expand Down

0 comments on commit 936aafb

Please sign in to comment.