Skip to content

Commit

Permalink
incrementing issue #44
Browse files Browse the repository at this point in the history
  • Loading branch information
phtcosta committed Nov 29, 2021
1 parent 8403878 commit ee0b8b4
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 15 deletions.
33 changes: 24 additions & 9 deletions src/main/java/lang/jimple/internal/Decompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import lang.jimple.internal.generated.FieldSignature;
import lang.jimple.internal.generated.Immediate;
import lang.jimple.internal.generated.Immediate.c_iValue;
import lang.jimple.internal.generated.Type.c_TObject;
import lang.jimple.internal.generated.InvokeExp;
import lang.jimple.internal.generated.LocalVariableDeclaration;
import lang.jimple.internal.generated.Method;
Expand Down Expand Up @@ -183,6 +184,7 @@ private void visitMethod(MethodNode mn) {

for (org.objectweb.asm.Type t : org.objectweb.asm.Type.getArgumentTypes(mn.desc)) {
methodFormalArgs.add(type(t.getDescriptor()));
// System.err.println("t.getDescriptor()="+t.getDescriptor());
}

if (mn.exceptions != null) {
Expand Down Expand Up @@ -1684,6 +1686,7 @@ private void createNewArrayIns(int aType) {
public void initFormalArgs(boolean staticMethod, Type classType, boolean emptyLocalVariableTable,
List<Type> formals, List<LocalVariableNode> nodes) {

// System.err.println("********** initFormalArgs="+ ((c_TObject)classType).name);
// System.out.println("************** initFormalArgs");
// nodes.forEach(System.out::println);

Expand All @@ -1707,23 +1710,35 @@ public void initFormalArgs(boolean staticMethod, Type classType, boolean emptyLo
if (!staticMethod) {
env.instructions.add(Statement.identity(LOCAL_NAME_FOR_IMPLICIT_PARAMETER, IMPLICIT_PARAMETER_NAME, classType));
}

// nodes.forEach(v -> System.err.println("VAR="+v.desc+", idx="+v.index+", name="+v.name+", sig="+v.signature));

int idx = 0;
for (Type t : formals) {
// System.err.println("FORMAL "+t.toString() +"=="+t.getBaseType()+" ... "+t.getConstructor()+" ... "+t.getVallangType()+" :: "+t.children());
// System.err.println(">>> "+(t instanceof c_TObject));
// if(t instanceof c_TObject) {
// c_TObject obj = (c_TObject) t;
// System.err.println("==="+obj.name);
//
// String name = (!staticMethod) ? nodes.get(idx+1).name : nodes.get(idx).name;
// System.err.println("\t\t >>>> "+name);
// }
//TODO o primeiro parametro deve ser o nome da variavel
//TODO da um erro estranho no teste TestDecompiler.decompileAndroidClass() se nao tiver a segunda condicao
// if(keepOriginalVarNames && idx < nodes.size()) {
if(keepOriginalVarNames && idx < nodes.size()) {
// System.err.println("FORMAL "+t.toString() + " ::: nodes="+nodes);
// nodes.forEach(v -> System.out.println(v.name));
// String name = (!staticMethod) ? nodes.get(idx+1).name : nodes.get(idx).name;
String name = (!staticMethod) ? nodes.get(idx+1).name : nodes.get(idx).name;
// System.err.println("\t\t >>>> "+name);
// env.instructions.add(Statement.identity(name, LOCAL_PARAMETER_PREFIX + idx, t));
// }else {
// env.instructions.add(Statement.identity(LOCAL_VARIABLE_PARAMETER_PREFIX + (idx + 1),
// LOCAL_PARAMETER_PREFIX + idx, t));
// }
env.instructions.add(Statement.identity(name, LOCAL_PARAMETER_PREFIX + idx, t));
}else {
env.instructions.add(Statement.identity(LOCAL_VARIABLE_PARAMETER_PREFIX + (idx + 1),
LOCAL_PARAMETER_PREFIX + idx, t));
}

env.instructions.add(Statement.identity(LOCAL_VARIABLE_PARAMETER_PREFIX + (idx + 1),
LOCAL_PARAMETER_PREFIX + idx, t));
// env.instructions.add(Statement.identity(LOCAL_VARIABLE_PARAMETER_PREFIX + (idx + 1),
// LOCAL_PARAMETER_PREFIX + idx, t));

idx++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.usethesource.vallang.IValueFactory;
import io.usethesource.vallang.impl.persistent.ValueFactory;

@Deprecated
public class TestDecompilerVariableNames {

@Test
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/samples/pointsto/simple/FooBarStatic.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ private static Node bar(Node s) {
return s.next;
}

private static void multipleArgs(int x, Node x1, double y, Node y1, Node s[], Double d) {

}

}

/* JIMPLE (SOOT)
Expand Down
115 changes: 115 additions & 0 deletions src/test/rascal/lang/jimple/tests/TestDecompilerVariableNames.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
module lang::jimple::tests::TestDecompilerVariableNames

import lang::jimple::core::Syntax;
import lang::jimple::core::Context;
import lang::jimple::decompiler::Decompiler;

import Exception;
import IO;

loc fooBar = |project://JimpleFramework/target/test-classes/samples/pointsto/simple/FooBar.class|;
loc fooBarStatic = |project://JimpleFramework/target/test-classes/samples/pointsto/simple/FooBarStatic.class|;


test bool testFooBar(){
ClassOrInterfaceDeclaration c = decompile(fooBar);

Method bar = getMethodByName(c,"bar");

list[LocalVariableDeclaration] localVarsExpected = [
localVariableDeclaration(TObject("samples.pointsto.simple.Node"),"s"),
localVariableDeclaration(TObject("samples.pointsto.simple.FooBar"),"r0"),
localVariableDeclaration(TObject("samples.pointsto.simple.Node"),"$r1")
];

list[Statement] stmtsExpected = [
identity("r0","@this",TObject("samples.pointsto.simple.FooBar")),
identity("s","@parameter0",TObject("samples.pointsto.simple.Node")),
assign(localVariable("$r1"),localFieldRef("s","samples/pointsto/simple/Node",TObject("samples.pointsto.simple.Node"),"next")),
returnStmt(local("$r1"))
];

return check(localVarsExpected,stmtsExpected,bar);
}

test bool testFooBarStatic(){
ClassOrInterfaceDeclaration c = decompile(fooBarStatic);

Method bar = getMethodByName(c,"bar");

list[LocalVariableDeclaration] localVarsExpected = [
localVariableDeclaration(TObject("samples.pointsto.simple.Node"),"s"),
localVariableDeclaration(TObject("samples.pointsto.simple.Node"),"$r1")
];

list[Statement] stmtsExpected = [
identity("s","@parameter0",TObject("samples.pointsto.simple.Node")),
assign(localVariable("$r1"),localFieldRef("s","samples/pointsto/simple/Node",TObject("samples.pointsto.simple.Node"),"next")),
returnStmt(local("$r1"))
];

return check(localVarsExpected,stmtsExpected,bar);
}

test bool testFooBarStaticMultipleArgs(){
ClassOrInterfaceDeclaration c = decompile(fooBarStatic);

//multipleArgs(int x, Node x1, double y, Node y1, Node s[])
Method bar = getMethodByName(c,"multipleArgs");

list[LocalVariableDeclaration] localVarsExpected = [
localVariableDeclaration(TObject("samples.pointsto.simple.Node"),"y1"),
localVariableDeclaration(TObject("samples.pointsto.simple.Node"),"x1"),
localVariableDeclaration(TArray(TObject("samples.pointsto.simple.Node")),"s"),
localVariableDeclaration(TDouble(),"y"),
localVariableDeclaration(TInteger(),"x"),
localVariableDeclaration(TObject("java.lang.Double"),"d")
];

list[Statement] stmtsExpected = [
identity("x","@parameter0",TInteger()),
identity("x1","@parameter1",TObject("samples.pointsto.simple.Node")),
identity("y","@parameter2",TDouble()),
identity("y1","@parameter3",TObject("samples.pointsto.simple.Node")),
identity("s","@parameter4",TArray(TObject("samples.pointsto.simple.Node"))),
identity("d","@parameter5",TObject("java.lang.Double")),
returnEmptyStmt()
];

return check(localVarsExpected,stmtsExpected,bar);
}

void show(method(list[Modifier] modifiers, Type returnType, Name name, list[Type] formals, list[Type] exceptions, MethodBody body)){

}

bool check(list[LocalVariableDeclaration] localVarsExpected, list[Statement] stmtsExpected, method(_, _, _, _, _, methodBody(list[LocalVariableDeclaration] localVariableDecls, list[Statement] stmts, _))){
return check(localVarsExpected, localVariableDecls) && check(stmtsExpected, stmts);
}

bool check(list[&T] expected, list[&T] original){
for(&T stmt <- expected){
if(stmt notin original){
return false;
}
}
return true;
}

Method getMethodByName(ClassOrInterfaceDeclaration c, str methodName){
list[Method] methods = getMethods(c);
for(m: method(_, _, Name name, _, _, _) <- methods){
if(name == methodName){
return m;
}
}
throw NoSuchKey(methodName);
}

list[Method] getMethods(ClassOrInterfaceDeclaration c){
switch(c) {
case classDecl(_, _, _, _, _, list[Method] methods): return methods;
case interfaceDecl(_, _, _, _, list[Method] methods): return methods;
}
return [];
}
19 changes: 13 additions & 6 deletions src/test/rascal/util/Teste.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,38 @@ tuple[list[loc] classPath, list[str] entryPoints] fooBar() {
return <files, es>;
}
tuple[list[loc] classPath, list[str] entryPoints] fooBarStatic() {
files = [|project://JimpleFramework/target/test-classes/samples/pointsto/simple/|];
es = ["samples.pointsto.simple.FooBarStatic.foo()", "samples.pointsto.simple.FooBar.bar(samples.pointsto.simple.Node)"];
files = [|project://JimpleFramework/target/test-classes/samples/pointsto/simple/FooBarStatic.class|];
es = ["samples.pointsto.simple.FooBarStatic.foo()", "samples.pointsto.simple.FooBarStatic.bar(samples.pointsto.simple.Node)"];
return <files, es>;
}

public void executar(){
//tuple[list[loc] cp, list[str] e] t = iris();
tuple[list[loc] cp, list[str] e] t = fooBar();
//tuple[list[loc] cp, list[str] e] t = fooBarStatic();
//tuple[list[loc] cp, list[str] e] t = fooBar();
tuple[list[loc] cp, list[str] e] t = fooBarStatic();

classPath = t.cp;
entryPoints = t.e;

ExecutionContext ctx = createExecutionContext(classPath, entryPoints, true);

//show(ctx);
toJimple(ctx);
//toPrettyPrint(ctx);
toJimple(ctx);
}

void toJimple(ExecutionContext ctx){
void toPrettyPrint(ExecutionContext ctx){
for(className <- ctx.ct){
println(prettyPrint(ctx.ct[className].dec));
}
}

void toJimple(ExecutionContext ctx){
for(className <- ctx.ct){
println(ctx.ct[className].dec);
}
}

void show(ExecutionContext ctx){
top-down visit(ctx.ct) {
case ClassOrInterfaceDeclaration c:{
Expand Down

0 comments on commit ee0b8b4

Please sign in to comment.