Skip to content

Enable unsafe-v2 by default in src/libraries (690 csproj enrolled) - #138081

Merged
EgorBo merged 3 commits into
dotnet:mainfrom
EgorBo:unsafe-v2-default-libraries
Nov 24, 2026
Merged

EgorBo merged 3 commits into
dotnet:mainfrom
EgorBo:unsafe-v2-default-libraries

Conversation

@EgorBo

@EgorBo EgorBo commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Makes the updated memory safety rules (unsafe-v2) the default for every project under src/libraries and opts out the projects that aren't ready yet. New libraries and tests start on v2, and opted-out projects can be migrated one by one.

Changes

  • src/libraries/Directory.Build.props defaults MemorySafetyRulesVersion to 2, scoped to projects under src/libraries
  • Projects that would need more than a few lines of changes are opted out:
    <!-- TODO: Remove once migrated to unsafe-v2. -->
    <MemorySafetyRulesVersion>1</MemorySafetyRulesVersion>
  • Ref projects stay on v2 only if they were already enrolled in Compile ref-assemblies with updated-memory-safety-rules #131733 or their /ref/ sources contain no unsafe at all, so no public API becomes requires-unsafe here.
  • Projects that needed only a few lines got minimal mechanical fixes
  • Removed the now redundant explicit MemorySafetyRulesVersion=2 from already enrolled projects.

Numbers

ref src gen tests other* total
enrolled 172 106 12 312 88 690
opted out 14 96 4 101 1 216

* shims, stubs, tools, fuzzing. The HttpStress and SslStress projects don't import src/libraries/Directory.Build.props, so they're unaffected.

System.Diagnostics.EventLog.Messages stays on v1 because it must contain no types, and the updated rules make the compiler embed attribute types into its netstandard2.0 build.

Large assemblies (CoreLib, Cryptography, Net.*, etc.) are opted out and will be migrated separately with the migration tooling.

References

Default MemorySafetyRulesVersion to 2 for all projects under src/libraries
and opt out (MemorySafetyRulesVersion=1) the 214 projects that need more
than a few lines of changes. Ref projects stay on v2 only if they were
enrolled in dotnet#131733 or have no `unsafe` in their sources.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 7475d88a-8fe0-451e-89f0-b638440a284d
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

Keep `safe` only where a /// <safety> comment can justify it. Local-function
UnsafeAccessor externs (where doc comments aren't valid) become `unsafe` with
an unsafe block at the call site. System.Threading.Channels.Tests goes back to
v1: Mono doesn't type-check [UnsafeAccessorType] object arguments.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 7475d88a-8fe0-451e-89f0-b638440a284d
@EgorBo EgorBo changed the title Enable unsafe-v2 by default in src/libraries (694 csproj enrolled) Enable unsafe-v2 by default in src/libraries (693 csproj enrolled) Nov 21, 2026
@EgorBo

EgorBo commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

/azp list

@azure-pipelines

This comment was marked as resolved.

@EgorBo

EgorBo commented Nov 21, 2026 •

Copy link
Copy Markdown
Member Author

/azp run runtime-extra-platforms, runtime-wasm, runtime-nativeaot-outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).

The assembly must contain no types (EventLogMessagesContainsNoTypes), but
targeting netstandard2.0 under the updated rules makes the compiler embed
MemorySafetyRulesAttribute, RequiresUnsafeAttribute and EmbeddedAttribute.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 7475d88a-8fe0-451e-89f0-b638440a284d
@EgorBo EgorBo changed the title Enable unsafe-v2 by default in src/libraries (693 csproj enrolled) Enable unsafe-v2 by default in src/libraries (690 csproj enrolled) Nov 22, 2026
@EgorBo

EgorBo commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-extra-platforms, runtime-wasm, runtime-nativeaot-outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Changing compiler-rule defaults across hundreds of projects and shared v1/v2 sources requires cross-platform, multi-target, generator, and safety-contract validation unavailable in this source-only review.

No open issues 🎉

What changed in this PR

Makes unsafe-v2 the default for projects under src/libraries, extending the earlier reference-assembly and compression migrations. Explicit opt-outs allow the remaining projects to migrate incrementally while preserving existing public safety contracts.

Changes:

  • Adds a directory-scoped default and removes redundant explicit v2 settings.
  • Keeps unmigrated projects on v1, including the type-free EventLog messages assembly.
  • Applies mechanical safety annotations and scoped unsafe blocks, enabling unsafe compilation where needed.

Builds and tests were not run during this review.

Paths below are relative to src/libraries/.

File Description
System.Windows.Extensions/​tests/​System.Windows.Extensions.Tests.csproj Keeps tests on v1 pending migration.
System.Windows.Extensions/​src/​System.Windows.Extensions.csproj Keeps implementation on v1.
System.Web.HttpUtility/​src/​System.Web.HttpUtility.csproj Keeps implementation on v1.
System.Transactions.Local/​src/​System.Transactions.Local.csproj Keeps implementation on v1.
System.Transactions.Local/​ref/​System.Transactions.Local.csproj Inherits the v2 default.
System.Threading/​tests/​System.Threading.Tests.csproj Keeps tests on v1.
System.Threading.ThreadPool/​tests/​WindowsThreadPool/​System.Threading.ThreadPool.WindowsThreadPool.Tests.csproj Keeps Windows thread-pool tests on v1.
System.Threading.ThreadPool/​tests/​System.Threading.ThreadPool.Tests.csproj Keeps thread-pool tests on v1.
System.Threading.ThreadPool/​ref/​System.Threading.ThreadPool.csproj Preserves reference assembly's v1 contracts.
System.Threading.ThreadPool.WebAssembly.Threading/​ref/​System.Threading.ThreadPool.WebAssembly.Threading.csproj Preserves WebAssembly reference contracts.
System.Threading.Thread/​tests/​System.Threading.Thread.Tests.csproj Keeps thread tests on v1.
System.Threading.Tasks.Parallel/​src/​System/​Threading/​Tasks/​ParallelLoopState.cs Moves unsafe operations into explicit blocks.
System.Threading.Tasks.Dataflow/​src/​System.Threading.Tasks.Dataflow.csproj Enables unsafe compilation for annotations.
System.Threading.Tasks.Dataflow/​src/​Internal/​Padding.cs Marks the non-overlapping field safe.
System.Threading.Overlapped/​tests/​System.Threading.Overlapped.Tests.csproj Keeps overlapped tests on v1.
System.Threading.Overlapped/​ref/​System.Threading.Overlapped.csproj Preserves reference assembly's v1 contracts.
System.Threading.Channels/​tests/​System.Threading.Channels.Tests.csproj Keeps channel tests on v1.
System.Threading.AccessControl/​src/​System.Threading.AccessControl.csproj Keeps implementation on v1.
System.Text.RegularExpressions/​tests/​UnitTests/​System.Text.RegularExpressions.Unit.Tests.csproj Keeps regex unit tests on v1.
System.Text.RegularExpressions/​src/​System.Text.RegularExpressions.csproj Keeps regex implementation on v1.
System.Text.RegularExpressions/​gen/​System.Text.RegularExpressions.Generator.csproj Keeps regex generator on v1.
System.Text.Json/​tests/​System.Text.Json.Tests/​System.Text.Json.Tests.csproj Inherits the v2 default.
System.Text.Json/​tests/​System.Text.Json.SourceGeneration.Unit.Tests/​System.Text.Json.SourceGeneration.Unit.Tests.targets Removes redundant v2 selection.
System.Text.Json/​tests/​System.Text.Json.SourceGeneration.Tests/​System.Text.Json.SourceGeneration.Tests.targets Removes redundant v2 selection.
System.Text.Json/​tests/​System.Text.Json.SourceGeneration.TestLibrary/​System.Text.Json.TestLibrary.targets Removes redundant v2 selection.
System.Text.Json/​src/​System.Text.Json.csproj Keeps JSON implementation on v1.
System.Text.Encodings.Web/​tests/​System.Text.Encodings.Web.Tests.csproj Keeps encoding tests on v1.
System.Text.Encodings.Web/​src/​System.Text.Encodings.Web.csproj Keeps implementation on v1.
System.Text.Encodings.Web/​ref/​System.Text.Encodings.Web.csproj Preserves reference assembly's v1 contracts.
System.Text.Encoding.Extensions/​ref/​System.Text.Encoding.Extensions.csproj Preserves reference assembly's v1 contracts.
System.Text.Encoding.CodePages/​src/​System.Text.Encoding.CodePages.csproj Keeps code-page implementation on v1.
System.Speech/​src/​System.Speech.csproj Keeps speech implementation on v1.
System.ServiceProcess.ServiceController/​tests/​System.ServiceProcess.ServiceController.TestService/​System.ServiceProcess.ServiceController.TestService.csproj Keeps test service on v1.
System.ServiceProcess.ServiceController/​src/​System.ServiceProcess.ServiceController.csproj Keeps implementation on v1.
System.Security.Principal.Windows/​src/​System.Security.Principal.Windows.csproj Keeps implementation on v1.
System.Security.Principal.Windows/​ref/​System.Security.Principal.Windows.csproj Inherits the v2 default.
System.Security.Cryptography/​tests/​System.Security.Cryptography.Tests.csproj Keeps cryptography tests on v1.
System.Security.Cryptography/​src/​System.Security.Cryptography.csproj Keeps cryptography implementation on v1.
System.Security.Cryptography/​ref/​System.Security.Cryptography.csproj Inherits the v2 default.
System.Security.Cryptography.Xml/​src/​System.Security.Cryptography.Xml.csproj Keeps XML cryptography on v1.
System.Security.Cryptography.ProtectedData/​src/​System.Security.Cryptography.ProtectedData.csproj Keeps protected-data implementation on v1.
System.Security.Cryptography.Pkcs/​tests/​Rfc3161/​TimestampTokenInfoTests.cs Wraps memory-identity assertion in unsafe context.
System.Security.Cryptography.Pkcs/​src/​System.Security.Cryptography.Pkcs.csproj Keeps PKCS implementation on v1.
System.Security.Cryptography.Cose/​src/​System.Security.Cryptography.Cose.csproj Keeps COSE implementation on v1.
System.Security.Cryptography.Cng/​tests/​System.Security.Cryptography.Cng.Tests.csproj Keeps CNG tests on v1.
System.Security.AccessControl/​tests/​System.Security.AccessControl.Tests.csproj Keeps access-control tests on v1.
System.Security.AccessControl/​src/​System.Security.AccessControl.csproj Keeps implementation on v1.
System.Runtime/​tests/​System.Threading.Tasks.Tests/​System.Threading.Tasks.Tests.csproj Enables unsafe compilation for accessor annotations.
System.Runtime/​tests/​System.Threading.Tasks.Tests/​System.Runtime.CompilerServices/​AsyncTaskMethodBuilderTests.cs Marks field accessors safe with rationale.
System.Runtime/​tests/​System.Text.Encoding.Tests/​System.Text.Encoding.Tests.csproj Keeps encoding tests on v1.
System.Runtime/​tests/​System.Security.SecureString.Tests/​System.Security.SecureString.Tests.csproj Keeps SecureString tests on v1.
System.Runtime/​tests/​System.Runtime.Tests/​System/​TimeZoneInfoTests.Invariant.cs Annotates accessor and wraps its call.
System.Runtime/​tests/​System.Runtime.Tests/​System/​Reflection/​InvokeInterpreted/​System.Runtime.ReflectionInvokeInterpreted.Tests.csproj Keeps interpreted-invoke tests on v1.
System.Runtime/​tests/​System.Runtime.Tests/​System/​Reflection/​InvokeEmit/​System.Runtime.ReflectionInvokeEmit.Tests.csproj Keeps emitted-invoke tests on v1.
System.Runtime/​tests/​System.Runtime.Tests/​System.Runtime.Tests.csproj Keeps runtime tests on v1.
System.Runtime/​tests/​System.Runtime.Tests/​NlsTests/​System.Runtime.Nls.Tests.csproj Keeps NLS runtime tests on v1.
System.Runtime/​tests/​System.Runtime.InteropServices.RuntimeInformation.Tests/​System.Runtime.InteropServices.RuntimeInformation.Tests.csproj Keeps runtime-information tests on v1.
System.Runtime/​tests/​System.Runtime.Handles.Tests/​System.Runtime.Handles.Tests.csproj Enables unsafe compilation for interop.
System.Runtime/​tests/​System.Runtime.Handles.Tests/​SafeHandle.cs Annotates native import and wraps calls.
System.Runtime/​tests/​System.Runtime.Extensions.Tests/​System.Runtime.Extensions.Tests.csproj Keeps runtime-extension tests on v1.
System.Runtime/​tests/​System.Runtime.CompilerServices.Unsafe.Tests/​System.Runtime.CompilerServices.Unsafe.Tests.csproj Keeps Unsafe API tests on v1.
System.Runtime/​tests/​System.Resources.ResourceManager.Tests/​System.Resources.ResourceManager.Tests.csproj Keeps resource-manager tests on v1.
System.Runtime/​tests/​System.Reflection.Tests/​System.Reflection.Tests.csproj Keeps reflection tests on v1.
System.Runtime/​tests/​System.Reflection.Tests/​InvokeInterpreted/​System.Reflection.InvokeInterpreted.Tests.csproj Keeps interpreted reflection tests on v1.
System.Runtime/​tests/​System.Reflection.Tests/​InvokeEmit/​System.Reflection.InvokeEmit.Tests.csproj Keeps emitted reflection tests on v1.
System.Runtime/​tests/​System.IO.UnmanagedMemoryStream.Tests/​System.IO.UnmanagedMemoryStream.Tests.csproj Keeps unmanaged-stream tests on v1.
System.Runtime/​tests/​System.IO.Tests/​System.IO.Tests.csproj Keeps I/O tests on v1.
System.Runtime/​tests/​System.IO.FileSystem.Tests/​System.IO.FileSystem.Tests.csproj Keeps filesystem tests on v1.
System.Runtime/​tests/​System.IO.FileSystem.Tests/​DisabledFileLockingTests/​System.IO.FileSystem.DisabledFileLocking.Tests.csproj Keeps disabled-locking tests on v1.
System.Runtime/​tests/​System.Globalization.Tests/​System.Globalization.Tests.csproj Keeps globalization tests on v1.
System.Runtime/​tests/​System.Globalization.Tests/​NlsTests/​System.Globalization.Nls.Tests.csproj Keeps NLS globalization tests on v1.
System.Runtime/​tests/​System.Globalization.Tests/​Invariant/​Invariant.Tests.csproj Keeps invariant-globalization tests on v1.
System.Runtime/​tests/​System.Dynamic.Runtime.Tests/​System.Dynamic.Runtime.Tests.csproj Keeps dynamic-runtime tests on v1.
System.Runtime/​tests/​System.Diagnostics.Debug.Tests/​System.Diagnostics.Debug.Tests.csproj Keeps debug tests on v1.
System.Runtime/​tests/​System.Buffers.Tests/​ArrayPool/​CollectionTests.cs Wraps native allocation in unsafe context.
System.Runtime/​ref/​System.Runtime.csproj Inherits the v2 default.
System.Runtime.Serialization.Xml/​tests/​XsdDataContractExporterTests/​SerializationTypes/​LegacyTypes.cs Removes obsolete type-level unsafe modifier.
System.Runtime.Serialization.Xml/​tests/​Canonicalization/​System.Runtime.Serialization.Xml.Canonicalization.Tests.csproj Keeps canonicalization tests on v1.
System.Runtime.Serialization.Formatters/​tests/​System.Runtime.Serialization.Formatters.Tests.csproj Keeps formatter tests on v1.
System.Runtime.Serialization.Formatters/​src/​System/​Runtime/​Serialization/​Formatters/​Binary/​BinaryParser.cs Scopes unsafe context to DateTime reinterpretation.
System.Runtime.Numerics/​tests/​System.Runtime.Numerics.Tests.csproj Keeps numerics tests on v1.
System.Runtime.Numerics/​src/​System.Runtime.Numerics.csproj Keeps numerics implementation on v1.
System.Runtime.Loader/​tests/​System.Runtime.Loader.Tests.csproj Keeps loader tests on v1.
System.Runtime.Loader/​tests/​System.Runtime.Loader.Test.Assembly2/​System.Runtime.Loader.Test.Assembly2.csproj Enables unsafe compilation for shared test code.
System.Runtime.Loader/​tests/​System.Runtime.Loader.Test.Assembly/​TestClass.cs Annotates native import and wraps invocation.
System.Runtime.Loader/​tests/​System.Runtime.Loader.Test.Assembly/​System.Runtime.Loader.Test.Assembly.csproj Enables unsafe compilation for test interop.
System.Runtime.Loader/​ref/​System.Runtime.Loader.csproj Preserves reference assembly's v1 contracts.
System.Runtime.Intrinsics/​tests/​System.Runtime.Intrinsics.Tests.csproj Keeps intrinsics tests on v1.
System.Runtime.Intrinsics/​ref/​System.Runtime.Intrinsics.csproj Inherits the v2 default.
System.Runtime.InteropServices/​tests/​TestAssets/​SharedTypes/​SharedTypes.csproj Keeps shared interop types on v1.
System.Runtime.InteropServices/​tests/​TestAssets/​NativeExports/​NativeExports.csproj Keeps native-export test asset on v1.
System.Runtime.InteropServices/​tests/​System.Runtime.InteropServices.UnitTests/​System.Runtime.InteropServices.Tests.csproj Keeps interop unit tests on v1.
System.Runtime.InteropServices/​tests/​System.Runtime.InteropServices.ComDisabled.UnitTests/​System.Runtime.InteropServices.ComDisabled.Tests.csproj Keeps COM-disabled tests on v1.
System.Runtime.InteropServices/​tests/​LibraryImportGenerator.Tests/​LibraryImportGenerator.Tests.csproj Keeps library-import tests on v1.
System.Runtime.InteropServices/​tests/​ComInterfaceGenerator.Tests/​ComInterfaceGenerator.Tests.csproj Keeps COM-generator tests on v1.
System.Runtime.InteropServices/​tests/​Ancillary.Interop/​Ancillary.Interop.csproj Keeps ancillary interop asset on v1.
System.Runtime.InteropServices/​src/​System.Runtime.InteropServices.csproj Keeps interop implementation on v1.
System.Runtime.InteropServices/​ref/​System.Runtime.InteropServices.csproj Inherits the v2 default.
System.Runtime.InteropServices/​gen/​Microsoft.Interop.SourceGeneration/​Microsoft.Interop.SourceGeneration.csproj Keeps shared generator infrastructure on v1.
System.Runtime.InteropServices/​gen/​ComInterfaceGenerator/​ComInterfaceGenerator.csproj Keeps COM generator on v1.
System.Runtime.InteropServices.JavaScript/​tests/​System.Runtime.InteropServices.JavaScript.UnitTests/​System.Runtime.InteropServices.JavaScript.Tests.csproj Keeps JavaScript interop tests on v1.
System.Runtime.InteropServices.JavaScript/​src/​System.Runtime.InteropServices.JavaScript.csproj Keeps JavaScript interop implementation on v1.
System.Runtime.InteropServices.JavaScript/​ref/​System.Runtime.InteropServices.JavaScript.csproj Preserves reference assembly's v1 contracts.
System.Runtime.InteropServices.JavaScript/​gen/​JSImportGenerator/​JSImportGenerator.csproj Keeps JavaScript import generator on v1.
System.Runtime.Caching/​src/​System.Runtime.Caching.csproj Keeps caching implementation on v1.
System.Resources.Extensions/​tests/​BinaryFormatTests/​System.Resources.Extensions.BinaryFormat.Tests.csproj Keeps binary-format tests on v1.
System.Resources.Extensions/​src/​System.Resources.Extensions.csproj Keeps resource extensions on v1.
System.Reflection.MetadataLoadContext/​tests/​System.Reflection.MetadataLoadContext.Tests.csproj Keeps metadata-load-context tests on v1.
System.Reflection.MetadataLoadContext/​src/​System.Reflection.MetadataLoadContext.csproj Keeps metadata-load-context implementation on v1.
System.Reflection.Metadata/​tests/​System.Reflection.Metadata.Tests.csproj Keeps metadata tests on v1.
System.Reflection.Metadata/​src/​System.Reflection.Metadata.csproj Keeps metadata implementation on v1.
System.Reflection.Metadata/​ref/​System.Reflection.Metadata.csproj Preserves reference assembly's v1 contracts.
System.Reflection.Emit/​tests/​System.Reflection.Emit.Tests.csproj Keeps reflection-emit tests on v1.
System.Reflection.Emit.Lightweight/​tests/​System.Reflection.Emit.Lightweight.Tests.csproj Keeps lightweight-emit tests on v1.
System.Reflection.Emit.Lightweight/​ref/​System.Reflection.Emit.Lightweight.csproj Preserves reference assembly's v1 contracts.
System.Reflection.Emit.ILGeneration/​tests/​ILGenerator/​Emit4Tests.cs Removes unsafe modifiers from signature helpers.
System.Reflection.DispatchProxy/​tests/​System.Reflection.DispatchProxy.Tests.csproj Keeps proxy tests on v1.
System.Reflection.DispatchProxy/​src/​System/​Reflection/​DispatchProxyGenerator.cs Scopes unsafe context to pointer extraction.
System.Reflection.Context/​tests/​ExtendedTypeTests.cs Removes type-level unsafe modifier.
System.Private.Xml/​src/​System.Private.Xml.csproj Keeps XML implementation on v1.
System.Private.Uri/​src/​System/​PercentEncodingHelper.cs Isolates byte-span conversion in unsafe context.
System.Private.Uri/​src/​System.Private.Uri.csproj Keeps URI implementation on v1.
System.Private.DataContractSerialization/​src/​System.Private.DataContractSerialization.csproj Keeps serialization implementation on v1.
System.Private.CoreLib/​ref/​System.Private.CoreLib.csproj Keeps CoreLib reference project on v1.
System.Numerics.Vectors/​tests/​System.Numerics.Vectors.Tests.csproj Keeps vector tests on v1.
System.Numerics.Vectors/​ref/​System.Numerics.Vectors.csproj Inherits the v2 default.
System.Numerics.Tensors/​tests/​System.Numerics.Tensors.Tests.csproj Keeps tensor tests on v1.
System.Numerics.Tensors/​src/​System.Numerics.Tensors.csproj Keeps tensor implementation on v1.
System.Numerics.Tensors/​ref/​System.Numerics.Tensors.csproj Preserves reference assembly's v1 contracts.
System.Net.WebSockets/​src/​System.Net.WebSockets.csproj Keeps WebSocket implementation on v1.
System.Net.WebSockets.Client/​src/​System.Net.WebSockets.Client.csproj Keeps WebSocket client on v1.
System.Net.WebProxy/​src/​System/​Net/​WebProxy.cs Wraps stack allocation in unsafe context.
System.Net.Sockets/​tests/​FunctionalTests/​System.Net.Sockets.Tests.csproj Keeps socket tests on v1.
System.Net.Sockets/​src/​System.Net.Sockets.csproj Keeps socket implementation on v1.
System.Net.ServerSentEvents/​tests/​System.Net.ServerSentEvents.Tests.csproj Enables unsafe compilation for tests.
System.Net.ServerSentEvents/​src/​System.Net.ServerSentEvents.csproj Keeps server-sent events implementation on v1.
System.Net.Security/​tests/​UnitTests/​System.Net.Security.Unit.Tests.csproj Keeps network-security unit tests on v1.
System.Net.Security/​tests/​FunctionalTests/​System.Net.Security.Tests.csproj Keeps network-security functional tests on v1.
System.Net.Security/​src/​System.Net.Security.csproj Keeps network-security implementation on v1.
System.Net.Requests/​src/​System.Net.Requests.csproj Keeps request implementation on v1.
System.Net.Quic/​tests/​FunctionalTests/​System.Net.Quic.Functional.Tests.csproj Keeps QUIC tests on v1.
System.Net.Quic/​src/​System.Net.Quic.csproj Keeps QUIC implementation on v1.
System.Net.Primitives/​tests/​UnitTests/​System.Net.Primitives.UnitTests.Tests.csproj Keeps network-primitives unit tests on v1.
System.Net.Primitives/​tests/​PalTests/​System.Net.Primitives.Pal.Tests.csproj Keeps platform-abstraction tests on v1.
System.Net.Primitives/​tests/​FunctionalTests/​System.Net.Primitives.Functional.Tests.csproj Keeps network-primitives functional tests on v1.
System.Net.Primitives/​src/​System.Net.Primitives.csproj Keeps network primitives on v1.
System.Net.Ping/​tests/​FunctionalTests/​System.Net.Ping.Functional.Tests.csproj Keeps ping tests on v1.
System.Net.Ping/​src/​System.Net.Ping.csproj Keeps ping implementation on v1.
System.Net.NetworkInformation/​tests/​FunctionalTests/​System.Net.NetworkInformation.Functional.Tests.csproj Keeps network-information tests on v1.
System.Net.NetworkInformation/​src/​System.Net.NetworkInformation.csproj Keeps network-information implementation on v1.
System.Net.NameResolution/​tests/​PalTests/​System.Net.NameResolution.Pal.Tests.csproj Keeps name-resolution platform tests on v1.
System.Net.NameResolution/​src/​System/​Net/​DnsEncodedName.cs Removes unnecessary method-level unsafe modifiers.
System.Net.NameResolution/​src/​System.Net.NameResolution.csproj Keeps name-resolution implementation on v1.
System.Net.Mail/​tests/​Unit/​System.Net.Mail.Unit.Tests.csproj Keeps mail unit tests on v1.
System.Net.Mail/​tests/​Functional/​System.Net.Mail.Functional.Tests.csproj Keeps mail functional tests on v1.
System.Net.Mail/​src/​System.Net.Mail.csproj Keeps mail implementation on v1.
System.Net.HttpListener/​tests/​System.Net.HttpListener.Tests.csproj Keeps HTTP listener tests on v1.
System.Net.HttpListener/​src/​System.Net.HttpListener.csproj Keeps HTTP listener implementation on v1.
System.Net.Http/​tests/​UnitTests/​System.Net.Http.Unit.Tests.csproj Keeps HTTP unit tests on v1.
System.Net.Http/​tests/​FunctionalTests/​System.Net.Http.Functional.Tests.csproj Keeps HTTP functional tests on v1.
System.Net.Http/​src/​System.Net.Http.csproj Keeps HTTP implementation on v1.
System.Net.Http.WinHttpHandler/​tests/​UnitTests/​System.Net.Http.WinHttpHandler.Unit.Tests.csproj Keeps WinHTTP handler tests on v1.
System.Net.Http.WinHttpHandler/​src/​System.Net.Http.WinHttpHandler.csproj Keeps WinHTTP handler on v1.
System.Memory/​tests/​System.Memory.Tests.csproj Keeps memory tests on v1.
System.Memory/​src/​System.Memory.csproj Keeps memory implementation on v1.
System.Memory/​ref/​System.Memory.csproj Inherits the v2 default.
System.Memory.Data/​tests/​System.Memory.Data.Tests.csproj Keeps memory-data tests on v1.
System.Memory.Data/​src/​System.Memory.Data.csproj Keeps memory-data implementation on v1.
System.Management/​src/​System.Management.csproj Keeps management implementation on v1.
System.Linq/​src/​System.Linq.csproj Keeps LINQ implementation on v1.
System.Linq.Expressions/​src/​System/​Dynamic/​Utils/​DelegateHelpers.cs Annotates accessors and wraps their calls.
System.IO.Ports/​tests/​System.IO.Ports.Tests.csproj Keeps serial-port tests on v1.
System.IO.Ports/​src/​System.IO.Ports.csproj Keeps serial-port implementation on v1.
System.IO.Pipes/​tests/​System.IO.Pipes.Tests.csproj Keeps pipe tests on v1.
System.IO.Pipes/​src/​System.IO.Pipes.csproj Keeps pipe implementation on v1.
System.IO.Pipelines/​tests/​Infrastructure/​TestMemoryPool.cs Places array extraction in unsafe contexts.
System.IO.Packaging/​tests/​System.IO.Packaging.Tests.csproj Enables unsafe compilation for packaging tests.
System.IO.Packaging/​tests/​LargeFilesTests.Net.cs Wraps uninitialized allocations in unsafe context.
System.IO.Packaging/​src/​System.IO.Packaging.csproj Keeps packaging implementation on v1.
System.IO.MemoryMappedFiles/​tests/​System.IO.MemoryMappedFiles.Tests.csproj Keeps memory-mapped-file tests on v1.
System.IO.MemoryMappedFiles/​src/​System.IO.MemoryMappedFiles.csproj Keeps memory-mapped-file implementation on v1.
System.IO.IsolatedStorage/​src/​System/​IO/​IsolatedStorage/​IsolatedStorage.cs Wraps stack-buffer formatting in unsafe context.
System.IO.Hashing/​tests/​NonCryptoHashTestDriver.cs Wraps uninitialized allocation in unsafe context.
System.IO.Hashing/​src/​System.IO.Hashing.csproj Keeps hashing implementation on v1.
System.IO.FileSystem.Watcher/​tests/​System.IO.FileSystem.Watcher.Tests.csproj Keeps filesystem-watcher tests on v1.
System.IO.FileSystem.Watcher/​src/​System.IO.FileSystem.Watcher.csproj Keeps filesystem watcher on v1.
System.IO.FileSystem.DriveInfo/​tests/​System.IO.FileSystem.DriveInfo.Tests.csproj Keeps drive-information tests on v1.
System.IO.FileSystem.DriveInfo/​src/​System.IO.FileSystem.DriveInfo.csproj Keeps drive-information implementation on v1.
System.IO.FileSystem.AccessControl/​src/​System.IO.FileSystem.AccessControl.csproj Keeps filesystem access control on v1.
System.IO.Compression/​tests/​System.IO.Compression.Tests.csproj Inherits the v2 default.
System.IO.Compression/​src/​System.IO.Compression.csproj Inherits the v2 default.
System.IO.Compression.ZipFile/​tests/​System.IO.Compression.ZipFile.Tests.csproj Keeps ZIP-file tests on v1.
System.IO.Compression.ZipFile/​src/​System.IO.Compression.ZipFile.csproj Keeps ZIP-file implementation on v1.
System.IO.Compression.Brotli/​src/​System.IO.Compression.Brotli.csproj Keeps Brotli implementation on v1.
System.Formats.Tar/​tests/​System.Formats.Tar.Tests.csproj Keeps TAR tests on v1.
System.Formats.Tar/​src/​System.Formats.Tar.csproj Keeps TAR implementation on v1.
System.Formats.Nrbf/​src/​System.Formats.Nrbf.csproj Keeps NRBF implementation on v1.
System.Formats.Cbor/​src/​System.Formats.Cbor.csproj Keeps CBOR implementation on v1.
System.Formats.Asn1/​tests/​System.Formats.Asn1.Tests.csproj Keeps ASN.1 tests on v1.
System.Formats.Asn1/​src/​System.Formats.Asn1.csproj Keeps ASN.1 implementation on v1.
System.Drawing.Primitives/​tests/​System.Drawing.Primitives.Tests.csproj Enables unsafe compilation for drawing tests.
System.Drawing.Primitives/​tests/​ColorTests.cs Annotates native import and wraps calls.
System.Drawing.Primitives/​src/​System.Drawing.Primitives.csproj Keeps drawing primitives on v1.
System.DirectoryServices/​src/​System.DirectoryServices.csproj Keeps directory services on v1.
System.DirectoryServices.Protocols/​src/​System.DirectoryServices.Protocols.csproj Keeps directory protocols on v1.
System.DirectoryServices.AccountManagement/​src/​System.DirectoryServices.AccountManagement.csproj Keeps account management on v1.
System.Diagnostics.Tracing/​tests/​System.Diagnostics.Tracing.Tests.csproj Keeps tracing tests on v1.
System.Diagnostics.Tracing/​ref/​System.Diagnostics.Tracing.csproj Preserves reference assembly's v1 contracts.
System.Diagnostics.TraceSource/​src/​System.Diagnostics.TraceSource.csproj Keeps trace-source implementation on v1.
System.Diagnostics.TextWriterTraceListener/​src/​System.Diagnostics.TextWriterTraceListener.csproj Keeps text-writer trace listener on v1.
System.Diagnostics.StackTrace/​tests/​StackTraceTests.cs Removes unsafe modifier from signature helper.
System.Diagnostics.StackTrace/​tests/​DiagnosticMethodInfoTests.cs Wraps function-pointer marshalling in unsafe context.
System.Diagnostics.StackTrace/​src/​System.Diagnostics.StackTrace.csproj Keeps stack-trace implementation on v1.
System.Diagnostics.StackTrace/​ref/​System.Diagnostics.StackTrace.csproj Inherits the v2 default.
System.Diagnostics.Process/​tests/​System.Diagnostics.Process.Tests.csproj Keeps process tests on v1.
System.Diagnostics.Process/​src/​System.Diagnostics.Process.csproj Keeps process implementation on v1.
System.Diagnostics.Process/​ref/​System.Diagnostics.Process.csproj Preserves reference assembly's v1 contracts.
System.Diagnostics.PerformanceCounter/​tests/​System.Diagnostics.PerformanceCounter.Tests.csproj Keeps performance-counter tests on v1.
System.Diagnostics.PerformanceCounter/​src/​System.Diagnostics.PerformanceCounter.csproj Keeps performance counters on v1.
System.Diagnostics.FileVersionInfo/​tests/​System.Diagnostics.FileVersionInfo.Tests/​System.Diagnostics.FileVersionInfo.Tests.csproj Keeps file-version tests on v1.
System.Diagnostics.FileVersionInfo/​src/​System.Diagnostics.FileVersionInfo.csproj Keeps file-version implementation on v1.
System.Diagnostics.EventLog/​src/​System.Diagnostics.EventLog.csproj Keeps event-log implementation on v1.
System.Diagnostics.EventLog/​src/​Messages/​System.Diagnostics.EventLog.Messages.csproj Uses v1 to prevent embedded attribute types.
System.Diagnostics.DiagnosticSource/​tests/​System.Diagnostics.DiagnosticSource.Tests.csproj Keeps diagnostic-source tests on v1.
System.Diagnostics.DiagnosticSource/​src/​System.Diagnostics.DiagnosticSource.csproj Keeps diagnostic-source implementation on v1.
System.Data.OleDb/​src/​System.Data.OleDb.csproj Keeps OLE DB implementation on v1.
System.Data.Odbc/​src/​System.Data.Odbc.csproj Keeps ODBC implementation on v1.
System.Data.Common/​src/​System.Data.Common.csproj Keeps common data implementation on v1.
System.Console/​tests/​System.Console.Tests.csproj Keeps console tests on v1.
System.Console/​src/​System.Console.csproj Keeps console implementation on v1.
System.Configuration.ConfigurationManager/​src/​System.Configuration.ConfigurationManager.csproj Keeps configuration manager on v1.
System.ComponentModel.TypeConverter/​src/​System.ComponentModel.TypeConverter.csproj Keeps type converters on v1.
System.Collections/​tests/​System.Collections.Tests.csproj Keeps collection tests on v1.
System.Collections/​src/​System.Collections.csproj Keeps collection implementation on v1.
System.Collections.Immutable/​tests/​System.Collections.Immutable.Tests.csproj Keeps immutable-collection tests on v1.
System.Collections.Immutable/​src/​System.Collections.Immutable.csproj Keeps immutable collections on v1.
System.Collections.Concurrent/​tests/​System.Collections.Concurrent.Tests.csproj Keeps concurrent-collection tests on v1.
System.CodeDom/​src/​System.CodeDom.csproj Keeps CodeDOM implementation on v1.
Microsoft.Win32.SystemEvents/​tests/​Microsoft.Win32.SystemEvents.Tests.csproj Keeps system-event tests on v1.
Microsoft.Win32.SystemEvents/​src/​Microsoft.Win32.SystemEvents.csproj Keeps system events on v1.
Microsoft.Win32.Registry/​src/​Microsoft.Win32.Registry.csproj Keeps registry implementation on v1.
Microsoft.Win32.Primitives/​tests/​Win32ExceptionTests.cs Annotates native import and wraps invocation.
Microsoft.Win32.Primitives/​tests/​Microsoft.Win32.Primitives.Tests.csproj Enables unsafe compilation for interop tests.
Microsoft.Extensions.Primitives/​src/​Microsoft.Extensions.Primitives.csproj Keeps extension primitives on v1.
Microsoft.Extensions.Options.DataAnnotations/​ref/​Microsoft.Extensions.Options.DataAnnotations.csproj Keeps reference project on v1.
Microsoft.Extensions.Logging.EventSource/​src/​Microsoft.Extensions.Logging.EventSource.csproj Keeps EventSource logging on v1.
Microsoft.Extensions.Logging.Console/​src/​Microsoft.Extensions.Logging.Console.csproj Keeps console logging on v1.
Microsoft.Extensions.Logging.Abstractions/​src/​LogValuesFormatter.cs Wraps stack-buffer initialization in unsafe contexts.
Microsoft.Extensions.Hosting/​tests/​UnitTests/​Microsoft.Extensions.Hosting.Unit.Tests.csproj Keeps hosting unit tests on v1.
Microsoft.Extensions.Hosting/​tests/​FunctionalTests/​Microsoft.Extensions.Hosting.Functional.Tests.csproj Keeps hosting functional tests on v1.
Microsoft.Extensions.Hosting.WindowsServices/​tests/​Microsoft.Extensions.Hosting.WindowsServices.Tests.csproj Keeps Windows-service hosting tests on v1.
Microsoft.Extensions.Hosting.WindowsServices/​src/​Microsoft.Extensions.Hosting.WindowsServices.csproj Keeps Windows-service hosting on v1.
Microsoft.Extensions.Hosting.Systemd/​src/​Microsoft.Extensions.Hosting.Systemd.csproj Keeps systemd hosting on v1.
Microsoft.Extensions.FileProviders.Physical/​tests/​Microsoft.Extensions.FileProviders.Physical.Tests.csproj Keeps physical-provider tests on v1.
Microsoft.Extensions.FileProviders.Physical/​src/​Microsoft.Extensions.FileProviders.Physical.csproj Keeps physical file provider on v1.
Microsoft.Extensions.Diagnostics/​src/​Tracing/​DefaultActivitySourceFactory.cs Wraps cached-source matching in unsafe context.
Microsoft.Extensions.Diagnostics/​src/​Metrics/​DefaultMeterFactory.cs Wraps cached-meter matching in unsafe context.
Microsoft.Extensions.Configuration/​src/​Microsoft.Extensions.Configuration.csproj Enables unsafe compilation for v2 migration.
Microsoft.Extensions.Configuration.EnvironmentVariables/​src/​Microsoft.Extensions.Configuration.EnvironmentVariables.csproj Enables unsafe compilation for v2 migration.
Microsoft.Extensions.Caching.Memory/​src/​Microsoft.Extensions.Caching.Memory.csproj Keeps memory caching on v1.
Microsoft.CSharp/​tests/​AccessTests.cs Wraps pointer-array helper calls in unsafe contexts.
Microsoft.CSharp/​src/​Microsoft.CSharp.csproj Keeps C# runtime binder on v1.
Microsoft.Bcl.Numerics/​src/​Microsoft.Bcl.Numerics.csproj Keeps numerics compatibility library on v1.
Microsoft.Bcl.Memory/​tests/​Microsoft.Bcl.Memory.Tests.csproj Keeps memory compatibility tests on v1.
Microsoft.Bcl.Memory/​src/​Microsoft.Bcl.Memory.csproj Keeps memory compatibility library on v1.
Microsoft.Bcl.Cryptography/​tests/​Microsoft.Bcl.Cryptography.Tests.csproj Keeps cryptography compatibility tests on v1.
Microsoft.Bcl.Cryptography/​src/​Microsoft.Bcl.Cryptography.csproj Keeps cryptography compatibility library on v1.
Fuzzing/​DotnetFuzzing/​DotnetFuzzing.csproj Keeps fuzzing application on v1.
Directory.Build.props Defaults projects within libraries to v2.
Common/​tests/​WasmTestRunner/​WasmTestRunner.csproj Enables unsafe compilation for WASI accessor.
Common/​tests/​WasmTestRunner/​WasmTestRunner.cs Annotates WASI accessor and wraps its call.
Common/​tests/​TestUtilities/​TestUtilities.csproj Keeps shared test utilities on v1.
Common/​tests/​StreamConformanceTests/​StreamConformanceTests.csproj Keeps stream-conformance tests on v1.
Common/​tests/​Common.Tests.csproj Keeps common tests on v1.
Common/​src/​System/​Text/​ValueStringBuilder.cs Wraps pinnable-reference retrieval in unsafe context.
Common/​src/​SourceGenerators/​SourceWriter.cs Wraps unsafe append calls in explicit contexts.
Common/​src/​Polyfills/​BitConverterPolyfills.cs Scopes unsafe context to bit reinterpretation.
Common/​src/​Interop/​Unix/​System.Security.Cryptography.Native/​Interop.Initialization.cs Annotates OpenSSL import and wraps initialization.
Common/​src/​Interop/​Unix/​System.Net.Security.Native/​Interop.NetSecurityNative.IsNtlmInstalled.cs Annotates GSS import and wraps initialization.

