Exemplo n.º 1
0
 public static byte[] Compile(CompilationRequest request)
 {
     if (request.Language == Models.Grunts.ImplantLanguage.CSharp)
     {
         return(CompileCSharp((CsharpCompilationRequest)request));
     }
     return(null);
 }
Exemplo n.º 2
0
        public CompilationResult Compile(CompilationRequest request)
        {
            if (request.NeedTypeReloading || _typeCache == null)
            {
                _typeCache = _platform.ProvideTypes();
            }

            var files = request.Sources.OfType <FileSource>()
                        .Select((fs, index) => new InputFile(index, fs.Path, AmmyLanguage.Instance))
                        .ToArray();

            if (files.Length > 0)
            {
                var cts        = new CancellationTokenSource();
                var references = request.ReferenceAssemblyPaths.Select(path => new FileLibReference(path));
                var project    = new FsProject <Start>(new FsSolution <Start>(), request.ProjectDir, files, references)
                {
                    Data = request.Data
                };

                var projectSupport = files.Select(f => f.Ast)
                                     .OfType <IProjectSupport>()
                                     .FirstOrDefault();

                if (projectSupport == null)
                {
                    throw new Exception("IProjectSupport not found");
                }

                if (!(projectSupport is Start))
                {
                    throw new Exception("IProjectSupport should have AST type Start");
                }

                var start = (Start)projectSupport;

                start.Platform = _platform;

                var data = projectSupport.RefreshReferences(cts.Token, project);

                var fileEvals = files.Select(fs => fs.GetEvalPropertiesData())
                                .ToImmutableArray();

                projectSupport.RefreshProject(cts.Token, fileEvals, request.Data ?? data);

                var compilerMessages = fileEvals.SelectMany(f => f.GetCompilerMessage())
                                       .Select(ToBackendCompilerMessage)
                                       .ToArray();

                var hasError    = compilerMessages.FirstOrDefault(msg => msg.Type == CompilerMessageType.Error) != null;
                var outputFiles = files.SelectMany(ToOutputFile).ToArray();

                return(new CompilationResult("", !hasError, compilerMessages, outputFiles));
            }

            return(new CompilationResult("", true, new CompilerMessage[0], new OutputFile[0]));
        }
Exemplo n.º 3
0
        private static string ToJson(CompilationRequest request)
        {
            string RequestFormat =
                @"{{
                ""source"": ""{0}"",
                ""frameworkVersion"": {1},
                ""outputKind"": {2}
              }}";

            return(String.Format(RequestFormat, JavaScriptStringEncode(request.Source), request.TargetDotNetVersion.ToString("D"), request.OutputKind.ToString("D")));
        }
