예제 #1
0
 override public void VisitBinaryEx(BinaryEx x)
 {
     _serializer.StartSerialize(typeof(BinaryEx).Name, SerializeSpan(x.Span),
                                new NodeObj("Operation", x.Operation.ToString()));
     base.VisitBinaryEx(x);
     _serializer.EndSerialize();
 }
예제 #2
0
 private byte[] FullyQualifiedTable(RegionInfo reg)
 {
     if (reg.Namespace?.Any() != true)
     {
         return(reg.Table);
     }
     return(BinaryEx.ConcatInOrder(reg.Namespace, new[] { ConstByte.Colon }, reg.Table));
 }
예제 #3
0
        /// <inheritdoc />
        override public void VisitBinaryEx(BinaryEx x)
        {
            VisitElement(x.LeftExpr);
            var leftEx = result;

            VisitElement(x.RightExpr);

            result = new BinaryEx(x.PublicOperation, (Expression)leftEx, (Expression)result);
        }
예제 #4
0
        /// <summary>
        /// Connects TrueBranch and FalseBranch to From. TrueBranch is followed from From if the condition holds,
        /// FalseBranch is followed from From if the condition does not hold.
        ///
        /// If decompose is true, it decomposes the condition expression using logical operations with respect to
        /// shortcircuit evaluation.
        /// Note that analyzer now expects that the condition expressions are decomposed and it no longer supports
        /// condition expressions that are not decomposed.
        /// </summary>
        /// <param name="condition">the condition of the branching.</param>
        /// <param name="From">the basic block where from which the branching starts.</param>
        /// <param name="TrueBranch">the branch which is taken if the condition holds.</param>
        /// <param name="FalseBranch">the branch which is taken if the condition does not hold.</param>
        /// <param name="decompose"></param>
        internal static void ConnectConditionalBranching(Expression condition, BasicBlock From, BasicBlock TrueBranch, BasicBlock FalseBranch, bool decompose = true)
        {
            var binaryCondition = condition as BinaryEx;

            if (!decompose || binaryCondition == null || (binaryCondition.PublicOperation != Operations.And && binaryCondition.PublicOperation != Operations.Or && binaryCondition.PublicOperation != Operations.Xor))
            {
                ConditionalEdge.AddConditionalEdge(From, TrueBranch, condition);
                DirectEdge.ConnectDirectEdge(From, FalseBranch);
                return;
            }

            BasicBlock intermediateBasicBlock = null;

            switch (binaryCondition.PublicOperation)
            {
            case Operations.And:
                intermediateBasicBlock = new BasicBlock();
                ConnectConditionalBranching(binaryCondition.LeftExpr, From, intermediateBasicBlock, FalseBranch);
                From = intermediateBasicBlock;
                ConnectConditionalBranching(binaryCondition.RightExpr, From, TrueBranch, FalseBranch);
                break;

            case Operations.Or:
                intermediateBasicBlock = new BasicBlock();
                ConnectConditionalBranching(binaryCondition.LeftExpr, From, TrueBranch, intermediateBasicBlock);
                From = intermediateBasicBlock;
                ConnectConditionalBranching(binaryCondition.RightExpr, From, TrueBranch, FalseBranch);
                break;

            case Operations.Xor:
                // Expands A xor B to (A and !B) || (!A and B)

                // Expansion expands A to A and !A and B to B and !B
                // For A and !A we the AST elements cannot be shared (must be unique) - the same for B and !B
                // We thus make copies of ast elements of left and right expression and use the copies to represent !A and !B
                var leftNegation  = new UnaryEx(Operations.LogicNegation, CFGVisitor.DeepCopyAstExpressionCopyVisitor(binaryCondition.LeftExpr));
                var rightNegation = new UnaryEx(Operations.LogicNegation, CFGVisitor.DeepCopyAstExpressionCopyVisitor(binaryCondition.RightExpr));

                var leftExpression  = new BinaryEx(Operations.And, binaryCondition.LeftExpr, rightNegation);
                var rightExpression = new BinaryEx(Operations.And, leftNegation, binaryCondition.RightExpr);
                var xorExpression   = new BinaryEx(Operations.Or, leftExpression, rightExpression);
                ConnectConditionalBranching(xorExpression, From, TrueBranch, FalseBranch);

                /*
                 * // Translation of xor in the level of control flow graph. More efficient than expansion of AST (translation in the level of program code).
                 * // Does not work because AST of sharing AST elements
                 * var intermediateBasicBlock1 = new BasicBlock();
                 * var intermediateBasicBlock2 = new BasicBlock();
                 * VisitIfStmtRec(binaryCondition.LeftExpr, intermediateBasicBlock1, intermediateBasicBlock2);
                 * FromBlock = intermediateBasicBlock1;
                 * VisitIfStmtRec(binaryCondition.RightExpr, FalseSink, TrueSink);
                 * FromBlock = intermediateBasicBlock2;
                 * VisitIfStmtRec(binaryCondition.RightExpr, TrueSink, FalseSink);*/
                break;
            }
        }
