xref: /llvm-project/libcxx/src/include/overridable_function.h (revision 4167ea2cb082a2acb00b8b1dc09aa780dc0e3110)
131452655SLouis Dionne // -*- C++ -*-
231452655SLouis Dionne //===----------------------------------------------------------------------===//
331452655SLouis Dionne //
431452655SLouis Dionne // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
531452655SLouis Dionne // See https://llvm.org/LICENSE.txt for license information.
631452655SLouis Dionne // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
731452655SLouis Dionne //
831452655SLouis Dionne //===----------------------------------------------------------------------===//
931452655SLouis Dionne 
1031452655SLouis Dionne #ifndef _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H
1131452655SLouis Dionne #define _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H
1231452655SLouis Dionne 
1331452655SLouis Dionne #include <__config>
1431452655SLouis Dionne #include <cstdint>
1531452655SLouis Dionne 
16e64e745eSLouis Dionne #if __has_feature(ptrauth_calls)
1798244c4eSLouis Dionne #  include <ptrauth.h>
1898244c4eSLouis Dionne #endif
1998244c4eSLouis Dionne 
2031452655SLouis Dionne #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2131452655SLouis Dionne #  pragma GCC system_header
2231452655SLouis Dionne #endif
2331452655SLouis Dionne 
2431452655SLouis Dionne //
2531452655SLouis Dionne // This file provides the std::__is_function_overridden utility, which allows checking
2631452655SLouis Dionne // whether an overridable function (typically a weak symbol) like `operator new`
2731452655SLouis Dionne // has been overridden by a user or not.
2831452655SLouis Dionne //
2931452655SLouis Dionne // This is a low-level utility which does not work on all platforms, since it needs
3031452655SLouis Dionne // to make assumptions about the object file format in use. Furthermore, it requires
3131452655SLouis Dionne // the "base definition" of the function (the one we want to check whether it has been
32*4167ea2cSPetr Hosek // overridden) to be annotated with the _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE macro.
3331452655SLouis Dionne //
3431452655SLouis Dionne // This currently works with Mach-O files (used on Darwin) and with ELF files (used on Linux
3531452655SLouis Dionne // and others). On platforms where we know how to implement this detection, the macro
3631452655SLouis Dionne // _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION is defined to 1, and it is defined to 0 on
37*4167ea2cSPetr Hosek // other platforms. The _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE macro is defined to
38*4167ea2cSPetr Hosek // nothing on unsupported platforms so that it can be used to decorate functions regardless
39*4167ea2cSPetr Hosek // of whether detection is actually supported.
4031452655SLouis Dionne //
4131452655SLouis Dionne // How does this work?
4231452655SLouis Dionne // -------------------
4331452655SLouis Dionne //
4431452655SLouis Dionne // Let's say we want to check whether a weak function `f` has been overridden by the user.
45*4167ea2cSPetr Hosek // The general mechanism works by placing `f`'s definition (in the libc++ built library)
46*4167ea2cSPetr Hosek // inside a special section, which we do using the `__section__` attribute via the
47*4167ea2cSPetr Hosek // _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE macro.
4831452655SLouis Dionne //
4931452655SLouis Dionne // Then, when comes the time to check whether the function has been overridden, we take
50*4167ea2cSPetr Hosek // the address of the function and we check whether it falls inside the special function
51*4167ea2cSPetr Hosek // we created. This can be done by finding pointers to the start and the end of the section
52*4167ea2cSPetr Hosek // (which is done differently for ELF and Mach-O), and then checking whether `f` falls
53*4167ea2cSPetr Hosek // within those bounds. If it falls within those bounds, then `f` is still inside the
54*4167ea2cSPetr Hosek // special section and so it is the version we defined in the libc++ built library, i.e.
55*4167ea2cSPetr Hosek // it was not overridden. Otherwise, it was overridden by the user because it falls
56*4167ea2cSPetr Hosek // outside of the section.
5731452655SLouis Dionne //
5831452655SLouis Dionne // Important note
5931452655SLouis Dionne // --------------
6031452655SLouis Dionne //
61*4167ea2cSPetr Hosek // This mechanism should never be used outside of the libc++ built library. In particular,
62*4167ea2cSPetr Hosek // attempting to use this within the libc++ headers will not work at all because we don't
63*4167ea2cSPetr Hosek // want to be defining special sections inside user's executables which use our headers.
6431452655SLouis Dionne //
6531452655SLouis Dionne 
6631452655SLouis Dionne #if defined(_LIBCPP_OBJECT_FORMAT_MACHO)
6731452655SLouis Dionne 
6862bd10f7SPetr Hosek #  define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 1
69*4167ea2cSPetr Hosek #  define _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE                                                                 \
70*4167ea2cSPetr Hosek     __attribute__((__section__("__TEXT,__lcxx_override,regular,pure_instructions")))
718dfae0c4SNico Weber 
728dfae0c4SNico Weber _LIBCPP_BEGIN_NAMESPACE_STD
73*4167ea2cSPetr Hosek template <class _Ret, class... _Args>
74*4167ea2cSPetr Hosek _LIBCPP_HIDE_FROM_ABI bool __is_function_overridden(_Ret (*__fptr)(_Args...)) noexcept {
75*4167ea2cSPetr Hosek   // Declare two dummy bytes and give them these special `__asm` values. These values are
76*4167ea2cSPetr Hosek   // defined by the linker, which means that referring to `&__lcxx_override_start` will
77*4167ea2cSPetr Hosek   // effectively refer to the address where the section starts (and same for the end).
78*4167ea2cSPetr Hosek   extern char __lcxx_override_start __asm("section$start$__TEXT$__lcxx_override");
79*4167ea2cSPetr Hosek   extern char __lcxx_override_end __asm("section$end$__TEXT$__lcxx_override");
808dfae0c4SNico Weber 
81*4167ea2cSPetr Hosek   // Now get a uintptr_t out of these locations, and out of the function pointer.
82*4167ea2cSPetr Hosek   uintptr_t __start = reinterpret_cast<uintptr_t>(&__lcxx_override_start);
83*4167ea2cSPetr Hosek   uintptr_t __end   = reinterpret_cast<uintptr_t>(&__lcxx_override_end);
84*4167ea2cSPetr Hosek   uintptr_t __ptr   = reinterpret_cast<uintptr_t>(__fptr);
858dfae0c4SNico Weber 
86*4167ea2cSPetr Hosek #  if __has_feature(ptrauth_calls)
87*4167ea2cSPetr Hosek   // We must pass a void* to ptrauth_strip since it only accepts a pointer type. Also, in particular,
88*4167ea2cSPetr Hosek   // we must NOT pass a function pointer, otherwise we will strip the function pointer, and then attempt
89*4167ea2cSPetr Hosek   // to authenticate and re-sign it when casting it to a uintptr_t again, which will fail because we just
90*4167ea2cSPetr Hosek   // stripped the function pointer. See rdar://122927845.
91*4167ea2cSPetr Hosek   __ptr = reinterpret_cast<uintptr_t>(ptrauth_strip(reinterpret_cast<void*>(__ptr), ptrauth_key_function_pointer));
92*4167ea2cSPetr Hosek #  endif
93*4167ea2cSPetr Hosek 
94*4167ea2cSPetr Hosek   // Finally, the function was overridden if it falls outside of the section's bounds.
95*4167ea2cSPetr Hosek   return __ptr < __start || __ptr > __end;
96*4167ea2cSPetr Hosek }
978dfae0c4SNico Weber _LIBCPP_END_NAMESPACE_STD
9862bd10f7SPetr Hosek 
99*4167ea2cSPetr Hosek // The NVPTX linker cannot create '__start/__stop' sections.
100*4167ea2cSPetr Hosek #elif defined(_LIBCPP_OBJECT_FORMAT_ELF) && !defined(__NVPTX__)
101*4167ea2cSPetr Hosek 
10284189554SPetr Hosek #  define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 1
103*4167ea2cSPetr Hosek #  define _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE __attribute__((__section__("__lcxx_override")))
104*4167ea2cSPetr Hosek 
105*4167ea2cSPetr Hosek // This is very similar to what we do for Mach-O above. The ELF linker will implicitly define
106*4167ea2cSPetr Hosek // variables with those names corresponding to the start and the end of the section.
107*4167ea2cSPetr Hosek //
108*4167ea2cSPetr Hosek // See https://stackoverflow.com/questions/16552710/how-do-you-get-the-start-and-end-addresses-of-a-custom-elf-section
109*4167ea2cSPetr Hosek extern char __start___lcxx_override;
110*4167ea2cSPetr Hosek extern char __stop___lcxx_override;
111*4167ea2cSPetr Hosek 
112*4167ea2cSPetr Hosek _LIBCPP_BEGIN_NAMESPACE_STD
113*4167ea2cSPetr Hosek template <class _Ret, class... _Args>
114*4167ea2cSPetr Hosek _LIBCPP_HIDE_FROM_ABI bool __is_function_overridden(_Ret (*__fptr)(_Args...)) noexcept {
115*4167ea2cSPetr Hosek   uintptr_t __start = reinterpret_cast<uintptr_t>(&__start___lcxx_override);
116*4167ea2cSPetr Hosek   uintptr_t __end   = reinterpret_cast<uintptr_t>(&__stop___lcxx_override);
117*4167ea2cSPetr Hosek   uintptr_t __ptr   = reinterpret_cast<uintptr_t>(__fptr);
118*4167ea2cSPetr Hosek 
119*4167ea2cSPetr Hosek #  if __has_feature(ptrauth_calls)
120*4167ea2cSPetr Hosek   // We must pass a void* to ptrauth_strip since it only accepts a pointer type. See full explanation above.
121*4167ea2cSPetr Hosek   __ptr = reinterpret_cast<uintptr_t>(ptrauth_strip(reinterpret_cast<void*>(__ptr), ptrauth_key_function_pointer));
122*4167ea2cSPetr Hosek #  endif
123*4167ea2cSPetr Hosek 
124*4167ea2cSPetr Hosek   return __ptr < __start || __ptr > __end;
125*4167ea2cSPetr Hosek }
126*4167ea2cSPetr Hosek _LIBCPP_END_NAMESPACE_STD
12784189554SPetr Hosek 
12831452655SLouis Dionne #else
12931452655SLouis Dionne 
13031452655SLouis Dionne #  define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 0
131*4167ea2cSPetr Hosek #  define _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE /* nothing */
13231452655SLouis Dionne 
13331452655SLouis Dionne #endif
13431452655SLouis Dionne 
13531452655SLouis Dionne #endif // _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H
136