Skip to content

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

Closed

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

Activity

  1. ghost added
    needs-area-label
    on Aug 1, 2023
  2. ghost added
    untriaged
    on Aug 1, 2023
  3. added
    area-CodeGen-coreclr
    and removed
    needs-area-label
    on Aug 1, 2023
  4. filipnavara commented on Aug 1, 2023

    @filipnavara
    Member

    I tried it on SDKs 7.0.306 and 8.0.100-preview.7. It didn't repro on my macOS machine in either x64 or arm64 builds.

  5. AndyAyersMS commented on Aug 1, 2023

    @AndyAyersMS
    Member

    @odyssjii was this with a debug or release build?

  6. odyssjii commented on Aug 2, 2023

    @odyssjii
    Author

    @AndyAyersMS This is with debug build.

    Here is repro in VS Code on a different machine also running macOS:

    image

    dotnet version: 7.0.304
    MSBuild version 17.6.3+07e294721 for .NET
    macOS Ventura 13.4.1

    Note that both of the machines are Apple computers with Intel CPUs.

  7. jakobbotsch commented on Aug 2, 2023

    @jakobbotsch
    Member

    Do I understand correctly that this only repros under the debugger and requires stepping in a very specific pattern?

    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.

    Do you happen to have an example that doesn't require attaching and stepping with a debugger? It sounds likely these could be two separate issues.

  8. added this to the 8.0.0 milestone on Aug 2, 2023
  9. ghost removed
    untriaged
    on Aug 2, 2023
  10. odyssjii commented on Aug 3, 2023

    @odyssjii
    Author

    Hi @jakobbotsch ! I will try to construct a repro that does not require the stepping in with a debugger. It is not as straightforward though, I still don't know under what condition that happens.

  11. jakobbotsch commented on Aug 3, 2023

    @jakobbotsch
    Member

    Thanks @odyssjii! If you are able to reproduce it without a debugger it would definitely indicate some sort of codegen bug. Sadly I don't have an Intel Mac and it seems like VSCode/OmniSharp does not support debugging with an SDK running under Rosetta, so I cannot try it while stepping.

    @dotnet/jit-contrib Anyone with an Intel Mac who wants to see if they are able to repro it? I've looked at the x64 debug codegen on my Mac and I do not see anything suspicious. I've also tried reproducing it when stepping on Ubuntu x64 (which should be the exact same codegen since it's the same ABI), but haven't had luck.

  12. removed this from the 8.0.0 milestone on Aug 7, 2023
  13. 17 remaining items

  14. jakobbotsch commented on Aug 9, 2023

    @jakobbotsch
    Member

    I'm going to move this to 9.0 and mark it as needing author action since there is nothing actionable from the codegen side at this point. Will happily take another look once a repro case that doesn't involve the debugger/stepping is provided.

  15. modified the milestones: 8.0.0, 9.0.0 on Aug 9, 2023
  16. added
    needs-author-action
    on Aug 9, 2023
  17. mikem8361 commented on Aug 10, 2023

    @mikem8361
    Contributor

    It doesn't repro if COMPlus_EnableAVX=0. If does also repro on .NET 8 even with the AVX PR ##89705 changes.

  18. removed
    needs-author-action
    on Aug 10, 2023
  19. jakobbotsch commented on Aug 10, 2023

    @jakobbotsch
    Member

    Ok, so the summary is that the repro case posted is a duplicate of #78991. We still need to determine whether there is a codegen issue, but we need a repro case that doesn't involve stepping or more details to do that.

  20. added
    needs-author-action
    on Aug 10, 2023
  21. ghost closed this as completedon Sep 8, 2023
  22. ghost locked as resolved and limited conversation to collaborators on Oct 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrneeds-author-actionno-recent-activity

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions