Skip to content

Commit

Permalink
Update to current pxb1988 version
Browse files Browse the repository at this point in the history
  • Loading branch information
ThexXTURBOXx committed Sep 1, 2023
2 parents bfa6a96 + 476e64c commit e25a0df
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 21 deletions.
27 changes: 22 additions & 5 deletions dex-translator/src/main/java/com/googlecode/d2j/dex/Dex2Asm.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@

public class Dex2Asm {

private static boolean isPowerOfTwo(int i) {
return (i & (i - 1)) == 0;
}

private static int removeHiddenAccess(int accessFlags) {
// Refer to art/libdexfile/dex/hidden_api_access_flags.h
if (!isPowerOfTwo(accessFlags & DexConstants.ACC_VISIBILITY_FLAGS)) {
accessFlags ^= DexConstants.ACC_VISIBILITY_FLAGS;
}
accessFlags &= ~((accessFlags & DexConstants.ACC_NATIVE) != 0 ?
DexConstants.ACC_DEX_HIDDEN_BIT_NATIVE : DexConstants.ACC_DEX_HIDDEN_BIT);
return accessFlags;
}

public static class ClzCtx {

public String classDescriptor;
Expand Down Expand Up @@ -347,10 +361,11 @@ private static MethodVisitor collectBasicMethodInfo(DexMethodNode methodNode, Cl
+ signature + ".");
signature = null;
}
int access = methodNode.access;
// clear ACC_DECLARED_SYNCHRONIZED, ACC_CONSTRUCTOR and ACC_SYNTHETIC from method flags
final int cleanFlag = ~((DexConstants.ACC_DECLARED_SYNCHRONIZED | DexConstants.ACC_CONSTRUCTOR
| Opcodes.ACC_SYNTHETIC));

// HiddenApiAccessFlags is valid for .dex but not for .class
int access = removeHiddenAccess(methodNode.access);
// clear ACC_DECLARED_SYNCHRONIZED and ACC_CONSTRUCTOR from method flags
final int cleanFlag = ~((DexConstants.ACC_DECLARED_SYNCHRONIZED | DexConstants.ACC_CONSTRUCTOR));
access &= cleanFlag;
return cv.visitMethod(access, methodNode.method.getName(), methodNode.method.getDesc(), signature, xthrows);
}
Expand Down Expand Up @@ -654,8 +669,10 @@ public void convertField(DexClassNode classNode, DexFieldNode fieldNode, ClassVi
signature = null;
}

// HiddenApiAccessFlags is valid for .dex but not for .class
int access = removeHiddenAccess(fieldNode.access);
final int fieldCleanFlag = ~((DexConstants.ACC_DECLARED_SYNCHRONIZED | Opcodes.ACC_SYNTHETIC));
FieldVisitor fv = cv.visitField(fieldNode.access & fieldCleanFlag, fieldNode.field.getName(),
FieldVisitor fv = cv.visitField(access & fieldCleanFlag, fieldNode.field.getName(),
fieldNode.field.getType(), signature, value);

if (fv == null) {
Expand Down
33 changes: 17 additions & 16 deletions dex-translator/src/main/java/com/googlecode/d2j/dex/ExDex2Asm.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.googlecode.d2j.DexException;
import com.googlecode.d2j.node.DexMethodNode;
import com.googlecode.dex2jar.tools.Constants;
import org.objectweb.asm.AsmBridge;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.tree.MethodNode;

Expand All @@ -16,7 +17,7 @@ public ExDex2Asm(DexExceptionHandler exceptionHandler) {

@Override
public void convertCode(DexMethodNode methodNode, MethodVisitor mv, ClzCtx clzCtx) {
// MethodVisitor mw = AsmBridge.searchMethodWriter(mv);
MethodVisitor mw = AsmBridge.searchMethodWriter(mv);
MethodNode mn = new MethodNode(Constants.ASM_VERSION, methodNode.access, methodNode.method.getName(),
methodNode.method.getDesc(), null, null);
try {
Expand All @@ -33,21 +34,21 @@ public void convertCode(DexMethodNode methodNode, MethodVisitor mv, ClzCtx clzCt
}
// code convert ok, copy to MethodWriter and check for Size
mn.accept(mv);
//if (mw != null) {
// try {
// AsmBridge.sizeOfMethodWriter(mw);
// } catch (Exception ex) {
// mn.instructions.clear();
// mn.tryCatchBlocks.clear();
// if (exceptionHandler == null) {
// new DexException(ex, "Failed to convert code for %s", methodNode.method)
// .printStackTrace();
// } else {
// exceptionHandler.handleMethodTranslateException(methodNode.method, methodNode, mn, ex);
// }
// AsmBridge.replaceMethodWriter(mw, mn);
// }
//}
if (mw != null) {
try {
AsmBridge.sizeOfMethodWriter(mw);
} catch (Exception ex) {
mn.instructions.clear();
mn.tryCatchBlocks.clear();
if (exceptionHandler == null) {
new DexException(ex, "Failed to convert code for %s", methodNode.method)
.printStackTrace();
} else {
exceptionHandler.handleMethodTranslateException(methodNode.method, methodNode, mn, ex);
}
AsmBridge.replaceMethodWriter(mw, mn);
}
}
}

}
101 changes: 101 additions & 0 deletions dex-translator/src/main/java/org/objectweb/asm/AsmBridge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* dex2jar - Tools to work with android .dex and java .class files
* Copyright (c) 2009-2014 Panxiaobo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.objectweb.asm;

import java.lang.reflect.Field;
import org.objectweb.asm.tree.MethodNode;

public final class AsmBridge {

public static MethodVisitor searchMethodWriter(MethodVisitor methodVisitor) {
while (methodVisitor != null && !(methodVisitor instanceof MethodWriter)) {
methodVisitor = methodVisitor.mv;
}
return methodVisitor;
}

public static int sizeOfMethodWriter(MethodVisitor methodVisitor) {
MethodWriter mw = (MethodWriter) methodVisitor;
return mw.computeMethodInfoSize();
}

private static void removeMethodWriter(MethodWriter methodWriter) {
MethodWriter firstMethodWriter;
MethodWriter lastMethodWriter;

try {
ClassWriter classWriter = reflectForClassWriter(methodWriter);

Field fmField = ClassWriter.class.getDeclaredField("firstMethod");
fmField.setAccessible(true);
firstMethodWriter = (MethodWriter) fmField.get(classWriter);

Field lmField = ClassWriter.class.getDeclaredField("lastMethod");
lmField.setAccessible(true);
lastMethodWriter = (MethodWriter) lmField.get(classWriter);

// mv must be the last element
if (firstMethodWriter == methodWriter) {
fmField.set(classWriter, null);
if (lastMethodWriter == methodWriter) {
lmField.set(classWriter, null);
}
} else {
while (firstMethodWriter != null) {
if (firstMethodWriter.mv == methodWriter) {
firstMethodWriter.mv = methodWriter.mv;
if (lastMethodWriter == methodWriter) {
lmField.set(classWriter, firstMethodWriter);
}
break;
} else {
firstMethodWriter = (MethodWriter) firstMethodWriter.mv;
}
}
}
} catch (IllegalAccessException | NoSuchFieldException exc) {
exc.printStackTrace();
}
}

private static ClassWriter reflectForClassWriter(MethodWriter methodWriter) throws NoSuchFieldException, IllegalAccessException {
// Get the SymbolTable for accessing ClassWriter
Field stField = MethodWriter.class.getDeclaredField("symbolTable");
stField.setAccessible(true);
SymbolTable symbolTable = (SymbolTable) stField.get(methodWriter);
// Get ClassWriter object from methodWriter's SymbolTable
return symbolTable.classWriter;
}

public static void replaceMethodWriter(MethodVisitor methodVisitor, MethodNode methodNode) {
MethodWriter methodWriter = (MethodWriter) methodVisitor;

try {
ClassWriter classWriter = reflectForClassWriter(methodWriter);

methodNode.accept(classWriter);
removeMethodWriter(methodWriter);
} catch (IllegalAccessException | NoSuchFieldException exc) {
exc.printStackTrace();
}
}

private AsmBridge() {
throw new UnsupportedOperationException();
}

}

0 comments on commit e25a0df

Please sign in to comment.