Skip to content

Fix interpreter double and 64 bit constant processing #116115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025

Conversation

janvorli
Copy link
Member

There is a bug in extracting 64 bit and double constants on both the interpreter compiler and execution sides. The problem was that the lower 32 bits were extracted as signed 32 bit int and then casted to signed 64 bit int and or-ed to the other 32 bits shifted up. The problem is that when the lower 32 bits were signed, the sign got extended to the upper 32 bits and the combined result was incorrect.

This change fixes the issue. It fixes 24 codegen bringup tests with the interpreter that were previously failing with "Assert.Equal() Failure: Values differ".

There is a bug in extracting 64 bit and double constants on both the
interpreter compiler and execution sides. The problem was that the lower
32 bits were extracted as signed 32 bit int and then casted to signed 64
bit int and or-ed to the other 32 bits shifted up. The problem is that
when the lower 32 bits were signed, the sign got extended to the upper
32 bits and the combined result was incorrect.

This fixes 24 codegen bringup tests with the interpreter that were
previously failing with "Assert.Equal() Failure: Values differ".
@Copilot Copilot AI review requested due to automatic review settings May 29, 2025 20:52
@janvorli janvorli requested review from BrzVlad and kg as code owners May 29, 2025 20:52
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 29, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug in extracting 64-bit and double constants by correcting how the lower 32 bits are handled, preventing unintended sign extension issues.

  • Fixed the extraction in INTOP_LDC_I8 and INTOP_LDC_R8 operations in interpexec.cpp
  • Changed the extraction of the lower 32 bits in intops.h from a signed version to an unsigned version using getU4LittleEndian

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/coreclr/vm/interpexec.cpp Adjusted casting for lower 32 bits to avoid sign extension issues.
src/coreclr/interpreter/intops.h Updated lower 32 bits extraction to use getU4LittleEndian for unsigned data.
Comments suppressed due to low confidence (2)

src/coreclr/vm/interpexec.cpp:146

  • Casting ip[2] to a uint32_t correctly prevents sign extension issues when constructing the 64-bit constant. Consider adding tests for edge-case values to ensure the behavior is as expected.
LOCAL_VAR(ip[1], int64_t) = (int64_t)(uint32_t)ip[2] + ((int64_t)ip[3] << 32);

src/coreclr/interpreter/intops.h:101

  • Using getU4LittleEndian for the lower 32 bits avoids erroneous sign extension. Verify that all related tests cover scenarios with negative lower-half values.
return (int64_t)getU4LittleEndian(ptr) | ((int64_t)getI4LittleEndian(ptr + 4)) << 32;

Copy link
Member

@kg kg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to see us just use memcpy, but lgtm

@janvorli janvorli merged commit f4dc588 into dotnet:main May 30, 2025
95 of 97 checks passed
@janvorli janvorli deleted the fix-double-ops branch May 30, 2025 11:16
@janvorli janvorli mentioned this pull request May 30, 2025
61 tasks
@teo-tsirpanis teo-tsirpanis added area-CodeGen-Interpreter-coreclr and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels May 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants