Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

[release/3.0] Do not use AllocateUninitializedArray in private array pools.#26366

Merged
stephentoub merged 1 commit into
dotnet:release/3.0from
stephentoub:port26338
Aug 26, 2019
Merged

[release/3.0] Do not use AllocateUninitializedArray in private array pools.#26366
stephentoub merged 1 commit into
dotnet:release/3.0from
stephentoub:port26338

Conversation

@stephentoub

@stephentoub stephentoub commented Aug 26, 2019

Copy link
Copy Markdown
Member

Port #26338 to release/3.0.
Fixes https://github.com/dotnet/corefx/issues/40485
cc: @VSadov

Description

In 3.0 we added the GC.AllocateUninitializedArray API, which allocates an array but doesn't forcibly zero-init it (unless necessary for GC safety), and then we used it in a few places, including in the ArrayPool. However, we were over-aggressive about where we used it, and in addition to using it in the shared ArrayPool, we also used it in the ArrayPool type that's returned when ArrayPool.Create is used. With the shared pool, developers can't have any expectations about the state of the arrays returned to them when renting (because some other code, including from the core framework itself, may have returned a populated array), but with ArrayPool.Create, code can create isolated pools over which it has full control, and some code depends on getting back zero-inited arrays. This change just reverts the regression (#24504), going back to using new T[...] instead of GC.AllocateUninitializedArray(...) in that configurable/isolatable pool.

Customer Impact

Non-zero'd arrays coming out of ArrayPool.Rent, which can then result in corruption for code that was expecting all elements to be zero.

Regression?

Yes, from .NET Core 2.2. This was reported by a customer while trying to upgrade to 3.0.

Risk

Very low. It's simply switching back three calls from GC.AllocateUninitializedArray(...) to the new T[...] instructions they were before.

…et#26338)

User may have extra expectations for the privatly owned array pools.
For example there could be an expectation that array never contains negative numbers (since user never puts them there). Uninitialized allocations can break such expectations.
@stephentoub stephentoub added this to the 3.0 milestone Aug 26, 2019
@sergiy-k

Copy link
Copy Markdown

/cc: @MeiChin-Tsai

@stephentoub stephentoub merged commit 6f87fd8 into dotnet:release/3.0 Aug 26, 2019
@stephentoub stephentoub deleted the port26338 branch August 26, 2019 21:25
@MeiChin-Tsai

Copy link
Copy Markdown

Stephen, do we now have test to catch this in the future? Just wondering how to prevent the same mistake in the future. Thanks.

@stephentoub

Copy link
Copy Markdown
Member Author

do we now have test to catch this in the future?

dotnet/corefx#40592

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants