Skip to content

Add a diagnostic mode for ArrayPool.Shared - #109695

Closed
stephentoub wants to merge 1 commit into
dotnet:mainfrom
stephentoub:diagnosticap
Closed

stephentoub wants to merge 1 commit into
dotnet:mainfrom
stephentoub:diagnosticap

Conversation

@stephentoub

Copy link
Copy Markdown
Member

@jkotas, @GrabYourPitchforks, @EgorBo, opinions on whether this is worthwhile, and/or better approaches?

@ghost ghost added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Nov 11, 2024
@stephentoub stephentoub added area-System.Runtime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Nov 11, 2024
// Store the shared ArrayPool in a field of its derived sealed type so the Jit can "see" the exact type
// when the Shared property is inlined which will allow it to devirtualize calls made on it.
private static readonly SharedArrayPool<T> s_shared = new SharedArrayPool<T>();
private static readonly ArrayPool<T> s_shared = SharedArrayPoolStatics.s_diagnosticPoolEnabled ?

@stephentoub stephentoub Nov 11, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The diagnostic pool in this PR is comparatively super expensive, but we could possibly have this turned on automatically when in debug / optimizations disabled, at least when running on coreclr.

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.

we could possibly have this turned on automatically when in debug / optimizations disabled

It is something to validate together with VS debugger. Similar super expensive debug-only aids did not fly well in the past. They degraded debugging experience too much.

@EgorBo

EgorBo commented Nov 11, 2024

Copy link
Copy Markdown
Member

Interesting idea! I presume it might miss cases when such arrays become immortal roots? (perhaps, a timer walking returned arrays in s_returnedArrays could help? 🙂)

@stephentoub

Copy link
Copy Markdown
Member Author

I presume it might miss cases when such arrays become immortal roots?

There are two things being checked currently:

  • is the same array returned multiple times erroneously
  • is the array written after it's returned (with some handwaving)

The first would work regardless of whether the array is rooted forever.

The second wouldn't work if the array is rooted forever as it's based on finalization and will only kick in after all references are dropped.

perhaps, a timer walking returned arrays in s_returnedArrays could help?

That's possible. Not sure it's worth it, but if others think the PR in general has merit, we could extend it in various ways.

@jkotas

jkotas commented Nov 11, 2024

Copy link
Copy Markdown
Member

Would this find ArrayPool related bugs that got checked in into this repo?

@GrabYourPitchforks

Copy link
Copy Markdown
Member

@stephentoub The implementation you have pretty much matches what was contemplated in one of the earlier security documents, with one exception:

The security doc also contemplated something like what @EgorBo said, where the array pool always returns more bytes than specified, to help weed out the cases where people were relying on some implementation detail of it. Essentially: keep the power-of-two growth logic as-is, but put a length++; or similar at the start of the method. This retains the amortized growth complexity other APIs might be relying on, but it also ensures that Rent never returns an array of the exact size requested by the caller.

@vcsjones

Copy link
Copy Markdown
Member

where the ArrayPool returns more bytes than specified

+1. This would have caught #71696

@stephentoub

Copy link
Copy Markdown
Member Author

Closing for now until we have time to gather sufficient evidence this would help.

@github-actions github-actions Bot locked and limited conversation to collaborators Feb 23, 2025
@stephentoub
stephentoub deleted the diagnosticap branch December 12, 2025 23:00
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants