Convert ConstSourceValue to a sealed class

master
Graham 5 years ago
parent 8aa3a79166
commit 84f18c4d10
  1. 20
      deob/src/main/java/dev/openrs2/deob/analysis/ConstSourceInterpreter.kt
  2. 78
      deob/src/main/java/dev/openrs2/deob/analysis/ConstSourceValue.java
  3. 16
      deob/src/main/java/dev/openrs2/deob/analysis/ConstSourceValue.kt
  4. 7
      deob/src/main/java/dev/openrs2/deob/transform/UnusedArgTransformer.kt

@ -13,29 +13,29 @@ class ConstSourceInterpreter : Interpreter<ConstSourceValue>(Opcodes.ASM7) {
override fun newValue(type: Type?): ConstSourceValue? { override fun newValue(type: Type?): ConstSourceValue? {
val basicValue = basicInterpreter.newValue(type) ?: return null val basicValue = basicInterpreter.newValue(type) ?: return null
return ConstSourceValue.createUnknown(basicValue) return ConstSourceValue.Unknown(basicValue)
} }
@Throws(AnalyzerException::class) @Throws(AnalyzerException::class)
override fun newOperation(insn: AbstractInsnNode): ConstSourceValue { override fun newOperation(insn: AbstractInsnNode): ConstSourceValue {
val basicValue = basicInterpreter.newOperation(insn) val basicValue = basicInterpreter.newOperation(insn)
return if (insn.intConstant != null) { return if (insn.intConstant != null) {
ConstSourceValue.createSingleSourceConstant(basicValue, insn) ConstSourceValue.Single(basicValue, insn)
} else { } else {
ConstSourceValue.createUnknown(basicValue) ConstSourceValue.Unknown(basicValue)
} }
} }
@Throws(AnalyzerException::class) @Throws(AnalyzerException::class)
override fun copyOperation(insn: AbstractInsnNode, value: ConstSourceValue): ConstSourceValue { override fun copyOperation(insn: AbstractInsnNode, value: ConstSourceValue): ConstSourceValue {
val basicValue = basicInterpreter.copyOperation(insn, value.basicValue) val basicValue = basicInterpreter.copyOperation(insn, value.basicValue)
return ConstSourceValue.createUnknown(basicValue) return ConstSourceValue.Unknown(basicValue)
} }
@Throws(AnalyzerException::class) @Throws(AnalyzerException::class)
override fun unaryOperation(insn: AbstractInsnNode, value: ConstSourceValue): ConstSourceValue? { override fun unaryOperation(insn: AbstractInsnNode, value: ConstSourceValue): ConstSourceValue? {
val basicValue = basicInterpreter.unaryOperation(insn, value.basicValue) ?: return null val basicValue = basicInterpreter.unaryOperation(insn, value.basicValue) ?: return null
return ConstSourceValue.createUnknown(basicValue) return ConstSourceValue.Unknown(basicValue)
} }
@Throws(AnalyzerException::class) @Throws(AnalyzerException::class)
@ -46,7 +46,7 @@ class ConstSourceInterpreter : Interpreter<ConstSourceValue>(Opcodes.ASM7) {
): ConstSourceValue? { ): ConstSourceValue? {
val basicValue = basicInterpreter.binaryOperation(insn, value1.basicValue, value2.basicValue) val basicValue = basicInterpreter.binaryOperation(insn, value1.basicValue, value2.basicValue)
?: return null ?: return null
return ConstSourceValue.createUnknown(basicValue) return ConstSourceValue.Unknown(basicValue)
} }
@Throws(AnalyzerException::class) @Throws(AnalyzerException::class)
@ -62,7 +62,7 @@ class ConstSourceInterpreter : Interpreter<ConstSourceValue>(Opcodes.ASM7) {
value2.basicValue, value2.basicValue,
value3.basicValue value3.basicValue
) ?: return null ) ?: return null
return ConstSourceValue.createUnknown(basicValue) return ConstSourceValue.Unknown(basicValue)
} }
@Throws(AnalyzerException::class) @Throws(AnalyzerException::class)
@ -72,7 +72,7 @@ class ConstSourceInterpreter : Interpreter<ConstSourceValue>(Opcodes.ASM7) {
): ConstSourceValue? { ): ConstSourceValue? {
val args = values.map { it.basicValue }.toList() val args = values.map { it.basicValue }.toList()
val basicValue = basicInterpreter.naryOperation(insn, args) ?: return null val basicValue = basicInterpreter.naryOperation(insn, args) ?: return null
return ConstSourceValue.createUnknown(basicValue) return ConstSourceValue.Unknown(basicValue)
} }
@Throws(AnalyzerException::class) @Throws(AnalyzerException::class)
@ -82,10 +82,10 @@ class ConstSourceInterpreter : Interpreter<ConstSourceValue>(Opcodes.ASM7) {
override fun merge(value1: ConstSourceValue, value2: ConstSourceValue): ConstSourceValue { override fun merge(value1: ConstSourceValue, value2: ConstSourceValue): ConstSourceValue {
val basicValue = basicInterpreter.merge(value1.basicValue, value2.basicValue) val basicValue = basicInterpreter.merge(value1.basicValue, value2.basicValue)
return if (value1.isSingleSourceConstant && value1 == value2) { return if (value1 is ConstSourceValue.Single && value1 == value2) {
value1 value1
} else { } else {
ConstSourceValue.createUnknown(basicValue) ConstSourceValue.Unknown(basicValue)
} }
} }
} }

