Add field references, only for getstatic currently

This commit is contained in:
Adam
2014-12-01 17:00:55 -05:00
parent 228f650b6c
commit 4a24560be5
15 changed files with 162 additions and 12 deletions

View File

@@ -2,7 +2,7 @@ package info.sigterm.deob;
import info.sigterm.deob.attributes.Attributes; import info.sigterm.deob.attributes.Attributes;
import info.sigterm.deob.pool.Class; import info.sigterm.deob.pool.Class;
import info.sigterm.deob.pool.UTF8; import info.sigterm.deob.pool.NameAndType;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
@@ -55,6 +55,11 @@ public class ClassFile
attributes = new Attributes(this); attributes = new Attributes(this);
} }
public ClassGroup getGroup()
{
return group;
}
public DataInputStream getStream() public DataInputStream getStream()
{ {
return is; return is;
@@ -68,23 +73,43 @@ public class ClassFile
public String getName() public String getName()
{ {
Class entry = (Class) pool.getEntry(this_class); Class entry = (Class) pool.getEntry(this_class);
UTF8 className = (UTF8) pool.getEntry(entry.getIndex()); return entry.getName();
return className.getValue(); }
public ClassFile getParent()
{
Class entry = (Class) pool.getEntry(super_class);
String superName = entry.getName();
ClassFile other = group.findClass(superName);
assert other != this;
return other;
}
public Field findField(NameAndType nat)
{
Field f = fields.findField(nat);
if (f != null)
return f;
ClassFile parent = getParent();
if (parent != null)
return parent.findField(nat);
return null;
} }
public void buildClassGraph() public void buildClassGraph()
{ {
Class entry = (Class) pool.getEntry(super_class); ClassFile other = getParent();
UTF8 className = (UTF8) pool.getEntry(entry.getIndex());
String superName = className.getValue();
ClassFile other = group.findClass(superName);
if (other == null) if (other == null)
return; // inherits from a class not in my group return; // inherits from a class not in my group
assert other != this;
this.parent = other; this.parent = other;
parent.children.add(this); parent.children.add(this);
} }
public void buildInstructionGraph()
{
methods.buildInstructionGraph();
}
} }

View File

@@ -32,4 +32,10 @@ public class ClassGroup
for (ClassFile c : classes) for (ClassFile c : classes)
c.buildClassGraph(); c.buildClassGraph();
} }
public void buildInstructionGraph()
{
for (ClassFile c : classes)
c.buildInstructionGraph();
}
} }

View File

@@ -26,5 +26,6 @@ public class Deob
} }
group.buildClassGraph(); group.buildClassGraph();
group.buildInstructionGraph();
} }
} }

View File

@@ -1,9 +1,12 @@
package info.sigterm.deob; package info.sigterm.deob;
import info.sigterm.deob.attributes.Attributes; import info.sigterm.deob.attributes.Attributes;
import info.sigterm.deob.attributes.code.Instruction;
import info.sigterm.deob.pool.UTF8;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
public class Field public class Field
{ {
@@ -14,6 +17,8 @@ public class Field
private int descriptorIndex; private int descriptorIndex;
private Attributes attributes; private Attributes attributes;
private ArrayList<Instruction> instructions = new ArrayList<Instruction>(); // instructions which reference this field
Field(Fields fields) throws IOException Field(Fields fields) throws IOException
{ {
this.fields = fields; this.fields = fields;
@@ -30,4 +35,21 @@ public class Field
{ {
return fields; return fields;
} }
public String getName()
{
UTF8 u = (UTF8) fields.getClassFile().getPool().getEntry(nameIndex);
return u.getValue();
}
public String getDescriptor()
{
UTF8 u = (UTF8) fields.getClassFile().getPool().getEntry(descriptorIndex);
return u.getValue();
}
public void addReference(Instruction ins)
{
instructions.add(ins);
}
} }

View File

@@ -1,5 +1,7 @@
package info.sigterm.deob; package info.sigterm.deob;
import info.sigterm.deob.pool.NameAndType;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
@@ -27,4 +29,12 @@ public class Fields
{ {
return classFile; return classFile;
} }
public Field findField(NameAndType nat)
{
for (Field f : fields)
if (f.getName().equals(nat.getName()) && f.getDescriptor().equals(nat.getDescriptor()))
return f;
return null;
}
} }

View File

@@ -37,4 +37,12 @@ public class Method
{ {
return (Code) attributes.findType(AttributeType.CODE); return (Code) attributes.findType(AttributeType.CODE);
} }
public void buildInstructionGraph()
{
Code code = getCode();
if (code != null)
code.buildInstructionGraph();
}
} }

View File

@@ -27,4 +27,10 @@ public class Methods
{ {
return classFile; return classFile;
} }
public void buildInstructionGraph()
{
for (Method m : methods)
m.buildInstructionGraph();
}
} }

View File

@@ -38,4 +38,9 @@ public class Code extends Attribute
{ {
return maxLocals; return maxLocals;
} }
public void buildInstructionGraph()
{
instructions.buildInstructionGraph();
}
} }

View File

@@ -22,6 +22,11 @@ public class Instruction
this.pc = pc; this.pc = pc;
} }
public Instructions getInstructions()
{
return instructions;
}
public int getPc() public int getPc()
{ {
return pc; return pc;
@@ -46,6 +51,10 @@ public class Instruction
{ {
} }
public void buildInstructionGraph()
{
}
public void execute(Execution e) public void execute(Execution e)
{ {
} }

View File

@@ -53,6 +53,12 @@ public class Instructions
i.buildJumpGraph(); i.buildJumpGraph();
} }
public void buildInstructionGraph()
{
for (Instruction i : instructions)
i.buildInstructionGraph();
}
public Code getCode() public Code getCode()
{ {
return code; return code;

View File

@@ -1,8 +1,13 @@
package info.sigterm.deob.attributes.code.instructions; package info.sigterm.deob.attributes.code.instructions;
import info.sigterm.deob.ClassFile;
import info.sigterm.deob.ConstantPool;
import info.sigterm.deob.attributes.code.Instruction; import info.sigterm.deob.attributes.code.Instruction;
import info.sigterm.deob.attributes.code.InstructionType; import info.sigterm.deob.attributes.code.InstructionType;
import info.sigterm.deob.attributes.code.Instructions; import info.sigterm.deob.attributes.code.Instructions;
import info.sigterm.deob.pool.Class;
import info.sigterm.deob.pool.Field;
import info.sigterm.deob.pool.NameAndType;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
@@ -20,4 +25,23 @@ public class GetStatic extends Instruction
length += 2; length += 2;
} }
@Override
public void buildInstructionGraph()
{
ConstantPool pool = this.getInstructions().getCode().getAttributes().getClassFile().getPool();
Field entry = (Field) pool.getEntry(index);
Class clazz = entry.getClassEntry();
NameAndType nat = entry.getNameAndType();
ClassFile cf = this.getInstructions().getCode().getAttributes().getClassFile().getGroup().findClass(clazz.getName());
if (cf == null)
return;
info.sigterm.deob.Field f = cf.findField(nat);
assert f != null;
f.addReference(this);
}
} }

View File

@@ -17,8 +17,9 @@ public class Class extends PoolEntry
index = is.readUnsignedShort(); index = is.readUnsignedShort();
} }
public int getIndex() public java.lang.String getName()
{ {
return index; UTF8 u = (UTF8) this.getPool().getEntry(index);
return u.getValue();
} }
} }

View File

@@ -19,4 +19,14 @@ public class Field extends PoolEntry
classIndex = is.readUnsignedShort(); classIndex = is.readUnsignedShort();
nameAndTypeIndex = is.readUnsignedShort(); nameAndTypeIndex = is.readUnsignedShort();
} }
public Class getClassEntry()
{
return (Class) this.getPool().getEntry(classIndex);
}
public NameAndType getNameAndType()
{
return (NameAndType) this.getPool().getEntry(nameAndTypeIndex);
}
} }

View File

@@ -19,4 +19,16 @@ public class NameAndType extends PoolEntry
nameIndex = is.readUnsignedShort(); nameIndex = is.readUnsignedShort();
descriptorIndex = is.readUnsignedShort(); descriptorIndex = is.readUnsignedShort();
} }
public java.lang.String getName()
{
UTF8 u = (UTF8) this.getPool().getEntry(nameIndex);
return u.getValue();
}
public java.lang.String getDescriptor()
{
UTF8 u = (UTF8) this.getPool().getEntry(descriptorIndex);
return u.getValue();
}
} }

View File

@@ -13,6 +13,11 @@ public abstract class PoolEntry
this.type = type; this.type = type;
} }
public ConstantPool getPool()
{
return pool;
}
public int getSlots() public int getSlots()
{ {
return 1; return 1;