コード例 #1
0
        public void ProgramPoint_Diff_Block()
        {
            var pp1 = new ProgramPoint(new TestBlock(), 1);
            var pp2 = new ProgramPoint(new TestBlock(), 1);

            Assert.AreNotEqual(pp1, pp2);
            Assert.AreNotEqual(pp1.GetHashCode(), pp2.GetHashCode());
        }
コード例 #2
0
        public void ProgramPoint_Equivalence()
        {
            var block = new TestBlock();
            var pp1 = new ProgramPoint(block, 1);
            var pp2 = new ProgramPoint(block, 1);

            Assert.AreEqual(pp1, pp2);
            Assert.AreEqual(pp1.GetHashCode(), pp2.GetHashCode());
        }
コード例 #3
0
            public override ProgramState PreProcessInstruction(ProgramPoint programPoint, ProgramState programState)
            {
                var instruction = programPoint.Block.Instructions[programPoint.Offset];

                switch (instruction.Kind())
                {
                case SyntaxKind.IdentifierName:
                    return(ProcessIdentifier(programPoint, programState, (IdentifierNameSyntax)instruction));

                case SyntaxKind.AwaitExpression:
                    return(ProcessAwait(programState, (AwaitExpressionSyntax)instruction));

                case SyntaxKind.SimpleMemberAccessExpression:
                case SyntaxKind.PointerMemberAccessExpression:
                    return(ProcessMemberAccess(programState, (MemberAccessExpressionSyntax)instruction));

                case SyntaxKind.ElementAccessExpression:
                    return(ProcessElementAccess(programState, (ElementAccessExpressionSyntax)instruction));

                default:
                    return(programState);
                }
            }
コード例 #4
0
        /// <summary>
        /// Add Program control point info
        /// </summary>
        /// <param name="program">Program Point</param>
        /// <param name="index">Index</param>
        public void Add(ProgramPoint program, int index)
        {
            try
            {
                ControlPointInfo newCPInfo = new ControlPointInfo
                {
                    ControlPointName = "PRG" + index,
                    Label            = program.Label,
                    FullLabel        = program.Description,
                    Type             = IdentifierTypes.PRGS,
                    Value            = "",
                    Units            = "",
                    AutoManual       = program.AutoManual == 0 ? "Auto" : "Manual",
                    Index            = (short)index
                };


                Programs.Add(newCPInfo);
            }
            catch (Exception ex)
            {
                ExceptionHandler.Show(ex, "Addition of new Program to ControlPointsInfo");
            }
        }
コード例 #5
0
 private static bool IsSuccessorForeachBranch(ProgramPoint programPoint) =>
 programPoint.Block.SuccessorBlocks.First() is BinaryBranchBlock successorBlock &&
コード例 #6
0
 private static bool IsSuccessorForeachBranch(ProgramPoint programPoint)
 {
     return(programPoint.Block.SuccessorBlocks.First() is BinaryBranchBlock successorBlock &&
            successorBlock.BranchingNode.IsKind(SyntaxKind.ForEachStatement));
 }
コード例 #7
0
 /// <inheritdoc />
 public override int GetHashCode()
 {
     return(Message.GetHashCode() + LangElement.Position.FirstOffset.GetHashCode() + FullFileName.GetHashCode()
            + Flag.GetHashCode() + ProgramPoint.GetHashCode());
 }
