Skip to content

Linker corrupts PDBs Metadata header id which makes it not to match Assembly Id any longer #1285

@joperezr

Description

@joperezr

cc: @sbomer @eerhardt

Say you have a very simple console app with the following code:

using System;
using System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        StackTrace thisTrace = new StackTrace(fNeedFileInfo: true);

        if (thisTrace.GetFrame(thisTrace.FrameCount - 1).GetFileLineNumber() == 0)
        {
            throw new Exception("Unable to retrieve line info");
        }
    }
}

This will work just fine if you try to run it in a self contained app, but if you pass in /p:PublishTrimmed=true then it will start throwing an exception. The reason is that after the linker trims the assembly and the PDB, StackTrace constructor will fail to find PDBs for the console app, particularly it will cause Metadata reader to fail this check: https://github.com/dotnet/runtime/blob/master/src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEReader.cs#L828-L832

                // Validate that the PDB matches the assembly version
                if (new BlobContentId(candidate.GetMetadataReader().DebugMetadataHeader!.Id) != id)
                {
                    return false;
                }

as that if check will find that the PDB doesn't match the assembly version any longer and the method body will return false, meaning a metadata provider won't be succesfully returned.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions