From 08d39ddf02c81c99bd49c19b808c855235cbabdc Mon Sep 17 00:00:00 2001 From: Jan Vorlicek Date: Tue, 3 Oct 2017 22:09:43 +0200 Subject: [PATCH] Fix build with latest Xcode on OSX (#14282) * Fix build with latest Xcode on OSX The latest Xcode 9 cannot successfully build CoreCLR PAL. There are several issues. First, it complains that min / max macros cannot be defined in C++ code, since they would collide with the std::min and std::max functions. Second, some of the headers that PAL includes pull in declarations of several template classes that we explicitly define in PAL and also the new operator declaration. To fix that, I have undefined the min and max macros for PAL and replaced their usage by the std::min / max functions. I have also removed the manual declaration of the colliding template classes and new operator and added inclusion of the proper C++ headers instead. The PAL was including non-pal safemath.h and to make this change compatible with it, I have added definition of USE_STL that makes safemath.h include type_trait from STL instead of our local trimmed copy. I have also removed some dead code that I have discovered during the process. Fixes #14279 * Fix build on ARM32 and very recent GLIBCXX --- src/pal/CMakeLists.txt | 1 + src/pal/src/cruntime/mbstring.cpp | 3 +- src/pal/src/cruntime/printf.cpp | 77 ----------------------------------- src/pal/src/cruntime/printfcpp.cpp | 77 ----------------------------------- src/pal/src/cruntime/wchar.cpp | 3 +- src/pal/src/exception/seh.cpp | 32 +-------------- src/pal/src/include/pal/cruntime.h | 18 -------- src/pal/src/include/pal/malloc.hpp | 4 +- src/pal/src/include/pal/palinternal.h | 8 ++++ src/pal/src/init/pal.cpp | 4 +- src/pal/src/misc/cgroup.cpp | 7 ++-- src/pal/src/misc/sysinfo.cpp | 21 +++++----- src/pal/src/numa/numa.cpp | 6 ++- src/pal/src/synchmgr/synchmanager.cpp | 10 +++-- 14 files changed, 44 insertions(+), 227 deletions(-) diff --git a/src/pal/CMakeLists.txt b/src/pal/CMakeLists.txt index c687d83..a510d25 100644 --- a/src/pal/CMakeLists.txt +++ b/src/pal/CMakeLists.txt @@ -9,6 +9,7 @@ include_directories(${COREPAL_SOURCE_DIR}/src) include_directories(${COREPAL_SOURCE_DIR}/../inc) add_compile_options(-fexceptions) +add_definitions(-DUSE_STL) add_subdirectory(src) diff --git a/src/pal/src/cruntime/mbstring.cpp b/src/pal/src/cruntime/mbstring.cpp index ace2aa5..828e4a3 100644 --- a/src/pal/src/cruntime/mbstring.cpp +++ b/src/pal/src/cruntime/mbstring.cpp @@ -31,6 +31,7 @@ Implementation Notes: #include "pal/palinternal.h" #include "pal/dbgmsg.h" +#include SET_DEFAULT_DEBUG_CHANNEL(CRT); @@ -125,7 +126,7 @@ _mbsninc( ret = (unsigned char *) string; if (GetCPInfo(CP_ACP, &cpinfo) && cpinfo.MaxCharSize == 1) { - ret += min(count, strlen((const char*)string)); + ret += std::min(count, strlen((const char*)string)); } else { diff --git a/src/pal/src/cruntime/printf.cpp b/src/pal/src/cruntime/printf.cpp index 72c7e11..6ae6263 100644 --- a/src/pal/src/cruntime/printf.cpp +++ b/src/pal/src/cruntime/printf.cpp @@ -47,83 +47,6 @@ static int SscanfFloatCheckExponent(LPCSTR buff, LPCSTR floatFmt, /******************************************************************************* Function: - Internal_AddPaddingA - -Parameters: - Out - - buffer to place padding and given string (In) - Count - - maximum chars to be copied so as not to overrun given buffer - In - - string to place into (Out) accompanied with padding - Padding - - number of padding chars to add - Flags - - padding style flags (PRINTF_FORMAT_FLAGS) -*******************************************************************************/ -BOOL Internal_AddPaddingA(LPSTR *Out, INT Count, LPSTR In, - INT Padding, INT Flags) -{ - LPSTR OutOriginal = *Out; - INT PaddingOriginal = Padding; - INT LengthInStr; - LengthInStr = strlen(In); - - - if (Padding < 0) - { - /* this is used at the bottom to determine if the buffer ran out */ - PaddingOriginal = 0; - } - if (Flags & PFF_MINUS) /* pad on right */ - { - if (strncpy_s(*Out, Count, In, min(LengthInStr + 1, Count)) != SAFECRT_SUCCESS) - { - return FALSE; - } - - *Out += min(LengthInStr, Count); - } - if (Padding > 0) - { - if (Flags & PFF_ZERO) /* '0', pad with zeros */ - { - while (Padding-- && Count > *Out - OutOriginal) - { - *(*Out)++ = '0'; - } - } - else /* pad left with spaces */ - { - while (Padding-- && Count > *Out - OutOriginal) - { - *(*Out)++ = ' '; - } - } - } - if (!(Flags & PFF_MINUS)) /* put 'In' after padding */ - { - if (strncpy_s(*Out, Count, In, - min(LengthInStr + 1, Count - (*Out - OutOriginal))) != SAFECRT_SUCCESS) - { - return FALSE; - } - - *Out += min(LengthInStr, Count - (*Out - OutOriginal)); - } - - if (LengthInStr + PaddingOriginal > Count) - { - return FALSE; - } - else - { - return TRUE; - } -} - -/******************************************************************************* -Function: PAL_printf_arg_remover Parameters: diff --git a/src/pal/src/cruntime/printfcpp.cpp b/src/pal/src/cruntime/printfcpp.cpp index 0b90721..662561b 100644 --- a/src/pal/src/cruntime/printfcpp.cpp +++ b/src/pal/src/cruntime/printfcpp.cpp @@ -776,83 +776,6 @@ BOOL Internal_ExtractFormatW(CPalThread *pthrCurrent, LPCWSTR *Fmt, LPSTR Out, L /******************************************************************************* Function: - Internal_AddPaddingW - -Parameters: - Out - - buffer to place padding and given string (In) - Count - - maximum chars to be copied so as not to overrun given buffer - In - - string to place into (Out) accompanied with padding - Padding - - number of padding chars to add - Flags - - padding style flags (PRINTF_FORMAT_FLAGS) -*******************************************************************************/ - -BOOL Internal_AddPaddingW(LPWSTR *Out, INT Count, LPWSTR In, INT Padding, INT Flags) -{ - LPWSTR OutOriginal = *Out; - INT PaddingOriginal = Padding; - INT LengthInStr; - LengthInStr = PAL_wcslen(In); - - - if (Padding < 0) - { - /* this is used at the bottom to determine if the buffer ran out */ - PaddingOriginal = 0; - } - if (Flags & PFF_MINUS) /* pad on right */ - { - if (wcsncpy_s(*Out, Count, In, min(LengthInStr + 1, Count - 1)) != SAFECRT_SUCCESS) - { - return FALSE; - } - - *Out += min(LengthInStr, Count - 1); - } - if (Padding > 0) - { - if (Flags & PFF_ZERO) /* '0', pad with zeros */ - { - while (Padding-- && Count > *Out - OutOriginal) - { - *(*Out)++ = '0'; - } - } - else /* pad left with spaces */ - { - while (Padding-- && Count > *Out - OutOriginal) - { - *(*Out)++ = ' '; - } - } - } - if (!(Flags & PFF_MINUS)) /* put 'In' after padding */ - { - if (wcsncpy_s(*Out, Count - (*Out - OutOriginal), In, - min(LengthInStr, Count - (*Out - OutOriginal) - 1)) != SAFECRT_SUCCESS) - { - return FALSE; - } - - *Out += min(LengthInStr, Count - (*Out - OutOriginal) - 1); - } - - if (LengthInStr + PaddingOriginal > Count - 1) - { - return FALSE; - } - else - { - return TRUE; - } -} - -/******************************************************************************* -Function: Internal_AddPaddingVfprintf Parameters: diff --git a/src/pal/src/cruntime/wchar.cpp b/src/pal/src/cruntime/wchar.cpp index 5b46696..c93a3d5 100644 --- a/src/pal/src/cruntime/wchar.cpp +++ b/src/pal/src/cruntime/wchar.cpp @@ -41,6 +41,7 @@ Abstract: #endif #include +#include SET_DEFAULT_DEBUG_CHANNEL(CRT); @@ -1237,7 +1238,7 @@ PAL_wcsncpy( wchar_16 * strDest, const wchar_16 *strSource, size_t count ) strDest, strSource, strSource, (unsigned long) count); memset( strDest, 0, length ); - length = min( count, PAL_wcslen( strSource ) ) * sizeof( wchar_16 ); + length = std::min( count, PAL_wcslen( strSource ) ) * sizeof( wchar_16 ); memcpy( strDest, strSource, length ); LOGEXIT("wcsncpy returning (wchar_16*): %p\n", strDest); diff --git a/src/pal/src/exception/seh.cpp b/src/pal/src/exception/seh.cpp index 2d1c182..9f5f074 100644 --- a/src/pal/src/exception/seh.cpp +++ b/src/pal/src/exception/seh.cpp @@ -39,37 +39,7 @@ Abstract: #include #include #include - -// Define the std::move so that we don't have to include the header -// which on some platforms pulls in STL stuff that collides with PAL stuff. -// The std::move is needed to enable using move constructor and assignment operator -// for PAL_SEHException. -namespace std -{ - template - struct remove_reference - { - typedef T type; - }; - - template - struct remove_reference - { - typedef T type; - }; - - template - struct remove_reference - { - typedef T type; - }; - - template inline - typename remove_reference::type&& move(T&& arg) - { // forward arg as movable - return ((typename remove_reference::type&&)arg); - } -} +#include using namespace CorUnix; diff --git a/src/pal/src/include/pal/cruntime.h b/src/pal/src/include/pal/cruntime.h index 65bf33c..3b3fa7b 100644 --- a/src/pal/src/include/pal/cruntime.h +++ b/src/pal/src/include/pal/cruntime.h @@ -98,24 +98,6 @@ typedef enum /******************************************************************************* Function: - Internal_AddPaddingA - -Parameters: - Out - - buffer to place padding and given string (In) - Count - - maximum chars to be copied so as not to overrun given buffer - In - - string to place into (Out) accompanied with padding - Padding - - number of padding chars to add - Flags - - padding style flags (PRINTF_FORMAT_FLAGS) -*******************************************************************************/ -BOOL Internal_AddPaddingA(LPSTR *Out, INT Count, LPSTR In, INT Padding, INT Flags); - -/******************************************************************************* -Function: PAL_printf_arg_remover Parameters: diff --git a/src/pal/src/include/pal/malloc.hpp b/src/pal/src/include/pal/malloc.hpp index c733341..9faf427 100644 --- a/src/pal/src/include/pal/malloc.hpp +++ b/src/pal/src/include/pal/malloc.hpp @@ -25,6 +25,7 @@ Abstract: #include #include +#include extern "C" { @@ -54,9 +55,6 @@ extern "C" ); } -inline void* operator new(size_t, void* p) throw () { return p; } -inline void* operator new[](size_t, void* p) throw () { return p; } - namespace CorUnix{ void * diff --git a/src/pal/src/include/pal/palinternal.h b/src/pal/src/include/pal/palinternal.h index 12312a3..abebcb5 100644 --- a/src/pal/src/include/pal/palinternal.h +++ b/src/pal/src/include/pal/palinternal.h @@ -153,6 +153,11 @@ function_name() to call the system's implementation #define _ENABLE_DEBUG_MESSAGES_ 0 #endif +/* Include type_traits before including the pal.h. On newer glibcxx versions, + the type_traits fail to compile if we redefine the wchar_t before including + the header */ +#include + #ifdef PAL_PERF #include "pal_perf.h" #endif @@ -534,6 +539,9 @@ function_name() to call the system's implementation #undef ctime +#undef min +#undef max + #undef SCHAR_MIN #undef SCHAR_MAX #undef UCHAR_MAX diff --git a/src/pal/src/init/pal.cpp b/src/pal/src/init/pal.cpp index 37c1677..996292e 100644 --- a/src/pal/src/init/pal.cpp +++ b/src/pal/src/init/pal.cpp @@ -83,6 +83,8 @@ int CacheLineSize; #include #endif +#include + using namespace CorUnix; // @@ -218,7 +220,7 @@ InitializeDefaultStackSize() if (errno == 0) { - g_defaultStackSize = max(size, PTHREAD_STACK_MIN); + g_defaultStackSize = std::max(size, (long int)PTHREAD_STACK_MIN); } } diff --git a/src/pal/src/misc/cgroup.cpp b/src/pal/src/misc/cgroup.cpp index ec0a0bd..7a3a926 100644 --- a/src/pal/src/misc/cgroup.cpp +++ b/src/pal/src/misc/cgroup.cpp @@ -17,6 +17,7 @@ SET_DEFAULT_DEBUG_CHANNEL(MISC); #include "pal/palinternal.h" #include #include "pal/virtual.h" +#include #define PROC_MOUNTINFO_FILENAME "/proc/self/mountinfo" #define PROC_CGROUP_FILENAME "/proc/self/cgroup" @@ -362,7 +363,7 @@ PAL_GetRestrictedPhysicalMemoryLimit() { rlimit_soft_limit = curr_rlimit.rlim_cur; } - physical_memory_limit = min(physical_memory_limit, rlimit_soft_limit); + physical_memory_limit = std::min(physical_memory_limit, rlimit_soft_limit); // Ensure that limit is not greater than real memory size long pages = sysconf(_SC_PHYS_PAGES); @@ -371,8 +372,8 @@ PAL_GetRestrictedPhysicalMemoryLimit() long pageSize = sysconf(_SC_PAGE_SIZE); if (pageSize != -1) { - physical_memory_limit = min(physical_memory_limit, - (size_t)pages * pageSize); + physical_memory_limit = std::min(physical_memory_limit, + (size_t)(pages * pageSize)); } } diff --git a/src/pal/src/misc/sysinfo.cpp b/src/pal/src/misc/sysinfo.cpp index a06f4b7..cb5dda6 100644 --- a/src/pal/src/misc/sysinfo.cpp +++ b/src/pal/src/misc/sysinfo.cpp @@ -81,6 +81,7 @@ Revision History: #include "pal/dbgmsg.h" +#include SET_DEFAULT_DEBUG_CHANNEL(MISC); @@ -437,16 +438,16 @@ PAL_GetLogicalProcessorCacheSizeFromOS() size_t cacheSize = 0; #ifdef _SC_LEVEL1_DCACHE_SIZE - cacheSize = max(cacheSize, sysconf(_SC_LEVEL1_DCACHE_SIZE)); + cacheSize = std::max(cacheSize, (size_t)sysconf(_SC_LEVEL1_DCACHE_SIZE)); #endif #ifdef _SC_LEVEL2_CACHE_SIZE - cacheSize = max(cacheSize, sysconf(_SC_LEVEL2_CACHE_SIZE)); + cacheSize = std::max(cacheSize, (size_t)sysconf(_SC_LEVEL2_CACHE_SIZE)); #endif #ifdef _SC_LEVEL3_CACHE_SIZE - cacheSize = max(cacheSize, sysconf(_SC_LEVEL3_CACHE_SIZE)); + cacheSize = std::max(cacheSize, (size_t)sysconf(_SC_LEVEL3_CACHE_SIZE)); #endif #ifdef _SC_LEVEL4_CACHE_SIZE - cacheSize = max(cacheSize, sysconf(_SC_LEVEL4_CACHE_SIZE)); + cacheSize = std::max(cacheSize, (size_t)sysconf(_SC_LEVEL4_CACHE_SIZE)); #endif #if defined(_ARM64_) @@ -455,15 +456,15 @@ PAL_GetLogicalProcessorCacheSizeFromOS() size_t size; if(ReadMemoryValueFromFile("/sys/devices/system/cpu/cpu0/cache/index0/size", &size)) - cacheSize = max(cacheSize, size); + cacheSize = std::max(cacheSize, size); if(ReadMemoryValueFromFile("/sys/devices/system/cpu/cpu0/cache/index1/size", &size)) - cacheSize = max(cacheSize, size); + cacheSize = std::max(cacheSize, size); if(ReadMemoryValueFromFile("/sys/devices/system/cpu/cpu0/cache/index2/size", &size)) - cacheSize = max(cacheSize, size); + cacheSize = std::max(cacheSize, size); if(ReadMemoryValueFromFile("/sys/devices/system/cpu/cpu0/cache/index3/size", &size)) - cacheSize = max(cacheSize, size); + cacheSize = std::max(cacheSize, size); if(ReadMemoryValueFromFile("/sys/devices/system/cpu/cpu0/cache/index4/size", &size)) - cacheSize = max(cacheSize, size); + cacheSize = std::max(cacheSize, size); } if(cacheSize == 0) @@ -488,7 +489,7 @@ PAL_GetLogicalProcessorCacheSizeFromOS() // Assume L3$/CPU grows linearly from 256K to 1.5M/CPU as logicalCPUs grows from 2 to 12 CPUs DWORD logicalCPUs = PAL_GetLogicalCpuCountFromOS(); - cacheSize = logicalCPUs*min(1536, max(256, logicalCPUs*128))*1024; + cacheSize = logicalCPUs*std::min(1536, std::max(256, logicalCPUs*128))*1024; } #endif diff --git a/src/pal/src/numa/numa.cpp b/src/pal/src/numa/numa.cpp index 383fb0e..6e4593d 100644 --- a/src/pal/src/numa/numa.cpp +++ b/src/pal/src/numa/numa.cpp @@ -32,6 +32,8 @@ SET_DEFAULT_DEBUG_CHANNEL(NUMA); #include #include +#include + #include "numashim.h" using namespace CorUnix; @@ -630,7 +632,7 @@ SetThreadAffinityMask( if (st == 0) { - for (int i = 0; i < min(8 * sizeof(KAFFINITY), g_possibleCpuCount); i++) + for (int i = 0; i < std::min(8 * (int)sizeof(KAFFINITY), g_possibleCpuCount); i++) { if (CPU_ISSET(i, &prevCpuSet)) { @@ -978,4 +980,4 @@ SetThreadIdealProcessorEx( PERF_EXIT(SetThreadIdealProcessorEx); return success; -} \ No newline at end of file +} diff --git a/src/pal/src/synchmgr/synchmanager.cpp b/src/pal/src/synchmgr/synchmanager.cpp index 048ea3e..349b3de 100644 --- a/src/pal/src/synchmgr/synchmanager.cpp +++ b/src/pal/src/synchmgr/synchmanager.cpp @@ -39,6 +39,10 @@ SET_DEFAULT_DEBUG_CHANNEL(SYNC); // some headers have code with asserts, so do t #include "pal/fakepoll.h" #endif // HAVE_POLL +#include + +const int CorUnix::CThreadSynchronizationInfo::PendingSignalingsArraySize; + // We use the synchronization manager's worker thread to handle // process termination requests. It does so by calling the // registered handler function. @@ -4146,7 +4150,7 @@ namespace CorUnix ERROR("Failed creating thread synchronization mutex [error=%d (%s)]\n", iRet, strerror(iRet)); if (EAGAIN == iRet && MaxUnavailableResourceRetries >= ++iEagains) { - poll(NULL, 0, min(100,10*iEagains)); + poll(NULL, 0, std::min(100,10*iEagains)); goto Mutex_retry; } else if (ENOMEM == iRet) @@ -4172,7 +4176,7 @@ namespace CorUnix "[error=%d (%s)]\n", iRet, strerror(iRet)); if (EAGAIN == iRet && MaxUnavailableResourceRetries >= ++iEagains) { - poll(NULL, 0, min(100,10*iEagains)); + poll(NULL, 0, std::min(100,10*iEagains)); goto Cond_retry; } else if (ENOMEM == iRet) @@ -4361,7 +4365,7 @@ namespace CorUnix if (0 < m_lPendingSignalingCount) { - LONG lArrayPendingSignalingCount = min(PendingSignalingsArraySize, m_lPendingSignalingCount); + LONG lArrayPendingSignalingCount = std::min(PendingSignalingsArraySize, m_lPendingSignalingCount); LONG lIdx = 0; PAL_ERROR palTempErr; -- 2.7.4