Skip to content

Commit d0adff8

Browse files
authored
Fix alloc-dealloc mismatches (#54701)
1 parent 6b5dbf6 commit d0adff8

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

src/coreclr/vm/ilstubresolver.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,7 @@ ILStubResolver::AllocGeneratedIL(
344344
if (!UseLoaderHeap())
345345
{
346346
NewArrayHolder<BYTE> pNewILCodeBuffer = new BYTE[cbCode];
347-
NewArrayHolder<CompileTimeState> pNewCompileTimeState = (CompileTimeState*)new BYTE[sizeof(CompileTimeState)];
348-
memset(pNewCompileTimeState, 0, sizeof(CompileTimeState));
347+
NewHolder<CompileTimeState> pNewCompileTimeState = new CompileTimeState{};
349348
NewArrayHolder<BYTE> pNewLocalSig = NULL;
350349

351350
if (0 != cbLocalSig)

src/coreclr/vm/methodtable.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8504,10 +8504,7 @@ MethodTable::GetMethodDataHelper(
85048504
MethodDataWrapper hDecl(GetMethodData(pMTDecl, FALSE));
85058505
MethodDataWrapper hImpl(GetMethodData(pMTImpl, FALSE));
85068506

8507-
UINT32 cb = MethodDataInterfaceImpl::GetObjectSize(pMTDecl);
8508-
NewArrayHolder<BYTE> pb(new BYTE[cb]);
8509-
MethodDataInterfaceImpl * pData = new (pb.GetValue()) MethodDataInterfaceImpl(rgDeclTypeIDs, cDeclTypeIDs, hDecl, hImpl);
8510-
pb.SuppressRelease();
8507+
MethodDataInterfaceImpl * pData = new ({ pMTDecl }) MethodDataInterfaceImpl(rgDeclTypeIDs, cDeclTypeIDs, hDecl, hImpl);
85118508

85128509
return pData;
85138510
} // MethodTable::GetMethodDataHelper
@@ -8548,10 +8545,8 @@ MethodTable::MethodData *MethodTable::GetMethodDataHelper(MethodTable *pMTDecl,
85488545
}
85498546
else {
85508547
UINT32 cb = MethodDataObject::GetObjectSize(pMTDecl);
8551-
NewArrayHolder<BYTE> pb(new BYTE[cb]);
85528548
MethodDataHolder h(FindParentMethodDataHelper(pMTDecl));
8553-
pData = new (pb.GetValue()) MethodDataObject(pMTDecl, h.GetValue());
8554-
pb.SuppressRelease();
8549+
pData = new ({ pMTDecl }) MethodDataObject(pMTDecl, h.GetValue());
85558550
}
85568551
}
85578552
else {

src/coreclr/vm/methodtable.h

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3157,7 +3157,7 @@ public :
31573157

31583158
protected:
31593159
//--------------------------------------------------------------------------------------
3160-
class MethodDataObject : public MethodData
3160+
class MethodDataObject final : public MethodData
31613161
{
31623162
public:
31633163
// Static method that returns the amount of memory to allocate for a particular type.
@@ -3237,19 +3237,32 @@ public :
32373237
{ LIMITED_METHOD_CONTRACT; return m_pMDImpl; }
32383238
};
32393239

3240-
//
3241-
// At the end of this object is an array, so you cannot derive from this class.
3242-
//
32433240

32443241
inline MethodDataObjectEntry *GetEntryData()
3245-
{ LIMITED_METHOD_CONTRACT; return (MethodDataObjectEntry *)(this + 1); }
3242+
{ LIMITED_METHOD_CONTRACT; return &m_rgEntries[0]; }
32463243

32473244
inline MethodDataObjectEntry *GetEntry(UINT32 i)
32483245
{ LIMITED_METHOD_CONTRACT; CONSISTENCY_CHECK(i < GetNumMethods()); return GetEntryData() + i; }
32493246

32503247
void FillEntryDataForAncestor(MethodTable *pMT);
32513248

3252-
// MethodDataObjectEntry m_rgEntries[...];
3249+
//
3250+
// At the end of this object is an array
3251+
//
3252+
MethodDataObjectEntry m_rgEntries[0];
3253+
3254+
public:
3255+
struct TargetMethodTable
3256+
{
3257+
MethodTable* pMT;
3258+
};
3259+
3260+
static void* operator new(size_t size, TargetMethodTable targetMT)
3261+
{
3262+
_ASSERTE(size <= GetObjectSize(targetMT.pMT));
3263+
return ::operator new(GetObjectSize(targetMT.pMT));
3264+
}
3265+
static void* operator new(size_t size) = delete;
32533266
}; // class MethodDataObject
32543267

32553268
//--------------------------------------------------------------------------------------
@@ -3303,7 +3316,7 @@ public :
33033316
}; // class MethodDataInterface
33043317

33053318
//--------------------------------------------------------------------------------------
3306-
class MethodDataInterfaceImpl : public MethodData
3319+
class MethodDataInterfaceImpl final : public MethodData
33073320
{
33083321
public:
33093322
// Object construction-related methods
@@ -3377,12 +3390,25 @@ public :
33773390
//
33783391

33793392
inline MethodDataEntry *GetEntryData()
3380-
{ LIMITED_METHOD_CONTRACT; return (MethodDataEntry *)(this + 1); }
3393+
{ LIMITED_METHOD_CONTRACT; return &m_rgEntries[0]; }
33813394

33823395
inline MethodDataEntry *GetEntry(UINT32 i)
33833396
{ LIMITED_METHOD_CONTRACT; CONSISTENCY_CHECK(i < GetNumMethods()); return GetEntryData() + i; }
33843397

3385-
// MethodDataEntry m_rgEntries[...];
3398+
MethodDataEntry m_rgEntries[0];
3399+
3400+
public:
3401+
struct TargetMethodTable
3402+
{
3403+
MethodTable* pMT;
3404+
};
3405+
3406+
static void* operator new(size_t size, TargetMethodTable targetMT)
3407+
{
3408+
_ASSERTE(size <= GetObjectSize(targetMT.pMT));
3409+
return ::operator new(GetObjectSize(targetMT.pMT));
3410+
}
3411+
static void* operator new(size_t size) = delete;
33863412
}; // class MethodDataInterfaceImpl
33873413

33883414
//--------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)