addresses.h (2064B)
1 // Copyright 2025 The Abseil Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef ABSL_DEBUGGING_INTERNAL_ADDRESSES_H_ 16 #define ABSL_DEBUGGING_INTERNAL_ADDRESSES_H_ 17 18 #include <stdint.h> 19 20 #include "absl/base/config.h" 21 22 namespace absl { 23 ABSL_NAMESPACE_BEGIN 24 namespace debugging_internal { 25 26 // Removes any metadata (tag bits) from the given pointer, converting it into a 27 // user-readable address. 28 inline uintptr_t StripPointerMetadata(uintptr_t ptr) { 29 #if defined(__aarch64__) 30 // When PAC-RET (-mbranch-protection=pac-ret) is enabled, return addresses 31 // stored on the stack will be signed, which means that pointer bits outside 32 // of the virtual address range are potentially set. Since the stacktrace code 33 // is expected to return normal code pointers, this function clears those 34 // bits. 35 register uintptr_t x30 __asm__("x30") = ptr; 36 // The normal instruction for clearing PAC bits is XPACI, but for 37 // compatibility with ARM platforms that do not support pointer 38 // authentication, we use the hint space instruction XPACLRI instead. Hint 39 // space instructions behave as NOPs on unsupported platforms. 40 #define ABSL_XPACLRI_HINT "hint #0x7;" 41 asm(ABSL_XPACLRI_HINT : "+r"(x30)); // asm("xpaclri" : "+r"(x30)); 42 #undef ABSL_XPACLRI_HINT 43 return x30; 44 #else 45 return ptr; 46 #endif 47 } 48 49 inline uintptr_t StripPointerMetadata(void* ptr) { 50 return StripPointerMetadata(reinterpret_cast<uintptr_t>(ptr)); 51 } 52 53 } // namespace debugging_internal 54 ABSL_NAMESPACE_END 55 } // namespace absl 56 57 #endif // ABSL_DEBUGGING_INTERNAL_ADDRESSES_H_