dotnet / dotnet/roslyn

InlineArray elements are zeroed by debugger

Open
#75,447 1 comment 0 reactions 1 assignee Claimed by @tmat View on GitHub
Area-Interactive Bug
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

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.
```cs
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)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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.