Ejemplo n.º 1
0
        private void AssembleStage2()
        {
            try
            {
                string intermediateOutputFileName = GetIntermediateBinaryPath("stage2.exe");
                int    exitCode = Helper.FasmCompile
                                  (
                    GetIntermediateSourcePath("Stage2.asm"),
                    intermediateOutputFileName,
                    out string fasmOutput
                                  );

                if (exitCode == 0)
                {
                    ExecutableHelper.ExtractShellcode(intermediateOutputFileName, GetIntermediateBinaryPath("stage2.shellcode"));
                }
                else
                {
                    Errors.Add(ErrorSource.Assembly, ErrorSeverity.Error, "FASM.exe returned status code " + exitCode + ".", fasmOutput.Trim());
                }
            }
            catch (ErrorException ex)
            {
                Errors.Add(ErrorSource.Assembly, ErrorSeverity.Error, ex.Message, ex.Details);
            }
            catch (Exception ex)
            {
                Errors.Add(ErrorSource.Assembly, ErrorSeverity.Error, "Unhandled " + ex.GetType() + " while assembling stage2.", ex.GetFullStackTrace());
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Validates that the project only uses executables of a specific bitness for RunPE actions.
        /// </summary>
        /// <param name="bitness">The bitness to check. This value must be 32 or 64.</param>
        public void ValidateRunPEBitness(int bitness)
        {
            if (bitness != 32 && bitness != 64)
            {
                throw new ArgumentException("Argument must be '32' or '64'.", nameof(bitness));
            }

            foreach (RunPEAction action in Project.Actions.OfType <RunPEAction>())
            {
                if (action.Source is EmbeddedSource embeddedSource)
                {
                    int bits = ExecutableHelper.GetExecutableBitness(embeddedSource.Path, false);

                    if (bits == 0)
                    {
                        Errors.Add(ErrorSource.Compiler, ErrorSeverity.Error, "File '" + Path.GetFileName(embeddedSource.Path) + "' is not a valid executable.");
                    }
                    else if (bits == 64 && bitness == 32)
                    {
                        Errors.Add(ErrorSource.Compiler, ErrorSeverity.Error, "Cannot use 64-bit executable '" + Path.GetFileName(embeddedSource.Path) + "' with RunPE in a 32-bit stub.");
                    }
                    else if (bits == 32 && bitness == 64)
                    {
                        Errors.Add(ErrorSource.Compiler, ErrorSeverity.Error, "Cannot use 32-bit executable '" + Path.GetFileName(embeddedSource.Path) + "' with RunPE in a 64-bit stub.");
                    }

                    if (ExecutableHelper.GetExecutableBitness(embeddedSource.Path, true) != 0)
                    {
                        Errors.Add(ErrorSource.Compiler, ErrorSeverity.Error, "Cannot use .NET executable '" + Path.GetFileName(embeddedSource.Path) + "' with RunPE. Use a .NET stub with an invoke action instead.");
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public Wrapper(Executable executable)
        {
            if (!string.IsNullOrWhiteSpace(ExecutableFilePath))
            {
                return;
            }

            if (!string.IsNullOrWhiteSpace(ExecutablesPath))
            {
                ExecutableFilePath = ExecutableHelper.SelectExecutableFromPath(executable, ExecutablesPath);

                if (!string.IsNullOrEmpty(ExecutableFilePath))
                {
                    return;
                }
            }

            Assembly entryAssembly = Assembly.GetEntryAssembly();

            if (entryAssembly != null)
            {
                string workingDirectory = Path.GetDirectoryName(entryAssembly.Location);

                ExecutableFilePath = ExecutableHelper.SelectExecutableFromPath(executable, workingDirectory);

                if (ExecutableFilePath != null)
                {
                    return;
                }
            }

            string[] paths = Environment.GetEnvironmentVariable("PATH")
                             .Split(Path.PathSeparator);

            foreach (string path in paths)
            {
                ExecutableFilePath = ExecutableHelper.SelectExecutableFromPath(executable, path);

                if (ExecutableFilePath != null)
                {
                    return;
                }
            }

            throw new ExecutableNotFoundException($"Could not find {executable.Name}. Please add it to your PATH variable or path to DIRECTORY with FFmpeg executables in {nameof(Wrapper)}.{nameof(ExecutablesPath)}");
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Appends EOF data to the compiled stub, if an <see cref="EmbeddedSource" /> has the <see cref="EmbeddedSource.EofData" /> flag.,
 /// </summary>
 /// <param name="outputFileName">The path to the compiled stub to append the EOF data to.</param>
 public void AppendEofData(string outputFileName)
 {
     if (Project.Sources.OfType <EmbeddedSource>().FirstOrDefault(source => source.EofData) is EmbeddedSource eofSource)
     {
         if (ExecutableHelper.ExtractEofData(eofSource.Path) is byte[] eofData)
         {
             using (FileStream file = File.OpenWrite(outputFileName))
             {
                 file.Seek(0, SeekOrigin.End);
                 file.Write(eofData);
             }
         }
         else
         {
             Errors.Add(ErrorSource.Assembly, ErrorSeverity.Warning, "File '" + Path.GetFileName(eofSource.Path) + "' does not contain EOF data.");
         }
     }
 }
    private static async Task VerifyPullRequestVersionIsCalculatedProperly(string pullRequestRef, Dictionary <string, string> env)
    {
        using var fixture = new EmptyRepositoryFixture();
        var remoteRepositoryPath = ExecutableHelper.GetTempPath();

        RepositoryFixtureBase.Init(remoteRepositoryPath);
        using (var remoteRepository = new Repository(remoteRepositoryPath))
        {
            remoteRepository.Config.Set("user.name", "Test");
            remoteRepository.Config.Set("user.email", "*****@*****.**");
            fixture.Repository.Network.Remotes.Add("origin", remoteRepositoryPath);
            Console.WriteLine("Created git repository at {0}", remoteRepositoryPath);
            remoteRepository.MakeATaggedCommit("1.0.3");

            var branch = remoteRepository.CreateBranch("FeatureBranch");
            Commands.Checkout(remoteRepository, branch);
            remoteRepository.MakeCommits(2);
            Commands.Checkout(remoteRepository, remoteRepository.Head.Tip.Sha);
            //Emulate merge commit
            var mergeCommitSha = remoteRepository.MakeACommit().Sha;
            Commands.Checkout(remoteRepository, TestBase.MainBranch); // HEAD cannot be pointing at the merge commit
            remoteRepository.Refs.Add(pullRequestRef, new ObjectId(mergeCommitSha));

            // Checkout PR commit
            Commands.Fetch((Repository)fixture.Repository, "origin", Array.Empty <string>(), new FetchOptions(), null);
            Commands.Checkout(fixture.Repository, mergeCommitSha);
        }

        var programFixture = new ProgramFixture(fixture.RepositoryPath);

        programFixture.WithEnv(env.ToArray());

        var result = await programFixture.Run();

        result.ExitCode.ShouldBe(0);
        result.OutputVariables.ShouldNotBeNull();
        result.OutputVariables.FullSemVer.ShouldBe("1.0.4-PullRequest0005.3");

        // Cleanup repository files
        DirectoryHelper.DeleteDirectory(remoteRepositoryPath);
    }
    public void WorkingDirectoryDoesNotExistFailsWithInformativeMessage()
    {
        var workingDirectory = PathHelper.Combine(ExecutableHelper.GetCurrentDirectory(), Guid.NewGuid().ToString("N"));
        var executable       = ExecutableHelper.GetDotNetExecutable();

        var output = new StringBuilder();
        var args   = ExecutableHelper.GetExecutableArgs($" /targetpath {workingDirectory} ");

        var exitCode = ProcessHelper.Run(
            s => output.AppendLine(s),
            s => output.AppendLine(s),
            null,
            executable,
            args,
            ExecutableHelper.GetCurrentDirectory());

        exitCode.ShouldNotBe(0);
        var outputString = output.ToString();

        outputString.ShouldContain($"The working directory '{workingDirectory}' does not exist.", Case.Insensitive, outputString);
    }
    private static async Task VerifyTagCheckoutVersionIsCalculatedProperly(Dictionary <string, string> env)
    {
        using var fixture = new EmptyRepositoryFixture();
        var remoteRepositoryPath = ExecutableHelper.GetTempPath();

        RepositoryFixtureBase.Init(remoteRepositoryPath);
        using (var remoteRepository = new Repository(remoteRepositoryPath))
        {
            remoteRepository.Config.Set("user.name", "Test");
            remoteRepository.Config.Set("user.email", "*****@*****.**");
            fixture.Repository.Network.Remotes.Add("origin", remoteRepositoryPath);
            Console.WriteLine("Created git repository at {0}", remoteRepositoryPath);
            remoteRepository.MakeATaggedCommit("0.1.0");
            Commands.Checkout(remoteRepository, remoteRepository.CreateBranch("develop"));
            remoteRepository.MakeACommit();
            Commands.Checkout(remoteRepository, remoteRepository.CreateBranch("release/0.2.0"));
            remoteRepository.MakeACommit();
            Commands.Checkout(remoteRepository, TestBase.MainBranch);
            remoteRepository.MergeNoFF("release/0.2.0", Generate.SignatureNow());
            remoteRepository.MakeATaggedCommit("0.2.0");

            Commands.Fetch((Repository)fixture.Repository, "origin", Array.Empty <string>(), new FetchOptions(), null);
            Commands.Checkout(fixture.Repository, "0.2.0");
        }

        var programFixture = new ProgramFixture(fixture.RepositoryPath);

        programFixture.WithEnv(env.ToArray());

        var result = await programFixture.Run();

        result.ExitCode.ShouldBe(0);
        result.OutputVariables.ShouldNotBeNull();
        result.OutputVariables.FullSemVer.ShouldBe("0.2.0");

        // Cleanup repository files
        DirectoryHelper.DeleteDirectory(remoteRepositoryPath);
    }
Ejemplo n.º 8
0
    private static ExecutionResults ExecuteIn(ArgumentBuilder arguments,
                                              params KeyValuePair <string, string?>[] environments
                                              )
    {
        var executable = ExecutableHelper.GetExecutable();
        var output     = new StringBuilder();

        var environmentalVariables = new Dictionary <string, string?>
        {
            { TeamCity.EnvironmentVariableName, null },
            { AppVeyor.EnvironmentVariableName, null },
            { TravisCi.EnvironmentVariableName, null },
            { Jenkins.EnvironmentVariableName, null },
            { AzurePipelines.EnvironmentVariableName, null },
            { GitHubActions.EnvironmentVariableName, null },
            { SpaceAutomation.EnvironmentVariableName, null }
        };

        foreach (var(key, value) in environments)
        {
            if (environmentalVariables.ContainsKey(key))
            {
                environmentalVariables[key] = value;
            }
            else
            {
                environmentalVariables.Add(key, value);
            }
        }

        var exitCode = -1;

        try
        {
            var args = ExecutableHelper.GetExecutableArgs(arguments.ToString());

            Console.WriteLine("Executing: {0} {1}", executable, args);
            Console.WriteLine();

            exitCode = ProcessHelper.Run(
                s => output.AppendLine(s),
                s => output.AppendLine(s),
                null,
                executable,
                args,
                arguments.WorkingDirectory,
                environmentalVariables.ToArray());
        }
        catch (Exception exception)
        {
            // NOTE: It's the exit code and output from the process we want to test,
            //       not the internals of the ProcessHelper. That's why we're catching
            //       any exceptions here, because problems in the process being executed
            //       should be visible in the output or exit code. @asbjornu
            Console.WriteLine(exception);
        }

        Console.WriteLine("Output from gitversion tool");
        Console.WriteLine("-------------------------------------------------------");
        Console.WriteLine(output.ToString());
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine("-------------------------------------------------------");

        if (arguments.LogFile.IsNullOrWhiteSpace() || !File.Exists(arguments.LogFile))
        {
            return(new ExecutionResults(exitCode, output.ToString(), null));
        }

        var logContents = File.ReadAllText(arguments.LogFile);

        Console.WriteLine("Log from gitversion tool");
        Console.WriteLine("-------------------------------------------------------");
        Console.WriteLine(logContents);
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine("-------------------------------------------------------");

        return(new ExecutionResults(exitCode, output.ToString(), logContents));
    }