Analyzing a small class with one method causes java.lang.IllegalStateException: At least 4 values were expected on the stack!

275 views
Skip to first unread message

jani.s...@gmail.com

unread,
Aug 26, 2016, 10:21:12 AM8/26/16
to SonarQube
I have the following java code (minimized and obscured):

package test;

public class Test {

    private static void test(long toRevision, boolean inverted, Object visitor) {

        testBlock: {
            if (Math.random() > 0.5)
                break testBlock;
            test(0, false ? inverted : !inverted, null);
        }
    }

    public static void main(String[] args) {
        test(0, false, null);
    }
}

While trying to analyze this the analyzing process fails with the following exception:

ERROR: Error during SonarQube Scanner execution
org.sonar.squidbridge.api.AnalysisException: SonarQube is unable to analyze file : 'C:\ws\sonar-cli\Test\src\test\Test.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:133)
at org.sonar.java.JavaSquid.scan(JavaSquid.java:126)
at org.sonar.plugins.java.JavaSquidSensor.execute(JavaSquidSensor.java:93)
at org.sonar.scanner.sensor.SensorWrapper.analyse(SensorWrapper.java:53)
at org.sonar.scanner.phases.SensorsExecutor.executeSensor(SensorsExecutor.java:57)
at org.sonar.scanner.phases.SensorsExecutor.execute(SensorsExecutor.java:49)
at org.sonar.scanner.phases.AbstractPhaseExecutor.execute(AbstractPhaseExecutor.java:78)
at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:184)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:142)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:127)
at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:241)
at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:236)
at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:226)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:142)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:127)
at org.sonar.scanner.task.ScanTask.execute(ScanTask.java:47)
at org.sonar.scanner.task.TaskContainer.doAfterStart(TaskContainer.java:86)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:142)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:127)
at org.sonar.scanner.bootstrap.GlobalContainer.executeTask(GlobalContainer.java:115)
at org.sonar.batch.bootstrapper.Batch.executeTask(Batch.java:118)
at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
at com.sun.proxy.$Proxy0.execute(Unknown Source)
at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:244)
at org.sonarsource.scanner.api.EmbeddedScanner.runAnalysis(EmbeddedScanner.java:154)
at org.sonarsource.scanner.cli.Main.runAnalysis(Main.java:110)
at org.sonarsource.scanner.cli.Main.execute(Main.java:72)
at org.sonarsource.scanner.cli.Main.main(Main.java:60)
Caused by: java.lang.IllegalStateException: At least 4 values were expected on the stack!
at org.sonar.java.se.ProgramState.peekValues(ProgramState.java:144)
at org.sonar.java.se.checks.UnclosedResourcesCheck$PreStatementVisitor.closeArguments(UnclosedResourcesCheck.java:296)
at org.sonar.java.se.checks.UnclosedResourcesCheck$PreStatementVisitor.visitMethodInvocation(UnclosedResourcesCheck.java:269)
at org.sonar.java.model.expression.MethodInvocationTreeImpl.accept(MethodInvocationTreeImpl.java:82)
at org.sonar.java.se.checks.UnclosedResourcesCheck.checkPreStatement(UnclosedResourcesCheck.java:78)
at org.sonar.java.se.CheckerDispatcher.executeCheckPreStatement(CheckerDispatcher.java:48)
at org.sonar.java.se.ExplodedGraphWalker.visit(ExplodedGraphWalker.java:345)
at org.sonar.java.se.ExplodedGraphWalker.execute(ExplodedGraphWalker.java:188)
at org.sonar.java.se.ExplodedGraphWalker.visitMethod(ExplodedGraphWalker.java:152)
at org.sonar.java.model.declaration.MethodTreeImpl.accept(MethodTreeImpl.java:218)
at org.sonar.java.se.SymbolicExecutionVisitor.visitNode(SymbolicExecutionVisitor.java:49)
at org.sonar.java.ast.visitors.SubscriptionVisitor.visit(SubscriptionVisitor.java:95)
at org.sonar.java.ast.visitors.SubscriptionVisitor.visitChildren(SubscriptionVisitor.java:120)
at org.sonar.java.ast.visitors.SubscriptionVisitor.visit(SubscriptionVisitor.java:97)
at org.sonar.java.ast.visitors.SubscriptionVisitor.visitChildren(SubscriptionVisitor.java:120)
at org.sonar.java.ast.visitors.SubscriptionVisitor.visit(SubscriptionVisitor.java:97)
at org.sonar.java.ast.visitors.SubscriptionVisitor.scanTree(SubscriptionVisitor.java:78)
at org.sonar.java.ast.visitors.SubscriptionVisitor.scanFile(SubscriptionVisitor.java:64)
at org.sonar.java.model.VisitorsBridge.visitFile(VisitorsBridge.java:120)
at org.sonar.java.ast.JavaAstScanner.simpleScan(JavaAstScanner.java:84)
... 34 more


I further tested this and found out that:

  1. the amount of test-methods parameters do matter (tried to decrease the parameters down to two but then the analyzing process completed successfully).
  2. testBlock code block is required for reproducing this bug
  3. the if (Math.random() > 0.5) is required (it cannot simply be if (false)
  4. the try-state inverted : !inverted, null is required (so one cannot pass just false for the method)

I assume that the analyzer somehow interprets that the method test is called with five arguments instead of the required 3, hence why at least 4 values were expected..

I tried to submit an issue directly to https://jira.sonarsource.com/browse/SONARJAVA/ but couldn't find a link for creating one


Nicolas Peru

unread,
Aug 27, 2016, 5:17:27 AM8/27/16
to jani.s...@gmail.com, SonarQube
Hi, 

Could you provide the version of the sonar java analyzer you are using ? 
Thanks.

--
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/5a4b4f02-fa69-4c68-ad2c-6aa11ac3a50a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Nicolas PERU | SonarSource
Senior Developer
http://sonarsource.com

jani.s...@gmail.com

unread,
Aug 27, 2016, 5:36:59 AM8/27/16
to SonarQube, jani.s...@gmail.com
At first I faced this issue in production with Jenkins with the following configuration:

INFO: SonarQube Scanner 2.7
INFO
: Java 1.8.0_101 Oracle Corporation (64-bit)
INFO
: Windows 10 10.0 amd64
INFO
: Error stacktraces are turned on.
INFO
: User cache: C:\Users\Jenkins\.sonar\cache
INFO
: Load global repositories
INFO
: Load global repositories (done) | time=744ms
INFO
: User cache: C:\Users\Jenkins\.sonar\cache
INFO
: Load plugins index
INFO
: Load plugins index (done) | time=10ms
INFO
: Download sonar-csharp-plugin-5.3.1.jar
INFO
: Download sonar-java-plugin-4.0.jar
INFO
: Download sonar-scm-git-plugin-1.2.jar
INFO
: Download sonar-scm-svn-plugin-1.3.jar
INFO
: Download sonar-javascript-plugin-2.14.jar
INFO
: SonarQube server 6.0

And then I started to narrow down why one of our classes failed with that exception (the IllegalStateException thrown is not quite specific on what is wrong with the current class though) and downloaded the sonar-scanner-cli from github. I cloned the master branch of the cli and started it with the proper arguments. So I assume that the version of that sonar java analyzer was a "master" as well. I can check this later

jani.s...@gmail.com

unread,
Aug 27, 2016, 7:50:18 AM8/27/16
to SonarQube, jani.s...@gmail.com
I updated the sonar-java-plugin on our SonarQube server to version 4.2 and yes, this can also be reproduced with the latest sonar-java-plugin-4.2.jar

Nicolas Peru

unread,
Aug 31, 2016, 3:53:04 AM8/31/16
to jani.s...@gmail.com, SonarQube
Hi, 

Thanks for the feedback and reproducer ! 

This is a flaw in the CFG construction for labeled break statement (which are not often used which explains why it was not caught earlier). Basically, the flow is wrong for this construction leading to wrong state of the stack in the following symbolic execution. Ticket created to fix the issue https://jira.sonarsource.com/browse/SONARJAVA-1827 that should be shipped in upcoming version 4.3 of the java analyzer.

Thanks again for the reproducer. 

Cheers, 


For more options, visit https://groups.google.com/d/optout.

jani.s...@gmail.com

unread,
Aug 31, 2016, 3:58:37 AM8/31/16
to SonarQube, jani.s...@gmail.com
Hi,

I'm glad to be of help :-)

Good to hear you found out the cause of this bug. Lets hope it'll make to the version 4.3.

Thank you for your fast replies and comments!
Reply all
Reply to author
Forward
0 new messages