1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/mac/dispatch_source_mach.h"
6 
7 #include <mach/mach.h>
8 
9 #include <memory>
10 
11 #include "base/logging.h"
12 #include "base/mac/scoped_mach_port.h"
13 #include "base/test/test_timeouts.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 namespace base {
17 
18 class DispatchSourceMachTest : public testing::Test {
19  public:
SetUp()20   void SetUp() override {
21     mach_port_t port = MACH_PORT_NULL;
22     ASSERT_EQ(KERN_SUCCESS, mach_port_allocate(mach_task_self(),
23         MACH_PORT_RIGHT_RECEIVE, &port));
24     receive_right_.reset(port);
25 
26     ASSERT_EQ(KERN_SUCCESS, mach_port_insert_right(mach_task_self(), port,
27         port, MACH_MSG_TYPE_MAKE_SEND));
28     send_right_.reset(port);
29   }
30 
GetPort()31   mach_port_t GetPort() { return receive_right_.get(); }
32 
WaitForSemaphore(dispatch_semaphore_t semaphore)33   void WaitForSemaphore(dispatch_semaphore_t semaphore) {
34     dispatch_semaphore_wait(semaphore, dispatch_time(
35         DISPATCH_TIME_NOW,
36         TestTimeouts::action_timeout().InSeconds() * NSEC_PER_SEC));
37   }
38 
39  private:
40   base::mac::ScopedMachReceiveRight receive_right_;
41   base::mac::ScopedMachSendRight send_right_;
42 };
43 
TEST_F(DispatchSourceMachTest,ReceiveAfterResume)44 TEST_F(DispatchSourceMachTest, ReceiveAfterResume) {
45   dispatch_semaphore_t signal = dispatch_semaphore_create(0);
46   mach_port_t port = GetPort();
47 
48   bool __block did_receive = false;
49   DispatchSourceMach source("org.chromium.base.test.ReceiveAfterResume",
50       port, ^{
51           mach_msg_empty_rcv_t msg = {{0}};
52           msg.header.msgh_size = sizeof(msg);
53           msg.header.msgh_local_port = port;
54           mach_msg_receive(&msg.header);
55           did_receive = true;
56 
57           dispatch_semaphore_signal(signal);
58       });
59 
60   mach_msg_empty_send_t msg = {{0}};
61   msg.header.msgh_size = sizeof(msg);
62   msg.header.msgh_remote_port = port;
63   msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND);
64   ASSERT_EQ(KERN_SUCCESS, mach_msg_send(&msg.header));
65 
66   EXPECT_FALSE(did_receive);
67 
68   source.Resume();
69 
70   WaitForSemaphore(signal);
71   dispatch_release(signal);
72 
73   EXPECT_TRUE(did_receive);
74 }
75 
TEST_F(DispatchSourceMachTest,NoMessagesAfterDestruction)76 TEST_F(DispatchSourceMachTest, NoMessagesAfterDestruction) {
77   mach_port_t port = GetPort();
78 
79   std::unique_ptr<int> count(new int(0));
80   int* __block count_ptr = count.get();
81 
82   std::unique_ptr<DispatchSourceMach> source(new DispatchSourceMach(
83       "org.chromium.base.test.NoMessagesAfterDestruction", port, ^{
84         mach_msg_empty_rcv_t msg = {{0}};
85         msg.header.msgh_size = sizeof(msg);
86         msg.header.msgh_local_port = port;
87         mach_msg_receive(&msg.header);
88         LOG(INFO) << "Receieve " << *count_ptr;
89         ++(*count_ptr);
90       }));
91   source->Resume();
92 
93   dispatch_queue_t queue =
94       dispatch_queue_create("org.chromium.base.test.MessageSend", NULL);
95   dispatch_semaphore_t signal = dispatch_semaphore_create(0);
96   for (int i = 0; i < 30; ++i) {
97     dispatch_async(queue, ^{
98         mach_msg_empty_send_t msg = {{0}};
99         msg.header.msgh_size = sizeof(msg);
100         msg.header.msgh_remote_port = port;
101         msg.header.msgh_bits =
102             MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND);
103         mach_msg_send(&msg.header);
104     });
105 
106     // After sending five messages, shut down the source and taint the
107     // pointer the handler dereferences. The test will crash if |count_ptr|
108     // is being used after "free".
109     if (i == 5) {
110       std::unique_ptr<DispatchSourceMach>* source_ptr = &source;
111       dispatch_async(queue, ^{
112           source_ptr->reset();
113           count_ptr = reinterpret_cast<int*>(0xdeaddead);
114           dispatch_semaphore_signal(signal);
115       });
116     }
117   }
118 
119   WaitForSemaphore(signal);
120   dispatch_release(signal);
121 
122   dispatch_release(queue);
123 }
124 
125 }  // namespace base
126