Skip to content

Plain struct not copied correctly when passed to method under certain conditions #89774

Closed
@odyssjii

Description

@odyssjii

Description

When passing a struct to a method on a class, if there is a second parameter of interface type and an instance of a struct implementing that interface is passed in as the second parameter, then if stepping into the record constructor of the struct and then stepping into the method using a debugger via VS Code or Visual Studio for Mac two of the fields of the first parameter are set to null.

This change in the value is "real" and not a visual bug in the debugger, attempting to access or read that value will confirm this.

Note that the bug was detected whilst running code without a debugger, when inexplicable null values were detected deeper in the call stack. So under certain conditions it happens even without stepping in the debugger, but this is a reliable repro.

Screenshot 2023-08-01 at 14 18 33 Screenshot 2023-08-01 at 14 19 08

Reproduction Steps

To produce the bug: place breakpoint on the first line, step into the constructor of the record, then step into the method Z::Foo, inspect the method argument obj and X::D and X::E have been set to null.

The second line which passes new B() instead of new A() does not have this issue.

Also placing breakpoint within Z::Foo and running directly to that point (do not step into record constructor) will NOT produce the bug.

Not related to using record struct, same problem with a regular struct.

new Z().Foo(new X(8, "hello", "world", "cool", "stuff", "baby"), new A());

new Z().Foo(new X(8, "hello", "world", "cool", "stuff", "baby"), new B());

sealed class Z
{
    public void Foo(X obj, IA service)
    {
        Console.WriteLine(string.Join(" ", obj.A, obj.B, obj.C, obj.D, obj.E, obj.F));
    }
}

public readonly record struct X(uint A, string B, string C, string D, string E, string F);

interface IA
{
    bool Bar();
}

readonly struct A : IA
{
    public readonly bool Bar() => true;
}

class B : IA
{
    public bool Bar() => true;
}

Expected behavior

When an instance of a plain struct is passed to a method, it is expected that the copy of the struct should arrive whole and intact -- i.e. some fields should not suddenly go missing or be altered.

Actual behavior

Under certain conditions, when passing an instance of a struct to method, some of the fields of the struct are missing in the copy of the instance accessible from within the method.

Regression?

No response

Known Workarounds

No response

Configuration

.net version: 7.0.304
OS: macOS Ventura 13.4.1
Arch: x64

VS Code and Visual Studio for Mac both exhibit the same behaviour.

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIneeds-author-actionAn issue or pull request that requires more info or actions from the author.no-recent-activity

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions