Skip to content

Commit 4246ba1

Browse files
authored
Use alternative mechanisms to allow us to use the default new/delete implementations (#101947)
1 parent 84885d7 commit 4246ba1

File tree

21 files changed

+75
-214
lines changed

21 files changed

+75
-214
lines changed

src/coreclr/clrdefinitions.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ if(CLR_CMAKE_TARGET_LINUX_MUSL)
4242
add_definitions(-DNO_FIXED_STACK_LIMIT)
4343
endif(CLR_CMAKE_TARGET_LINUX_MUSL)
4444

45-
add_definitions(-D_BLD_CLR)
4645
add_definitions(-DDEBUGGING_SUPPORTED)
4746
add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:DAC_COMPONENT>>>:PROFILING_SUPPORTED>)
4847
add_compile_definitions($<$<BOOL:$<TARGET_PROPERTY:DAC_COMPONENT>>:PROFILING_SUPPORTED_DATA>)

src/coreclr/debug/inc/dbgipcevents.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifndef _DbgIPCEvents_h_
1111
#define _DbgIPCEvents_h_
1212

13-
#include <new.hpp>
13+
#include <new>
1414
#include <cor.h>
1515
#include <cordebug.h>
1616
#include <corjit.h> // for ICorDebugInfo::VarLocType & VarLoc
@@ -25,6 +25,8 @@
2525

2626
#include "./common.h"
2727

28+
using std::nothrow;
29+
2830
//-----------------------------------------------------------------------------
2931
// V3 additions to IPC protocol between LS and RS.
3032
//-----------------------------------------------------------------------------

src/coreclr/ilasm/main.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,6 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
151151
memset(wzOutputFilename,0,sizeof(wzOutputFilename));
152152
memset(wzPdbFilename, 0, sizeof(wzPdbFilename));
153153

154-
#ifdef _DEBUG
155-
DisableThrowCheck();
156-
//CONTRACT_VIOLATION(ThrowsViolation);
157-
#endif
158-
159154
if(argc < 2) goto ErrorExit;
160155
#ifdef _PREFAST_
161156
#pragma warning(push)

src/coreclr/ildasm/windasm.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
#include <clrversion.h>
1818
#include "resource.h"
1919

20-
#include "new.hpp"
20+
#include <new>
21+
22+
using std::nothrow;
2123

2224
#define MODE_DUMP_ALL 0
2325
#define MODE_DUMP_CLASS 1
@@ -465,10 +467,6 @@ int main(int argc, char* argv[])
465467
}
466468
#endif
467469

468-
#ifdef _DEBUG
469-
DisableThrowCheck();
470-
#endif
471-
472470
int iCommandLineParsed = 0;
473471
WCHAR* wzCommandLine = NULL;
474472
char* szCommandLine = NULL;

src/coreclr/inc/clrhost.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#ifndef __CLRHOST_H__
99
#define __CLRHOST_H__
1010

11+
#include <new>
12+
1113
#include "windows.h" // worth to include before mscoree.h so we are guaranteed to pick few definitions
1214
#ifdef CreateSemaphore
1315
#undef CreateSemaphore
@@ -16,7 +18,9 @@
1618
#include "clrinternal.h"
1719
#include "switches.h"
1820
#include "holder.h"
19-
#include "new.hpp"
21+
22+
using std::nothrow;
23+
2024
#include "staticcontract.h"
2125
#include "predeftlsslot.h"
2226
#include "safemath.h"

src/coreclr/inc/corhlpr.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
****************************************************************************/
99
#ifndef SOS_INCLUDE
1010

11-
#ifdef _BLD_CLR
1211
#include "utilcode.h"
13-
#endif
1412
#include "corhlpr.h"
1513
#include <stdlib.h>
1614

src/coreclr/inc/corhlpr.h

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,9 @@
2222
#include "corhdr.h"
2323
#include "corerror.h"
2424
#include "unreachable.h"
25+
#include <new>
2526

26-
// This header is consumed both within the runtime and externally. In the former
27-
// case we need to wrap memory allocations, in the latter there is no
28-
// infrastructure to support this. Detect which way we're building and provide a
29-
// very simple abstraction layer (handles allocating bytes only).
30-
#ifdef _BLD_CLR
31-
#include "new.hpp"
32-
27+
using std::nothrow;
3328

3429
#define NEW_NOTHROW(_bytes) new (nothrow) BYTE[_bytes]
3530
#define NEW_THROWS(_bytes) new BYTE[_bytes]
@@ -38,27 +33,6 @@ inline void DECLSPEC_NORETURN THROW_OUT_OF_MEMORY()
3833
{
3934
ThrowOutOfMemory();
4035
}
41-
#else
42-
#define NEW_NOTHROW(_bytes) new BYTE[_bytes]
43-
#define NEW_THROWS(_bytes) __CorHlprNewThrows(_bytes)
44-
static inline void DECLSPEC_NORETURN __CorHlprThrowOOM()
45-
{
46-
RaiseException(STATUS_NO_MEMORY, 0, 0, NULL);
47-
__UNREACHABLE();
48-
}
49-
static inline BYTE *__CorHlprNewThrows(size_t bytes)
50-
{
51-
BYTE *pbMemory = new BYTE[bytes];
52-
if (pbMemory == NULL)
53-
__CorHlprThrowOOM();
54-
return pbMemory;
55-
}
56-
inline void DECLSPEC_NORETURN THROW_OUT_OF_MEMORY()
57-
{
58-
__CorHlprThrowOOM();
59-
}
60-
#endif
61-
6236

6337
//*****************************************************************************
6438
// There are a set of macros commonly used in the helpers which you will want

