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

Implement module cancellation and stack probing instrumentations #71896

Merged
merged 16 commits into from
Mar 11, 2024

Conversation

tmat
Copy link
Member

@tmat tmat commented Jan 31, 2024

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:

  • Adds CancellationToken static writable field to <PrivateImplementationDetails>. The host can set this token via Reflection before executing the compiled code.
  • Inserts calls to ThrowIfCancellationRequested on the host token into each method, loop or goto.
  • Replaces any tokens passed as arguments with the host token.
  • Replaces calls to methods that do not take 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

@tmat tmat requested review from a team as code owners January 31, 2024 19:34
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jan 31, 2024
@tmat
Copy link
Member Author

tmat commented Jan 31, 2024

@dotnet/roslyn-compiler PTAL
@dotnet/roslyn-ide FYI

@AlekseyTs
Copy link
Contributor

@tmat

Replaces any tokens passed as arguments with the host token

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?

@AlekseyTs
Copy link
Contributor

@tmat It looks like there are some analyzer errors

@tmat
Copy link
Member Author

tmat commented Feb 2, 2024

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?

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).

@jaredpar
Copy link
Member

@jjonescz for second review

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 12)

@AlekseyTs
Copy link
Contributor

@tmat It looks like there are legitimate test failures

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 13). Besides the test failure, I am still tracking the following threads:

@tmat
Copy link
Member Author

tmat commented Feb 28, 2024

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.

@AlekseyTs
Copy link
Contributor

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}");
Copy link
Contributor

Choose a reason for hiding this comment

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

trumented

Typo?

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 15)

@AlekseyTs
Copy link
Contributor

@jjonescz Please review the latest revision

@tmat tmat merged commit 8e274ea into dotnet:main Mar 11, 2024
27 of 30 checks passed
@tmat tmat deleted the Instrumentation branch March 11, 2024 20:49
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Mar 11, 2024
@RikkiGibson RikkiGibson modified the milestones: Next, 17.10 P3 Mar 25, 2024
typeMap1: null,
parametersWithCancellationToken.AsSpan(0, methodDefinition.Parameters.Length),
typeMap,
MemberSignatureComparer.RefKindCompareMode.ConsiderDifferences,
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants