From cda44f70cfebfae4875cd77455a171075aebac4d Mon Sep 17 00:00:00 2001 From: "JesusFreke@JesusFreke.com" Date: Sun, 24 Jan 2010 19:33:38 +0000 Subject: [PATCH] Implemented verification for packed-switch and sparse-switch git-svn-id: https://smali.googlecode.com/svn/trunk@580 55b6fa8a-2a1e-11de-a435-ffa8d773f76a --- .../dexlib/Code/Analysis/MethodAnalyzer.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Analysis/MethodAnalyzer.java b/dexlib/src/main/java/org/jf/dexlib/Code/Analysis/MethodAnalyzer.java index 942b3137..b7a1efa9 100644 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Analysis/MethodAnalyzer.java +++ b/dexlib/src/main/java/org/jf/dexlib/Code/Analysis/MethodAnalyzer.java @@ -473,6 +473,10 @@ public class MethodAnalyzer { case GOTO_32: //nothing to do return true; + case PACKED_SWITCH: + return handleSwitch(analyzedInstruction, Format.PackedSwitchData); + case SPARSE_SWITCH: + return handleSwitch(analyzedInstruction, Format.SparseSwitchData); } assert false; return false; @@ -1167,6 +1171,31 @@ public class MethodAnalyzer { return true; } + private boolean handleSwitch(AnalyzedInstruction analyzedInstruction, Format expectedSwitchDataFormat) { + int register = ((SingleRegisterInstruction)analyzedInstruction.instruction).getRegisterA(); + int switchCodeAddressOffset = ((OffsetInstruction)analyzedInstruction.instruction).getTargetAddressOffset(); + + RegisterType registerType = analyzedInstruction.getPreInstructionRegisterType(register); + assert registerType != null; + + if (registerType.category == RegisterType.Category.Unknown) { + return false; + } + + checkRegister(registerType, Primitive32BitCategories); + + int switchDataCodeAddress = this.getInstructionAddress(analyzedInstruction) + switchCodeAddressOffset; + AnalyzedInstruction switchDataAnalyzedInstruction = instructions.get(switchDataCodeAddress); + + if (switchDataAnalyzedInstruction == null || + switchDataAnalyzedInstruction.instruction.getFormat() != expectedSwitchDataFormat) { + throw new ValidationException(String.format("There is no %s structure at code address 0x%x", + expectedSwitchDataFormat.name(), switchDataCodeAddress)); + } + + return true; + } + private static void checkRegister(RegisterType registerType, EnumSet validCategories) { if (!validCategories.contains(registerType.category)) { //TODO: add expected categories to error message