use of org.jf.dexlib2.iface.instruction.formats.Instruction20bc in project atlas by alibaba.
the class InsTructionsReIClassDef method reInstructions.
@Override
protected Iterable<? extends Instruction> reInstructions(Iterable<? extends Instruction> instructions) {
final List<Instruction> reinstructions = new ArrayList<Instruction>();
for (final Instruction instruction : instructions) {
if (instruction instanceof ReferenceInstruction) {
Opcode opcode = instruction.getOpcode();
if (opcode.referenceType == ReferenceType.METHOD) {
boolean isBasic = false;
MethodReference methodReference = null;
try {
methodReference = (MethodReference) ((ReferenceInstruction) instruction).getReference();
if (DexObfuscatedTool.aliProguard && methodReference.getName().equals("getName") && methodReference.getDefiningClass().equals("Ljava/lang/Class;") && opcode.equals(Opcode.INVOKE_VIRTUAL)) {
opcode = Opcode.INVOKE_STATIC;
List<CharSequence> list = new ArrayList<>();
list.add("Ljava/lang/Class;");
Iterable iterable = new Iterable() {
@Override
public Iterator iterator() {
return list.iterator();
}
};
ImmutableMethodReference immutableMethodReference = new ImmutableMethodReference("Lcom/ali/mobisecenhance/ReflectMap;", "getName", iterable, "Ljava/lang/String;");
reinstructions.add(new ImmutableInstruction35c(opcode, ((Instruction35c) instruction).getRegisterCount(), ((Instruction35c) instruction).getRegisterC(), ((Instruction35c) instruction).getRegisterD(), ((Instruction35c) instruction).getRegisterE(), ((Instruction35c) instruction).getRegisterF(), ((Instruction35c) instruction).getRegisterG(), immutableMethodReference));
continue;
}
if (methodReference.getDefiningClass().contains("Ljava/lang") || methodReference.getDefiningClass().startsWith("Ljava/util/") || methodReference.getDefiningClass().startsWith("[Ljava/lang")) {
reinstructions.add(ImmutableInstruction.of(instruction));
continue;
}
String returnType = methodReference.getReturnType();
boolean isArray = false;
if (returnType.startsWith("[")) {
isArray = true;
}
String methodName = methodReference.getName();
if (basicType.containsKey(returnType)) {
isBasic = true;
}
List<? extends CharSequence> paramTypes = methodReference.getParameterTypes();
List<CharSequence> dalvikParamTypes = new ArrayList<CharSequence>();
List<CharSequence> newParamTypes = new ArrayList<CharSequence>();
for (CharSequence charSequence : paramTypes) {
if (basicType.containsKey(charSequence.toString())) {
newParamTypes.add(charSequence);
dalvikParamTypes.add(basicType.get(charSequence.toString()));
continue;
}
boolean isArray1 = charSequence.toString().startsWith("[");
dalvikParamTypes.add(DefineUtils.getDalvikClassName(charSequence.toString()) + (isArray ? "[]" : ""));
newParamTypes.add(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(charSequence.toString())).className, isArray1));
}
final ImmutableMethodReference immutableReference = new ImmutableMethodReference(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(methodReference.getDefiningClass())).className, methodReference.getDefiningClass().startsWith("[")), classProcessor.methodProcess(DefineUtils.getDalvikClassName(methodReference.getDefiningClass()), methodReference.getName(), isBasic ? basicType.get(methodReference.getReturnType()) : DefineUtils.getDalvikClassName(methodReference.getReturnType()) + (isArray ? "[]" : ""), StringUtils.join(dalvikParamTypes.toArray(), ",")).methodName, newParamTypes, isBasic ? returnType : DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(methodReference.getReturnType())).className, methodReference.getReturnType().startsWith("[")));
if (instruction instanceof Instruction3rc) {
reinstructions.add(new ImmutableInstruction3rc(instruction.getOpcode(), ((Instruction3rc) instruction).getStartRegister(), ((Instruction3rc) instruction).getRegisterCount(), immutableReference));
} else if (instruction instanceof Instruction20bc) {
reinstructions.add(new ImmutableInstruction20bc(instruction.getOpcode(), ((Instruction20bc) instruction).getVerificationError(), immutableReference));
} else if (instruction instanceof Instruction21c) {
reinstructions.add(new ImmutableInstruction21c(instruction.getOpcode(), ((Instruction21c) instruction).getRegisterA(), immutableReference));
} else if (instruction instanceof Instruction22c) {
reinstructions.add(new ImmutableInstruction22c(instruction.getOpcode(), ((Instruction22c) instruction).getRegisterA(), ((Instruction22c) instruction).getRegisterB(), immutableReference));
} else if (instruction instanceof Instruction31c) {
reinstructions.add(new ImmutableInstruction31c(instruction.getOpcode(), ((Instruction31c) instruction).getRegisterA(), immutableReference));
} else if (instruction instanceof Instruction35c) {
reinstructions.add(new ImmutableInstruction35c(instruction.getOpcode(), ((Instruction35c) instruction).getRegisterCount(), ((Instruction35c) instruction).getRegisterC(), ((Instruction35c) instruction).getRegisterD(), ((Instruction35c) instruction).getRegisterE(), ((Instruction35c) instruction).getRegisterF(), ((Instruction35c) instruction).getRegisterG(), immutableReference));
}
} catch (Exception e) {
}
} else if (opcode.referenceType == ReferenceType.FIELD) {
FieldReference fieldReference = null;
boolean isBasic = false;
boolean isBasicArray = false;
fieldReference = (FieldReference) ((ReferenceInstruction) instruction).getReference();
if (fieldReference.getDefiningClass().startsWith("Ljava/lang/") || fieldReference.getDefiningClass().startsWith("Ljava/util/") || fieldReference.getDefiningClass().startsWith("[Ljava/lang/")) {
reinstructions.add(ImmutableInstruction.of(instruction));
continue;
}
if (basicType.containsKey(fieldReference.getType())) {
isBasic = true;
}
final ImmutableFieldReference immutableFieldReference = new ImmutableFieldReference(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(fieldReference.getDefiningClass())).className, fieldReference.getDefiningClass().startsWith("[")), classProcessor.filedProcess(DefineUtils.getDalvikClassName(fieldReference.getDefiningClass()), isBasic ? basicType.get(fieldReference.getType()) : DefineUtils.getDalvikClassName(fieldReference.getType()), fieldReference.getName()).fieldName, isBasic ? fieldReference.getType() : DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(fieldReference.getType())).className, fieldReference.getType().startsWith("[")));
if (instruction instanceof Instruction3rc) {
reinstructions.add(new ImmutableInstruction3rc(instruction.getOpcode(), ((Instruction3rc) instruction).getStartRegister(), ((Instruction3rc) instruction).getRegisterCount(), immutableFieldReference));
} else if (instruction instanceof Instruction20bc) {
reinstructions.add(new ImmutableInstruction20bc(instruction.getOpcode(), ((Instruction20bc) instruction).getVerificationError(), immutableFieldReference));
} else if (instruction instanceof Instruction21c) {
reinstructions.add(new ImmutableInstruction21c(instruction.getOpcode(), ((Instruction21c) instruction).getRegisterA(), immutableFieldReference));
} else if (instruction instanceof Instruction22c) {
reinstructions.add(new ImmutableInstruction22c(instruction.getOpcode(), ((Instruction22c) instruction).getRegisterA(), ((Instruction22c) instruction).getRegisterB(), immutableFieldReference));
} else if (instruction instanceof Instruction31c) {
reinstructions.add(new ImmutableInstruction31c(instruction.getOpcode(), ((Instruction31c) instruction).getRegisterA(), immutableFieldReference));
} else if (instruction instanceof Instruction35c) {
reinstructions.add(new ImmutableInstruction35c(instruction.getOpcode(), ((Instruction35c) instruction).getRegisterCount(), ((Instruction35c) instruction).getRegisterC(), ((Instruction35c) instruction).getRegisterD(), ((Instruction35c) instruction).getRegisterE(), ((Instruction35c) instruction).getRegisterF(), ((Instruction35c) instruction).getRegisterG(), immutableFieldReference));
}
} else if (opcode.referenceType == ReferenceType.TYPE) {
TypeReference typeReference = (TypeReference) ((ReferenceInstruction) instruction).getReference();
String type = typeReference.getType();
if (!basicType.containsKey(type) && !type.startsWith("Ljava/lang") && !type.startsWith("Ljava/util/") && !type.startsWith("[Ljava/lang")) {
type = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(type)).className, type.startsWith("["));
}
ImmutableTypeReference immutableTypeReference = new ImmutableTypeReference(type);
if (instruction instanceof Instruction3rc) {
reinstructions.add(new ImmutableInstruction3rc(instruction.getOpcode(), ((Instruction3rc) instruction).getStartRegister(), ((Instruction3rc) instruction).getRegisterCount(), immutableTypeReference));
} else if (instruction instanceof Instruction20bc) {
reinstructions.add(new ImmutableInstruction20bc(instruction.getOpcode(), ((Instruction20bc) instruction).getVerificationError(), immutableTypeReference));
} else if (instruction instanceof Instruction21c) {
reinstructions.add(new ImmutableInstruction21c(instruction.getOpcode(), ((Instruction21c) instruction).getRegisterA(), immutableTypeReference));
} else if (instruction instanceof Instruction22c) {
reinstructions.add(new ImmutableInstruction22c(instruction.getOpcode(), ((Instruction22c) instruction).getRegisterA(), ((Instruction22c) instruction).getRegisterB(), immutableTypeReference));
} else if (instruction instanceof Instruction31c) {
reinstructions.add(new ImmutableInstruction31c(instruction.getOpcode(), ((Instruction31c) instruction).getRegisterA(), immutableTypeReference));
} else if (instruction instanceof Instruction35c) {
reinstructions.add(new ImmutableInstruction35c(instruction.getOpcode(), ((Instruction35c) instruction).getRegisterCount(), ((Instruction35c) instruction).getRegisterC(), ((Instruction35c) instruction).getRegisterD(), ((Instruction35c) instruction).getRegisterE(), ((Instruction35c) instruction).getRegisterF(), ((Instruction35c) instruction).getRegisterG(), immutableTypeReference));
}
} else if (opcode.referenceType == ReferenceType.STRING) {
StringReference stringReference = (StringReference) ((ReferenceInstruction) instruction).getReference();
String type = stringReference.getString();
// if (!basicType.contains(type) && !type.startsWith("[Ljava/lang")) {
// type = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(type)).className);
// }
ImmutableStringReference immutableStringReference = new ImmutableStringReference(type);
if (instruction instanceof Instruction3rc) {
reinstructions.add(new ImmutableInstruction3rc(instruction.getOpcode(), ((Instruction3rc) instruction).getStartRegister(), ((Instruction3rc) instruction).getRegisterCount(), immutableStringReference));
} else if (instruction instanceof Instruction20bc) {
reinstructions.add(new ImmutableInstruction20bc(instruction.getOpcode(), ((Instruction20bc) instruction).getVerificationError(), immutableStringReference));
} else if (instruction instanceof Instruction21c) {
reinstructions.add(new ImmutableInstruction21c(instruction.getOpcode(), ((Instruction21c) instruction).getRegisterA(), immutableStringReference));
} else if (instruction instanceof Instruction22c) {
reinstructions.add(new ImmutableInstruction22c(instruction.getOpcode(), ((Instruction22c) instruction).getRegisterA(), ((Instruction22c) instruction).getRegisterB(), immutableStringReference));
} else if (instruction instanceof Instruction31c) {
reinstructions.add(new ImmutableInstruction31c(instruction.getOpcode(), ((Instruction31c) instruction).getRegisterA(), immutableStringReference));
} else if (instruction instanceof Instruction35c) {
reinstructions.add(new ImmutableInstruction35c(instruction.getOpcode(), ((Instruction35c) instruction).getRegisterCount(), ((Instruction35c) instruction).getRegisterC(), ((Instruction35c) instruction).getRegisterD(), ((Instruction35c) instruction).getRegisterE(), ((Instruction35c) instruction).getRegisterF(), ((Instruction35c) instruction).getRegisterG(), immutableStringReference));
}
} else {
reinstructions.add(ImmutableInstruction.of(instruction));
}
} else {
reinstructions.add(ImmutableInstruction.of(instruction));
}
}
return new Iterable<Instruction>() {
@Override
public Iterator<Instruction> iterator() {
return reinstructions.iterator();
}
};
}
use of org.jf.dexlib2.iface.instruction.formats.Instruction20bc in project smali by JesusFreke.
the class InstructionMethodItem method writeTo.
@Override
public boolean writeTo(BaksmaliWriter writer) throws IOException {
Opcode opcode = instruction.getOpcode();
String verificationErrorName = null;
Writable referenceWritable = null;
Writable referenceWritable2 = null;
boolean commentOutInstruction = false;
if (instruction instanceof Instruction20bc) {
int verificationError = ((Instruction20bc) instruction).getVerificationError();
verificationErrorName = VerificationError.getVerificationErrorName(verificationError);
if (verificationErrorName == null) {
writer.write("#was invalid verification error type: ");
writer.writeSignedIntAsDec(verificationError);
writer.write("\n");
verificationErrorName = "generic-error";
}
}
if (instruction instanceof ReferenceInstruction) {
ReferenceInstruction referenceInstruction = (ReferenceInstruction) instruction;
Reference reference = referenceInstruction.getReference();
try {
reference.validateReference();
referenceWritable = () -> writer.writeReference(reference);
} catch (Reference.InvalidReferenceException ex) {
commentOutInstruction = true;
writer.write("#");
writer.write(ex.getMessage());
writer.write("\n");
referenceWritable = () -> writer.write(ex.getInvalidReferenceRepresentation());
}
if (instruction instanceof DualReferenceInstruction) {
DualReferenceInstruction dualReferenceInstruction = (DualReferenceInstruction) instruction;
try {
Reference reference2 = dualReferenceInstruction.getReference2();
reference2.validateReference();
referenceWritable2 = () -> writer.writeReference(reference2);
} catch (Reference.InvalidReferenceException ex) {
commentOutInstruction = true;
writer.write("#");
writer.write(ex.getMessage());
writer.write("\n");
referenceWritable = () -> writer.write(ex.getInvalidReferenceRepresentation());
}
}
}
if (instruction instanceof Instruction31t) {
boolean validPayload = true;
switch(instruction.getOpcode()) {
case PACKED_SWITCH:
int baseAddress = methodDef.getPackedSwitchBaseAddress(this.codeAddress + ((Instruction31t) instruction).getCodeOffset());
if (baseAddress == -1) {
validPayload = false;
}
break;
case SPARSE_SWITCH:
baseAddress = methodDef.getSparseSwitchBaseAddress(this.codeAddress + ((Instruction31t) instruction).getCodeOffset());
if (baseAddress == -1) {
validPayload = false;
}
break;
case FILL_ARRAY_DATA:
try {
methodDef.findPayloadOffset(this.codeAddress + ((Instruction31t) instruction).getCodeOffset(), Opcode.ARRAY_PAYLOAD);
} catch (InvalidSwitchPayload ex) {
validPayload = false;
}
break;
default:
throw new ExceptionWithContext("Invalid 31t opcode: %s", instruction.getOpcode());
}
if (!validPayload) {
writer.write("#invalid payload reference\n");
commentOutInstruction = true;
}
}
if (opcode.odexOnly()) {
if (!isAllowedOdex(opcode)) {
writer.write("#disallowed odex opcode\n");
commentOutInstruction = true;
}
}
if (commentOutInstruction) {
writer.write("#");
}
switch(instruction.getOpcode().format) {
case Format10t:
case Format20t:
case Format30t:
writeOpcode(writer);
writer.write(' ');
writeTargetLabel(writer);
break;
case Format10x:
if (instruction instanceof UnknownInstruction) {
writer.write("#unknown opcode: 0x");
writer.writeUnsignedLongAsHex(((UnknownInstruction) instruction).getOriginalOpcode());
writer.write('\n');
}
writeOpcode(writer);
break;
case Format11n:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeLiteral(writer);
break;
case Format11x:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
break;
case Format12x:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
break;
case Format20bc:
writeOpcode(writer);
writer.write(' ');
writer.write(verificationErrorName);
writer.write(", ");
assert referenceWritable != null;
referenceWritable.write();
break;
case Format21c:
case Format31c:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
referenceWritable.write();
break;
case Format21ih:
case Format21lh:
case Format21s:
case Format31i:
case Format51l:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeLiteral(writer);
if (instruction.getOpcode().setsWideRegister()) {
writeCommentIfLikelyDouble(writer);
} else {
boolean isResourceId = writeCommentIfResourceId(writer);
if (!isResourceId)
writeCommentIfLikelyFloat(writer);
}
break;
case Format21t:
case Format31t:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeTargetLabel(writer);
break;
case Format22b:
case Format22s:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
writer.write(", ");
writeLiteral(writer);
break;
case Format22c:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
writer.write(", ");
assert referenceWritable != null;
referenceWritable.write();
break;
case Format22cs:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
writer.write(", ");
writeFieldOffset(writer);
break;
case Format22t:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
writer.write(", ");
writeTargetLabel(writer);
break;
case Format22x:
case Format32x:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
break;
case Format23x:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
writer.write(", ");
writeThirdRegister(writer);
break;
case Format35c:
writeOpcode(writer);
writer.write(' ');
writeInvokeRegisters(writer);
writer.write(", ");
assert referenceWritable != null;
referenceWritable.write();
break;
case Format35mi:
writeOpcode(writer);
writer.write(' ');
writeInvokeRegisters(writer);
writer.write(", ");
writeInlineIndex(writer);
break;
case Format35ms:
writeOpcode(writer);
writer.write(' ');
writeInvokeRegisters(writer);
writer.write(", ");
writeVtableIndex(writer);
break;
case Format3rc:
writeOpcode(writer);
writer.write(' ');
writeInvokeRangeRegisters(writer);
writer.write(", ");
assert referenceWritable != null;
referenceWritable.write();
break;
case Format3rmi:
writeOpcode(writer);
writer.write(' ');
writeInvokeRangeRegisters(writer);
writer.write(", ");
writeInlineIndex(writer);
break;
case Format3rms:
writeOpcode(writer);
writer.write(' ');
writeInvokeRangeRegisters(writer);
writer.write(", ");
writeVtableIndex(writer);
break;
case Format45cc:
writeOpcode(writer);
writer.write(' ');
writeInvokeRegisters(writer);
writer.write(", ");
assert referenceWritable != null;
referenceWritable.write();
writer.write(", ");
assert referenceWritable2 != null;
referenceWritable2.write();
break;
case Format4rcc:
writeOpcode(writer);
writer.write(' ');
writeInvokeRangeRegisters(writer);
writer.write(", ");
assert referenceWritable != null;
referenceWritable.write();
writer.write(", ");
assert referenceWritable2 != null;
referenceWritable2.write();
break;
default:
assert false;
return false;
}
if (commentOutInstruction) {
writer.write("\nnop");
}
return true;
}
use of org.jf.dexlib2.iface.instruction.formats.Instruction20bc in project smali by JesusFreke.
the class InstructionMethodItem method writeTo.
@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
Opcode opcode = instruction.getOpcode();
String verificationErrorName = null;
String referenceString = null;
String referenceString2 = null;
boolean commentOutInstruction = false;
if (instruction instanceof Instruction20bc) {
int verificationError = ((Instruction20bc) instruction).getVerificationError();
verificationErrorName = VerificationError.getVerificationErrorName(verificationError);
if (verificationErrorName == null) {
writer.write("#was invalid verification error type: ");
writer.printSignedIntAsDec(verificationError);
writer.write("\n");
verificationErrorName = "generic-error";
}
}
if (instruction instanceof ReferenceInstruction) {
ReferenceInstruction referenceInstruction = (ReferenceInstruction) instruction;
String classContext = null;
if (methodDef.classDef.options.implicitReferences) {
classContext = methodDef.method.getDefiningClass();
}
try {
Reference reference = referenceInstruction.getReference();
referenceString = ReferenceUtil.getReferenceString(reference, classContext);
assert referenceString != null;
} catch (InvalidItemIndex ex) {
commentOutInstruction = true;
referenceString = writeInvalidItemIndex(ex, referenceInstruction.getReferenceType(), writer);
} catch (ReferenceType.InvalidReferenceTypeException ex) {
writer.write("#invalid reference type: ");
writer.printSignedIntAsDec(ex.getReferenceType());
commentOutInstruction = true;
referenceString = "invalid_reference";
}
if (instruction instanceof DualReferenceInstruction) {
DualReferenceInstruction dualReferenceInstruction = (DualReferenceInstruction) instruction;
try {
Reference reference2 = dualReferenceInstruction.getReference2();
referenceString2 = ReferenceUtil.getReferenceString(reference2, classContext);
} catch (InvalidItemIndex ex) {
commentOutInstruction = true;
referenceString2 = writeInvalidItemIndex(ex, dualReferenceInstruction.getReferenceType2(), writer);
} catch (ReferenceType.InvalidReferenceTypeException ex) {
writer.write("#invalid reference type: ");
writer.printSignedIntAsDec(ex.getReferenceType());
commentOutInstruction = true;
referenceString2 = "invalid_reference";
}
}
}
if (instruction instanceof Instruction31t) {
boolean validPayload = true;
switch(instruction.getOpcode()) {
case PACKED_SWITCH:
int baseAddress = methodDef.getPackedSwitchBaseAddress(this.codeAddress + ((Instruction31t) instruction).getCodeOffset());
if (baseAddress == -1) {
validPayload = false;
}
break;
case SPARSE_SWITCH:
baseAddress = methodDef.getSparseSwitchBaseAddress(this.codeAddress + ((Instruction31t) instruction).getCodeOffset());
if (baseAddress == -1) {
validPayload = false;
}
break;
case FILL_ARRAY_DATA:
try {
methodDef.findPayloadOffset(this.codeAddress + ((Instruction31t) instruction).getCodeOffset(), Opcode.ARRAY_PAYLOAD);
} catch (InvalidSwitchPayload ex) {
validPayload = false;
}
break;
default:
throw new ExceptionWithContext("Invalid 31t opcode: %s", instruction.getOpcode());
}
if (!validPayload) {
writer.write("#invalid payload reference\n");
commentOutInstruction = true;
}
}
if (opcode.odexOnly()) {
if (!isAllowedOdex(opcode)) {
writer.write("#disallowed odex opcode\n");
commentOutInstruction = true;
}
}
if (commentOutInstruction) {
writer.write("#");
}
switch(instruction.getOpcode().format) {
case Format10t:
writeOpcode(writer);
writer.write(' ');
writeTargetLabel(writer);
break;
case Format10x:
if (instruction instanceof UnknownInstruction) {
writer.write("#unknown opcode: 0x");
writer.printUnsignedLongAsHex(((UnknownInstruction) instruction).getOriginalOpcode());
writer.write('\n');
}
writeOpcode(writer);
break;
case Format11n:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeLiteral(writer);
break;
case Format11x:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
break;
case Format12x:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
break;
case Format20bc:
writeOpcode(writer);
writer.write(' ');
writer.write(verificationErrorName);
writer.write(", ");
writer.write(referenceString);
break;
case Format20t:
case Format30t:
writeOpcode(writer);
writer.write(' ');
writeTargetLabel(writer);
break;
case Format21c:
case Format31c:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writer.write(referenceString);
break;
case Format21ih:
case Format21lh:
case Format21s:
case Format31i:
case Format51l:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeLiteral(writer);
if (instruction.getOpcode().setsWideRegister()) {
writeCommentIfLikelyDouble(writer);
} else {
boolean isResourceId = writeCommentIfResourceId(writer);
if (!isResourceId)
writeCommentIfLikelyFloat(writer);
}
break;
case Format21t:
case Format31t:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeTargetLabel(writer);
break;
case Format22b:
case Format22s:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
writer.write(", ");
writeLiteral(writer);
break;
case Format22c:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
writer.write(", ");
writer.write(referenceString);
break;
case Format22cs:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
writer.write(", ");
writeFieldOffset(writer);
break;
case Format22t:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
writer.write(", ");
writeTargetLabel(writer);
break;
case Format22x:
case Format32x:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
break;
case Format23x:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
writer.write(", ");
writeThirdRegister(writer);
break;
case Format35c:
writeOpcode(writer);
writer.write(' ');
writeInvokeRegisters(writer);
writer.write(", ");
writer.write(referenceString);
break;
case Format35mi:
writeOpcode(writer);
writer.write(' ');
writeInvokeRegisters(writer);
writer.write(", ");
writeInlineIndex(writer);
break;
case Format35ms:
writeOpcode(writer);
writer.write(' ');
writeInvokeRegisters(writer);
writer.write(", ");
writeVtableIndex(writer);
break;
case Format3rc:
writeOpcode(writer);
writer.write(' ');
writeInvokeRangeRegisters(writer);
writer.write(", ");
writer.write(referenceString);
break;
case Format3rmi:
writeOpcode(writer);
writer.write(' ');
writeInvokeRangeRegisters(writer);
writer.write(", ");
writeInlineIndex(writer);
break;
case Format3rms:
writeOpcode(writer);
writer.write(' ');
writeInvokeRangeRegisters(writer);
writer.write(", ");
writeVtableIndex(writer);
break;
case Format45cc:
writeOpcode(writer);
writer.write(' ');
writeInvokeRegisters(writer);
writer.write(", ");
writer.write(referenceString);
writer.write(", ");
writer.write(referenceString2);
break;
case Format4rcc:
writeOpcode(writer);
writer.write(' ');
writeInvokeRangeRegisters(writer);
writer.write(", ");
writer.write(referenceString);
writer.write(", ");
writer.write(referenceString2);
break;
default:
assert false;
return false;
}
if (commentOutInstruction) {
writer.write("\nnop");
}
return true;
}
use of org.jf.dexlib2.iface.instruction.formats.Instruction20bc in project atlas by alibaba.
the class InstructionMethodItem method writeTo.
@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
Opcode opcode = instruction.getOpcode();
String verificationErrorName = null;
String referenceString = null;
String referenceString2 = null;
boolean commentOutInstruction = false;
if (instruction instanceof Instruction20bc) {
int verificationError = ((Instruction20bc) instruction).getVerificationError();
verificationErrorName = VerificationError.getVerificationErrorName(verificationError);
if (verificationErrorName == null) {
writer.write("#was invalid verification error type: ");
writer.printSignedIntAsDec(verificationError);
writer.write("\n");
verificationErrorName = "generic-error";
}
}
if (instruction instanceof ReferenceInstruction) {
ReferenceInstruction referenceInstruction = (ReferenceInstruction) instruction;
String classContext = null;
if (methodDef.classDef.options.implicitReferences) {
classContext = methodDef.method.getDefiningClass();
}
try {
Reference reference = referenceInstruction.getReference();
referenceString = org.jf.dexlib2.util.ReferenceUtil.getReferenceString(reference, classContext);
assert referenceString != null;
} catch (InvalidItemIndex ex) {
commentOutInstruction = true;
referenceString = writeInvalidItemIndex(ex, referenceInstruction.getReferenceType(), writer);
} catch (ReferenceType.InvalidReferenceTypeException ex) {
writer.write("#invalid reference type: ");
writer.printSignedIntAsDec(ex.getReferenceType());
commentOutInstruction = true;
referenceString = "invalid_reference";
}
if (instruction instanceof DualReferenceInstruction) {
DualReferenceInstruction dualReferenceInstruction = (DualReferenceInstruction) instruction;
try {
Reference reference2 = dualReferenceInstruction.getReference2();
referenceString2 = org.jf.dexlib2.util.ReferenceUtil.getReferenceString(reference2, classContext);
} catch (InvalidItemIndex ex) {
commentOutInstruction = true;
referenceString2 = writeInvalidItemIndex(ex, dualReferenceInstruction.getReferenceType2(), writer);
} catch (ReferenceType.InvalidReferenceTypeException ex) {
writer.write("#invalid reference type: ");
writer.printSignedIntAsDec(ex.getReferenceType());
commentOutInstruction = true;
referenceString2 = "invalid_reference";
}
}
}
if (instruction instanceof Instruction31t) {
boolean validPayload = true;
switch(instruction.getOpcode()) {
case PACKED_SWITCH:
int baseAddress = methodDef.getPackedSwitchBaseAddress(this.codeAddress + ((Instruction31t) instruction).getCodeOffset());
if (baseAddress == -1) {
validPayload = false;
}
break;
case SPARSE_SWITCH:
baseAddress = methodDef.getSparseSwitchBaseAddress(this.codeAddress + ((Instruction31t) instruction).getCodeOffset());
if (baseAddress == -1) {
validPayload = false;
}
break;
case FILL_ARRAY_DATA:
try {
methodDef.findPayloadOffset(this.codeAddress + ((Instruction31t) instruction).getCodeOffset(), Opcode.ARRAY_PAYLOAD);
} catch (org.jf.baksmali.Adaptors.MethodDefinition.InvalidSwitchPayload ex) {
validPayload = false;
}
break;
default:
throw new ExceptionWithContext("Invalid 31t opcode: %s", instruction.getOpcode());
}
if (!validPayload) {
writer.write("#invalid payload reference\n");
commentOutInstruction = true;
}
}
if (opcode.odexOnly()) {
if (!isAllowedOdex(opcode)) {
writer.write("#disallowed odex opcode\n");
commentOutInstruction = true;
}
}
if (commentOutInstruction) {
writer.write("#");
}
switch(instruction.getOpcode().format) {
case Format10t:
writeOpcode(writer);
writer.write(' ');
writeTargetLabel(writer);
break;
case Format10x:
if (instruction instanceof UnknownInstruction) {
writer.write("#unknown opcode: 0x");
writer.printUnsignedLongAsHex(((UnknownInstruction) instruction).getOriginalOpcode());
writer.write('\n');
}
writeOpcode(writer);
break;
case Format11n:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeLiteral(writer);
break;
case Format11x:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
break;
case Format12x:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
break;
case Format20bc:
writeOpcode(writer);
writer.write(' ');
writer.write(verificationErrorName);
writer.write(", ");
writer.write(referenceString);
break;
case Format20t:
case Format30t:
writeOpcode(writer);
writer.write(' ');
writeTargetLabel(writer);
break;
case Format21c:
case Format31c:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writer.write(referenceString);
break;
case Format21ih:
case Format21lh:
case Format21s:
case Format31i:
case Format51l:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeLiteral(writer);
if (instruction.getOpcode().setsWideRegister()) {
writeCommentIfLikelyDouble(writer);
} else {
boolean isResourceId = writeCommentIfResourceId(writer);
if (!isResourceId)
writeCommentIfLikelyFloat(writer);
}
break;
case Format21t:
case Format31t:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeTargetLabel(writer);
break;
case Format22b:
case Format22s:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
writer.write(", ");
writeLiteral(writer);
break;
case Format22c:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
writer.write(", ");
writer.write(referenceString);
break;
case Format22cs:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
writer.write(", ");
writeFieldOffset(writer);
break;
case Format22t:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
writer.write(", ");
writeTargetLabel(writer);
break;
case Format22x:
case Format32x:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
break;
case Format23x:
writeOpcode(writer);
writer.write(' ');
writeFirstRegister(writer);
writer.write(", ");
writeSecondRegister(writer);
writer.write(", ");
writeThirdRegister(writer);
break;
case Format35c:
writeOpcode(writer);
writer.write(' ');
writeInvokeRegisters(writer);
writer.write(", ");
writer.write(referenceString);
break;
case Format35mi:
writeOpcode(writer);
writer.write(' ');
writeInvokeRegisters(writer);
writer.write(", ");
writeInlineIndex(writer);
break;
case Format35ms:
writeOpcode(writer);
writer.write(' ');
writeInvokeRegisters(writer);
writer.write(", ");
writeVtableIndex(writer);
break;
case Format3rc:
writeOpcode(writer);
writer.write(' ');
writeInvokeRangeRegisters(writer);
writer.write(", ");
writer.write(referenceString);
break;
case Format3rmi:
writeOpcode(writer);
writer.write(' ');
writeInvokeRangeRegisters(writer);
writer.write(", ");
writeInlineIndex(writer);
break;
case Format3rms:
writeOpcode(writer);
writer.write(' ');
writeInvokeRangeRegisters(writer);
writer.write(", ");
writeVtableIndex(writer);
break;
case Format45cc:
writeOpcode(writer);
writer.write(' ');
writeInvokeRegisters(writer);
writer.write(", ");
writer.write(referenceString);
writer.write(", ");
writer.write(referenceString2);
break;
case Format4rcc:
writeOpcode(writer);
writer.write(' ');
writeInvokeRangeRegisters(writer);
writer.write(", ");
writer.write(referenceString);
writer.write(", ");
writer.write(referenceString2);
break;
default:
assert false;
return false;
}
if (commentOutInstruction) {
writer.write("\nnop");
}
return true;
}
use of org.jf.dexlib2.iface.instruction.formats.Instruction20bc in project smali by JesusFreke.
the class DexWriter method writeCodeItem.
private int writeCodeItem(@Nonnull DexDataWriter writer, @Nonnull ByteArrayOutputStream ehBuf, @Nonnull MethodKey methodKey, @Nonnull List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks, @Nullable Iterable<? extends Instruction> instructions, int debugItemOffset) throws IOException {
if (instructions == null && debugItemOffset == NO_OFFSET) {
return -1;
}
numCodeItemItems++;
writer.align();
int codeItemOffset = writer.getPosition();
writer.writeUshort(classSection.getRegisterCount(methodKey));
boolean isStatic = AccessFlags.STATIC.isSet(classSection.getMethodAccessFlags(methodKey));
Collection<? extends TypeKey> parameters = typeListSection.getTypes(protoSection.getParameters(methodSection.getPrototype(methodKey)));
writer.writeUshort(MethodUtil.getParameterRegisterCount(parameters, isStatic));
if (instructions != null) {
tryBlocks = TryListBuilder.massageTryBlocks(tryBlocks);
int outParamCount = 0;
int codeUnitCount = 0;
for (Instruction instruction : instructions) {
codeUnitCount += instruction.getCodeUnits();
if (instruction.getOpcode().referenceType == ReferenceType.METHOD) {
ReferenceInstruction refInsn = (ReferenceInstruction) instruction;
MethodReference methodRef = (MethodReference) refInsn.getReference();
Opcode opcode = instruction.getOpcode();
int paramCount;
if (InstructionUtil.isInvokePolymorphic(opcode)) {
paramCount = ((VariableRegisterInstruction) instruction).getRegisterCount();
} else {
paramCount = MethodUtil.getParameterRegisterCount(methodRef, InstructionUtil.isInvokeStatic(opcode));
}
if (paramCount > outParamCount) {
outParamCount = paramCount;
}
}
}
writer.writeUshort(outParamCount);
writer.writeUshort(tryBlocks.size());
writer.writeInt(debugItemOffset);
InstructionWriter instructionWriter = InstructionWriter.makeInstructionWriter(opcodes, writer, stringSection, typeSection, fieldSection, methodSection, protoSection, methodHandleSection, callSiteSection);
writer.writeInt(codeUnitCount);
int codeOffset = 0;
for (Instruction instruction : instructions) {
try {
switch(instruction.getOpcode().format) {
case Format10t:
instructionWriter.write((Instruction10t) instruction);
break;
case Format10x:
instructionWriter.write((Instruction10x) instruction);
break;
case Format11n:
instructionWriter.write((Instruction11n) instruction);
break;
case Format11x:
instructionWriter.write((Instruction11x) instruction);
break;
case Format12x:
instructionWriter.write((Instruction12x) instruction);
break;
case Format20bc:
instructionWriter.write((Instruction20bc) instruction);
break;
case Format20t:
instructionWriter.write((Instruction20t) instruction);
break;
case Format21c:
instructionWriter.write((Instruction21c) instruction);
break;
case Format21ih:
instructionWriter.write((Instruction21ih) instruction);
break;
case Format21lh:
instructionWriter.write((Instruction21lh) instruction);
break;
case Format21s:
instructionWriter.write((Instruction21s) instruction);
break;
case Format21t:
instructionWriter.write((Instruction21t) instruction);
break;
case Format22b:
instructionWriter.write((Instruction22b) instruction);
break;
case Format22c:
instructionWriter.write((Instruction22c) instruction);
break;
case Format22cs:
instructionWriter.write((Instruction22cs) instruction);
break;
case Format22s:
instructionWriter.write((Instruction22s) instruction);
break;
case Format22t:
instructionWriter.write((Instruction22t) instruction);
break;
case Format22x:
instructionWriter.write((Instruction22x) instruction);
break;
case Format23x:
instructionWriter.write((Instruction23x) instruction);
break;
case Format30t:
instructionWriter.write((Instruction30t) instruction);
break;
case Format31c:
instructionWriter.write((Instruction31c) instruction);
break;
case Format31i:
instructionWriter.write((Instruction31i) instruction);
break;
case Format31t:
instructionWriter.write((Instruction31t) instruction);
break;
case Format32x:
instructionWriter.write((Instruction32x) instruction);
break;
case Format35c:
instructionWriter.write((Instruction35c) instruction);
break;
case Format35mi:
instructionWriter.write((Instruction35mi) instruction);
break;
case Format35ms:
instructionWriter.write((Instruction35ms) instruction);
break;
case Format3rc:
instructionWriter.write((Instruction3rc) instruction);
break;
case Format3rmi:
instructionWriter.write((Instruction3rmi) instruction);
break;
case Format3rms:
instructionWriter.write((Instruction3rms) instruction);
break;
case Format45cc:
instructionWriter.write((Instruction45cc) instruction);
break;
case Format4rcc:
instructionWriter.write((Instruction4rcc) instruction);
break;
case Format51l:
instructionWriter.write((Instruction51l) instruction);
break;
case ArrayPayload:
instructionWriter.write((ArrayPayload) instruction);
break;
case PackedSwitchPayload:
instructionWriter.write((PackedSwitchPayload) instruction);
break;
case SparseSwitchPayload:
instructionWriter.write((SparseSwitchPayload) instruction);
break;
default:
throw new ExceptionWithContext("Unsupported instruction format: %s", instruction.getOpcode().format);
}
} catch (RuntimeException ex) {
throw new ExceptionWithContext(ex, "Error while writing instruction at code offset 0x%x", codeOffset);
}
codeOffset += instruction.getCodeUnits();
}
if (tryBlocks.size() > 0) {
writer.align();
// filter out unique lists of exception handlers
Map<List<? extends ExceptionHandler>, Integer> exceptionHandlerOffsetMap = Maps.newHashMap();
for (TryBlock<? extends ExceptionHandler> tryBlock : tryBlocks) {
exceptionHandlerOffsetMap.put(tryBlock.getExceptionHandlers(), 0);
}
DexDataWriter.writeUleb128(ehBuf, exceptionHandlerOffsetMap.size());
for (TryBlock<? extends ExceptionHandler> tryBlock : tryBlocks) {
int startAddress = tryBlock.getStartCodeAddress();
int endAddress = startAddress + tryBlock.getCodeUnitCount();
int tbCodeUnitCount = endAddress - startAddress;
writer.writeInt(startAddress);
writer.writeUshort(tbCodeUnitCount);
if (tryBlock.getExceptionHandlers().size() == 0) {
throw new ExceptionWithContext("No exception handlers for the try block!");
}
Integer offset = exceptionHandlerOffsetMap.get(tryBlock.getExceptionHandlers());
if (offset != 0) {
// exception handler has already been written out, just use it
writer.writeUshort(offset);
} else {
// if offset has not been set yet, we are about to write out a new exception handler
offset = ehBuf.size();
writer.writeUshort(offset);
exceptionHandlerOffsetMap.put(tryBlock.getExceptionHandlers(), offset);
// check if the last exception handler is a catch-all and adjust the size accordingly
int ehSize = tryBlock.getExceptionHandlers().size();
ExceptionHandler ehLast = tryBlock.getExceptionHandlers().get(ehSize - 1);
if (ehLast.getExceptionType() == null) {
ehSize = ehSize * (-1) + 1;
}
// now let's layout the exception handlers, assuming that catch-all is always last
DexDataWriter.writeSleb128(ehBuf, ehSize);
for (ExceptionHandler eh : tryBlock.getExceptionHandlers()) {
TypeKey exceptionTypeKey = classSection.getExceptionType(eh);
int codeAddress = eh.getHandlerCodeAddress();
if (exceptionTypeKey != null) {
// regular exception handling
DexDataWriter.writeUleb128(ehBuf, typeSection.getItemIndex(exceptionTypeKey));
DexDataWriter.writeUleb128(ehBuf, codeAddress);
} else {
// catch-all
DexDataWriter.writeUleb128(ehBuf, codeAddress);
}
}
}
}
if (ehBuf.size() > 0) {
ehBuf.writeTo(writer);
ehBuf.reset();
}
}
} else {
// no instructions, all we have is the debug item offset
writer.writeUshort(0);
writer.writeUshort(0);
writer.writeInt(debugItemOffset);
writer.writeInt(0);
}
return codeItemOffset;
}
Aggregations