Skip to content

JIT: (bug) int.Log2/long.Log2 with a negative constant corrupts the importer stack (InvalidProgramException) #133711

Closed

Description

@EgorBo
using System;
using System.Runtime.CompilerServices;

class P
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test() => int.Log2(-1);

    static void Main()
    {
        Console.WriteLine(Test());
    }
}

Expected: System.ArgumentOutOfRangeException from int.Log2 (it throws for negative input).
Actual: Unhandled exception. System.InvalidProgramException: Common Language Runtime detected an invalid program. (exit code 0xE0434352)

Notes:

  • csc -optimize+, then corerun repro.dll. Reproduces with tiering on, with DOTNET_TieredCompilation=0, and with -optimize-.
  • long.Log2(-1L) fails the same way; so does a call with another value underneath on the stack, e.g. k + int.Log2(-1).
  • src/coreclr/jit/importercalls.cpp:6935-6959 (NI_PRIMITIVE_Log2): the constant branch calls impPopStack() before checking signedness, and for a negative signed constant leaves result == nullptr, so the intrinsic is declined after the argument has already been removed from the importer stack.

Activity

  1. added this to the 12.0.0 milestone on Sep 11, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 11, 2026
  3. dotnet-policy-service commented on Sep 11, 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 a commit that references this issue on Sep 14, 2026
    09638d0
  5. added a commit that references this issue on Sep 18, 2026
    4cd10ac
  6. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 8.0.425
    • 9.0.318
    • 10.0.401
    • 11.0.100-rc.2.26467.112
  7. locked and limited conversation to collaborators on Oct 19, 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