Example #1
0
        private static List <TflField> GetFields(CfgNode process, IEnumerable <Field> fieldDefinitions, ILogger logger, string entityName)
        {
            var fields = new List <TflField>();

            foreach (var fd in fieldDefinitions)
            {
                if (fd.Type.Equals("string"))
                {
                    logger.EntityInfo(entityName, "Using {0} character string for {1}.", fd.Length, fd.Name);
                }
                else
                {
                    logger.EntityInfo(entityName, "Using {0} for {1}.", fd.Type, fd.Name);
                }

                var field = fd;
                fields.Add(process.GetDefaultOf <TflField>(f => {
                    f.Name       = field.Name;
                    f.Length     = field.Length;
                    f.Type       = field.Type;
                    f.QuotedWith = field.QuotedWith;
                }));
            }
            return(fields);
        }
 protected static void CreateExceptionalEdges(ProgramState state, CfgNode entryNode)
 {
     foreach (var node in state.NodesToLinkWithExceptionBlock)
     {
         node.ExceptionNodes.Add(entryNode);
     }
     state.NodesToLinkWithExceptionBlock = new List <CfgNode>();
 }
Example #3
0
 /// <summary>
 /// Registers a node in the CFG and set it as the successfor of the previous node.
 /// </summary>
 /// <param name="state">Current program state.</param>
 /// <param name="node">Node to register.</param>
 protected void RegisterNode(ProgramState state, CfgNode node)
 {
     state.Cfg.RegisterNode(node);
     state.PreviousNode.Successors.Add(node);
     if (RememberNodeOffset)
     {
         state.SaveNodeOffset(node, PreviousProgramStack);
         RememberNodeOffset = false;
     }
 }
Example #4
0
        private CfgNode AddCfgNode(List <AstNode> nextNodes)
        {
            CfgNode cfgNode = new CfgNode();

            foreach (var lines in nextNodes)
            {
                cfgNode.LineNumbers.Add(lines.LineNumber);
            }
            return(cfgNode);
        }
Example #5
0
 private void PrintInternal(CfgNode node)
 {
     //foreach (var lines in node.LineNumbers)
     //{
     //    Console.WriteLine(lines + ",");
     //}
     //foreach (var child in node.NextNodes)
     //{
     //    foreach (var lines in child.LineNumbers)
     //    {
     //        Console.WriteLine(lines + ",");
     //    }
     //}
     //PrintInternal(node.NextNodes.Last());
 }
Example #6
0
        private string InnerSerialize(CfgNode node) {
            var meta = CfgMetadataCache.GetMetadata(node.GetType());
            var builder = new StringBuilder();
            if (meta.All(kv => kv.Value.ListType == null)) {
                builder.Append("{");
                SerializeAttributes(meta, node, builder);
                builder.TrimEnd(", ");
                builder.Append(" }");
            } else {
                builder.AppendLine("{");
                SerializeAttributes(meta, node, builder);
                SerializeElements(meta, node, builder, 1);
                builder.AppendLine();
                builder.Append("}");
            }

            return builder.ToString();
        }
Example #7
0
        /// <summary>
        /// Creates a new node to ensure finally instructions aren't attached to a body node.
        /// </summary>
        private static CfgNode CreateFinallyHandlerNonExceptionalEntry(ProgramState state,
                                                                       ExceptionHandler handler,
                                                                       Instruction leaveTarget)
        {
            CfgNode finallyHandlerStartNode = null;

            (var nodeOffset, _) = state.GetOffsetNode(handler.HandlerStart.Offset);
            if (nodeOffset == null)
            {
                finallyHandlerStartNode = new StatementNode(
                    location: GetHandlerStartLocation(state, handler),
                    kind: StatementNode.StatementNodeKind.MethodBody,
                    proc: state.ProcDesc);
                state.Cfg.RegisterNode(finallyHandlerStartNode);
                state.PreviousNode.Successors.Add(finallyHandlerStartNode);
                state.AppendToPreviousNode = true;
            }
            state.EndfinallyControlFlow = leaveTarget;
            return(finallyHandlerStartNode);
        }
 /// <summary>
 /// Registers a node in the CFG and set it as the successor of the previous node.
 /// </summary>
 /// <param name="state">Current program state.</param>
 /// <param name="node">Node to register.</param>
 protected void RegisterNode(ProgramState state, CfgNode node)
 {
     state.Cfg.RegisterNode(node);
     state.PreviousNode.Successors.Add(node);
     node.BlockEndOffset = state.MethodExceptionHandlers
                           .GetBlockEndOffsetFromOffset(
         state.CurrentInstruction.Offset);
     if (state.MethodExceptionHandlers.GetExceptionHandlerAtInstruction(
             state.CurrentInstruction) != null)
     {
         state.NodesToLinkWithExceptionBlock.Add(node);
     }
     if (RememberNodeOffset)
     {
         state.SaveNodeOffset(node,
                              PreviousProgramStack,
                              state.PreviousNode.BlockEndOffset);
         RememberNodeOffset = false;
     }
 }
