Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/5.0-rc2] AccessViolation when using ValueTypes as a Service #42213

Merged
merged 2 commits into from
Sep 16, 2020

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented Sep 14, 2020

Backport of #42152 to release/5.0-rc2

/cc @eerhardt

Customer Impact

When a consumer of DependencyInjection is using a struct type as a service, we are generating incorrect IL. We can get scenarios of double unboxing a struct, or using a value type as an object reference. Both of which can cause memory corruption and Access Violations.

This was a regression introduced early in .NET 5 which was fixing a different IL generation problem. Both the original issue and the regression are fixed by this change.

Testing

I added new regression tests that cover the affected scenario. Also ran the repro project reported by the customer, and the issue is fixed.

@ericstj and I statically analyzed the cases that could run into this issue and found another place - when using a collection of value types. Added a test case for this as well.

Risk

Since anything to do with IL generation is risky, I would say this is "medium" risk. The applied fix only affects value types, so typical usage of DI shouldn't be affected. All current and new tests pass with this change.

When using ValueTypes as services in DependencyInjection, we are generating incorrect IL in a few cases:

- When the ValueType is a top level service
- When the ValueType is in an IEnumerable of services

We were double unboxing the ValueType, which was causing AccessViolations.

This was a regression caused by a previous change when ValueTypes were used in constructor parameters of services.

To fix this, I lifted the unboxing into the VisitConstructor and VisitIEnumerable methods instead of forcing VisitConstant to always do the unboxing. VisitConstant doesn't have enough context to know whether it should do the unboxing or not.

Fix #42037
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@eerhardt eerhardt self-assigned this Sep 14, 2020
@eerhardt eerhardt added area-Extensions-DependencyInjection Servicing-consider Issue for next servicing release review labels Sep 14, 2020
@ghost
Copy link

ghost commented Sep 14, 2020

Tagging subscribers to this area: @eerhardt, @maryamariyan
See info in area-owners.md if you want to be subscribed.

@danmoseley
Copy link
Member

test failure is #42225

@leecow leecow added Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review labels Sep 15, 2020
@leecow leecow added this to the 5.0.0 milestone Sep 15, 2020
@danmoseley
Copy link
Member

#42037

@danmoseley
Copy link
Member

@eerhardt can we merge and close #42037?

@eerhardt eerhardt merged commit f29844e into release/5.0-rc2 Sep 16, 2020
@eerhardt eerhardt deleted the backport/pr-42152-to-release/5.0-rc2 branch September 16, 2020 17:19
@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2020
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