コード例 #8
0
        private void VisitInstruction(Node node)
        {
            var instruction     = node.ProgramPoint.Block.Instructions[node.ProgramPoint.Offset];
            var newProgramPoint = new ProgramPoint(node.ProgramPoint.Block, node.ProgramPoint.Offset + 1);
            var currentState    = node.ProgramState;

            switch (instruction.Kind())
            {
            case SyntaxKind.VariableDeclarator:
            {
                var declarator = (VariableDeclaratorSyntax)instruction;
                var leftSymbol = semanticModel.GetDeclaredSymbol(declarator);

                if (leftSymbol == null)
                {
                    break;
                }

                ISymbol           rightSymbol = null;
                Optional <object> constValue  = null;
                if (declarator.Initializer?.Value != null)
                {
                    rightSymbol = semanticModel.GetSymbolInfo(declarator.Initializer.Value).Symbol;
                    constValue  = semanticModel.GetConstantValue(declarator.Initializer.Value);
                }

                currentState = GetNewProgramStateForAssignment(node.ProgramState, leftSymbol, rightSymbol, constValue);
            }
            break;

            case SyntaxKind.SimpleAssignmentExpression:
            {
                var assignment = (AssignmentExpressionSyntax)instruction;
                var leftSymbol = semanticModel.GetSymbolInfo(assignment.Left).Symbol;

                if (IsLocalScoped(leftSymbol))
                {
                    var rightSymbol = semanticModel.GetSymbolInfo(assignment.Right).Symbol;
                    var constValue  = semanticModel.GetConstantValue(assignment.Right);

                    currentState = GetNewProgramStateForAssignment(node.ProgramState, leftSymbol, rightSymbol, constValue);
                }
            }
            break;

            case SyntaxKind.IdentifierName:
            {
                var identifier    = (IdentifierNameSyntax)instruction;
                var parenthesized = identifier.GetSelfOrTopParenthesizedExpression();
                var argument      = parenthesized.Parent as ArgumentSyntax;
                if (argument == null ||
                    argument.RefOrOutKeyword.IsKind(SyntaxKind.None))
                {
                    break;
                }

                var symbol = semanticModel.GetSymbolInfo(identifier).Symbol;

                if (IsLocalScoped(symbol))
                {
                    currentState = GetNewProgramStateForAssignment(node.ProgramState, symbol);
                }
            }
            break;

            default:
                break;
            }

            EnqueueNewNode(newProgramPoint, currentState);
            OnInstructionProcessed(instruction, node.ProgramPoint, currentState);
        }
コード例 #9
0
            public override ProgramState PreProcessInstruction(ProgramPoint programPoint, ProgramState programState)
            {
                var instruction = programPoint.Block.Instructions[programPoint.Offset];

                return instruction.IsKind(SyntaxKind.CastExpression)
                    ? ProcessCastAccess(programState, (CastExpressionSyntax)instruction)
                    : programState;
            }
コード例 #10
0
 public override ProgramState PreProcessInstruction(ProgramPoint programPoint, ProgramState programState) =>
 programPoint.CurrentInstruction is InvocationExpressionSyntax invocation
コード例 #11
0
 private static bool IsSuccessorForeachBranch(ProgramPoint programPoint)
 {
     var successorBlock = programPoint.Block.SuccessorBlocks.First() as BinaryBranchBlock;
     return successorBlock != null &&
         successorBlock.BranchingNode.IsKind(SyntaxKind.ForEachStatement);
 }
コード例 #12
0
            private ProgramState ProcessIdentifier(ProgramPoint programPoint, ProgramState programState, IdentifierNameSyntax identifier)
            {
                if (programPoint.Block.Instructions.Last() != identifier ||
                    programPoint.Block.SuccessorBlocks.Count != 1 ||
                    (!IsSuccessorForeachBranch(programPoint) && !IsExceptionThrow(identifier)))
                {
                    return programState;
                }

                var symbol = semanticModel.GetSymbolInfo(identifier).Symbol;
                return ProcessIdentifier(programState, identifier, symbol);
            }
コード例 #13
0
            public override ProgramState PreProcessInstruction(ProgramPoint programPoint, ProgramState programState)
            {
                var instruction = programPoint.Block.Instructions[programPoint.Offset];
                switch (instruction.Kind())
                {
                    case SyntaxKind.IdentifierName:
                        return ProcessIdentifier(programPoint, programState, (IdentifierNameSyntax)instruction);

                    case SyntaxKind.AwaitExpression:
                        return ProcessAwait(programState, (AwaitExpressionSyntax)instruction);

                    case SyntaxKind.SimpleMemberAccessExpression:
                    case SyntaxKind.PointerMemberAccessExpression:
                        return ProcessMemberAccess(programState, (MemberAccessExpressionSyntax)instruction);

                    case SyntaxKind.ElementAccessExpression:
                        return ProcessElementAccess(programState, (ElementAccessExpressionSyntax)instruction);

                    default:
                        return programState;
                }
            }
コード例 #14
0
 public Node(ProgramPoint programPoint, ProgramState programState)
 {
     ProgramState = programState;
     ProgramPoint = programPoint;
 }
