示例#1
0
		public static Jint_Function jintFunction(this Jint_Visitor jintVisitor, MethodCall methodCall)
		{
			var jintFunction = new Jint_Function();			
			var previous = jintVisitor.previous_All<Identifier>(methodCall);     
			if (previous.size()>0)
			{
				jintFunction.Name = previous.first().Text;
				previous.Reverse();  
				var className = "";				
				foreach(var item in previous)
					className= "{0}.{1}".format(className, item.Text);
				className = className.removeFirstChar(); 				 				
				jintFunction.Class = className;
			}
			else
				jintFunction.Name = "[anonymous]";
			jintFunction.Arguments = methodCall.Arguments;
			//foreach(var argument in methodCall.Arguments)
			//	jintFunction.Arguments.Add(argument.str()); 
			return jintFunction;
		}
示例#2
0
		public void Visit(MethodCall expression)
		{
			Builder.Append("(");
			var first = true;
			foreach (var parameter in expression.Arguments)
			{
				if (first == false)
					Builder.Append(", ");
				first = false;
				parameter.Accept(this);
			}
			Builder.Append(")");
		}
示例#3
0
        public void Visit(MethodCall methodCall)
        {
            var that = CallTarget;
            var target = Result;

            if (target == JsUndefined.Instance || Result == null) {
                if (!String.IsNullOrEmpty(lastIdentifier)) {

                }
                else {
                    throw new JsException(Global.TypeErrorClass.New("Method isn't defined"));
                }
            }

            Type[] genericParameters = null;

            if (methodCall.Generics.Count > 0)
            {
                genericParameters = new Type[methodCall.Generics.Count];

                try
                {
                    int i = 0;
                    foreach (Expression generic in methodCall.Generics)
                    {
                        generic.Accept(this);
                        genericParameters[i] = Global.Marshaller.MarshalJsValue<Type>(Result);
                        i++;
                    }
                }
                catch (Exception e)
                {
                    throw new JintException("A type parameter is required", e);
                }
            }

            #region Evaluates parameters
            JsInstance[] parameters = new JsInstance[methodCall.Arguments.Count];

            if (methodCall.Arguments.Count > 0) {

                for (int j = 0; j < methodCall.Arguments.Count; j++) {
                    methodCall.Arguments[j].Accept(this);
                    parameters[j] = Result;
                }

            }
            #endregion

            if (target is JsFunction) {
                JsFunction function = (JsFunction)target;

                if (DebugMode) {
                    string stack = function.Name + "(";
                    string[] paramStrings = new string[parameters.Length];

                    for (int i = 0; i < parameters.Length; i++) {
                        if (parameters[i] != null)
                            paramStrings[i] = parameters[i].ToSource();
                        else
                            paramStrings[i] = "null";
                    }

                    stack += String.Join(", ", paramStrings);
                    stack += ")";
                    CallStack.Push(stack);
                }

                returnInstance = JsUndefined.Instance;

                JsInstance[] original = new JsInstance[parameters.Length];
                parameters.CopyTo(original, 0);

                ExecuteFunction(function, that, parameters, genericParameters);

                for (int i = 0; i < original.Length; i++)
                    if (original[i] != parameters[i]) {
                        if (methodCall.Arguments[i] is MemberExpression && ((MemberExpression)methodCall.Arguments[i]).Member is IAssignable)
                            Assign((MemberExpression)methodCall.Arguments[i], parameters[i]);
                        else if (methodCall.Arguments[i] is Identifier)
                            Assign(new MemberExpression(methodCall.Arguments[i], null), parameters[i]);
                    }

                if (DebugMode) {
                    CallStack.Pop();
                }

                Result = returnInstance;
                returnInstance = JsUndefined.Instance;
                return;
            }
            else {
                throw new JsException(Global.ErrorClass.New("Function expected."));
            }
        }
