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

Traps not handled properly when analyzing source code #533

Closed
thebesttv opened this issue Dec 31, 2022 · 2 comments
Closed

Traps not handled properly when analyzing source code #533

thebesttv opened this issue Dec 31, 2022 · 2 comments

Comments

@thebesttv
Copy link
Contributor

Hello, I was trying to analyze the following Java source code:

public class HelloWorld {
    public static void main(String[] var0) {
        int x = 0, y = 0;
        try {
            System.out.println(x / y);
        } catch (Exception e){
            System.out.println("Exception caught");
        }
    }
}

using code from the BasicSetup.java example, but got the following exception:

Exception in thread "main" java.lang.RuntimeException: StmtGraph of <HelloWorld: void main(java.lang.String[])> is invalid.
        at sootup.core.model.Body$BodyBuilder.build(Body.java:520)
        at sootup.java.sourcecode.frontend.WalaIRToJimpleConverter.createBody(WalaIRToJimpleConverter.java:557)
        at sootup.java.sourcecode.frontend.WalaIRToJimpleConverter.convertMethod(WalaIRToJimpleConverter.java:278)
        at sootup.java.sourcecode.frontend.WalaIRToJimpleConverter.convertToClassSource(WalaIRToJimpleConverter.java:175)
        at java.util.Optional.map(Optional.java:215)
        at sootup.java.sourcecode.frontend.WalaJavaClassProvider.getClassSource(WalaJavaClassProvider.java:298)
        at sootup.java.sourcecode.frontend.WalaJavaClassProvider.createClassSource(WalaJavaClassProvider.java:354)
        at sootup.java.sourcecode.inputlocation.JavaSourcePathAnalysisInputLocation.getClassSource(JavaSourcePathAnalysisInputLocation.java:162)
        at sootup.java.core.views.JavaView.lambda$getAbstractClass$1(JavaView.java:130)
        at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
        at java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1361)
        at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
        at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:499)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:486)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
        at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.util.stream.ReferencePipeline.findAny(ReferencePipeline.java:536)
        at sootup.java.core.views.JavaView.getAbstractClass(JavaView.java:136)
        at sootup.java.core.views.JavaView.getClass(JavaView.java:118)
        at thebesttv.sootup.Main.main(Main.java:65)
Caused by: java.lang.IllegalStateException: visualize invalid StmtGraph: http://magjac.com/graphviz-visual-editor/?dot=digraph+G+%7B%0A%09compound%3Dtrue%0A%09labelloc%3Db%0A%09style%3Dfilled%0A%09color%3Dgray90%0A%09node+%5Bshape%3Dbox%2Cstyle%3Dfilled%2Ccolor%3Dwhite%5D%0A%09edge+%5Bfontsize%3D10%2Carrowsize%3D1.5%2Cfontcolor%3Dgrey40%5D%0A%09fontsize%3D10%0A%0A%2F%2F++lines+%5B6%3A+7%5D+%0A%09subgraph+cluster_573424492+%7B+%0A%09%09label+%3D+%22Block+%231%22%0A%09%09979865152%5Blabel%3D%22%24r2+%3A%3D+%40caughtexception%22%5D%0A%09%09876994034%5Blabel%3D%22%24r3+%3D+%24r2%22%5D%0A%09%09666389658%5Blabel%3D%22%24r4+%3D+%26lt%3Bjava.lang.System%3A+java.io.PrintStream+out%26gt%3B%22%5D%0A%09%09564908963%5Blabel%3D%22virtualinvoke+%24r4.%26lt%3Bjava.io.PrintStream%3A+void+println%28java.lang.String%29%26gt%3B%28%26quot%3BException+caught%26quot%3B%29%22%5D%0A%0A%09%09979865152+-%3E+876994034+-%3E+666389658+-%3E+564908963%0A%09%7D%0A%09564908963%3As+-%3E+1788841007%3An%0A%0A%2F%2F++lines+%5B3%3A+4%5D+%0A%09subgraph+cluster_596403167+%7B+%0A%09%09label+%3D+%22Block+%232%22%0A%09%091605393415%5Blabel%3D%22%24r0+%3A%3D+%40parameter0%3A+java.lang.String%5B%5D%22%2Cshape%3DMdiamond%2Ccolor%3Dgrey50%2Cfillcolor%3Dwhite%5D%0A%09%09422278820%5Blabel%3D%22%24i0+%3D+0%22%5D%0A%09%091838590101%5Blabel%3D%22%24i1+%3D+0%22%5D%0A%09%09815497540%5Blabel%3D%22%24r1+%3D+%26lt%3Bjava.lang.System%3A+java.io.PrintStream+out%26gt%3B%22%5D%0A%09%091892261395%5Blabel%3D%22%24i2+%3D+%24i0+%2F+%24i1%22%5D%0A%09%09840259832%5Blabel%3D%22virtualinvoke+%24r1.%26lt%3Bjava.io.PrintStream%3A+void+println%28int%29%26gt%3B%28%24i2%29%22%5D%0A%09%091047698758%5Blabel%3D%22goto%22%5D%0A%0A%09%091605393415+-%3E+422278820+-%3E+1838590101+-%3E+815497540+-%3E+1892261395+-%3E+840259832+-%3E+1047698758%0A%09%7D%0A%091047698758%3As+-%3E+1788841007%3An%0A%0A%2F%2F++lines+%5B7%3A+7%5D+%0A%09subgraph+cluster_107131289+%7B+%0A%09%09label+%3D+%22Block+%233%22%0A%09%091788841007%5Blabel%3D%22return%22%2Cshape%3DMdiamond%2Ccolor%3Dgrey50%2Cfillcolor%3Dwhite%5D%0A%09%7D%0A%0A%7D
        at sootup.core.graph.StmtGraph.validateStmtConnectionsInGraph(StmtGraph.java:242)
        at sootup.core.model.Body$BodyBuilder.build(Body.java:518)
        ... 20 more
Caused by: java.lang.IllegalStateException: Stmt '$r2 := @caughtexception' which is neither the StartingStmt nor a TrapHandler is missing a predecessor!
        at sootup.core.graph.StmtGraph.validateStmtConnectionsInGraph(StmtGraph.java:188)
        ... 21 more

When analyzing the compiled Java bytecode, everything seems fine.

I think this has something to do with the commented code at line 535 of WalaIRToJimpleConverter.java, because once I changed it to:

          AbstractCFG<? extends SSAInstruction, IBasicBlock<SSAInstruction>> newCfg =
                  (AbstractCFG<? extends SSAInstruction, IBasicBlock<SSAInstruction>>) cfg;
          final Collection<IBasicBlock<SSAInstruction>> exceptionalPredecessors = newCfg.getExceptionalPredecessors(block);

          for (IBasicBlock<SSAInstruction> exceptionalPredecessor : exceptionalPredecessors) {
            Stmt from = index2Stmt.get(exceptionalPredecessor.getFirstInstructionIndex());
            Stmt to = index2Stmt.get(exceptionalPredecessor.getLastInstructionIndex() + 1); // exclusive!

            Stmt handlerStmt = index2Stmt.get(block.getFirstInstructionIndex());
            for (TypeReference type : exceptionTypes) {
              ClassType exception = (ClassType) convertType(type);
              traps.add(new Trap(exception, from, to, handlerStmt));
            }
          }

the analysis seems to work.

Is the support for try-catch block in Java source code not yet available, or am I missing something?
Thanks a lot!

@swissiety
Copy link
Collaborator

Hello,
yes this can work occasionally, but the commented code worked just with those simple non nested trycatch blocks - the generated jimple for nested trycatch was wrong semantically (#482) - so currently its not supported for the sourcecodefrontend, yet.

@thebesttv
Copy link
Contributor Author

Thanks for the clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants