diff options
author | Jeremy Koritzinsky <jekoritz@microsoft.com> | 2021-07-13 01:55:26 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-13 01:55:26 +0300 |
commit | 1509b1a0e66aae1504137488fb25c00bf822e3e6 (patch) | |
tree | 6f788a06b7bd60d67ea0c97e6b21d8a0b117a89c /src/coreclr/vm | |
parent | d91e11a2bc2f21f3f9df6e16603aa097bea1bd16 (diff) |
Fix more alloc-dealloc mismatches and use-after-scopes (#55420)
* 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 <jkotas@microsoft.com>
* Use standard size_t instead of custom SIZE_T typedef.
* Fix formatting.
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Diffstat (limited to 'src/coreclr/vm')
-rw-r--r-- | src/coreclr/vm/threadstatics.cpp | 8 | ||||
-rw-r--r-- | src/coreclr/vm/threadstatics.h | 14 |
2 files changed, 17 insertions, 5 deletions
diff --git a/src/coreclr/vm/threadstatics.cpp b/src/coreclr/vm/threadstatics.cpp index 2644d7ad5fc..bac0f082fb8 100644 --- a/src/coreclr/vm/threadstatics.cpp +++ b/src/coreclr/vm/threadstatics.cpp @@ -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; } @@ -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); @@ -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. diff --git a/src/coreclr/vm/threadstatics.h b/src/coreclr/vm/threadstatics.h index 1755bf7230d..ddb59b5cbc2 100644 --- a/src/coreclr/vm/threadstatics.h +++ b/src/coreclr/vm/threadstatics.h @@ -450,6 +450,20 @@ public: 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) |