Skip to content

riscv64: 64-bit constant in method data truncated to 4 bytes (ulong division by constant gives wrong results) #136034

Closed

Description

@glomdom

Description

On riscv64, RyuJIT lowers ulong / 10 to a magic-number multiply (mulhu + srli 3) and loads the 64-bit magic 0xCCCCCCCCCCCCCCCD from the method's read-only data with ld. Only the low 4 bytes of the constant are emitted (CD CC CC CC), so ld reads 0x00000000CCCCCCCD plus whatever follows, resulting in a wrong division result.

I found this while compiling a bare-metal project with NativeAOT for riscv64.

Reproduction Steps

repro.cs
namespace System {
    public class Object {
        private IntPtr _methodTable;
    }

    public struct Void { }
    public struct Boolean { }
    public struct Char { }
    public struct SByte { }
    public struct Byte { }
    public struct Int16 { }
    public struct UInt16 { }
    public struct Int32 { }
    public struct UInt32 { }
    public struct Int64 { }
    public struct UInt64 { }
    public struct IntPtr { }
    public struct UIntPtr { }
    public struct Single { }
    public struct Double { }

    public abstract class ValueType { }
    public abstract class Enum : ValueType { }

    public sealed class String {
        private readonly int _length;
        private char _firstChar;
    }

    public abstract class Array { }
    public abstract class Delegate { }
    public abstract class MulticastDelegate : Delegate { }

    public struct RuntimeTypeHandle { }
    public struct RuntimeMethodHandle { }
    public struct RuntimeFieldHandle { }

    public enum AttributeTargets { }
    public class Attribute { }

    public sealed class AttributeUsageAttribute : Attribute {
        public AttributeUsageAttribute(AttributeTargets validOn) { }

        public bool AllowMultiple { get; set; }
        public bool Inherited { get; set; }
    }

    public static class Buffer {
        internal static void BulkMoveWithWriteBarrier(ref byte destination, ref byte source, nuint byteCount) {}
    }

    internal static class SpanHelpers {
        public static void ClearWithoutReferences(ref byte dest, nuint len) {}

        internal static void Memmove(ref byte dest, ref byte src, nuint len) {}
        internal static void Fill(ref byte dest, byte value, nuint len) {}
    }
}

namespace System.Runtime {
    internal sealed class RuntimeExportAttribute : Attribute {
        public RuntimeExportAttribute(string entry) { }
    }

    internal unsafe struct MethodTable {}

    internal static class TypeCast {
        public static unsafe object CheckCastAny(MethodTable* pTargetType, object obj) {
            return obj;
        }

        public static unsafe object CheckCastInterface(MethodTable* pTargetType, object obj) {
            return obj;
        }

        public static unsafe object CheckCastClass(MethodTable* pTargetType, object obj) {
            return obj;
        }

        public static unsafe object CheckCastClassSpecial(MethodTable* pTargetType, object obj) {
            return obj;
        }

        public static unsafe object? IsInstanceOfAny(MethodTable* pTargetType, object? obj) {
            return obj;
        }

        public static unsafe object? IsInstanceOfInterface(MethodTable* pTargetType, object? obj) {
            return obj;
        }

        public static unsafe object? IsInstanceOfClass(MethodTable* pTargetType, object? obj) {
            return obj;
        }

        public static unsafe object? IsInstanceOfException(MethodTable* pTargetType, object? obj) {
            return obj;
        }
    }
}

namespace Internal.Runtime.CompilerHelpers {
    internal static class StartupCodeHelpers { }
}

internal static class Repro {
    [System.Runtime.RuntimeExport("div10")]
    private static ulong Div10(ulong x) {
        return x / 10;
    }

    [System.Runtime.RuntimeExport("after")]
    private static ulong After(ulong x) {
        return x + 1;
    }
}

Build:

csc /nologo /noconfig /nostdlib /unsafe /target:library /runtimemetadataversion:v4.0.30319 /out:repro.dll repro.cs
ilc repro.dll -o repro.o --systemmodule repro --nativelib --targetos linux --targetarch riscv64 -O

Dump:

llvm-objdump -d --mattr=+m,+c --no-show-raw-insn repro.o
llvm-objdump -s -j __managedcode repro.o

Expected behavior

The 8-byte constant at the ld target contains CD CC CC CC CC CC CC CC (0xCCCCCCCCCCCCCCCD), and Div10(1234) returns 123.

Actual behavior

Div10 loads from offset 0x30:

0000000000000000 <repro_Repro__Div10>:
       0:       addi    sp, sp, -0x10
       4:       sd      s0, 0x0(sp)
       8:       sd      ra, 0x8(sp)
       c:       mv      s0, sp
      10:       auipc   a1, 0x0
      14:       ld      a1, 0x20(a1)
      18:       mulhu   a0, a0, a1
      1c:       srli    a0, a0, 0x3
      20:       ld      ra, 0x8(sp)
      24:       ld      s0, 0x0(sp)
      28:       addi    sp, sp, 0x10
      2c:       ret
      30:       beqz    s1, 0xea <__managed__Startup+0x7a>
      32:       sw      a1, 0x1c(s1)
                ...

note: 0x30 to 0x33 is the constant data, misdecoded by objdump as instructions: c.beqz = 0xCCCD, c.sw = 0xCCCC. The ld reads from auipc@0x10 (0x10) + 0x20 = 0x30

The data at 0x30 has only 4 bytes of the constant:

 0030 cdcccccc 00000000 00000000 00000000  ................

ld reads 0x00000000CCCCCCCD, so Div10(1234) returns 0.

Regression?

Unknown, first time testing this target.

Known Workarounds

Using a non-constant divisor (e.g. returned from a NoInlining method) produces plain divu/remu and avoids the issue.

Configuration

Release dotnet/runtime built from commit ec0059c, subsets clr.aot+clr.alljitscommunity
Host: Windows 11 26H2 x64
riscv64 target only; the same IL compiled for loongarch64 is correct.

Other information

The same repro.dll compiled with --targetarch loongarch64 creates the constant from immediates and is correct.
riscv64 instead loads it from method data with ld, and only 4 bytes of it are emitted, so the bug appears to be in how the riscv64 emitter writes 64-bit constants to the data section.

Activity

  1. added
    area-CodeGen-coreclr
    on Oct 18, 2026
  2. added
    untriaged
    on Oct 18, 2026
  3. dotnet-policy-service commented on Oct 18, 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. added
    arch-riscv
    on Oct 18, 2026
  5. added 2 commits that reference this issue on Oct 22, 2026
    2246746
    30736da
  6. added a commit that references this issue on Oct 22, 2026
    73cc9a3
  7. removed
    untriaged
    on Oct 22, 2026
  8. locked and limited conversation to collaborators on Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arch-riscvarea-CodeGen-coreclr

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions