Search in sources :

Example 11 with CFG

use of org.sonar.java.cfg.CFG in project sonar-java by SonarSource.

the class ParameterReassignedToCheck method visitMethod.

@Override
public void visitMethod(MethodTree tree) {
    BlockTree block = tree.block();
    if (block == null) {
        return;
    }
    CFG cfg = CFG.build(tree);
    LiveVariables analyze = LiveVariables.analyze(cfg);
    Set<Symbol> live = analyze.getIn(cfg.entry());
    for (VariableTree parameterTree : tree.parameters()) {
        if (!live.contains(parameterTree.symbol())) {
            variables.add(parameterTree.symbol());
        }
    }
    super.visitMethod(tree);
    for (VariableTree parameterTree : tree.parameters()) {
        if (!live.contains(parameterTree.symbol())) {
            variables.remove(parameterTree.symbol());
        }
    }
}
Also used : CFG(org.sonar.java.cfg.CFG) LiveVariables(org.sonar.java.cfg.LiveVariables) Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 12 with CFG

use of org.sonar.java.cfg.CFG in project sonar-java by SonarSource.

the class ParameterReassignedToCheck method visitCatch.

@Override
public void visitCatch(CatchTree tree) {
    CFG cfg = CFG.buildCFG(tree.block().body(), true);
    Symbol var = tree.parameter().symbol();
    boolean liveVar = true;
    if (var.owner().isMethodSymbol()) {
        cfg.setMethodSymbol((Symbol.MethodSymbol) var.owner());
        LiveVariables analyze = LiveVariables.analyze(cfg);
        Set<Symbol> live = analyze.getIn(cfg.entry());
        liveVar = live.contains(var);
    }
    if (!liveVar) {
        variables.add(var);
    }
    super.visitCatch(tree);
    if (!liveVar) {
        variables.remove(var);
    }
}
Also used : CFG(org.sonar.java.cfg.CFG) LiveVariables(org.sonar.java.cfg.LiveVariables) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 13 with CFG

use of org.sonar.java.cfg.CFG in project sonar-java by SonarSource.

the class PrivateFieldUsedLocallyCheck method isLiveInMethodEntry.

private static boolean isLiveInMethodEntry(Symbol privateFieldSymbol, MethodTree methodTree) {
    CFG cfg = CFG.build(methodTree);
    LiveVariables liveVariables = LiveVariables.analyzeWithFields(cfg);
    return liveVariables.getIn(cfg.entry()).contains(privateFieldSymbol);
}
Also used : CFG(org.sonar.java.cfg.CFG) LiveVariables(org.sonar.java.cfg.LiveVariables)

Example 14 with CFG

use of org.sonar.java.cfg.CFG in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method test_enqueuing_exceptional_yields2.

