mac: Fix crashpad_snapshot_test ProcessReaderMac.Child*Thread* for 10.14

pthread_threadid_np() reports an incorrect thread ID after fork() on
macOS 10.14 (“Mojave”). See https://openradar.appspot.com/43843552. As a
workaround, use thread_info(…, THREAD_IDENTIFIER_INFO, …).

This uses MachThreadSelf(), which in turn uses pthread_mach_thread_np(),
which does not suffer from the same bug. As an alternative,
base::mac::ScopedMachSendRight(mach_thread_self()) could be used.

Bug: crashpad:249
Change-Id: I757d6e94236cff533b9c1326f028110b6d214ee5
Reviewed-on: https://chromium-review.googlesource.com/c/1318271
Reviewed-by: Joshua Peraza <jperaza@chromium.org>
Commit-Queue: Mark Mentovai <mark@chromium.org>
This commit is contained in:
Mark Mentovai 2018-11-05 13:34:51 -05:00 committed by Commit Bot
parent 1b20b620cf
commit 131dd81d4c

View File

@ -15,6 +15,7 @@
#include "snapshot/mac/process_reader_mac.h" #include "snapshot/mac/process_reader_mac.h"
#include <AvailabilityMacros.h> #include <AvailabilityMacros.h>
#include <errno.h>
#include <OpenCL/opencl.h> #include <OpenCL/opencl.h>
#include <mach-o/dyld.h> #include <mach-o/dyld.h>
#include <mach-o/dyld_images.h> #include <mach-o/dyld_images.h>
@ -26,7 +27,7 @@
#include <utility> #include <utility>
#include "base/logging.h" #include "base/logging.h"
#include "base/mac/scoped_mach_port.h" #include "base/mac/mach_logging.h"
#include "base/posix/eintr_wrapper.h" #include "base/posix/eintr_wrapper.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "build/build_config.h" #include "build/build_config.h"
@ -127,8 +128,8 @@ TEST(ProcessReaderMac, ChildBasic) {
// This function CHECKs success and returns the thread ID directly. // This function CHECKs success and returns the thread ID directly.
uint64_t PthreadToThreadID(pthread_t pthread) { uint64_t PthreadToThreadID(pthread_t pthread) {
uint64_t thread_id; uint64_t thread_id;
int rv = pthread_threadid_np(pthread, &thread_id); errno = pthread_threadid_np(pthread, &thread_id);
CHECK_EQ(rv, 0); PCHECK(errno == 0) << "pthread_threadid_np";
return thread_id; return thread_id;
} }
@ -410,6 +411,18 @@ TEST(ProcessReaderMac, SelfSeveralThreads) {
EXPECT_TRUE(found_thread_self); EXPECT_TRUE(found_thread_self);
} }
uint64_t GetThreadID() {
thread_identifier_info info;
mach_msg_type_number_t info_count = THREAD_IDENTIFIER_INFO_COUNT;
kern_return_t kr = thread_info(MachThreadSelf(),
THREAD_IDENTIFIER_INFO,
reinterpret_cast<thread_info_t>(&info),
&info_count);
MACH_CHECK(kr == KERN_SUCCESS, kr) << "thread_info";
return info.thread_id;
}
class ProcessReaderThreadedChild final : public MachMultiprocess { class ProcessReaderThreadedChild final : public MachMultiprocess {
public: public:
explicit ProcessReaderThreadedChild(size_t thread_count) explicit ProcessReaderThreadedChild(size_t thread_count)
@ -463,7 +476,7 @@ class ProcessReaderThreadedChild final : public MachMultiprocess {
// This thread isnt part of the thread pool, but the parent will be able // This thread isnt part of the thread pool, but the parent will be able
// to inspect it. Write an entry for it. // to inspect it. Write an entry for it.
uint64_t thread_id = PthreadToThreadID(pthread_self()); uint64_t thread_id = GetThreadID();
CheckedWriteFile(write_handle, &thread_id, sizeof(thread_id)); CheckedWriteFile(write_handle, &thread_id, sizeof(thread_id));