Пример #1
0
        public void TestIsInstance()
        {
            Assert.False(DeclarationPatternSyntaxWrapper.IsInstance(null));
            Assert.False(DeclarationPatternSyntaxWrapper.IsInstance(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)));

            var syntaxNode = SyntaxFactory.DeclarationPattern(
                SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword)),
                SyntaxFactory.DiscardDesignation());

            Assert.True(DeclarationPatternSyntaxWrapper.IsInstance(syntaxNode));
        }
 public void TestIsInstance()
 {
     Assert.False(DeclarationPatternSyntaxWrapper.IsInstance(null));
     Assert.False(DeclarationPatternSyntaxWrapper.IsInstance(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)));
 }
        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;

            newProgramState = InvokeChecks(newProgramState, (ps, check) => check.PreProcessInstruction(node.ProgramPoint, ps));
            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:
                newProgramState = newProgramState.PopValue();
                newProgramState = newProgramState.PushValue(new SymbolicValue());
                break;

            case SyntaxKind.AwaitExpression:
                newProgramState = newProgramState.RemoveSymbols(IsFieldSymbol);
                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        = this.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:
            {
                newProgramState = newProgramState.PopValue(out var 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 = newProgramState.SetConstraint(sv, ObjectConstraint.NotNull);
                newProgramState = newProgramState.PushValue(sv);
                newProgramState = InvokeChecks(newProgramState,
                                               (ps, check) => check.ObjectCreated(ps, sv, instruction));
            }
            break;

            case SyntaxKind.DefaultExpression:
                newProgramState = VisitDefaultExpression((DefaultExpressionSyntax)instruction, newProgramState);
                break;

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

                var sv = new SymbolicValue();
                newProgramState = newProgramState.SetConstraint(sv, ObjectConstraint.NotNull);
                newProgramState = newProgramState.PushValue(sv);
                newProgramState = InvokeChecks(newProgramState,
                                               (ps, check) => check.ObjectCreated(ps, sv, instruction));
            }
            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.IsNameof(SemanticModel))
                {
                    newProgramState = newProgramState.RemoveSymbols(IsFieldSymbol);
                }
            }
            break;

            case SyntaxKind.BaseConstructorInitializer:
            case SyntaxKind.ThisConstructorInitializer:
                var constructorInitializer = (ConstructorInitializerSyntax)instruction;
                if (constructorInitializer.ArgumentList != null)
                {
                    var ctorInifializerArgumentsCount = constructorInitializer.ArgumentList.Arguments.Count;
                    newProgramState = newProgramState.PopValues(ctorInifializerArgumentsCount);
                }
                break;

            case SyntaxKindEx.IsPatternExpression:
                // condition with pattern (if, do, while, for, etc.) "if (x is string s)" or "if (x is null)"
                var isPatternExpression = (IsPatternExpressionSyntaxWrapper)instruction;
                if (ConstantPatternSyntaxWrapper.IsInstance(isPatternExpression.Pattern))
                {
                    // "x is null" is equivalent to "x == null"
                    newProgramState = VisitValueEquals(newProgramState);
                }
                else if (DeclarationPatternSyntaxWrapper.IsInstance(isPatternExpression.Pattern))
                {
                    // "x is string s"
                    // VisitDeclarationPattern() expects SV_s on top of the stack, hence we pop SV_x
                    newProgramState = newProgramState.PopValue();

                    newProgramState = VisitDeclarationPattern((DeclarationPatternSyntaxWrapper)isPatternExpression.Pattern, newProgramState);
                }
                else
                {
                    throw new NotSupportedException($"{instruction.Kind()}");
                }
                break;

            case SyntaxKindEx.DeclarationPattern:
                // a pattern from a case section "string s"
                newProgramState = VisitDeclarationPattern((DeclarationPatternSyntaxWrapper)instruction, newProgramState);
                break;

            case SyntaxKindEx.ConstantPattern:
                // The 0 in 'case 0 when ...'
                // Do nothing
                break;

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

            newProgramState = EnsureStackState(parenthesizedExpression, newProgramState);
            OnInstructionProcessed(instruction, node.ProgramPoint, newProgramState);
            EnqueueNewNode(newProgramPoint, newProgramState);
        }