Example #9
0
        string InnerSerialize(CfgNode node)
        {
            var type = node.GetType();

#if NETS
            var attribute = type.GetTypeInfo().GetCustomAttributes(typeof(CfgAttribute), true).FirstOrDefault() as CfgAttribute;
#else
            var attribute = type.GetCustomAttributes(typeof(CfgAttribute), true).FirstOrDefault() as CfgAttribute;
#endif
            if (attribute != null && !attribute.serialize)
            {
                return(string.Empty);
            }

            var name    = !string.IsNullOrEmpty(attribute?.name) ? attribute.name : type.Name;
            var meta    = CfgMetadataCache.GetMetadata(type);
            var builder = new StringBuilder();

            if (JustAttributes(meta, node))
            {
                builder.Append("<add");
                SerializeAttributes(meta, node, builder);
                builder.Append(" />");
            }
            else
            {
                builder.Append("<");
                builder.Append(name);
                SerializeAttributes(meta, node, builder);
                builder.AppendLine(">");
                SerializeElements(meta, node, builder, 1);
                builder.Append("</");
                builder.Append(name);
                builder.Append(">");
            }

            return(builder.ToString());
        }
Example #10
0
        string InnerSerialize(CfgNode node) {

            var type = node.GetType();
            var meta = CfgMetadataCache.GetMetadata(type);
            var builder = new StringBuilder();

            if (JustAttributes(meta, node)) {
                builder.Append("<add");
                SerializeAttributes(meta, node, builder);
                builder.Append(" />");
            } else {
                builder.Append("<");
                builder.Append(type.Name);
                SerializeAttributes(meta, node, builder);
                builder.AppendLine(">");
                SerializeElements(meta, node, builder, 1);
                builder.Append("</");
                builder.Append(type.Name);
                builder.Append(">");
            }

            return builder.ToString();
        }
Example #11
0
        private string InnerSerialize(CfgNode node)
        {
            var meta    = CfgMetadataCache.GetMetadata(node.GetType());
            var builder = new StringBuilder();

            if (meta.All(kv => kv.Value.ListType == null))
            {
                builder.Append("{");
                SerializeAttributes(meta, node, builder);
                builder.TrimEnd(", ");
                builder.Append(" }");
            }
            else
            {
                builder.AppendLine("{");
                SerializeAttributes(meta, node, builder);
                SerializeElements(meta, node, builder, 1);
                builder.TrimEnd(", ");
                builder.AppendLine();
                builder.Append("}");
            }

            return(builder.ToString());
        }
Example #12
0
 void AssertIns(CfgNode node, params string[] regs)
 {
     CollectionAssert.AreEquivalent(regs, node.In);
 }
