From 21a29571d43a083b6fe1adbb1e1bc64671df16e6 Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Fri, 29 Nov 2024 12:01:30 +0100 Subject: [PATCH] smallest tc where trap fails --- sootup.tests/pom.xml | 6 + .../sootup/tests/JimpleSerializationTest.java | 153 +++++++++++++++++- .../repository/git/TrapSerialization.class | Bin 0 -> 735 bytes .../repository/git/TrapSerialization.java | 26 +++ 4 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 sootup.tests/src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/TrapSerialization.class create mode 100644 sootup.tests/src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/TrapSerialization.java diff --git a/sootup.tests/pom.xml b/sootup.tests/pom.xml index d2c9e05b0bd..1562de85a68 100644 --- a/sootup.tests/pom.xml +++ b/sootup.tests/pom.xml @@ -44,6 +44,12 @@ slf4j-simple test + + com.squareup + javapoet + 1.13.0 + test + diff --git a/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java b/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java index 96084645e26..1b131a16222 100644 --- a/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java +++ b/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java @@ -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 methodOpt = view.getMethod(view.getIdentifierFactory().parseMethodSignature( @@ -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 nestedTrap = javaView.getMethod(javaView.getIdentifierFactory().parseMethodSignature( + "" + )); + + 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> 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/")); + } + } diff --git a/sootup.tests/src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/TrapSerialization.class b/sootup.tests/src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/TrapSerialization.class new file mode 100644 index 0000000000000000000000000000000000000000..33ff6c5999cf2778152886358ae672d06f44165f GIT binary patch literal 735 zcmZuuO>fgc5Pcgvbun?`wxMkbEgvVRRq=rfG{B`QQc+0-iHbOG+{NAEc&+s&6n+gq z0Zu_6QO^7*#B6~!sCeZ!GjC^h-p>B``Q;mcN4V*u0>^_}LlrX&;V~ccIOBO5?+=be zVi;x~%3PYq3{KQO@G*-k9{d`vqQ)?Hk-nQ7kqXV=jd?Z_`>z=mquuu2rHqb;I>W*R zzjKm^!bp|-2;f+lvufrUmZPci{*hL1`5+UW_JM~c!;3@><4oovQM!l|ksHmkLzNDB zTnarFI+mnK=RAu`qcl%NtVN+pNx{1~l_u_MUi5^PJd^LaRlwkGt3$!i*rRj1qv1g4 zzU|4dQfQTkvV1Ac(auSc$wZoMUK*LF3=gJ-_KYU7GbQ~G^S@y*1U#qXPPg2!`8RI46NIN*)o{%+t*YIOth@OgvmdD* zsM89_-oY(GV1rgb9cM#<~$<0zy_MwBrW%_gcfDi;dDI&&poU> SC5s!xxuiUxf0g_S)_ws}nW