public void Init()
    {
        evaluator = new UnityELEvaluator();

        functionResolver = new TestFunctionResolver();
        evaluator.DefaultFunctionResolver = functionResolver;
    }
Exemple #2
0
        public NamespaceSymbol(string name, IEnumerable <FunctionOverload> functionOverloads, IEnumerable <BannedFunction> bannedFunctions)
            : base(name)
        {
            var methodResolver = new FunctionResolver(functionOverloads, bannedFunctions);

            Type = new NamespaceType(name, Enumerable.Empty <TypeProperty>(), methodResolver);
        }
Exemple #3
0
        public void Can_Correctly_Resolve_A_Function() {
            
            var funcs = FunctionResolver.GetFunctions(typeof(MyTestFunction).Assembly, new HashSet<Type> { 
                typeof(ILogger), 
                typeof(HttpRequest), 
                typeof(HttpTriggerAttribute) 
            });

            Assert.NotEmpty(funcs);

            var func = funcs.Single();

            Assert.Equal("TestHttpTrigger", func.Name);
            Assert.Equal(OperationType.Get, func.Methods.Single());
            Assert.Equal(5, func.Returns.Length);
            Assert.Collection(func.Returns, new Action<ReturnsAttribute>[] {
                a => { Assert.Equal(200, a.StatusCode); Assert.Equal("OK", a.Description); Assert.Equal(typeof(TestType), a.BodyType); },
                a => { Assert.Equal(400, a.StatusCode); Assert.Equal("Bad", a.Description); },
                a => { Assert.Equal(404, a.StatusCode); Assert.Equal("NotFound", a.Description); },
                a => { Assert.Equal(401, a.StatusCode); Assert.Equal("Unauthorized", a.Description); },
                a => { Assert.Equal(403, a.StatusCode); Assert.Equal("Forbidden", a.Description); },
            });
            Assert.Equal("/result/{sessionId}", func.Route);
            Assert.Single(func.Parameters);
            Assert.Collection(func.Parameters, new Action<ParameterInfo>[] {
                p => { Assert.Equal("sessionId", p.Name); Assert.Equal(typeof(String), p.ParameterType); }
            });

        }
        public void MultipleResovle()
        {
            (IType[] types, Function[] scope) = init();
            InheritanceHierarchy ih = new InheritanceHierarchy(Arr.create <IType>(types));
            FunctionResolver     fr = new FunctionResolver(ih, new Seq <Function>(scope));

            var res     = fr.resolve(new Function("B_toZ_", new[] { types[1] }, new[] { types[6] }));
            var correct = new Set <string>(new[] { "AtoZ", "BtoZ" });

            match(from someres in res select someres.name,
                  Some: v => Assert.AreEqual(v, correct),
                  None: () => throw new FunctionResolutionError("Function resolution return empty set, when should not"));
        }
Exemple #5
0
 private static Resolver CreateFrom(FunctionResolver resolver)
 {
     return(new Resolver(resolver.IgnoreCase, resolver.LanguageId));
 }
Exemple #6
0
        /// <inheritdoc />
        protected override FunctionResolverBase createFunctionResolver()
        {
            var functionResolver = new FunctionResolver();

            return(functionResolver);
        }
 public override LessNode VisitFunction(LessParser.FunctionContext context)
 {
     return(FunctionResolver.Resolve(context.functionName().GetText(), GetExpression(context.expression())));
 }
Exemple #8
0
        /// <summary>
        /// Is called after each include/require/include_once/require_once expression (can be resolved according to flow.CurrentPartial)
        /// </summary>
        /// <param name="flow">Flow controller where include extensions can be stored</param>
        /// <param name="includeFile">File argument of include statement</param>
        public override void Include(FlowController flow, MemoryEntry includeFile)
        {
            if (FlagsHandler.IsDirty(includeFile.PossibleValues, FlagType.FilePathDirty))
            {
                AnalysisWarningHandler.SetWarning(flow.OutSet, new AnalysisSecurityWarning(flow.CurrentScript.FullName, flow.CurrentPartial, flow.CurrentProgramPoint, FlagType.FilePathDirty, ""));
            }

            bool isAlwaysConcrete = true;
            //extend current program point as Include
            List <string> files = FunctionResolver.GetFunctionNames(includeFile, flow, out isAlwaysConcrete);

            if (isAlwaysConcrete == false)
            {
                AnalysisWarningHandler.SetWarning(flow.OutSet, new AnalysisWarning(flow.CurrentScript.FullName, "Couldn't resolve all possible includes", flow.CurrentPartial, flow.CurrentProgramPoint, AnalysisWarningCause.COULDNT_RESOLVE_ALL_INCLUDES));
            }


            IncludingEx includeExpression = flow.CurrentPartial as IncludingEx;

            foreach (var branchKey in flow.ExtensionKeys)
            {
                if (!files.Remove(branchKey as string))
                {
                    //this include is now not resolved as possible include branch
                    flow.RemoveExtension(branchKey);
                }
            }

            int numberOfWarnings = 0;
            var includedFiles    = flow.OutSet.GetControlVariable(new VariableName(".includedFiles")).ReadMemory(flow.OutSet.Snapshot);

            foreach (var file in files)
            {
                var fileInfo = findFile(flow, file);

                if (fileInfo == null)
                {
                    AnalysisWarningHandler.SetWarning(flow.OutSet, new AnalysisWarning(flow.CurrentScript.FullName, "The file " + file + " to be included not found", flow.CurrentProgramPoint.Partial, flow.CurrentProgramPoint, AnalysisWarningCause.FILE_TO_BE_INCLUDED_NOT_FOUND));
                    numberOfWarnings++;
                    continue;
                }

                string fileName = fileInfo.FullName;

                // Handling include_once, require_once
                var varIncluded = flow.OutSet.GetControlVariable(new VariableName(fileName));
                if (includeExpression.InclusionType == InclusionTypes.IncludeOnce || includeExpression.InclusionType == InclusionTypes.RequireOnce)
                {
                    var includedInfo = varIncluded.ReadMemory(flow.OutSet.Snapshot);
                    if (includedInfo != null)
                    {
                        var includeType = (includeExpression.InclusionType == InclusionTypes.IncludeOnce) ? "include_once" : "require_once";
                        if (includedInfo.PossibleValues.Count() > 1)
                        {
                            //TODO: report or not?
                            //AnalysisWarningHandler.SetWarning (flow.OutSet, new AnalysisWarning (flow.CurrentScript.FullName, includeType + " is called more times in some program paths with the file " + fileName, flow.CurrentProgramPoint.Partial, flow.CurrentProgramPoint, AnalysisWarningCause.INCLUDE_REQUIRE_ONCE_CALLED_MORE_TIMES_WITH_SAME_FILE));
                            // TODO: include the file or not??
                            continue;
                        }
                        else
                        {
                            if (!(includedInfo.PossibleValues.First() is UndefinedValue))
                            {
                                //TODO: report or not?
                                //AnalysisWarningHandler.SetWarning (flow.OutSet, new AnalysisWarning (flow.CurrentScript.FullName, includeType + " is called more times with the file " + fileName, flow.CurrentProgramPoint.Partial, flow.CurrentProgramPoint, AnalysisWarningCause.INCLUDE_REQUIRE_ONCE_CALLED_MORE_TIMES_WITH_SAME_FILE));
                                continue;
                            }
                        }
                    }
                }


                // Avoid recursive includes
                // the file was never included
                int numberOfIncludes = -1;
                // TODO: optimization - avoid iterating all included files
                //  - make .includedFiles associative array with names of functions as indexes
                foreach (InfoValue <NumberOfCalls <string> > includeInfo in includedFiles.PossibleValues.Where(a => (a is InfoValue <NumberOfCalls <string> >)))
                {
                    if (includeInfo.Data.Callee == fileName)
                    {
                        numberOfIncludes = Math.Max(numberOfIncludes, includeInfo.Data.TimesCalled);
                    }
                }
                if (numberOfIncludes >= 0)
                {
                    if (numberOfIncludes > 2 || sharedFiles.Contains(fileName))
                    {
                        if (sharedFiles.Contains(fileName))
                        {
                            //set graph sharing for this function
                            if (!sharedProgramPoints.ContainsKey(fileName))
                            {
                                try
                                {
                                    //create single graph instance
                                    sharedProgramPoints[fileName] = ProgramPointGraph.FromSource(ControlFlowGraph.ControlFlowGraph.FromFile(fileInfo));
                                }
                                catch (ControlFlowGraph.ControlFlowException)
                                {
                                    numberOfWarnings++;
                                    AnalysisWarningHandler.SetWarning(flow.OutSet, new AnalysisWarning(flow.CurrentScript.FullName, "Control flow graph creation error", flow.CurrentPartial, flow.CurrentProgramPoint, AnalysisWarningCause.CFG_EXCEPTION_IN_INCLUDE_OR_EVAL));
                                }
                                catch (Parsers.ParserException)
                                {
                                    numberOfWarnings++;
                                    AnalysisWarningHandler.SetWarning(flow.OutSet, new AnalysisWarning(flow.CurrentScript.FullName, "Parser error", flow.CurrentPartial, flow.CurrentProgramPoint, AnalysisWarningCause.PARSER_EXCEPTION_IN_INCLUDE_OR_EVAL));
                                }
                            }

                            //get shared instance of program point graph
                            flow.AddExtension(fileName, sharedProgramPoints[fileName], ExtensionType.ParallelInclude);
                            continue;
                        }
                        else
                        {
                            sharedFiles.Add(fileName);
                        }
                    }
                }

                try
                {
                    // Write information about inclusion of the file
                    varIncluded.WriteMemory(flow.OutSet.Snapshot, new MemoryEntry(flow.OutSet.Snapshot.CreateBool(true)));

                    //Create graph for every include - NOTE: we can share pp graphs
                    var cfg     = ControlFlowGraph.ControlFlowGraph.FromFile(fileInfo);
                    var ppGraph = ProgramPointGraph.FromSource(cfg);
                    flow.AddExtension(fileName, ppGraph, ExtensionType.ParallelInclude);
                }
                catch (ControlFlowGraph.ControlFlowException)
                {
                    numberOfWarnings++;
                    AnalysisWarningHandler.SetWarning(flow.OutSet, new AnalysisWarning(flow.CurrentScript.FullName, "Control flow graph creation error", flow.CurrentPartial, flow.CurrentProgramPoint, AnalysisWarningCause.CFG_EXCEPTION_IN_INCLUDE_OR_EVAL));
                }
                catch (Parsers.ParserException)
                {
                    numberOfWarnings++;
                    AnalysisWarningHandler.SetWarning(flow.OutSet, new AnalysisWarning(flow.CurrentScript.FullName, "Parser error", flow.CurrentPartial, flow.CurrentProgramPoint, AnalysisWarningCause.PARSER_EXCEPTION_IN_INCLUDE_OR_EVAL));
                }
            }

            if (numberOfWarnings > 0 && (includeExpression.InclusionType == InclusionTypes.Require || includeExpression.InclusionType == InclusionTypes.RequireOnce))
            {
                if (numberOfWarnings == files.Count && isAlwaysConcrete == true)
                {
                    fatalError(flow, true);
                }
                else
                {
                    fatalError(flow, false);
                }
            }
        }