Skip to content

Commit

Permalink
smallest tc where trap fails
Browse files Browse the repository at this point in the history
  • Loading branch information
sahilagichani14 committed Nov 29, 2024
1 parent a295490 commit 21a2957
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 3 deletions.
6 changes: 6 additions & 0 deletions sootup.tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>1.13.0</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
153 changes: 150 additions & 3 deletions sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
package sootup.tests;

import com.squareup.javapoet.*;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.core.model.SourceType;
import sootup.core.signatures.MethodSignature;
import sootup.core.types.PrimitiveType;
import sootup.core.types.VoidType;
import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.core.JavaSootMethod;
import sootup.java.core.views.JavaView;
import sootup.jimple.frontend.JimpleAnalysisInputLocation;
import sootup.jimple.frontend.JimpleStringAnalysisInputLocation;
import sootup.jimple.frontend.JimpleView;

import java.util.Optional;
import javax.lang.model.element.Modifier;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Tag("Java8")
public class JimpleSerializationTest {

@Test
public void testTrapSerialization() {
AnalysisInputLocation inputLocation = new JavaClassPathAnalysisInputLocation("src/test/resources/bugs/1119_trap-serialization");
AnalysisInputLocation inputLocation = new JavaClassPathAnalysisInputLocation("src/test/resources/bugs/1119_trap-serialization", SourceType.Application, Collections.emptyList());
JavaView view = new JavaView(inputLocation);

Optional<JavaSootMethod> methodOpt = view.getMethod(view.getIdentifierFactory().parseMethodSignature(
Expand All @@ -26,4 +37,140 @@ public void testTrapSerialization() {
JavaSootMethod method = methodOpt.get();
method.getBody().toString();
}

@Test
public void testBasicTrapSerialization() {
AnalysisInputLocation inputLocation = new JavaClassPathAnalysisInputLocation("src/test/resources/bugs/1119_trap-serialization", SourceType.Application, Collections.emptyList());
JavaView javaView = new JavaView(inputLocation);
Optional<JavaSootMethod> nestedTrap = javaView.getMethod(javaView.getIdentifierFactory().parseMethodSignature(
"<com.linecorp.centraldogma.server.internal.storage.repository.git.TrapSerialization: java.lang.Integer processWithExplicitCasting(java.lang.String,java.lang.String)>"
));

assertTrue(nestedTrap.isPresent());
JavaSootMethod nestedTrapMethod = nestedTrap.get();
System.out.println(nestedTrapMethod.getBody());
}

@Test
public void testJimpleTrapSerialization() {
String jimpleString = "class DummyClass extends java.lang.Object {\n" +
"\tint testTrapSerialization() {\n" +
"\t\tsootUp.RQ1.jb_a.JB_A this;\n" +
"\t\tunknown $stack4, $stack5, a, b, e;\n" +
"\n" +
"\n" +
"\t\tthis := @this: sootUp.RQ1.jb_a.JB_A;\n" +
"\t\ta = 0;\n" +
"\t\tb = 10;\n" +
"\n" +
"\t label1:\n" +
"\t\tb = b / a;\n" +
"\t\t$stack4 = b;\n" +
"\t\treturn a;\n" +
"\n" +
"\t label2:\n" +
"\t\treturn $stack4;\n" +
"\n" +
"\t label3:\n" +
"\t\t$stack5 := @caughtexception;\n" +
"\t\te = $stack5;\n" +
"\n" +
"\t\treturn b;\n" +
"\n" +
"\t catch java.lang.ArithmeticException from label1 to label2 with label3;\n" +
"\t catch java.lang.NullPointerException from label1 to label2 with label3;\n" +
"\t}\n" +
"}";

JimpleStringAnalysisInputLocation analysisInputLocation = new JimpleStringAnalysisInputLocation(jimpleString, SourceType.Application, Collections.emptyList());
JimpleView view = new JimpleView(analysisInputLocation);
assertTrue(view.getClass(analysisInputLocation.getClassType()).isPresent());
MethodSignature methodSig =
view.getIdentifierFactory()
.getMethodSignature(
analysisInputLocation.getClassType(),
"testTrapSerialization",
PrimitiveType.IntType.getInstance(),
Collections.emptyList());
assertTrue(view.getMethod(methodSig).isPresent());
}


@Test
public void addNopInEndOfTryCatchFinally() throws IOException {
// Define the method body
CodeBlock methodBody = CodeBlock.builder()
.addStatement("$T<String, $T<?>> result = new $T<>()", Map.class, Map.Entry.class, HashMap.class)
.beginControlFlow("try")
.addStatement("result.put($S, new $T.SimpleEntry<>($S, $S))", "try", AbstractMap.class, "Key1", "Value1")
.addStatement("return result")
.nextControlFlow("catch ($T e)", Exception.class)
.addStatement("result.put($S, new $T.SimpleEntry<>($S, $S))", "catch", AbstractMap.class, "Key2", "Value2")
.addStatement("return result")
.nextControlFlow("finally")
.addStatement("result.put($S, new $T.SimpleEntry<>($S, $S))", "finally", AbstractMap.class, "Key3", "Value3")
.endControlFlow()
.addStatement("return result")
.addStatement("return result")
.build();

// Create the method
MethodSpec tryCatchFinallyMethod = MethodSpec.methodBuilder("tryCatchFinallyMethod")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(ParameterizedTypeName.get(Map.class, String.class))
.addCode(methodBody)
.build();

// Create the class
TypeSpec tryCatchFinallyClass = TypeSpec.classBuilder("TryCatchFinallyExample")
.addModifiers(Modifier.PUBLIC)
.addMethod(tryCatchFinallyMethod)
.build();

// Write to a Java file
JavaFile javaFile = JavaFile.builder("com.example", tryCatchFinallyClass)
.build();

// Write the generated Java file to the file system
javaFile.writeTo(Paths.get("./src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/"));
}

@Test
public void addNopInEndOfNestedTrap() throws IOException {
// Define the method body with nested try-catch blocks
CodeBlock methodBody = CodeBlock.builder()
.beginControlFlow("try")
.addStatement("System.out.println($S)", "Outer try block")
.beginControlFlow("try")
.addStatement("System.out.println($S)", "Inner try block")
.addStatement("throw new RuntimeException($S)", "Inner exception")
.nextControlFlow("catch (Exception e)")
.addStatement("System.out.println($S + e.getMessage())", "Caught inner exception: ")
.endControlFlow()
.nextControlFlow("catch (Exception e)")
.addStatement("System.out.println($S + e.getMessage())", "Caught outer exception: ")
.endControlFlow()
.build();

// Create the method
MethodSpec nestedTryCatchMethod = MethodSpec.methodBuilder("nestedTryCatch")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(void.class)
.addCode(methodBody)
.build();

// Create the class
TypeSpec nestedTryCatchClass = TypeSpec.classBuilder("NestedTryCatchExample")
.addModifiers(Modifier.PUBLIC)
.addMethod(nestedTryCatchMethod)
.build();

// Write to a Java file
JavaFile javaFile = JavaFile.builder("com.example", nestedTryCatchClass)
.build();

// Write the generated Java file to the file system
javaFile.writeTo(Paths.get("./src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/"));
}

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.linecorp.centraldogma.server.internal.storage.repository.git;

import java.util.*;
import java.io.*;

public class TrapSerialization {

public Integer processWithExplicitCasting(String var2, String var4) throws Exception{
Object var19;
try {
try {
var19 = 10;
throw new Exception();
} catch (Exception e) {
var19 = 20;
throw new Exception(e);
} finally {
var19 = 30;
}
} catch (Exception ex) {
var19 = 40;
}
return (Integer) var19;
}

}

0 comments on commit 21a2957

Please sign in to comment.