Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid handle bug happening when TypeBuilder type used in exception catch clause #106665

Merged
merged 3 commits into from
Aug 20, 2024

Conversation

buyaa-n
Copy link
Contributor

@buyaa-n buyaa-n commented Aug 19, 2024

PersistedAssemblyBuilder's ILGenerator cannot handle catch block with newly created TypeBuilder exception type, because the TypeBuilder token is not valid when ILGenerator.BeginCatchBlock(Type? exceptionType) called and the handle will not be populated properly until the assembly saved into a file or stream.

The current solution for handling unpopulated tokens: BlobBuilder.ReserveBytes(int) will not work for this case as ControlFlowBuilder.AddCatchRegion(...) validates and reserves the exception type handle, not writes the token into BLOB directly

So instead of adding exception handler blocks directly into the ControlFlowBuilder instance collecting them into a list and adding them to the ControlFlowBuilder during Save where all tokens will be populated.

Alternative solution: tried to fill valid TypeBuilder handles using reflection, but it's getting very messy, need to access and manipulate private/internal members of ControlFlowBuilder.

Fixes #106485

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you!

@buyaa-n
Copy link
Contributor Author

buyaa-n commented Aug 20, 2024

/backport to release/9.0

Copy link
Contributor

Started backporting to release/9.0: https://github.com/dotnet/runtime/actions/runs/10475035721

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PersistedAssemblyBuilder ILGenerator cannot handle catch block with newly created exception type
2 participants