@Test
public void test_enqueuing_exceptional_yields2() {
    BytecodeCFG cfg = SETestUtils.bytecodeCFG(TRY_WRONG_CATCH_SIGNATURE, squidClassLoader);
    BytecodeCFG.Block b2 = cfg.blocks().get(2);
    walker.programState = ProgramState.EMPTY_STATE.stackValue(new SymbolicValue()).stackValue(new SymbolicValue());
    walker.programPosition = new ProgramPoint(b2).next().next();
    walker.executeInstruction(b2.elements().get(3));
    assertThat(walker.workList).hasSize(3);
    assertThat(walker.workList.pop().programState.exitValue()).isNotNull().isInstanceOf(SymbolicValue.ExceptionalSymbolicValue.class).extracting(sv -> ((SymbolicValue.ExceptionalSymbolicValue) sv).exceptionType().fullyQualifiedName()).containsExactly("java.lang.IllegalStateException");
    assertThat(walker.workList.pop().programState.exitValue()).isNull();
}
Also used : ASTORE(org.objectweb.asm.Opcodes.ASTORE) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SETestUtils(org.sonar.java.se.SETestUtils) DMUL(org.objectweb.asm.Opcodes.DMUL) LSUB(org.objectweb.asm.Opcodes.LSUB) MethodYield(org.sonar.java.se.xproc.MethodYield) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) LSHR(org.objectweb.asm.Opcodes.LSHR) DSUB(org.objectweb.asm.Opcodes.DSUB) LMUL(org.objectweb.asm.Opcodes.LMUL) Mockito.doThrow(org.mockito.Mockito.doThrow) LSHL(org.objectweb.asm.Opcodes.LSHL) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Collectors(java.util.stream.Collectors) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) LAND(org.objectweb.asm.Opcodes.LAND) List(java.util.List) LXOR(org.objectweb.asm.Opcodes.LXOR) Instruction(org.sonar.java.bytecode.cfg.Instruction) GOTO(org.objectweb.asm.Opcodes.GOTO) Constraint(org.sonar.java.se.constraint.Constraint) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) LUSHR(org.objectweb.asm.Opcodes.LUSHR) BeforeClass(org.junit.BeforeClass) ProgramState(org.sonar.java.se.ProgramState) Label(org.objectweb.asm.Label) Mockito.spy(org.mockito.Mockito.spy) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ArrayList(java.util.ArrayList) Instructions(org.sonar.java.bytecode.cfg.Instructions) DivisionByZeroCheck(org.sonar.java.se.checks.DivisionByZeroCheck) ISTORE(org.objectweb.asm.Opcodes.ISTORE) INVOKESTATIC(org.objectweb.asm.Opcodes.INVOKESTATIC) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) Before(org.junit.Before) JavaParser(org.sonar.java.ast.parser.JavaParser) LOR(org.objectweb.asm.Opcodes.LOR) ICONST_4(org.objectweb.asm.Opcodes.ICONST_4) Opcodes(org.objectweb.asm.Opcodes) DREM(org.objectweb.asm.Opcodes.DREM) ICONST_3(org.objectweb.asm.Opcodes.ICONST_3) ICONST_2(org.objectweb.asm.Opcodes.ICONST_2) ICONST_1(org.objectweb.asm.Opcodes.ICONST_1) ICONST_0(org.objectweb.asm.Opcodes.ICONST_0) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DADD(org.objectweb.asm.Opcodes.DADD) Type(org.sonar.plugins.java.api.semantic.Type) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) File(java.io.File) LADD(org.objectweb.asm.Opcodes.LADD) CFG(org.sonar.java.cfg.CFG) Printer(org.objectweb.asm.util.Printer) RETURN(org.objectweb.asm.Opcodes.RETURN) SemanticModel(org.sonar.java.resolve.SemanticModel) LREM(org.objectweb.asm.Opcodes.LREM) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) ILOAD(org.objectweb.asm.Opcodes.ILOAD) ProgramPoint(org.sonar.java.se.ProgramPoint) Preconditions(com.google.common.base.Preconditions) DDIV(org.objectweb.asm.Opcodes.DDIV) Collections(java.util.Collections) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) LDIV(org.objectweb.asm.Opcodes.LDIV) ProgramPoint(org.sonar.java.se.ProgramPoint) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) Test(org.junit.Test)

Example 15 with CFG

use of org.sonar.java.cfg.CFG in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method test_tableswitch.

@Test
public void test_tableswitch() throws Exception {
    Instructions instr = new Instructions();
    instr.visitVarInsn(ILOAD, 0);
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    Label l3 = new Label();
    instr.visitTableSwitchInsn(0, 2, l3, new Label[] { l0, l1, l2 });
    instr.visitLabel(l0);
    instr.visitInsn(ICONST_0);
    instr.visitVarInsn(ISTORE, 1);
    instr.visitJumpInsn(GOTO, l3);
    instr.visitLabel(l1);
    instr.visitInsn(ICONST_0);
    instr.visitVarInsn(ISTORE, 2);
    instr.visitJumpInsn(GOTO, l3);
    instr.visitLabel(l2);
    instr.visitInsn(ICONST_0);
    instr.visitVarInsn(ISTORE, 3);
    instr.visitLabel(l3);
    instr.visitInsn(RETURN);
    BytecodeCFG cfg = instr.cfg();
    CFG.IBlock<Instruction> entry = cfg.entry();
    BytecodeEGWalker walker = new BytecodeEGWalker(null, null);
    walker.programState = ProgramState.EMPTY_STATE.stackValue(new SymbolicValue());
    walker.handleBlockExit(new ProgramPoint(entry));
    assertThat(walker.workList).hasSize(entry.successors().size());
    walker.workList.forEach(node -> {
        assertThat(node.programState.peekValue()).isNull();
        assertThat(entry.successors().contains(node.programPoint.block)).isTrue();
    });
}
Also used : BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) CFG(org.sonar.java.cfg.CFG) ProgramPoint(org.sonar.java.se.ProgramPoint) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) Label(org.objectweb.asm.Label) Instructions(org.sonar.java.bytecode.cfg.Instructions) Instruction(org.sonar.java.bytecode.cfg.Instruction) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) Test(org.junit.Test)

Aggregations

CFG (org.sonar.java.cfg.CFG)17 Test (org.junit.Test)7 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)6 LiveVariables (org.sonar.java.cfg.LiveVariables)5 Symbol (org.sonar.plugins.java.api.semantic.Symbol)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)4 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)4 List (java.util.List)3 Set (java.util.Set)3 Label (org.objectweb.asm.Label)3 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)3 Tree (org.sonar.plugins.java.api.tree.Tree)3 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)3 Preconditions (com.google.common.base.Preconditions)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 Collectors (java.util.stream.Collectors)2 SonarComponents (org.sonar.java.SonarComponents)2