diff options
author | rsesek <rsesek@chromium.org> | 2015-05-13 12:49:10 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-05-13 19:50:15 +0000 |
commit | 6cba592c47a536fe2b5cb10f8d650f8ba4c99766 (patch) | |
tree | 3be4e4abf50d304762c8d71587928e57857af525 /sandbox | |
parent | 1e53d034efe6813bf0f2cfe6dc2658c37afe192c (diff) | |
download | chromium_src-6cba592c47a536fe2b5cb10f8d650f8ba4c99766.zip chromium_src-6cba592c47a536fe2b5cb10f8d650f8ba4c99766.tar.gz chromium_src-6cba592c47a536fe2b5cb10f8d650f8ba4c99766.tar.bz2 |
Move DispatchSourceMach from //sandbox/mac to //base/mac.
BUG=487674
Review URL: https://codereview.chromium.org/1136953012
Cr-Commit-Position: refs/heads/master@{#329700}
Diffstat (limited to 'sandbox')
-rw-r--r-- | sandbox/mac/BUILD.gn | 3 | ||||
-rw-r--r-- | sandbox/mac/dispatch_source_mach.cc | 62 | ||||
-rw-r--r-- | sandbox/mac/dispatch_source_mach.h | 61 | ||||
-rw-r--r-- | sandbox/mac/dispatch_source_mach_unittest.cc | 119 | ||||
-rw-r--r-- | sandbox/mac/mach_message_server.cc | 3 | ||||
-rw-r--r-- | sandbox/mac/mach_message_server.h | 5 | ||||
-rw-r--r-- | sandbox/mac/sandbox_mac.gypi | 3 | ||||
-rw-r--r-- | sandbox/mac/xpc_message_server.cc | 3 | ||||
-rw-r--r-- | sandbox/mac/xpc_message_server.h | 5 |
9 files changed, 6 insertions, 258 deletions
diff --git a/sandbox/mac/BUILD.gn b/sandbox/mac/BUILD.gn index d74bfcb..13960bf 100644 --- a/sandbox/mac/BUILD.gn +++ b/sandbox/mac/BUILD.gn @@ -48,8 +48,6 @@ component("sandbox") { sources = [ "bootstrap_sandbox.cc", "bootstrap_sandbox.h", - "dispatch_source_mach.cc", - "dispatch_source_mach.h", "launchd_interception_server.cc", "launchd_interception_server.h", "mach_message_server.cc", @@ -85,7 +83,6 @@ component("sandbox") { test("sandbox_mac_unittests") { sources = [ "bootstrap_sandbox_unittest.mm", - "dispatch_source_mach_unittest.cc", "policy_unittest.cc", "xpc_message_server_unittest.cc", ] diff --git a/sandbox/mac/dispatch_source_mach.cc b/sandbox/mac/dispatch_source_mach.cc deleted file mode 100644 index a6f3ac8..0000000 --- a/sandbox/mac/dispatch_source_mach.cc +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "sandbox/mac/dispatch_source_mach.h" - -namespace sandbox { - -DispatchSourceMach::DispatchSourceMach(const char* name, - mach_port_t port, - void (^event_handler)()) - // TODO(rsesek): Specify DISPATCH_QUEUE_SERIAL, in the 10.7 SDK. NULL - // means the same thing but is not symbolically clear. - : DispatchSourceMach(dispatch_queue_create(name, NULL), - port, - event_handler) { - // Since the queue was created above in the delegated constructor, and it was - // subsequently retained, release it here. - dispatch_release(queue_); -} - -DispatchSourceMach::DispatchSourceMach(dispatch_queue_t queue, - mach_port_t port, - void (^event_handler)()) - : queue_(queue), - source_(dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV, - port, 0, queue_)), - source_canceled_(dispatch_semaphore_create(0)) { - dispatch_retain(queue); - - dispatch_source_set_event_handler(source_, event_handler); - dispatch_source_set_cancel_handler(source_, ^{ - dispatch_semaphore_signal(source_canceled_); - }); -} - -DispatchSourceMach::~DispatchSourceMach() { - Cancel(); -} - -void DispatchSourceMach::Resume() { - dispatch_resume(source_); -} - -void DispatchSourceMach::Cancel() { - if (source_) { - dispatch_source_cancel(source_); - dispatch_release(source_); - source_ = NULL; - - dispatch_semaphore_wait(source_canceled_, DISPATCH_TIME_FOREVER); - dispatch_release(source_canceled_); - source_canceled_ = NULL; - } - - if (queue_) { - dispatch_release(queue_); - queue_ = NULL; - } -} - -} // namespace sandbox diff --git a/sandbox/mac/dispatch_source_mach.h b/sandbox/mac/dispatch_source_mach.h deleted file mode 100644 index e3a3aa0..0000000 --- a/sandbox/mac/dispatch_source_mach.h +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef SANDBOX_MAC_DISPATCH_SOURCE_MACH_H_ -#define SANDBOX_MAC_DISPATCH_SOURCE_MACH_H_ - -#include <dispatch/dispatch.h> - -#include "base/macros.h" -#include "sandbox/sandbox_export.h" - -namespace sandbox { - -// This class encapsulates a MACH_RECV dispatch source. When this object is -// destroyed, the source will be cancelled and it will wait for the source -// to stop executing work. The source can run on either a user-supplied queue, -// or it can create its own for the source. -class SANDBOX_EXPORT DispatchSourceMach { - public: - // Creates a new dispatch source for the |port| and schedules it on a new - // queue that will be created with |name|. When a Mach message is received, - // the |event_handler| will be called. - DispatchSourceMach(const char* name, - mach_port_t port, - void (^event_handler)()); - - // Creates a new dispatch source with the same semantics as above, but rather - // than creating a new queue, it schedules the source on |queue|. - DispatchSourceMach(dispatch_queue_t queue, - mach_port_t port, - void (^event_handler)()); - - // Cancels the source and waits for it to become fully cancelled before - // releasing the source. - ~DispatchSourceMach(); - - // Resumes the source. This must be called before any Mach messages will - // be received. - void Resume(); - - private: - // Cancels the source, after which this class will no longer receive Mach - // messages. Calling this more than once is a no-op. - void Cancel(); - - // The dispatch queue used to service the source_. - dispatch_queue_t queue_; - - // A MACH_RECV dispatch source. - dispatch_source_t source_; - - // Semaphore used to wait on the |source_|'s cancellation in the destructor. - dispatch_semaphore_t source_canceled_; - - DISALLOW_COPY_AND_ASSIGN(DispatchSourceMach); -}; - -} // namespace sandbox - -#endif // SANDBOX_MAC_DISPATCH_SOURCE_MACH_H_ diff --git a/sandbox/mac/dispatch_source_mach_unittest.cc b/sandbox/mac/dispatch_source_mach_unittest.cc deleted file mode 100644 index 123a44c..0000000 --- a/sandbox/mac/dispatch_source_mach_unittest.cc +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "sandbox/mac/dispatch_source_mach.h" - -#include <mach/mach.h> - -#include "base/logging.h" -#include "base/mac/scoped_mach_port.h" -#include "base/memory/scoped_ptr.h" -#include "base/test/test_timeouts.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace sandbox { - -class DispatchSourceMachTest : public testing::Test { - public: - void SetUp() override { - mach_port_t port = MACH_PORT_NULL; - ASSERT_EQ(KERN_SUCCESS, mach_port_allocate(mach_task_self(), - MACH_PORT_RIGHT_RECEIVE, &port)); - receive_right_.reset(port); - - ASSERT_EQ(KERN_SUCCESS, mach_port_insert_right(mach_task_self(), port, - port, MACH_MSG_TYPE_MAKE_SEND)); - send_right_.reset(port); - } - - mach_port_t port() { return receive_right_.get(); } - - void WaitForSemaphore(dispatch_semaphore_t semaphore) { - dispatch_semaphore_wait(semaphore, dispatch_time( - DISPATCH_TIME_NOW, - TestTimeouts::action_timeout().InSeconds() * NSEC_PER_SEC)); - } - - private: - base::mac::ScopedMachReceiveRight receive_right_; - base::mac::ScopedMachSendRight send_right_; -}; - -TEST_F(DispatchSourceMachTest, ReceiveAfterResume) { - dispatch_semaphore_t signal = dispatch_semaphore_create(0); - - bool __block did_receive = false; - DispatchSourceMach source("org.chromium.sandbox.test.ReceiveAfterResume", - port(), ^{ - mach_msg_empty_rcv_t msg = {{0}}; - msg.header.msgh_size = sizeof(msg); - msg.header.msgh_local_port = port(); - mach_msg_receive(&msg.header); - did_receive = true; - - dispatch_semaphore_signal(signal); - }); - - mach_msg_empty_send_t msg = {{0}}; - msg.header.msgh_size = sizeof(msg); - msg.header.msgh_remote_port = port(); - msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND); - ASSERT_EQ(KERN_SUCCESS, mach_msg_send(&msg.header)); - - EXPECT_FALSE(did_receive); - - source.Resume(); - - WaitForSemaphore(signal); - - EXPECT_TRUE(did_receive); -} - -TEST_F(DispatchSourceMachTest, NoMessagesAfterDestruction) { - scoped_ptr<int> count(new int(0)); - int* __block count_ptr = count.get(); - - scoped_ptr<DispatchSourceMach> source(new DispatchSourceMach( - "org.chromium.sandbox.test.NoMessagesAfterDestruction", - port(), ^{ - mach_msg_empty_rcv_t msg = {{0}}; - msg.header.msgh_size = sizeof(msg); - msg.header.msgh_local_port = port(); - mach_msg_receive(&msg.header); - LOG(INFO) << "Receieve " << *count_ptr; - ++(*count_ptr); - })); - source->Resume(); - - dispatch_queue_t queue = - dispatch_queue_create("org.chromium.sandbox.test.MessageSend", NULL); - dispatch_semaphore_t signal = dispatch_semaphore_create(0); - for (int i = 0; i < 30; ++i) { - dispatch_async(queue, ^{ - mach_msg_empty_send_t msg = {{0}}; - msg.header.msgh_size = sizeof(msg); - msg.header.msgh_remote_port = port(); - msg.header.msgh_bits = - MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND); - mach_msg_send(&msg.header); - }); - - // After sending five messages, shut down the source and taint the - // pointer the handler dereferences. The test will crash if |count_ptr| - // is being used after "free". - if (i == 5) { - scoped_ptr<DispatchSourceMach>* source_ptr = &source; - dispatch_async(queue, ^{ - source_ptr->reset(); - count_ptr = reinterpret_cast<int*>(0xdeaddead); - dispatch_semaphore_signal(signal); - }); - } - } - - WaitForSemaphore(signal); - dispatch_release(queue); -} - -} // namespace sandbox diff --git a/sandbox/mac/mach_message_server.cc b/sandbox/mac/mach_message_server.cc index 5a73357..7cfcecc 100644 --- a/sandbox/mac/mach_message_server.cc +++ b/sandbox/mac/mach_message_server.cc @@ -12,7 +12,6 @@ #include "base/logging.h" #include "base/mac/mach_logging.h" #include "base/strings/stringprintf.h" -#include "sandbox/mac/dispatch_source_mach.h" namespace sandbox { @@ -69,7 +68,7 @@ bool MachMessageServer::Initialize() { // Set up the dispatch queue to service the bootstrap port. std::string label = base::StringPrintf( "org.chromium.sandbox.MachMessageServer.%p", demuxer_); - dispatch_source_.reset(new DispatchSourceMach( + dispatch_source_.reset(new base::DispatchSourceMach( label.c_str(), server_port_.get(), ^{ ReceiveMessage(); })); dispatch_source_->Resume(); diff --git a/sandbox/mac/mach_message_server.h b/sandbox/mac/mach_message_server.h index 645b691..20a543b 100644 --- a/sandbox/mac/mach_message_server.h +++ b/sandbox/mac/mach_message_server.h @@ -7,6 +7,7 @@ #include <mach/mach.h> +#include "base/mac/dispatch_source_mach.h" #include "base/mac/scoped_mach_port.h" #include "base/mac/scoped_mach_vm.h" #include "base/memory/scoped_ptr.h" @@ -14,8 +15,6 @@ namespace sandbox { -class DispatchSourceMach; - // A Mach message server that operates a receive port. Messages are received // and then passed to the MessageDemuxer for handling. The Demuxer // can use the server class to send a reply, forward the message to a @@ -62,7 +61,7 @@ class MachMessageServer : public MessageServer { base::mac::ScopedMachVM reply_buffer_; // MACH_RECV dispatch source that handles the |server_port_|. - scoped_ptr<DispatchSourceMach> dispatch_source_; + scoped_ptr<base::DispatchSourceMach> dispatch_source_; // Whether or not ForwardMessage() was called during ReceiveMessage(). bool did_forward_message_; diff --git a/sandbox/mac/sandbox_mac.gypi b/sandbox/mac/sandbox_mac.gypi index a2a616f..d5013f8 100644 --- a/sandbox/mac/sandbox_mac.gypi +++ b/sandbox/mac/sandbox_mac.gypi @@ -10,8 +10,6 @@ 'sources': [ 'bootstrap_sandbox.cc', 'bootstrap_sandbox.h', - 'dispatch_source_mach.cc', - 'dispatch_source_mach.h', 'launchd_interception_server.cc', 'launchd_interception_server.h', 'mach_message_server.cc', @@ -93,7 +91,6 @@ 'type': 'executable', 'sources': [ 'bootstrap_sandbox_unittest.mm', - 'dispatch_source_mach_unittest.cc', 'policy_unittest.cc', 'xpc_message_server_unittest.cc', ], diff --git a/sandbox/mac/xpc_message_server.cc b/sandbox/mac/xpc_message_server.cc index 45559dd5..1cd5c63 100644 --- a/sandbox/mac/xpc_message_server.cc +++ b/sandbox/mac/xpc_message_server.cc @@ -10,7 +10,6 @@ #include "base/mac/mach_logging.h" #include "base/strings/stringprintf.h" -#include "sandbox/mac/dispatch_source_mach.h" #include "sandbox/mac/xpc.h" #if defined(MAC_OS_X_VERSION_10_7) && \ @@ -52,7 +51,7 @@ bool XPCMessageServer::Initialize() { std::string label = base::StringPrintf( "org.chromium.sandbox.XPCMessageServer.%p", demuxer_); - dispatch_source_.reset(new DispatchSourceMach( + dispatch_source_.reset(new base::DispatchSourceMach( label.c_str(), server_port_.get(), ^{ ReceiveMessage(); })); dispatch_source_->Resume(); diff --git a/sandbox/mac/xpc_message_server.h b/sandbox/mac/xpc_message_server.h index 2810097..5f5a9fa 100644 --- a/sandbox/mac/xpc_message_server.h +++ b/sandbox/mac/xpc_message_server.h @@ -7,6 +7,7 @@ #include <AvailabilityMacros.h> +#include "base/mac/dispatch_source_mach.h" #include "base/mac/scoped_mach_port.h" #include "base/memory/scoped_ptr.h" #include "sandbox/mac/message_server.h" @@ -27,8 +28,6 @@ XPC_EXPORT XPC_NONNULL1 void xpc_release(xpc_object_t object); namespace sandbox { -class DispatchSourceMach; - // An implementation of MessageServer that uses XPC pipes to read and write XPC // messages from a Mach port. class SANDBOX_EXPORT XPCMessageServer : public MessageServer { @@ -62,7 +61,7 @@ class SANDBOX_EXPORT XPCMessageServer : public MessageServer { base::mac::ScopedMachReceiveRight server_port_; // MACH_RECV dispatch source that handles the |server_port_|. - scoped_ptr<DispatchSourceMach> dispatch_source_; + scoped_ptr<base::DispatchSourceMach> dispatch_source_; // The reply message, if one has been created. xpc_object_t reply_message_; |