src/coreclr/inc/corhlprpriv.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
****************************************************************************/
99
#ifndef SOS_INCLUDE
1010

11-
#ifdef _BLD_CLR
1211
#include "utilcode.h"
13-
#endif
1412
#include "corhlprpriv.h"
1513
#include <stdlib.h>
1614

@@ -23,7 +21,6 @@
2321
template <SIZE_T SIZE, SIZE_T INCREMENT>
2422
HRESULT CQuickMemoryBase<SIZE, INCREMENT>::ReSizeNoThrow(SIZE_T iItems)
2523
{
26-
#ifdef _BLD_CLR
2724
#ifdef _DEBUG
2825
#ifndef DACCESS_COMPILE
2926
// Exercise heap for OOM-fault injection purposes
@@ -38,7 +35,6 @@ HRESULT CQuickMemoryBase<SIZE, INCREMENT>::ReSizeNoThrow(SIZE_T iItems)
3835
delete [] pTmp;
3936
}
4037
#endif
41-
#endif
4238
#endif
4339
BYTE *pbBuffNew;
4440
if (iItems <= cbTotal)
@@ -47,12 +43,10 @@ HRESULT CQuickMemoryBase<SIZE, INCREMENT>::ReSizeNoThrow(SIZE_T iItems)
4743
return NOERROR;
4844
}
4945

50-
#ifdef _BLD_CLR
5146
#ifndef DACCESS_COMPILE
5247
// not allowed to do allocation if current thread suspends EE
5348
if (IsSuspendEEThread ())
5449
return E_OUTOFMEMORY;
55-
#endif
5650
#endif
5751
pbBuffNew = NEW_NOTHROW(iItems + INCREMENT);
5852
if (!pbBuffNew)

src/coreclr/inc/corhlprpriv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class CQuickMemoryBase
8181
template <BOOL bGrow, BOOL bThrow>
8282
void *_Alloc(SIZE_T iItems)
8383
{
84-
#if defined(_BLD_CLR) && defined(_DEBUG)
84+
#if defined(_DEBUG)
8585
{ // Exercise heap for OOM-fault injection purposes
8686
BYTE * pb = NSQuickBytesHelper::_AllocBytes<bThrow>::Invoke(iItems);
8787
_ASSERTE(!bThrow || pb != NULL); // _AllocBytes would have thrown if bThrow == TRUE

src/coreclr/inc/ex.h

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,15 @@ Exception *ExThrowWithInnerHelper(Exception *inner);
827827
} \
828828
SCAN_EHMARKER_END_TRY(); \
829829
} \
830+
PAL_CPP_CATCH_NON_DERIVED_NOARG (const std::bad_alloc&) \
831+
{ \
832+
SCAN_EHMARKER_CATCH(); \
833+
__state.SetCaughtCxx(); \
834+
__state.m_pExceptionPtr = Exception::GetOOMException(); \
835+
SCAN_EHMARKER_END_CATCH(); \
836+
SCAN_IGNORE_THROW_MARKER; \
837+
ThrowOutOfMemory(); \
838+
} \
830839
PAL_CPP_CATCH_DERIVED (DerivedExceptionClass, __pExceptionRaw) \
831840
{ \
832841
SCAN_EHMARKER_CATCH(); \
@@ -862,18 +871,34 @@ Exception *ExThrowWithInnerHelper(Exception *inner);
862871
PAL_CPP_TRY \
863872
{ \
864873
SCAN_EHMARKER_TRY(); \
865-
CAutoTryCleanup<STATETYPE> __autoCleanupTry(__state); \
866-
/* prevent annotations from being dropped by optimizations in debug */ \
867-
INDEBUG(static bool __alwayszero;) \
868-
INDEBUG(VolatileLoad(&__alwayszero);) \
874+
SCAN_EHMARKER(); \
875+
PAL_CPP_TRY \
869876
{ \
870-
/* Disallow returns to make exception handling work. */ \
871-
/* Some work is done after the catch, see EX_ENDTRY. */ \
872-
DEBUG_ASSURE_NO_RETURN_BEGIN(EX_TRY) \
877+
SCAN_EHMARKER_TRY(); \
878+
CAutoTryCleanup<STATETYPE> __autoCleanupTry(__state); \
879+
/* prevent annotations from being dropped by optimizations in debug */ \
880+
INDEBUG(static bool __alwayszero;) \
881+
INDEBUG(VolatileLoad(&__alwayszero);) \
882+
{ \
883+
/* Disallow returns to make exception handling work. */ \
884+
/* Some work is done after the catch, see EX_ENDTRY. */ \
885+
DEBUG_ASSURE_NO_RETURN_BEGIN(EX_TRY) \
873886

874887
#define EX_CATCH_IMPL_CPP_ONLY \
875-
DEBUG_ASSURE_NO_RETURN_END(EX_TRY) \
888+
DEBUG_ASSURE_NO_RETURN_END(EX_TRY) \
889+
} \
890+
SCAN_EHMARKER_END_TRY(); \
891+
} \
892+
PAL_CPP_CATCH_NON_DERIVED_NOARG (const std::bad_alloc&) \
893+
{ \
894+
SCAN_EHMARKER_CATCH(); \
895+
__state.SetCaughtCxx(); \
896+
__state.m_pExceptionPtr = Exception::GetOOMException(); \
897+
SCAN_EHMARKER_END_CATCH(); \
898+
SCAN_IGNORE_THROW_MARKER; \
899+
ThrowOutOfMemory(); \
876900
} \
901+
PAL_CPP_ENDTRY \
877902
SCAN_EHMARKER_END_TRY(); \
878903
} \
879904
PAL_CPP_CATCH_DERIVED (Exception, __pExceptionRaw) \

0 commit comments

Comments
 (0)