示例#4
0
        public void Visit(MethodCall methodCall)
        {
            if (Result == JsUndefined.Instance || Result == null)
            {
                if (!String.IsNullOrEmpty(lastIdentifier))
                {
                    throw new JsException(Global.TypeErrorClass.New("Object expected: " + lastIdentifier));
                }
                else
                {
                    throw new JsException(Global.TypeErrorClass.New("Object expected"));
                }
            }

            #region Evaluates parameters
            JsInstance[] parameters = new JsInstance[methodCall.Arguments.Count];
            Type[] types = new Type[methodCall.Arguments.Count];

            if (methodCall.Arguments.Count > 0)
            {
                JsInstance oldResult = Result;
                JsDictionaryObject oldCallTarget = callTarget;
                callTarget = null;
                JsDictionaryObject currentScope = CurrentScope;
                if (currentScope.Class != JsScope.TYPEOF)
                    ExitScope();
                try
                {
                    for (int j = 0; j < methodCall.Arguments.Count; j++)
                    {
                        methodCall.Arguments[j].Accept(this);
                        parameters[j] = Result;
                    }
                }
                finally
                {
                    if (currentScope.Class != JsScope.TYPEOF)
                        EnterScope(currentScope);
                }
                Result = oldResult;
                callTarget = oldCallTarget;
            }
            #endregion

            if (Result.Class == JsFunction.TYPEOF)
            {
                JsFunction function = (JsFunction)Result;

                if (DebugMode)
                {
                    string stack = function.Name + "(";
                    string[] paramStrings = new string[parameters.Length];

                    for (int i = 0; i < parameters.Length; i++)
                    {
                        if (parameters[i] != null)
                            paramStrings[i] = parameters[i].ToSource();
                        else
                            paramStrings[i] = "null";
                    }

                    stack += String.Join(", ", paramStrings);
                    stack += ")";
                    CallStack.Push(stack);
                }

                returnInstance = JsUndefined.Instance;

                ExecuteFunction(function, callTarget, parameters);

                if (DebugMode)
                {
                    CallStack.Pop();
                }

                Result = returnInstance;
                return;
            }

            if (Result.Class == JsClrMethodInfo.TYPEOF)
            {
                // Fallback to CLR methods
                object result = null;

                EnsureClrAllowed();

                #region Converts parameters
                object[] clrParameters = JsClr.ConvertParameters(parameters);
                #endregion

                JsClrMethodInfo clrMethod = (JsClrMethodInfo)Result;
                try
                {

                    List<Type> generics = new List<Type>();
                    if (methodCall.Generics.Count > 0)
                    {
                        JsDictionaryObject oldCallTarget = callTarget;
                        foreach (Expression generic in methodCall.Generics)
                        {
                            generic.Accept(this);

                            if (!(Result.Value is Type))
                            {
                                PermissionSet.PermitOnly();
                                throw new JintException("Invalid type in generics specifier");
                            }

                            generics.Add((Type)Result.Value);
                        }
                        callTarget = oldCallTarget;
                    }

                    MethodInfo methodInfo = methodInvoker.Invoke(callTarget.Value, clrMethod.Value.ToString(), clrParameters, generics.ToArray());

                    PermissionSet.PermitOnly();

                    if (methodInfo == null)
                    {
                        throw new JintException("Method not found with specified arguments: " + clrMethod.Value.ToString());
                    }

                    try
                    {
                        result = methodInfo.Invoke(callTarget.Value, clrParameters);
                    }
                    catch (Exception e)
                    {
                        if (e.InnerException is JsException)
                        {
                            throw e.InnerException;
                        }

                        throw;
                    }
                }
                catch (TargetInvocationException e)
                {
                    // Extract SecurityExpression if thrown
                    throw e.InnerException;
                }
                finally
                {
                    CodeAccessPermission.RevertPermitOnly();
                }

                Result = Global.WrapClr(result);
            }

        }
示例#5
0
        void Analyze(MethodCall Stmt)
        {
            SetCurrentLineAndCharNos(Stmt);
            int LineNo = CurrentLineNo;
            JintItem LastItem;
            if (JintStack.Count > 0)
            {
                LastItem = JintStack[JintStack.Count - 1];
                if (LastItem.State == JintState.MethodCallArgument || LastItem.State == JintState.MethodName)
                {
                    LastItem = new JintItem(CurrentLineNo, 0, JintState.AnonymousMethod);
                    JintStack.Add(LastItem);
                }
            }
            else
            {
                LastItem = new JintItem(CurrentLineNo, 0, JintState.AnonymousMethod);
                JintStack.Add(LastItem);
            }
            int MethodCallIndex = 0;
            if (LastItem.State == JintState.Identifier || LastItem.State == JintState.Property || LastItem.State == JintState.Indexer || LastItem.State == JintState.StringIndex || LastItem.State == JintState.IdentifierIndex || LastItem.State == JintState.IntIndex || LastItem.State == JintState.AnonymousMethod || LastItem.LineNo != LineNo)
            {
                RemoveJintStackFrom(JintStack.Count - 1);
                if (LastItem.State == JintState.Identifier || LastItem.State == JintState.Property)
                    JintStack.Add(new JintItem(LastItem.LineNo, LastItem.CharNo, JintState.MethodCallName, LastItem.Value));
                else
                    JintStack.Add(new JintItem(LastItem.LineNo, LastItem.CharNo, JintState.MethodCallName, ""));

                LineNo = LastItem.LineNo;
                MethodCallIndex = JintStack.Count - 1;

                if (Stmt.Arguments != null)
                {
                    if (Stmt.Arguments.Count > 0)
                        Analyze(Stmt.Arguments);
                    else
                        AddToJintStack(Stmt.Source, JintState.MethodCallArgument);
                }

                List<JintItem> MethodRelatedItems = GetMethodItems(MethodCallIndex);
                ItemChecker IC = new ItemChecker(this);
                List<JintItem> MethodArguments = IC.GetLastMethodArguments(MethodRelatedItems);
                List<int> SourcePositions = new List<int>();
                List<int> SinkPositions = new List<int>();

                for (int i = 0; i < MethodArguments.Count; i++)
                {
                    TaintResult Result = IC.Check(MethodArguments[i].SubItems);
                    if (Result.SourceTaint)
                    {
                        SourcePositions.Add(i);
                        Result.SourceReasons.Add("Method Argument is a Source");
                        AddSourceLine(CurrentLineNo, Result.SourceReasons);
                    }
                    if (Result.SinkTaint)
                    {
                        SinkPositions.Add(i);
                        Result.SinkReasons.Add("Method Argument is a Sink");
                        AddSinkLine(CurrentLineNo, Result.SinkReasons);
                    }
                }

                foreach (List<JintItem> Template in ArgumentAssignedASourceMethods)
                {
                    if(Template.Count == 0) continue;
                    TaintResult MethodResult = IC.IsMatch(MethodRelatedItems, Template);
                    if (MethodResult.NeutralReasons.Count > 0)
                    {
                        List<JintItem> TemplateArguments = IC.GetLastMethodArguments(Template);
                        if(TemplateArguments.Count == 0) continue;
                        AddSourceLine(LineNo, Template[0].SourceReasons);
                        if (TemplateArguments.Count == 1 && TemplateArguments[0].SubItems.Count == 0 && SinkPositions.Count > 0)
                            AddSourceToSinkLine(LineNo);
                        else if (MethodArguments.Count == TemplateArguments.Count)
                        {
                            foreach (int i in SinkPositions)
                            {
                                if(TemplateArguments.Count > i)
                                    if(TemplateArguments[i].SubItems.Count > 0)
                                        if(TemplateArguments[i].SubItems[0].State == JintState.MethodCallArgumentTaintPointer)
                                            AddSourceToSinkLine(LineNo);
                            }
                        }
                    }
                }

                foreach (List<JintItem> Template in ArgumentAssignedToSinkMethods)
                {
                    if (Template.Count == 0) continue;
                    TaintResult MethodResult = IC.IsMatch(MethodRelatedItems, Template);
                    if (MethodResult.NeutralReasons.Count > 0)
                    {
                        List<JintItem> TemplateArguments = IC.GetLastMethodArguments(Template);
                        if (TemplateArguments.Count == 0) continue;
                        AddSinkLine(LineNo, Template[0].SinkReasons);
                        if (TemplateArguments.Count == 1 && TemplateArguments[0].SubItems.Count == 0 && SourcePositions.Count > 0)
                            AddSourceToSinkLine(LineNo);
                        else if (MethodArguments.Count == TemplateArguments.Count)
                        {
                            foreach (int i in SourcePositions)
                            {
                                if (TemplateArguments.Count > i)
                                    if (TemplateArguments[i].SubItems.Count > 0)
                                        if (TemplateArguments[i].SubItems[0].State == JintState.MethodCallArgumentTaintPointer)
                                            AddSourceToSinkLine(LineNo);
                            }
                        }
                    }
                }
            }
            else
            {
                IronException.Report("MethodName missing in IronJint", "LastItem State -" + LastItem.State.ToString());
            }

            if (Stmt.Generics != null) Analyze(Stmt.Generics);
        }