Skip to content

Use System.Numerics.IEqualityOperators.op_Equality in SpanHelper.T.cs where possible. #74567

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

Merged
merged 2 commits into from
Aug 29, 2022

Conversation

lateralusX
Copy link
Member

@lateralusX lateralusX commented Aug 25, 2022

Workaround crash hit by #74179 making sure we avoid hitting codepath emitting this null pointer checks. The full fix includes codegen fixes as well, but will be performed in separate PR. There are still locations in SpanHelper.T.cs that uses Equal virtual call on value types that could be managed pointers to value types, but that code has remained the same for the last 4 years to 15 months and have not hit this issue in the past.

Fixes #74179.

Full fix for underlying codegen issue, #74638.

Workaround crash hit by dotnet#74179
making sure we avoid hitting codepath emitting this null pointer checks.
The full fix includes codegen fixes as well, but will be performed
in separate PR. There are still locations in SpanHelper.T.cs that uses
Equal virtual call on value types that could be managed pointers to
value types, but that code has remained the same for the last
4 years to 15 months and have not hit this issue in the past.
@ghost
Copy link

ghost commented Aug 25, 2022

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

Issue Details

Workaround crash hit by #74179 making sure we avoid hitting codepath emitting this null pointer checks. The full fix includes codegen fixes as well, but will be performed in separate PR. There are still locations in SpanHelper.T.cs that uses Equal virtual call on value types that could be managed pointers to value types, but that code has remained the same for the last 4 years to 15 months and have not hit this issue in the past.

Fixes #74179.

Author: lateralusX
Assignees: -
Labels:

area-System.Globalization

Milestone: -

@lateralusX lateralusX added the runtime-mono specific to the Mono runtime label Aug 25, 2022
Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

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

LGTM, thank you very much for investigating this complex issue @lateralusX !

@ghost
Copy link

ghost commented Aug 25, 2022

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

Issue Details

Workaround crash hit by #74179 making sure we avoid hitting codepath emitting this null pointer checks. The full fix includes codegen fixes as well, but will be performed in separate PR. There are still locations in SpanHelper.T.cs that uses Equal virtual call on value types that could be managed pointers to value types, but that code has remained the same for the last 4 years to 15 months and have not hit this issue in the past.

Fixes #74179.

Author: lateralusX
Assignees: lateralusX
Labels:

area-System.Memory, runtime-mono

Milestone: -

@lateralusX
Copy link
Member Author

/azp run runtime-extra-platforms

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@lambdageek
Copy link
Member

Equal virtual call on value types that could be managed pointers to value types

@lateralusX just to make sure I understand: Mono does emit correct code for these calls, right? except that we use a 32-bit read for the null check? So once we change the null check to stay in-bounds there are no other codegen issues?

the workaround here is actually some kind of micro-optimization to avoid boxing that also happens to avoid the null-check?

@lateralusX
Copy link
Member Author

lateralusX commented Aug 26, 2022

@lambdageek, since this specific code has a constraint on where TValue : struct, INumber<TValue> its possible to use == that in turn will use System.Numerics.IEqualityOperators`3<!!TValue,!!TValue,bool>::op_Equality for the compare, so it won't trigger a virtual call, meaning it won't hit our code path that injects an OP_CHECK_THIS that in turn would have done a read that could have read pass end of buffer into unallocated memory or a page with no read access.

This will mitigate the issue introduced by changes to SpanHelpers.T.cs, but could of course happen in other areas, so the full codegen fix is needed to fully mitigate the issue, but due to potential risk, it was split into two PR's, meaning we could just backport this to 7.0 and would be back to the state we had before recent optimizations to SpanHelpers.T.cs. or we could backport both and then this fix will mainly just be eliminating the virtual call that gets devirtualized and inlined, so mainly just eliminate the addition OP_CHECK_THIS.

So our virtual calls are fine, just that our lowering of OP_CHECK_THIS reads to much memory, that is fixed by #74638.

@lateralusX
Copy link
Member Author

Failures in runtime-extra-platforms unrelated.

@lateralusX
Copy link
Member Author

/backport to release/7.0

@github-actions
Copy link
Contributor

Started backporting to release/7.0: https://github.com/dotnet/runtime/actions/runs/2946871738

@lateralusX lateralusX merged commit edb7d7e into dotnet:main Aug 29, 2022
@karelz karelz added this to the 8.0.0 milestone Aug 29, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Sep 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Memory runtime-mono specific to the Mono runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CI failure: System.Globalization.Tests.InvariantModeTests.TestLastIndexOf, object reference not set to an instance of an object
6 participants