Skip to content

Commit

Permalink
Remove remaining CRT PAL wrappers and enable including standard heade…
Browse files Browse the repository at this point in the history
…rs in the CoreCLR build (dotnet#98336)

- Remove malloc-family PAL and update callsites that might get passed 0 to bump up to 1.
- Move `getenv` usage to use the `PAL` function directly when on non-Windows (to preserve the existing behavior).
- Remove other remaining CRT PAL shims
- Remove header shims and enable building with the CRT and STL headers across the product, with various build fixes required (mostly around using the standard min/max definitions)
  • Loading branch information
jkoritzinsky authored Apr 4, 2024
1 parent 01f039c commit 5c4e2a3
Show file tree
Hide file tree
Showing 240 changed files with 559 additions and 5,127 deletions.
1 change: 0 additions & 1 deletion eng/native/configurecompiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ if (CLR_CMAKE_HOST_UNIX)
add_compile_options(-Wno-null-conversion)
add_compile_options(-glldb)
else()
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Werror=conversion-null>)
add_compile_options(-g)
endif()
endif()
Expand Down
6 changes: 6 additions & 0 deletions eng/native/functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ endfunction(convert_to_absolute_path)
function(preprocess_file inputFilename outputFilename)
get_compile_definitions(PREPROCESS_DEFINITIONS)
get_include_directories(PREPROCESS_INCLUDE_DIRECTORIES)
get_source_file_property(SOURCE_FILE_DEFINITIONS ${inputFilename} COMPILE_DEFINITIONS)

foreach(DEFINITION IN LISTS SOURCE_FILE_DEFINITIONS)
list(APPEND PREPROCESS_DEFINITIONS -D${DEFINITION})
endforeach()

if (MSVC)
add_custom_command(
OUTPUT ${outputFilename}
Expand Down
13 changes: 7 additions & 6 deletions src/coreclr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,12 @@ if(CLR_CMAKE_HOST_UNIX)
add_subdirectory(debug/createdump)
endif(CLR_CMAKE_HOST_OSX OR (CLR_CMAKE_HOST_LINUX AND NOT CLR_CMAKE_HOST_UNIX_X86 AND NOT CLR_CMAKE_HOST_ANDROID))

# Include the dummy c++ include files
include_directories("pal/inc/rt/cpp")

# This prevents inclusion of standard C compiler headers
add_compile_options(-nostdinc)
# The CoreCLR PAL used to redefine NULL, which caused a number of null conversion and arithmetic
# warnings and errors to be suppressed.
# Suppress these warnings here to avoid breaking the build.
add_compile_options($<$<COMPILE_LANG_AND_ID:CXX,Clang,AppleClang>:-Wno-null-arithmetic>)
add_compile_options($<$<COMPILE_LANG_AND_ID:CXX,GNU>:-Wno-conversion-null>)
add_compile_options($<$<COMPILE_LANG_AND_ID:CXX,GNU>:-Wno-pointer-arith>)

set (NATIVE_RESOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/nativeresources)
include_directories(${NATIVE_RESOURCE_DIR})
Expand All @@ -218,7 +219,7 @@ if(CLR_CMAKE_HOST_UNIX)
# given Windows .rc file. The target C++ file path is returned in the
# variable specified by the TARGET_FILE parameter.
function(build_resources SOURCE TARGET_NAME TARGET_FILE)

set_property(SOURCE ${SOURCE} APPEND PROPERTY COMPILE_DEFINITIONS "RC_INVOKED")
set(PREPROCESSED_SOURCE ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.rc.i)

preprocess_file(${SOURCE} ${PREPROCESSED_SOURCE})
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/binder/assemblyname.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
//
// ============================================================

#include "common.h"
#include "assemblyname.hpp"
#include "assemblybindercommon.hpp"

#include "common.h"
#include "utils.hpp"

#include "textualidentityparser.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/clrdefinitions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ if(CLR_CMAKE_HOST_WIN32)
add_definitions(-D_WIN32_WINNT=0x0602)
add_definitions(-DWIN32_LEAN_AND_MEAN)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_compile_definitions(NOMINMAX)
endif(CLR_CMAKE_HOST_WIN32)

if (NOT (CLR_CMAKE_TARGET_ARCH_I386 AND CLR_CMAKE_TARGET_UNIX))
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/debug/createdump/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ else(CLR_CMAKE_HOST_WIN32)
endif(CLR_CMAKE_HOST_OSX)
endif (CORECLR_SET_RPATH)

add_definitions(-DPAL_STDCPP_COMPAT)

# This is so we can include "version.c"
include_directories(${CMAKE_BINARY_DIR})

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/debug/daccess/daccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5793,7 +5793,7 @@ ClrDataAccess::RawGetMethodName(
SIZE_T maxPrecodeSize = sizeof(StubPrecode);

#ifdef HAS_THISPTR_RETBUF_PRECODE
maxPrecodeSize = max(maxPrecodeSize, sizeof(ThisPtrRetBufPrecode));
maxPrecodeSize = max((size_t)maxPrecodeSize, sizeof(ThisPtrRetBufPrecode));
#endif

for (SIZE_T i = 0; i < maxPrecodeSize / PRECODE_ALIGNMENT; i++)
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/debug/dbgutil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ if(CLR_CMAKE_HOST_WIN32 OR CLR_CMAKE_HOST_OSX)
include_directories(${CLR_DIR}/inc/llvm)
endif(CLR_CMAKE_HOST_WIN32 OR CLR_CMAKE_HOST_OSX)

add_definitions(-DPAL_STDCPP_COMPAT)

if(CLR_CMAKE_TARGET_LINUX_MUSL)
add_definitions(-DTARGET_LINUX_MUSL)
endif(CLR_CMAKE_TARGET_LINUX_MUSL)
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/debug/debug-pal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ include_directories(../inc)
include_directories(../../pal/inc)
include_directories(${EP_GENERATED_HEADER_PATH})

add_definitions(-DPAL_STDCPP_COMPAT)

set(SHARED_EVENTPIPE_SOURCE_PATH ${CLR_SRC_NATIVE_DIR}/eventpipe)
add_definitions(-DFEATURE_CORECLR)
add_definitions(-DFEATURE_PERFTRACING)
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/debug/di/rspriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -3975,9 +3975,9 @@ class CordbProcess :

// CORDB_ADDRESS's are UINT_PTR's (64 bit under HOST_64BIT, 32 bit otherwise)
#if defined(TARGET_64BIT)
#define MAX_ADDRESS (_UI64_MAX)
#define MAX_ADDRESS (UINT64_MAX)
#else
#define MAX_ADDRESS (_UI32_MAX)
#define MAX_ADDRESS (UINT32_MAX)
#endif
#define MIN_ADDRESS (0x0)
CORDB_ADDRESS m_minPatchAddr; //smallest patch in table
Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/debug/di/rsthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5122,7 +5122,7 @@ HRESULT CordbValueEnum::Next(ULONG celt, ICorDebugValue *values[], ULONG *pceltF

HRESULT hr = S_OK;

int iMax = min( m_iMax, m_iCurrent+celt);
int iMax = (int)min( (ULONG)m_iMax, m_iCurrent+celt);
int i;
for (i = m_iCurrent; i< iMax;i++)
{
Expand Down Expand Up @@ -8186,7 +8186,7 @@ HRESULT CordbJITILFrame::FabricateNativeInfo(DWORD dwIndex,
// first argument, but thereafter we have to decrement it
// before getting the variable's location from it. So increment
// it here to be consistent later.
rpCur += max(cbType, cbArchitectureMin);
rpCur += max((ULONG)cbType, cbArchitectureMin);
#endif

// Grab the IL code's function's method signature so we can see if it's static.
Expand Down Expand Up @@ -8219,7 +8219,7 @@ HRESULT CordbJITILFrame::FabricateNativeInfo(DWORD dwIndex,
IfFailThrow(pArgType->GetUnboxedObjectSize(&cbType));

#if defined(TARGET_X86) // STACK_GROWS_DOWN_ON_ARGS_WALK
rpCur -= max(cbType, cbArchitectureMin);
rpCur -= max((ULONG)cbType, cbArchitectureMin);
m_rgNVI[i].loc.vlFixedVarArg.vlfvOffset =
(unsigned)(m_FirstArgAddr - rpCur);

Expand All @@ -8229,7 +8229,7 @@ HRESULT CordbJITILFrame::FabricateNativeInfo(DWORD dwIndex,
#else // STACK_GROWS_UP_ON_ARGS_WALK
m_rgNVI[i].loc.vlFixedVarArg.vlfvOffset =
(unsigned)(rpCur - m_FirstArgAddr);
rpCur += max(cbType, cbArchitectureMin);
rpCur += max((ULONG)cbType, cbArchitectureMin);
AlignAddressForType(pArgType, rpCur);
#endif

Expand Down Expand Up @@ -10877,7 +10877,7 @@ HRESULT CordbCodeEnum::Next(ULONG celt, ICorDebugCode *values[], ULONG *pceltFet

HRESULT hr = S_OK;

int iMax = min( m_iMax, m_iCurrent+celt);
int iMax = (int)min( (ULONG)m_iMax, m_iCurrent+celt);
int i;

for (i = m_iCurrent; i < iMax; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/debug/di/rstype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2898,7 +2898,7 @@ HRESULT CordbTypeEnum::Next(ULONG celt, ICorDebugType *values[], ULONG *pceltFet

HRESULT hr = S_OK;

int iMax = min( m_iMax, m_iCurrent+celt);
int iMax = (int)min( (ULONG)m_iMax, m_iCurrent+celt);
int i;

for (i = m_iCurrent; i < iMax; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/debug/di/shimcallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ HRESULT ShimProxyCallback::DataBreakpoint(ICorDebugProcess* pProcess, ICorDebugT
this->m_pThread.Assign(pThread);

_ASSERTE(contextSize == sizeof(CONTEXT));
this->m_contextSize = min(contextSize, sizeof(CONTEXT));
this->m_contextSize = min(contextSize, (ULONG32)sizeof(CONTEXT));
memcpy(&(this->m_context), pContext, this->m_contextSize);
}

Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/debug/di/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include <stdio.h>
#include <windows.h>
#include <winnt.h>
#include <algorithm>
using std::min;
using std::max;

#include <dbgtargetcontext.h>

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/debug/ee/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3029,7 +3029,7 @@ HRESULT Debugger::GetILToNativeMappingIntoArrays(
if (pDJI == NULL)
return E_FAIL;

ULONG32 cMap = min(cMapMax, pDJI->GetSequenceMapCount());
ULONG32 cMap = min((ULONG32)cMapMax, pDJI->GetSequenceMapCount());
DebuggerILToNativeMap * rgMapInt = pDJI->GetSequenceMap();

NewArrayHolder<UINT> rguiILOffsetTemp = new (nothrow) UINT[cMap];
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/debug/ee/funceval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2806,7 +2806,7 @@ void PackArgumentArray(DebuggerEval *pDE,

#ifdef FEATURE_HFA
// The buffer for HFAs has to be always ENREGISTERED_RETURNTYPE_MAXSIZE
size = max(size, ENREGISTERED_RETURNTYPE_MAXSIZE);
size = max(size, (unsigned)ENREGISTERED_RETURNTYPE_MAXSIZE);
#endif

BYTE * pTemp = new (interopsafe) BYTE[ALIGN_UP(sizeof(ValueClassInfo), 8) + size];
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/debug/ee/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <stdint.h>
#include <wchar.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>

#include <windows.h>

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/debug/inc/dbgipcevents.h
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ class MSLAYOUT VMPTR_Base
//
// Operators to emulate Pointer semantics.
//
bool IsNull() { SUPPORTS_DAC; return m_addr == NULL; }
bool IsNull() { SUPPORTS_DAC; return m_addr == (TADDR)0; }

static VMPTR_This NullPtr()
{
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/debug/shared/dbgtransportsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1949,7 +1949,7 @@ void DbgTransportSession::TransportWorker()
DWORD cbBytesToRead = sReceiveHeader.TypeSpecificData.MemoryAccess.m_cbLeftSideBuffer;
while (cbBytesToRead)
{
DWORD cbTransfer = min(cbBytesToRead, sizeof(rgDummy));
DWORD cbTransfer = min(cbBytesToRead, (DWORD)sizeof(rgDummy));
if (!ReceiveBlock(rgDummy, cbTransfer))
HANDLE_TRANSIENT_ERROR();
cbBytesToRead -= cbTransfer;
Expand Down
8 changes: 0 additions & 8 deletions src/coreclr/dlls/mscordac/mscordac_unixexports.src
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ nativeStringResourceTable_mscorrc
; All the # exports are prefixed with DAC_
#PAL_CatchHardwareExceptionHolderEnter
#PAL_CatchHardwareExceptionHolderExit
#PAL_bsearch
#PAL_CopyModuleData
#PAL_errno
#PAL_free
#PAL_GetLogicalCpuCountFromOS
#PAL_GetTotalCpuCount
#PAL_GetUnwindInfoSize
#PAL_stdout
#PAL_stderr
#PAL_GetApplicationGroupId
#PAL_GetTransportName
#PAL_GetCurrentThread
Expand All @@ -47,9 +42,6 @@ nativeStringResourceTable_mscorrc
#PAL_ReadProcessMemory
#PAL_ProbeMemory
#PAL_Random
#PAL_malloc
#PAL_realloc
#PAL_qsort
#PAL__wcstoui64
#PAL_wcstoul
#PAL_wcstod
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/dlls/mscorpe/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <assert.h>
#include <stdio.h>
#include <stddef.h>
#include <algorithm>

#define FEATURE_NO_HOST // Do not use host interface
#include <utilcode.h>
Expand All @@ -21,3 +22,6 @@
#include "ceegen.h"
#include "ceefilegenwriter.h"
#include "ceesectionstring.h"

using std::min;
using std::max;
3 changes: 3 additions & 0 deletions src/coreclr/gc/env/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include <math.h>

#include <new>
#include <type_traits>
#include <limits>
#include <algorithm>

#ifdef TARGET_UNIX
#include <pthread.h>
Expand Down
9 changes: 0 additions & 9 deletions src/coreclr/gc/env/gcenv.base.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,6 @@ inline HRESULT HRESULT_FROM_WIN32(unsigned long x)

#define ZeroMemory(Destination,Length) memset((Destination),0,(Length))

#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif

#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif

#define C_ASSERT(cond) static_assert( cond, #cond )

#define UNREFERENCED_PARAMETER(P) (void)(P)
Expand Down Expand Up @@ -393,7 +385,6 @@ typedef struct _PROCESSOR_NUMBER {
uint8_t Number;
uint8_t Reserved;
} PROCESSOR_NUMBER, *PPROCESSOR_NUMBER;

#endif // _INC_WINDOWS

// -----------------------------------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 5c4e2a3

Please sign in to comment.