xref: /freebsd/contrib/llvm-project/libcxx/src/include/overridable_function.h (revision 52418fc2be8efa5172b90a3a9e617017173612c4)
17a6dacacSDimitry Andric // -*- C++ -*-
27a6dacacSDimitry Andric //===----------------------------------------------------------------------===//
37a6dacacSDimitry Andric //
47a6dacacSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
57a6dacacSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
67a6dacacSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
77a6dacacSDimitry Andric //
87a6dacacSDimitry Andric //===----------------------------------------------------------------------===//
97a6dacacSDimitry Andric 
107a6dacacSDimitry Andric #ifndef _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H
117a6dacacSDimitry Andric #define _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H
127a6dacacSDimitry Andric 
137a6dacacSDimitry Andric #include <__config>
147a6dacacSDimitry Andric #include <cstdint>
157a6dacacSDimitry Andric 
16*52418fc2SDimitry Andric #if __has_feature(ptrauth_calls)
170fca6ea1SDimitry Andric #  include <ptrauth.h>
180fca6ea1SDimitry Andric #endif
190fca6ea1SDimitry Andric 
207a6dacacSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
217a6dacacSDimitry Andric #  pragma GCC system_header
227a6dacacSDimitry Andric #endif
237a6dacacSDimitry Andric 
247a6dacacSDimitry Andric //
257a6dacacSDimitry Andric // This file provides the std::__is_function_overridden utility, which allows checking
267a6dacacSDimitry Andric // whether an overridable function (typically a weak symbol) like `operator new`
277a6dacacSDimitry Andric // has been overridden by a user or not.
287a6dacacSDimitry Andric //
297a6dacacSDimitry Andric // This is a low-level utility which does not work on all platforms, since it needs
307a6dacacSDimitry Andric // to make assumptions about the object file format in use. Furthermore, it requires
317a6dacacSDimitry Andric // the "base definition" of the function (the one we want to check whether it has been
327a6dacacSDimitry Andric // overridden) to be annotated with the _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE macro.
337a6dacacSDimitry Andric //
347a6dacacSDimitry Andric // This currently works with Mach-O files (used on Darwin) and with ELF files (used on Linux
357a6dacacSDimitry Andric // and others). On platforms where we know how to implement this detection, the macro
367a6dacacSDimitry Andric // _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION is defined to 1, and it is defined to 0 on
377a6dacacSDimitry Andric // other platforms. The _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE macro is defined to
387a6dacacSDimitry Andric // nothing on unsupported platforms so that it can be used to decorate functions regardless
397a6dacacSDimitry Andric // of whether detection is actually supported.
407a6dacacSDimitry Andric //
417a6dacacSDimitry Andric // How does this work?
427a6dacacSDimitry Andric // -------------------
437a6dacacSDimitry Andric //
447a6dacacSDimitry Andric // Let's say we want to check whether a weak function `f` has been overridden by the user.
457a6dacacSDimitry Andric // The general mechanism works by placing `f`'s definition (in the libc++ built library)
467a6dacacSDimitry Andric // inside a special section, which we do using the `__section__` attribute via the
477a6dacacSDimitry Andric // _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE macro.
487a6dacacSDimitry Andric //
497a6dacacSDimitry Andric // Then, when comes the time to check whether the function has been overridden, we take
507a6dacacSDimitry Andric // the address of the function and we check whether it falls inside the special function
517a6dacacSDimitry Andric // we created. This can be done by finding pointers to the start and the end of the section
527a6dacacSDimitry Andric // (which is done differently for ELF and Mach-O), and then checking whether `f` falls
537a6dacacSDimitry Andric // within those bounds. If it falls within those bounds, then `f` is still inside the
547a6dacacSDimitry Andric // special section and so it is the version we defined in the libc++ built library, i.e.
557a6dacacSDimitry Andric // it was not overridden. Otherwise, it was overridden by the user because it falls
567a6dacacSDimitry Andric // outside of the section.
577a6dacacSDimitry Andric //
587a6dacacSDimitry Andric // Important note
597a6dacacSDimitry Andric // --------------
607a6dacacSDimitry Andric //
617a6dacacSDimitry Andric // This mechanism should never be used outside of the libc++ built library. In particular,
627a6dacacSDimitry Andric // attempting to use this within the libc++ headers will not work at all because we don't
637a6dacacSDimitry Andric // want to be defining special sections inside user's executables which use our headers.
647a6dacacSDimitry Andric //
657a6dacacSDimitry Andric 
667a6dacacSDimitry Andric #if defined(_LIBCPP_OBJECT_FORMAT_MACHO)
677a6dacacSDimitry Andric 
687a6dacacSDimitry Andric #  define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 1
697a6dacacSDimitry Andric #  define _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE                                                                 \
707a6dacacSDimitry Andric     __attribute__((__section__("__TEXT,__lcxx_override,regular,pure_instructions")))
717a6dacacSDimitry Andric 
727a6dacacSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
737a6dacacSDimitry Andric template <class _Ret, class... _Args>
__is_function_overridden(_Ret (* __fptr)(_Args...))747a6dacacSDimitry Andric _LIBCPP_HIDE_FROM_ABI bool __is_function_overridden(_Ret (*__fptr)(_Args...)) noexcept {
757a6dacacSDimitry Andric   // Declare two dummy bytes and give them these special `__asm` values. These values are
767a6dacacSDimitry Andric   // defined by the linker, which means that referring to `&__lcxx_override_start` will
777a6dacacSDimitry Andric   // effectively refer to the address where the section starts (and same for the end).
787a6dacacSDimitry Andric   extern char __lcxx_override_start __asm("section$start$__TEXT$__lcxx_override");
797a6dacacSDimitry Andric   extern char __lcxx_override_end __asm("section$end$__TEXT$__lcxx_override");
807a6dacacSDimitry Andric 
817a6dacacSDimitry Andric   // Now get a uintptr_t out of these locations, and out of the function pointer.
827a6dacacSDimitry Andric   uintptr_t __start = reinterpret_cast<uintptr_t>(&__lcxx_override_start);
837a6dacacSDimitry Andric   uintptr_t __end   = reinterpret_cast<uintptr_t>(&__lcxx_override_end);
847a6dacacSDimitry Andric   uintptr_t __ptr   = reinterpret_cast<uintptr_t>(__fptr);
857a6dacacSDimitry Andric 
86*52418fc2SDimitry Andric #  if __has_feature(ptrauth_calls)
870fca6ea1SDimitry Andric   // We must pass a void* to ptrauth_strip since it only accepts a pointer type. Also, in particular,
880fca6ea1SDimitry Andric   // we must NOT pass a function pointer, otherwise we will strip the function pointer, and then attempt
890fca6ea1SDimitry Andric   // to authenticate and re-sign it when casting it to a uintptr_t again, which will fail because we just
900fca6ea1SDimitry Andric   // stripped the function pointer. See rdar://122927845.
910fca6ea1SDimitry Andric   __ptr = reinterpret_cast<uintptr_t>(ptrauth_strip(reinterpret_cast<void*>(__ptr), ptrauth_key_function_pointer));
920fca6ea1SDimitry Andric #  endif
930fca6ea1SDimitry Andric 
947a6dacacSDimitry Andric   // Finally, the function was overridden if it falls outside of the section's bounds.
957a6dacacSDimitry Andric   return __ptr < __start || __ptr > __end;
967a6dacacSDimitry Andric }
977a6dacacSDimitry Andric _LIBCPP_END_NAMESPACE_STD
987a6dacacSDimitry Andric 
997a6dacacSDimitry Andric #elif defined(_LIBCPP_OBJECT_FORMAT_ELF)
1007a6dacacSDimitry Andric 
1017a6dacacSDimitry Andric #  define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 1
1027a6dacacSDimitry Andric #  define _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE __attribute__((__section__("__lcxx_override")))
1037a6dacacSDimitry Andric 
1047a6dacacSDimitry Andric // This is very similar to what we do for Mach-O above. The ELF linker will implicitly define
1057a6dacacSDimitry Andric // variables with those names corresponding to the start and the end of the section.
1067a6dacacSDimitry Andric //
1077a6dacacSDimitry Andric // See https://stackoverflow.com/questions/16552710/how-do-you-get-the-start-and-end-addresses-of-a-custom-elf-section
1087a6dacacSDimitry Andric extern char __start___lcxx_override;
1097a6dacacSDimitry Andric extern char __stop___lcxx_override;
1107a6dacacSDimitry Andric 
1117a6dacacSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
1127a6dacacSDimitry Andric template <class _Ret, class... _Args>
1137a6dacacSDimitry Andric _LIBCPP_HIDE_FROM_ABI bool __is_function_overridden(_Ret (*__fptr)(_Args...)) noexcept {
1147a6dacacSDimitry Andric   uintptr_t __start = reinterpret_cast<uintptr_t>(&__start___lcxx_override);
1157a6dacacSDimitry Andric   uintptr_t __end   = reinterpret_cast<uintptr_t>(&__stop___lcxx_override);
1167a6dacacSDimitry Andric   uintptr_t __ptr   = reinterpret_cast<uintptr_t>(__fptr);
1177a6dacacSDimitry Andric 
1187a6dacacSDimitry Andric   return __ptr < __start || __ptr > __end;
1197a6dacacSDimitry Andric }
1207a6dacacSDimitry Andric _LIBCPP_END_NAMESPACE_STD
1217a6dacacSDimitry Andric 
1227a6dacacSDimitry Andric #else
1237a6dacacSDimitry Andric 
1247a6dacacSDimitry Andric #  define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 0
1257a6dacacSDimitry Andric #  define _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE /* nothing */
1267a6dacacSDimitry Andric 
1277a6dacacSDimitry Andric #endif
1287a6dacacSDimitry Andric 
1297a6dacacSDimitry Andric #endif // _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H
130