Reduce unsafe scope in trimmable type map code#11468
Draft
Copilot wants to merge 5 commits into
Draft
Conversation
Agent-Logs-Url: https://github.com/dotnet/android/sessions/f976dfb0-61ce-4e48-b20e-ebcb73ddb9cf Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/android/sessions/f976dfb0-61ce-4e48-b20e-ebcb73ddb9cf Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Revisit use of unsafe in generated code
Reduce unsafe scope in trimmable type map code
May 23, 2026
Member
simonrozsival
left a comment
There was a problem hiding this comment.
@copilot address this review
| ulong hashValue = BinaryPrimitives.ReadUInt64LittleEndian (hash); | ||
| BinaryPrimitives.WriteUInt64LittleEndian (hash, hashValue ^ (ulong) bytesWritten); | ||
| return ToHexString (hash); | ||
| byte[] rented = ArrayPool<byte>.Shared.Rent (byteCount); |
Member
There was a problem hiding this comment.
I want to keep stackalloc, I don't want to compromise here. Simply use the unsafe block here instead.
| } | ||
|
|
||
| static unsafe int GetNamespaceAssemblyUtf8Bytes (string ns, string assemblyName, Span<byte> destination) | ||
| static int GetNamespaceAssemblyUtf8Bytes (string ns, string assemblyName, byte[] destination) |
Member
There was a problem hiding this comment.
I want to keep this method the way it was. Keep it unsafe and just add the /// <safety> comment here + call it from unsafe { ... }
Agent-Logs-Url: https://github.com/dotnet/android/sessions/b540e64f-426f-4e62-89e3-b09e5e497c93 Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/android/sessions/b540e64f-426f-4e62-89e3-b09e5e497c93 Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The trimmable type map path used broad
unsafepatterns that do not align well with the upcoming C# memory-safety model. This revisits the generated/runtime registration path to make unsafe boundaries explicit while preserving existing performance-sensitive unsafe code where needed.Hashing helper
stackallocand pointer-based UTF-8 encoding path.GetNamespaceAssemblyUtf8Bytes(...)unsafe and documents its caller requirements with/// <safety>.unsafeblocks withSAFETYnotes.Runtime native registration
TrimmableTypeMap.RegisterNativeMethods()to remain safe-callable.unsafeblock with aSAFETYnote.Generated-code model
Example pattern: