exception_handler_macos: Avoid undefined behavior due to accessing misaligned pointer

Caught by UBSan.  I don't think this has a meaningful performance impact
(especially since the thread state doesn't include floating-point/vector
registers), but I haven't tested it.
This commit is contained in:
comex 2023-07-01 16:57:31 -07:00 committed by merry
parent d333a09b3b
commit 40636020fc

View file

@ -220,11 +220,15 @@ mig_external kern_return_t catch_mach_exception_raise_state(
return KERN_FAILURE;
}
dynarmic_thread_state_t* ts = reinterpret_cast<dynarmic_thread_state_t*>(new_state);
std::memcpy(ts, reinterpret_cast<const dynarmic_thread_state_t*>(old_state), sizeof(dynarmic_thread_state_t));
*new_stateCnt = THREAD_STATE_COUNT;
// The input/output pointers are not necessarily 8-byte aligned.
dynarmic_thread_state_t ts;
std::memcpy(&ts, old_state, sizeof(ts));
return mach_handler.HandleRequest(ts);
kern_return_t ret = mach_handler.HandleRequest(&ts);
std::memcpy(new_state, &ts, sizeof(ts));
*new_stateCnt = THREAD_STATE_COUNT;
return ret;
}
struct ExceptionHandler::Impl final {