From 40636020fcdf094e6c19588cf55b6ae9461422d1 Mon Sep 17 00:00:00 2001 From: comex Date: Sat, 1 Jul 2023 16:57:31 -0700 Subject: [PATCH] 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. --- src/dynarmic/backend/exception_handler_macos.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/dynarmic/backend/exception_handler_macos.cpp b/src/dynarmic/backend/exception_handler_macos.cpp index 201c7584..04144593 100644 --- a/src/dynarmic/backend/exception_handler_macos.cpp +++ b/src/dynarmic/backend/exception_handler_macos.cpp @@ -220,11 +220,15 @@ mig_external kern_return_t catch_mach_exception_raise_state( return KERN_FAILURE; } - dynarmic_thread_state_t* ts = reinterpret_cast(new_state); - std::memcpy(ts, reinterpret_cast(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 {