Exemplo n.º 4
0
        public static byte[] Compile(CompilationRequest request)
        {
            switch (request.Language)
            {
            case Models.Grunts.ImplantLanguage.CSharp:
                return(CompileCSharp(request));

            default:
                return(CompileCSharp(request));
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Add a contact to publication
        /// </summary>
        /// <param name="publicationID">ID of publication</param>
        /// <param name="contactID">ID of contact</param>
        /// <param name="token">token</param>
        public CompilationRequest addContactToPublication(int publicationID, int contactID, string token)
        {
            CompilationRequest cr = new CompilationRequest()
            {
                contactID     = contactID,
                publicationID = publicationID,
                token         = token
            };

            return(AddEntity(cr));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Add a public publication
        /// </summary>
        /// <param name="publicationID">ID of publication</param>
        public CompilationRequest addPublicPublication(int publicationID)
        {
            CompilationRequest cr = new CompilationRequest()
            {
                //public contact
                contactID     = 1,
                publicationID = publicationID,
                token         = null
            };

            return(AddEntity(cr));
        }
        public CompilationResult Compile([FromBody] CompilationRequest request)
        {
            CompilationResult result   = new CompilationResult();
            LexicalAnalyzer   analyzer = new LexicalAnalyzer(LexicalLanguage.GetLanguage(), request.Input);
            BottomUpParser    parser   = new BottomUpParser(analyzer);

            parser.Parse();

            result.IL       = parser.GetILAsString();
            result.Assembly = new CodeGenerator().GenerateAsString(parser.GetIL());

            return(result);
        }
Exemplo n.º 8
0
 public byte[] Compile(CompilationRequest compilationRequest)
 {
     this.SetHeaders();
     try
     {
         return(Convert.FromBase64String(client.UploadString(this.SharpShellURI, ToJson(compilationRequest))));
     }
     catch (WebException e)
     {
         using (var reader = new StreamReader(e.Response.GetResponseStream()))
         {
             throw new CompilationException("CompilationException: " + reader.ReadToEnd());
         }
     }
 }
        public ASTResult AST([FromBody] CompilationRequest request)
        {
            ASTResult result = new ASTResult();

            LexicalAnalyzer analyzer = new LexicalAnalyzer(LexicalLanguage.GetLanguage(), request.Input);
            BottomUpParser  parser   = new BottomUpParser(analyzer);

            parser.Parse();

            result.AST += "digraph C {\r\n";
            result.AST += parser.TopLevelAST.ToDot();
            result.AST += "}";

            return(result);
        }
Exemplo n.º 10
0
        public static byte[] Compile(CompilationRequest request)
        {
            var sourceSyntaxTrees = Directory.GetFiles(request.SourceDirectory, "*.cs", SearchOption.AllDirectories)
                                    .Select(f => new SourceSyntaxTree {
                FileName = f, SyntaxTree = CSharpSyntaxTree.ParseText(File.ReadAllText(f), new CSharpParseOptions())
            })
                                    .ToList();

            var compilationTrees = sourceSyntaxTrees.Select(s => s.SyntaxTree).ToList();
            var options          = new CSharpCompilationOptions(outputKind: request.OutputKind, optimizationLevel: OptimizationLevel.Release, platform: request.Platform);

            var compilation = CSharpCompilation.Create(
                request.AssemblyName,
                compilationTrees,
                request.References.Where(r => r.Framework == request.TargetDotNetVersion).Where(r => r.Enabled).Select(r =>
            {
                return(MetadataReference.CreateFromFile(request.ReferenceDirectory + Path.DirectorySeparatorChar + r.File));
            }).ToList(),
                options
                );

            EmitResult emitResult;

            byte[] ILbytes = null;
            using (var ms = new MemoryStream())
            {
                emitResult = compilation.Emit(ms);
                if (emitResult.Success)
                {
                    ms.Flush();
                    ms.Seek(0, SeekOrigin.Begin);
                    ILbytes = ms.ToArray();
                }
                else
                {
                    var sb = new StringBuilder();
                    foreach (var d in emitResult.Diagnostics)
                    {
                        sb.AppendLine(d.ToString());
                    }
                    throw new CompilerException("CompilationErrors: " + Environment.NewLine + sb);
                }
            }
            return(ILbytes);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Add a new Result
        /// </summary>
        /// <param name="compilationRequestID">The ID of compilation request</param>
        /// <param name="xmlResult">The xml</param>
        /// <returns>Result Object</returns>
        public Object addResult(int compilationRequestID, XElement xmlResult)
        {
            CompilationRequest cr = context.CompilationRequest.FirstOrDefault(f => f.compilReqID == compilationRequestID);
            Publication        p  = cr.Publication;

            cr.compiled = true;
            Result r = new Result();

            if (p.anonymResult)
            {
                r.publicationID = p.publicationID;
                r.xmlResult     = xmlResult;
                return(AddEntity(r));
            }
            else
            {
                r.compilReqID = compilationRequestID;
                r.xmlResult   = xmlResult;
                return(AddEntity(r));
            }
        }
Exemplo n.º 12
0
        static async Task Main(string[] args)
        {
            Microsoft.Build.Locator.MSBuildLocator.RegisterDefaults();

            ApplicationLogging.SetLoggerFactory(LoggerFactory.Create(l => l.SetMinimumLevel(LogLevel.Information)
                                                                     .AddZLoggerConsole(options => options.PrefixFormatter = (buf, info) => ZString.Utf8Format(buf, "[{0}] [{1:D2}:{2:D2}:{3:D2}] ", GetLogLevelString(info.LogLevel), info.Timestamp.LocalDateTime.Hour, info.Timestamp.LocalDateTime.Minute, info.Timestamp.LocalDateTime.Second))));

            var settings = new H5DotJson_AssemblySettings();
            var request  = new CompilationRequest("App", settings)
                           .NoPackageResources()
                           .NoHTML()
                           .WithPackageReference("h5", NuGetVersion.Parse("0.0.8537"))
                           .WithPackageReference("h5.Core", NuGetVersion.Parse("0.0.8533"))
                           .WithSourceFile("App.cs",
                                           @"
using System;
using H5;

namespace Test
{
    internal static class App
    {
        private static int HelloWorld;
        private static void Main()
        {
            Console.WriteLine(nameof(HelloWorld));
        }
    }
}
");
            var compiledJavascript = await CompilationProcessor.CompileAsync(request);


            foreach (var(file, code) in compiledJavascript.Output)
            {
                Logger.ZLogInformation("File: {0}\n\n----------------------------\n\n{1}\n\n----------------------------\n\n", file, code);
            }

            await Task.Delay(1000); //Awaits to print all log messages
        }
Exemplo n.º 13
0
        public void getResultsByPublicationIDTest()
        {
            XElement           xe = XElement.Parse("<catalog><book id=\"bk101\"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre>text1</book><!--Comment1--><book id=\"bk102\"><!--Comment2--><author>Ralls, Kim</author><title>Midnight Rain</title><genre>Fantasy</genre></book></catalog>");
            List <Result>      listResultToRemove = new List <Result>();
            CompilationRequest s = storageManager.getEntityByID <CompilationRequest>(11);

            if (s != null)
            {
                for (int i = 0; i < 10; i++)
                {
                    Result r = (Result)storageManager.addResult(11, xe);
                    listResultToRemove.Add(r);
                }
            }

            List <Result> lr = storageManager.getResultsByPublicationID(s.publicationID.Value);

            if (listResultToRemove.Count > 0)
            {
                storageManager.removeEntity <Result>(listResultToRemove);
            }
        }
Exemplo n.º 14
0
        public static byte[] Compile(CompilationRequest request)
        {
            // Gather SyntaxTrees for compilation
            List <SourceSyntaxTree> sourceSyntaxTrees = new List <SourceSyntaxTree>();
            List <SyntaxTree>       compilationTrees  = new List <SyntaxTree>();

            if (request.SourceDirectories != null)
            {
                foreach (var sourceDirectory in request.SourceDirectories)
                {
                    sourceSyntaxTrees.AddRange(Directory.GetFiles(sourceDirectory, "*.cs", SearchOption.AllDirectories)
                                               .Select(F => new SourceSyntaxTree {
                        FileName = F, SyntaxTree = CSharpSyntaxTree.ParseText(File.ReadAllText(F), new CSharpParseOptions())
                    })
                                               .ToList());
                    compilationTrees.AddRange(sourceSyntaxTrees.Select(S => S.SyntaxTree).ToList());
                }
            }
            SyntaxTree sourceTree = CSharpSyntaxTree.ParseText(request.Source, new CSharpParseOptions());

            compilationTrees.Add(sourceTree);

            List <PortableExecutableReference> references = request.References.Where(R => R.Framework == request.TargetDotNetVersion).Where(R => R.Enabled).Select(R =>
            {
                switch (R.Framework)
                {
                case Common.DotNetVersion.Net35:
                    return(MetadataReference.CreateFromFile(R.File));

                case Common.DotNetVersion.Net40:
                    return(MetadataReference.CreateFromFile(R.File));

                case Common.DotNetVersion.NetCore21:
                    return(MetadataReference.CreateFromFile(R.File));

                default:
                    return(null);
                }
            }).ToList();

            // Use specified OutputKind and Platform
            CSharpCompilationOptions options = new CSharpCompilationOptions(outputKind: request.OutputKind, optimizationLevel: OptimizationLevel.Release, platform: request.Platform, allowUnsafe: request.UnsafeCompile);
            // Compile to obtain SemanticModel
            CSharpCompilation compilation = CSharpCompilation.Create(
                request.AssemblyName == null ? Path.GetRandomFileName() : request.AssemblyName,
                compilationTrees,
                references,
                options
                );

            // Perform source code optimization, removing unused types
            if (request.Optimize)
            {
                // Find all Types used by the generated compilation
                List <ITypeSymbol> usedTypes = new List <ITypeSymbol>();
                GetUsedTypesRecursively(compilation, sourceTree, ref usedTypes, ref sourceSyntaxTrees);
                usedTypes = usedTypes.Distinct().ToList();
                List <string> usedTypeNames = usedTypes.Select(T => GetFullyQualifiedTypeName(T)).ToList();

                // Filter SyntaxTrees to trees that define a used Type, otherwise the tree is not needed in this compilation
                compilationTrees = sourceSyntaxTrees.Where(SST => SyntaxTreeDefinesUsedType(compilation, SST.SyntaxTree, usedTypeNames))
                                   .Select(SST => SST.SyntaxTree)
                                   .ToList();

                // Removed unused Using statements from the additional entrypoint source
                List <string> usedNamespaceNames = GetUsedTypes(compilation, sourceTree)
                                                   .Select(T => GetFullyQualifiedContainingNamespaceName(T)).Distinct().ToList();
                List <SyntaxNode> unusedUsingDirectives = sourceTree.GetRoot().DescendantNodes().Where(N =>
                {
                    return(N.Kind() == SyntaxKind.UsingDirective && !((UsingDirectiveSyntax)N).Name.ToFullString().StartsWith("System.") && !usedNamespaceNames.Contains(((UsingDirectiveSyntax)N).Name.ToFullString()));
                }).ToList();
                sourceTree = sourceTree.GetRoot().RemoveNodes(unusedUsingDirectives, SyntaxRemoveOptions.KeepNoTrivia).SyntaxTree;
                // Console.WriteLine("source: " + sourceTree.ToString());

                // Compile again, with unused SyntaxTrees and unused using statements removed
                compilationTrees.Add(sourceTree);
                compilation = CSharpCompilation.Create(
                    request.AssemblyName == null ? Path.GetRandomFileName() : request.AssemblyName,
                    compilationTrees,
                    request.References.Where(R => R.Framework == request.TargetDotNetVersion).Where(R => R.Enabled).Select(R =>
                {
                    switch (request.TargetDotNetVersion)
                    {
                    case Common.DotNetVersion.Net35:
                        return(MetadataReference.CreateFromFile(R.File));

                    case Common.DotNetVersion.Net40:
                        return(MetadataReference.CreateFromFile(R.File));

                    case Common.DotNetVersion.NetCore21:
                        return(MetadataReference.CreateFromFile(R.File));

                    default:
                        return(null);
                    }
                }).ToList(),
                    options
                    );
            }

            // Emit compilation
            EmitResult emitResult;

            byte[] ILbytes = null;
            using (var ms = new MemoryStream())
            {
                emitResult = compilation.Emit(
                    ms,
                    manifestResources: request.EmbeddedResources.Where(ER =>
                {
                    return(request.Platform == Platform.AnyCpu || ER.Platform == Platform.AnyCpu || ER.Platform == request.Platform);
                }).Where(ER => ER.Enabled).Select(ER =>
                {
                    return(new ResourceDescription(ER.Name, () => File.OpenRead(ER.File), true));
                }).ToList()
                    );
                if (emitResult.Success)
                {
                    ms.Flush();
                    ms.Seek(0, SeekOrigin.Begin);
                    ILbytes = ms.ToArray();
                }
                else
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (Diagnostic d in emitResult.Diagnostics)
                    {
                        sb.AppendLine(d.ToString());
                    }
                    throw new CompilerException("CompilationErrors: " + Environment.NewLine + sb);
                }
            }
            if (request.Confuse)
            {
                return(Confuse(ILbytes));
            }
            return(ILbytes);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Make the workflow pointed by this WorkflowReference computable
        /// </summary>
        /// <param name="ftype">The <see cref="Security.FormType"/>of the computable workflow</param>
        /// <param name="serviceId"> The serviceId allowed to fill this workflow</param>
        /// <param name="publicationDescription"> The workflowdescription of the publication, empty string for empty description</param>
        /// <param name="expireDate"> The expire date gg/mm/aaaa</param>
        /// <param name="communicationUri"> The uri used by communication package</param>
        /// <returns>A <see cref="Security.ComputableWorkflowReference"/> pointing the newly created <see cref="Core.WF.IComputableWorkflow"/>; <value>null</value> if an error happens</returns>
        public ComputableWorkflowReference MakeComputable(FormType ftype, int serviceId, string publicationDescription, string expireDate, string communicationUri)
        {
            if (publicationDescription == null)
            {
                return(null);
            }
            if (!complete)
            {
                fillReference();
            }

            bool publicp          = ((ftype == FormType.PUBLIC_BY_SERVICE) || (ftype == FormType.PUBLIC_WITH_REPLICATION) || (ftype == FormType.PUBLIC_WITHOUT_REPLICATION));
            bool anonym           = ((ftype == FormType.PRIVATE_ANONYM) || (ftype == FormType.PUBLIC_BY_SERVICE) || (ftype == FormType.PUBLIC_WITH_REPLICATION) || (ftype == FormType.PUBLIC_WITHOUT_REPLICATION));
            bool resultReplicated = (ftype == FormType.PUBLIC_WITH_REPLICATION);

            if (ftype == FormType.PUBLIC_BY_SERVICE && (serviceId == 1))
            {
                // Not allowed, we need a valid serviceId
                return(null);
            }


            if (wf_cache == null)
            {
                if (!_RetrieveWf())
                {
                    return(null);
                }
            }

            IFormatProvider provider = CultureInfo.InvariantCulture;
            DateTime        date;

            if (!DateTime.TryParse(expireDate, provider, DateTimeStyles.AdjustToUniversal, out date))
            {
                provider = CultureInfo.GetCultureInfo("it-IT");
                if (!DateTime.TryParse(expireDate, provider, DateTimeStyles.AdjustToUniversal, out date))
                {
                    date = DateTime.Now.AddMonths(1);
                }
            }

            Publication pub = sto.publish(modelId, DateTime.Now, date, "", wf_cache.Save(), anonym, publicp, resultReplicated, serviceId, communicationUri, "Inutile", publicationDescription);

            if (pub == null)
            {
                return(null);
            }

            // Make it public
            int creqid = -1;

            if (publicp)
            {
                CompilationRequest creq = null;
                if (resultReplicated)
                {
                    creq = sto.addPublicPublication(pub.publicationID);
                    if (creq == null)
                    {
                        return(null);
                    }
                    creqid = creq.compilReqID;
                }

                Storage.User user = sto.getEntityByID <Storage.User>(userId);
                //Genero email per creatore
                try
                {
                    System.Net.Mail.MailAddress userMail = new System.Net.Mail.MailAddress(user.mail);
                    MailForPublicForm           mail     = new MailForPublicForm(userMail, pub, creq);
                    LoaMailSender sender = new LoaMailSender();
                    sender.SendMail(mail);
                }
                catch
                {
                    //non dovrebbe mai arrivare qui visto che la mail dell'utente e' controllata in fase di registrazione
                    return(null);
                }
            }
            if (!String.IsNullOrEmpty(communicationUri))
            {
                CommunicationService.Instance.Setup(pub);
            }

            return(new ComputableWorkflowReference(userId, pub.publicationID, pub.namePublication, pub.description, themeId, creqid, !publicp, false, date));
        }
Exemplo n.º 16
0
        public async Task <IActionResult> ChangeTaskWithFile(IFormFile file, string framework)
        {
            if (file != null)
            {
                var isAlive = await CheckIfAlive(_config.CompilerServicesOrchestrator);

                if (isAlive)
                {
                    var req = new CompilationRequest
                    {
                        File      = await file.GetBytes(),
                        Framework = framework
                    };

                    string url = _config.CompilerServicesOrchestrator.GetFullTaskLinkFrom(_config.FrontEnd);
                    using var httpClient = new HttpClient();
                    using var form       = JsonContent.Create(req);
                    HttpResponseMessage response = await httpClient.PostAsync(url, form);

                    string apiResponse = await response.Content.ReadAsStringAsync();

                    if (response.IsSuccessStatusCode)
                    {
                        CompilationResponse compResponse = JsonConvert.DeserializeObject <CompilationResponse>(apiResponse);

                        if (compResponse.OK)
                        {
                            string dirPath = "c:/TemporaryTaskDownloads";

                            dirPath += "/" + Guid.NewGuid().ToString();
                            while (Directory.Exists(dirPath))
                            {
                                dirPath += "/" + Guid.NewGuid().ToString();
                            }

                            var dir = Directory.CreateDirectory(dirPath);

                            string fullPath = dirPath + "/" + file.Name;

                            System.IO.File.WriteAllBytes(fullPath, compResponse.File);
                            ZipFile.ExtractToDirectory(fullPath, dirPath, true);

                            var pathToDll = FindAssemblyFile(dir, ".dll");

                            if (pathToDll == null)
                            {
                                pathToDll = FindAssemblyFile(dir, ".exe");
                            }

                            List <ClassDefinition> result = null;
                            if (pathToDll != null)
                            {
                                result = ConvertProject(pathToDll);
                            }

                            dir.Delete(true);

                            if (result != null)
                            {
                                var response1 = new
                                {
                                    status = "success",
                                    data   = result
                                };

                                return(Json(response1));
                            }
                            else
                            {
                                var response2 = new
                                {
                                    status = "error",
                                    data   = "No dll or exe file found!"
                                };

                                return(Json(response2));
                            }
                        }

                        var response3 = new
                        {
                            status = "error",
                            data   = "Compilation container returned error with message: " + compResponse.Message
                        };

                        return(Json(response3));
                    }

                    var response4 = new
                    {
                        status = "error",
                        data   = "Compilation container returned " + response.StatusCode + " !"
                    };

                    return(Json(response4));
                }

                var response5 = new
                {
                    status = "error",
                    data   = "Compilation serviec is dead!"
                };

                return(Json(response5));
            }
            var response6 = new
            {
                status = "error",
                data   = "No file uploaded!"
            };

            return(Json(response6));
        }