Skip to content

JIT: Avoid folding returned null-stored Task in async versions - #133741

Open
jakobbotsch with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-null-reference-exception
Open

jakobbotsch with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-null-reference-exception

Conversation

Copilot AI commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Runtime-async await folding mistakes a null Task reference for a zero-initialized ValueTask, completing successfully instead of throwing NullReferenceException.

  • Fix: Require the zero constant in impFoldAwaitedTopOfStack to be TYP_INT, rejecting TYP_REF nulls.
  • Regression coverage: Add null Task and Task<int> cases using the parameter-store pattern that triggers the incorrect fold.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
16 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

Co-authored-by: jakobbotsch <7887810+jakobbotsch@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix NullReferenceException not thrown for null Task JIT: Preserve NullReferenceException when runtime-async awaits a null Task Sep 11, 2026
Copilot AI requested a review from jakobbotsch September 11, 2026 20:57
@jakobbotsch

Copy link
Copy Markdown
Member

@copilot Please rephrase the code and the PR description to more specifically refer to the fact that this has to do with the folding optimization, and not generally awaiting null tasks. General null task awaits should be throwing except in the very specific case where an async version ends with t = null; return t and we fold that.

Co-authored-by: jakobbotsch <7887810+jakobbotsch@users.noreply.github.com>
@jakobbotsch jakobbotsch changed the title JIT: Preserve NullReferenceException when runtime-async awaits a null Task JIT: Avoid folding returned null-stored Task in async versions Sep 11, 2026

Copilot AI commented Sep 11, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Please rephrase the code and the PR description to more specifically refer to the fact that this has to do with the folding optimization, and not generally awaiting null tasks. General null t...

Renamed the regression to NullTaskReturnMustNotFold and clarified the helper names/comment in 1b4252e. All three return tests passed with tiering enabled and disabled.

I couldn't update the live PR body with the available tools. Could you replace its summary with:

impFoldAwaitedTopOfStack incorrectly folds the store/return pattern in an async version ending in t = null; return t as a zero-initialized ValueTask return, suppressing the required NullReferenceException. Ordinary null Task awaits already throw. Require the zero constant to be TYP_INT to exclude TYP_REF nulls from this optimization. Regression tests cover this specific pattern for both Task and Task<int>.

@jakobbotsch
jakobbotsch marked this pull request as ready for review September 14, 2026 12:05
Copilot AI lite review requested due to automatic review settings September 14, 2026 12:05
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@jakobbotsch

Copy link
Copy Markdown
Member

PTAL @EgorBo

There is a pattern match for return default(ValueTask<T>), but that match ended up also recognizing Task t = null; return t;.

@jakobbotsch
jakobbotsch requested a review from EgorBo September 14, 2026 12:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No unresolved blocking issues were identified.

Pull request overview

Fixes JIT folding of null Task references in async methods and adds regression coverage.

Changes:

  • Restricts zero-value folding to TYP_INT constants.
  • Adds null Task and Task<int> regression tests.
File summaries
File Description
src/tests/async/returns/returns.cs Adds null-task regression tests.
src/coreclr/jit/importer.cpp Prevents folding typed null references.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

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

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

JIT: (bug) runtime-async await of a null Task completes successfully instead of throwing NullReferenceException

4 participants