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

InlineArray elements are zeroed by debugger #75447

Open
SinnersSum opened this issue Oct 9, 2024 · 0 comments
Open

InlineArray elements are zeroed by debugger #75447

SinnersSum opened this issue Oct 9, 2024 · 0 comments
Labels
Area-Interactive untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@SinnersSum
Copy link

SinnersSum commented Oct 9, 2024

dotnet --version 8.0.403

Summary

Attempting to read the elements of an InlineArray decorated type through the dotnet debugger produces incorrect results. The value read by the debugger will appear to have it's latter elements zeroed- as though it had been incompletely copied.

Reproduction

The issue can be reproduced by creating an instance of an InlineArray decorated type, then inspecting it's value with the debugger. This simple test program reproduces the issue. The type ILATest is a minimal InlineArray type —representing perhaps a vector. Our Main function simply constructs an ILATest and prints it’s contents.

using System;
using System.Runtime.CompilerServices;

public class Program {
    [InlineArray(4)]
    private struct ILATest
    {
        private int v;


        public readonly int X => this[0];
        public readonly int Y => this[1];
        public readonly int Z => this[2];
        public readonly int W => this[3];

        public void Deconstruct(out int x, out int y, out int z, out int w) => (x, y, z, w) = (X, Y, Z, W);

        public ILATest(int x, int y, int z, int w) {
            this[0] = x;
            this[1] = y;
            this[2] = z;
            this[3] = w;
        }

        public readonly override string ToString() {
            return $"({this[0]} {this[1]} {this[2]} {this[3]})";
        }
    }

    public static void Main() {
        ILATest v = new(3, 2, 1, 0);

        var (x, y, z, w) = v;

        Console.WriteLine(((ReadOnlySpan<int>)v).SequenceEqual([3, 2, 1, 0]));

        Console.WriteLine(v);
        // Place a breakpoint here, and inspect the value of v
    }
}

Result

True
(3 2 1 0)

The program produces correct output, but any attached debugger will display the value of v incorrectly and inconsistently. In most functions, the latter elements (all but the first) will be displayed as zeroed, while direct attempts to access the variable's fields may sometimes display them correctly.

Visual studio immediate output

Attempting to evaluate the object in the debugger will display an incorrect value. v should display as (3 2 1 0) —matching console output— but instead the latter elements are displayed as zeros. In addition, we can see that the properties as read by the debugger appear to have incorrect values.

> v
< {(3 0 0 0)}
    W: 0x00000000
    X: 0x00000003
    Y: 0x00000000
    Z: 0x00000000
    v: 0x00000003

Despite this, directly accessing the properties and implied InlineArray indexer produces correct results. Worse than simply incorrect, this behavior is inconsistent.

> v[0]
< 0x00000003
> v[1]
< 0x00000002
> v[2]
< 0x00000001
> v[3]
< 0x00000000
> v.X
< 0x00000003
> v.Y
< 0x00000002

In addition I have tested this test program in Visual Studio Code, which produced the exact same (incorrect) behavior, confirming that the problem is upstream of the GUI/IDE —e.g. the debugger.

Conclusions

The debugger cannot consistently read the elements of InlineArray types, frequently retrieving zeroes instead of the actual values. The fact that it is consistently the latter fields which get 'zeroed' suggests that the debugger is somehow executing an incomplete copy of the structure, or something like it.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Interactive untriaged Issues and PRs which have not yet been triaged by a lead labels Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Interactive untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

1 participant