chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.98k stars 1.22k forks source link

aligned_alloc check is not correct on macOS #1090

Open jcelerier opened 2 years ago

jcelerier commented 2 years ago

The problem:

config.hpp:

// Standard library support for aligned allocation.
#if !defined(BOOST_ASIO_HAS_STD_ALIGNED_ALLOC)
# if !defined(BOOST_ASIO_DISABLE_STD_ALIGNED_ALLOC)
#  if (__cplusplus >= 201703)
#   if defined(__clang__)
#    if defined(BOOST_ASIO_HAS_CLANG_LIBCXX)
#     if (_LIBCPP_STD_VER > 14) && defined(_LIBCPP_HAS_ALIGNED_ALLOC) \
        && !defined(_LIBCPP_MSVCRT) && !defined(__MINGW32__)
#      define BOOST_ASIO_HAS_STD_ALIGNED_ALLOC 1
#     endif // (_LIBCPP_STD_VER > 14) && defined(_LIBCPP_HAS_ALIGNED_ALLOC)
            //   && !defined(_LIBCPP_MSVCRT) && !defined(__MINGW32__)
#    elif defined(_GLIBCXX_HAVE_ALIGNED_ALLOC)
#     define BOOST_ASIO_HAS_STD_ALIGNED_ALLOC 1
#    endif // defined(_GLIBCXX_HAVE_ALIGNED_ALLOC)
#   elif defined(__GNUC__)
#    if defined(_GLIBCXX_HAVE_ALIGNED_ALLOC)
#     define BOOST_ASIO_HAS_STD_ALIGNED_ALLOC 1
#    endif // defined(_GLIBCXX_HAVE_ALIGNED_ALLOC)
#   endif // defined(__GNUC__)
#  endif // (__cplusplus >= 201703)
# endif // !defined(BOOST_ASIO_DISABLE_STD_ALIGNED_ALLOC)
#endif // !defined(BOOST_ASIO_HAS_STD_ALIGNED_ALLOC)

yields true on macOS as the standard library "has" aligned_alloc and defines _LIBCPP_HAS_ALIGNED_ALLOC in recent macOS SDKs.

But it is only supported for macOS >= 10.15 (the function is not present in libc in 10.14 and before) which fails at runtime on this hardware.

Here's the code from Apple's libc:

#if (__DARWIN_C_LEVEL >= __DARWIN_C_FULL) || \
    (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
    (defined(__cplusplus) && __cplusplus >= 201703L)
void    *aligned_alloc(size_t __alignment, size_t __size) __result_use_check __alloc_size(2) __OSX_AVAILABLE(10.15) __IOS_AVAILABLE(13.0) __TVOS_AVAILABLE(13.0) __WATCHOS_AVAILABLE(6.0);
#endif

The guard should be :

#if (_LIBCPP_STD_VER > 14) && defined(_LIBCPP_HAS_ALIGNED_ALLOC) && !defined(_LIBCPP_MSVCRT) && !defined(__MINGW32__) && (!defined(__APPLE__) || (TARGET_OS_MAC && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15) || (TARGET_OS_IOS && ...))
H-G-Hristov commented 2 years ago

After we upgraded to boost 1.79.0, macOS 12.5.1 and Xcode 13.4.1 we started getting on macOS 10.14 and below:

Date/Time: 2022-09-05 16:18:07.120 +0200 OS Version: Mac OS X 10.14.6 (18G9216) Report Version: 12 Anonymous UUID: D66AD689-B4E2-6363-C03B-587A0BECA90E

Time Awake Since Boot: 2900 seconds

System Integrity Protection: enabled

Crashed Thread: 10

Exception Type: EXC_CRASH (SIGABRT) Exception Codes: 0x0000000000000000, 0x0000000000000000 Exception Note: EXC_CORPSE_NOTIFY

Termination Reason: DYLD, [0x4] Symbol missing

Dyld Error Message: Symbol not found: _aligned_alloc Referenced from: /Library/N-central Agent/nagent Expected in: /usr/lib/libSystem.B.dylib

We are testing this as a fix:

if (CMAKE_OSX_DEPLOYMENT_TARGET VERSION_LESS "10.15")
  target_compile_definitions (${PROJECT_NAME} PRIVATE -DBOOST_ASIO_DISABLE_STD_ALIGNED_ALLOC)
endif (CMAKE_OSX_DEPLOYMENT_TARGET VERSION_LESS "10.15")