Example #13
0
 public string Serialize(CfgNode node) {
     return JsonConvert.SerializeObject(node, _settings);
 }
        protected static void CreateCatchHandlerEntryBlock(ProgramState state,
                                                           ExceptionHandlerNode handlerNode,
                                                           CfgNode handlerEntryPredecessor,
                                                           Identifier exceptionIdentifier)
        {
            (var trueBranch, var falseBranch) = CreateExceptionTypeCheckBranchNodes(
                state, handlerNode.ExceptionHandler, exceptionIdentifier);
            handlerEntryPredecessor.Successors.Add(trueBranch);
            handlerEntryPredecessor.Successors.Add(falseBranch);

            if (!state.ExceptionHandlerToCatchVarNode.ContainsKey(handlerNode.ExceptionHandler))
            {
                state.ExceptionHandlerToCatchVarNode[handlerNode.ExceptionHandler] =
                    CreateLoadCatchVarNode(state, handlerNode.ExceptionHandler);
                // The CIL specification dictates that the exception object is on top of
                // the stack when the catch handler is entered; the first instruction of
                // the catch handler will handle the object pushed onto the stack.
                state.PushExpr(new VarExpression(exceptionIdentifier),
                               new Tptr(Tptr.PtrKind.Pk_pointer,
                                        new Tstruct("System.Object")));
                state.PushInstruction(
                    handlerNode.ExceptionHandler.HandlerStart,
                    state.ExceptionHandlerToCatchVarNode[handlerNode.ExceptionHandler].node);
            }
            (var loadCatchVarNode, _) = GetHandlerCatchVarNode(
                state, handlerNode.ExceptionHandler);
            trueBranch.Successors.Add(loadCatchVarNode);

            if (handlerNode.NextCatchBlock != null)
            {
                // Continues translation with catch handler's first instruction from
                // the handler's catch variable load node.
                CreateCatchHandlerEntryBlock(state,
                                             handlerNode.NextCatchBlock,
                                             falseBranch,
                                             exceptionIdentifier);
            }
            // Last catch handler of set; need to route control flow through the false
            // exception type-matching node.
            else
            {
                if (handlerNode.FinallyBlock != null)
                {
                    var finallyBranchNode = CreateFinallyExceptionBranchNode(
                        state, handlerNode.ExceptionHandler);
                    falseBranch.Successors
                    .Add(finallyBranchNode);
                    (var finallyLoadCatchVar, _) = GetHandlerCatchVarNode(
                        state, handlerNode.FinallyBlock);
                    finallyBranchNode.Successors.Add(finallyLoadCatchVar);
                }
                else
                {
                    var returnVariable = new LvarExpression(
                        new LocalVariable(Identifier.ReturnIdentifier, state.Method));
                    var retType  = state.Method.ReturnType.GetElementType();
                    var retInstr = new Store(
                        returnVariable,
                        new ExnExpression(new VarExpression(exceptionIdentifier)),
                        Typ.FromTypeReference(retType),
                        GetHandlerStartLocation(state,
                                                handlerNode.ExceptionHandler));
                    falseBranch.Instructions
                    .Add(retInstr);
                    falseBranch.Successors
                    .Add(state.ProcDesc.ExceptionSinkNode);
                }
            }
        }
Example #15
0
        private static List<TflField> GetFields(CfgNode process, IEnumerable<Field> fieldDefinitions, ILogger logger, string entityName) {
            var fields = new List<TflField>();
            foreach (var fd in fieldDefinitions) {
                if (fd.Type.Equals("string")) {
                    logger.EntityInfo(entityName, "Using {0} character string for {1}.", fd.Length, fd.Name);
                } else {
                    logger.EntityInfo(entityName, "Using {0} for {1}.", fd.Type, fd.Name);
                }

                var field = fd;
                fields.Add(process.GetDefaultOf<TflField>(f => {
                    f.Name = field.Name;
                    f.Length = field.Length;
                    f.Type = field.Type;
                    f.QuotedWith = field.QuotedWith;
                }));
            }
            return fields;
        }
Example #16
0
        protected override bool ParseCilInstructionInternal(Instruction instruction,
                                                            ProgramState state)
        {
            int     index;
            CfgNode node = null;

            switch (instruction.OpCode.Code)
            {
            case Code.Ldarg:
                index = (int)instruction.Operand;
                break;

            case Code.Ldarg_0:
                index = 0;
                break;

            case Code.Ldarg_1:
                index = 1;
                break;

            case Code.Ldarg_2:
                index = 2;
                break;

            case Code.Ldarg_3:
                index = 3;
                break;

            case Code.Ldarg_S:
            case Code.Ldarga:
            case Code.Ldarga_S:
                // Sequence accounts for the implict "this" argument, if applicable.
                index = (instruction.Operand as ParameterDefinition).Sequence;
                break;

            default:
                return(false);
            }

            (var argVar, var argType) = CreateArg(index, state.Method);

            if (instruction.OpCode.Code == Code.Ldarga || instruction.OpCode.Code == Code.Ldarga_S)
            {
                var argAddressType = new Address(Tptr.PtrKind.Pk_pointer,
                                                 argType,
                                                 argVar,
                                                 referenceKind: Address.ReferenceKind.Parameter);
                state.PushExpr(argVar, argAddressType);
            }
            else
            {
                if (ParameterIsByReference(index, state.Method))
                {
                    // Strips out the pointer because the type produced by CreateArg already
                    // carries it.
                    var argAddressType =
                        new Address(Tptr.PtrKind.Pk_pointer,
                                    argType.StripPointer(),
                                    argVar,
                                    referenceKind: Address.ReferenceKind.Parameter);
                    state.PushExpr(argVar, argAddressType);
                }
                else
                {
                    var loadArgument = state.PushAndLoad(argVar, argType);
                    node = AddMethodBodyInstructionsToCfg(state, loadArgument);
                    state.AppendToPreviousNode = true;
                }
            }
            state.PushInstruction(instruction.Next, node);
            return(true);
        }
