public ArgumentSet Parse(string[] args, ArgumentSet arguments) { if (args == null || args.Length == 0) { return arguments; } _queue = new Lexer().Tokenize(args); _arguments = arguments; Args(); return _arguments; }
private void DeclareParameter(Parameter p, ParameterInfo pi) { IPythonType paramType; // If type is known from annotation, use it. if (pi != null && !pi.Type.IsUnknown() && !pi.Type.IsGenericParameter()) { // TODO: technically generics may have constraints. Should we consider them? paramType = pi.Type; } else { paramType = pi?.DefaultValue?.GetPythonType() ?? UnknownType; } DeclareVariable(p.Name, paramType.CreateInstance(ArgumentSet.Empty(p.NameExpression, this)), VariableSource.Declaration, p.NameExpression); }
public static Configuration Load(ArgumentSet args) { var isReplayRecord = args.Get("recordReplay", false); var replayPath = args.Get("replayPath", null); var savedReplayRecord = ((replayPath != null) && !isReplayRecord) ? new InputRecordConfig(replayPath) : null; var randomSeed = args.Get("randomSeed", 0); if ((isReplayRecord || (replayPath != null)) && (randomSeed == 0)) { throw new InvalidOperationException("Random seed should be set to record/rewind replay!"); } if (isReplayRecord && (replayPath == null)) { throw new InvalidOperationException("Replay path should be set to record replay!"); } return(new Configuration(isReplayRecord, savedReplayRecord, replayPath, randomSeed, 0.0005)); }
public IMember GetValueFromClassCtor(IPythonClassType cls, CallExpression expr) { SymbolTable.Evaluate(cls.ClassDefinition); // Determine argument types var args = ArgumentSet.Empty; var init = cls.GetMember <IPythonFunctionType>(@"__init__"); if (init != null) { var a = new ArgumentSet(init, 0, new PythonInstance(cls), expr, Module, this); if (a.Errors.Count > 0) { // AddDiagnostics(Module.Uri, a.Errors); } args = a.Evaluate(); } return(cls.CreateInstance(cls.Name, GetLoc(expr), args)); }
public IMember GetValueFromIndex(IndexExpression expr, LookupOptions lookupOptions = LookupOptions.Normal) { if (expr?.Target == null) { return(null); } var target = GetValueFromExpression(expr.Target, lookupOptions); // Try generics first since this may be an expression like Dict[int, str] var result = GetValueFromGeneric(target, expr, lookupOptions); if (result != null) { return(result); } if (expr.Index is SliceExpression || expr.Index is TupleExpression) { // When slicing, assume result is the same type return(target); } var type = target.GetPythonType(); if (type != null) { if (!(target is IPythonInstance instance)) { instance = type.CreateInstance(ArgumentSet.Empty(expr, this)) as IPythonInstance; } if (instance != null) { var index = GetValueFromExpression(expr.Index, lookupOptions); if (index != null) { return(type.Index(instance, new ArgumentSet(new[] { index }, expr, this))); } } } return(UnknownType); }
private void HandleTypedVariable(IPythonType variableType, IMember value, Expression expr) { // Check value type for compatibility IMember instance = null; if (value != null) { var valueType = value.GetPythonType(); if (!variableType.IsUnknown() && !valueType.Equals(variableType)) { // TODO: warn incompatible value type. // TODO: verify values. Value may be list() while variable type is List[str]. // Leave it as variable type. } else { instance = value; } } var args = ArgumentSet.Empty(expr, Eval); instance = instance ?? variableType?.CreateInstance(args) ?? Eval.UnknownType.CreateInstance(ArgumentSet.WithoutContext); if (expr is NameExpression ne) { Eval.DeclareVariable(ne.Name, instance, VariableSource.Declaration, ne); return; } if (expr is MemberExpression m) { // self.x : int = 42 var self = Eval.LookupNameInScopes("self", out var scope); var argType = self?.GetPythonType(); if (argType is PythonClassType cls && scope != null) { cls.AddMember(m.Name, instance, true); } } }
public async Task <IMember> GetValueFromClassCtorAsync(IPythonClassType cls, CallExpression expr, CancellationToken cancellationToken = default) { await SymbolTable.EvaluateAsync(cls.ClassDefinition, cancellationToken); // Determine argument types var args = ArgumentSet.Empty; var init = cls.GetMember <IPythonFunctionType>(@"__init__"); if (init != null) { var a = new ArgumentSet(init, new PythonInstance(cls), expr, Module, this); if (a.Errors.Count > 0) { AddDiagnostics(a.Errors); } else { args = await a.EvaluateAsync(cancellationToken); } } return(cls.CreateInstance(cls.Name, GetLoc(expr), args)); }
private IMember GetValueFromUnaryOp(UnaryExpression expr, string op, LookupOptions lookupOptions) { var target = GetValueFromExpression(expr.Expression, lookupOptions); if (target is IPythonInstance instance) { var fn = instance.GetPythonType()?.GetMember <IPythonFunctionType>(op); // Process functions declared in code modules. Scraped/compiled/stub modules do not actually perform any operations. if (fn?.DeclaringModule != null && (fn.DeclaringModule.ModuleType == ModuleType.User || fn.DeclaringModule.ModuleType == ModuleType.Library)) { var result = fn.Call(instance, op, ArgumentSet.Empty(expr, this)); if (!result.IsUnknown()) { return(result); } } return(instance is IPythonConstant c && instance.TryGetConstant <int>(out var value) ? new PythonConstant(-value, c.Type) : instance); } return(UnknownType); }
public ArgumentSet Parse(string[] args, ArgumentSet arguments) { if (args == null || args.Length == 0) { return arguments; } for (int i = 0; i < args.Length; i++) { var arg = args[i]; if (arg.StartsWith("-")) { var parameterName = arg; string parameterValue = null; if (i + 1 <= args.Length) { try { parameterValue = args[i + 1]; } catch {} } arguments[parameterName] = new Argument { IsMatch = true, Value = parameterValue }; i++; } else { arguments[arg] = new Argument { IsMatch = true }; } } return arguments; }
public void ParseSuccess() { var opts = new MyOptions(); var argSet = new ArgumentSet() .Required(opts.RunMe) .Required(opts.File) .Optional(opts.Flag) .Optional(opts.Names) .Optional(opts.Sport) .OnMatch(() => { opts.RunMe.IsPresent.ShouldBeTrue(); opts.File.Value.ShouldBe(@"C:\some\path\file.txt"); // value is convenience to calling Values.Single() opts.File.Value.ShouldBe(@"C:\some\path\file.txt"); opts.File.Values.Single().ShouldBe(@"C:\some\path\file.txt"); // optionl flag, must test if present if (!opts.Sport.IsPresent) { // default value has been assigned opts.Sport.Value.ShouldBe(MyOptions.SportDefault); } // safe to enumerate an optional list opts.Names.Values.Any("Jill").ShouldBeTrue(); opts.Names.Values.Any("Bob").ShouldBeTrue(); }), var args = new string[] {"RunMe", "--Names", "Bob", "Jill", "--file", @"C:\some\path\file.txt"}; LineCommander.Parse(args, argSet) .OnError(bestMatch => { Log.Error($"No matching argument set: did you mean '{bestMatch}'"); }); }
public IMember GetConstantFromLiteral(Expression expr) { if (expr is ConstantExpression ce) { switch (ce.Value) { case string s: return(new PythonUnicodeString(s, Interpreter)); case AsciiString b: return(new PythonAsciiString(b, Interpreter)); case int integer: return(new PythonConstant(integer, Interpreter.GetBuiltinType(BuiltinTypeId.Int))); case bool b: return(new PythonConstant(b, Interpreter.GetBuiltinType(BuiltinTypeId.Bool))); } } var t = SuppressBuiltinLookup ? UnknownType : (GetTypeFromLiteral(expr) ?? UnknownType); return(t.CreateInstance(ArgumentSet.Empty(expr, this))); }
public static IMember GetReturnValueFromAnnotation(ExpressionEval eval, Expression annotation) { if (eval == null || annotation == null) { return(null); } var annotationType = eval.GetTypeFromAnnotation(annotation, LookupOptions.All); if (annotationType.IsUnknown()) { return(null); } // Annotations are typically types while actually functions return // instances unless specifically annotated to a type such as Type[T]. // TODO: try constructing argument set from types. Consider Tuple[_T1, _T2] where _T1 = TypeVar('_T1', str, bytes) var t = annotationType.CreateInstance(ArgumentSet.Empty(annotation, eval)); // If instance could not be created, such as when return type is List[T] and // type of T is not yet known, just use the type. var instance = t.IsUnknown() ? (IMember)annotationType : t; return(instance); }
public void Execute(Dictionary <string, string> arguments) { string connectInfo = ""; bool verbose; ArgumentSet argumentSet; try { argumentSet = ArgumentSet.FromDictionary( arguments, new List <string>() { "/server" }); } catch (Exception e) { Console.WriteLine($"[x] Error: {e.Message}"); return; } argumentSet.GetExtraBool("/verbose", out verbose); SqlConnection connection; SQLExecutor.ConnectionInfo(arguments, argumentSet.connectserver, argumentSet.database, argumentSet.sqlauth, out connectInfo); if (String.IsNullOrEmpty(connectInfo)) { return; } if (!SQLExecutor.Authenticate(connectInfo, out connection)) { return; } // I am confused about why it is necessary to perform this step as a separate procedure // But it seems in-line impersonation doesn't work properly if (!String.IsNullOrEmpty(argumentSet.impersonate)) { Console.WriteLine("[*] Attempting impersonation as {0}", argumentSet.impersonate); SQLExecutor.ExecuteProcedure(connection, "", argumentSet.impersonate); } if (!verbose && String.IsNullOrEmpty(argumentSet.target)) { string procedure = "EXECUTE sp_linkedservers;"; procedure = SQLExecutor.PrepareSimpleStatement(procedure, argumentSet.impersonate); SqlCommand command = new SqlCommand(procedure, connection); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Console.WriteLine("[*] Linked SQL server: " + reader[0]); } } } else { string query = @"SELECT name AS 'SQL Server', is_linked AS 'Linked', is_remote_login_enabled AS 'Remote Login', is_data_access_enabled AS 'Data Access', is_rpc_out_enabled AS 'RPC Out' FROM sys.servers; "; if (String.IsNullOrEmpty(argumentSet.target) && String.IsNullOrEmpty(argumentSet.intermediate)) { SQLExecutor.ExecuteQuery( connection, query, argumentSet.impersonate, true ); } else if (String.IsNullOrEmpty(argumentSet.intermediate)) { SQLExecutor.ExecuteLinkedQuery( connection, query, argumentSet.target, argumentSet.impersonate, argumentSet.impersonate_linked, true ); } else { SQLExecutor.ExecuteDoublyLinkedQuery( connection, query, argumentSet.target, argumentSet.intermediate, argumentSet.impersonate, argumentSet.impersonate_linked, argumentSet.impersonate_intermediate, true ); } } connection.Close(); }
public void Execute(Dictionary <string, string> arguments) { string connectInfo = ""; ArgumentSet argumentSet; try { argumentSet = ArgumentSet.FromDictionary( arguments, new List <string>() { "/server" }); } catch (Exception e) { Console.WriteLine($"[x] Error: {e.Message}"); return; } SqlConnection connection; SQLExecutor.ConnectionInfo(arguments, argumentSet.connectserver, argumentSet.database, argumentSet.sqlauth, out connectInfo); if (String.IsNullOrEmpty(connectInfo)) { return; } if (!SQLExecutor.Authenticate(connectInfo, out connection)) { return; } var procedures = new Dictionary <string, string>(); // I am confused about why it is necessary to perform this step as a separate procedure // But it seems in-line impersonation doesn't work properly if (!String.IsNullOrEmpty(argumentSet.impersonate)) { Console.WriteLine("[*] Attempting impersonation as {0}", argumentSet.impersonate); SQLExecutor.ExecuteProcedure(connection, "", argumentSet.impersonate); } procedures.Add("Enabling RPC..", $"EXEC sp_serveroption '{argumentSet.target}', 'rpc', 'true'; EXEC sp_serveroption '{argumentSet.target}', 'rpc out', 'true';"); foreach (string step in procedures.Keys) { Console.WriteLine("[*] {0}", step); if (String.IsNullOrEmpty(argumentSet.intermediate)) { SQLExecutor.ExecuteProcedure( connection, procedures[step], argumentSet.impersonate ); } else { // This may appear strange, but when we perform this kind of procedure, // we're not on the target server itself, but on an adjacent server SQLExecutor.ExecuteLinkedProcedure( connection, procedures[step], argumentSet.intermediate, argumentSet.impersonate, argumentSet.impersonate_intermediate ); } } connection.Close(); }
public async Task InstallPackage(Package package, ArgumentSet args) { if (package.Product != "binutils") { throw new ArgumentException("Invalid package for installer: " + package.Product); } /* Configure whether to build current and subsequent packages */ bool performBuild = args.GetValues("PerformBuild")[0] == "true"; if (performBuild && package.Dependecies.Any()) { args.SetValue("PerformBuild", "false"); } /* Begin installing dependencies. */ var installTasks = package.Dependecies.Select((pkg) => this.InstallPackage(pkg, args)); /* Get the installation directory and package. */ string installDir = args.GetValues("d:install")[0]; string buildDir = args.GetValues("d:build")[0]; string packagePath = Path.Combine(Path.GetTempPath(), String.Format("{0}-{1}.package", package.Product, Path.GetTempFileName())); // Download package using (WebClient client = new WebClient()) { Console.WriteLine("Beginning download of package '{0}'", package); client.DownloadFileCompleted += (s, e) => { Console.WriteLine("Download of package '{0}' complete", package); }; await client.DownloadFileTaskAsync(package.Location, packagePath); } /* Wait until the dependencies have been installed */ await Task.WhenAll(installTasks); // Unpack package - auto-detect archive type using (Archive archive = new Archive(packagePath)) { archive.ExtractToDirectory(buildDir); } /* Only perform build if requested */ if (performBuild) { var configureArgs = String.Format("{0} --target={1} --prefix=\"{2}\" {3}", Path.Combine(Path.GetFullPath(buildDir), "configure"), args.GetValues("Target")[0], Path.GetFullPath(installDir), args.GetValues("BuildParameters")[0] ); int exitCode; /* Begin configure */ if ((exitCode = Execute("bash", args: configureArgs).ExitCode) != 0) { throw new Exception("Error configuring binutils: " + exitCode.ToString()); } /* Begin build */ if ((exitCode = Execute("make", workingDir: Path.GetFullPath(buildDir)).ExitCode) != 0) { throw new Exception("Error building binutils: " + exitCode.ToString()); } /* Begin install */ if ((exitCode = Execute("make", args: "install", workingDir: Path.GetFullPath(buildDir)).ExitCode) != 0) { throw new Exception("Error installing binutils: " + exitCode.ToString()); } } }
internal virtual bool UpdateParameters(ArgumentSet callArgs, bool enqueue = true) { return(((FunctionScope)Scope).UpdateParameters(this, callArgs, enqueue, null)); }
internal override bool UpdateParameters(ArgumentSet callArgs, bool enqueue = true) { var defScope = _originalUnit.Scope; return(((FunctionScope)Scope).UpdateParameters(this, callArgs, enqueue, (FunctionScope)defScope)); }
internal bool UpdateParameters(ArgumentSet callArgs, bool enqueue = true) { var defScope = _originalUnit != null ? _originalUnit.Scope as FunctionScope : null; return(((FunctionScope)Scope).UpdateParameters(this, callArgs, enqueue, defScope)); }
public void Execute(Dictionary <string, string> arguments) { string connectInfo; bool verbose; bool exclude_default; ArgumentSet argumentSet; try { argumentSet = ArgumentSet.FromDictionary( arguments, new List <string>() { "/server" }); } catch (Exception e) { Console.WriteLine($"[x] Error: {e.Message}"); return; } argumentSet.GetExtraBool("/verbose", out verbose); argumentSet.GetExtraBool("/nodefault", out exclude_default); SqlConnection connection; SQLExecutor.ConnectionInfo(arguments, argumentSet.connectserver, argumentSet.database, argumentSet.sqlauth, out connectInfo); if (String.IsNullOrEmpty(connectInfo)) { return; } if (!SQLExecutor.Authenticate(connectInfo, out connection)) { return; } // I am confused about why it is necessary to perform this step as a separate procedure // But it seems in-line impersonation doesn't work properly if (!String.IsNullOrEmpty(argumentSet.impersonate)) { Console.WriteLine("[*] Attempting impersonation as {0}", argumentSet.impersonate); SQLExecutor.ExecuteProcedure(connection, "", argumentSet.impersonate); } string select = verbose ? "name AS 'Database', suser_sname(owner_sid) AS 'Owner', is_trustworthy_on AS 'Trustworthy'": "name AS 'Database'"; string where = exclude_default ? "WHERE name NOT IN('master', 'tempdb', 'model', 'msdb')" : ""; var queries = new List <string>(); queries.Add( $@"SELECT {select} FROM sys.databases {where};" ); foreach (string query in queries) { if (String.IsNullOrEmpty(argumentSet.target) && String.IsNullOrEmpty(argumentSet.intermediate)) { SQLExecutor.ExecuteQuery( connection, query, argumentSet.impersonate, true ); } else if (String.IsNullOrEmpty(argumentSet.intermediate)) { SQLExecutor.ExecuteLinkedQuery( connection, query, argumentSet.target, argumentSet.impersonate, argumentSet.impersonate_linked, true ); } else { SQLExecutor.ExecuteDoublyLinkedQuery( connection, query, argumentSet.target, argumentSet.intermediate, argumentSet.impersonate, argumentSet.impersonate_linked, argumentSet.impersonate_intermediate, true ); } } connection.Close(); }
/// <summary> /// Primary Program Execution /// </summary> private void Run(string[] args) { string executableName = Process.GetCurrentProcess().ProcessName + ".exe"; ArgumentSet allArguments = ArgumentSet.Parse(args); var helpWriter = new CommandHelpWriter(_logger); bool showHelp = allArguments.ContainsName("help"); string commandName = showHelp ? allArguments.GetByName("help") : allArguments.AnonymousArgs.FirstOrDefault(); ICommand command = null; if (commandName != null) { command = _commandRepo.GetCommand(commandName); } if (command == null) { if (showHelp) { // no command name was found, show the list of available commands WriteAppUsageHelp(executableName); helpWriter.WriteCommandList(_commandRepo.Commands); } else { // invalid command name was given _logger.WriteError("'{0}' is not a DotNetMigrations command.", commandName); _logger.WriteLine(string.Empty); _logger.WriteError("See '{0} -help' for a list of available commands.", executableName); } } if (showHelp && command != null) { // show help for the given command helpWriter.WriteCommandHelp(command, executableName); } else if (command != null) { command.Log = _logger; var commandArgumentSet = ArgumentSet.Parse(args.Skip(1).ToArray()); IArguments commandArgs = command.CreateArguments(); commandArgs.Parse(commandArgumentSet); if (commandArgs.IsValid) { var timer = new Stopwatch(); timer.Start(); try { command.Run(commandArgs); } catch (Exception ex) { //_logger.WriteLine(string.Empty); if (_logFullErrors) { _logger.WriteError(ex.ToString()); } else { WriteShortErrorMessages(ex); } if (Debugger.IsAttached) { throw; } } finally { timer.Stop(); _logger.WriteLine(string.Empty); _logger.WriteLine(string.Format("Command duration was {0}.", decimal.Divide(timer.ElapsedMilliseconds, 1000).ToString( "0.0000s"))); } } else { // argument validation failed, show errors WriteValidationErrors(command.CommandName, commandArgs.Errors); _logger.WriteLine(string.Empty); helpWriter.WriteCommandHelp(command, executableName); } } if (_keepConsoleOpen) { Console.WriteLine(); Console.WriteLine(" > Uh-oh. It looks like you didn't run me from a console."); Console.WriteLine(" > Did you double-click me?"); Console.WriteLine(" > I don't like to be clicked."); Console.WriteLine(" > Please open a command prompt and run me by typing " + executableName + "."); Console.WriteLine(); Console.WriteLine("Press any key to exit..."); Console.Read(); } }
static async Task StartMain(string[] args) { var @params = new ArgumentSet(); @params.AddLinkedFlags("f:p", "format-plain"); @params.AddOption("d:package", "packages/"); @params.AddOption("d:install", "tools/"); @params.AddOption("d:build", null); @params.AddOption("version", "*"); @params.AddOption("repo", "*"); @params.Parse(args); var repo = new MasterRepository(); MasterRepository.Register(new GnuRepository()); if ("list".Equals(@params[0], StringComparison.CurrentCultureIgnoreCase)) { IEnumerable <Object> output; if ("packages".Equals(@params[1], StringComparison.CurrentCultureIgnoreCase)) { if (!String.IsNullOrWhiteSpace(@params[2])) { output = await repo.QueryPackages(@params[2]); } else { output = await repo.QueryPackages(); } } else if ("products".Equals(@params[1], StringComparison.CurrentCultureIgnoreCase)) { output = await repo.QueryProducts(); } else if ("installed".Equals(@params[1], StringComparison.CurrentCultureIgnoreCase)) { throw new NotImplementedException(); } else { Console.WriteLine("[ERROR] Unknown command"); throw new Exception(); } if ([email protected]("f:p")) { Console.WriteLine("\n{0}:", @params[1]); foreach (Object obj in output) { Console.WriteLine(" - " + obj.ToString()); } } else { foreach (Object obj in output) { Console.WriteLine(obj.ToString()); } } } else if ("download".Equals(@params[0], StringComparison.CurrentCultureIgnoreCase)) { } /*Package lastestBinutils = (from pkg in await repo.QueryPackages("binutils") * orderby pkg.Version select pkg).FirstOrDefault(); * * var installer = new Installers.BinutilsInstaller(); * * await Task.WhenAll(installer.InstallPackage(lastestBinutils, @params));*/ using (Archive arch = new Archive(@"J:\Users\James\Downloads\binutils-2.24.tar.gz")) { arch.ExtractToDirectory(Directory.CreateDirectory("test").FullName); } }
public IReadOnlyList <IParameterInfo> CreateFunctionParameters( IPythonClassType self, IPythonClassMember function, FunctionDefinition fd, bool declareVariables) { // For class method no need to add extra parameters, but first parameter type should be the class. // For static and unbound methods do not add or set anything. // For regular bound methods add first parameter and set it to the class. var parameters = new List <ParameterInfo>(); var skip = 0; if (self != null && function.HasClassFirstArgument()) { var p0 = fd.Parameters.FirstOrDefault(); if (p0 != null && !string.IsNullOrEmpty(p0.Name)) { var annType = GetTypeFromAnnotation(p0.Annotation, out var isGeneric); // Actual parameter type will be determined when method is invoked. // The reason is that if method might be called on a derived class. // Declare self or cls in this scope. if (declareVariables) { DeclareVariable(p0.Name, self.CreateInstance(ArgumentSet.Empty(p0.NameExpression, this)), VariableSource.Declaration, p0.NameExpression); } // Set parameter info, declare type as annotation type for generic self // e.g def test(self: T) var pi = new ParameterInfo(Ast, p0, isGeneric ? annType : self, null, false); parameters.Add(pi); skip++; } } // Declare parameters in scope for (var i = skip; i < fd.Parameters.Length; i++) { var p = fd.Parameters[i]; if (!string.IsNullOrEmpty(p.Name)) { var defaultValue = GetValueFromExpression(p.DefaultValue); var paramType = GetTypeFromAnnotation(p.Annotation, out var isGeneric) ?? UnknownType; if (paramType.IsUnknown()) { // If parameter has default value, look for the annotation locally first // since outer type may be getting redefined. Consider 's = None; def f(s: s = 123): ... paramType = GetTypeFromAnnotation(p.Annotation, out isGeneric, LookupOptions.Local | LookupOptions.Builtins); // Default value of None does not mean the parameter is None, just says it can be missing. defaultValue = defaultValue.IsUnknown() || defaultValue.IsOfType(BuiltinTypeId.None) ? null : defaultValue; if (paramType == null && defaultValue != null) { paramType = defaultValue.GetPythonType(); } } // If all else fails, look up globally. var pi = new ParameterInfo(Ast, p, paramType, defaultValue, isGeneric | paramType.IsGeneric()); if (declareVariables) { DeclareParameter(p, pi); } parameters.Add(pi); } else if (p.IsList || p.IsDictionary) { parameters.Add(new ParameterInfo(Ast, p, null, null, false)); } } return(parameters); }
public static Field Simple(string name) { return(new Field(string.Empty, name, ArgumentSet.Empty(), DirectiveSet.Empty(), SelectionSet.Empty())); }
public IMember GetValueFromFunctionType(IPythonFunctionType fn, IPythonInstance instance, CallExpression expr) { // If order to be able to find matching overload, we need to know // parameter types and count. This requires function to be analyzed. // Since we don't know which overload we will need, we have to // process all known overloads for the function. foreach (var o in fn.Overloads) { SymbolTable.Evaluate(o.FunctionDefinition); } // Pick the best overload. FunctionDefinition fd; ArgumentSet args; var instanceType = instance?.GetPythonType(); if (fn.Overloads.Count == 1) { fd = fn.Overloads[0].FunctionDefinition; args = new ArgumentSet(fn, 0, instanceType, expr, this); args = args.Evaluate(); } else { args = FindOverload(fn, instanceType, expr); if (args == null) { return(UnknownType); } fd = fn.Overloads.Count > 0 ? fn.Overloads[args.OverloadIndex].FunctionDefinition : null; } // Re-declare parameters in the function scope since originally // their types might not have been known and now argument set // may contain concrete values. if (fd != null && EvaluateFunctionBody(fn)) { using (OpenScope(fn.DeclaringModule, fn.FunctionDefinition, out _)) { args.DeclareParametersInScope(this); } } // If instance type is not the same as the declaring type, then call most probably comes // from the derived class which means that the original 'self' and 'cls' variables // are no longer valid and function has to be re-evaluated with new arguments. // Note that there is nothing to re-evaluate in stubs. if (instanceType == null || fn.DeclaringType == null || fn.IsSpecialized || instanceType.IsSpecialized || fn.DeclaringType.IsSpecialized || instanceType.Equals(fn.DeclaringType) || fn.IsStub || !string.IsNullOrEmpty(fn.Overloads[args.OverloadIndex].GetReturnDocumentation())) { LoadFunctionDependencyModules(fn); var m = instance?.Call(fn.Name, args) ?? fn.Call(null, fn.Name, args); if (!m.IsUnknown()) { return(m); } } // We could not tell the return type from the call. Here we try and evaluate with specific arguments. // Note that it does not make sense evaluating stubs or compiled/scraped modules since they // should be either annotated or static return type known from the analysis. // // Also, we do not evaluate library code with arguments for performance reasons. // This will prevent cases like // def func(a, b): return a + b // from working in libraries, but this is small sacrifice for significant performance // increase in library analysis. if (fn.DeclaringModule is IDocument && EvaluateFunctionBody(fn)) { // Stubs are coming from another module. return(TryEvaluateWithArguments(fn, args)); } return(UnknownType); }
public CalledFunctionAnalysisUnit(IVersioned agg, FunctionAnalysisUnit originalUnit, CallChain callChain, ArgumentSet callArgs) : base(originalUnit.Function, originalUnit._declUnit) { _originalUnit = originalUnit; _agg = agg; CallChain = callChain; var scope = new FunctionScope( Function, Ast, originalUnit.Scope.OuterScope, originalUnit.DeclaringModule.ProjectEntry ); scope.UpdateParameters(this, callArgs, false, originalUnit.Scope as FunctionScope); _scope = scope; var walker = new OverviewWalker(_originalUnit.ProjectEntry, this, Tree); if (Ast.Body != null) { Ast.Body.Walk(walker); } AnalysisLog.NewUnit(this); Enqueue(); }
private IMember GetValueFromProperty(IPythonPropertyType p, IPythonInstance instance, CallExpression expr) { // Function may not have been walked yet. Do it now. SymbolTable.Evaluate(p.FunctionDefinition); return(instance.Call(p.Name, ArgumentSet.Empty(expr, this))); }
public void Execute(Dictionary <string, string> arguments) { string connectInfo = ""; string query = ""; ArgumentSet argumentSet; try { argumentSet = ArgumentSet.FromDictionary( arguments, new List <string>() { "/server", "/command", "/target" }); } catch (Exception e) { Console.WriteLine($"[x] Error: {e.Message}"); return; } argumentSet.GetExtraString("/query", out query); SqlConnection connection; SQLExecutor.ConnectionInfo(arguments, argumentSet.connectserver, argumentSet.database, argumentSet.sqlauth, out connectInfo); if (String.IsNullOrEmpty(connectInfo)) { return; } if (!SQLExecutor.Authenticate(connectInfo, out connection)) { return; } // I am confused about why it is necessary to perform this step as a separate procedure // But it seems in-line impersonation doesn't work properly if (!String.IsNullOrEmpty(argumentSet.impersonate)) { Console.WriteLine("[*] Attempting impersonation as {0}", argumentSet.impersonate); SQLExecutor.ExecuteProcedure(connection, "", argumentSet.impersonate); } if (String.IsNullOrEmpty(argumentSet.intermediate)) { SQLExecutor.ExecuteLinkedQuery( connection, query, argumentSet.target, argumentSet.impersonate, argumentSet.impersonate_linked ); } else { SQLExecutor.ExecuteDoublyLinkedQuery( connection, query, argumentSet.target, argumentSet.intermediate, argumentSet.impersonate, argumentSet.impersonate_linked, argumentSet.impersonate_intermediate ); } connection.Close(); }
public override IMember Index(IArgumentSet args) => _collectionType.Index(this, args).GetPythonType().CreateInstance(ArgumentSet.Empty(args.Expression, args.Eval));
internal bool UpdateParameters( FunctionAnalysisUnit unit, ArgumentSet others, bool enqueue = true, FunctionScope scopeWithDefaultParameters = null, bool usePlaceholders = false ) { EnsureParameters(unit, usePlaceholders); var astParams = Function.FunctionDefinition.ParametersInternal; var added = false; var entry = unit.DependencyProject; var state = unit.State; var limits = state.Limits; for (int i = 0; i < others.Args.Length && i < astParams.Length; ++i) { var name = astParams[i].Name; VariableDef param; if (string.IsNullOrEmpty(name)) { continue; } if (name == _seqParameters?.Name) { param = _seqParameters; } else if (name == _dictParameters?.Name) { param = _dictParameters; } else if (!_parameters.TryGetValue(name, out param)) { Debug.Fail($"Parameter {name} has no variable in this function"); _parameters[name] = param = new LocatedVariableDef(Function.AnalysisUnit.ProjectEntry, new EncodedLocation(unit, (Node)astParams[i].NameExpression ?? astParams[i])); } var arg = others.Args[i].Resolve(unit); param.MakeUnionStrongerIfMoreThan(limits.NormalArgumentTypes, arg); added |= param.AddTypes(entry, arg, enqueue, unit.ProjectEntry); } if (_seqParameters != null) { var arg = others.SequenceArgs.Resolve(unit); _seqParameters.List.MakeUnionStrongerIfMoreThan(limits.ListArgumentTypes, arg); added |= _seqParameters.List.AddTypes(unit, new[] { arg }); } if (_dictParameters != null) { var arg = others.DictArgs.Resolve(unit); _dictParameters.Dict.MakeUnionStrongerIfMoreThan(limits.DictArgumentTypes, arg); added |= _dictParameters.Dict.AddTypes(Function.FunctionDefinition, unit, state.GetConstant(""), arg); } if (scopeWithDefaultParameters != null) { for (int i = 0; i < others.Args.Length && i < astParams.Length; ++i) { VariableDef defParam, param; if (TryGetVariable(astParams[i].Name, out param) && !param.HasTypes && scopeWithDefaultParameters.TryGetVariable(astParams[i].Name, out defParam)) { param.MakeUnionStrongerIfMoreThan( limits.NormalArgumentTypes, defParam.GetTypesNoCopy(unit, AnalysisValue.DeclaringModule) ); added |= param.AddTypes(entry, defParam.GetTypesNoCopy(unit, AnalysisValue.DeclaringModule), enqueue, unit.ProjectEntry); } } } if (enqueue && added) { unit.Enqueue(); } return(added); }
public void Execute(Dictionary <string, string> arguments) { string connectInfo = ""; string cmd = ""; ArgumentSet argumentSet; try { argumentSet = ArgumentSet.FromDictionary( arguments, new List <string>() { "/server", "/command" }); } catch (Exception e) { Console.WriteLine($"[x] Error: {e.Message}"); return; } argumentSet.GetExtraString("/command", out cmd); SqlConnection connection; SQLExecutor.ConnectionInfo(arguments, argumentSet.connectserver, argumentSet.database, argumentSet.sqlauth, out connectInfo); if (String.IsNullOrEmpty(connectInfo)) { return; } if (!SQLExecutor.Authenticate(connectInfo, out connection)) { return; } // I am confused about why it is necessary to perform this step as a separate procedure // But it seems in-line impersonation doesn't work properly if (!String.IsNullOrEmpty(argumentSet.impersonate)) { Console.WriteLine("[*] Attempting impersonation as {0}", argumentSet.impersonate); SQLExecutor.ExecuteProcedure(connection, "", argumentSet.impersonate); } var procedures = new Dictionary <string, string>(); procedures.Add("Enabling advanced options..", $"EXEC sp_configure 'show advanced options', 1; RECONFIGURE;"); procedures.Add("Enabling 'xp_cmdshell'..", $"EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;"); procedures.Add("Executing command..", $"EXEC xp_cmdshell 'powershell -enc {cmd}';"); procedures.Add("Disabling 'xp_cmdshell'..", $"EXEC sp_configure 'xp_cmdshell', 0; RECONFIGURE;"); foreach (string step in procedures.Keys) { Console.WriteLine("[*] {0}", step); if (String.IsNullOrEmpty(argumentSet.target) && String.IsNullOrEmpty(argumentSet.intermediate)) { SQLExecutor.ExecuteProcedure( connection, procedures[step], argumentSet.impersonate ); } else if (String.IsNullOrEmpty(argumentSet.intermediate)) { SQLExecutor.ExecuteLinkedProcedure( connection, procedures[step], argumentSet.target, argumentSet.impersonate, argumentSet.impersonate_linked ); } else { SQLExecutor.ExecuteDoubleLinkedProcedure( connection, procedures[step], argumentSet.target, argumentSet.intermediate, argumentSet.impersonate, argumentSet.impersonate_linked, argumentSet.impersonate_intermediate ); } } connection.Close(); }
public void Execute(Dictionary <string, string> arguments) { string connectInfo; bool permissions; ArgumentSet argumentSet; try { argumentSet = ArgumentSet.FromDictionary( arguments, new List <string>() { "/server" }); } catch (Exception e) { Console.WriteLine($"[x] Error: {e.Message}"); return; } argumentSet.GetExtraBool("/permissions", out permissions); SqlConnection connection; SQLExecutor.ConnectionInfo(arguments, argumentSet.connectserver, argumentSet.database, argumentSet.sqlauth, out connectInfo); if (String.IsNullOrEmpty(connectInfo)) { return; } if (!SQLExecutor.Authenticate(connectInfo, out connection)) { return; } // I am confused about why it is necessary to perform this step as a separate procedure // But it seems in-line impersonation doesn't work properly if (!String.IsNullOrEmpty(argumentSet.impersonate)) { Console.WriteLine("[*] Attempting impersonation as {0}", argumentSet.impersonate); SQLExecutor.ExecuteProcedure(connection, "", argumentSet.impersonate); } var queries = new List <string>(); queries.Add("SELECT SYSTEM_USER as 'Logged in as', CURRENT_USER as 'Mapped as';"); queries.Add("SELECT IS_SRVROLEMEMBER('public') as 'Public role';"); queries.Add("SELECT IS_SRVROLEMEMBER('sysadmin') as 'Sysadmin role';"); foreach (string query in queries) { if (String.IsNullOrEmpty(argumentSet.target) && String.IsNullOrEmpty(argumentSet.intermediate)) { SQLExecutor.ExecuteQuery( connection, query, argumentSet.impersonate); } else if (String.IsNullOrEmpty(argumentSet.intermediate)) { SQLExecutor.ExecuteLinkedQuery( connection, query, argumentSet.target, argumentSet.impersonate, argumentSet.impersonate_linked ); } else { SQLExecutor.ExecuteDoublyLinkedQuery( connection, query, argumentSet.target, argumentSet.intermediate, argumentSet.impersonate, argumentSet.impersonate_linked, argumentSet.impersonate_intermediate ); } } /* * The following query is quite difficult to wrap within my SQLExecutor, mostly due to the fact I implemented the output in tabular format */ if (permissions) { Console.WriteLine("[*] Checking user permissions.."); string query = @"SELECT * FROM(SELECT 'OBJECT' AS entity_class, NAME, subentity_name, permission_name FROM sys.objects CROSS APPLY fn_my_permissions(QUOTENAME(NAME), 'OBJECT') a UNION ALL SELECT 'DATABASE' AS entity_class, NAME, subentity_name, permission_name FROM sys.databases CROSS APPLY fn_my_permissions(QUOTENAME(NAME), 'DATABASE') a UNION ALL SELECT 'SERVER' AS entity_class, @@SERVERNAME AS NAME, subentity_name, permission_name FROM fn_my_permissions(NULL, 'SERVER')) p ORDER BY entity_class, NAME"; if (!String.IsNullOrEmpty(argumentSet.intermediate) && !String.IsNullOrEmpty(argumentSet.target)) { query = SQLExecutor.PrepareDoublyLinkedQuery( query, argumentSet.target, argumentSet.intermediate, argumentSet.impersonate, argumentSet.impersonate_linked, argumentSet.impersonate_intermediate ); } else if (!String.IsNullOrEmpty(argumentSet.target)) { query = SQLExecutor.PrepareLinkedQuery( query, argumentSet.target, argumentSet.impersonate, argumentSet.impersonate_linked ); } SqlCommand command = new SqlCommand(query, connection); TablePrinter.PrintRow("ENTITY", "NAME", "SUBENTITY", "PERMISSION"); TablePrinter.PrintLine(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { TablePrinter.PrintRow(reader.GetString(0), reader.GetString(1), reader.GetString(2), reader.GetString(3)); } } TablePrinter.PrintLine(); } connection.Close(); }
public void Execute(Dictionary <string, string> arguments) { string connectInfo = ""; ArgumentSet argumentSet; try { argumentSet = ArgumentSet.FromDictionary( arguments, new List <string>() { "/server" }); } catch (Exception e) { Console.WriteLine($"[x] Error: {e.Message}"); return; } SqlConnection connection; SQLExecutor.ConnectionInfo(arguments, argumentSet.connectserver, argumentSet.database, argumentSet.sqlauth, out connectInfo); if (String.IsNullOrEmpty(connectInfo)) { return; } if (!SQLExecutor.Authenticate(connectInfo, out connection)) { return; } // I am confused about why it is necessary to perform this step as a separate procedure // But it seems in-line impersonation doesn't work properly if (!String.IsNullOrEmpty(argumentSet.impersonate)) { Console.WriteLine("[*] Attempting impersonation as {0}", argumentSet.impersonate); SQLExecutor.ExecuteProcedure(connection, "", argumentSet.impersonate); } var queries = new List <string>(); queries.Add("SELECT SYSTEM_USER as 'Logged in as', CURRENT_USER as 'Mapped as';"); queries.Add("SELECT distinct b.name AS 'Login that can be impersonated' FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = 'IMPERSONATE';"); queries.Add("SELECT name as 'Owner that can be impersonated', db as 'Trustworthy DB' FROM (SELECT distinct b.name FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = 'IMPERSONATE') impersonable LEFT JOIN (select name AS db, suser_sname( owner_sid ) as owner, is_trustworthy_on from sys.databases) owners ON owners.owner = impersonable.name WHERE is_trustworthy_on = 1;"); foreach (string query in queries) { if (String.IsNullOrEmpty(argumentSet.target) && String.IsNullOrEmpty(argumentSet.intermediate)) { SQLExecutor.ExecuteQuery( connection, query, argumentSet.impersonate ); } else if (String.IsNullOrEmpty(argumentSet.intermediate)) { SQLExecutor.ExecuteLinkedQuery( connection, query, argumentSet.target, argumentSet.impersonate, argumentSet.impersonate_linked ); } else { SQLExecutor.ExecuteDoublyLinkedQuery( connection, query, argumentSet.target, argumentSet.intermediate, argumentSet.impersonate, argumentSet.impersonate_linked, argumentSet.impersonate_intermediate ); } } connection.Close(); }
private IMember GetValueFromMember(MemberExpression expr) { if (expr?.Target == null || string.IsNullOrEmpty(expr.Name)) { return(null); } IPythonInstance instance = null; var m = GetValueFromExpression(expr.Target); if (m is IPythonType typeInfo) { var member = typeInfo.GetMember(expr.Name); // If container is class/type info rather than the instance, then the method is an unbound function. // Example: C.f where f is a method of C. Compare to C().f where f is bound to the instance of C. if (member is PythonFunctionType f && !f.IsStatic && !f.IsClassMethod) { f.AddReference(GetLocationOfName(expr)); return(f.ToUnbound()); } instance = new PythonInstance(typeInfo); } instance = instance ?? m as IPythonInstance; var type = m?.GetPythonType(); // Try inner type var value = type?.GetMember(expr.Name); type?.AddMemberReference(expr.Name, this, GetLocationOfName(expr)); if (type is IPythonModule) { return(value); } // Class type GetMember returns a type. However, class members are // mostly instances (consider self.x = 1, x is an instance of int). // However, it is indeed possible to have them as types, like in // class X ... // class C: ... // self.x = X // which is somewhat rare as compared to self.x = X() but does happen. switch (value) { case IPythonClassType _: return(value); case IPythonPropertyType prop: return(prop.Call(instance, prop.Name, ArgumentSet.Empty(expr, this))); case IPythonType p: return(new PythonBoundType(p, instance)); case null: Log?.Log(TraceEventType.Verbose, $"Unknown member {expr.ToCodeString(Ast).Trim()}"); return(UnknownType); default: return(value); } }