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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add validation that weak interior pointers have interior pointers whi…
…ch point into the middle of the specified object
  • Loading branch information
davidwrighton committed Apr 15, 2024
commit 6a5b21fdacc338bb7f82c5620f138ca6be3b9e2b
14 changes: 14 additions & 0 deletions src/coreclr/gc/env/gcenv.base.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,20 @@ typedef uintptr_t TADDR;
typedef DPTR(size_t) PTR_size_t;
typedef DPTR(uint8_t) PTR_uint8_t;

#if defined(BUILD_AS_STANDALONE)
// In the standalone build, we don't have the DAC headers, so we need to define a non-DAC variant of dac_cast.
template <typename Tgt, typename Src>
inline Tgt dac_cast(Src src)
{
// In non-DAC builds, dac_cast is the same as a C-style cast because we need to support:
// - casting away const
// - conversions between pointers and TADDR
// Perhaps we should more precisely restrict it's usage, but we get the precise
// restrictions in DAC builds, so it wouldn't buy us much.
return (Tgt)(src);
}
#endif

// -----------------------------------------------------------------------------------------------------------

#define DATA_ALIGNMENT sizeof(uintptr_t)
Expand Down
26 changes: 26 additions & 0 deletions src/coreclr/gc/handletablescan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,15 @@ static void VerifyObjectAndAge(_UNCHECKED_OBJECTREF from, _UNCHECKED_OBJECTREF o
}
}

size_t my_get_size (_UNCHECKED_OBJECTREF ob)
{
DPTR(MethodTable) mT = dac_cast<DPTR(MethodTable)>((TADDR)ob->GetGCSafeMethodTable());

return (mT->GetBaseSize() +
(mT->HasComponentSize() ?
((size_t)dac_cast<DPTR(ArrayBase)>(ob)->GetNumComponents() * mT->RawGetComponentSize()) : 0));
}

/*
* BlockVerifyAgeMapForBlocksWorker
*
Expand Down Expand Up @@ -992,6 +1001,23 @@ void BlockVerifyAgeMapForBlocksWorker(uint32_t *pdwGen, uint32_t dwClumpMask, Sc
}
}
}
if (uType == HNDTYPE_WEAK_INTERIOR_POINTER)
{
PTR_uintptr_t pUserData = HandleQuickFetchUserDataPointer((OBJECTHANDLE)pValue);

// 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

_UNCHECKED_OBJECTREF pObjectPointerRef = *pValue;
TADDR pObjectPointer = dac_cast<TADDR>(pObjectPointerRef);
if (pObjectInteriorPointer < pObjectPointer || pObjectInteriorPointer >= (pObjectPointer + my_get_size(pObjectPointerRef)))
{
_ASSERTE(!"Weak interior pointer has interior pointer which does not point at the object of the handle.");
GCToEEInterface::HandleFatalError(COR_E_EXECUTIONENGINE);
}
}
}
}
}
}
Expand Down
Loading