Skip to content

Commit

Permalink
Fix more alloc-dealloc mismatches and use-after-scopes (#55420)
Browse files Browse the repository at this point in the history
* Fix another dynamically-sized allocation to use new/delete instead of the mismatched new[]/delete.

* Fix use-after-scope

* Fix another alloc-dealloc mismatch

* Update src/coreclr/vm/threadstatics.cpp

Co-authored-by: Jan Kotas <[email protected]>

* Use standard size_t instead of custom SIZE_T typedef.

* Fix formatting.

Co-authored-by: Jan Kotas <[email protected]>
  • Loading branch information
jkoritzinsky and jkotas authored Jul 12, 2021
1 parent d91e11a commit 1509b1a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/coreclr/jit/lsrabuild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3559,6 +3559,7 @@ int LinearScan::BuildReturn(GenTree* tree)
noway_assert(op1->IsMultiRegCall() || op1->IsMultiRegLclVar());

int srcCount;
ReturnTypeDesc nonCallRetTypeDesc;
const ReturnTypeDesc* pRetTypeDesc;
if (op1->OperIs(GT_CALL))
{
Expand All @@ -3567,13 +3568,12 @@ int LinearScan::BuildReturn(GenTree* tree)
else
{
assert(compiler->lvaEnregMultiRegVars);
LclVarDsc* varDsc = compiler->lvaGetDesc(op1->AsLclVar()->GetLclNum());
ReturnTypeDesc retTypeDesc;
retTypeDesc.InitializeStructReturnType(compiler, varDsc->GetStructHnd(),
compiler->info.compCallConv);
pRetTypeDesc = &retTypeDesc;
LclVarDsc* varDsc = compiler->lvaGetDesc(op1->AsLclVar()->GetLclNum());
nonCallRetTypeDesc.InitializeStructReturnType(compiler, varDsc->GetStructHnd(),
compiler->info.compCallConv);
pRetTypeDesc = &nonCallRetTypeDesc;
assert(compiler->lvaGetDesc(op1->AsLclVar()->GetLclNum())->lvFieldCnt ==
retTypeDesc.GetReturnRegCount());
nonCallRetTypeDesc.GetReturnRegCount());
}
srcCount = pRetTypeDesc->GetReturnRegCount();
// For any source that's coming from a different register file, we need to ensure that
Expand Down
8 changes: 3 additions & 5 deletions src/coreclr/vm/threadstatics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void ThreadLocalBlock::FreeTable()
SpinLock::Holder lock(&m_TLMTableLock);

// Free the table itself
delete m_pTLMTable;
delete[] m_pTLMTable;
m_pTLMTable = NULL;
}

Expand Down Expand Up @@ -136,7 +136,7 @@ void ThreadLocalBlock::EnsureModuleIndex(ModuleIndex index)

// If this allocation fails, we will throw. If it succeeds,
// then we are good to go
PTR_TLMTableEntry pNewModuleSlots = (PTR_TLMTableEntry) (void*) new BYTE[sizeof(TLMTableEntry) * aModuleIndices];
PTR_TLMTableEntry pNewModuleSlots = new TLMTableEntry[aModuleIndices];

// Zero out the new TLM table
memset(pNewModuleSlots, 0 , sizeof(TLMTableEntry) * aModuleIndices);
Expand Down Expand Up @@ -704,9 +704,7 @@ PTR_ThreadLocalModule ThreadStatics::AllocateTLM(Module * pModule)

SIZE_T size = pModule->GetThreadLocalModuleSize();

_ASSERTE(size >= ThreadLocalModule::OffsetOfDataBlob());

PTR_ThreadLocalModule pThreadLocalModule = (ThreadLocalModule*)new BYTE[size];
PTR_ThreadLocalModule pThreadLocalModule = new({ pModule }) ThreadLocalModule;

// We guarantee alignment for 64-bit regular thread statics on 32-bit platforms even without FEATURE_64BIT_ALIGNMENT for performance reasons.

Expand Down
14 changes: 14 additions & 0 deletions src/coreclr/vm/threadstatics.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,20 @@ struct ThreadLocalModule
return GetPrecomputedStaticsClassData()[classID] & ClassInitFlags::INITIALIZED_FLAG;
}

void* operator new(size_t) = delete;

struct ParentModule { PTR_Module pModule; };

void* operator new(size_t baseSize, ParentModule parentModule)
{
size_t size = parentModule.pModule->GetThreadLocalModuleSize();

_ASSERTE(size >= baseSize);
_ASSERTE(size >= ThreadLocalModule::OffsetOfDataBlob());

return ::operator new(size);
}

#ifndef DACCESS_COMPILE

FORCEINLINE void EnsureClassAllocated(MethodTable * pMT)
Expand Down

0 comments on commit 1509b1a

Please sign in to comment.