Skip to content

Segfault on Linux in Release configuration when wrapping Numerics types in a explicit-layout struct #128373

Closed

Description

@SamboyCoding

Description

When running the provided C# file on Linux under Release configuration, the background tiered compilation thread hits a segfault in LinearScan::processBlockStartLocations. Specifically this line: https://github.com/dotnet/runtime/blob/v10.0.4/src/coreclr/jit/lsra.cpp#L4591, while compiling the ProblematicBody method. Which seems very similar in symptoms at least to this old .NET 5 bug #49489 .

Converting the wrapper structs to a non-explicit layout and accessing the components via properties avoids the crash. Running in Debug configuration does not crash.

Reproduction Steps

https://gist.github.com/SamboyCoding/2db342852aab4fa4fd6d9a6a06f9fd0e

Run with dotnet run -c Release Program.cs

Expected behavior

The application runs successfully.

Actual behavior

The application segfaults.

Regression?

Unknown. Apparently doesn't occur on .NET 8 but I haven't confirmed this myself.

Known Workarounds

As above - making this not an explicit struct fixes the issue. As, of course, would using the relevant Numerics structs directly.

Configuration

.NET 10.0.4 (SDK 10.0.104)
Linux (EndeavourOS) Kernel 7.0.5-arch1-1
x64
Also confirmed by someone else on Arch 7.0.8-arch1-1.

Other information

No response

Activity

  1. added
    untriaged
    on May 19, 2026
  2. added
    area-CodeGen-coreclr
    on May 19, 2026
  3. dotnet-policy-service commented on May 19, 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. removed
    untriaged
    on May 19, 2026
  5. added this to the 11.0.0 milestone on May 19, 2026
  6. added a commit that references this issue on May 31, 2026
    df448be
  7. tannergooding commented on Jul 7, 2026

    @tannergooding
    Member

    @SamboyCoding, I'd note that while this is a bug and we are fixing it, your code is currently pessimizing itself "a lot" by using explicit layout structs like this.

    You should not be using public fields, you should not be overlapping fields, you should not be using non-0 based field offsets, etc.

    Rather, if you want optimal codegen here then you should simply wrap the System.Numerics type as the internal private value field and expose properties which get/set the respective field so that the various JIT optimizations can light up and give you the expected optimal codegen.

    If you cannot do that, such as due to binary compat, then the next best thing is to use Unsafe.BitCast to convert between your struct and the corresponding System.Numerics type with an identical layout, and then to use the operators on the built-in types before casting back. This allows the JIT to flag your type as being used in SIMD intrinsics and still generally optimize as expected.

    i.e. this is as close to optimal as you can get with a wrapper type:

    struct WrappedVector3
    {
        private NumericsVector3 _value;
    
        public float X
        {
            get => _value.X;
            set => _value.X = value;
        }
    
        public static WrappedVector3 operator +(WrappedVector3 left, WrappedVector3 right) => Unsafe.BitCast<NumericsVector3, WrappedVector3>(left._value + right._value);
    }

    and if you can't do that only because you already have public fields exposed, this is the next closest thing:

    struct WrappedVector3
    {
        public float X;
        public float Y;
        public float Z;
    
        public static WrappedVector3 operator +(WrappedVector3 left, WrappedVector3 right) => Unsafe.BitCast<NumericsVector3, WrappedVector3>(Unsafe.BitCast<WrappedVector3, NumericsVector3>(left) + Unsafe.BitCast<WrappedVector3, NumericsVector3>(right));
    }
  8. added a commit that references this issue on Jul 7, 2026
    03389cb
  9. SamboyCoding commented on Jul 11, 2026

    @SamboyCoding
    Author

    Yeah I've already had this changed for weeks at this point, but glad to know it's fixed now.

  10. added a commit that references this issue on Jul 15, 2026
    269ef8d
  11. locked and limited conversation to collaborators on Aug 11, 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