[MachException] Avoid alignment UB, NFC
authorVedant Kumar <vsk@apple.com>
Fri, 8 Dec 2017 01:09:40 +0000 (01:09 +0000)
committerVedant Kumar <vsk@apple.com>
Fri, 8 Dec 2017 01:09:40 +0000 (01:09 +0000)
Fix alignment UB in some Mach exception-handling logic.

This lets us build lldb and debugserver with UBSan in trapping mode, and
get further along in the testing process before a trap is encountered.

rdar://35923991

llvm-svn: 320127

lldb/tools/debugserver/source/MacOSX/MachException.cpp
lldb/tools/debugserver/source/MacOSX/MachException.h

index 12479b8b7e87a7d8be51e82931091440f8f5fff9..cc309e47d86bffa5eeddd6b94049c9644a9f241a 100644 (file)
@@ -113,8 +113,7 @@ catch_mach_exception_raise(mach_port_t exc_port, mach_port_t thread_port,
     g_message->task_port = task_port;
     g_message->thread_port = thread_port;
     g_message->exc_type = exc_type;
-    for (mach_msg_type_number_t i=0; i<exc_data_count; ++i)
-      g_message->exc_data.push_back(exc_data[i]);
+    g_message->AppendExceptionData(exc_data, exc_data_count);
     return KERN_SUCCESS;
   } else if (!MachTask::IsValid(g_message->task_port)) {
     // Our original exception port isn't valid anymore check for a SIGTRAP
@@ -126,8 +125,7 @@ catch_mach_exception_raise(mach_port_t exc_port, mach_port_t thread_port,
       g_message->task_port = task_port;
       g_message->thread_port = thread_port;
       g_message->exc_type = exc_type;
-      for (mach_msg_type_number_t i=0; i<exc_data_count; ++i)
-        g_message->exc_data.push_back(exc_data[i]);
+      g_message->AppendExceptionData(exc_data, exc_data_count);
       return KERN_SUCCESS;
     }
   }
index a45a41e01f42f9438c438d52584d0738c57d0ec7..e1af12def10a772d5adf93f59ff976bf3ce0a909 100644 (file)
@@ -71,6 +71,15 @@ public:
       return (exc_type == EXC_BREAKPOINT ||
               ((exc_type == EXC_SOFTWARE) && exc_data[0] == 1));
     }
+    void AppendExceptionData(mach_exception_data_t Data,
+                             mach_msg_type_number_t Count) {
+      mach_exception_data_type_t Buf;
+      for (mach_msg_type_number_t i = 0; i < Count; ++i) {
+        // Perform an unaligned copy.
+        memcpy(&Buf, Data + i, sizeof(mach_exception_data_type_t));
+        exc_data.push_back(Buf);
+      }
+    }
     void Dump() const;
     void DumpStopReason() const;
     bool GetStopInfo(struct DNBThreadStopInfo *stop_info) const;