Skip to content

[clr-interp] Fix access of boxed static fields address #115198

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 6, 2025

Conversation

BrzVlad
Copy link
Member

@BrzVlad BrzVlad commented Apr 30, 2025

Previous code was assuming that the field address obtained from the runtime points to the address where the field value resides. This is false for CORINFO_FLG_FIELD_STATIC_IN_HEAP fields, where at the field address we actually have the boxed instance reference, so we need to access the field value inside this box object instead.

Previous code was assuming that the field address obtained from the runtime points to the address where the field value resides. This is false for CORINFO_FLG_FIELD_STATIC_IN_HEAP fields, where at the field address we actually have the boxed instance reference, so we need to access the field value inside this box object instead.
@Copilot Copilot AI review requested due to automatic review settings April 30, 2025 16:19
@BrzVlad BrzVlad requested review from janvorli and kg as code owners April 30, 2025 16:19
Copy link
Contributor

Tagging subscribers to this area: @BrzVlad, @janvorli, @kg
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 fixes the access of boxed static fields so that the field value is correctly obtained from the boxed instance rather than directly at the static field address.

  • Introduces a flag to detect boxed static fields
  • Adjusts stack pointer arithmetic to first load the boxed instance reference and then skips over the method table word
  • Updates the code flow to correctly manage and push the new interpreted types

@@ -1861,6 +1862,25 @@ void InterpCompiler::EmitStaticFieldAddress(CORINFO_FIELD_INFO *pFieldInfo, CORI
assert(0);
break;
}

if (isBoxedStatic)
Copy link
Preview

Copilot AI Apr 30, 2025

Choose a reason for hiding this comment

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

Consider refactoring the pointer arithmetic logic within the isBoxedStatic block. Extracting intermediate values using temporary variables or a helper function could improve clarity and reduce potential errors.

Copilot uses AI. Check for mistakes.

// Skip method table word
m_pStackPointer--;
AddIns(INTOP_ADD_P_IMM);
m_pLastNewIns->data[0] = sizeof(void*);
Copy link
Preview

Copilot AI Apr 30, 2025

Choose a reason for hiding this comment

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

Using sizeof(void*) as a magic value can be unclear; consider defining a named constant to document its purpose.

Suggested change
m_pLastNewIns->data[0] = sizeof(void*);
m_pLastNewIns->data[0] = POINTER_SIZE;

Copilot uses AI. Check for mistakes.

@BrzVlad
Copy link
Member Author

BrzVlad commented Apr 30, 2025

@cshung You reported some issues with thread static fields a while ago. Don't remember exactly what the test case was, but found some incorrect behavior when revisiting the code. In case you remember how you reproduced the issue, let me know if this fixes it.

@cshung
Copy link
Contributor

cshung commented Apr 30, 2025

@cshung You reported some issues with thread static fields a while ago. Don't remember exactly what the test case was, but found some incorrect behavior when revisiting the code. In case you remember how you reproduced the issue, let me know if this fixes it.

@kg would know - I was helping her to debug something related to GC reporting - she gave me that repro.

Copy link
Member

@janvorli janvorli left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@kg
Copy link
Member

kg commented Apr 30, 2025

The existing code is unconditionally pushing byrefs, i.e.

PushInterpType(InterpTypeByRef, NULL);

I think it needs to push InterpTypeO for the case where the static field address is in the heap, right?

@BrzVlad
Copy link
Member Author

BrzVlad commented May 1, 2025

@kg Why would it ? Field address is always a byref. It is not an object reference, rather an interior pointer for an object in your case.

@BrzVlad BrzVlad merged commit 5ccaddf into dotnet:main May 6, 2025
94 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jun 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants