Skip to content

Conversation

fabrimaz
Copy link

@fabrimaz fabrimaz commented May 3, 2025

Removing cumulative sum for GUID heap offsets
Fixes #113910

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label May 3, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection-metadata
See info in area-owners.md if you want to be subscribed.

@fabrimaz
Copy link
Author

fabrimaz commented May 3, 2025

Run unit-tests on this library:

=== TEST EXECUTION SUMMARY ===
System.Reflection.Metadata.Tests Total: 852, Errors: 0, Failed: 0, Skipped: 1, Time: 5,376s

stringSizes[r + 1] = stringSizes[r] + deltaReaders[r].GetHeapSize(HeapIndex.String);
blobSizes[r + 1] = blobSizes[r] + deltaReaders[r].GetHeapSize(HeapIndex.Blob);
guidSizes[r + 1] = guidSizes[r] + deltaReaders[r].GetHeapSize(HeapIndex.Guid) / guidSize;
guidSizes[r + 1] = deltaReaders[r].GetHeapSize(HeapIndex.Guid) / guidSize;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tmat
Copy link
Member

tmat commented May 6, 2025

It'd be good to have some tests that demonstrate correctness. There are some existing tests for MetadataAggregator, so you can update them. Make sure the test fails without your change and passes with the change.

@fabrimaz
Copy link
Author

fabrimaz commented May 10, 2025

It'd be good to have some tests that demonstrate correctness. There are some existing tests for MetadataAggregator, so you can update them. Make sure the test fails without your change and passes with the change.

Hello! The linked comment was updated :)

The method "CalculateHeapSizes(..)" I edited is private and its result is assigned to a private field directly in the constructor of the class.
Even if I try to unit-test that constructor, I wouldn't be able to easily access the private field from unit-test class to assert its values.

What would you suggest? Should I split the mentioned method in a separate class to enhance testability?

Or else making the method 'internal'?

@fabrimaz fabrimaz requested a review from tmat May 10, 2025 07:56
@fabrimaz
Copy link
Author

Ping @tmat

@fabrimaz
Copy link
Author

fabrimaz commented Jul 6, 2025

ping @tmat

@tmat
Copy link
Member

tmat commented Jul 7, 2025

@ericstj
Copy link
Member

ericstj commented Jul 7, 2025

@fabrimaz as @tmat mentions the action here is to add a test to https://github.com/dotnet/runtime/blob/main/src/libraries/System.Reflection.Metadata/tests/Metadata/Ecma335/MetadataAggregatorTests.cs. It looks like there are already tests that call TestGenerationHandle which should be able to observe the broken behavior called out in #113910.

@fabrimaz
Copy link
Author

fabrimaz commented Aug 19, 2025

@ericstj I might be completely wrong but feels like all the existing unit-tests are building _heapSize in a simplified way through a fake internal ctor, hence not replicating our 'issue'.

To unit-test our behaviour, I need to use at least a baseReader and a list of MetadataReader deltas.
Going in that direction and keeping you posted
@tmat

@fabrimaz
Copy link
Author

By chance, do you have any clue on how to create a minimal MetadataReader with an allocated GUID? Couldn't find a good option among existing unit-tests for MetadataReader

@fabrimaz
Copy link
Author

@dotnet-policy-service agree

@fabrimaz
Copy link
Author

@ericstj @tmat
glad to tell that I managed to build meaningful unit-tests for the specific case.
Needed to create MetadataReader manually to fully test the behaviour.

Please, give it a review and let me know your opinion about :)

// to avoid minimal flag exception.
reader.TableRowCounts[(int)TableIndex.EncMap] = 1;
reader.IsMinimalDelta = true;
return reader;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're returning reader that accesses memory at pointer p but the target array can move once fixed block is exited. You need to keep the array pinned while using the reader.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored according to your suggestions


// GUID-heap allocation shouldn't be cumulative, since GUIDs are copied among generations.
// The delta-reader above need indeed a single GUID allocation in each gen.
AssertExtensions.Throws<ArgumentException>("handle", () => TestGenerationHandle(aggregator, MetadataTokens.GuidHandle(2), expectedHandle: MetadataTokens.GuidHandle(2), expectedGeneration: 0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to read 3 guids since there is one in baseline, and one in each delta, no?

Copy link
Author

@fabrimaz fabrimaz Sep 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tmat in a real case yes, not in this test though.
Our new implementation removes cumulative sum and relies on each delta-reader GUID heap size, relying on deltareaders the copying of previous GUIDs and adding of existing new ones.

With new implementation, each generation here allocates a single GUID, hence only that one at index 1 could be read.
Tried to allocate more GUIDs on manually created delta-readers but couldn't find a working example.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With cumulative it was
Gen0: #1
Gen1: #1 #2
Gen2: #1 #2 #3

With new implementation
Gen0: #1
Gen1: #1
Gen2: #1

It's ok because all existing GUIDs are actually copied in next generation of metadata reader normally.

@fabrimaz fabrimaz requested a review from tmat September 8, 2025 15:56
@fabrimaz
Copy link
Author

Ping @tmat @ericstj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Reflection.Metadata community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MetadataAggregator.GetGenerationHandle returns incorrect results for Guid heap handles
4 participants