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

Box-unbox optimization swallows InvalidCastException on different signs #71632

Closed
MichalPetryka opened this issue Jul 5, 2022 · 2 comments · Fixed by #71633
Closed

Box-unbox optimization swallows InvalidCastException on different signs #71632

MichalPetryka opened this issue Jul 5, 2022 · 2 comments · Fixed by #71633
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Milestone

Comments

@MichalPetryka
Copy link
Contributor

Description

Values with different signs are casted with no exception while roundtripping through object.

Reproduction Steps

[MethodImpl(MethodImplOptions.NoInlining)]
private static sbyte Problem(byte a)
{
    object box = a;
    Use(ref box);
    return (sbyte)box;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static sbyte Problem2(byte a)
{
    object box = a;
    return (sbyte)box;
}

public static void Use<T>(ref T arg) { }

Expected behavior

Both methods throw InvalidCastException.

Actual behavior

Problem2 doesn't throw it.

Regression?

Yes, introduced probably in Box-unbox enum optimization introduced in #70167.

Known Workarounds

No response

Configuration

.Net 7 builds off main branch, Preview 5 doesn't reproduce it.

Other information

It's probably caused by getTypeForPrimitiveValueClass treating types of the same size as the same here:
https://github.com/dotnet/runtime/blob/main/src/coreclr/vm/jitinterface.cpp#L3974

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 5, 2022
@SingleAccretion SingleAccretion added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jul 5, 2022
@SingleAccretion SingleAccretion added this to the 7.0.0 milestone Jul 5, 2022
@ghost
Copy link

ghost commented Jul 5, 2022

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

Issue Details

Description

Values with different signs are casted with no exception while roundtripping through object.

Reproduction Steps

[MethodImpl(MethodImplOptions.NoInlining)]
private static sbyte Problem(byte a)
{
    object box = a;
    Use(ref box);
    return (sbyte)box;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static sbyte Problem2(byte a)
{
    object box = a;
    return (sbyte)box;
}

public static void Use<T>(ref T arg) { }

Expected behavior

Both methods throw InvalidCastException.

Actual behavior

Problem2 doesn't throw it.

Regression?

Yes, introduced probably in Box-unbox enum optimization introduced in #70167.

Known Workarounds

No response

Configuration

.Net 7 builds off main branch, Preview 5 doesn't reproduce it.

Other information

It's probably caused by getTypeForPrimitiveValueClass treating types of the same size as the same here:
https://github.com/dotnet/runtime/blob/main/src/coreclr/vm/jitinterface.cpp#L3974

Author: MichalPetryka
Assignees: -
Labels:

area-CodeGen-coreclr, untriaged

Milestone: -

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jul 5, 2022
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 5, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 5, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Aug 5, 2022
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants