Skip to content

Commit

Permalink
Properly handle offsets for Long and Double
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-raca committed Jun 24, 2020
1 parent c7d6f51 commit 6cb158e
Showing 1 changed file with 65 additions and 15 deletions.
80 changes: 65 additions & 15 deletions dex-translator/src/main/java/com/googlecode/d2j/dex/Dex2Asm.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,60 @@
package com.googlecode.d2j.dex;

import java.util.*;

import com.googlecode.d2j.DexConstants;
import com.googlecode.d2j.DexLabel;
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.Visibility;
import com.googlecode.d2j.converter.Dex2IRConverter;
import org.objectweb.asm.*;
import org.objectweb.asm.tree.InnerClassNode;

import com.googlecode.d2j.*;
import com.googlecode.d2j.converter.IR2JConverter;
import com.googlecode.d2j.node.*;
import com.googlecode.d2j.node.DexAnnotationNode;
import com.googlecode.d2j.node.DexClassNode;
import com.googlecode.d2j.node.DexDebugNode;
import com.googlecode.d2j.node.DexFieldNode;
import com.googlecode.d2j.node.DexFileNode;
import com.googlecode.d2j.node.DexMethodNode;
import com.googlecode.dex2jar.ir.IrMethod;
import com.googlecode.dex2jar.ir.ts.*;
import com.googlecode.dex2jar.ir.ts.AggTransformer;
import com.googlecode.dex2jar.ir.ts.CleanLabel;
import com.googlecode.dex2jar.ir.ts.DeadCodeTransformer;
import com.googlecode.dex2jar.ir.ts.EndRemover;
import com.googlecode.dex2jar.ir.ts.ExceptionHandlerTrim;
import com.googlecode.dex2jar.ir.ts.Ir2JRegAssignTransformer;
import com.googlecode.dex2jar.ir.ts.MultiArrayTransformer;
import com.googlecode.dex2jar.ir.ts.NewTransformer;
import com.googlecode.dex2jar.ir.ts.NpeTransformer;
import com.googlecode.dex2jar.ir.ts.RemoveConstantFromSSA;
import com.googlecode.dex2jar.ir.ts.RemoveLocalFromSSA;
import com.googlecode.dex2jar.ir.ts.TypeTransformer;
import com.googlecode.dex2jar.ir.ts.UnSSATransformer;
import com.googlecode.dex2jar.ir.ts.VoidInvokeTransformer;
import com.googlecode.dex2jar.ir.ts.ZeroTransformer;
import com.googlecode.dex2jar.ir.ts.array.FillArrayTransformer;

import sun.reflect.generics.tree.Tree;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.InnerClassNode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;

public class Dex2Asm {

Expand Down Expand Up @@ -597,30 +638,39 @@ public void convertMethod(DexClassNode classNode, DexMethodNode methodNode, Clas
boolean isStatic = (methodNode.access & Opcodes.ACC_STATIC) != 0;
String[] paramTypes = methodNode.method.getParameterTypes();

int localsOffset = 0;
Map<Integer, LocalVar> localVars = new TreeMap<>();
if (!isStatic) {
LocalVar local = new LocalVar(0, "this", methodNode.method.getOwner(), null);
LocalVar local = new LocalVar(localsOffset, "this", methodNode.method.getOwner(), null);
localVars.put(local.reg, local);
localsOffset++;
}

// Handle debugNode.parameterNames
if (debugNode.parameterNames != null) {
for (int i = 0; i < debugNode.parameterNames.size(); i++) {
String paramName = debugNode.parameterNames.get(i);
for (int i = 0; i < paramTypes.length; i++) {
String paramName = i < debugNode.parameterNames.size() ? debugNode.parameterNames.get(i) : null;
String paramType = paramTypes[i];

// Put parameter name in MethodParameters attribute
mv.visitParameter(paramName, 0);
//mv.visitParameter(paramName, 0);

// Also put it into the LocalVariableTable
if (paramName != null) {
LocalVar local = new LocalVar(isStatic ? i : i + 1, paramName, paramTypes[i], null);
LocalVar local = new LocalVar(localsOffset, paramName, paramType, null);
localVars.put(local.reg, local);
}

// If the local variable at index is of type double or long, it occupies both index and index + 1.
if ("J".equals(paramType) || "D".equals(paramType)) {
localsOffset += 2;
} else {
localsOffset++;
}
}
}

// Handle debugNodes to create a LocalVariableTable
int localsOffset = paramTypes.length + (isStatic ? 0 : 1);
if (debugNode.debugNodes != null) {
for (DexDebugNode.DexDebugOpNode opDebugNode : debugNode.debugNodes) {
if (opDebugNode instanceof DexDebugNode.DexDebugOpNode.StartLocalNode) {
Expand Down

0 comments on commit 6cb158e

Please sign in to comment.