Skip to content

Commit

Permalink
core: guard endless regions processing
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Jun 20, 2014
1 parent e4dde3f commit 26aa504
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.InstructionRemover;
import jadx.core.utils.RegionUtils;
import jadx.core.utils.exceptions.JadxOverflowException;

import java.util.ArrayList;
import java.util.BitSet;
Expand All @@ -49,8 +50,12 @@
public class RegionMaker {
private static final Logger LOG = LoggerFactory.getLogger(RegionMaker.class);

// 'dumb' guard to prevent endless loop in regions processing
private static final int REGIONS_LIMIT = 1000 * 1000;

private final MethodNode mth;
private BitSet processedBlocks;
private int regionsCount;

public RegionMaker(MethodNode mth) {
this.mth = mth;
Expand All @@ -68,6 +73,10 @@ public Region makeRegion(BlockNode startBlock, RegionStack stack) {
processedBlocks.set(id);
}
}
regionsCount++;
if (regionsCount > REGIONS_LIMIT) {
throw new JadxOverflowException("Regions count limit reached");
}

Region r = new Region(stack.peekRegion());
BlockNode next = startBlock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jadx.core.dex.nodes.BlockNode;
import jadx.core.dex.nodes.IRegion;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.utils.exceptions.JadxOverflowException;

import java.util.ArrayDeque;
import java.util.Collection;
Expand All @@ -17,6 +18,8 @@ final class RegionStack {
private static final Logger LOG = LoggerFactory.getLogger(RegionStack.class);
private static final boolean DEBUG = false;

private static final int REGIONS_STACK_LIMIT = 1000;

static {
if (DEBUG) {
LOG.debug("Debug enabled for {}", RegionStack.class);
Expand Down Expand Up @@ -58,8 +61,8 @@ public RegionStack(MethodNode mth) {

public void push(IRegion region) {
stack.push(curState);
if (stack.size() > 1000) {
throw new StackOverflowError("Deep code hierarchy");
if (stack.size() > REGIONS_STACK_LIMIT) {
throw new JadxOverflowException("Regions stack size limit reached");
}
curState = curState.copy();
curState.region = region;
Expand Down
5 changes: 3 additions & 2 deletions jadx-core/src/main/java/jadx/core/utils/ErrorsCounter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jadx.core.dex.attributes.nodes.JadxErrorAttr;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.utils.exceptions.JadxOverflowException;

import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -32,9 +33,9 @@ private static void addError(IAttributeNode node, String msg, Throwable e) {
errorsCount++;

if (e != null) {
if (e.getClass() == StackOverflowError.class) {
if (e.getClass() == JadxOverflowException.class) {
// don't print full stack trace
e = new StackOverflowError(e.getMessage());
e = new JadxOverflowException(e.getMessage());
LOG.error(msg);
} else {
LOG.error(msg, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package jadx.core.utils.exceptions;

public class JadxOverflowException extends JadxRuntimeException {
public JadxOverflowException(String message) {
super(message);
}
}

0 comments on commit 26aa504

Please sign in to comment.