@ -1,78 +0,0 @@
package dev.openrs2.deob.analysis;
import java.util.Objects;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.analysis.BasicValue;
import org.objectweb.asm.tree.analysis.Value;
public final class ConstSourceValue implements Value {
public static ConstSourceValue createUnknown(BasicValue basicValue) {
Preconditions.checkNotNull(basicValue);
return new ConstSourceValue(basicValue, null);
}
public static ConstSourceValue createSingleSourceConstant(BasicValue basicValue, AbstractInsnNode source) {
Preconditions.checkArgument(basicValue == BasicValue.INT_VALUE);
Preconditions.checkNotNull(source);
return new ConstSourceValue(basicValue, source);
}
private final BasicValue basicValue;
private final AbstractInsnNode source;
private ConstSourceValue(BasicValue basicValue, AbstractInsnNode source) {
this.basicValue = basicValue;
this.source = source;
}
public BasicValue getBasicValue() {
return basicValue;
}
public boolean isUnknown() {
return source == null;
}
public boolean isSingleSourceConstant() {
return source != null;
}
public AbstractInsnNode getSource() {
Preconditions.checkState(source != null);
return source;
}
@Override
public int getSize() {
return basicValue.getSize();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ConstSourceValue that = (ConstSourceValue) o;
return basicValue.equals(that.basicValue) &&
Objects.equals(source, that.source);
}
@Override
public int hashCode() {
return Objects.hash(basicValue, source);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("basicValue", basicValue)
.add("source", source)
.toString();
}
}

@ -0,0 +1,16 @@
package dev.openrs2.deob.analysis
import org.objectweb.asm.tree.AbstractInsnNode
import org.objectweb.asm.tree.analysis.BasicValue
import org.objectweb.asm.tree.analysis.Value
sealed class ConstSourceValue : Value {
data class Unknown(override val basicValue: BasicValue) : ConstSourceValue()
data class Single(override val basicValue: BasicValue, val source: AbstractInsnNode) : ConstSourceValue()
abstract val basicValue: BasicValue
override fun getSize(): Int {
return basicValue.size
}
}

@ -10,6 +10,7 @@ import dev.openrs2.asm.transform.Transformer
import dev.openrs2.common.collect.DisjointSet import dev.openrs2.common.collect.DisjointSet
import dev.openrs2.deob.ArgRef import dev.openrs2.deob.ArgRef
import dev.openrs2.deob.analysis.ConstSourceInterpreter import dev.openrs2.deob.analysis.ConstSourceInterpreter
import dev.openrs2.deob.analysis.ConstSourceValue
import dev.openrs2.deob.remap.TypedRemapper import dev.openrs2.deob.remap.TypedRemapper
import org.objectweb.asm.Opcodes import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type import org.objectweb.asm.Type
@ -75,7 +76,7 @@ class UnusedArgTransformer : Transformer() {
val args = Type.getArgumentTypes(insn.desc).size val args = Type.getArgumentTypes(insn.desc).size
for (j in 0 until args) { for (j in 0 until args) {
val source = frame.getStack(stackSize - args + j) val source = frame.getStack(stackSize - args + j)
if (!source.isSingleSourceConstant) { if (source !is ConstSourceValue.Single) {
retainedArgs.add(ArgRef(invokePartition, j)) retainedArgs.add(ArgRef(invokePartition, j))
} }
} }
@ -123,8 +124,8 @@ class UnusedArgTransformer : Transformer() {
for ((j, argType) in argTypes.withIndex()) { for ((j, argType) in argTypes.withIndex()) {
if (INT_SORTS.contains(argType.sort) && !retainedArgs.contains(ArgRef(partition, j))) { if (INT_SORTS.contains(argType.sort) && !retainedArgs.contains(ArgRef(partition, j))) {
val source = frame.getStack(stackSize - argTypes.size + j).source val value = frame.getStack(stackSize - argTypes.size + j) as ConstSourceValue.Single
deadInsns.add(source) deadInsns.add(value.source)
} else { } else {
newArgTypes.add(argType) newArgTypes.add(argType)
} }

Loading…
Cancel
Save