-
Notifications
You must be signed in to change notification settings - Fork 5k
[NativeAOT] Objective-C Marshal: object tracker support #78280
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7b694f7
NativeAOT Objective-C Marshal: object tracker support
AustinWise a1ded48
Centralize logic for turning on Objective-C Marshal
AustinWise 0d88691
Remove stray comment
AustinWise 6f677ad
Revert "Centralize logic for turning on Objective-C Marshal"
AustinWise 0dd4e62
Respond to feedback, plus add missing GC trigger disable
AustinWise 3da32c9
Remove some extraneous spaces and unneeded changes
AustinWise 2ec8884
Update src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs
AustinWise 6a06e1b
Fix build
AustinWise b1eb66d
Simplify initialization logic
AustinWise a7a6a9b
Respond to feedback
AustinWise 7cbf935
Apply suggestions from code review
AustinWise 916cf08
Revert "Apply suggestions from code review"
AustinWise 1ec0fa9
Revert "Revert "Apply suggestions from code review""
AustinWise 9163e95
Add SuppressGCTransition and fix compile error by adding cast
AustinWise e9e7845
Merge remote-tracking branch 'origin/main' into austin/ObjC-Reference…
AustinWise File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#ifdef FEATURE_OBJCMARSHAL | ||
|
||
class ObjCMarshalNative | ||
{ | ||
public: | ||
using TryGetCallback = void*(REDHAWK_CALLCONV *)(void); | ||
using TryGetTaggedMemoryCallback = CLR_BOOL(REDHAWK_CALLCONV *)(_In_ Object *, _Out_ void **); | ||
using BeginEndCallback = void(REDHAWK_CALLCONV *)(void); | ||
using IsReferencedCallback = int(REDHAWK_CALLCONV *)(_In_ void*); | ||
using EnteredFinalizationCallback = void(REDHAWK_CALLCONV *)(_In_ void*); | ||
|
||
public: // Instance inspection | ||
static bool IsTrackedReference(_In_ Object * pObject, _Out_ bool* isReferenced); | ||
public: // GC interaction | ||
static bool RegisterBeginEndCallback(void * callback); | ||
static void BeforeRefCountedHandleCallbacks(); | ||
static void AfterRefCountedHandleCallbacks(); | ||
static void OnEnteredFinalizerQueue(_In_ Object * object); | ||
}; | ||
|
||
#endif // FEATURE_OBJCMARSHAL |
178 changes: 178 additions & 0 deletions
178
src/coreclr/nativeaot/Runtime/interoplibinterface_objc.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#include "common.h" | ||
#include "CommonTypes.h" | ||
#include "CommonMacros.h" | ||
#include "daccess.h" | ||
#include "PalRedhawkCommon.h" | ||
#include "PalRedhawk.h" | ||
#include "rhassert.h" | ||
#include "slist.h" | ||
#include "holder.h" | ||
#include "gcrhinterface.h" | ||
#include "shash.h" | ||
#include "RWLock.h" | ||
#include "rhbinder.h" | ||
#include "Crst.h" | ||
#include "RuntimeInstance.h" | ||
#include "TypeManager.h" | ||
#include "MethodTable.h" | ||
#include "ObjectLayout.h" | ||
#include "event.h" | ||
#include "varint.h" | ||
#include "regdisplay.h" | ||
#include "StackFrameIterator.h" | ||
#include "thread.h" | ||
#include "threadstore.h" | ||
#include "threadstore.inl" | ||
|
||
#include "interoplibinterface.h" | ||
|
||
#include "MethodTable.inl" | ||
|
||
#ifdef FEATURE_OBJCMARSHAL | ||
|
||
namespace | ||
{ | ||
// TODO: Support registering multiple begin/end callbacks. | ||
// NativeAOT support the concept of multiple managed "worlds", | ||
// each with their own object heirarchy. Each of these worlds | ||
// could potentially have its own set of callbacks. | ||
// However this Objective-C Marshal API was not designed with such a | ||
// possibility in mind. | ||
ObjCMarshalNative::BeginEndCallback s_pBeginEndCallback = nullptr; | ||
|
||
struct DisableTriggerGcWhileCallingManagedCode | ||
{ | ||
Thread * m_pThread; | ||
bool m_fGcStressWasSuppressed; | ||
|
||
DisableTriggerGcWhileCallingManagedCode() | ||
{ | ||
// It is illegal for any of the callouts to trigger a GC. | ||
m_pThread = ThreadStore::GetCurrentThread(); | ||
m_pThread->SetDoNotTriggerGc(); | ||
|
||
// Due to the above we have better suppress GC stress. | ||
m_fGcStressWasSuppressed = m_pThread->IsSuppressGcStressSet(); | ||
if (!m_fGcStressWasSuppressed) | ||
m_pThread->SetSuppressGcStress(); | ||
} | ||
|
||
~DisableTriggerGcWhileCallingManagedCode() | ||
{ | ||
// Revert GC stress mode if we changed it. | ||
if (!m_fGcStressWasSuppressed) | ||
m_pThread->ClearSuppressGcStress(); | ||
|
||
m_pThread->ClearDoNotTriggerGc(); | ||
} | ||
}; | ||
|
||
bool TryGetTaggedMemory(_In_ Object * obj, _Out_ void ** tagged) | ||
{ | ||
void* fn = obj->get_EEType() | ||
AustinWise marked this conversation as resolved.
Show resolved
Hide resolved
|
||
->GetTypeManagerPtr() | ||
->AsTypeManager() | ||
->GetClasslibFunction(ClasslibFunctionId::ObjectiveCMarshalTryGetTaggedMemory); | ||
|
||
ASSERT(fn != nullptr); | ||
|
||
auto pTryGetTaggedMemoryCallback = reinterpret_cast<ObjCMarshalNative::TryGetTaggedMemoryCallback>(fn); | ||
|
||
DisableTriggerGcWhileCallingManagedCode disabler{}; | ||
|
||
bool result = pTryGetTaggedMemoryCallback(obj, tagged); | ||
|
||
return result; | ||
} | ||
|
||
// Calls a managed classlib function to potentially get an unmanaged callback. | ||
// Not for use with ObjectiveCMarshalTryGetTaggedMemory. | ||
void * GetCallbackViaClasslibCallback(_In_ Object * object, _In_ ClasslibFunctionId id) | ||
{ | ||
void* fn = object->get_EEType() | ||
->GetTypeManagerPtr() | ||
->AsTypeManager() | ||
->GetClasslibFunction(id); | ||
|
||
ASSERT(fn != nullptr); | ||
|
||
DisableTriggerGcWhileCallingManagedCode disabler{}; | ||
|
||
fn = ((ObjCMarshalNative::TryGetCallback)fn)(); | ||
|
||
ASSERT(fn != nullptr); | ||
|
||
return fn; | ||
} | ||
} | ||
|
||
bool ObjCMarshalNative::RegisterBeginEndCallback(void * callback) | ||
{ | ||
ASSERT(callback != nullptr); | ||
|
||
// We must be in Cooperative mode since we are setting callbacks that | ||
// will be used during a GC and we want to ensure a GC isn't occurring | ||
// while they are being set. | ||
ASSERT(ThreadStore::GetCurrentThread()->IsCurrentThreadInCooperativeMode()); | ||
|
||
return PalInterlockedCompareExchangePointer(&s_pBeginEndCallback, callback, nullptr) == nullptr; | ||
} | ||
|
||
bool ObjCMarshalNative::IsTrackedReference(_In_ Object * object, _Out_ bool* isReferenced) | ||
{ | ||
*isReferenced = false; | ||
|
||
if (!object->GetGCSafeMethodTable()->IsTrackedReferenceWithFinalizer()) | ||
return false; | ||
|
||
auto pIsReferencedCallbackCallback = (ObjCMarshalNative::IsReferencedCallback)GetCallbackViaClasslibCallback( | ||
object, ClasslibFunctionId::ObjectiveCMarshalGetIsTrackedReferenceCallback); | ||
|
||
void* taggedMemory; | ||
if (!TryGetTaggedMemory(object, &taggedMemory)) | ||
{ | ||
// It should not be possible to create a ref-counted handle | ||
// without setting up the tagged memory first. | ||
ASSERT(false); | ||
return false; | ||
} | ||
|
||
int result = pIsReferencedCallbackCallback(taggedMemory); | ||
|
||
*isReferenced = (result != 0); | ||
return true; | ||
} | ||
|
||
void ObjCMarshalNative::BeforeRefCountedHandleCallbacks() | ||
{ | ||
if (s_pBeginEndCallback != nullptr) | ||
s_pBeginEndCallback(); | ||
} | ||
|
||
void ObjCMarshalNative::AfterRefCountedHandleCallbacks() | ||
{ | ||
if (s_pBeginEndCallback != nullptr) | ||
s_pBeginEndCallback(); | ||
} | ||
|
||
void ObjCMarshalNative::OnEnteredFinalizerQueue(_In_ Object * object) | ||
{ | ||
auto pOnEnteredFinalizerQueueCallback = (ObjCMarshalNative::EnteredFinalizationCallback)GetCallbackViaClasslibCallback( | ||
object, ClasslibFunctionId::ObjectiveCMarshalGetOnEnteredFinalizerQueueCallback); | ||
|
||
void* taggedMemory; | ||
if (!TryGetTaggedMemory(object, &taggedMemory)) | ||
{ | ||
// Its possible to create an object that supports reference tracking | ||
// without ever creating a ref-counted handle for it. In this case, | ||
// there will be no tagged memory. | ||
return; | ||
} | ||
|
||
pOnEnteredFinalizerQueueCallback(taggedMemory); | ||
} | ||
|
||
#endif // FEATURE_OBJCMARSHAL |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.