Skip to content

Commit

Permalink
fix: allow override type with wider one only from debug info (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Dec 6, 2018
1 parent 37071db commit b9fffa1
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,11 @@ public boolean canBeArray() {
return isArray() || (!isTypeKnown() && contains(PrimitiveType.ARRAY));
}

public boolean canBePrimitive(PrimitiveType primitiveType) {
return (isPrimitive() && getPrimitiveType() == primitiveType)
|| (!isTypeKnown() && contains(primitiveType));
}

public static ArgType convertFromPrimitiveType(PrimitiveType primitiveType) {
switch (primitiveType) {
case BOOLEAN:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jadx.core.Consts;
import jadx.core.deobf.NameMapper;
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType;
Expand All @@ -34,8 +35,8 @@
import jadx.core.utils.exceptions.JadxException;

@JadxVisitor(
name = "Debug Info Parser",
desc = "Parse debug information (variable names and types, instruction lines)",
name = "Debug Info Apply",
desc = "Apply debug info to registers (type and names)",
runAfter = {
SSATransform.class,
TypeInferenceVisitor.class,
Expand Down Expand Up @@ -101,7 +102,7 @@ private static void searchDebugInfoByOffset(MethodNode mth, SSAVar ssaVar) {
int startAddr = localVar.getStartAddr();
int endAddr = localVar.getEndAddr();
if (isInside(startOffset, startAddr, endAddr) || isInside(endOffset, startAddr, endAddr)) {
if (LOG.isDebugEnabled()) {
if (Consts.DEBUG && LOG.isDebugEnabled()) {
LOG.debug("Apply debug info by offset for: {} to {}", ssaVar, localVar);
}
applyDebugInfo(mth, ssaVar, localVar.getType(), localVar.getName());
Expand All @@ -126,15 +127,15 @@ private static int getInsnOffsetByArg(InsnArg arg) {
}

public static void applyDebugInfo(MethodNode mth, SSAVar ssaVar, ArgType type, String varName) {
TypeUpdateResult result = mth.root().getTypeUpdate().apply(ssaVar, type);
if (NameMapper.isValidIdentifier(varName)) {
ssaVar.setName(varName);
}
TypeUpdateResult result = mth.root().getTypeUpdate().applyDebug(ssaVar, type);
if (result == TypeUpdateResult.REJECT) {
if (LOG.isDebugEnabled()) {
LOG.debug("Reject debug info of type: {} and name: '{}' for {}, mth: {}", type, varName, ssaVar, mth);
}
} else {
if (NameMapper.isValidIdentifier(varName)) {
ssaVar.setName(varName);
}
detachDebugInfo(ssaVar.getAssign());
ssaVar.getUseList().forEach(DebugInfoApplyVisitor::detachDebugInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,6 @@ public ArgTypeComparator getComparator() {
return comparator;
}

/**
*
*/
private final class ArgTypeComparator implements Comparator<ArgType> {
@Override
public int compare(ArgType a, ArgType b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,22 @@ public final class TypeUpdate {
private final TypeUpdateRegistry listenerRegistry;
private final TypeCompare comparator;

private ThreadLocal<Boolean> applyDebug = new ThreadLocal<>();

public TypeUpdate(RootNode root) {
this.listenerRegistry = initListenerRegistry();
this.comparator = new TypeCompare(root);
}

public TypeUpdateResult applyDebug(SSAVar ssaVar, ArgType candidateType) {
try {
applyDebug.set(true);
return apply(ssaVar, candidateType);
} finally {
applyDebug.set(false);
}
}

public TypeUpdateResult apply(SSAVar ssaVar, ArgType candidateType) {
if (candidateType == null) {
return REJECT;
Expand Down Expand Up @@ -71,6 +82,16 @@ private TypeUpdateResult updateTypeChecked(TypeUpdateInfo updateInfo, InsnArg ar
if (arg.isTypeImmutable() && currentType != ArgType.UNKNOWN) {
return REJECT;
}
TypeCompareEnum compareResult = comparator.compareTypes(candidateType, currentType);
if (compareResult == TypeCompareEnum.CONFLICT) {
return REJECT;
}
if (compareResult == TypeCompareEnum.WIDER || compareResult == TypeCompareEnum.WIDER_BY_GENERIC) {
// allow wider types for apply from debug info
if (applyDebug.get() != Boolean.TRUE) {
return REJECT;
}
}
if (arg instanceof RegisterArg) {
RegisterArg reg = (RegisterArg) arg;
return updateTypeForSsaVar(updateInfo, reg.getSVar(), candidateType);
Expand Down Expand Up @@ -316,6 +337,14 @@ private TypeUpdateResult ifListener(TypeUpdateInfo updateInfo, InsnNode insn, In
if (candidateType.isArray() && updateArgType.canBeArray()) {
return SAME;
}
if (candidateType.isPrimitive()) {
if (updateArgType.canBePrimitive(candidateType.getPrimitiveType())) {
return SAME;
}
if (updateArgType.isTypeKnown() && candidateType.getRegCount() == updateArgType.getRegCount()) {
return SAME;
}
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public int test() {
File output = null;
try {
output = File.createTempFile("f", "a", dir);
if (!output.exists()) {
return 1;
}
return 0;
} catch (Exception e) {
if (output != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package jadx.tests.integration.types;

import org.junit.Test;

import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest;

import static jadx.tests.api.utils.JadxMatchers.containsOne;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class TestPrimitivesInIf extends IntegrationTest {

public static class TestCls {

public boolean test(String str) {
short sh = Short.parseShort(str);
int i = Integer.parseInt(str);
System.out.println(sh + " vs " + i);
return sh == i;
}

public void check() {
assertTrue(test("1"));
}
}

@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();

assertThat(code, containsOne("short sh = Short.parseShort(str);"));
assertThat(code, containsOne("int i = Integer.parseInt(str);"));
assertThat(code, containsOne("return sh == i;"));
}

@Test
public void test2() {
setOutputCFG();
noDebugInfo();
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();

assertThat(code, containsOne("short parseShort = Short.parseShort(str);"));
}
}

0 comments on commit b9fffa1

Please sign in to comment.