예제 #5
0
        public static byte[] CreateRegionSearchKey(byte[] table, byte[] key)
        {
            var metaKey = BinaryEx.ConcatInOrder(
                table,
                new[] { ConstByte.Comma },
                key,
                new[] { ConstByte.Comma },
                new[] { ConstByte.Colon }
                );

            return(metaKey);
        }
예제 #6
0
 private void assumeBinary(BinaryEx exp)
 {
     if (exp == null)
     {
         return;
     }
     switch (exp.PublicOperation)
     {
     case Operations.Equal:
         assumeEqual(exp.LeftExpr, exp.RightExpr);
         break;
     }
 }
예제 #7
0
 /// <summary>
 /// Makes the assumption in case of <c>false</c> as a condition result.
 /// </summary>
 /// <param name="langElement">The language element to assume.</param>
 /// <param name="memoryContext">The memory context of the code block and it's variables.</param>
 /// <param name="flowOutputSet">The Output set of a program point.</param>
 private void AssumeFalse(LangElement langElement, MemoryContext memoryContext, FlowOutputSet flowOutputSet)
 {
     if (langElement is BinaryEx)
     {
         BinaryEx binaryExpression = (BinaryEx)langElement;
         if (binaryExpression.PublicOperation == Operations.Equal)
         {
             AssumeNotEquals(binaryExpression.LeftExpr, binaryExpression.RightExpr, memoryContext);
         }
         else if (binaryExpression.PublicOperation == Operations.NotEqual)
         {
             AssumeEquals(binaryExpression.LeftExpr, binaryExpression.RightExpr, memoryContext, flowOutputSet.Snapshot);
         }
         else if (binaryExpression.PublicOperation == Operations.GreaterThan)
         {
             AssumeLesserThan(binaryExpression.LeftExpr, binaryExpression.RightExpr, true, memoryContext, flowOutputSet.Snapshot);
         }
         else if (binaryExpression.PublicOperation == Operations.GreaterThanOrEqual)
         {
             AssumeLesserThan(binaryExpression.LeftExpr, binaryExpression.RightExpr, false, memoryContext, flowOutputSet.Snapshot);
         }
         else if (binaryExpression.PublicOperation == Operations.LessThan)
         {
             AssumeGreaterThan(binaryExpression.LeftExpr, binaryExpression.RightExpr, true, memoryContext, flowOutputSet.Snapshot);
         }
         else if (binaryExpression.PublicOperation == Operations.LessThanOrEqual)
         {
             AssumeGreaterThan(binaryExpression.LeftExpr, binaryExpression.RightExpr, false, memoryContext, flowOutputSet.Snapshot);
         }
     }
     else if (langElement is UnaryEx)
     {
         UnaryEx unaryExpression = (UnaryEx)langElement;
         if (unaryExpression.PublicOperation == Operations.LogicNegation)
         {
             AssumeTrue(unaryExpression.Expr, memoryContext, flowOutputSet);
         }
     }
     else if (langElement is VarLikeConstructUse)
     {
         var variableLikeUse = (VarLikeConstructUse)langElement;
         AssumeFalseElementUse(variableLikeUse, memoryContext, flowOutputSet.Snapshot);
     }
     else if (langElement is IssetEx)
     {
         IssetEx issetEx = (IssetEx)langElement;
         AssumeIsset(issetEx, memoryContext, flowOutputSet.Snapshot, false);
     }
 }
예제 #8
0
        /// <summary>
        /// Constructs the simple condition from the given list.
        /// </summary>
        /// <param name="conditionList">The condition list.</param>
        /// <returns></returns>
        private Expression constructSimpleCondition(List <Expression> conditionList)
        {
            Expression groupCondition;

            if (conditionList.Count > 0)
            {
                groupCondition = conditionList[0];
                for (int index = 1; index < conditionList.Count; index++)
                {
                    Position newPosition = mergePositions(groupCondition.Position, conditionList[index].Position);
                    groupCondition = new BinaryEx(newPosition, Operations.And, groupCondition, conditionList[index]);
                }
            }
            else
            {
                groupCondition = new BoolLiteral(Position.Invalid, true);
            }

            return(groupCondition);
        }
예제 #9
0
 public override void VisitBinaryEx(BinaryEx x)
 {
     VisitElement(x.LeftExpr);
     ConsumeToken(TokenFacts.GetOperationToken(x.Operation), x.OperationPosition);
     VisitElement(x.RightExpr);
 }
예제 #10
0
        /// <summary>
        /// Recursive method used for generating the body of text representation of controlflow graph.
        /// It generates the result for globalcode and dureing the generation it calls
        /// itself resursivly on controlflow graphs of declared functions and methods.
        /// </summary>
        /// <param name="counter">Body of string representation in dot language of current conftrolflow graph.</param>
        /// <returns></returns>
        private string generateText(int counter)
        {
            string            result = "";
            List <BasicBlock> nodes  = CollectAllBasicBlocks();

            /*
             * generating text for all nodes
             */
            string functionsResult = "";
            int    i          = counter;
            int    oldCounter = counter;

            foreach (var node in nodes)
            {
                string label = "";
                foreach (var statement in node.Statements)
                {
                    /*
                     * recursive generating cfg and text representation for function
                     */
                    if (statement.GetType() == typeof(FunctionDecl))
                    {
                        FunctionDecl function = (FunctionDecl)statement;
                        label += "function " + function.Function.Name + Environment.NewLine;

                        try
                        {
                            ControlFlowGraph cfg = new ControlFlowGraph(globalCode, function, File);
                            functionsResult += cfg.generateText((counter / 10000 + 1) * 10000);
                            counter         += 10000;
                        }
                        catch (Weverca.ControlFlowGraph.ControlFlowException e)
                        {
                            Console.WriteLine(e.Message);
                            return("");
                        }
                    }

                    /*
                     * recursive generating cfg and text representation for objects
                     */
                    else if (statement.GetType() == typeof(TypeDecl))
                    {
                        TypeDecl clas = (TypeDecl)statement;
                        label += "class " + clas.Name.ToString() + Environment.NewLine;
                        foreach (var method in clas.Members)
                        {
                            if (method.GetType() == typeof(MethodDecl))
                            {
                                try
                                {
                                    ControlFlowGraph cfg = new ControlFlowGraph(globalCode, method as MethodDecl, File);
                                    functionsResult += cfg.generateText((counter / 10000 + 1) * 10000);
                                    counter         += 10000;
                                }
                                catch (Weverca.ControlFlowGraph.ControlFlowException e)
                                {
                                    Console.WriteLine(e.Message);
                                    return("");
                                }
                            }
                        }
                    }
                    else if (statement is ForeachStmt)
                    {
                        var      forEach             = statement as ForeachStmt;
                        Position foreachHeadPosition = new Position(forEach.Position.FirstLine, forEach.Position.FirstColumn, forEach.Position.FirstOffset,
                                                                    forEach.Body.Position.FirstLine, forEach.Body.Position.FirstColumn, forEach.Body.Position.FirstOffset - 1);
                        label += globalCode.SourceUnit.GetSourceCode(foreachHeadPosition) + Environment.NewLine;
                    }
                    else
                    {
                        label += globalCode.SourceUnit.GetSourceCode(statement.Position) + Environment.NewLine;
                    }
                }
                if (node.EndIngTryBlocks.Count > 0)
                {
                    label += "//ending Try block";
                }
                label   = label.Replace("\"", "\\\"");
                result += "node" + i + "[label=\"" + label + "\"]" + Environment.NewLine;
                i++;
            }

            /*
             * drawing edges
             */
            i = oldCounter;
            foreach (var node in nodes)
            {
                foreach (var e in node.OutgoingEdges)
                {
                    int index = oldCounter + nodes.IndexOf(e.To);
                    if (e.EdgeType == BasicBlockEdgeTypes.CONDITIONAL)
                    {
                        string label = "";
                        //in case a condition is not from original ast and hes been aded in constructiion of cfg, we need to write it in different way
                        if (!e.Condition.Position.IsValid || this.cfgAddedElements.Contains(e.Condition))
                        {
                            if (e.Condition.GetType() == typeof(BoolLiteral))
                            {
                                label = e.Condition.Value.ToString();
                            }
                            if (e.Condition.GetType() == typeof(BinaryEx))
                            {
                                BinaryEx bin = (BinaryEx)e.Condition;
                                //dirty trick how to acces internal field
                                var a = bin.GetType().GetField("operation", BindingFlags.NonPublic | BindingFlags.Instance);
                                if ((Operations)a.GetValue(bin) == Operations.Equal)
                                {
                                    Expression l = (Expression)bin.GetType().GetField("leftExpr", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(bin);
                                    Expression r = (Expression)bin.GetType().GetField("rightExpr", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(bin);
                                    if (l.Position.IsValid == false)
                                    {
                                        if (l.GetType() == typeof(IntLiteral))
                                        {
                                            label += "" + ((IntLiteral)l).Value;
                                            label += "=";
                                            label += globalCode.SourceUnit.GetSourceCode(r.Position);
                                        }
                                        else
                                        {
                                            label += "else";
                                        }
                                    }
                                    else
                                    {
                                        label += globalCode.SourceUnit.GetSourceCode(l.Position);
                                        label += "=";
                                        label += globalCode.SourceUnit.GetSourceCode(r.Position);
                                    }
                                }
                                else
                                {
                                    label += "else";
                                    //label = globalCode.SourceUnit.GetSourceCode(edge.Condition.Position);
                                }
                            }
                            if (e.Condition.GetType() == typeof(DirectFcnCall))
                            {
                                DirectFcnCall functionCall = (DirectFcnCall)e.Condition;

                                label += functionCall.QualifiedName + "(";
                                foreach (var parameter in functionCall.CallSignature.Parameters)
                                {
                                    if (parameter.Expression.GetType() == typeof(StringLiteral))
                                    {
                                        StringLiteral literal = (StringLiteral)parameter.Expression;
                                        label += "\"" + literal.Value + "\"" + ",";
                                    }
                                    else
                                    {
                                        label += globalCode.SourceUnit.GetSourceCode(parameter.Expression.Position) + ",";
                                    }
                                }
                                label += ")";
                            }
                        }
                        else
                        {
                            label = globalCode.SourceUnit.GetSourceCode(e.Condition.Position);
                        }
                        label   = label.Replace("\"", "\\\"");
                        result += "node" + i + " -> node" + index + "[headport=n, tailport=s,label=\"" + label + "\"]" + Environment.NewLine;
                    }
                    else if (e.EdgeType == BasicBlockEdgeTypes.FOREACH)
                    {
                        result += "node" + i + " -> node" + index + "[headport=n, tailport=s,label=\"foreach special edge\"]" + Environment.NewLine;
                    }
                    else
                    {
                        result += "node" + i + " -> node" + index + "[headport=n, tailport=s,label=\"direct edge\"]" + Environment.NewLine;
                    }
                }
                if (node.DefaultBranch != null)
                {
                    string elseString = string.Empty;
                    if (node.OutgoingEdges.Count > 0)
                    {
                        elseString = "else";
                    }

                    int index = oldCounter + nodes.IndexOf(node.DefaultBranch.To);
                    result += "node" + i + " -> node" + index + "[headport=n, tailport=s,label=\" " + elseString + "  \"]" + Environment.NewLine;
                }
                if (node.EndIngTryBlocks.Count > 0)
                {
                    foreach (var tryblock in node.EndIngTryBlocks)
                    {
                        foreach (var catchblock in tryblock.catchBlocks)
                        {
                            int index = oldCounter + nodes.IndexOf(catchblock);
                            result += "node" + i + " -> node" + index + "[headport=n, tailport=s,label=\" catched " + catchblock.ClassName.QualifiedName.Name.Value + "  \"]" + Environment.NewLine;
                        }
                    }
                }
                i++;
            }
            result += functionsResult;
            result += Environment.NewLine;
            return(result);
        }
예제 #11
0
 /// <summary>
 /// Visit left and right expressions of binary expression.
 /// </summary>
 /// <param name="x"></param>
 virtual public void VisitBinaryEx(BinaryEx x)
 {
     VisitElement(x.LeftExpr);
     VisitElement(x.RightExpr);
 }
예제 #12
0
        /// <summary>
        /// Visits switch statement and builds controlflow graf for switch construct.
        ///
        /// </summary>
        /// <param name="x">SwitchStmt</param>
        public override void VisitSwitchStmt(SwitchStmt x)
        {
            var        aboveCurrentCaseBlock = currentBasicBlock;
            BasicBlock lastDefaultStartBlock = null;
            BasicBlock lastDefaultEndBlock   = null;

            currentBasicBlock = new BasicBlock();
            //in case of switch statement, continue and break means the same so we make the edge always to the block under the switch
            BasicBlock underLoop = new BasicBlock();

            loopData.Push(new LoopData(underLoop, underLoop));

            aboveCurrentCaseBlock.CreateWorklistSegment(underLoop);

            for (int j = 0; j < x.SwitchItems.Count; j++)
            {
                var switchItem = x.SwitchItems[j];
                var caseItem   = switchItem as CaseItem;

                // The basic block corresponding to current switch item
                var switchBlock = new BasicBlock();

                // Connect previous switch item (implicitly, subsequent switch items are connected - break must
                // be there to force the flow to not go to subsequent switch item)
                if (j > 0)
                {
                    BasicBlockEdge.ConnectDirectEdge(currentBasicBlock, switchBlock);
                }

                if (caseItem == null)
                {
                    // Default branch

                    // Just mark the last default branch
                    lastDefaultStartBlock = switchBlock;
                }
                else
                {
                    // Case branch

                    // Create condition of the current case branch
                    BinaryEx condition = new BinaryEx(caseItem.CaseVal.Position, Operations.Equal, x.SwitchValue, caseItem.CaseVal);
                    graph.cfgAddedElements.Add(condition);

                    // Create conditional branching: true condition goes to the case, else condition goes above the next switch item
                    var elseBlock = new BasicBlock();
                    BasicBlockEdge.ConnectConditionalBranching(condition, aboveCurrentCaseBlock, switchBlock, elseBlock);
                    aboveCurrentCaseBlock = elseBlock;
                }

                // Builds CFG for the body of the switch element
                currentBasicBlock = switchBlock;
                switchItem.VisitMe(this);

                if (caseItem == null)
                {
                    // Just to mark the last default branch
                    lastDefaultEndBlock = currentBasicBlock;
                }
            }

            loopData.Pop();

            if (lastDefaultStartBlock == null) // No default branch
            {
                // Connect the last case with the code under the switch
                BasicBlockEdge.ConnectDirectEdge(currentBasicBlock, underLoop);
                // Connect the else of the last case with the code under the switch
                BasicBlockEdge.ConnectDirectEdge(aboveCurrentCaseBlock, underLoop);
            }
            else // There is default branch
            {
                // The last default branch is the else of the last case
                BasicBlockEdge.ConnectDirectEdge(aboveCurrentCaseBlock, lastDefaultStartBlock);
                if (lastDefaultEndBlock.DefaultBranch == null) // break/continue in the default branch
                // Connect it with the code under the swithch
                {
                    BasicBlockEdge.ConnectDirectEdge(lastDefaultEndBlock, underLoop);
                }
                else // no break/continue in the default branch
                     // Connect the last case with the code under the switch
                {
                    BasicBlockEdge.ConnectDirectEdge(currentBasicBlock, underLoop);
                }
            }

            currentBasicBlock = underLoop;
        }
예제 #13
0
        /// <summary>
        /// Visits JumpStmt (break, continue, return).
        /// </summary>
        /// <param name="x">JumpStmt</param>
        public override void VisitJumpStmt(JumpStmt x)
        {
            switch (x.Type)
            {
            case JumpStmt.Types.Break:
            case JumpStmt.Types.Continue:
                if (x.Expression == null)    //break without saying how many loops to break
                {
                    BasicBlock target;
                    //break/continue
                    if (loopData.Count == 0)
                    {
                        if (x.Type == JumpStmt.Types.Break)
                        {
                            throw new ControlFlowException(ControlFlowExceptionCause.BREAK_NOT_IN_CYCLE, x.Position);
                        }
                        else
                        {
                            throw new ControlFlowException(ControlFlowExceptionCause.CONTINUE_NOT_IN_CYCLE, x.Position);
                        }
                    }
                    if (x.Type == JumpStmt.Types.Break)
                    {
                        target = loopData.Peek().BreakTarget;
                    }
                    else
                    {
                        target = loopData.Peek().ContinueTarget;
                    }
                    BasicBlockEdge.ConnectDirectEdge(currentBasicBlock, target);
                }
                else
                {
                    int breakValue = 1;
                    for (int i = loopData.Count - 1; i >= 0; --i)
                    {
                        BasicBlock target;
                        if (x.Type == JumpStmt.Types.Break)
                        {
                            target = loopData.ElementAt(i).BreakTarget;
                        }
                        else
                        {
                            target = loopData.ElementAt(i).ContinueTarget;
                        }
                        BinaryEx condition = new BinaryEx(x.Position, Operations.Equal, new IntLiteral(Position.Invalid, breakValue), x.Expression);
                        graph.cfgAddedElements.Add(condition);
                        BasicBlockEdge.ConnectConditionalBranching(condition, currentBasicBlock, target, new BasicBlock());
                        //BasicBlockEdge.AddConditionalEdge(currentBasicBlock, target, condition);
                        ++breakValue;
                    }
                }
                break;

            case JumpStmt.Types.Return:

                PHP.Core.Debug.Assert(functionSinkStack.Count > 0);

                currentBasicBlock.AddElement(x);
                BasicBlockEdge.ConnectDirectEdge(currentBasicBlock, functionSinkStack.Peek());

                currentBasicBlock = new BasicBlock();

                break;
            }


            currentBasicBlock = new BasicBlock();
        }
예제 #14
0
        public override void VisitBinaryEx(BinaryEx x)
        {
            var        lOperand = CreateRValue(x.LeftExpr);
            ValuePoint rOperand;

            BinaryExPoint expression;

            switch (x.PublicOperation)
            {
            case Operations.And:
            case Operations.Or:

                /* Points are created in current ordering
                 *    1. blockStart,
                 *    2. shortendPath,
                 *    3. nonShortendPath,
                 *    4. rOperand
                 */

                var shortableForm    = x.PublicOperation == Operations.And ? ConditionForm.None : ConditionForm.All;
                var nonShortableForm = shortableForm == ConditionForm.All ? ConditionForm.None : ConditionForm.All;

                var shortableCondition = new AssumptionCondition(shortableForm, x.LeftExpr);
                //shortened evaluation path
                var shortendPath = new AssumePoint(shortableCondition, new[] { lOperand });

                var nonShortableCondition = new AssumptionCondition(nonShortableForm, x.LeftExpr);
                //normal evaluation
                var nonShortendPath = new AssumePoint(nonShortableCondition, new[] { lOperand });

                //block borders
                var blockStart = new EmptyProgramPoint();
                //1.
                AppendToChain(blockStart);
                //2.
                AppendToChain(shortendPath);
                //3.
                AppendToChain(nonShortendPath);
                //4.
                rOperand = CreateRValue(x.RightExpr);

                expression = new BinaryExPoint(x, lOperand, rOperand);

                //shortend path is added via chain
                blockStart.AddFlowChild(nonShortendPath);

                //set explicit edge
                PreventChainEdge(shortendPath);
                shortendPath.AddFlowChild(expression);



                break;

            default:
                rOperand   = CreateRValue(x.RightExpr);
                expression = new BinaryExPoint(x, lOperand, rOperand);
                break;
            }

            Result(expression);
        }
예제 #15
0
 /// <inheritdoc />
 public override void VisitBinaryEx(BinaryEx x)
 {
     RValueResult(x);
 }
예제 #16
0
 /// <summary>
 /// Visits BinaryEx.
 /// </summary>
 /// <param name="x"></param>
 public override void VisitBinaryEx(BinaryEx x)
 {
     this.VisitElement(x);
 }
예제 #17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BinaryExPoint" /> class.
 /// </summary>
 /// <param name="expression">Binary expression</param>
 /// <param name="lOperand">Program point with left binary operand</param>
 /// <param name="rOperand">Program point with right binary operand</param>
 internal BinaryExPoint(BinaryEx expression, ValuePoint lOperand, ValuePoint rOperand)
 {
     Expression   = expression;
     LeftOperand  = lOperand;
     RightOperand = rOperand;
 }
예제 #18
0
        /// <summary>
        /// Makes the assumption in case of <c>false</c> as a condition result.
        /// </summary>
        /// <param name="langElement">The language element to assume.</param>
        /// <param name="memoryContext">The memory context of the code block and it's variables.</param>
        /// <param name="flowOutputSet">The Output set of a program point.</param>
        void AssumeFalse(LangElement langElement, MemoryContext memoryContext, FlowOutputSet flowOutputSet)
        {
            if (langElement is BinaryEx)
            {
                BinaryEx binaryExpression = (BinaryEx)langElement;
                if (binaryExpression.PublicOperation == Operations.Equal)
                {
                    AssumeNotEquals(binaryExpression.LeftExpr, binaryExpression.RightExpr, memoryContext);
                }
                else if (binaryExpression.PublicOperation == Operations.NotEqual)
                {
                    AssumeEquals(binaryExpression.LeftExpr, binaryExpression.RightExpr, memoryContext, flowOutputSet.Snapshot);
                }
                else if (binaryExpression.PublicOperation == Operations.GreaterThan)
                {
                    AssumeLesserThan(binaryExpression.LeftExpr, binaryExpression.RightExpr, true, memoryContext, flowOutputSet.Snapshot);
                }
                else if (binaryExpression.PublicOperation == Operations.GreaterThanOrEqual)
                {
                    AssumeLesserThan(binaryExpression.LeftExpr, binaryExpression.RightExpr, false, memoryContext, flowOutputSet.Snapshot);
                }
                else if (binaryExpression.PublicOperation == Operations.LessThan)
                {
                    AssumeGreaterThan(binaryExpression.LeftExpr, binaryExpression.RightExpr, true, memoryContext, flowOutputSet.Snapshot);
                }
                else if (binaryExpression.PublicOperation == Operations.LessThanOrEqual)
                {
                    AssumeGreaterThan(binaryExpression.LeftExpr, binaryExpression.RightExpr, false, memoryContext, flowOutputSet.Snapshot);
                }
                else if (binaryExpression.PublicOperation == Operations.And ||
                         binaryExpression.PublicOperation == Operations.Or ||
                         binaryExpression.PublicOperation == Operations.Xor)
                {
                    ConditionForm conditionForm = ConditionForm.SomeNot; // !(a AND b) --> !a OR !b
                    if (binaryExpression.PublicOperation == Operations.Or)
                    {
                        conditionForm = ConditionForm.None; // !(a OR b) --> !a AND !b
                    }
                    else if (binaryExpression.PublicOperation == Operations.Xor)
                    {
                        conditionForm = ConditionForm.NotExactlyOne; //!(a XOR b) --> !((a OR b) AND !(a AND b)) --> (!a AND !b) OR (a AND b)
                    }

                    MemoryContext  currentMemoryContext = new MemoryContext(log, flowOutputSet);
                    ConditionParts condition            = new ConditionParts(conditionForm, flowOutputSet, log, binaryExpression.LeftExpr, binaryExpression.RightExpr);
                    condition.MakeAssumption(currentMemoryContext);
                    memoryContext.UnionMerge(currentMemoryContext);
                }
            }
            else if (langElement is UnaryEx)
            {
                UnaryEx unaryExpression = (UnaryEx)langElement;
                if (unaryExpression.PublicOperation == Operations.LogicNegation)
                {
                    AssumeTrue(unaryExpression.Expr, memoryContext, flowOutputSet);
                }
            }
            else if (langElement is DirectVarUse)
            {
                DirectVarUse directVarUse = (DirectVarUse)langElement;
                AssumeFalseDirectVarUse(directVarUse, memoryContext, flowOutputSet.Snapshot);
            }
            else if (langElement is IssetEx)
            {
                IssetEx issetEx = (IssetEx)langElement;
                AssumeIsset(issetEx, memoryContext, flowOutputSet.Snapshot, false);
            }
        }
예제 #19
0
        /// <summary>
        /// Makes the assumption in case of <c>true</c> as a condition result.
        /// </summary>
        /// <param name="langElement">The language element to assume.</param>
        /// <param name="memoryContext">The memory context of the code block and it's variables.</param>
        /// <param name="flowOutputSet">The Output set of a program point.</param>
        void AssumeTrue(LangElement langElement, MemoryContext memoryContext, FlowOutputSet flowOutputSet)
        {
            if (langElement is BinaryEx)
            {
                BinaryEx binaryExpression = (BinaryEx)langElement;
                if (binaryExpression.PublicOperation == Operations.Equal)
                {
                    AssumeEquals(binaryExpression.LeftExpr, binaryExpression.RightExpr, memoryContext, flowOutputSet.Snapshot);
                }
                else if (binaryExpression.PublicOperation == Operations.NotEqual)
                {
                    AssumeNotEquals(binaryExpression.LeftExpr, binaryExpression.RightExpr, memoryContext);
                }
                else if (binaryExpression.PublicOperation == Operations.GreaterThan)
                {
                    AssumeGreaterThan(binaryExpression.LeftExpr, binaryExpression.RightExpr, false, memoryContext, flowOutputSet.Snapshot);
                }
                else if (binaryExpression.PublicOperation == Operations.GreaterThanOrEqual)
                {
                    AssumeGreaterThan(binaryExpression.LeftExpr, binaryExpression.RightExpr, true, memoryContext, flowOutputSet.Snapshot);
                }
                else if (binaryExpression.PublicOperation == Operations.LessThan)
                {
                    AssumeLesserThan(binaryExpression.LeftExpr, binaryExpression.RightExpr, false, memoryContext, flowOutputSet.Snapshot);
                }
                else if (binaryExpression.PublicOperation == Operations.LessThanOrEqual)
                {
                    AssumeLesserThan(binaryExpression.LeftExpr, binaryExpression.RightExpr, true, memoryContext, flowOutputSet.Snapshot);
                }
                else if (binaryExpression.PublicOperation == Operations.And ||
                         binaryExpression.PublicOperation == Operations.Or ||
                         binaryExpression.PublicOperation == Operations.Xor)
                {
                    ConditionForm conditionForm = ConditionForm.All;
                    if (binaryExpression.PublicOperation == Operations.Or)
                    {
                        conditionForm = ConditionForm.Some;
                    }
                    else if (binaryExpression.PublicOperation == Operations.Xor)
                    {
                        conditionForm = ConditionForm.ExactlyOne;
                    }

                    MemoryContext  currentMemoryContext = new MemoryContext(log, flowOutputSet);
                    ConditionParts condition            = new Weverca.Analysis.FlowResolver.Deprecated.ConditionParts(conditionForm, flowOutputSet, log, binaryExpression.LeftExpr, binaryExpression.RightExpr);
                    condition.MakeAssumption(currentMemoryContext);
                    memoryContext.UnionMerge(currentMemoryContext);
                }
            }
            else if (langElement is UnaryEx)
            {
                UnaryEx unaryExpression = (UnaryEx)langElement;
                if (unaryExpression.PublicOperation == Operations.LogicNegation)
                {
                    AssumeFalse(unaryExpression.Expr, memoryContext, flowOutputSet);
                }
            }
            else if (langElement is DirectVarUse)
            {
                DirectVarUse directVarUse = (DirectVarUse)langElement;
                AssumeTrueDirectVarUse(directVarUse, memoryContext, flowOutputSet.Snapshot);
            }
            else if (langElement is IssetEx)
            {
                IssetEx issetEx = (IssetEx)langElement;
                AssumeIsset(issetEx, memoryContext, flowOutputSet.Snapshot, true);
            }
        }