Skip to content

Commit

Permalink
Use alternative mechanisms to allow us to use the default new/delete …
Browse files Browse the repository at this point in the history
…implementations (#101947)
  • Loading branch information
jkoritzinsky authored May 17, 2024
1 parent 84885d7 commit 4246ba1
Show file tree
Hide file tree
Showing 21 changed files with 75 additions and 214 deletions.
1 change: 0 additions & 1 deletion src/coreclr/clrdefinitions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ if(CLR_CMAKE_TARGET_LINUX_MUSL)
add_definitions(-DNO_FIXED_STACK_LIMIT)
endif(CLR_CMAKE_TARGET_LINUX_MUSL)

add_definitions(-D_BLD_CLR)
add_definitions(-DDEBUGGING_SUPPORTED)
add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:DAC_COMPONENT>>>:PROFILING_SUPPORTED>)
add_compile_definitions($<$<BOOL:$<TARGET_PROPERTY:DAC_COMPONENT>>:PROFILING_SUPPORTED_DATA>)
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/debug/inc/dbgipcevents.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#ifndef _DbgIPCEvents_h_
#define _DbgIPCEvents_h_

#include <new.hpp>
#include <new>
#include <cor.h>
#include <cordebug.h>
#include <corjit.h> // for ICorDebugInfo::VarLocType & VarLoc
Expand All @@ -25,6 +25,8 @@

#include "./common.h"

using std::nothrow;

//-----------------------------------------------------------------------------
// V3 additions to IPC protocol between LS and RS.
//-----------------------------------------------------------------------------
Expand Down
5 changes: 0 additions & 5 deletions src/coreclr/ilasm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,6 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
memset(wzOutputFilename,0,sizeof(wzOutputFilename));
memset(wzPdbFilename, 0, sizeof(wzPdbFilename));

#ifdef _DEBUG
DisableThrowCheck();
//CONTRACT_VIOLATION(ThrowsViolation);
#endif

if(argc < 2) goto ErrorExit;
#ifdef _PREFAST_
#pragma warning(push)
Expand Down
8 changes: 3 additions & 5 deletions src/coreclr/ildasm/windasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#include <clrversion.h>
#include "resource.h"

#include "new.hpp"
#include <new>

using std::nothrow;

#define MODE_DUMP_ALL 0
#define MODE_DUMP_CLASS 1
Expand Down Expand Up @@ -465,10 +467,6 @@ int main(int argc, char* argv[])
}
#endif

#ifdef _DEBUG
DisableThrowCheck();
#endif

int iCommandLineParsed = 0;
WCHAR* wzCommandLine = NULL;
char* szCommandLine = NULL;
Expand Down
6 changes: 5 additions & 1 deletion src/coreclr/inc/clrhost.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#ifndef __CLRHOST_H__
#define __CLRHOST_H__

#include <new>

#include "windows.h" // worth to include before mscoree.h so we are guaranteed to pick few definitions
#ifdef CreateSemaphore
#undef CreateSemaphore
Expand All @@ -16,7 +18,9 @@
#include "clrinternal.h"
#include "switches.h"
#include "holder.h"
#include "new.hpp"

using std::nothrow;

#include "staticcontract.h"
#include "predeftlsslot.h"
#include "safemath.h"
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/inc/corhlpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
****************************************************************************/
#ifndef SOS_INCLUDE

#ifdef _BLD_CLR
#include "utilcode.h"
#endif
#include "corhlpr.h"
#include <stdlib.h>

Expand Down
30 changes: 2 additions & 28 deletions src/coreclr/inc/corhlpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@
#include "corhdr.h"
#include "corerror.h"
#include "unreachable.h"
#include <new>

// This header is consumed both within the runtime and externally. In the former
// case we need to wrap memory allocations, in the latter there is no
// infrastructure to support this. Detect which way we're building and provide a
// very simple abstraction layer (handles allocating bytes only).
#ifdef _BLD_CLR
#include "new.hpp"

using std::nothrow;

