Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JimplePrinter escaping to work like (old) Soot #647

Draft
wants to merge 14 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void typeSignature(@Nonnull Type type) {
((ArrayType) type).toString(this);
} else {
// primitive types: there should be no need to escape sth
output.append(type.toString());
output.append(type);
}
} else {
output.append(Jimple.escape(type.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,19 @@ private void printMethods(SootClass<?> cl, LabeledStmtPrinter printer, PrintWrit
while (methodIt.hasNext()) {
SootMethod method = (SootMethod) methodIt.next();

printer.handleIndent();
method.toString(printer);

if (method.hasBody()) {
Body body = method.getBody();
// print method's full signature information
method.toString(printer);
printer.newline();
incJimpleLnNum();
printBody(body, printer);

} else {
printer.handleIndent();
method.toString(printer);
printer.literal(";");
printer.newline();
incJimpleLnNum();
}

Expand Down Expand Up @@ -323,6 +325,7 @@ private void printStmts(StmtGraph<?> stmtGraph, LabeledStmtPrinter printer) {

final Map<Stmt, String> labels = printer.getLabels();
for (Stmt currentStmt : linearizedStmtGraph) {
if (currentStmt == null) continue;
previousStmt = currentStmt;

// Print appropriate header.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@
* #L%
*/

import com.google.common.collect.ImmutableSet;
import java.util.Set;
import javax.annotation.Nonnull;
import sootup.core.jimple.common.stmt.Stmt;
import sootup.core.jimple.javabytecode.stmt.JSwitchStmt;
import sootup.core.types.ClassType;
import sootup.core.types.Type;
import sootup.core.util.StringTools;

/**
* StmtPrinter implementation for normal (full) Jimple for OldSoot
Expand All @@ -35,10 +41,82 @@
*/
public class LegacyJimplePrinter extends NormalStmtPrinter {

// source:
// https://github.com/soot-oss/soot/blob/1ad74494974165e8b5f2286c90f218a00eadc243/eclipse/ca.mcgill.sable.soot/src/ca/mcgill/sable/soot/editors/JimpleScanner.java
Set<String> soot_jimple_keywords =
ImmutableSet.of(
"ignored",
"abstract",
"final",
"native",
"public",
"protected",
"private",
"static",
"synchronized",
"transient",
"volatile",
"class",
"interface",
"void",
"boolean",
"byte",
"short",
"char",
"int",
"long",
"float",
"double",
"null_type",
"unknown",
"extends",
"implements",
"breakpoint",
"case",
"catch",
"cmp",
"cmpg",
"cmpl",
"default",
"entermonitor",
"exitmonitor",
"goto",
"if",
"instanceof",
"interfaceinvoke",
"lengthof",
"lookupswitch",
"neg",
"new",
"newarray",
"newmultiarray",
"nop",
"ret",
"return",
"specialinvoke",
"staticinvoke",
"tableswitch",
"throw",
"throws",
"virtualinvoke",
"null",
"from",
"to",
"with",
"annotation",
"enum");

public LegacyJimplePrinter() {
super();
}

String sootEscape(String str) {
if (str.length() == 0) {
return "\"\"";
swissiety marked this conversation as resolved.
Show resolved Hide resolved
}
return StringTools.getQuotedStringOf(str, soot_jimple_keywords.contains(str));
}

@Override
void enableImports(boolean enable) {
if (enable) {
Expand All @@ -47,6 +125,27 @@ void enableImports(boolean enable) {
}
}

@Override
public void typeSignature(@Nonnull Type type) {
handleIndent();
if (type instanceof ClassType) {
ClassType ctype = (ClassType) type;
final String[] splits = ctype.getPackageName().getPackageName().split("\\.");
for (String split : splits) {
if (split.length() == 0) {
continue;
}
output.append(sootEscape(split));
output.append(".");
}
output.append(sootEscape(ctype.getClassName()));

} else {
// primitivetypes
output.append(type);
}
}

@Override
public void stmt(Stmt currentStmt) {
startStmt(currentStmt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@
public class LegacyJimplePrinterTest {

SootClass buildClass(Body.BodyBuilder builder) {
return buildClass(builder, "dummyMain", "main");
}

SootClass buildClass(Body.BodyBuilder builder, String className, String methodName) {

Project project =
JavaProject.builder(new JavaLanguage(8)).addInputLocation(new EagerInputLocation()).build();
View view = project.createView();

MethodSignature methodSignature =
view.getIdentifierFactory()
.getMethodSignature("main", "dummyMain", "void", Collections.emptyList());
.getMethodSignature(methodName, className, "void", Collections.emptyList());
Body body =
builder
.setMethodSignature(methodSignature)
Expand All @@ -63,7 +67,7 @@ SootClass buildClass(Body.BodyBuilder builder) {
null,
null,
null,
view.getIdentifierFactory().getClassType("dummyMain"),
view.getIdentifierFactory().getClassType(className),
new EagerInputLocation()),
SourceType.Application);
}
Expand Down Expand Up @@ -157,4 +161,17 @@ public void testValidOptions() {
new JimplePrinter(JimplePrinter.Option.UseImports, JimplePrinter.Option.LegacyMode);
p.printTo(buildClass(Body.builder()), new PrintWriter(new StringWriter()));
}

@Test
public void testLegacyEscaping() {
StringWriter out = new StringWriter();
PrintWriter writer = new PrintWriter(out);
JimplePrinter printer = new JimplePrinter(JimplePrinter.Option.LegacyMode);

SootClass clazz = buildClass(Body.builder(), "dummyMain", "from");
printer.printTo(clazz, writer);
String jimple = out.toString();
assertEquals("public class dummyMain\n{\n public static void \'from\'()\n {\n }\n}\n\r\n", jimple);
swissiety marked this conversation as resolved.
Show resolved Hide resolved
}

}