Path: blob/main/contrib/llvm-project/libcxx/src/include/overridable_function.h
35231 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H10#define _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H1112#include <__config>13#include <cstdint>1415#if __has_feature(ptrauth_calls)16# include <ptrauth.h>17#endif1819#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)20# pragma GCC system_header21#endif2223//24// This file provides the std::__is_function_overridden utility, which allows checking25// whether an overridable function (typically a weak symbol) like `operator new`26// has been overridden by a user or not.27//28// This is a low-level utility which does not work on all platforms, since it needs29// to make assumptions about the object file format in use. Furthermore, it requires30// the "base definition" of the function (the one we want to check whether it has been31// overridden) to be annotated with the _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE macro.32//33// This currently works with Mach-O files (used on Darwin) and with ELF files (used on Linux34// and others). On platforms where we know how to implement this detection, the macro35// _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION is defined to 1, and it is defined to 0 on36// other platforms. The _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE macro is defined to37// nothing on unsupported platforms so that it can be used to decorate functions regardless38// of whether detection is actually supported.39//40// How does this work?41// -------------------42//43// Let's say we want to check whether a weak function `f` has been overridden by the user.44// The general mechanism works by placing `f`'s definition (in the libc++ built library)45// inside a special section, which we do using the `__section__` attribute via the46// _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE macro.47//48// Then, when comes the time to check whether the function has been overridden, we take49// the address of the function and we check whether it falls inside the special function50// we created. This can be done by finding pointers to the start and the end of the section51// (which is done differently for ELF and Mach-O), and then checking whether `f` falls52// within those bounds. If it falls within those bounds, then `f` is still inside the53// special section and so it is the version we defined in the libc++ built library, i.e.54// it was not overridden. Otherwise, it was overridden by the user because it falls55// outside of the section.56//57// Important note58// --------------59//60// This mechanism should never be used outside of the libc++ built library. In particular,61// attempting to use this within the libc++ headers will not work at all because we don't62// want to be defining special sections inside user's executables which use our headers.63//6465#if defined(_LIBCPP_OBJECT_FORMAT_MACHO)6667# define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 168# define _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE \69__attribute__((__section__("__TEXT,__lcxx_override,regular,pure_instructions")))7071_LIBCPP_BEGIN_NAMESPACE_STD72template <class _Ret, class... _Args>73_LIBCPP_HIDE_FROM_ABI bool __is_function_overridden(_Ret (*__fptr)(_Args...)) noexcept {74// Declare two dummy bytes and give them these special `__asm` values. These values are75// defined by the linker, which means that referring to `&__lcxx_override_start` will76// effectively refer to the address where the section starts (and same for the end).77extern char __lcxx_override_start __asm("section$start$__TEXT$__lcxx_override");78extern char __lcxx_override_end __asm("section$end$__TEXT$__lcxx_override");7980// Now get a uintptr_t out of these locations, and out of the function pointer.81uintptr_t __start = reinterpret_cast<uintptr_t>(&__lcxx_override_start);82uintptr_t __end = reinterpret_cast<uintptr_t>(&__lcxx_override_end);83uintptr_t __ptr = reinterpret_cast<uintptr_t>(__fptr);8485# if __has_feature(ptrauth_calls)86// We must pass a void* to ptrauth_strip since it only accepts a pointer type. Also, in particular,87// we must NOT pass a function pointer, otherwise we will strip the function pointer, and then attempt88// to authenticate and re-sign it when casting it to a uintptr_t again, which will fail because we just89// stripped the function pointer. See rdar://122927845.90__ptr = reinterpret_cast<uintptr_t>(ptrauth_strip(reinterpret_cast<void*>(__ptr), ptrauth_key_function_pointer));91# endif9293// Finally, the function was overridden if it falls outside of the section's bounds.94return __ptr < __start || __ptr > __end;95}96_LIBCPP_END_NAMESPACE_STD9798#elif defined(_LIBCPP_OBJECT_FORMAT_ELF)99100# define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 1101# define _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE __attribute__((__section__("__lcxx_override")))102103// This is very similar to what we do for Mach-O above. The ELF linker will implicitly define104// variables with those names corresponding to the start and the end of the section.105//106// See https://stackoverflow.com/questions/16552710/how-do-you-get-the-start-and-end-addresses-of-a-custom-elf-section107extern char __start___lcxx_override;108extern char __stop___lcxx_override;109110_LIBCPP_BEGIN_NAMESPACE_STD111template <class _Ret, class... _Args>112_LIBCPP_HIDE_FROM_ABI bool __is_function_overridden(_Ret (*__fptr)(_Args...)) noexcept {113uintptr_t __start = reinterpret_cast<uintptr_t>(&__start___lcxx_override);114uintptr_t __end = reinterpret_cast<uintptr_t>(&__stop___lcxx_override);115uintptr_t __ptr = reinterpret_cast<uintptr_t>(__fptr);116117return __ptr < __start || __ptr > __end;118}119_LIBCPP_END_NAMESPACE_STD120121#else122123# define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 0124# define _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE /* nothing */125126#endif127128#endif // _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H129130131