#define NEW_NOTHROW(_bytes) new (nothrow) BYTE[_bytes]
#define NEW_THROWS(_bytes) new BYTE[_bytes]
Expand All @@ -38,27 +33,6 @@ inline void DECLSPEC_NORETURN THROW_OUT_OF_MEMORY()
{
ThrowOutOfMemory();
}
#else
#define NEW_NOTHROW(_bytes) new BYTE[_bytes]
#define NEW_THROWS(_bytes) __CorHlprNewThrows(_bytes)
static inline void DECLSPEC_NORETURN __CorHlprThrowOOM()
{
RaiseException(STATUS_NO_MEMORY, 0, 0, NULL);
__UNREACHABLE();
}
static inline BYTE *__CorHlprNewThrows(size_t bytes)
{
BYTE *pbMemory = new BYTE[bytes];
if (pbMemory == NULL)
__CorHlprThrowOOM();
return pbMemory;
}
inline void DECLSPEC_NORETURN THROW_OUT_OF_MEMORY()
{
__CorHlprThrowOOM();
}
#endif


//*****************************************************************************
// There are a set of macros commonly used in the helpers which you will want
Expand Down
6 changes: 0 additions & 6 deletions src/coreclr/inc/corhlprpriv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
****************************************************************************/
#ifndef SOS_INCLUDE

#ifdef _BLD_CLR
#include "utilcode.h"
#endif
#include "corhlprpriv.h"
#include <stdlib.h>

Expand All @@ -23,7 +21,6 @@
template <SIZE_T SIZE, SIZE_T INCREMENT>
HRESULT CQuickMemoryBase<SIZE, INCREMENT>::ReSizeNoThrow(SIZE_T iItems)
{
#ifdef _BLD_CLR
#ifdef _DEBUG
#ifndef DACCESS_COMPILE
// Exercise heap for OOM-fault injection purposes
Expand All @@ -38,7 +35,6 @@ HRESULT CQuickMemoryBase<SIZE, INCREMENT>::ReSizeNoThrow(SIZE_T iItems)
delete [] pTmp;
}
#endif
#endif
#endif
BYTE *pbBuffNew;
if (iItems <= cbTotal)
Expand All @@ -47,12 +43,10 @@ HRESULT CQuickMemoryBase<SIZE, INCREMENT>::ReSizeNoThrow(SIZE_T iItems)
return NOERROR;
}