コード例 #15
0
        protected override void VisitInstruction(ExplodedGraphNode node)
        {
            var instruction             = node.ProgramPoint.Block.Instructions[node.ProgramPoint.Offset];
            var expression              = instruction as ExpressionSyntax;
            var parenthesizedExpression = expression?.GetSelfOrTopParenthesizedExpression();
            var newProgramPoint         = new ProgramPoint(node.ProgramPoint.Block, node.ProgramPoint.Offset + 1);
            var newProgramState         = node.ProgramState;

            foreach (var explodedGraphCheck in explodedGraphChecks)
            {
                newProgramState = explodedGraphCheck.PreProcessInstruction(node.ProgramPoint, newProgramState);
                if (newProgramState == null)
                {
                    return;
                }
            }

            switch (instruction.Kind())
            {
            case SyntaxKind.VariableDeclarator:
                newProgramState = VisitVariableDeclarator((VariableDeclaratorSyntax)instruction, newProgramState);
                break;

            case SyntaxKind.SimpleAssignmentExpression:
                newProgramState = VisitSimpleAssignment((AssignmentExpressionSyntax)instruction, newProgramState);
                break;

            case SyntaxKind.OrAssignmentExpression:
                newProgramState = VisitBooleanBinaryOpAssignment(newProgramState, (AssignmentExpressionSyntax)instruction, (l, r) => new OrSymbolicValue(l, r));
                break;

            case SyntaxKind.AndAssignmentExpression:
                newProgramState = VisitBooleanBinaryOpAssignment(newProgramState, (AssignmentExpressionSyntax)instruction, (l, r) => new AndSymbolicValue(l, r));
                break;

            case SyntaxKind.ExclusiveOrAssignmentExpression:
                newProgramState = VisitBooleanBinaryOpAssignment(newProgramState, (AssignmentExpressionSyntax)instruction, (l, r) => new XorSymbolicValue(l, r));
                break;

            case SyntaxKind.SubtractAssignmentExpression:
            case SyntaxKind.AddAssignmentExpression:
            case SyntaxKind.DivideAssignmentExpression:
            case SyntaxKind.MultiplyAssignmentExpression:
            case SyntaxKind.ModuloAssignmentExpression:

            case SyntaxKind.LeftShiftAssignmentExpression:
            case SyntaxKind.RightShiftAssignmentExpression:
                newProgramState = VisitOpAssignment((AssignmentExpressionSyntax)instruction, newProgramState);
                break;

            case SyntaxKind.PreIncrementExpression:
            case SyntaxKind.PreDecrementExpression:
                newProgramState = VisitPrefixIncrement((PrefixUnaryExpressionSyntax)instruction, newProgramState);
                break;

            case SyntaxKind.PostIncrementExpression:
            case SyntaxKind.PostDecrementExpression:
                newProgramState = VisitPostfixIncrement((PostfixUnaryExpressionSyntax)instruction, newProgramState);
                break;

            case SyntaxKind.IdentifierName:
                newProgramState = VisitIdentifier((IdentifierNameSyntax)instruction, newProgramState);
                break;

            case SyntaxKind.BitwiseOrExpression:
                newProgramState = VisitBinaryOperator(newProgramState, (l, r) => new OrSymbolicValue(l, r));
                break;

            case SyntaxKind.BitwiseAndExpression:
                newProgramState = VisitBinaryOperator(newProgramState, (l, r) => new AndSymbolicValue(l, r));
                break;

            case SyntaxKind.ExclusiveOrExpression:
                newProgramState = VisitBinaryOperator(newProgramState, (l, r) => new XorSymbolicValue(l, r));
                break;

            case SyntaxKind.LessThanExpression:
                newProgramState = VisitComparisonBinaryOperator(newProgramState, (BinaryExpressionSyntax)instruction, (l, r) => new ComparisonSymbolicValue(ComparisonKind.Less, l, r));
                break;

            case SyntaxKind.LessThanOrEqualExpression:
                newProgramState = VisitComparisonBinaryOperator(newProgramState, (BinaryExpressionSyntax)instruction, (l, r) => new ComparisonSymbolicValue(ComparisonKind.LessOrEqual, l, r));
                break;

            case SyntaxKind.GreaterThanExpression:
                newProgramState = VisitComparisonBinaryOperator(newProgramState, (BinaryExpressionSyntax)instruction, (l, r) => new ComparisonSymbolicValue(ComparisonKind.Less, r, l));
                break;

            case SyntaxKind.GreaterThanOrEqualExpression:
                newProgramState = VisitComparisonBinaryOperator(newProgramState, (BinaryExpressionSyntax)instruction, (l, r) => new ComparisonSymbolicValue(ComparisonKind.LessOrEqual, r, l));
                break;

            case SyntaxKind.SubtractExpression:
            case SyntaxKind.AddExpression:
            case SyntaxKind.DivideExpression:
            case SyntaxKind.MultiplyExpression:
            case SyntaxKind.ModuloExpression:

            case SyntaxKind.LeftShiftExpression:
            case SyntaxKind.RightShiftExpression:

                newProgramState = newProgramState.PopValues(2);
                newProgramState = newProgramState.PushValue(new SymbolicValue());
                break;

            case SyntaxKind.EqualsExpression:
                var binary = (BinaryExpressionSyntax)instruction;
                newProgramState = IsOperatorOnObject(instruction)
                        ? VisitReferenceEquals(binary, newProgramState)
                        : VisitValueEquals(newProgramState);

                break;

            case SyntaxKind.NotEqualsExpression:
                newProgramState = IsOperatorOnObject(instruction)
                        ? VisitBinaryOperator(newProgramState, (l, r) => new ReferenceNotEqualsSymbolicValue(l, r))
                        : VisitBinaryOperator(newProgramState, (l, r) => new ValueNotEqualsSymbolicValue(l, r));
                break;

            case SyntaxKind.BitwiseNotExpression:
            case SyntaxKind.UnaryMinusExpression:
            case SyntaxKind.UnaryPlusExpression:
            case SyntaxKind.AddressOfExpression:
            case SyntaxKind.PointerIndirectionExpression:

            case SyntaxKind.MakeRefExpression:
            case SyntaxKind.RefTypeExpression:
            case SyntaxKind.RefValueExpression:

            case SyntaxKind.MemberBindingExpression:

            case SyntaxKind.AwaitExpression:
                newProgramState = newProgramState.PopValue();
                newProgramState = newProgramState.PushValue(new SymbolicValue());
                break;

            case SyntaxKind.AsExpression:
            case SyntaxKind.IsExpression:
                newProgramState = VisitSafeCastExpression((BinaryExpressionSyntax)instruction, newProgramState);
                break;

            case SyntaxKind.SimpleMemberAccessExpression:
            {
                var memberAccess = (MemberAccessExpressionSyntax)instruction;
                var check        = explodedGraphChecks.OfType <EmptyNullableValueAccess.NullValueAccessedCheck>().FirstOrDefault();
                if (check == null ||
                    !check.TryProcessInstruction(memberAccess, newProgramState, out newProgramState))
                {
                    // Default behavior
                    newProgramState = VisitMemberAccess(memberAccess, newProgramState);
                }
            }
            break;

            case SyntaxKind.PointerMemberAccessExpression:
            {
                newProgramState = VisitMemberAccess((MemberAccessExpressionSyntax)instruction, newProgramState);
            }
            break;

            case SyntaxKind.GenericName:
            case SyntaxKind.AliasQualifiedName:
            case SyntaxKind.QualifiedName:

            case SyntaxKind.PredefinedType:
            case SyntaxKind.NullableType:

            case SyntaxKind.OmittedArraySizeExpression:

            case SyntaxKind.AnonymousMethodExpression:
            case SyntaxKind.ParenthesizedLambdaExpression:
            case SyntaxKind.SimpleLambdaExpression:
            case SyntaxKind.QueryExpression:

            case SyntaxKind.ArgListExpression:
                newProgramState = newProgramState.PushValue(new SymbolicValue());
                break;

            case SyntaxKind.LogicalNotExpression:
            {
                SymbolicValue sv;
                newProgramState = newProgramState.PopValue(out sv);
                newProgramState = newProgramState.PushValue(new LogicalNotSymbolicValue(sv));
            }
            break;

            case SyntaxKind.TrueLiteralExpression:
                newProgramState = newProgramState.PushValue(SymbolicValue.True);
                break;

            case SyntaxKind.FalseLiteralExpression:
                newProgramState = newProgramState.PushValue(SymbolicValue.False);
                break;

            case SyntaxKind.NullLiteralExpression:
                newProgramState = newProgramState.PushValue(SymbolicValue.Null);
                break;

            case SyntaxKind.ThisExpression:
                newProgramState = newProgramState.PushValue(SymbolicValue.This);
                break;

            case SyntaxKind.BaseExpression:
                newProgramState = newProgramState.PushValue(SymbolicValue.Base);
                break;

            case SyntaxKind.CharacterLiteralExpression:
            case SyntaxKind.StringLiteralExpression:
            case SyntaxKind.NumericLiteralExpression:

            case SyntaxKind.SizeOfExpression:
            case SyntaxKind.TypeOfExpression:

            case SyntaxKind.ArrayCreationExpression:
            case SyntaxKind.ImplicitArrayCreationExpression:
            case SyntaxKind.StackAllocArrayCreationExpression:
            {
                var sv = new SymbolicValue();
                newProgramState = sv.SetConstraint(ObjectConstraint.NotNull, newProgramState);
                newProgramState = newProgramState.PushValue(sv);
            }
            break;

            case SyntaxKind.DefaultExpression:
            {
                var sv = new SymbolicValue();
                newProgramState = SetNonNullConstraintIfValueType(instruction, sv, newProgramState);
                newProgramState = newProgramState.PushValue(sv);
            }
            break;

            case SyntaxKind.AnonymousObjectCreationExpression:
            {
                var creation = (AnonymousObjectCreationExpressionSyntax)instruction;
                newProgramState = newProgramState.PopValues(creation.Initializers.Count);

                var sv = new SymbolicValue();
                newProgramState = sv.SetConstraint(ObjectConstraint.NotNull, newProgramState);
                newProgramState = newProgramState.PushValue(sv);
            }
            break;

            case SyntaxKind.CastExpression:

            case SyntaxKind.CheckedExpression:
            case SyntaxKind.UncheckedExpression:
                // Do nothing
                break;

            case SyntaxKind.InterpolatedStringExpression:
                newProgramState = newProgramState.PopValues(((InterpolatedStringExpressionSyntax)instruction).Contents.OfType <InterpolationSyntax>().Count());
                newProgramState = newProgramState.PushValue(new SymbolicValue());
                break;

            case SyntaxKind.ObjectCreationExpression:
                newProgramState = VisitObjectCreation((ObjectCreationExpressionSyntax)instruction, newProgramState);
                break;

            case SyntaxKind.ElementAccessExpression:
                newProgramState = newProgramState.PopValues((((ElementAccessExpressionSyntax)instruction).ArgumentList?.Arguments.Count ?? 0) + 1);
                newProgramState = newProgramState.PushValue(new SymbolicValue());
                break;

            case SyntaxKind.ImplicitElementAccess:
                newProgramState = newProgramState
                                  .PopValues(((ImplicitElementAccessSyntax)instruction).ArgumentList?.Arguments.Count ?? 0)
                                  .PushValue(new SymbolicValue());
                break;

            case SyntaxKind.ObjectInitializerExpression:
            case SyntaxKind.ArrayInitializerExpression:
            case SyntaxKind.CollectionInitializerExpression:
            case SyntaxKind.ComplexElementInitializerExpression:
                newProgramState = VisitInitializer(instruction, parenthesizedExpression, newProgramState);
                break;

            case SyntaxKind.ArrayType:
                newProgramState = newProgramState.PopValues(((ArrayTypeSyntax)instruction).RankSpecifiers.SelectMany(rs => rs.Sizes).Count());
                break;

            case SyntaxKind.ElementBindingExpression:
                newProgramState = newProgramState.PopValues(((ElementBindingExpressionSyntax)instruction).ArgumentList?.Arguments.Count ?? 0);
                newProgramState = newProgramState.PushValue(new SymbolicValue());
                break;

            case SyntaxKind.InvocationExpression:
            {
                var invocation        = (InvocationExpressionSyntax)instruction;
                var invocationVisitor = new InvocationVisitor(invocation, SemanticModel, newProgramState);
                newProgramState = invocationVisitor.ProcessInvocation();

                if (invocation.Expression.IsOnThis() && !invocation.IsNameof(SemanticModel))
                {
                    newProgramState = newProgramState.RemoveSymbols(IsFieldSymbol);
                }
            }
            break;

            default:
                throw new NotImplementedException($"{instruction.Kind()}");
            }

            newProgramState = EnsureStackState(parenthesizedExpression, newProgramState);
            OnInstructionProcessed(instruction, node.ProgramPoint, newProgramState);
            EnqueueNewNode(newProgramPoint, newProgramState);
        }
コード例 #16
0
 public virtual ProgramState PreProcessInstruction(ProgramPoint programPoint, ProgramState programState)
 {
     return programState;
 }
コード例 #17
0
 public override ProgramState PreProcessInstruction(ProgramPoint programPoint, ProgramState programState)
 {
     return(!(programPoint.Block.Instructions[programPoint.Offset] is InvocationExpressionSyntax instruction)
         ? programState
         : VisitInvocationExpression(instruction, programState));
 }
コード例 #18
0
 public override ProgramState PostProcessInstruction(ProgramPoint programPoint, ProgramState programState) =>
 programPoint.CurrentInstruction switch
 {