use of org.jf.dexlib2.immutable.ImmutableDexFile in project smali by JesusFreke.
the class MethodAnalyzerTest method testInstanceOfNarrowingEqz_dalvik.
@Test
public void testInstanceOfNarrowingEqz_dalvik() throws IOException {
MethodImplementationBuilder builder = new MethodImplementationBuilder(2);
builder.addInstruction(new BuilderInstruction22c(Opcode.INSTANCE_OF, 0, 1, new ImmutableTypeReference("Lmain;")));
builder.addInstruction(new BuilderInstruction21t(Opcode.IF_EQZ, 0, builder.getLabel("not_instance_of")));
builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
builder.addLabel("not_instance_of");
builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
MethodImplementation methodImplementation = builder.getMethodImplementation();
Method method = new ImmutableMethod("Lmain;", "narrowing", Collections.singletonList(new ImmutableMethodParameter("Ljava/lang/Object;", null, null)), "V", AccessFlags.PUBLIC.getValue(), null, methodImplementation);
ClassDef classDef = new ImmutableClassDef("Lmain;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, Collections.singletonList(method));
DexFile dexFile = new ImmutableDexFile(Opcodes.forApi(19), Collections.singletonList(classDef));
ClassPath classPath = new ClassPath(new DexClassProvider(dexFile));
MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, null, false);
List<AnalyzedInstruction> analyzedInstructions = methodAnalyzer.getAnalyzedInstructions();
Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(2).getPreInstructionRegisterType(1).type.getType());
Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(3).getPreInstructionRegisterType(1).type.getType());
}
use of org.jf.dexlib2.immutable.ImmutableDexFile in project smali by JesusFreke.
the class MethodAnalyzerTest method testInstanceOfNarrowingEqz_art.
@Test
public void testInstanceOfNarrowingEqz_art() throws IOException {
MethodImplementationBuilder builder = new MethodImplementationBuilder(2);
builder.addInstruction(new BuilderInstruction22c(Opcode.INSTANCE_OF, 0, 1, new ImmutableTypeReference("Lmain;")));
builder.addInstruction(new BuilderInstruction21t(Opcode.IF_EQZ, 0, builder.getLabel("not_instance_of")));
builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
builder.addLabel("not_instance_of");
builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
MethodImplementation methodImplementation = builder.getMethodImplementation();
Method method = new ImmutableMethod("Lmain;", "narrowing", Collections.singletonList(new ImmutableMethodParameter("Ljava/lang/Object;", null, null)), "V", AccessFlags.PUBLIC.getValue(), null, methodImplementation);
ClassDef classDef = new ImmutableClassDef("Lmain;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, Collections.singletonList(method));
DexFile dexFile = new ImmutableDexFile(forArtVersion(56), Collections.singletonList(classDef));
ClassPath classPath = new ClassPath(Lists.newArrayList(new DexClassProvider(dexFile)), true, 56);
MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, null, false);
List<AnalyzedInstruction> analyzedInstructions = methodAnalyzer.getAnalyzedInstructions();
Assert.assertEquals("Lmain;", analyzedInstructions.get(2).getPreInstructionRegisterType(1).type.getType());
Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(3).getPreInstructionRegisterType(1).type.getType());
}
use of org.jf.dexlib2.immutable.ImmutableDexFile in project atlas by alibaba.
the class FastPatchTool method doPatch.
public void doPatch() throws IOException, PatchException, RecognitionException {
MappingReader mappingReader = null;
MappingProcessor mappingProcessor = null;
if (outDir == null || !outDir.exists()) {
return;
}
outPatchFile = new File(outDir, "apatch-unsigned.apatch");
if (mappingFile != null && mappingFile.exists()) {
mappingReader = new MappingReader(mappingFile);
mappingProcessor = new MappingProcessorImpl(superClassMap);
mappingReader.pump(mappingProcessor);
mappingProcessor.updateMethod();
mappingProcessor.updateFieldType();
}
for (FastPatchObject fastPatchObject : patchObjects) {
Set<ClassDef> classes = new HashSet<ClassDef>();
Map<ClassDef, List<Method>> patchClassDefs = new HashMap<ClassDef, List<Method>>();
Set<ClassDef> addedClasses = new HashSet<ClassDef>();
Map<ClassDef, List<Method>> newClassDef = new HashMap<ClassDef, List<Method>>();
ArrayList<Method> methods = new ArrayList<Method>();
for (File dexFile : fastPatchObject.DexFiles) {
DexFile dFile = DexFileFactory.loadDexFile(dexFile.getAbsolutePath(), 19, true);
classes.addAll(dFile.getClasses());
}
final Set<ClassDef> newClasses = new HashSet<ClassDef>();
for (ClassDef classDef : classes) {
String type = classDef.getType();
if (fastPatchObject.addedClass.contains(SmaliUtils.getDalvikClassName(type))) {
System.out.println("patch added class:" + type);
addedClasses.add(classDef);
continue;
}
for (Map.Entry<String, List<String>> entry : fastPatchObject.modifyClasses.entrySet()) {
if (entry.getKey().equals(SmaliUtils.getDalvikClassName(type))) {
ArrayList<Method> newMethods = new ArrayList<Method>();
for (Method method : classDef.getMethods()) {
System.err.println(getMethodFullName(method));
if (entry.getValue().contains(getMethodFullName(method))) {
newMethods.add(method);
}
}
patchClassDefs.put(classDef, newMethods);
break;
}
}
}
if (patchClassDefs.size() == 0 && addedClasses.size() == 0) {
continue;
}
if (mappingFile != null && mappingFile.exists()) {
//第一步先将prepareclass混淆掉
for (String className : fastPatchObject.prepareClasses) {
ApkPatch.prepareClasses.add(mappingProcessor.getNewClassName(className).className);
}
//将replaceanatation混淆掉
MethodReplaceAnnotation.ANNOTATION = DefineUtils.getDefineClassName(mappingProcessor.getNewClassName(DefineUtils.getDalvikClassName("Lcom/alipay/euler/andfix/annotation/MethodReplace;")).className, false);
//将dex代码混淆掉
InsTructionsReIClassDef insTructionsReDef = new InsTructionsReIClassDef(new MappingClassProcessor(mappingProcessor));
for (ClassDef c : classes) {
if (patchClassDefs.containsKey(c)) {
for (Method method : c.getMethods()) {
if (patchClassDefs.get(c).contains(method)) {
methods.add(insTructionsReDef.reMethod(method));
}
}
newClassDef.put(insTructionsReDef.reClassDef(c), methods);
} else if (addedClasses.contains(c)) {
newClassDef.put(insTructionsReDef.reClassDef(c), new ArrayList<Method>());
}
if (c.getType().contains("/R$")) {
continue;
}
newClasses.add(insTructionsReDef.reClassDef(c));
}
} else {
ApkPatch.prepareClasses.addAll(fastPatchObject.prepareClasses);
}
File patchDexFile = new File(outDir, "patch.dex");
for (ClassDef classDef : newClassDef.keySet()) {
System.out.println("modify class:" + classDef.getType());
}
if (newClassDef.size() > 0) {
DexFileFactory.writeDexFile(patchDexFile.getAbsolutePath(), new ImmutableDexFile(newClassDef.keySet()));
} else if (patchClassDefs.size() > 0) {
DexFileFactory.writeDexFile(patchDexFile.getAbsolutePath(), new ImmutableDexFile(patchClassDefs.keySet()));
}
File tempDexFile = new File(outDir, "temp.dex");
if (newClasses.size() > 0) {
DexFileFactory.writeDexFile(tempDexFile.getAbsolutePath(), new DexFile() {
@Nonnull
@Override
public Set<? extends ClassDef> getClasses() {
return new AbstractSet<ClassDef>() {
@Nonnull
@Override
public Iterator<ClassDef> iterator() {
return newClasses.iterator();
}
@Override
public int size() {
return newClasses.size();
}
};
}
});
} else if (classes.size() > 0) {
DexFileFactory.writeDexFile(tempDexFile.getAbsolutePath(), new ImmutableDexFile(classes));
}
SmaliDiffUtils.scanClasses(new File(outDir, "smali2"), Lists.newArrayList(tempDexFile));
DexFile patchDex = DexFileFactory.loadDexFile(patchDexFile.getAbsolutePath(), 19, true);
DexFile tempDex = DexFileFactory.loadDexFile(tempDexFile.getAbsolutePath(), 19, true);
Set<? extends ClassDef> patchClasses = patchDex.getClasses();
DexDiffInfo dexDiffInfo = new DexDiffInfo();
for (ClassDef patchClassDef : patchClasses) {
String type = patchClassDef.getType();
if (fastPatchObject.addedClass.contains(SmaliUtils.getDalvikClassName(type))) {
dexDiffInfo.getAddedClasses().add((DexBackedClassDef) patchClassDef);
dexDiffInfo.addManifestAddClass(type);
continue;
}
for (Method method : patchClassDef.getMethods()) {
List<? extends CharSequence> parameters = method.getParameterTypes();
if (methods.size() > 0) {
for (Method modifyMethod : methods) {
List<? extends CharSequence> modifyParameters = modifyMethod.getParameterTypes();
if (parameters.size() != modifyParameters.size() || !isEqualObj(parameters, modifyParameters)) {
continue;
}
if (modifyMethod.getName().equals(method.getName()))
dexDiffInfo.addModifiedMethods((DexBackedMethod) method);
}
} else if (patchClassDefs.size() > 0) {
for (ClassDef classDef : patchClassDefs.keySet()) {
if (classDef.getType().equals(patchClassDef.getType())) {
List<Method> methodList = patchClassDefs.get(classDef);
for (Method method1 : methodList) {
if (method1.getName().equals(method.getName())) {
dexDiffInfo.addModifiedMethods((DexBackedMethod) method);
}
}
}
}
}
}
}
FastBuild fastBuild = new FastBuild(fastPatchObject.bundleName, new File(outDir, bundleName));
fastBuild.setClasses(tempDex.getClasses());
fastBuild.setDiffInfo(dexDiffInfo);
new AndFixFilterImpl(dexDiffInfo).filterDex();
dexDiffInfo.update();
APatchTool.isApatch = true;
File adiffFile = new File(outDir, "apatch-diff.txt");
File adiffJsonFile = new File(outDir, "apatch-diff.json");
dexDiffInfo.writeToFile(fastPatchObject.bundleName, adiffFile, adiffJsonFile);
try {
File patchJarFile = fastBuild.dopatch();
patchJarFiles.add(patchJarFile);
} catch (CertificateException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (UnrecoverableEntryException e) {
e.printStackTrace();
}
}
File[] aPatchFiles = new File[patchJarFiles.size()];
aPatchFiles = patchJarFiles.toArray(aPatchFiles);
File mergePatchFile = null;
if (null != aPatchFiles && aPatchFiles.length > 1) {
MergePatch mergePatch = new MergePatch(aPatchFiles, "com_taobao_android", outDir);
mergePatchFile = mergePatch.doMerge();
} else if (null != aPatchFiles && aPatchFiles.length == 1) {
mergePatchFile = aPatchFiles[0];
}
if (null != mergePatchFile && mergePatchFile.exists()) {
FileUtils.moveFile(mergePatchFile, outPatchFile);
}
}
use of org.jf.dexlib2.immutable.ImmutableDexFile in project smali by JesusFreke.
the class CustomMethodInlineTableTest method testCustomMethodInlineTable_Virtual.
@Test
public void testCustomMethodInlineTable_Virtual() throws IOException {
List<ImmutableInstruction> instructions = Lists.newArrayList(new ImmutableInstruction35mi(Opcode.EXECUTE_INLINE, 1, 0, 0, 0, 0, 0, 0), new ImmutableInstruction10x(Opcode.RETURN_VOID));
ImmutableMethodImplementation methodImpl = new ImmutableMethodImplementation(1, instructions, null, null);
ImmutableMethod method = new ImmutableMethod("Lblah;", "blah", null, "V", AccessFlags.PUBLIC.getValue(), null, methodImpl);
ClassDef classDef = new ImmutableClassDef("Lblah;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, null, null, ImmutableList.of(method));
DexFile dexFile = new ImmutableDexFile(Opcodes.getDefault(), ImmutableList.of(classDef));
ClassPathResolver resolver = new ClassPathResolver(ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), dexFile);
ClassPath classPath = new ClassPath(resolver.getResolvedClassProviders(), false, ClassPath.NOT_ART);
InlineMethodResolver inlineMethodResolver = new CustomInlineMethodResolver(classPath, "Lblah;->blah()V");
MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, inlineMethodResolver, false);
Instruction deodexedInstruction = methodAnalyzer.getInstructions().get(0);
Assert.assertEquals(Opcode.INVOKE_VIRTUAL, deodexedInstruction.getOpcode());
MethodReference methodReference = (MethodReference) ((Instruction35c) deodexedInstruction).getReference();
Assert.assertEquals(method, methodReference);
}
use of org.jf.dexlib2.immutable.ImmutableDexFile in project smali by JesusFreke.
the class MethodAnalyzerTest method testInstanceOfNarrowingAfterMove_art.
@Test
public void testInstanceOfNarrowingAfterMove_art() throws IOException {
MethodImplementationBuilder builder = new MethodImplementationBuilder(3);
builder.addInstruction(new BuilderInstruction12x(Opcode.MOVE_OBJECT, 1, 2));
builder.addInstruction(new BuilderInstruction22c(Opcode.INSTANCE_OF, 0, 1, new ImmutableTypeReference("Lmain;")));
builder.addInstruction(new BuilderInstruction21t(Opcode.IF_EQZ, 0, builder.getLabel("not_instance_of")));
builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
builder.addLabel("not_instance_of");
builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
MethodImplementation methodImplementation = builder.getMethodImplementation();
Method method = new ImmutableMethod("Lmain;", "narrowing", Collections.singletonList(new ImmutableMethodParameter("Ljava/lang/Object;", null, null)), "V", AccessFlags.PUBLIC.getValue(), null, methodImplementation);
ClassDef classDef = new ImmutableClassDef("Lmain;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, Collections.singletonList(method));
DexFile dexFile = new ImmutableDexFile(forArtVersion(56), Collections.singletonList(classDef));
ClassPath classPath = new ClassPath(Lists.newArrayList(new DexClassProvider(dexFile)), true, 56);
MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, null, false);
List<AnalyzedInstruction> analyzedInstructions = methodAnalyzer.getAnalyzedInstructions();
Assert.assertEquals("Lmain;", analyzedInstructions.get(3).getPreInstructionRegisterType(1).type.getType());
Assert.assertEquals("Lmain;", analyzedInstructions.get(3).getPreInstructionRegisterType(2).type.getType());
Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(4).getPreInstructionRegisterType(1).type.getType());
Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(4).getPreInstructionRegisterType(2).type.getType());
}
Aggregations