toptensoftware / nvpatch

14 stars 4 forks source link

Fix #1: Find, parse, and update the .NET bundle manifest #2

Closed SteffanDonal closed 1 year ago

SteffanDonal commented 1 year ago

A hopefully not-awful fix for the issue I was running into with my PublishSingleFile-enabled application. At the end of the patching process, I've added a branch that checks for when there is extra content after the PE file. If there is, that content is rewritten after the new end of the PE file, and we then check for a .NET bundle manifest. It contains offsets to all files stored after the PE file, which all must be updated - including the bundle marker stored in the .data freeform section.

Digging around the .NET runtime source code to find all this was quite interesting; as far as I can tell, the implementation here should work universally until the bundle format is changed again... Touch wood.

(Also, I spent too long trying to figure out the format of the bundle marker in the .data section to see if it could be parsed before realising that .NET itself doesn't even bother - it just searches for the magic bytes of the bundle's signature. It came together pretty quickly after that)

My reference: https://github.com/dotnet/designs/blob/main/accepted/2020/single-file/bundler.md https://github.com/dotnet/runtime/blob/main/src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.cs https://github.com/dotnet/runtime/blob/main/src/installer/managed/Microsoft.NET.HostModel/Bundle/Manifest.cs