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

JIT: Late expansion of delegate calls can reorder null checks with other argument exceptions #75832

Closed
jakobbotsch opened this issue Sep 19, 2022 · 1 comment · Fixed by #89175
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI Priority:3 Work that is nice to have
Milestone

Comments

@jakobbotsch
Copy link
Member

jakobbotsch commented Sep 19, 2022

The JIT inlines delegate calls, which is done in lowering by inserting indirections to fetch the instance and target from the delegate object. However, the indirection that fetches the instance is inserted right after the existing 'this' argument node. This can reorder the null-check of the delegate object with exceptions thrown by other arguments.

For example, the following example throws NullReferenceException in release. It should throw DivisionByZeroException (as it does in debug).

public static void Main()
{
    Test(0);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void Test(int i)
{
    GetAction()(100 / i);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static Action<int> GetAction() => null;

category:correctness
theme:delegates
skill-level:intermediate
cost:small
impact:small

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Sep 19, 2022
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Sep 19, 2022
@ghost
Copy link

ghost commented Sep 19, 2022

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

Issue Details

The JIT inlines delegate calls, which is done in lowering by inserting indirections to fetch the instance and target from the delegate object. However, the indirection that fetches the instance is inserted right after the existing 'this' argument node. This can reorder the null-check of the delegate object with exceptions thrown by other arguments.

For example, the following example throws NullReferenceException in release. It should throw DivisionByZeroException (as it does in debug).

public static void Main()
{
    Test(0);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void Test(int i)
{
    GetAction()(100 / i);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static Action<int> GetAction() => null;
Author: jakobbotsch
Assignees: -
Labels:

area-CodeGen-coreclr, untriaged

Milestone: -

@jakobbotsch jakobbotsch removed the untriaged New issue has not been triaged by the area owner label Sep 19, 2022
@jakobbotsch jakobbotsch self-assigned this Sep 19, 2022
@jakobbotsch jakobbotsch added this to the 8.0.0 milestone Sep 19, 2022
jakobbotsch added a commit to jakobbotsch/runtime that referenced this issue Sep 19, 2022
Expand delegate calls in morph to allow CSE'ing and hoisting of delegate
targets/instances.

Fix dotnet#75832
Close dotnet#75255
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Sep 19, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Oct 20, 2022
@jakobbotsch jakobbotsch added the Priority:3 Work that is nice to have label Jun 20, 2023
jakobbotsch added a commit to jakobbotsch/runtime that referenced this issue Jul 19, 2023
The access of the target instance was incorrectly inserted right after
the location of the delegate instance. Since this indirection can throw
a NRE this is incorrect; to get the proper inlined behavior, the
indirection must happen only after all arguments have been evaluated.

Fix dotnet#75832
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 19, 2023
jakobbotsch added a commit that referenced this issue Jul 20, 2023
The access of the target instance was incorrectly inserted right after
the location of the delegate instance. Since this indirection can throw
a NRE this is incorrect; to get the proper inlined behavior, the
indirection must happen only after all arguments have been evaluated.

Fix #75832
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 20, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Aug 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI Priority:3 Work that is nice to have
Projects
None yet
1 participant