use of org.sonar.java.se.SymbolicExecutionVisitor in project sonar-java by SonarSource.
the class VisitorsBridge method visitFile.
public void visitFile(@Nullable Tree parsedTree) {
semanticModel = null;
CompilationUnitTree tree = new JavaTree.CompilationUnitTreeImpl(null, new ArrayList<>(), new ArrayList<>(), null, null);
boolean fileParsed = parsedTree != null;
if (fileParsed && parsedTree.is(Tree.Kind.COMPILATION_UNIT)) {
tree = (CompilationUnitTree) parsedTree;
if (isNotJavaLangOrSerializable(PackageUtils.packageName(tree.packageDeclaration(), "/"))) {
try {
semanticModel = SemanticModel.createFor(tree, classLoader);
} catch (Exception e) {
LOG.error("Unable to create symbol table for : " + currentFile.getAbsolutePath(), e);
addAnalysisError(e, currentFile.getPath(), AnalysisError.Kind.SEMANTIC_ERROR);
return;
}
createSonarSymbolTable(tree);
} else {
SemanticModel.handleMissingTypes(tree);
}
}
JavaFileScannerContext javaFileScannerContext = createScannerContext(tree, semanticModel, sonarComponents, fileParsed);
// Symbolic execution checks
if (symbolicExecutionEnabled && isNotJavaLangOrSerializable(PackageUtils.packageName(tree.packageDeclaration(), "/"))) {
runScanner(javaFileScannerContext, new SymbolicExecutionVisitor(executableScanners, behaviorCache), AnalysisError.Kind.SE_ERROR);
behaviorCache.cleanup();
}
executableScanners.forEach(scanner -> runScanner(javaFileScannerContext, scanner, AnalysisError.Kind.CHECK_ERROR));
if (semanticModel != null) {
classesNotFound.addAll(semanticModel.classesNotFound());
}
}
use of org.sonar.java.se.SymbolicExecutionVisitor in project sonar-java by SonarSource.
the class HappyPathYieldTest method test_toString.
@Test
public void test_toString() throws Exception {
SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/HappyPathYields.java");
Set<String> yieldsToString = getMethodBehavior(sev, "bar").yields().stream().map(MethodYield::toString).collect(Collectors.toSet());
assertThat(yieldsToString).contains("{params: [[TRUE], [NOT_NULL]], result: null (-1)}", "{params: [[FALSE], []], result: null (-1)}");
}
use of org.sonar.java.se.SymbolicExecutionVisitor in project sonar-java by SonarSource.
the class MethodYieldTest method calling_varargs_method_with_no_arg.
@Test
public void calling_varargs_method_with_no_arg() throws Exception {
SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/VarArgsWithNoArgYield.java");
MethodBehavior mb = getMethodBehavior(sev, "toArr");
List<MethodYield> yields = mb.yields();
assertThat(yields).hasSize(1);
assertThat(mb.isMethodVarArgs()).isTrue();
}
use of org.sonar.java.se.SymbolicExecutionVisitor in project sonar-java by SonarSource.
the class MethodYieldTest method test_yield_on_reassignments.
@Test
public void test_yield_on_reassignments() throws Exception {
SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/XProcYieldsReassignments.java");
MethodBehavior mb = getMethodBehavior(sev, "foo");
assertThat(mb.happyPathYields()).allMatch(y -> y.parametersConstraints.get(0) != null && !ObjectConstraint.NULL.equals(y.parametersConstraints.get(0).get(ObjectConstraint.class)));
}
use of org.sonar.java.se.SymbolicExecutionVisitor in project sonar-java by SonarSource.
the class MethodYieldTest method constraints_on_varargs.
@Test
public void constraints_on_varargs() throws Exception {
ActionParser<Tree> p = JavaParser.createParser();
CompilationUnitTree cut = (CompilationUnitTree) p.parse(new File("src/test/files/se/VarArgsYields.java"));
SemanticModel semanticModel = SemanticModel.createFor(cut, new SquidClassLoader(new ArrayList<>()));
SymbolicExecutionVisitor sev = new SymbolicExecutionVisitor(Lists.newArrayList(new SECheck[] {}), new BehaviorCache(new SquidClassLoader(new ArrayList<>())));
JavaFileScannerContext context = mock(JavaFileScannerContext.class);
when(context.getTree()).thenReturn(cut);
when(context.getSemanticModel()).thenReturn(semanticModel);
sev.scanFile(context);
MethodSymbol methodSymbol = ((MethodTree) ((ClassTree) cut.types().get(0)).members().get(0)).symbol();
MethodBehavior mb = getMethodBehavior(sev, "varArgMethod");
List<MethodYield> yields = mb.yields();
assertThat(yields).hasSize(5);
assertThat(mb.exceptionalPathYields()).hasSize(4);
MethodYield yield = mb.happyPathYields().findFirst().get();
// check that we have NOT_NULL constraint on the first argument
assertThat(yield.parametersConstraints.get(0).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
// check that we have NOT_NULL constraint on the variadic argument
assertThat(yield.parametersConstraints.get(1).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
List<IdentifierTree> usages = methodSymbol.usages();
assertThat(usages).hasSize(6);
List<List<Type>> arguments = usages.stream().map(MethodYieldTest::getMethodIncoationArgumentsTypes).collect(Collectors.toList());
ProgramState ps = ProgramState.EMPTY_STATE;
ProgramState psResult;
SymbolicValue svFirstArg = new SymbolicValue();
SymbolicValue svVarArg1 = new SymbolicValue();
SymbolicValue svVarArg2 = new SymbolicValue();
SymbolicValue svResult = new SymbolicValue();
// apply constraint NotNull to parameter
Collection<ProgramState> arrayOfA = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg, svVarArg1), arguments.get(0), ps, () -> svResult).collect(Collectors.toList());
assertThat(arrayOfA).hasSize(1);
psResult = arrayOfA.iterator().next();
assertThat(psResult.getConstraint(svFirstArg, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
assertThat(psResult.getConstraint(svVarArg1, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
// apply constraint NotNull to parameter (B[] is a subtype of A[])
Collection<ProgramState> arrayOfB = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg, svVarArg1), arguments.get(1), ps, () -> svResult).collect(Collectors.toList());
assertThat(arrayOfB).hasSize(1);
psResult = arrayOfB.iterator().next();
assertThat(psResult.getConstraint(svFirstArg, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
assertThat(psResult.getConstraint(svVarArg1, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
// no constraint, as 'a' is not an array
Collection<ProgramState> objectA = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg, svVarArg1), arguments.get(2), ps, () -> svResult).collect(Collectors.toList());
assertThat(objectA).hasSize(1);
psResult = objectA.iterator().next();
assertThat(psResult.getConstraint(svFirstArg, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
assertThat(psResult.getConstraint(svVarArg1, ObjectConstraint.class)).isNull();
// no constraint, as 'a' and 'b' can not receive the constraint of the array
Collection<ProgramState> objectsAandB = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg, svVarArg1, svVarArg2), arguments.get(3), ps, () -> svResult).collect(Collectors.toList());
assertThat(objectsAandB).hasSize(1);
psResult = objectsAandB.iterator().next();
assertThat(psResult.getConstraint(svFirstArg, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
assertThat(psResult.getConstraint(svVarArg1, ObjectConstraint.class)).isNull();
assertThat(psResult.getConstraint(svVarArg2, ObjectConstraint.class)).isNull();
// no param, we only learn something about the argument which is not variadic
Collection<ProgramState> noParam = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg), arguments.get(4), ps, () -> svResult).collect(Collectors.toList());
assertThat(noParam).hasSize(1);
psResult = noParam.iterator().next();
assertThat(psResult.getConstraint(svFirstArg, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
// null param, contradiction, no resulting program state
ps = ProgramState.EMPTY_STATE.addConstraint(svFirstArg, ObjectConstraint.NULL);
Collection<ProgramState> nullParam = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg, svVarArg1), arguments.get(5), ps, () -> svResult).collect(Collectors.toList());
assertThat(nullParam).isEmpty();
}
Aggregations