Skip to content

Box-unbox optimization swallows InvalidCastException on different signs #71632

Closed

Description

@MichalPetryka

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

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jul 5, 2022
  2. added
    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
    on Jul 5, 2022
  3. added this to the 7.0.0 milestone on Jul 5, 2022
  4. ghost removed
    untriagedNew issue has not been triaged by the area owner
    on Jul 5, 2022
  5. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 5, 2022
  6. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 5, 2022
  7. ghost locked as resolved and limited conversation to collaborators on Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions