Commit 8dab9b83 authored by Skylot's avatar Skylot

core: fix various codegen errors

parent 7b264ef2
...@@ -50,21 +50,24 @@ public class ClassGen { ...@@ -50,21 +50,24 @@ public class ClassGen {
private final ClassGen parentGen; private final ClassGen parentGen;
private final AnnotationGen annotationGen; private final AnnotationGen annotationGen;
private final boolean fallback; private final boolean fallback;
private final boolean showInconsistentCode;
private boolean showInconsistentCode = false;
private final Set<ClassInfo> imports = new HashSet<ClassInfo>(); private final Set<ClassInfo> imports = new HashSet<ClassInfo>();
private int clsDeclLine; private int clsDeclLine;
public ClassGen(ClassNode cls, ClassGen parentClsGen, IJadxArgs jadxArgs) { public ClassGen(ClassNode cls, IJadxArgs jadxArgs) {
this(cls, parentClsGen, jadxArgs.isFallbackMode()); this(cls, null, jadxArgs.isFallbackMode(), jadxArgs.isShowInconsistentCode());
this.showInconsistentCode = jadxArgs.isShowInconsistentCode(); }
public ClassGen(ClassNode cls, ClassGen parentClsGen) {
this(cls, parentClsGen, parentClsGen.fallback, parentClsGen.showInconsistentCode);
} }
public ClassGen(ClassNode cls, ClassGen parentClsGen, boolean fallback) { public ClassGen(ClassNode cls, ClassGen parentClsGen, boolean fallback, boolean showBadCode) {
this.cls = cls; this.cls = cls;
this.parentGen = parentClsGen; this.parentGen = parentClsGen;
this.fallback = fallback; this.fallback = fallback;
this.showInconsistentCode = showBadCode;
this.annotationGen = new AnnotationGen(cls, this); this.annotationGen = new AnnotationGen(cls, this);
} }
...@@ -230,7 +233,7 @@ public class ClassGen { ...@@ -230,7 +233,7 @@ public class ClassGen {
|| innerCls.isAnonymous()) { || innerCls.isAnonymous()) {
continue; continue;
} }
ClassGen inClGen = new ClassGen(innerCls, getParentGen(), fallback); ClassGen inClGen = new ClassGen(innerCls, getParentGen());
code.newLine(); code.newLine();
inClGen.addClassCode(code); inClGen.addClassCode(code);
imports.addAll(inClGen.getImports()); imports.addAll(inClGen.getImports());
...@@ -299,6 +302,7 @@ public class ClassGen { ...@@ -299,6 +302,7 @@ public class ClassGen {
ErrorsCounter.methodError(mth, "Inconsistent code"); ErrorsCounter.methodError(mth, "Inconsistent code");
if (showInconsistentCode) { if (showInconsistentCode) {
mth.remove(AFlag.INCONSISTENT_CODE); mth.remove(AFlag.INCONSISTENT_CODE);
badCode = false;
} }
} }
MethodGen mthGen; MethodGen mthGen;
...@@ -384,7 +388,7 @@ public class ClassGen { ...@@ -384,7 +388,7 @@ public class ClassGen {
} }
if (f.getCls() != null) { if (f.getCls() != null) {
code.add(' '); code.add(' ');
new ClassGen(f.getCls(), this, fallback).addClassBody(code); new ClassGen(f.getCls(), this).addClassBody(code);
} }
if (it.hasNext()) { if (it.hasNext()) {
code.add(','); code.add(',');
......
...@@ -15,7 +15,7 @@ public class CodeGen extends AbstractVisitor { ...@@ -15,7 +15,7 @@ public class CodeGen extends AbstractVisitor {
@Override @Override
public boolean visit(ClassNode cls) throws CodegenException { public boolean visit(ClassNode cls) throws CodegenException {
ClassGen clsGen = new ClassGen(cls, null, args); ClassGen clsGen = new ClassGen(cls, args);
CodeWriter clsCode = clsGen.makeClass(); CodeWriter clsCode = clsGen.makeClass();
clsCode.finish(); clsCode.finish();
cls.setCode(clsCode); cls.setCode(clsCode);
......
...@@ -554,7 +554,7 @@ public class InsnGen { ...@@ -554,7 +554,7 @@ public class InsnGen {
useClass(code, parent); useClass(code, parent);
} }
code.add("() "); code.add("() ");
new ClassGen(cls, mgen.getClassGen().getParentGen(), fallback).addClassBody(code); new ClassGen(cls, mgen.getClassGen().getParentGen()).addClassBody(code);
return; return;
} }
if (insn.isSelf()) { if (insn.isSelf()) {
......
...@@ -235,7 +235,7 @@ public class MethodGen { ...@@ -235,7 +235,7 @@ public class MethodGen {
* Return fallback variant of method codegen * Return fallback variant of method codegen
*/ */
public static MethodGen getFallbackMethodGen(MethodNode mth) { public static MethodGen getFallbackMethodGen(MethodNode mth) {
ClassGen clsGen = new ClassGen(mth.getParentClass(), null, true); ClassGen clsGen = new ClassGen(mth.getParentClass(), null, true, true);
return new MethodGen(clsGen, mth); return new MethodGen(clsGen, mth);
} }
......
...@@ -88,8 +88,10 @@ public class TryCatchBlock { ...@@ -88,8 +88,10 @@ public class TryCatchBlock {
insn.removeAttr(attr); insn.removeAttr(attr);
} }
insns.clear(); insns.clear();
for (BlockNode block : mth.getBasicBlocks()) { if (mth.getBasicBlocks() != null) {
block.removeAttr(attr); for (BlockNode block : mth.getBasicBlocks()) {
block.removeAttr(attr);
}
} }
} }
......
package jadx.core.dex.visitors; package jadx.core.dex.visitors;
import jadx.core.dex.attributes.AType;
import jadx.core.dex.info.ClassInfo; import jadx.core.dex.info.ClassInfo;
import jadx.core.dex.instructions.args.ArgType; import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.instructions.args.InsnArg; import jadx.core.dex.instructions.args.InsnArg;
...@@ -39,7 +40,7 @@ public class DependencyCollector extends AbstractVisitor { ...@@ -39,7 +40,7 @@ public class DependencyCollector extends AbstractVisitor {
} }
// TODO: process annotations and generics // TODO: process annotations and generics
for (MethodNode methodNode : cls.getMethods()) { for (MethodNode methodNode : cls.getMethods()) {
if (methodNode.isNoCode()) { if (methodNode.isNoCode() || methodNode.contains(AType.JADX_ERROR)) {
continue; continue;
} }
processMethod(dex, depList, methodNode); processMethod(dex, depList, methodNode);
......
...@@ -66,6 +66,10 @@ public class ProcessTryCatchRegions extends AbstractRegionVisitor { ...@@ -66,6 +66,10 @@ public class ProcessTryCatchRegions extends AbstractRegionVisitor {
// for each try block search nearest dominator block // for each try block search nearest dominator block
for (TryCatchBlock tb : tryBlocks) { for (TryCatchBlock tb : tryBlocks) {
if (tb.getHandlersCount() == 0) {
LOG.warn("No exception handlers in catch block, method: {}", mth);
continue;
}
BitSet bs = new BitSet(mth.getBasicBlocks().size()); BitSet bs = new BitSet(mth.getBasicBlocks().size());
for (ExceptionHandler excHandler : tb.getHandlers()) { for (ExceptionHandler excHandler : tb.getHandlers()) {
SplitterBlockAttr splitter = excHandler.getHandlerBlock().get(AType.SPLITTER_BLOCK); SplitterBlockAttr splitter = excHandler.getHandlerBlock().get(AType.SPLITTER_BLOCK);
......
...@@ -202,14 +202,19 @@ public class SSATransform extends AbstractVisitor { ...@@ -202,14 +202,19 @@ public class SSATransform extends AbstractVisitor {
private static void fixPhiInTryCatch(PhiInsn phi) { private static void fixPhiInTryCatch(PhiInsn phi) {
int argsCount = phi.getArgsCount(); int argsCount = phi.getArgsCount();
for (int i = 0; i < argsCount; i++) { int k = 0;
RegisterArg arg = phi.getArg(i); while (k < argsCount) {
RegisterArg arg = phi.getArg(k);
InsnNode parentInsn = arg.getAssignInsn(); InsnNode parentInsn = arg.getAssignInsn();
if (parentInsn != null if (parentInsn != null
&& parentInsn.getResult() != null && parentInsn.getResult() != null
&& parentInsn.contains(AFlag.TRY_LEAVE)) { && parentInsn.contains(AFlag.TRY_LEAVE)) {
phi.removeArg(arg); if (phi.removeArg(arg)) {
argsCount--;
continue;
}
} }
k++;
} }
} }
......
...@@ -312,8 +312,9 @@ public class BlockUtils { ...@@ -312,8 +312,9 @@ public class BlockUtils {
} }
} }
private static boolean traverseSuccessorsUntil(BlockNode from, BlockNode until, BitSet visited) { private static boolean traverseSuccessorsUntil(BlockNode from, BlockNode until, BitSet visited, boolean clean) {
for (BlockNode s : from.getCleanSuccessors()) { List<BlockNode> nodes = clean ? from.getCleanSuccessors() : from.getSuccessors();
for (BlockNode s : nodes) {
if (s == until) { if (s == until) {
return true; return true;
} }
...@@ -323,7 +324,7 @@ public class BlockUtils { ...@@ -323,7 +324,7 @@ public class BlockUtils {
if (until.isDominator(s)) { if (until.isDominator(s)) {
return true; return true;
} }
if (traverseSuccessorsUntil(s, until, visited)) { if (traverseSuccessorsUntil(s, until, visited, clean)) {
return true; return true;
} }
} }
...@@ -340,7 +341,19 @@ public class BlockUtils { ...@@ -340,7 +341,19 @@ public class BlockUtils {
if (start.getPredecessors().contains(end)) { if (start.getPredecessors().contains(end)) {
return false; return false;
} }
return traverseSuccessorsUntil(start, end, new BitSet()); return traverseSuccessorsUntil(start, end, new BitSet(), true);
}
public static boolean isAnyPathExists(BlockNode start, BlockNode end) {
if (start == end
|| end.isDominator(start)
|| start.getSuccessors().contains(end)) {
return true;
}
if (start.getPredecessors().contains(end)) {
return false;
}
return traverseSuccessorsUntil(start, end, new BitSet(), false);
} }
public static BlockNode getTopBlock(Collection<BlockNode> blocks) { public static BlockNode getTopBlock(Collection<BlockNode> blocks) {
...@@ -350,7 +363,7 @@ public class BlockUtils { ...@@ -350,7 +363,7 @@ public class BlockUtils {
for (BlockNode from : blocks) { for (BlockNode from : blocks) {
boolean top = true; boolean top = true;
for (BlockNode to : blocks) { for (BlockNode to : blocks) {
if (from != to && !isPathExists(from, to)) { if (from != to && !isAnyPathExists(from, to)) {
top = false; top = false;
break; break;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment