Registry Mirror
Skip to content

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

Closed

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

Activity

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

    @MichalPetryka
    Contributor

    Does this reproduce with DOTNET_TieredCompilation=0? Could you check on .Net 8 too?

    This looks like an OSR bug, cc @AndyAyersMS

  5. AndyAyersMS commented on Jul 29, 2023

    @AndyAyersMS
    Member

    Does this reproduce with DOTNET_TieredCompilation=0? Could you check on .Net 8 too?

    This looks like an OSR bug, cc @AndyAyersMS

    Yes, it does.

    @check4game thank you for reporting this.

  6. removed
    untriaged
    on Jul 29, 2023
  7. self-assigned this
    on Jul 29, 2023
  8. added this to the 8.0.0 milestone on Jul 29, 2023
  9. AndyAyersMS commented on Jul 29, 2023

    @AndyAyersMS
    Member

    Also repros in 8.0.

  10. AndyAyersMS commented on Jul 30, 2023

    @AndyAyersMS
    Member

    @dotnet/jit-contrib feel free to poach this one if you want to gain some experience with OSR.

  11. AndyAyersMS commented on Jul 31, 2023

    @AndyAyersMS
    Member

    The bug is in this bit of code. For OSR we don't have accurate ref counts like we do for regular methods, since some of the uses may be in the Tier0 part of the method.

    // We have accurate ref counts when running late liveness so we can eliminate
    // some stores if the lhs local has a ref count of 1.
    if (isDef && compRationalIRForm && (varDsc.lvRefCnt() == 1) && !varDsc.lvPinned)
    {
    if (varDsc.lvIsStructField)
    {
    if ((lvaGetDesc(varDsc.lvParentLcl)->lvRefCnt() == 1) &&
    (lvaGetParentPromotionType(&varDsc) == PROMOTION_TYPE_DEPENDENT))
    {
    return true;
    }
    }
    else if (varTypeIsPromotable(varDsc.lvType))
    {
    if (lvaGetPromotionType(&varDsc) != PROMOTION_TYPE_INDEPENDENT)
    {
    return true;
    }
    }
    else
    {
    return true;
    }
    }

    Fix is to simply exclude OSR locals here.

  12. added a commit that references this issue on Jul 31, 2023
    03f1839
  13. ghost added
    in-pr
    on Jul 31, 2023
  14. added a commit that references this issue on Aug 1, 2023
    11126f8
  15. ghost removed
    in-pr
    on Aug 1, 2023
  16. added 2 commits that reference this issue on Aug 2, 2023
    dc72f3f
    1dc85ba
  17. added a commit that references this issue on Aug 10, 2023
    df0930c
  18. ghost locked as resolved and limited conversation to collaborators on Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions