Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EndOfStreamException thrown when using loaddependencies=true #2963

Closed
uglybugger opened this issue Dec 1, 2020 · 3 comments · Fixed by #2971
Closed

EndOfStreamException thrown when using loaddependencies=true #2963

uglybugger opened this issue Dec 1, 2020 · 3 comments · Fixed by #2971
Labels
Milestone

Comments

@uglybugger
Copy link

uglybugger commented Dec 1, 2020

What You Are Seeing?

Hi! I'm trying to install an addin using the loaddependencies=true parameter:

#addin "nuget:?package=Some.Package&version=0.0.0&loaddependencies=true"

I'm observing an error where Cake scans the dependency graph and attempts to detect if each assembly is a CLR assembly.

  Error: System.IO.EndOfStreamException: Unable to read beyond the end of the stream.
     at System.IO.BinaryReader.InternalRead(Int32 numBytes)
     at System.IO.BinaryReader.ReadUInt16()
     at Cake.Core.IO.FileExtensions.IsClrAssembly(IFile file) in C:\projects\cake\src\Cake.Core\IO\FileExtensions.cs:line 136
     at Cake.Core.Polyfill.AssemblyHelper.LoadAssembly(ICakeEnvironment environment, IFileSystem fileSystem, FilePath path) in C:\projects\cake\src\Cake.Core\Polyfill\AssemblyHelper.cs:line 49
     at Cake.Core.Reflection.AssemblyLoader.Load(FilePath path, Boolean verify) in C:\projects\cake\src\Cake.Core\Reflection\AssemblyLoader.cs:line 31
     at Cake.Core.Scripting.ScriptRunner.Run(IScriptHost host, FilePath scriptPath, IDictionary`2 arguments) in C:\projects\cake\src\Cake.Core\Scripting\ScriptRunner.cs:line 173
     at Cake.Commands.BuildCommand.Execute(CakeOptions options) in C:\projects\cake\src\Cake\Commands\BuildCommand.cs:line 41
     at Cake.CakeApplication.Run(CakeOptions options) in C:\projects\cake\src\Cake\CakeApplication.cs:line 45
     at Cake.Program.Main() in C:\projects\cake\src\Cake\Program.cs:line 81

I suspect that there's something in the offset reading code in FileExtensions.IsClrAssembly which, when presented with a non-CLR .dll is reading at one offset, finding what it thinks is another offset, and attempting to set the stream pointer to somewhere beyond the stream's end.

It looks like this line sets the stream position to whatever it's read at the previous location without checking whether that puts things beyond the end of the stream.

This also suggests that there might be other non-CLR DLLs being tested by Cake which happen to have a small number at the first offset read, which are sneaking through the test. I haven't dug too far into it so please take that with a grain or two of salt.

What is Expected?

Cake correctly walks the dependency graph and filters out non-CLR assemblies.

Workaround

This is preventing the use of the loaddependencies=true parameter. The best workaround I've found is to include all of the Cake addin's dependencies manually in build.cake as well. There is a long list of dependencies :)

What version of Cake are you using?

0.38.5

Are you running on a 32 or 64 bit system?

  • win-x64
  • linux-x64

What environment are you running on? Windows? Linux? Mac?

  • Works fine on Windows
  • Fails on Linux.
  • Not tested on Mac.

Are you running on a CI Server? If so, which one?

  • Fails under TeamCity on a agents running inside Linux containers.
  • Fails locally running inside a Linux container.
  • Works locally running on Windows on metal.

How Did You Get This To Happen? (Steps to Reproduce)

I just added an addin directive to my Cakefile with loaddependencies=true. It worked fine locally and then went 💥 on the build server.

Other Cakefiles with other addins seem to work fine so I suspect that it's just the dependencies of this (unfortunately currently internal) addin that cause Cake to throw.

Output logs

Unfortunately sanitising that much of an output log isn't feasible. I've fished out the stack trace (above); hopefully that gives a good pointer to what's going on.

@augustoproiete
Copy link
Member

@uglybugger Your analysis was correct and I was able to reproduce the error by creating a Cake add-in that includes a .dll file that is not a valid PE file.

using System.IO;

void Main()
{
    var buffer = new byte[365]; // Minimum size for Cake to consider analyzing it
    using (var stream = new MemoryStream(buffer))
    {
        using (var binaryWriter = new BinaryWriter(stream))
        {
            binaryWriter.Write(new byte[60]);
            binaryWriter.Write(5000); // Tell Cake to jump to a position beyond the length of the stream
        }
    }

    Directory.CreateDirectory(@"C:\Temp");
    File.WriteAllBytes(@"C:\Temp\InvalidPE.dll", buffer);
}

augustoproiete added a commit to augustoproiete-forks/cake-build--cake that referenced this issue Dec 5, 2020
augustoproiete added a commit to augustoproiete-forks/cake-build--cake that referenced this issue Dec 5, 2020
@devlead devlead added the Bug label Dec 5, 2020
@devlead devlead added this to the v1.0.0 milestone Dec 5, 2020
augustoproiete added a commit to augustoproiete-forks/cake-build--cake that referenced this issue Dec 5, 2020
augustoproiete added a commit to augustoproiete-forks/cake-build--cake that referenced this issue Dec 5, 2020
augustoproiete added a commit to augustoproiete-forks/cake-build--cake that referenced this issue Dec 5, 2020
augustoproiete added a commit to augustoproiete-forks/cake-build--cake that referenced this issue Dec 5, 2020
@devlead
Copy link
Member

devlead commented Dec 8, 2020

Fixed by #2971

@cake-build-bot
Copy link

🎉 This issue has been resolved in version v1.0.0 🎉

The release is available on:

Your GitReleaseManager bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants