Java Analysis: NullPointerException in ProgramState.cleanupDeadSymbols

405 views
Skip to first unread message

Patrick Bänziger

unread,
Feb 5, 2016, 7:43:53 AM2/5/16
to SonarQube
Hello there

We are running into an issue when analyzing a complex maven project with the newest Sonar (5.3) and Sonar Java plugin (3.10).
The analysis crashes with the stacktrace attached at the end when run using  mvn clean sonar:sonar.

I managed to reduce our code to reproduce it with a minimal code example.

The complete project (with POM) can be downloaded here: https://drive.google.com/file/d/0ByjUMh5UybW7OG03NC1QcmlpXzA/view?usp=sharing
Alternatively, I'll attach the single Java file at the end of the post.

I'd be happy to assist with debugging or provide more information if I can.

Thanks for the awesome work that you put into SonarQube.

Cheers,
Patrick



[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.0.1:sonar (default-cli) on project [...]: SonarQube is unable to analyze file : 'C:\git\t
estproject\src\main\java\[...]\restore\RestoreService.java': NullPointerException -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.0.1:sonar (default-cli) on project [...]:
 SonarQube is unable to analyze file : 'C:\git\testproject\src\main\java\[...]\RestoreService.java'
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: SonarQube is unable to analyze file : 'C:\git\testproject\src\main\java\[...]\RestoreService.java'
        at org.sonarsource.scanner.maven.bootstrap.ExceptionHandling.handle(ExceptionHandling.java:36)
        at org.sonarsource.scanner.maven.bootstrap.RunnerBootstrapper.execute(RunnerBootstrapper.java:81)
        at org.sonarsource.scanner.maven.SonarQubeMojo.execute(SonarQubeMojo.java:112)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
        ... 20 more
Caused by: org.sonar.squidbridge.api.AnalysisException: SonarQube is unable to analyze file : 'C:\git\testproject\src\main\java\[...]\RestoreService.java'
        at org.sonar.java.ast.JavaAstScanner.simpleScan(JavaAstScanner.java:93)
        at org.sonar.java.ast.JavaAstScanner.scan(JavaAstScanner.java:67)
        at org.sonar.java.JavaSquid.scanSources(JavaSquid.java:135)
        at org.sonar.java.JavaSquid.scan(JavaSquid.java:128)
        at org.sonar.plugins.java.JavaSquidSensor.analyse(JavaSquidSensor.java:90)
        at org.sonar.batch.phases.SensorsExecutor.executeSensor(SensorsExecutor.java:58)
        at org.sonar.batch.phases.SensorsExecutor.execute(SensorsExecutor.java:50)
        at org.sonar.batch.phases.PhaseExecutor.execute(PhaseExecutor.java:98)
        at org.sonar.batch.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:185)
        at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:132)
        at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:117)
        at org.sonar.batch.scan.ProjectScanContainer.scan(ProjectScanContainer.java:243)
        at org.sonar.batch.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:238)
        at org.sonar.batch.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:228)
        at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:132)
        at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:117)
        at org.sonar.batch.task.ScanTask.execute(ScanTask.java:55)
        at org.sonar.batch.task.TaskContainer.doAfterStart(TaskContainer.java:86)
        at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:132)
        at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:117)
        at org.sonar.batch.bootstrap.GlobalContainer.executeTask(GlobalContainer.java:122)
        at org.sonar.batch.bootstrapper.Batch.executeTask(Batch.java:119)
        at org.sonar.runner.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:67)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.sonar.runner.impl.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:61)
        at com.sun.proxy.$Proxy23.execute(Unknown Source)
        at org.sonar.runner.api.EmbeddedRunner.doExecute(EmbeddedRunner.java:274)
        at org.sonar.runner.api.EmbeddedRunner.runAnalysis(EmbeddedRunner.java:165)
        at org.sonar.runner.api.EmbeddedRunner.runAnalysis(EmbeddedRunner.java:152)
        at org.sonarsource.scanner.maven.bootstrap.RunnerBootstrapper.execute(RunnerBootstrapper.java:78)
        ... 23 more
Caused by: java.lang.NullPointerException
        at org.sonar.java.se.ProgramState.cleanupDeadSymbols(ProgramState.java:242)
        at org.sonar.java.se.ExplodedGraphWalker.cleanUpProgramState(ExplodedGraphWalker.java:226)
        at org.sonar.java.se.ExplodedGraphWalker.handleBlockExit(ExplodedGraphWalker.java:234)
        at org.sonar.java.se.ExplodedGraphWalker.execute(ExplodedGraphWalker.java:169)
        at org.sonar.java.se.ExplodedGraphWalker.visitMethod(ExplodedGraphWalker.java:132)
        at org.sonar.java.model.declaration.MethodTreeImpl.accept(MethodTreeImpl.java:218)
        at org.sonar.java.se.SymbolicExecutionVisitor.visitNode(SymbolicExecutionVisitor.java:42)
        at org.sonar.java.ast.visitors.SubscriptionVisitor.visit(SubscriptionVisitor.java:88)
        at org.sonar.java.ast.visitors.SubscriptionVisitor.visitChildren(SubscriptionVisitor.java:113)
        at org.sonar.java.ast.visitors.SubscriptionVisitor.visit(SubscriptionVisitor.java:90)
        at org.sonar.java.ast.visitors.SubscriptionVisitor.visitChildren(SubscriptionVisitor.java:113)
        at org.sonar.java.ast.visitors.SubscriptionVisitor.visit(SubscriptionVisitor.java:90)
        at org.sonar.java.ast.visitors.SubscriptionVisitor.visitChildren(SubscriptionVisitor.java:113)
        at org.sonar.java.ast.visitors.SubscriptionVisitor.visit(SubscriptionVisitor.java:90)
        at org.sonar.java.ast.visitors.SubscriptionVisitor.scanTree(SubscriptionVisitor.java:71)
        at org.sonar.java.ast.visitors.SubscriptionVisitor.scanFile(SubscriptionVisitor.java:64)
        at org.sonar.java.model.VisitorsBridge.visitFile(VisitorsBridge.java:124)
        at org.sonar.java.ast.JavaAstScanner.simpleScan(JavaAstScanner.java:84)
        ... 55 more
[ERROR]
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:

package my.test.pkg;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class RestoreService  {

  public static class FullCode {
    public static final int ID = 1;
  }

  protected class Runner implements Runnable {

    private final Map<Object, Set<Object>> m_anotherMap;
    private final Object m_restriction;

    public Runner(Object restriction, Map<Object, Set<Object>> somemap) {
      m_restriction = restriction;
      m_anotherMap = new HashMap<>(somemap);
    }

    @Override
    public void run() throws Exception {
      try {
        int type = m_restriction == null ? 1 : 2;
        for (Entry<Object, Set<Object>> e : m_anotherMap.entrySet()) {
          final Object tableDef = e.getKey();
          final Set<Object> aSet = e.getValue();

          try {
            Date date = new Date(new Date().getTime() - 1000);
            
            int rowCount = -1;
            if (type == FullCode.ID) {
              
            }
            else {

            }
          }
          finally {
            
          }
        }
      }
      finally {
        for (Object tableDef : m_anotherMap.keySet()) {
          
        }
      }
    }
  }

}


Nicolas Peru

unread,
Feb 9, 2016, 4:16:05 AM2/9/16
to Patrick Bänziger, SonarQube
Hi Patrick, 

Thank you SO MUCH for the reproducer. It took me a while to figure it out but I figure out the problem (hold onto your hats this is rather tricky) :
The problem was in the pruning phase of the CFG construction : 
When creating the CFG we might create blocks that will be somehow purposeless for symbolic execution. To simplify things a bit for SE, we get rid of those by replacing them by their successors and renumbering the blocks.

In order to handle more correctly the case of raise of exception with finally we introduced a notion of an exit block in the CFG. But (and I am to blame here) we did not update take care of replacing the exit block during the pruning phase. 
The consequence was that during Live Variable Analysis was not able to find the live variables after that exit block (because in the end this block was not in the CFG block list) and this resulted in the NPE you encountered. 

Long story short, fix is pretty simple and I am going to handle it right away : https://jira.sonarsource.com/browse/SONARJAVA-1531

Thanks again for the detailed feedback with a reproducer. 

Cheers,



--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/f9e4dd0c-c3d9-4766-bc8d-3ac119611ca2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

patrickb...@gmail.com

unread,
Feb 9, 2016, 4:38:23 AM2/9/16
to SonarQube, patrickb...@gmail.com
Hello Nicolas

Thanks for your quick answer and detailed explanation.
I'm glad that my reduced code sample was useful :)

Thanks for fixing this, we'll look forward to 3.11.

Cheers,
Patrick

patrickb...@gmail.com

unread,
Feb 22, 2016, 7:27:58 AM2/22/16
to SonarQube, patrickb...@gmail.com
Just a quick feedback:
I ran the analysis locally with the SonarJava 3.11-RC1 that was published on GitHub and it's running without any issue on my small test example and our large internal project.

Thanks for fixing this.
Best regards,
Patrick
Reply all
Reply to author
Forward
0 new messages