Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 0bd13a2

Browse files
Add handle assignment validation to VM side.
1 parent 80ca680 commit 0bd13a2

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

src/vm/gchandleutilities.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,19 @@ class GCHandleUtilities
3030
GCHandleUtilities() = delete;
3131
};
3232

33-
void ValidateHandleAndAppDomain(OBJECTHANDLE handle);
33+
void ValidateObjectAndAppDomain(OBJECTREF objRef, ADIndex appDomainIndex);
34+
void ValidateHandleAssignment(OBJECTHANDLE handle, OBJECTREF objRef);
3435

3536
// Given a handle, returns an OBJECTREF for the object it refers to.
3637
inline OBJECTREF ObjectFromHandle(OBJECTHANDLE handle)
3738
{
3839
_ASSERTE(handle);
3940

4041
#ifdef _DEBUG_IMPL
41-
ValidateHandleAndAppDomain(handle);
42+
DWORD context = (DWORD)GCHandleUtilities::GetGCHandleManager()->GetHandleContext(handle);
43+
OBJECTREF objRef = ObjectToOBJECTREF(*(Object**)handle);
44+
45+
ValidateObjectAndAppDomain(objRef, ADIndex(context));
4246
#endif // _DEBUG_IMPL
4347

4448
// Wrap the raw OBJECTREF and return it
@@ -173,16 +177,22 @@ inline OBJECTHANDLE CreateVariableHandle(IGCHandleStore* store, OBJECTREF object
173177

174178
inline void StoreObjectInHandle(OBJECTHANDLE handle, OBJECTREF object)
175179
{
180+
ValidateHandleAssignment(handle, object);
181+
176182
GCHandleUtilities::GetGCHandleManager()->StoreObjectInHandle(handle, OBJECTREFToObject(object));
177183
}
178184

179185
inline bool StoreFirstObjectInHandle(OBJECTHANDLE handle, OBJECTREF object)
180186
{
187+
ValidateHandleAssignment(handle, object);
188+
181189
return GCHandleUtilities::GetGCHandleManager()->StoreObjectInHandleIfNull(handle, OBJECTREFToObject(object));
182190
}
183191

184192
inline void* InterlockedCompareExchangeObjectInHandle(OBJECTHANDLE handle, OBJECTREF object, OBJECTREF comparandObject)
185193
{
194+
ValidateHandleAssignment(handle, object);
195+
186196
return GCHandleUtilities::GetGCHandleManager()->CompareAndSwapObjectInHandle(handle, OBJECTREFToObject(object), OBJECTREFToObject(comparandObject));
187197
}
188198

src/vm/gcheaputilities.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,12 @@ bool g_sw_ww_enabled_for_gc_heap = false;
4040
gc_alloc_context g_global_alloc_context = {};
4141

4242
// Debug-only validation for handle.
43-
void ValidateHandleAndAppDomain(OBJECTHANDLE handle)
43+
44+
void ValidateObjectAndAppDomain(OBJECTREF objRef, ADIndex appDomainIndex)
4445
{
4546
#ifdef _DEBUG_IMPL
46-
OBJECTREF objRef = ObjectToOBJECTREF(*(Object**)handle);
4747
VALIDATEOBJECTREF(objRef);
4848

49-
IGCHandleManager *pHandleManager = GCHandleUtilities::GetGCHandleManager();
50-
51-
DWORD context = (DWORD)pHandleManager->GetHandleContext(handle);
52-
53-
ADIndex appDomainIndex = ADIndex(context);
5449
AppDomain *domain = SystemDomain::GetAppDomainAtIndex(appDomainIndex);
5550

5651
// Access to a handle in an unloaded domain is not allowed
@@ -72,3 +67,25 @@ void ValidateHandleAndAppDomain(OBJECTHANDLE handle)
7267
#endif // CHECK_APP_DOMAIN_LEAKS
7368
#endif // _DEBUG_IMPL
7469
}
70+
71+
void ValidateHandleAssignment(OBJECTHANDLE handle, OBJECTREF objRef)
72+
{
73+
#ifdef _DEBUG_IMPL
74+
_ASSERTE(handle);
75+
76+
#ifdef DEBUG_DestroyedHandleValue
77+
// Verify that we are not trying to access a freed handle.
78+
_ASSERTE("Attempt to access destroyed handle." && *(_UNCHECKED_OBJECTREF*)handle != DEBUG_DestroyedHandleValue);
79+
#endif
80+
81+
ADIndex appDomainIndex = HndGetHandleADIndex(handle);
82+
83+
AppDomain *unloadingDomain = SystemDomain::AppDomainBeingUnloaded();
84+
if (unloadingDomain && unloadingDomain->GetIndex() == appDomainIndex && unloadingDomain->NoAccessToHandleTable())
85+
{
86+
_ASSERTE (!"Access to a handle in unloaded domain is not allowed");
87+
}
88+
89+
ValidateObjectAndAppDomain(objRef, appDomainIndex);
90+
#endif // _DEBUG_IMPL
91+
}

0 commit comments

Comments
 (0)