xref: /aosp_15_r20/external/cronet/base/apple/dispatch_source_mach_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 The Chromium Authors
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/apple/dispatch_source_mach.h"
6 
7 #include <mach/mach.h>
8 
9 #include <memory>
10 
11 #include "base/apple/scoped_mach_port.h"
12 #include "base/logging.h"
13 #include "base/test/test_timeouts.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 namespace base::apple {
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, port,
27                                                    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(
35         semaphore, dispatch_time(DISPATCH_TIME_NOW,
36                                  TestTimeouts::action_timeout().InSeconds() *
37                                      NSEC_PER_SEC));
38   }
39 
40  private:
41   base::apple::ScopedMachReceiveRight receive_right_;
42   base::apple::ScopedMachSendRight send_right_;
43 };
44 
TEST_F(DispatchSourceMachTest,ReceiveAfterResume)45 TEST_F(DispatchSourceMachTest, ReceiveAfterResume) {
46   dispatch_semaphore_t signal = dispatch_semaphore_create(0);
47   mach_port_t port = GetPort();
48 
49   bool __block did_receive = false;
50   DispatchSourceMach source("org.chromium.base.test.ReceiveAfterResume", port,
51                             ^{
52                               mach_msg_empty_rcv_t msg = {{0}};
53                               msg.header.msgh_size = sizeof(msg);
54                               msg.header.msgh_local_port = port;
55                               mach_msg_receive(&msg.header);
56                               did_receive = true;
57 
58                               dispatch_semaphore_signal(signal);
59                             });
60 
61   mach_msg_empty_send_t msg = {{0}};
62   msg.header.msgh_size = sizeof(msg);
63   msg.header.msgh_remote_port = port;
64   msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND);
65   ASSERT_EQ(KERN_SUCCESS, mach_msg_send(&msg.header));
66 
67   EXPECT_FALSE(did_receive);
68 
69   source.Resume();
70 
71   WaitForSemaphore(signal);
72   dispatch_release(signal);
73 
74   EXPECT_TRUE(did_receive);
75 }
76 
TEST_F(DispatchSourceMachTest,NoMessagesAfterDestruction)77 TEST_F(DispatchSourceMachTest, NoMessagesAfterDestruction) {
78   mach_port_t port = GetPort();
79 
80   std::unique_ptr<int> count(new int(0));
81   int* __block count_ptr = count.get();
82 
83   std::unique_ptr<DispatchSourceMach> source(new DispatchSourceMach(
84       "org.chromium.base.test.NoMessagesAfterDestruction", port, ^{
85         mach_msg_empty_rcv_t msg = {{0}};
86         msg.header.msgh_size = sizeof(msg);
87         msg.header.msgh_local_port = port;
88         mach_msg_receive(&msg.header);
89         LOG(INFO) << "Receive " << *count_ptr;
90         ++(*count_ptr);
91       }));
92   source->Resume();
93 
94   dispatch_queue_t queue =
95       dispatch_queue_create("org.chromium.base.test.MessageSend", NULL);
96   dispatch_semaphore_t signal = dispatch_semaphore_create(0);
97   for (int i = 0; i < 30; ++i) {
98     dispatch_async(queue, ^{
99       mach_msg_empty_send_t msg = {{0}};
100       msg.header.msgh_size = sizeof(msg);
101       msg.header.msgh_remote_port = port;
102       msg.header.msgh_bits = 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::apple
126