Skip to content

Commit 0d482fa

Browse files
committed
cleaning up the [ab]use of unions
1 parent 7192221 commit 0d482fa

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

libunwind/src/UnwindLevel1.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,30 @@
9494
// integer and an authenticated function pointer, so we need this helper
9595
// function to keep things clean.
9696
static _Unwind_Personality_Fn get_handler_function(unw_proc_info_t *frameInfo) {
97-
union {
98-
void *opaque_handler;
99-
_Unwind_Personality_Fn __ptrauth_unwind_upi_handler *handler;
100-
} u;
101-
u.opaque_handler = (void *)&frameInfo->handler;
102-
return *u.handler;
97+
// Converting from an authenticated integer to a _Unwind_Personality_Fn
98+
// requires multiple steps, but as the schema of _Unwind_Personality_Fn is
99+
// not address diversified we can mostly just rely on automatic re-signing
100+
// by clang.
101+
102+
// Step 1. Assign from the address diversified integer in frameInfo->handler
103+
// to the non-address diversified schema of `_Unwind_Personality_Fn`
104+
uintptr_t __unwind_ptrauth_restricted_intptr(ptrauth_key_function_pointer,
105+
0,
106+
ptrauth_function_pointer_type_discriminator(_Unwind_Personality_Fn))
107+
reauthenticatedIntegerHandler = frameInfo->handler;
108+
109+
// Step 2. Memcpy from our re-signed integer typed handler to an
110+
// _Unwind_Personality_Fn typed local - this avoids any confused
111+
// re-signing of values that already have a signature.
112+
_Unwind_Personality_Fn handler;
113+
uintptr_t __unwind_ptrauth_restricted_intptr(ptrauth_key_function_pointer, 0,
114+
ptrauth_function_pointer_type_discriminator(_Unwind_Personality_Fn))
115+
f;
116+
memcpy(&handler, (void *)&reauthenticatedIntegerHandler,
117+
sizeof(_Unwind_Personality_Fn));
118+
119+
// Step 3. Finally return the correctly typed and signed value.
120+
return handler;
103121
}
104122

105123
static _Unwind_Reason_Code

0 commit comments

Comments
 (0)