Review effort: Balanced

@EgorBo

EgorBo commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

PTAL @jjonescz @jkotas @agocke

This flips the switch for MemorySafetyRulesVersion to be 2 by default (for now, only via src/libraries/Directory.Build.props) and then opts out assemblies that need migration. Thus, it enrolls 690 into the unsafe-v2.

I had to opt out most ref assemblies cause I didn't want to accidentally introduce new caller-unsafe to public APIs without API review.

Next steps:

  1. Once stackalloc is sorted out Remove the unsafe v2 stackalloc/SkipLocalsInit restriction roslyn#86294 - enroll the assemblies with it
  2. Eventually, enable MemorySafetyRulesVersion=2 at the repo root Directory.Build.props

@jjonescz

Copy link
Copy Markdown
Member

It sounds like you updated to v2, then checked what is failing and fixed up those parts. But by updating to v2, you are also certifying that all public members that are not marked unsafe are "safe", does that need some auditing?

@EgorBo

EgorBo commented Nov 23, 2026 •

Copy link
Copy Markdown
Member Author

It sounds like you updated to v2, then checked what is failing and fixed up those parts. But by updating to v2, you are also certifying that all public members that are not marked unsafe are "safe", does that need some auditing?

The enrollment consists of two phases:

  1. Phase 1: The mechanical part (mostly the migration script). The script can't tell on its own whether an inner unsafe context should be propagated, so it leaves // SAFETY: TODO Audit markers on inner contexts and enrolls the code into the new rules.
  2. Phase 2: The AI/human part. Find all TODO markers, replace them with proper safety comments, and decide whether each inner unsafe context has to propagate to the outer one (caller-unsafe). This might also require an API review since it may affect the public surface.

This PR (like my previous ones) is still phase 1.

Comment thread src/libraries/Directory.Build.props Resolved
@EgorBo
EgorBo enabled auto-merge (squash) November 24, 2026 01:05
@EgorBo

EgorBo commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

/ba-g runtime-nativeaot-outerloop stuck

@EgorBo
EgorBo merged commit 9db18fa into dotnet:main Nov 24, 2026
107 of 138 checks passed
@EgorBo
EgorBo deleted the unsafe-v2-default-libraries branch November 24, 2026 01:13
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants