| 1 | // -*- C++ -*- |
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H |
| 11 | #define _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H |
| 12 | |
| 13 | #include <__config> |
| 14 | |
| 15 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 16 | # pragma GCC system_header |
| 17 | #endif |
| 18 | |
| 19 | // |
| 20 | // This file provides the std::__is_function_overridden utility, which allows checking |
| 21 | // whether an overridable function (typically a weak symbol) like `operator new` |
| 22 | // has been overridden by a user or not. |
| 23 | // |
| 24 | // This is a low-level utility which does not work on all platforms, since it needs |
| 25 | // to make assumptions about the object file format in use. Furthermore, it requires |
| 26 | // the "base definition" of the function (the one we want to check whether it has been |
| 27 | // overridden) to be defined using the OVERRIDABLE_FUNCTION macro. |
| 28 | // |
| 29 | // This currently works with Mach-O files (used on Darwin) and with ELF files (used on Linux |
| 30 | // and others). On platforms where we know how to implement this detection, the macro |
| 31 | // _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION is defined to 1, and it is defined to 0 on |
| 32 | // other platforms. The OVERRIDABLE_FUNCTION macro is defined to perform a normal |
| 33 | // function definition on unsupported platforms so that it can be used to define functions |
| 34 | // regardless of whether detection is actually supported. |
| 35 | // |
| 36 | // How does this work? |
| 37 | // ------------------- |
| 38 | // |
| 39 | // Let's say we want to check whether a weak function `f` has been overridden by the user. |
| 40 | // The general mechanism works by defining a local symbol `__impl_ref<f>::__impl_` with |
| 41 | // the same address as `f` as a constant expression using direct PC-relative |
| 42 | // materialization thus pointing at the symbol defined in the same TU. At runtime, it |
| 43 | // compares the address of `__impl_ref<f>::__impl_` with the address of `f` loaded from |
| 44 | // GOT: if `f` was overridden by the user in another TU, the addresses will be different. |
| 45 | // |
| 46 | // When pointer authentication is used, the above mechanism doesn't work. It also doesn't work |
| 47 | // with recent Mach-O linkers, which bind references to a weak definition directly (instead of |
| 48 | // through a weak-def-coalesced GOT entry) when that definition has a local alias at the same |
| 49 | // address. In both cases, we use a different strategy, placing `f`'s definition (in the libc++ |
| 50 | // built library) inside a special section, which we do using the `__section__` attribute via |
| 51 | // the OVERRIDABLE_FUNCTION macro. Then, when comes the time to check whether the function has |
| 52 | // been overridden, we take the address of the function and we check whether it falls inside |
| 53 | // the special section we created. This can be done by finding pointers to the start and |
| 54 | // the end of the section, and then checking whether `f` falls within those bounds. |
| 55 | // |
| 56 | // Important note |
| 57 | // -------------- |
| 58 | // |
| 59 | // This mechanism should never be used outside of the libc++ built library. In particular, |
| 60 | // attempting to use this within the libc++ headers will not work at all because we don't |
| 61 | // want to be defining special sections inside user's executables which use our headers. |
| 62 | // |
| 63 | |
| 64 | #if defined(_LIBCPP_OBJECT_FORMAT_MACHO) || (defined(_LIBCPP_OBJECT_FORMAT_ELF) && !defined(__NVPTX__)) |
| 65 | |
| 66 | # define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 1 |
| 67 | |
| 68 | # if !__has_feature(ptrauth_calls) && !defined(_LIBCPP_OBJECT_FORMAT_MACHO) |
| 69 | |
| 70 | # define OVERRIDABLE_FUNCTION [[gnu::weak]] |
| 71 | |
| 72 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 73 | |
| 74 | namespace { |
| 75 | |
| 76 | // This is used to prevent TBAA from optimizing away the function pointer comparison. |
| 77 | template <typename T> |
| 78 | [[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI T* __launder_function_pointer(T* __ptr) noexcept { |
| 79 | __asm__ volatile("" : "+r" (__ptr)); |
| 80 | return __ptr; |
| 81 | } |
| 82 | |
| 83 | } // namespace |
| 84 | |
| 85 | template <auto* _Func> |
| 86 | struct __impl_ref; |
| 87 | |
| 88 | // __impl_ref<...>::__impl_ is expected to be defined elsewhere, so the compiler emits |
| 89 | // assembly references to the mangled symbol with no definition. This template saves us |
| 90 | // the trouble of providing manual declarations for overloads with some other local name |
| 91 | // for each function name being overloaded (operator new, operator new[], etc.). |
| 92 | template <typename _Ret, typename... _Args, _Ret (*_Func)(_Args...)> |
| 93 | struct __impl_ref<_Func> { |
| 94 | [[gnu::visibility("hidden" )]] static _Ret __impl_(_Args...); |
| 95 | }; |
| 96 | |
| 97 | // This takes a function type template argument first so that the second non-type template |
| 98 | // argument (pointer to the public function) gets the benefit of type-aware overload |
| 99 | // resolution, rather than having to use a static_cast. |
| 100 | template <typename T, T* _Func> |
| 101 | _LIBCPP_HIDE_FROM_ABI inline bool __is_function_overridden() noexcept { |
| 102 | # if !defined(_LIBCPP_CLANG_VER) || _LIBCPP_CLANG_VER >= 2101 |
| 103 | __asm__("%cc0 = %cc1" : : "X" (__impl_ref<_Func>::__impl_), "X" (_Func)); |
| 104 | # else |
| 105 | __asm__("%c0 = %c1" : : "X" (__impl_ref<_Func>::__impl_), "X" (_Func)); |
| 106 | # endif |
| 107 | // This just has the compiler compare the two symbols. For PIC mode, this will do a |
| 108 | // direct PC-relative materialization for __impl_ref<...>::__impl_ and a GOT load for |
| 109 | // the _Func symbol. The compiler thinks __impl_ref<...>::__impl_ is defined elsewhere |
| 110 | // at link time and will be an undefined symbol. It doesn't know that the __asm__ tells |
| 111 | // the assembler to define it as a local symbol. |
| 112 | return __launder_function_pointer(_Func) != __impl_ref<_Func>::__impl_; |
| 113 | } |
| 114 | |
| 115 | _LIBCPP_END_NAMESPACE_STD |
| 116 | |
| 117 | # else // __has_feature(ptrauth_calls) || defined(_LIBCPP_OBJECT_FORMAT_MACHO) |
| 118 | |
| 119 | # include <cstdint> |
| 120 | # include <ptrauth.h> |
| 121 | |
| 122 | # if defined(_LIBCPP_OBJECT_FORMAT_MACHO) |
| 123 | # define OVERRIDABLE_FUNCTION [[gnu::weak, gnu::section("__TEXT,__lcxx_override,regular,pure_instructions")]] |
| 124 | // Declare two dummy bytes and give them these special `__asm` values. These values are |
| 125 | // defined by the linker, which means that referring to `&__lcxx_override_start` will |
| 126 | // effectively refer to the address where the section starts (and same for the end). |
| 127 | extern char __start___lcxx_override __asm("section$start$__TEXT$__lcxx_override" ); |
| 128 | extern char __stop___lcxx_override __asm("section$end$__TEXT$__lcxx_override" ); |
| 129 | # elif defined(_LIBCPP_OBJECT_FORMAT_ELF) |
| 130 | // This is very similar to what we do for Mach-O above. The ELF linker will implicitly define |
| 131 | // variables with those names corresponding to the start and the end of the section. |
| 132 | // |
| 133 | // See https://stackoverflow.com/questions/16552710/how-do-you-get-the-start-and-end-addresses-of-a-custom-elf-section |
| 134 | # define OVERRIDABLE_FUNCTION [[gnu::weak, gnu::section("__lcxx_override")]] |
| 135 | extern char __start___lcxx_override; |
| 136 | extern char __stop___lcxx_override; |
| 137 | # endif |
| 138 | |
| 139 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 140 | template <typename T, T* _Func> |
| 141 | _LIBCPP_HIDE_FROM_ABI inline bool __is_function_overridden() noexcept { |
| 142 | uintptr_t __start = reinterpret_cast<uintptr_t>(&__start___lcxx_override); |
| 143 | uintptr_t __end = reinterpret_cast<uintptr_t>(&__stop___lcxx_override); |
| 144 | uintptr_t __ptr = reinterpret_cast<uintptr_t>(_Func); |
| 145 | |
| 146 | // We must pass a void* to ptrauth_strip since it only accepts a pointer type. Also, in particular, |
| 147 | // we must NOT pass a function pointer, otherwise we will strip the function pointer, and then attempt |
| 148 | // to authenticate and re-sign it when casting it to a uintptr_t again, which will fail because we just |
| 149 | // stripped the function pointer. See rdar://122927845. |
| 150 | __ptr = reinterpret_cast<uintptr_t>(ptrauth_strip(reinterpret_cast<void*>(__ptr), ptrauth_key_function_pointer)); |
| 151 | |
| 152 | // Finally, the function was overridden if it falls outside of the section's bounds. |
| 153 | return __ptr < __start || __ptr > __end; |
| 154 | } |
| 155 | _LIBCPP_END_NAMESPACE_STD |
| 156 | |
| 157 | # endif // __has_feature(ptrauth_calls) || defined(_LIBCPP_OBJECT_FORMAT_MACHO) |
| 158 | |
| 159 | #else // defined(_LIBCPP_OBJECT_FORMAT_MACHO) || (defined(_LIBCPP_OBJECT_FORMAT_ELF) && !defined(__NVPTX__)) |
| 160 | |
| 161 | # define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 0 |
| 162 | # define OVERRIDABLE_FUNCTION [[gnu::weak]] |
| 163 | |
| 164 | #endif // defined(_LIBCPP_OBJECT_FORMAT_MACHO) || (defined(_LIBCPP_OBJECT_FORMAT_ELF) && !defined(__NVPTX__)) |
| 165 | |
| 166 | #endif // _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H |
| 167 | |