Skip to content
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

Weak interior handles #100446

Merged
merged 7 commits into from
Apr 18, 2024
Merged

Conversation

davidwrighton
Copy link
Member

Add a new type of handle (HNDTYPE_WEAK_INTERIOR_POINTER) and a use of it to make storage of managed class objects consistent between collectible and non-collectible variants

Copy link
Contributor

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

@davidwrighton
Copy link
Member Author

@Maoni0 I'd like it if you could take a look at this sometime. I've not managed to test it ... but this is the sort of thing I want to do

//
// We work around these details by the following means
// 1. We use a LOADERHANDLE to keep the object alive until the LoaderAllocator is freed.
// 2. We hold the crstLoaderAllocatorLock, and double check to wnsure that the data is ready to be filled in
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// 2. We hold the crstLoaderAllocatorLock, and double check to wnsure that the data is ready to be filled in
// 2. We hold the crstLoaderAllocatorLock, and double check to ensure that the data is ready to be filled in

@Maoni0
Copy link
Member

Maoni0 commented Apr 1, 2024

I presume the extra info for these handles must always point to the middle of the primary target of this handle (it's not obvious from the caller I'm seeing in loaderallocator.cpp). we should add some verification code that this is the case.

I thought I'd mention we do have a `GCHeap::GetContainingObject" that gets you from an interior pointer to its containing object, in case this is useful to you.

otherwise the change looks ok to me.

Don't forget to set the typehandle into the RuntimeType object
…ch point into the middle of the specified object
@davidwrighton
Copy link
Member Author

davidwrighton commented Apr 15, 2024

I presume the extra info for these handles must always point to the middle of the primary target of this handle (it's not obvious from the caller I'm seeing in loaderallocator.cpp). we should add some verification code that this is the case.

Ok, done.

I thought I'd mention we do have a `GCHeap::GetContainingObject" that gets you from an interior pointer to its containing object, in case this is useful to you.

Unfortunately my understanding is that api can only be called when the runtime is suspended due to race conditions, so I can't use this in any of the cases I'm working with.

@davidwrighton davidwrighton marked this pull request as ready for review April 15, 2024 22:42
// if we did then copy the value
if (pUserData)
{
TADDR pObjectInteriorPointer = **dac_cast<DPTR(DPTR(TADDR))>(pUserData);
Copy link
Member

Choose a reason for hiding this comment

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

Nit: None of the surrounding code is DACized so dac_cast is unnecessary complication.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I thought that VerifyHeap worked in the DAC, but it turns out that it is an entirely parallel managed implementation these days. I'll pull that out if you'd like.

Copy link
Member

Choose a reason for hiding this comment

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

There is a build break introduced by the dac_cast macro. My guess is that the easiest way to fix it is to delete the use here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

There is a potential follow up codegen improvement for collectible assemblies:

We can change getRuntimeTypePointer to return InfoAccessType in addition to the pointer itself and allow JIT to fetch the Type object by a simple dereference instead of calling the helper in collectible code.

cc @EgorBo

//
// We work around these details by the following means
// 1. We use a LOADERHANDLE to keep the object alive until the LoaderAllocator is freed.
// 2. We hold the crstLoaderAllocatorLock, and double check to ensure that the data is ready to be filled in
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// 2. We hold the crstLoaderAllocatorLock, and double check to ensure that the data is ready to be filled in
// 2. We hold the m_crstLoaderAllocator lock, and double check to ensure that the data is ready to be filled in

Nit

CONTRACTL_END;

const RUNTIMETYPEHANDLE handle = m_hExposedClassObject;
OBJECTREF retVal = ObjectToOBJECTREF((Object*)handle);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
OBJECTREF retVal = ObjectToOBJECTREF((Object*)handle);
OBJECTREF retVal = ObjectToOBJECTREF(handle);

RUNTIMETYPEHANDLE can be defined as Object* to save casts like this. It is actually Object* now.

@davidwrighton davidwrighton merged commit 56510f0 into dotnet:main Apr 18, 2024
92 checks passed
matouskozak pushed a commit to matouskozak/runtime that referenced this pull request Apr 30, 2024
- Implementation of weak interior handle
- Use new weak interior handle to make managed class object storage consistent between collectible and non-collectible types
@github-actions github-actions bot locked and limited conversation to collaborators May 19, 2024
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