Skip to content

JIT: (bug) [ARM64] Contained BITCAST in a small (byte/short) STORE_LCL_FLD emits an oversized store and clobbers adjacent fields #134054

Closed

Description

@EgorBo

Storing a BITCAST-ed float into a byte struct field emits a full 4-byte store, overwriting the neighbouring field. Checked arm64 asserts dstSize == 8 in instr.cpp; Release arm64 and x64 silently corrupt the struct.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public struct S
{
    public byte B;
    public byte C;
    public int D;
}

public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Use(ref S s) { }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(float f)
    {
        S s = default;
        s.C = 7;
        s.B = (byte)BitConverter.SingleToInt32Bits(f * 3.0f);
        Use(ref s);
        return s.B + (s.C << 8) + (s.D << 16);
    }

    static void Main()
    {
        Console.WriteLine(Test(1.1f)); // expected 1844
    }
}

1.1f * 3.0f is 0x40533334, so s.B == 0x34, s.C == 7, s.D == 0 => 1844.

Expected

1844

Actual

Release arm64 / x64 (and self-contained 11.0.0-preview.7):

13108

13108 == 0x3334: s.B is correct but s.C was overwritten with 0x33. Checked arm64 instead asserts:

Assertion failed 'dstSize == 8' in 'Program:Test(float):int' during 'Generate code'
    File: src/coreclr/jit/instr.cpp:2647

Codegen shows str s16, [fp, #0x18] (4 bytes) into the 1-byte field s.B; on x64 the equivalent movss.

Notes

Lowering::ContainCheckStoreLoc (lowerarmarch.cpp, same shape in lowerxarch.cpp) contains any
GT_BITCAST whose source is in a register without checking that the store width is 4 or 8 bytes.
genCodeForStoreLclFld then calls ins_StoreFromSrc(fpReg, TYP_UBYTE), which only handles 4/8-byte
destinations. fgOptimizeCastOnStore drops the (byte) cast on the STORE_LCL_FLD path, leaving the
raw BITCAST as the store data. Fix direction: refuse containment (or emit an exact-width store) when
genTypeSize(store) < 4. Also reproduces on .NET 10.0.9, so not a .NET 11 regression.

Activity

  1. added this to the 12.0.0 milestone on Sep 16, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 16, 2026
  3. dotnet-policy-service commented on Sep 16, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. self-assigned this
    on Oct 12, 2026
  5. added a commit that references this issue on Oct 13, 2026
    a920216
  6. locked and limited conversation to collaborators on Nov 13, 2026
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