Skip to content

Commit

Permalink
move removeHiddenAccess to Dex2Asm
Browse files Browse the repository at this point in the history
  • Loading branch information
pxb1988 committed Sep 1, 2023
1 parent 3ec7e74 commit 7d89bb0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -981,24 +981,10 @@ private String getType(int id) {
return getString(typeIdIn.getInt(id * 4));
}

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;
}

private int acceptField(ByteBuffer in, int lastIndex, DexClassVisitor dcv,
Map<Integer, Integer> fieldAnnotationPositions, Object value, int config) {
int diff = readULeb128i(in);
int field_access_flags = removeHiddenAccess(readULeb128i(in));
int field_access_flags = readULeb128i(in);
int field_id = lastIndex + diff;
Field field = getField(field_id);
// //////////////////////////////////////////////////////////////
Expand All @@ -1024,7 +1010,7 @@ private int acceptMethod(ByteBuffer in, int lastIndex, DexClassVisitor cv, Map<I
Map<Integer, Integer> parameterAnnos, int config, boolean firstMethod) {
int offset = in.position();
int diff = readULeb128i(in);
int method_access_flags = removeHiddenAccess(readULeb128i(in));
int method_access_flags = readULeb128i(in);
int code_off = readULeb128i(in);
int method_id = lastIndex + diff;
Method method = getMethod(method_id);
Expand Down
25 changes: 21 additions & 4 deletions dex-translator/src/main/java/com/googlecode/d2j/dex/Dex2Asm.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@
import java.util.Stack;

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;
public String hexDecodeMethodNamePrefix;
Expand Down Expand Up @@ -319,7 +333,9 @@ private static MethodVisitor collectBasicMethodInfo(DexMethodNode methodNode, Cl
+ " by changing its signature to null");
signature = null;
}
int access = methodNode.access;

// 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;
Expand Down Expand Up @@ -615,10 +631,11 @@ 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(),
fieldNode.field.getType(), signature, value);
FieldVisitor fv = cv.visitField(access & fieldCleanFlag, fieldNode.field.getName(),
fieldNode.field.getType(), signature, value);

if (fv == null) {
return;
Expand Down

0 comments on commit 7d89bb0

Please sign in to comment.