-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Implement module cancellation and stack probing instrumentations #71896
Conversation
@dotnet/roslyn-compiler PTAL |
I might be misinterpreting this, but doesn't this mean that the application will no longer be able to cancel execution through user provided tokens (since they will be ignored)? Should the tokens be "combined" instead? Do we already have a consumer for this instrumentation? Could you provide some details if that is the case? Are we going to document this capability and how to take advantage of it somewhere? |
@tmat It looks like there are some analyzer errors |
We don't expect the user-specified code that is being executed by the host to explicitly handle cancellations in the current scenarios (executing small scripts that have access to the APIs exposed by the host), so I went with the simple implementation. We could definitely combine the tokens. Shouldn't add too much complexity. I can follow up on that. The cancellation instrumentation is very specialized API tailored to very specific scenarios (similarly to EnC APIs). |
@jjonescz for second review |
src/Compilers/CSharp/Portable/Lowering/SyntheticBoundNodeFactory.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Lowering/SyntheticBoundNodeFactory.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Lowering/Instrumentation/Instrumenter.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/MemberSignatureComparer.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/MemberSignatureComparer.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/MemberSignatureComparer.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Lowering/Instrumentation/ModuleCancellationInstrumenter.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Lowering/Instrumentation/ModuleCancellationInstrumenter.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Lowering/Instrumentation/ModuleCancellationInstrumenter.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Lowering/Instrumentation/ModuleCancellationInstrumenter.cs
Show resolved
Hide resolved
Done with review pass (commit 12) |
@tmat It looks like there are legitimate test failures |
Done with review pass (commit 13). Besides the test failure, I am still tracking the following threads: |
Fixing the test failure. This is addressed - I have added code that makes the check insensitive to whitespace. The parameter is removed. |
Done with review pass (commit 14). Still tracking #71896 (review) |
var isInstrumented = il.Contains("System.Runtime.CompilerServices.RuntimeHelpers.EnsureSufficientExecutionStack"); | ||
|
||
Assert.False(isInstrumented, | ||
$"Method '{qualifiedMethodName}' should not be trumented. Actual IL:{Environment.NewLine}{il}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (commit 15)
@jjonescz Please review the latest revision |
src/Compilers/CSharp/Test/Emit2/Emit/RuntimeProbing/ModuleCancellationTests.cs
Show resolved
Hide resolved
typeMap1: null, | ||
parametersWithCancellationToken.AsSpan(0, methodDefinition.Parameters.Length), | ||
typeMap, | ||
MemberSignatureComparer.RefKindCompareMode.ConsiderDifferences, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am wondering if any unexpected behavior changes could occur if:
- the original method was a
readonly
struct method and the replacement method was not, for example. - Or, the other way around, the original is non-readonly and the replacement is readonly.
Since it seems like we are not checking the readonly-ness of the this
parameter in these checks.
Since this PR is already merged some time ago, I will open a new issue based on this comment.
The goal of the stack probing instrumentation is to protect a host process executing user-specified C# code from accidental mistakes that cause unrecoverable stack overflow.
Similarly, the module-level cancellation instrumentation allows the host to avoid the compiled user code accidentally blocking for long time. The instrumentation:
CancellationToken
static writable field to<PrivateImplementationDetails>
. The host can set this token via Reflection before executing the compiled code.ThrowIfCancellationRequested
on the host token into each method, loop or goto.CancellationToken
as the last parameter with matching overloads that do and passes it the host token.The instrumentation may change semantics of the user specified code in rare cases, which is the trade-off the host needs to make.
Neither instrumentation provides guarantees that stack overflow will be avoided, or the code won't run indefinitely.
Supports #71268
Public API: #71961