#ifdef _BLD_CLR
#ifndef DACCESS_COMPILE
// not allowed to do allocation if current thread suspends EE
if (IsSuspendEEThread ())
return E_OUTOFMEMORY;
#endif
#endif
pbBuffNew = NEW_NOTHROW(iItems + INCREMENT);
if (!pbBuffNew)
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/inc/corhlprpriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class CQuickMemoryBase
template <BOOL bGrow, BOOL bThrow>
void *_Alloc(SIZE_T iItems)
{
#if defined(_BLD_CLR) && defined(_DEBUG)
#if defined(_DEBUG)
{ // Exercise heap for OOM-fault injection purposes
BYTE * pb = NSQuickBytesHelper::_AllocBytes<bThrow>::Invoke(iItems);
_ASSERTE(!bThrow || pb != NULL); // _AllocBytes would have thrown if bThrow == TRUE
Expand Down
41 changes: 33 additions & 8 deletions src/coreclr/inc/ex.h
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,15 @@ Exception *ExThrowWithInnerHelper(Exception *inner);
} \
SCAN_EHMARKER_END_TRY(); \
} \
PAL_CPP_CATCH_NON_DERIVED_NOARG (const std::bad_alloc&) \
{ \
SCAN_EHMARKER_CATCH(); \
__state.SetCaughtCxx(); \
__state.m_pExceptionPtr = Exception::GetOOMException(); \
SCAN_EHMARKER_END_CATCH(); \
SCAN_IGNORE_THROW_MARKER; \
ThrowOutOfMemory(); \
} \
PAL_CPP_CATCH_DERIVED (DerivedExceptionClass, __pExceptionRaw) \
{ \
SCAN_EHMARKER_CATCH(); \
Expand Down Expand Up @@ -862,18 +871,34 @@ Exception *ExThrowWithInnerHelper(Exception *inner);
PAL_CPP_TRY \
{ \
SCAN_EHMARKER_TRY(); \
CAutoTryCleanup<STATETYPE> __autoCleanupTry(__state); \
/* prevent annotations from being dropped by optimizations in debug */ \
INDEBUG(static bool __alwayszero;) \
INDEBUG(VolatileLoad(&__alwayszero);) \
SCAN_EHMARKER(); \
PAL_CPP_TRY \
{ \
/* Disallow returns to make exception handling work. */ \
/* Some work is done after the catch, see EX_ENDTRY. */ \
DEBUG_ASSURE_NO_RETURN_BEGIN(EX_TRY) \
SCAN_EHMARKER_TRY(); \
CAutoTryCleanup<STATETYPE> __autoCleanupTry(__state); \
/* prevent annotations from being dropped by optimizations in debug */ \
INDEBUG(static bool __alwayszero;) \
INDEBUG(VolatileLoad(&__alwayszero);) \
{ \
/* Disallow returns to make exception handling work. */ \
/* Some work is done after the catch, see EX_ENDTRY. */ \
DEBUG_ASSURE_NO_RETURN_BEGIN(EX_TRY) \

#define EX_CATCH_IMPL_CPP_ONLY \
DEBUG_ASSURE_NO_RETURN_END(EX_TRY) \
DEBUG_ASSURE_NO_RETURN_END(EX_TRY) \
} \
SCAN_EHMARKER_END_TRY(); \
} \
PAL_CPP_CATCH_NON_DERIVED_NOARG (const std::bad_alloc&) \
{ \
SCAN_EHMARKER_CATCH(); \
__state.SetCaughtCxx(); \
__state.m_pExceptionPtr = Exception::GetOOMException(); \
SCAN_EHMARKER_END_CATCH(); \
SCAN_IGNORE_THROW_MARKER; \
ThrowOutOfMemory(); \
} \
PAL_CPP_ENDTRY \
SCAN_EHMARKER_END_TRY(); \
} \
PAL_CPP_CATCH_DERIVED (Exception, __pExceptionRaw) \
Expand Down
18 changes: 0 additions & 18 deletions src/coreclr/inc/new.hpp

This file was deleted.

2 changes: 2 additions & 0 deletions src/coreclr/inc/palclr.h
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@
#define PAL_CPP_THROW(type, obj) do { SCAN_THROW_MARKER; throw obj; } while (false)
#define PAL_CPP_RETHROW do { SCAN_THROW_MARKER; throw; } while (false)
#define PAL_CPP_CATCH_DERIVED(type, obj) catch (type * obj)
#define PAL_CPP_CATCH_NON_DERIVED(type, obj) catch (type obj)
#define PAL_CPP_CATCH_NON_DERIVED_NOARG(type) catch (type)
#define PAL_CPP_CATCH_ALL catch (...)
#define PAL_CPP_CATCH_EXCEPTION_NOARG catch (Exception *)

Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/inc/utilcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#include <algorithm>
#include <stdio.h>
#include <limits.h>
#include <new>

using std::nothrow;

#include "crtwrap.h"
#include "winwrap.h"
Expand All @@ -29,7 +32,6 @@
#include "corhlprpriv.h"
#include "check.h"
#include "safemath.h"
#include "new.hpp"

#include "contract.h"

Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/md/external.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@

#include <clrtypes.h>
#include <safemath.h>
#include <new.hpp>
#include <new>

using std::nothrow;
2 changes: 2 additions & 0 deletions src/coreclr/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -4418,6 +4418,8 @@ class NativeExceptionHolderFactory
#define PAL_CPP_CATCH_EXCEPTION(ident) } catch (Exception *ident) {
#define PAL_CPP_CATCH_EXCEPTION_NOARG } catch (Exception *) {
#define PAL_CPP_CATCH_DERIVED(type, ident) } catch (type *ident) {
#define PAL_CPP_CATCH_NON_DERIVED(type, ident) } catch (type ident) {
#define PAL_CPP_CATCH_NON_DERIVED_NOARG(type) } catch (type) {
#define PAL_CPP_CATCH_ALL } catch (...) { \
try { throw; } \
catch (PAL_SEHException& ex) { ex.SecondPassDone(); } \
Expand Down
Loading

0 comments on commit 4246ba1

Please sign in to comment.