use of org.jf.dexlib2.builder.MethodImplementationBuilder in project smali by JesusFreke.
the class FixGotoTest method testFixGoto16ToGoto32.
@Test
public void testFixGoto16ToGoto32() {
MethodImplementationBuilder builder = new MethodImplementationBuilder(1);
Label gotoTarget = builder.getLabel("gotoTarget");
builder.addInstruction(new BuilderInstruction20t(Opcode.GOTO_16, gotoTarget));
for (int i = 0; i < 70000; i++) {
builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
}
builder.addLabel("gotoTarget");
builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
MethodImplementation impl = builder.getMethodImplementation();
List<? extends Instruction> instructions = Lists.newArrayList(impl.getInstructions());
Assert.assertEquals(70002, instructions.size());
Assert.assertEquals(Opcode.GOTO_32, instructions.get(0).getOpcode());
Assert.assertEquals(70003, ((OffsetInstruction) instructions.get(0)).getCodeOffset());
}
use of org.jf.dexlib2.builder.MethodImplementationBuilder 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());
}
use of org.jf.dexlib2.builder.MethodImplementationBuilder in project smali by JesusFreke.
the class CallSiteTest method testBuilderCallSite.
@Test
public void testBuilderCallSite() throws IOException {
DexBuilder dexBuilder = new DexBuilder(Opcodes.forArtVersion(111));
BuilderCallSiteReference callSite = dexBuilder.internCallSite(new ImmutableCallSiteReference("call_site_1", new ImmutableMethodHandleReference(MethodHandleType.INVOKE_STATIC, new ImmutableMethodReference("Lcls1;", "loader", ImmutableList.of("Ljava/lang/invoke/Lookup;", "Ljava/lang/String;", "Ljava/lang/invoke/MethodType;"), "Ljava/lang/invoke/CallSite;")), "someMethod", new ImmutableMethodProtoReference(ImmutableList.of(), "V"), ImmutableList.of()));
MethodImplementationBuilder methodImplementationBuilder = new MethodImplementationBuilder(10);
methodImplementationBuilder.addInstruction(new BuilderInstruction35c(Opcode.INVOKE_CUSTOM, 0, 0, 0, 0, 0, 0, callSite));
BuilderMethod method = dexBuilder.internMethod("Lcls1;", "method1", null, "V", 0, ImmutableSet.of(), ImmutableSet.of(), methodImplementationBuilder.getMethodImplementation());
dexBuilder.internClassDef("Lcls1;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, ImmutableSet.of(), null, ImmutableList.of(method));
File tempFile = File.createTempFile("dex", ".dex");
dexBuilder.writeTo(new FileDataStore(tempFile));
verifyDexFile(DexFileFactory.loadDexFile(tempFile, Opcodes.forArtVersion(111)));
}
use of org.jf.dexlib2.builder.MethodImplementationBuilder in project soot by Sable.
the class DexPrinter method toTries.
private void toTries(Collection<Trap> traps, StmtVisitor stmtV, MethodImplementationBuilder builder, LabelAssigner labelAssigner) {
// Original code: assume that the mapping startCodeAddress -> TryItem is
// enough for
// a "code range", ignore different end Units / try lengths
// That's definitely not enough since we can have two handlers H1, H2
// with
// H1:240-322, H2:242-322. There is no valid ordering for such
// overlapping traps
// in dex. Current solution: If there is already a trap T' for a
// subrange of the
// current trap T, merge T and T' on the fully range of T. This is not a
// 100%
// correct since we extend traps over the requested range, but it's
// better than
// the previous code that produced APKs which failed Dalvik's bytecode
// verification.
// (Steven Arzt, 09.08.2013)
// There are cases in which we need to split traps, e.g. in cases like
// ( (t1) ... (t2) )<big catch all around it> where the all three
// handlers do
// something different. That's why we run the TrapSplitter before we get
// here.
// (Steven Arzt, 25.09.2013)
Map<CodeRange, List<ExceptionHandler>> codeRangesToTryItem = new LinkedHashMap<CodeRange, List<ExceptionHandler>>();
for (Trap t : traps) {
// see if there is old handler info at this code range
Stmt beginStmt = (Stmt) t.getBeginUnit();
Stmt endStmt = (Stmt) t.getEndUnit();
int startCodeAddress = labelAssigner.getLabel(beginStmt).getCodeAddress();
int endCodeAddress = labelAssigner.getLabel(endStmt).getCodeAddress();
CodeRange range = new CodeRange(startCodeAddress, endCodeAddress);
String exceptionType = SootToDexUtils.getDexTypeDescriptor(t.getException().getType());
int codeAddress = labelAssigner.getLabel((Stmt) t.getHandlerUnit()).getCodeAddress();
ImmutableExceptionHandler exceptionHandler = new ImmutableExceptionHandler(exceptionType, codeAddress);
List<ExceptionHandler> newHandlers = new ArrayList<ExceptionHandler>();
for (CodeRange r : codeRangesToTryItem.keySet()) {
// trap over the bigger range containing this range
if (r.containsRange(range)) {
range.startAddress = r.startAddress;
range.endAddress = r.endAddress;
// copy the old handlers to a bigger array (the old one
// cannot be modified...)
List<ExceptionHandler> oldHandlers = codeRangesToTryItem.get(r);
if (oldHandlers != null)
newHandlers.addAll(oldHandlers);
break;
} else // range.
if (range.containsRange(r)) {
range.startAddress = r.startAddress;
range.endAddress = r.endAddress;
// just use the newly found handler info
List<ExceptionHandler> oldHandlers = codeRangesToTryItem.get(range);
if (oldHandlers != null)
newHandlers.addAll(oldHandlers);
// remove the old range, the new one will be added anyway
// and contain
// the merged handlers
codeRangesToTryItem.remove(r);
break;
}
}
if (!newHandlers.contains(exceptionHandler))
newHandlers.add(exceptionHandler);
codeRangesToTryItem.put(range, newHandlers);
}
// Check for overlaps
for (CodeRange r1 : codeRangesToTryItem.keySet()) {
for (CodeRange r2 : codeRangesToTryItem.keySet()) {
if (r1 != r2 && r1.overlaps(r2))
LOGGER.warn("Trap region overlaps detected");
}
}
for (CodeRange range : codeRangesToTryItem.keySet()) {
boolean allCaughtForRange = false;
for (ExceptionHandler handler : codeRangesToTryItem.get(range)) {
// anyway.
if (allCaughtForRange)
continue;
// therefore hack it using java.lang.Throwable.
if ("Ljava/lang/Throwable;".equals(handler.getExceptionType())) {
/*
* builder.addCatch(labelAssigner.getLabelAtAddress(range. startAddress),
* labelAssigner.getLabelAtAddress(range.endAddress),
* labelAssigner.getLabelAtAddress(handler. getHandlerCodeAddress()));
*/
allCaughtForRange = true;
}
// else
builder.addCatch(new ImmutableTypeReference(handler.getExceptionType()), labelAssigner.getLabelAtAddress(range.startAddress), labelAssigner.getLabelAtAddress(range.endAddress), labelAssigner.getLabelAtAddress(handler.getHandlerCodeAddress()));
}
}
}
use of org.jf.dexlib2.builder.MethodImplementationBuilder 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, 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