Fix build with latest Xcode on OSX (#14282)
authorJan Vorlicek <janvorli@microsoft.com>
Tue, 3 Oct 2017 20:09:43 +0000 (22:09 +0200)
committerGitHub <noreply@github.com>
Tue, 3 Oct 2017 20:09:43 +0000 (22:09 +0200)
* 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

14 files changed:
src/pal/CMakeLists.txt
src/pal/src/cruntime/mbstring.cpp
src/pal/src/cruntime/printf.cpp
src/pal/src/cruntime/printfcpp.cpp
src/pal/src/cruntime/wchar.cpp
src/pal/src/exception/seh.cpp
src/pal/src/include/pal/cruntime.h
src/pal/src/include/pal/malloc.hpp
src/pal/src/include/pal/palinternal.h
src/pal/src/init/pal.cpp
src/pal/src/misc/cgroup.cpp
src/pal/src/misc/sysinfo.cpp
src/pal/src/numa/numa.cpp
src/pal/src/synchmgr/synchmanager.cpp

index c687d83..a510d25 100644 (file)
@@ -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)
 
index ace2aa5..828e4a3 100644 (file)
@@ -31,6 +31,7 @@ Implementation Notes:
 #include "pal/palinternal.h"
 #include "pal/dbgmsg.h"
 
+#include <algorithm>
 
 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
         {
index 72c7e11..6ae6263 100644 (file)
@@ -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:
index 0b90721..662561b 100644 (file)
@@ -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:
index 5b46696..c93a3d5 100644 (file)
@@ -41,6 +41,7 @@ Abstract:
 #endif
 
 #include <errno.h>
+#include <algorithm>
 
 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);
index 2d1c182..9f5f074 100644 (file)
@@ -39,37 +39,7 @@ Abstract:
 #include <unistd.h>
 #include <pthread.h>
 #include <stdlib.h>
-
-// Define the std::move so that we don't have to include the <utility> 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<typename T>
-    struct remove_reference
-    {
-        typedef T type;
-    };
-
-    template<typename T>
-    struct remove_reference<T&>
-    {
-        typedef T type;
-    };
-
-    template<typename T>
-    struct remove_reference<T&&>
-    {
-        typedef T type;
-    };
-
-    template<class T> inline
-    typename remove_reference<T>::type&& move(T&& arg)
-    {   // forward arg as movable
-        return ((typename remove_reference<T>::type&&)arg);
-    }
-}
+#include <utility>
 
 using namespace CorUnix;
 
index 65bf33c..3b3fa7b 100644 (file)
@@ -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:
index c733341..9faf427 100644 (file)
@@ -25,6 +25,7 @@ Abstract:
 
 #include <stdarg.h>
 #include <stdlib.h>
+#include <new>
 
 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 *
index 12312a3..abebcb5 100644 (file)
@@ -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 <type_traits>
+
 #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
index 37c1677..996292e 100644 (file)
@@ -83,6 +83,8 @@ int CacheLineSize;
 #include <kvm.h>
 #endif
 
+#include <algorithm>
+
 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);
         }
     }
 
index ec0a0bd..7a3a926 100644 (file)
@@ -17,6 +17,7 @@ SET_DEFAULT_DEBUG_CHANNEL(MISC);
 #include "pal/palinternal.h"
 #include <sys/resource.h>
 #include "pal/virtual.h"
+#include <algorithm>
 
 #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));
         }
     }
 
index a06f4b7..cb5dda6 100644 (file)
@@ -81,6 +81,7 @@ Revision History:
 
 #include "pal/dbgmsg.h"
 
+#include <algorithm>
 
 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
 
index 383fb0e..6e4593d 100644 (file)
@@ -32,6 +32,8 @@ SET_DEFAULT_DEBUG_CHANNEL(NUMA);
 #include <pthread.h>
 #include <dlfcn.h>
 
+#include <algorithm>
+
 #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
+}
index 048ea3e..349b3de 100644 (file)
@@ -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 <algorithm>
+
+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;