Closed andrewcrawley closed 1 year ago
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
Tagging subscribers to this area: @dotnet/area-system-reflection-metadata See info in area-owners.md if you want to be subscribed.
Author: | andrewcrawley |
---|---|
Assignees: | - |
Labels: | `area-System.Reflection.Metadata`, `untriaged` |
Milestone: | - |
Thanks for reporting the issue @andrewcrawley, any interest in fixing the bug? Feel free to raise a PR with corresponding unit tests(s)
Description
I'm creating a
PEReader
to pull some metadata out of an assembly that's already loaded in memory. If I pass theIsLoadedImage
andPrefetchMetadata
flags together,PEReader
throws aBadImageFormatException
("Missing data directory.").Other combinations of flags (e.g.
IsLoadedImage
alone, orIsLoadedImage
+PrefetchEntireImage
) work fine.Reproduction Steps
Paste the following into a .net 6 Windows console app and run.
Expected behavior
Passing the
IsLoadedImage
andPrefetchMetadata
flags should result in a usablePEReader
.Actual behavior
Passing the
IsLoadedImage
andPrefetchMetadata
flags results in an exception being thrown:Regression?
Not a regression.
Known Workarounds
I can avoid passing the
IsLoadedImage
andPrefetchMetadata
flags together, but I'd prefer to be able to prefetch the metadata so I no longer need to keep the stream around.PrefetchEntireImage
works, but spends time / memory reading a bunch of extra data I don't need.Configuration
Provided repro uses .net 6 on Windows AMD64, but the issue isn't specific to that configuration.
Other information
It appears that the
IsLoadedImage
flag doesn't get propagated to thePEHeaders
object that is created in thePEReader
constructor for this combination of flags, resulting in invalid RVAs being calculated for various headers. See: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEReader.cs#L201In local testing, the issue no longer repros for me after patching
PEReader
to pass the flag along.