Skip to content

Reduce unsafe scope in trimmable type map code - #11468

Closed
simonrozsival with Copilot wants to merge 5 commits into
mainfrom
copilot/revise-unsafe-code-gen
Closed

simonrozsival with Copilot wants to merge 5 commits into
mainfrom
copilot/revise-unsafe-code-gen

Conversation

Copilot AI commented May 23, 2026 •

Copy link
Copy Markdown
Contributor

The trimmable type map path used broad unsafe patterns 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

    • Preserves the existing stackalloc and pointer-based UTF-8 encoding path.
    • Keeps GetNamespaceAssemblyUtf8Bytes(...) unsafe and documents its caller requirements with /// <safety>.
    • Wraps calls to the unsafe helper in explicit unsafe blocks with SAFETY notes.
  • Runtime native registration

    • Changed TrimmableTypeMap.RegisterNativeMethods() to remain safe-callable.
    • Scoped native pointer work to an inner unsafe block with a SAFETY note.
  • Generated-code model

    • Updated the typemap emitter’s pseudo-C# to show the intended unsafe boundary pattern around JNI native registration.

Example pattern:

unsafe {
	// SAFETY: UTF-8 data is pinned for the duration of RegisterNatives.
	JniEnvironment.Types.RegisterNatives (runtimeClass.PeerReference, [method]);
}

Copilot AI and others added 2 commits May 23, 2026 15:22
Copilot AI changed the title [WIP] Revisit use of unsafe in generated code Reduce unsafe scope in trimmable type map code May 23, 2026
Copilot AI requested a review from simonrozsival May 23, 2026 15:27

@simonrozsival simonrozsival left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I want to keep this method the way it was. Keep it unsafe and just add the /// <safety> comment here + call it from unsafe { ... }

Copilot AI and others added 2 commits May 23, 2026 19:48
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>

Copilot AI commented May 23, 2026

Copy link
Copy Markdown
Contributor Author

@copilot address this review

Addressed the review in 9f6abc4 and follow-up cleanup 6252660. The stackalloc path is restored, GetNamespaceAssemblyUtf8Bytes remains unsafe with /// <safety>, and call sites now use explicit unsafe blocks.

Copilot AI requested a review from simonrozsival May 23, 2026 19:52
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Revisit use of unsafe in generated code and in libraries

2 participants