Skip to content

Conversation

BrzVlad
Copy link
Member

@BrzVlad BrzVlad commented Jul 5, 2025

On amd64 it seems our jit expects i4 return to be sign extended to the full 64bit register. On arm64 apple, unlike arm64 linux, callers of methods returning i1,u1,i2 or u2 are expecting a value sign extended to i4. In order to prevent any potential issues around this area, this commit takes on a conservative approach and sign extends every time, since there is no real cost to it.

stackval_to_data_sign_ext is called either with a data pointer from a CallContext register or the address of a native int local variable in the mini_get_interp_in_wrapper. This means that it should always be safe to write the full value.

Fixes #115859
Fixes #110649

More general alternative to #117306

@Copilot Copilot AI review requested due to automatic review settings July 5, 2025 19:40
@BrzVlad BrzVlad requested a review from lateralusX July 5, 2025 19:40
Copy link
Contributor

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

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 ensures small integer return values are always sign-extended to the native register size on 64-bit platforms by introducing a specialized conversion function and updating return-value marshaling.

  • Adds stackval_to_data_sign_ext to handle sign-/zero-extension for 8-, 16-, and 32-bit return types on 64-bit targets.
  • Replaces calls to stackval_to_data with stackval_to_data_sign_ext in interp_frame_arg_to_data and interp_entry.
Comments suppressed due to low confidence (1)

src/mono/mono/mini/interp/interp.c:988

  • No tests currently verify that small-integer return values are correctly sign-/zero-extended. Consider adding interpreter unit tests that exercise methods returning i1, u1, i2, u2, i4, and u4 on 64-bit platforms to catch any regression.
static void

@BrzVlad BrzVlad closed this Jul 8, 2025
@BrzVlad BrzVlad reopened this Jul 8, 2025
…en returning to compiled code

On amd64 it seems our jit expects i4 to be sign extended to the full 64bit register. On arm64 apple, unlike arm64 linux, callers of methods returning i1,u1,i2 or u2 are expecting a value sign extended to i4. In order to prevent any potential issues around this area, this commit takes on a conservative approach and sign extends every time, since there is no real cost to it.

stackval_to_data_sign_ext is called either with a data pointer from a CallContext register or the address of a native int local variable in the `mini_get_interp_in_wrapper`. This means that it should always be safe to write the full value.
Those wrappers use the real type as the return, so sign extension shouldn't be needed and it wouldn't be safe, because the wrapper passes the address of the return var of the actual type. Attemptying to sign extend could overflow the storage of this var.
@BrzVlad BrzVlad force-pushed the fix-interp-sign-ext branch from ef0505f to 4bb2c5e Compare July 10, 2025 05:49
@BrzVlad BrzVlad merged commit 413d5b8 into dotnet:main Jul 10, 2025
65 of 69 checks passed
@srxqds
Copy link
Contributor

srxqds commented Jul 10, 2025

backport to release/9.0?

@BrzVlad
Copy link
Member Author

BrzVlad commented Jul 10, 2025

The bug is not critical enough to backport so late in the release cycle.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 10, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

3 participants