Example #17
0
 public string Serialize(CfgNode node) {
     return InnerSerialize(node);
 }
Example #18
0
        protected override bool ParseCilInstructionInternal(Instruction instruction,
                                                            ProgramState state)
        {
            int     index;
            CfgNode node = null;

            switch (instruction.OpCode.Code)
            {
            case Code.Stloc_0:
                index = 0;
                break;

            case Code.Stloc_1:
                index = 1;
                break;

            case Code.Stloc_2:
                index = 2;
                break;

            case Code.Stloc_3:
                index = 3;
                break;

            case Code.Stloc_S:
                index = (instruction.Operand as VariableDefinition).Index;
                break;

            case Code.Stloc:
                index = (int)instruction.Operand;
                break;

            default:
                return(false);
            }

            (var value, var type) = state.Pop();

            // Records that the variable stores a boxed value.
            if (type is BoxedValueType boxedValueType)
            {
                state.VariableIndexToBoxedValueType[index] = boxedValueType;
            }
            // A non-boxed value is being stored at the location, so the corresponding record is
            // accordingly updated.
            else if (!(type is BoxedValueType) &&
                     state.VariableIndexToBoxedValueType.ContainsKey(index))
            {
                state.VariableIndexToBoxedValueType.Remove(index);
            }

            var variable = new LocalVariable(LocalName(index), state.Method);
            var storeValueIntoVariable = new Store(new LvarExpression(variable),
                                                   value,
                                                   type,
                                                   state.CurrentLocation);

            node = AddMethodBodyInstructionsToCfg(state, storeValueIntoVariable);
            RegisterLocalVariable(state, variable, type);
            state.PushInstruction(instruction.Next, node);
            return(true);
        }
Example #19
0
 public string Serialize(CfgNode node)
 {
     return(InnerSerialize(node));
 }
Example #20
0
 public string Serialize(CfgNode node)
 {
     return(JsonConvert.SerializeObject(node, _settings));
 }
Example #21
0
        protected override bool ParseCilInstructionInternal(Instruction instruction,
                                                            ProgramState state)
        {
            int     index;
            CfgNode node = null;

            switch (instruction.OpCode.Code)
            {
            case Code.Ldloc:
            case Code.Ldloca:
                index = (int)instruction.Operand;
                break;

            case Code.Ldloc_0:
                index = 0;
                break;

            case Code.Ldloc_1:
                index = 1;
                break;

            case Code.Ldloc_2:
                index = 2;
                break;

            case Code.Ldloc_3:
                index = 3;
                break;

            case Code.Ldloc_S:
            case Code.Ldloca_S:
                index = (instruction.Operand as VariableDefinition).Index;
                break;

            default:
                return(false);
            }

            (var variableExpression, var variableType) = CreateLocal(index, state.Method);

            // Updates the type to the appropriate boxed one if the variable contains a boxed
            // value.
            if (state.VariableIndexToBoxedValueType.ContainsKey(index))
            {
                variableType = state.VariableIndexToBoxedValueType[index];
            }

            if (instruction.OpCode.Code == Code.Ldloca || instruction.OpCode.Code == Code.Ldloca_S)
            {
                // Stores the variable as the value stored at the address expression.
                state.PushExpr(variableExpression, new Address(Tptr.PtrKind.Pk_pointer,
                                                               variableType,
                                                               variableExpression));
            }
            else
            {
                // Loads the value at the heap location onto the stack.
                var variableInstruction = state.PushAndLoad(variableExpression,
                                                            variableType);
                node = AddMethodBodyInstructionsToCfg(state, variableInstruction);
                state.AppendToPreviousNode = true;
            }
            state.PushInstruction(instruction.Next, node);
            return(true);
        }
Example #22
0
 public string Serialize(CfgNode node)
 {
     _serializer.Serialize(_writer, node);
     return(_writer.ToString());
 }