Repository navigation
Compile ref-assemblies with updated-memory-safety-rules - #131733
Conversation
Under the updated memory safety rules, 'unsafe' on a member is a caller contract carried in metadata as RequiresUnsafeAttribute. The compiler only honors that attribute when the assembly defining the member is itself marked with MemorySafetyRulesAttribute, which it synthesizes when the assembly is compiled with the updated rules. Consumers bind against the reference assemblies, and those are separate compilations whose members are all bodyless. So opting the reference assemblies in costs almost nothing, and it lets the contracts approved in API review take effect for anyone who opts in, well before the implementation assemblies are migrated. Mark the 293 members approved in #126956, #127098, #128075 and #129750 'unsafe', across System.Runtime, System.Runtime.InteropServices, System.Runtime.Intrinsics, System.Numerics.Vectors and System.Memory. Most of these cannot be inferred from a signature: Unsafe.As, SkipInit, BitCast and the ref-taking Add and CopyBlock overloads name no pointer at all, so nothing today asks their callers to audit them. Also settle the contracts the compiler requires once the rules are on: 'unsafe' comes off type declarations, where it no longer means anything, and the explicit-layout fields of BINDPTR, ELEMDESC and VARDESC become 'safe', matching the audit of their implementations. The pointer members of IntPtr and UIntPtr, and ArgumentNullException.ThrowIfNull(void*), lose 'unsafe' for the same reason: they convert or compare a pointer without dereferencing it. Verified against the shipped reference assembly. A caller that has not opted in is unaffected in both, so this is not a break. A caller that has opted in sees the newly declared contracts, and no longer needs an unsafe context for the members audited as safe. Note that GenAPI infers 'unsafe' in reference source from pointer types in the signature, so regenerating these files today would discard the contracts that do not name a pointer. That is tracked in #131451. Co-authored-by: Copilot <[email protected]> Copilot-Session: 78ab2fd-adea-41cf-a67f-c0b823438e07
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @dotnet/area-infrastructure-libraries |
There was a problem hiding this comment.
Pull request overview
This PR opts several ref-assembly projects into the updated-memory-safety-rules compiler feature and updates the ref surfaces to reflect the approved “caller-unsafe” API annotations by applying the unsafe modifier to the specified members. It also enables AllowUnsafeBlocks in ref projects that now contain unsafe-annotated declarations.
Changes:
- Enable
updated-memory-safety-rules(andAllowUnsafeBlockswhere needed) for multiple ref projects. - Mark approved APIs as caller-unsafe by adding
unsafeto their ref declarations across System.Runtime, interop, intrinsics/vectors, crypto, and other libraries. - Adjust a few ref declarations to align with updated safety-contract semantics (e.g., use of the new
safecontextual keyword in FieldOffset unions).
Reviewed changes
Copilot reviewed 26 out of 26 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Transactions.Local/ref/System.Transactions.Local.csproj | Enable unsafe blocks + opt into updated memory safety rules for ref compilation. |
| src/libraries/System.Transactions.Local/ref/System.Transactions.Local.cs | Mark IDtcTransaction pointer-accepting members as unsafe (caller-unsafe). |
| src/libraries/System.Security.Principal.Windows/ref/System.Security.Principal.Windows.csproj | Enable unsafe blocks + opt into updated memory safety rules for ref compilation. |
| src/libraries/System.Security.Principal.Windows/ref/System.Security.Principal.Windows.cs | Mark handle-based ctor APIs as unsafe (caller-unsafe). |
| src/libraries/System.Security.Cryptography/ref/System.Security.Cryptography.csproj | Enable unsafe blocks + opt into updated memory safety rules for ref compilation. |
| src/libraries/System.Security.Cryptography/ref/System.Security.Cryptography.cs | Mark various handle-based crypto/X509 APIs as unsafe (caller-unsafe). |
| src/libraries/System.Runtime/ref/System.Runtime.csproj | Opt into updated memory safety rules for System.Runtime ref compilation. |
| src/libraries/System.Runtime/ref/System.Runtime.cs | Mark approved APIs as unsafe (caller-unsafe) across GC, RuntimeHelpers, Unsafe, MemoryMarshal, handles, etc. |
| src/libraries/System.Runtime.Intrinsics/ref/System.Runtime.Intrinsics.csproj | Opt into updated memory safety rules for intrinsics ref compilation. |
| src/libraries/System.Runtime.Intrinsics/ref/System.Runtime.Intrinsics.cs | Mark approved vector APIs (CreateScalarUnsafe, LoadUnsafe, StoreUnsafe, etc.) as unsafe. |
| src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.csproj | Opt into updated memory safety rules for interop ref compilation. |
| src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs | Mark approved interop APIs as unsafe (caller-unsafe), including many Marshal members and COM-related surfaces. |
| src/libraries/System.Numerics.Vectors/ref/System.Numerics.Vectors.csproj | Opt into updated memory safety rules for vectors ref compilation. |
| src/libraries/System.Numerics.Vectors/ref/System.Numerics.Vectors.cs | Mark approved Vector*Unsafe APIs as unsafe (caller-unsafe). |
| src/libraries/System.Net.Sockets/ref/System.Net.Sockets.csproj | Enable unsafe blocks + opt into updated memory safety rules for ref compilation. |
| src/libraries/System.Net.Sockets/ref/System.Net.Sockets.cs | Mark handle-wrapping ctor as unsafe (caller-unsafe). |
| src/libraries/System.Memory/ref/System.Memory.csproj | Opt into updated memory safety rules for ref compilation. |
| src/libraries/System.Memory/ref/System.Memory.cs | Mark SequenceMarshal.TryRead<T> as unsafe (caller-unsafe). |
| src/libraries/System.IO.Pipes/ref/System.IO.Pipes.csproj | Enable unsafe blocks + opt into updated memory safety rules for ref compilation. |
| src/libraries/System.IO.Pipes/ref/System.IO.Pipes.cs | Mark handle-wrapping ctor as unsafe (caller-unsafe). |
| src/libraries/System.Diagnostics.StackTrace/ref/System.Diagnostics.StackTrace.csproj | Enable unsafe blocks + opt into updated memory safety rules for ref compilation. |
| src/libraries/System.Diagnostics.StackTrace/ref/System.Diagnostics.StackTrace.cs | Mark symbol-store COM-pointer APIs as unsafe (caller-unsafe). |
| src/libraries/System.Diagnostics.Process/ref/System.Diagnostics.Process.csproj | Opt into updated memory safety rules for ref compilation. |
| src/libraries/System.Diagnostics.Process/ref/System.Diagnostics.Process.cs | Mark handle-wrapping ctor as unsafe (caller-unsafe). |
| src/libraries/Microsoft.Win32.Registry/ref/Microsoft.Win32.Registry.csproj | Enable unsafe blocks + opt into updated memory safety rules for ref compilation. |
| src/libraries/Microsoft.Win32.Registry/ref/Microsoft.Win32.Registry.cs | Mark handle-wrapping ctor as unsafe (caller-unsafe). |
… APIs Adds the members approved in #129751 across four more reference assemblies. These are the ones nothing flags today: none of them names a pointer, so the compat-mode rule that infers a caller contract from a pointer in the signature cannot see them, and callers get no signal even after opting in. They are the APIs that take a raw address, or hand back memory, in a way the runtime will later act on. RuntimeTypeHandle.FromIntPtr and its method and field counterparts fabricate a handle from an integer. RuntimeHelpers.Box reads SizeOf(type) bytes from a caller-supplied reference. GC.AllocateUninitializedArray hands back memory whose contents are whatever was on the heap. The X509 and OpenSsl constructors, SecurityIdentifier, and the symbol-store and DTC interfaces adopt a native pointer supplied by the caller. SafeHandle is left out entirely, both the derived constructors the proposal covers and the base members it does not. Marking the base constructor propagates to every subclass constructor, including parameterless ones that adopt no handle at all, so the shape of that contract needs deciding on its own. ArrayPool and MemoryPool are excluded too: the API review removed them pending a wider audience. The reference projects gain AllowUnsafeBlocks where they did not already set it, since the modifier is otherwise CS0227. Verified against the shipped reference assembly, which reports nothing for these members even with the opt-in enabled, while the rebuilt one reports each of them. A caller that has not opted in is unaffected. Co-authored-by: Copilot <[email protected]> Copilot-Session: 78ab2fd-adea-41cf-a67f-c0b823438e07
3a8ae35 to
a8d2df6
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 18 out of 18 changed files in this pull request and generated no new comments.
Suppressed comments (3)
src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs:495
StrategyBasedComWrappers.CreateObject(nint, CreateObjectFlags)is still not markedunsafe, even though the approved proposal for #129750 marks both CreateObject overloads as caller-unsafe. This leaves the two overloads inconsistent and diverges from the approved API shape.
protected sealed override object CreateObject(nint externalComObject, System.Runtime.InteropServices.CreateObjectFlags flags) { throw null; }
unsafe protected sealed override object? CreateObject(nint externalComObject, System.Runtime.InteropServices.CreateObjectFlags flags, object? userState, out System.Runtime.InteropServices.CreatedWrapperFlags wrapperFlags) { throw null; }
src/libraries/System.Runtime/ref/System.Runtime.cs:462
ArgumentNullException.ThrowIfNull(void*)had itsunsafemodifier removed. This changes the API's safety contract under the updated-memory-safety-rules feature, but it is not part of the linked approved proposals. If this isn’t intentionally expanding the scope beyond the referenced API proposals, this should be reverted (or covered by an additional approved proposal).
src/libraries/System.Runtime/ref/System.Runtime.cs:15571- This PR marks several
GCHandlemembers asunsafe(e.g.,Free,FromIntPtr, explicit cast). These safety-contract changes are not part of the linked approved proposals in the PR description (e.g., #129751 does not list anyGCHandleAPIs). Please either (1) link the correspondingapi-approvedproposal(s) covering these members, or (2) drop these additions to keep the PR aligned with the stated scope.
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Annotate APIs as caller-unsafe based on the approved API proposals:
Doing this properly requires full migration to the new rules in sources (a huge change) + changes in GenAPI tool in arcade (BTW, it looks like we mosly modify
/ref/by hands). A simpler manual change for .NET 11.0.