Skip to content

Span<byte> from local variable bug in .net 7.x Release #89666

@check4game

Description

@check4game

Description

When using Span to map a local variable to an array, after a certain number of iterations, changing the local variable does not change the bytes ​​in the Span

    int value = 0;

    Span<byte> span = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref value, 1));

    for (int i = 0; i < numEntries; i++)
    {
        //value = 0; bug fix for .net 7.x Release

        value = random.Next();

        file.Write(span);
    }

Reproduction Steps

using System.Runtime.InteropServices;

#if DEBUG
string config = "Deb";
#else
string config = "Rel";
#endif

string template = "{0}.{1}.dat.{2}";

test(5000, 0);
test(5000, 1);
test(5000, 2);

cmp(0, 1);
cmp(0, 2);
cmp(1, 2);

void cmp(int fIdx, int sIdx)
{
    byte[] f = File.ReadAllBytes(string.Format(template, config, Environment.Version, fIdx));
    byte[] s = File.ReadAllBytes(string.Format(template, config, Environment.Version, sIdx));

    for (int i = 0; i < f.Length; i++)
    {
        if (f[i] != s[i])
        {
            Console.WriteLine("first diff {0} ~ {1}, offset={2}, {3}/{4}", f[i], s[i], i, string.Format(template, config, Environment.Version, fIdx), string.Format(template, config, Environment.Version, sIdx));
            return;
        }
    }

    Console.WriteLine("{0}/{1} is equal", string.Format(template, config, Environment.Version, fIdx), string.Format(template, config, Environment.Version, sIdx));
}

void test(long numEntries, int idx)
{
    Random random = new Random(1);

    using var file = File.OpenWrite(string.Format(template, config, Environment.Version, idx));

    file.SetLength(0);

    int value = 0;

    Span<byte> span = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref value, 1));

    for (int i = 0; i < numEntries; i++)
    {
        //value = 0; bug fix for .net 7.x Release

        value = random.Next();

        file.Write(span);
    }
}

Expected behavior

all output files must be identical

Actual behavior

Results
 
first diff 15 ~ 222, offset=19996, Rel.7.0.9.dat.0/Rel.7.0.9.dat.1
first diff 96 ~ 241, offset=3996, Rel.7.0.9.dat.0/Rel.7.0.9.dat.2
first diff 96 ~ 241, offset=3996, Rel.7.0.9.dat.1/Rel.7.0.9.dat.2

Deb.7.0.9.dat.0/Deb.7.0.9.dat.1 is equal
Deb.7.0.9.dat.0/Deb.7.0.9.dat.2 is equal
Deb.7.0.9.dat.1/Deb.7.0.9.dat.2 is equal

Rel.6.0.20.dat.0/Rel.6.0.20.dat.1 is equal
Rel.6.0.20.dat.0/Rel.6.0.20.dat.2 is equal
Rel.6.0.20.dat.1/Rel.6.0.20.dat.2 is equal

Regression?

No response

Known Workarounds

No response

Configuration

.net 6.0.20
.net 7.0.9
Windows Server 2022

Other information

No response

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