Skip to content

Commit 4d6cb98

Browse files
Abseil Teamcopybara-github
authored andcommitted
Migrate ClearPacBits() to a more generic implementation and location
PiperOrigin-RevId: 736664098 Change-Id: Ic99ff4ebae02097b9f7691ebdcb2e10f7d255834
1 parent 45acc96 commit 4d6cb98

File tree

5 files changed

+59
-17
lines changed

5 files changed

+59
-17
lines changed

CMake/AbseilDll.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ set(ABSL_INTERNAL_DLL_FILES
126126
"debugging/symbolize.h"
127127
"debugging/internal/address_is_readable.cc"
128128
"debugging/internal/address_is_readable.h"
129+
"debugging/internal/addresses.h"
129130
"debugging/internal/bounded_utf8_length_sequence.h"
130131
"debugging/internal/decode_rust_punycode.cc"
131132
"debugging/internal/decode_rust_punycode.h"

absl/debugging/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ cc_library(
202202
],
203203
hdrs = [
204204
"internal/address_is_readable.h",
205+
"internal/addresses.h",
205206
"internal/elf_mem_image.h",
206207
"internal/vdso_support.h",
207208
],

absl/debugging/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ absl_cc_library(
174174
debugging_internal
175175
HDRS
176176
"internal/address_is_readable.h"
177+
"internal/addresses.h"
177178
"internal/elf_mem_image.h"
178179
"internal/vdso_support.h"
179180
SRCS
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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(void* 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") = reinterpret_cast<uintptr_t>(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 reinterpret_cast<uintptr_t>(ptr);
46+
#endif
47+
}
48+
49+
} // namespace debugging_internal
50+
ABSL_NAMESPACE_END
51+
} // namespace absl
52+
53+
#endif // ABSL_DEBUGGING_INTERNAL_ADDRESSES_H_

absl/debugging/internal/stacktrace_aarch64-inl.inc

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "absl/base/attributes.h"
2020
#include "absl/debugging/internal/address_is_readable.h"
21+
#include "absl/debugging/internal/addresses.h"
2122
#include "absl/debugging/internal/vdso_support.h" // a no-op on non-elf or non-glibc systems
2223
#include "absl/debugging/stacktrace.h"
2324

@@ -178,22 +179,6 @@ static void **NextStackFrame(void **old_frame_pointer, const void *uc,
178179
return new_frame_pointer;
179180
}
180181

181-
// When PAC-RET (-mbranch-protection=pac-ret) is enabled, return addresses
182-
// stored on the stack will be signed, which means that pointer bits outside of
183-
// the VA range are potentially set. Since the stacktrace code is expected to
184-
// return normal code pointers, this function clears those bits.
185-
inline void* ClearPacBits(void* ptr) {
186-
register void* x30 __asm__("x30") = ptr;
187-
// The normal instruction for clearing PAC bits is XPACI, but for
188-
// compatibility with ARM platforms that do not support pointer
189-
// authentication, we use the hint space instruction XPACLRI instead. Hint
190-
// space instructions behave as NOPs on unsupported platforms.
191-
#define ABSL_XPACLRI_HINT "hint #0x7;"
192-
asm(ABSL_XPACLRI_HINT : "+r"(x30)); // asm("xpaclri" : "+r"(x30));
193-
#undef ABSL_XPACLRI_HINT
194-
return x30;
195-
}
196-
197182
template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
198183
// We count on the bottom frame being this one. See the comment
199184
// at prev_return_address
@@ -235,7 +220,8 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
235220
if (skip_count > 0) {
236221
skip_count--;
237222
} else {
238-
result[n] = ClearPacBits(prev_return_address);
223+
result[n] = reinterpret_cast<void *>(
224+
absl::debugging_internal::StripPointerMetadata(prev_return_address));
239225
if (IS_STACK_FRAMES) {
240226
sizes[n] = static_cast<int>(
241227
ComputeStackFrameSize(prev_frame_pointer, frame_pointer));

0 commit comments

Comments
 (0)