Skip to content

Enroll System.IO.Compression and its tests in unsafe-v2 - #136657

Merged
EgorBo merged 11 commits into
dotnet:mainfrom
EgorBo:unsafe-v2-system-io-compression
Nov 20, 2026
Merged

EgorBo merged 11 commits into
dotnet:mainfrom
EgorBo:unsafe-v2-system-io-compression

Conversation

@EgorBo

@EgorBo EgorBo commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

I chose System.IO.Compression (and its tests) as the first target because it's relatively small and interop-heavy.

This PR covers only the mechanical migration: there is no AI-driven work to fill in safety comments or decide which methods might need a caller-unsafe contract. This lets the review focus entirely on the code fixer output.

References

A prompt for reading all the relevant documentation and running the driver script:

Read https://gist.githubusercontent.com/EgorBo/271abeeac2bfb5a34013e4562507062e/raw/unsafe-v2-migration-agent-prompt.md in full
and follow it for **System.IO.Compression** and its tests. Run the driver once per project.

(I also tell it where to find a prebuilt dotnet/runtime in WSL so it can handle Linux targets there.)

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 5d9d8def-16fe-4105-9e0f-b8aad187abce
Unmodified output of the migration driver across net12.0-windows, net12.0-unix, net12.0-browser, net12.0-wasi, and net12.0.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 5d9d8def-16fe-4105-9e0f-b8aad187abce
Co-authored-by: Copilot <[email protected]>

Copilot-Session: 5d9d8def-16fe-4105-9e0f-b8aad187abce
Unmodified output of the fixed migration driver across net12.0-windows, net12.0-unix, and net12.0-browser.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 5d9d8def-16fe-4105-9e0f-b8aad187abce
Enable the two affected consumer test projects to compile the migrated shared helper. Neither project is enrolled in unsafe-v2.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 5d9d8def-16fe-4105-9e0f-b8aad187abce
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
12 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: @karelz, @dotnet/area-system-io-compression
See info in area-owners.md if you want to be subscribed.

Try all valid block candidates before unsafe expressions, including expression-body conversions. Keep expressions as a fallback and preserve the explicit expression-first action.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 5d9d8def-16fe-4105-9e0f-b8aad187abce
Retain unsafe-v2 enrollment and approved consumer settings. Restore only the previous automatic migration output before regenerating contexts with the updated fixer.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 5d9d8def-16fe-4105-9e0f-b8aad187abce
Untouched output of a full driver replay from pre-migration sources across all five library TFMs. Control-flow headers and convertible expression bodies now prefer unsafe blocks.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 5d9d8def-16fe-4105-9e0f-b8aad187abce
Untouched output of the fixed migration driver across all three test TFMs.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 5d9d8def-16fe-4105-9e0f-b8aad187abce
@EgorBo
EgorBo requested a review from sbomer as a code owner October 29, 2026 21:20
@dotnet-policy-service dotnet-policy-service Bot added linkable-framework Issues associated with delivering a linker friendly framework labels Oct 29, 2026
@EgorBo

EgorBo commented Oct 29, 2026 •

Copy link
Copy Markdown
Member Author

PTAL @agocke @jkotas @jjonescz. It contains a small fix (and tests) for UnsafeContext injector (to prioritize statements over expressions).

Tracking issue: #136664
Here is the explanation why this PR doesn't do the full migration and only the mechanical: #136657 (comment)

I don't expect a PR per assembly, I think many of them could be batched. (say up to 2000 LOC of changes or more).
PS: I recommend disabling whitespaces.

@EgorBo

EgorBo commented Oct 30, 2026 •

Copy link
Copy Markdown
Member Author

NOTE: there are no functional changes here, it's really about agreeing on the format.

@rzikm

rzikm commented Nov 2, 2026

Copy link
Copy Markdown
Member

@EgorBo are we as IO.Compression owners expected to review this? If so, what should we be looking for?

@EgorBo

EgorBo commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

@EgorBo are we as IO.Compression owners expected to review this? If so, what should we be looking for?

I'm not sure, @jkotas requested the review. I assume for correctness issues?

@jkotas

jkotas commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

I'm not sure, @jkotas requested the review. I assume for correctness issues?

I have requested the review for awareness - to make sure that the area owners are comfortable with assuming the ownership of these annotations.

@rzikm

rzikm commented Nov 2, 2026

Copy link
Copy Markdown
Member

I see, and I assume once this is merged, it is up to us to fill in those security annotations? Is there some document/playbook summarizing the approach? I may have gaps in how unsafe-v2 is supposed to work so I could use some examples to work from.

@EgorBo

EgorBo commented Nov 2, 2026 •

Copy link
Copy Markdown
Member Author

I see, and I assume once this is merged, it is up to us to fill in those security annotations? Is there some document/playbook summarizing the approach? I may have gaps in how unsafe-v2 is supposed to work so I could use some examples to work from.

unsafe-v2 spec: https://github.com/dotnet/csharplang/blob/main/proposals/unsafe-evolution.md

The clean up work can be done by code owners or we'll just send an AI PR that you can review. The work is:

  1. Inspect every inner unsafe{} (simple grep over // SAFETY: To be audited) whether the containing function has to be marked as caller-unsafe or not (propagation of unsafe).
  2. Replace the To be audited with a proper explanation why it's safe to call an unsafe-API/unsafe operation here.
  3. Replace unsafe with safe where explicit unsafe/safe is required, currently it is all extern/LibraryImport and fields in ExplicitLayout/ExtendedLayout (or/and Pack=1). E.g.
        // Compression level functions
        [LibraryImport(Libraries.CompressionNative)]
        internal static unsafe partial int ZSTD_minCLevel();

looks like a perfect candidate for unsafe to be replaced with safe here. In that case, a /// <safety>... comment must be added to explain why it's safe.


Optional: replace unsafe{} with a code that doesn't require it.

@rzikm rzikm 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.

LGTM from IO.Compression perspective, I will raise the follow-up PR with the required annotations.

@rzikm

rzikm commented Nov 12, 2026

Copy link
Copy Markdown
Member

@EgorBo Are we waiting for review from others or can I merge this?

richlander added a commit to richlander/dotnet-inspect that referenced this pull request Nov 16, 2026
A member's safety contract is its RequiresUnsafe marker plus its <safety>
prose. dotnet/runtime has begun enrolling implementation projects in the
updated memory-safety rules (dotnet/runtime#136657), so later runtime packs
may mark their own members, including non-public ones the reference
assembly omits. The reference surface stays authoritative for members it
declares; the implementation supplies the contract only for members the
surface omits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
richlander added a commit to richlander/dotnet-inspect that referenced this pull request Nov 16, 2026
* State the Platform surface and implementation roles

A Platform Library gets the same two roles as a package Library: the
reference-pack assembly is its surface (Type list, declarations, counts,
Opportunities, explicit memory-safety contracts, row navigation) and the
same-named runtime-pack assembly is its implementation (bodies, Metadata,
Integrations, Performance, Unsafe body evidence, call graphs). Ref-less
assemblies keep the shared-role rule, ref-only assemblies report
implementation views unavailable, facades keep their reference surface,
and forwarded-Type activation stays on runtime-pack evidence.

Memory-safety contracts make the split load-bearing: .NET 11 runtime-pack
assemblies carry no RequiresUnsafe or MemorySafetyRules markers; only the
reference pack does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Assign safety contracts to the declaring Platform role

A member's safety contract is its RequiresUnsafe marker plus its <safety>
prose. dotnet/runtime has begun enrolling implementation projects in the
updated memory-safety rules (dotnet/runtime#136657), so later runtime packs
may mark their own members, including non-public ones the reference
assembly omits. The reference surface stays authoritative for members it
declares; the implementation supplies the contract only for members the
surface omits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
EgorBo and others added 2 commits November 17, 2026 00:01
…ompression

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 9190ec1a-b880-4650-942f-d7f17a4d06f8
Taking the address of an unassigned out parameter now requires an unsafe context.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 9190ec1a-b880-4650-942f-d7f17a4d06f8
@EgorBo

EgorBo commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

@rzikm I just got back from vacation. We plan to stop requiring unsafe for stackalloc (via dotnet/roslyn#86294), but this PR has only a few of them, so it's probably fine to merge as is (and then remove the redundant unsafe{} once we update Roslyn). Can you re-approve it?

@EgorBo

EgorBo commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

ping @rzikm

@EgorBo
EgorBo enabled auto-merge (squash) November 20, 2026 21:42
@EgorBo

EgorBo commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

/ba-g merge stuck

@EgorBo
EgorBo merged commit 1078913 into dotnet:main Nov 20, 2026
85 of 87 checks passed
@EgorBo
EgorBo deleted the unsafe-v2-system-io-compression branch November 20, 2026 21:45
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.IO.Compression linkable-framework Issues associated with delivering a linker friendly framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants