Skip to content

Commit

Permalink
ddd
Browse files Browse the repository at this point in the history
  • Loading branch information
pxb1988 committed Aug 30, 2023
1 parent 7cbbf17 commit d34a4e8
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ sAnnotationValue
:sSubannotation
|sBaseValue
|sArrayValue
| ( '.iget' | '.iput' | '.sget' | '.sput' ) FIELD_FULL
| ( '.iget' | '.iput' | '.sget' | 'static-get@' | '.sput' | 'static-put@' ) FIELD_FULL
| ( '.invoke-instance' | '.invoke-static' ) METHOD_FULL
;// field,method,array,subannotation
sBaseValue
Expand Down Expand Up @@ -264,11 +264,15 @@ f1x : op=('move-result'|'move-result-wide'|'move-result-object'
| THROW
| 'monitor-enter' | 'monitor-exit' ) r1=REGISTER
;
method_handler: 'static-get' '@' METHOD_FULL
;
fconst
: op=('const/4'|'const/16'|CONST|'const/high16'|'const-wide/16'|'const-wide/32'|'const-wide/high16'|'const-wide')
r1=REGISTER ',' cst=(INT|LONG)
| op=('const-string'|'const-string/jumbo') r1=REGISTER ',' cst=STRING
| op=('const-class'|'check-cast'|'new-instance') r1=REGISTER ',' cst=(OBJECT_TYPE|ARRAY_TYPE)
| op='const-method-handle' r1=REGISTER ',' method_handler
| op='const-method-type' r1=REGISTER ',' METHOD_PROTO
;
ff1c : op=(SGET
|'sget-wide'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,13 @@ static String escapeValue(Object obj) {
private static String escapeMethodHandle(MethodHandle obj) {
switch (obj.getType()) {
case MethodHandle.INSTANCE_GET:
return ".iget " + escapeField(obj.getField());
return "instance-get@" + escapeField(obj.getField());
case MethodHandle.INSTANCE_PUT:
return ".iput " + escapeField(obj.getField());
return "instance-put@" + escapeField(obj.getField());
case MethodHandle.STATIC_GET:
return ".sget " + escapeField(obj.getField());
return "static-get@" + escapeField(obj.getField());
case MethodHandle.STATIC_PUT:
return ".sput " + escapeField(obj.getField());
return "static-put@" + escapeField(obj.getField());

case MethodHandle.INVOKE_INSTANCE:
return ".invoke-instance " + escapeMethod(obj.getMethod());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ public static Constant nShort(short i) {
public static Constant nString(String i) {
return new Constant(i);
}

public static Constant nMethodHandle(MethodHandle i) {
return new Constant(i);
}
public static Constant nProto(Proto i) {
return new Constant(i);
}
public static BinopExpr nAdd(Value a, Value b, String type) {
return new BinopExpr(VT.ADD, a, b, type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public void execute(DexStmtNode insn, DvmInterpreter<V> interpreter) {
case CONST_STRING:
case CONST_STRING_JUMBO:
case CONST_CLASS:
case CONST_METHOD_HANDLE:
case CONST_METHOD_TYPE:
setReg(((ConstStmtNode) insn).a, interpreter.newOperation(insn));
setTmp(null);
break;
Expand Down Expand Up @@ -378,7 +380,7 @@ public void execute(DexStmtNode insn, DvmInterpreter<V> interpreter) {
setTmp(null);
break;
default:
throw new RuntimeException();
throw new RuntimeException("not support " + insn.op);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.googlecode.d2j.reader.test;

import com.googlecode.d2j.node.DexFileNode;
import com.googlecode.d2j.reader.DexFileReader;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

public class Dex039Test {
@Test
public void test() throws IOException {
DexFileNode dv = new DexFileNode();
try (InputStream is = Dex039Test.class.getClassLoader().getResourceAsStream("dex039.dex")) {
DexFileReader r = new DexFileReader(is);
r.accept(dv);
}
System.out.println(dv);
}
}
Binary file added dex-reader/src/test/resources/dex039.dex
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.googlecode.d2j.DexType;
import com.googlecode.d2j.Field;
import com.googlecode.d2j.Method;
import com.googlecode.d2j.MethodHandle;
import com.googlecode.d2j.Proto;
import com.googlecode.d2j.node.DexCodeNode;
import com.googlecode.d2j.node.TryCatchNode;
import com.googlecode.d2j.node.analysis.DvmFrame;
Expand Down Expand Up @@ -560,6 +562,10 @@ public DvmValue newOperation(DexStmtNode insn) {
case CONST_STRING:
case CONST_STRING_JUMBO:
return b(nString((String) ((ConstStmtNode) insn).value));
case CONST_METHOD_HANDLE:
return b(nMethodHandle((MethodHandle) ((ConstStmtNode) insn).value));
case CONST_METHOD_TYPE:
return b(nProto((Proto) ((ConstStmtNode) insn).value));
case SGET:
case SGET_BOOLEAN:
case SGET_BYTE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.googlecode.d2j.DexType;
import com.googlecode.d2j.Method;
import com.googlecode.d2j.MethodHandle;
import com.googlecode.d2j.Proto;
import com.googlecode.d2j.asm.LdcOptimizeAdapter;
import com.googlecode.d2j.dex.Dex2Asm;
Expand Down Expand Up @@ -679,8 +680,8 @@ private void accept(Value value, MethodVisitor asm) {
Constant cst = (Constant) value;
if (cst.value.equals(Constant.Null)) {
asm.visitInsn(ACONST_NULL);
} else if (cst.value instanceof DexType) {
asm.visitLdcInsn(Type.getType(((DexType) cst.value).desc));
} else if (cst.value instanceof DexType || cst.value instanceof MethodHandle || cst.value instanceof Proto) {
asm.visitLdcInsn(Dex2Asm.convertConstantValue(cst.value));
} else {
asm.visitLdcInsn(cst.value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public void init(final Class<?> testClass) throws InitializationError {
List<Runner> runners = new ArrayList<>(files.size());

for (final Path f : files) {
if(!f.getFileName().toString().equals("dex039.dex")) {
continue;
}
final DexFileNode fileNode = readDex(f);
runners.add(new ParentRunner<DexClassNode>(testClass) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public void visitFillArrayDataStmt(Op op, int ra, Object value) {
@Override
public void visitConstStmt(Op op, int ra, Object value) {
switch (op.format) {
case kFmt21c:// value is field,type,string
case kFmt21c:// value is field,type,string,method_handle,proto
case kFmt31c:// value is string,
value = cp.wrapEncodedItem(value);
ops.add(new CodeWriter.IndexedInsn(op, ra, 0, (BaseItem) value));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.googlecode.d2j.dex.writer.item;

import com.googlecode.d2j.dex.writer.io.DataOut;

public class CallSiteItem extends BaseItem implements Comparable<CallSiteItem> {
@Override
public void write(DataOut out) {

}

@Override
public int place(int offset) {
return 0;
}

@Override
public int compareTo(CallSiteItem o) {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.googlecode.d2j.DexType;
import com.googlecode.d2j.Field;
import com.googlecode.d2j.Method;
import com.googlecode.d2j.MethodHandle;
import com.googlecode.d2j.Proto;
import com.googlecode.d2j.dex.writer.DexWriteException;

import java.util.*;
Expand All @@ -41,6 +43,9 @@ public class ConstPool {
public Map<String, TypeIdItem> types = new TreeMap<>();
public Map<TypeIdItem, ClassDefItem> classDefs = new HashMap<>();

public Map<MethodHandleItem, MethodHandleItem> method_handles = new TreeMap<>();
public Map<CallSiteItem, CallSiteItem> call_sites = new TreeMap<>();

public Object wrapEncodedItem(Object value) {
if (value instanceof DexType) {
value = uniqType(((DexType) value).desc);
Expand All @@ -50,10 +55,18 @@ public Object wrapEncodedItem(Object value) {
value = uniqString((String) value);
} else if (value instanceof Method) {
value = uniqMethod((Method) value);
} else if (value instanceof MethodHandle) {
value = uniqMethodHandle((MethodHandle) value);
} else if (value instanceof Proto) {
value = uniqProto((Proto) value);
}
return value;
}

private MethodHandleItem uniqMethodHandle(MethodHandle value) {
return null;
}

public void clean() {
encodedArrayItems.clear();
annotationSetRefListItems.clear();
Expand Down Expand Up @@ -223,7 +236,11 @@ public MethodIdItem uniqMethod(MethodIdItem key) {
}

private ProtoIdItem uniqProto(Method method) {
return uniqProto(method.getParameterTypes(), method.getReturnType());
return uniqProto(method.getProto());
}

private ProtoIdItem uniqProto(Proto proto) {
return uniqProto(proto.getParameterTypes(), proto.getReturnType());
}

public ProtoIdItem uniqProto(String[] types, String retDesc) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.googlecode.d2j.dex.writer.item;

import com.googlecode.d2j.dex.writer.io.DataOut;

public class MethodHandleItem extends BaseItem implements Comparable<MethodHandleItem>{
@Override
public void write(DataOut out) {

}

@Override
public int place(int offset) {
return 0;
}

@Override
public int compareTo(MethodHandleItem o) {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public enum SectionType {
TYPE_FIELD_ID_ITEM(0x0004, 4, 0), //
TYPE_METHOD_ID_ITEM(0x0005, 1, 0), //
TYPE_CLASS_DEF_ITEM(0x0006, 4, 0), //
TYPE_CALL_SITE_ID_ITEM(0x0007, 4, 0), //
TYPE_METHOD_HANDLE_ITEM(0x0008, 4, 0), //
TYPE_MAP_LIST(0x1000, 4, 0), //
TYPE_TYPE_LIST(0x1001, 4, 0), //
TYPE_ANNOTATION_SET_REF_LIST(0x1002, 4, 0), //
Expand Down

0 comments on commit d34a4e8

Please sign in to comment.