use of org.jf.dexlib2.iface.reference.StringReference in project soot by Sable.
the class ConstStringInstruction method jimplify.
@Override
public void jimplify(DexBody body) {
int dest = ((OneRegisterInstruction) instruction).getRegisterA();
String s;
if (instruction instanceof Instruction21c) {
Instruction21c i = (Instruction21c) instruction;
s = ((StringReference) (i.getReference())).getString();
} else if (instruction instanceof Instruction31c) {
Instruction31c i = (Instruction31c) instruction;
s = ((StringReference) (i.getReference())).getString();
} else
throw new IllegalArgumentException("Expected Instruction21c or Instruction31c but got neither.");
StringConstant sc = StringConstant.v(s);
AssignStmt assign = Jimple.v().newAssignStmt(body.getRegisterLocal(dest), sc);
setUnit(assign);
addTags(assign);
body.add(assign);
if (IDalvikTyper.ENABLE_DVKTYPER) {
DalvikTyper.v().setType(assign.getLeftOpBox(), sc.getType(), false);
}
}
use of org.jf.dexlib2.iface.reference.StringReference in project soot by Sable.
the class DexPrinter method addRegisterAssignmentDebugInfo.
private void addRegisterAssignmentDebugInfo(LocalRegisterAssignmentInformation registerAssignment, Map<Local, Integer> seenRegisters, MethodImplementationBuilder builder) {
Local local = registerAssignment.getLocal();
String dexLocalType = SootToDexUtils.getDexTypeDescriptor(local.getType());
StringReference localName = new ImmutableStringReference(local.getName());
Register reg = registerAssignment.getRegister();
int register = reg.getNumber();
Integer beforeRegister = seenRegisters.get(local);
if (beforeRegister != null) {
if (beforeRegister == register)
// No change
return;
builder.addEndLocal(beforeRegister);
}
builder.addStartLocal(register, localName, new ImmutableTypeReference(dexLocalType), new ImmutableStringReference(""));
seenRegisters.put(local, register);
}
use of org.jf.dexlib2.iface.reference.StringReference in project soot by Sable.
the class ConstantVisitor method caseStringConstant.
public void caseStringConstant(StringConstant s) {
StringReference ref = new ImmutableStringReference(s.value);
stmtV.addInsn(new Insn21c(Opcode.CONST_STRING, destinationReg, ref), origStmt);
}
use of org.jf.dexlib2.iface.reference.StringReference 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.reference.StringReference in project smali by JesusFreke.
the class JumboStringConversionTest method testJumboStringConversion.
@Test
public void testJumboStringConversion() throws IOException {
DexBuilder dexBuilder = new DexBuilder(Opcodes.getDefault());
MethodImplementationBuilder methodBuilder = new MethodImplementationBuilder(1);
for (int i = 0; i < 66000; i++) {
methodBuilder.addInstruction(new BuilderInstruction21c(Opcode.CONST_STRING, 0, dexBuilder.internStringReference(String.format("%08d", i))));
}
methodBuilder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
dexBuilder.internClassDef("Ltest;", 0, "Ljava/lang/Object;", null, null, ImmutableSet.<Annotation>of(), null, ImmutableList.of(dexBuilder.internMethod("Ltest;", "test", null, "V", 0, ImmutableSet.<Annotation>of(), ImmutableSet.of(), methodBuilder.getMethodImplementation())));
MemoryDataStore dexStore = new MemoryDataStore();
dexBuilder.writeTo(dexStore);
DexBackedDexFile dexFile = new DexBackedDexFile(Opcodes.getDefault(), dexStore.getBuffer());
ClassDef classDef = Iterables.getFirst(dexFile.getClasses(), null);
Assert.assertNotNull(classDef);
Method method = Iterables.getFirst(classDef.getMethods(), null);
Assert.assertNotNull(method);
MethodImplementation impl = method.getImplementation();
Assert.assertNotNull(impl);
List<? extends Instruction> instructions = Lists.newArrayList(impl.getInstructions());
Assert.assertEquals(66001, instructions.size());
for (int i = 0; i < 65536; i++) {
Assert.assertEquals(Opcode.CONST_STRING, instructions.get(i).getOpcode());
Assert.assertEquals(String.format("%08d", i), ((StringReference) ((ReferenceInstruction) instructions.get(i)).getReference()).getString());
}
for (int i = 65536; i < 66000; i++) {
Assert.assertEquals(Opcode.CONST_STRING_JUMBO, instructions.get(i).getOpcode());
Assert.assertEquals(String.format("%08d", i), ((StringReference) ((ReferenceInstruction) instructions.get(i)).getReference()).getString());
}
Assert.assertEquals(Opcode.RETURN_VOID, instructions.get(66000).getOpcode());
}
Aggregations