Skip to content

Merge in changes from upstream - #505

Merged
agocke merged 9 commits into
dotnet:mainfrom
sbomer:mergeUpstream
Aug 11, 2026
Merged

agocke merged 9 commits into
dotnet:mainfrom
sbomer:mergeUpstream

Conversation

@sbomer

@sbomer sbomer commented Aug 11, 2026

Copy link
Copy Markdown
Member

To pick up the fix from jbevain#958 (see #504 for context).

mrvoorhe and others added 9 commits June 18, 2024 16:35
In the .NET world, executable assemblies have a Foo.exe and Foo.dll file.  Foo.exe is not a managed assembly and attempt to load it will cause
```
System.BadImageFormatException: Format of the executable (.exe) or library (.dll) is invalid.
   at Mono.Cecil.PE.ImageReader.ReadOptionalHeaders(UInt16& subsystem, UInt16& dll_characteristics) in C:\UnitySrc\dev\il2cpp-18\external\cecil\Mono.Cecil.PE\ImageReader.cs:line 198
   at Mono.Cecil.PE.ImageReader.ReadImage() in C:\UnitySrc\dev\il2cpp-18\external\cecil\Mono.Cecil.PE\ImageReader.cs:line 86
   at Mono.Cecil.PE.ImageReader.ReadImage(Disposable`1 stream, String file_name) in C:\UnitySrc\dev\il2cpp-18\external\cecil\Mono.Cecil.PE\ImageReader.cs:line 766
   at Mono.Cecil.ModuleDefinition.ReadModule(Disposable`1 stream, String fileName, ReaderParameters parameters) in C:\UnitySrc\dev\il2cpp-18\external\cecil\Mono.Cecil\ModuleDefinition.cs:line 1141
   at Mono.Cecil.ModuleDefinition.ReadModule(String fileName, ReaderParameters parameters) in C:\UnitySrc\dev\il2cpp-18\external\cecil\Mono.Cecil\ModuleDefinition.cs:line 1110
   at Mono.Cecil.AssemblyDefinition.ReadAssembly(String fileName, ReaderParameters parameters) in C:\UnitySrc\dev\il2cpp-18\external\cecil\Mono.Cecil\AssemblyDefinition.cs:line 132
 ```

By attempting to locate `.dll` files before `.exe` this problem can be avoided.

For context, I hit this in one of our resolvers while attempting to resolve all of the dependencies of an executable that referenced another executable.  I fixed our resolver the same way but noticed `BaseAssemblyResolver` had the same behavior we had so I thought I'd upstream the change.
…flection.Metadata (jbevain#958)

* Fix WriteCompressedInt32 logic by using the same code as in System.Reflection.Metadata

Note: the implementation in SRM uses methods that are not available in netfx 4.0. To make the code work with all platforms supported by Mono.Cecil, I extracted the required methods.

* Cleanup whitespaces

---------

Co-authored-by: Jb Evain <jb@evain.net>
* Fixed: SymWriter COM object is not released on exception

The Problem:

Problem observed when being used in coverlet:
coverlet-coverage/coverlet#1471
If an exception caused `NativePdbWriter.Write` to never be
called, it would not call `SymWriter.Close`,
which in turn meant `Marshal.ReleaseComObject` was left uncalled.

The garbage collector will eventually destroy the object
and thereby release the COM object, but that happens non-deterministically.
The COM object is holding to a file handle, which prevents
other operations on the written file until it is released.
The result was random file access issues.

The Solution:

Luckily NativePdbWriter is IDisposable. I added a call to
`writer.Close` there. But now it could be called twice,
so I had to add a boolean to SymWriter to avoid releasing
everything twice.

* Code style

---------

Co-authored-by: Jb Evain <jb@evain.net>
* Preserve custom debug info on types

* Use ICustomDebugInformationProvider in ISymbolReader/ISymbolWriter

* Move get logic to Mixin helpers

* Add test dll/pdb

* PR feedback

- Use static lambdas
- Check HasCustomDebugInformation
- Test writing modified CustomDebugInformation
- Formatting fixes

* Update Test/Mono.Cecil.Tests/PortablePdbTests.cs

Co-authored-by: Jackson Schuster <36744439+jtschuster@users.noreply.github.com>

* Build for netstandard2.0

---------

Co-authored-by: Jackson Schuster <36744439+jtschuster@users.noreply.github.com>
* Protect against circular type forwarders

If we have two assemblies with type forwarders that point to each other, we enter an infinite loop and a stack overflow.

This breaks the cycle by detecting reentrancy.

Fixes jbevain#706

* Add a test for TypeForwardedTo circularity

* Move field up
* Preserve Documents

* Target net6.0

To remove reference to RefSafetyRulesAttribute

* Target .NET Framework

To avoid references to System.Runtime
@agocke
agocke merged commit 251438f into dotnet:main Aug 11, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants