ipc/glue/MessageLink.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: sw=4 ts=4 et :
michael@0 3 */
michael@0 4 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 #include "mozilla/ipc/MessageLink.h"
michael@0 9 #include "mozilla/ipc/MessageChannel.h"
michael@0 10 #include "mozilla/ipc/BrowserProcessSubThread.h"
michael@0 11 #include "mozilla/ipc/ProtocolUtils.h"
michael@0 12
michael@0 13 #ifdef MOZ_NUWA_PROCESS
michael@0 14 #include "ipc/Nuwa.h"
michael@0 15 #include "mozilla/Preferences.h"
michael@0 16 #endif
michael@0 17
michael@0 18 #include "nsDebug.h"
michael@0 19 #include "nsISupportsImpl.h"
michael@0 20 #include "nsXULAppAPI.h"
michael@0 21
michael@0 22 using namespace mozilla;
michael@0 23 using namespace std;
michael@0 24
michael@0 25 template<>
michael@0 26 struct RunnableMethodTraits<mozilla::ipc::ProcessLink>
michael@0 27 {
michael@0 28 static void RetainCallee(mozilla::ipc::ProcessLink* obj) { }
michael@0 29 static void ReleaseCallee(mozilla::ipc::ProcessLink* obj) { }
michael@0 30 };
michael@0 31
michael@0 32 // We rely on invariants about the lifetime of the transport:
michael@0 33 //
michael@0 34 // - outlives this MessageChannel
michael@0 35 // - deleted on the IO thread
michael@0 36 //
michael@0 37 // These invariants allow us to send messages directly through the
michael@0 38 // transport without having to worry about orphaned Send() tasks on
michael@0 39 // the IO thread touching MessageChannel memory after it's been deleted
michael@0 40 // on the worker thread. We also don't need to refcount the
michael@0 41 // Transport, because whatever task triggers its deletion only runs on
michael@0 42 // the IO thread, and only runs after this MessageChannel is done with
michael@0 43 // the Transport.
michael@0 44 template<>
michael@0 45 struct RunnableMethodTraits<mozilla::ipc::MessageChannel::Transport>
michael@0 46 {
michael@0 47 static void RetainCallee(mozilla::ipc::MessageChannel::Transport* obj) { }
michael@0 48 static void ReleaseCallee(mozilla::ipc::MessageChannel::Transport* obj) { }
michael@0 49 };
michael@0 50
michael@0 51 namespace mozilla {
michael@0 52 namespace ipc {
michael@0 53
michael@0 54 MessageLink::MessageLink(MessageChannel *aChan)
michael@0 55 : mChan(aChan)
michael@0 56 {
michael@0 57 }
michael@0 58
michael@0 59 MessageLink::~MessageLink()
michael@0 60 {
michael@0 61 mChan = nullptr;
michael@0 62 }
michael@0 63
michael@0 64 ProcessLink::ProcessLink(MessageChannel *aChan)
michael@0 65 : MessageLink(aChan),
michael@0 66 mExistingListener(nullptr)
michael@0 67 {
michael@0 68 }
michael@0 69
michael@0 70 ProcessLink::~ProcessLink()
michael@0 71 {
michael@0 72 mIOLoop = 0;
michael@0 73 if (mTransport) {
michael@0 74 mTransport->set_listener(0);
michael@0 75
michael@0 76 // we only hold a weak ref to the transport, which is "owned"
michael@0 77 // by GeckoChildProcess/GeckoThread
michael@0 78 mTransport = 0;
michael@0 79 }
michael@0 80 }
michael@0 81
michael@0 82 void
michael@0 83 ProcessLink::Open(mozilla::ipc::Transport* aTransport, MessageLoop *aIOLoop, Side aSide)
michael@0 84 {
michael@0 85 NS_PRECONDITION(aTransport, "need transport layer");
michael@0 86
michael@0 87 // FIXME need to check for valid channel
michael@0 88
michael@0 89 mTransport = aTransport;
michael@0 90
michael@0 91 // FIXME figure out whether we're in parent or child, grab IO loop
michael@0 92 // appropriately
michael@0 93 bool needOpen = true;
michael@0 94 if(aIOLoop) {
michael@0 95 // We're a child or using the new arguments. Either way, we
michael@0 96 // need an open.
michael@0 97 needOpen = true;
michael@0 98 mChan->mSide = (aSide == UnknownSide) ? ChildSide : aSide;
michael@0 99 } else {
michael@0 100 NS_PRECONDITION(aSide == UnknownSide, "expected default side arg");
michael@0 101
michael@0 102 // parent
michael@0 103 mChan->mSide = ParentSide;
michael@0 104 needOpen = false;
michael@0 105 aIOLoop = XRE_GetIOMessageLoop();
michael@0 106 }
michael@0 107
michael@0 108 mIOLoop = aIOLoop;
michael@0 109
michael@0 110 NS_ASSERTION(mIOLoop, "need an IO loop");
michael@0 111 NS_ASSERTION(mChan->mWorkerLoop, "need a worker loop");
michael@0 112
michael@0 113 {
michael@0 114 MonitorAutoLock lock(*mChan->mMonitor);
michael@0 115
michael@0 116 if (needOpen) {
michael@0 117 // Transport::Connect() has not been called. Call it so
michael@0 118 // we start polling our pipe and processing outgoing
michael@0 119 // messages.
michael@0 120 mIOLoop->PostTask(
michael@0 121 FROM_HERE,
michael@0 122 NewRunnableMethod(this, &ProcessLink::OnChannelOpened));
michael@0 123 } else {
michael@0 124 // Transport::Connect() has already been called. Take
michael@0 125 // over the channel from the previous listener and process
michael@0 126 // any queued messages.
michael@0 127 mIOLoop->PostTask(
michael@0 128 FROM_HERE,
michael@0 129 NewRunnableMethod(this, &ProcessLink::OnTakeConnectedChannel));
michael@0 130 }
michael@0 131
michael@0 132 #ifdef MOZ_NUWA_PROCESS
michael@0 133 if (IsNuwaProcess() &&
michael@0 134 Preferences::GetBool("dom.ipc.processPrelaunch.testMode")) {
michael@0 135 // The pref value is turned on in a deadlock test against the Nuwa
michael@0 136 // process. The sleep here makes it easy to trigger the deadlock
michael@0 137 // that an IPC channel is still opening but the worker loop is
michael@0 138 // already frozen.
michael@0 139 sleep(5);
michael@0 140 }
michael@0 141 #endif
michael@0 142
michael@0 143 // Should not wait here if something goes wrong with the channel.
michael@0 144 while (!mChan->Connected() && mChan->mChannelState != ChannelError) {
michael@0 145 mChan->mMonitor->Wait();
michael@0 146 }
michael@0 147 }
michael@0 148 }
michael@0 149
michael@0 150 void
michael@0 151 ProcessLink::EchoMessage(Message *msg)
michael@0 152 {
michael@0 153 mChan->AssertWorkerThread();
michael@0 154 mChan->mMonitor->AssertCurrentThreadOwns();
michael@0 155
michael@0 156 mIOLoop->PostTask(
michael@0 157 FROM_HERE,
michael@0 158 NewRunnableMethod(this, &ProcessLink::OnEchoMessage, msg));
michael@0 159 // OnEchoMessage takes ownership of |msg|
michael@0 160 }
michael@0 161
michael@0 162 void
michael@0 163 ProcessLink::SendMessage(Message *msg)
michael@0 164 {
michael@0 165 mChan->AssertWorkerThread();
michael@0 166 mChan->mMonitor->AssertCurrentThreadOwns();
michael@0 167
michael@0 168 mIOLoop->PostTask(
michael@0 169 FROM_HERE,
michael@0 170 NewRunnableMethod(mTransport, &Transport::Send, msg));
michael@0 171 }
michael@0 172
michael@0 173 void
michael@0 174 ProcessLink::SendClose()
michael@0 175 {
michael@0 176 mChan->AssertWorkerThread();
michael@0 177 mChan->mMonitor->AssertCurrentThreadOwns();
michael@0 178
michael@0 179 mIOLoop->PostTask(
michael@0 180 FROM_HERE, NewRunnableMethod(this, &ProcessLink::OnCloseChannel));
michael@0 181 }
michael@0 182
michael@0 183 ThreadLink::ThreadLink(MessageChannel *aChan, MessageChannel *aTargetChan)
michael@0 184 : MessageLink(aChan),
michael@0 185 mTargetChan(aTargetChan)
michael@0 186 {
michael@0 187 }
michael@0 188
michael@0 189 ThreadLink::~ThreadLink()
michael@0 190 {
michael@0 191 // :TODO: MonitorAutoLock lock(*mChan->mMonitor);
michael@0 192 // Bug 848949: We need to prevent the other side
michael@0 193 // from sending us any more messages to avoid Use-After-Free.
michael@0 194 // The setup here is as shown:
michael@0 195 //
michael@0 196 // (Us) (Them)
michael@0 197 // MessageChannel MessageChannel
michael@0 198 // | ^ \ / ^ |
michael@0 199 // | | X | |
michael@0 200 // v | / \ | v
michael@0 201 // ThreadLink ThreadLink
michael@0 202 //
michael@0 203 // We want to null out the diagonal link from their ThreadLink
michael@0 204 // to our MessageChannel. Note that we must hold the monitor so
michael@0 205 // that we do this atomically with respect to them trying to send
michael@0 206 // us a message.
michael@0 207 if (mTargetChan) {
michael@0 208 static_cast<ThreadLink*>(mTargetChan->mLink)->mTargetChan = 0;
michael@0 209 }
michael@0 210 mTargetChan = 0;
michael@0 211 }
michael@0 212
michael@0 213 void
michael@0 214 ThreadLink::EchoMessage(Message *msg)
michael@0 215 {
michael@0 216 mChan->AssertWorkerThread();
michael@0 217 mChan->mMonitor->AssertCurrentThreadOwns();
michael@0 218
michael@0 219 mChan->OnMessageReceivedFromLink(*msg);
michael@0 220 delete msg;
michael@0 221 }
michael@0 222
michael@0 223 void
michael@0 224 ThreadLink::SendMessage(Message *msg)
michael@0 225 {
michael@0 226 mChan->AssertWorkerThread();
michael@0 227 mChan->mMonitor->AssertCurrentThreadOwns();
michael@0 228
michael@0 229 if (mTargetChan)
michael@0 230 mTargetChan->OnMessageReceivedFromLink(*msg);
michael@0 231 delete msg;
michael@0 232 }
michael@0 233
michael@0 234 void
michael@0 235 ThreadLink::SendClose()
michael@0 236 {
michael@0 237 mChan->AssertWorkerThread();
michael@0 238 mChan->mMonitor->AssertCurrentThreadOwns();
michael@0 239
michael@0 240 mChan->mChannelState = ChannelClosed;
michael@0 241
michael@0 242 // In a ProcessLink, we would close our half the channel. This
michael@0 243 // would show up on the other side as an error on the I/O thread.
michael@0 244 // The I/O thread would then invoke OnChannelErrorFromLink().
michael@0 245 // As usual, we skip that process and just invoke the
michael@0 246 // OnChannelErrorFromLink() method directly.
michael@0 247 if (mTargetChan)
michael@0 248 mTargetChan->OnChannelErrorFromLink();
michael@0 249 }
michael@0 250
michael@0 251 bool
michael@0 252 ThreadLink::Unsound_IsClosed() const
michael@0 253 {
michael@0 254 MonitorAutoLock lock(*mChan->mMonitor);
michael@0 255 return mChan->mChannelState == ChannelClosed;
michael@0 256 }
michael@0 257
michael@0 258 uint32_t
michael@0 259 ThreadLink::Unsound_NumQueuedMessages() const
michael@0 260 {
michael@0 261 // ThreadLinks don't have a message queue.
michael@0 262 return 0;
michael@0 263 }
michael@0 264
michael@0 265 //
michael@0 266 // The methods below run in the context of the IO thread
michael@0 267 //
michael@0 268
michael@0 269 void
michael@0 270 ProcessLink::OnMessageReceived(const Message& msg)
michael@0 271 {
michael@0 272 AssertIOThread();
michael@0 273 NS_ASSERTION(mChan->mChannelState != ChannelError, "Shouldn't get here!");
michael@0 274 MonitorAutoLock lock(*mChan->mMonitor);
michael@0 275 mChan->OnMessageReceivedFromLink(msg);
michael@0 276 }
michael@0 277
michael@0 278 void
michael@0 279 ProcessLink::OnEchoMessage(Message* msg)
michael@0 280 {
michael@0 281 AssertIOThread();
michael@0 282 OnMessageReceived(*msg);
michael@0 283 delete msg;
michael@0 284 }
michael@0 285
michael@0 286 void
michael@0 287 ProcessLink::OnChannelOpened()
michael@0 288 {
michael@0 289 mChan->AssertLinkThread();
michael@0 290 {
michael@0 291 MonitorAutoLock lock(*mChan->mMonitor);
michael@0 292
michael@0 293 mExistingListener = mTransport->set_listener(this);
michael@0 294 #ifdef DEBUG
michael@0 295 if (mExistingListener) {
michael@0 296 queue<Message> pending;
michael@0 297 mExistingListener->GetQueuedMessages(pending);
michael@0 298 MOZ_ASSERT(pending.empty());
michael@0 299 }
michael@0 300 #endif // DEBUG
michael@0 301
michael@0 302 mChan->mChannelState = ChannelOpening;
michael@0 303 lock.Notify();
michael@0 304 }
michael@0 305 /*assert*/mTransport->Connect();
michael@0 306 }
michael@0 307
michael@0 308 void
michael@0 309 ProcessLink::OnTakeConnectedChannel()
michael@0 310 {
michael@0 311 AssertIOThread();
michael@0 312
michael@0 313 queue<Message> pending;
michael@0 314 {
michael@0 315 MonitorAutoLock lock(*mChan->mMonitor);
michael@0 316
michael@0 317 mChan->mChannelState = ChannelConnected;
michael@0 318
michael@0 319 mExistingListener = mTransport->set_listener(this);
michael@0 320 if (mExistingListener) {
michael@0 321 mExistingListener->GetQueuedMessages(pending);
michael@0 322 }
michael@0 323 lock.Notify();
michael@0 324 }
michael@0 325
michael@0 326 // Dispatch whatever messages the previous listener had queued up.
michael@0 327 while (!pending.empty()) {
michael@0 328 OnMessageReceived(pending.front());
michael@0 329 pending.pop();
michael@0 330 }
michael@0 331 }
michael@0 332
michael@0 333 void
michael@0 334 ProcessLink::OnChannelConnected(int32_t peer_pid)
michael@0 335 {
michael@0 336 AssertIOThread();
michael@0 337
michael@0 338 {
michael@0 339 MonitorAutoLock lock(*mChan->mMonitor);
michael@0 340 mChan->mChannelState = ChannelConnected;
michael@0 341 mChan->mMonitor->Notify();
michael@0 342 }
michael@0 343
michael@0 344 if (mExistingListener)
michael@0 345 mExistingListener->OnChannelConnected(peer_pid);
michael@0 346
michael@0 347 mChan->OnChannelConnected(peer_pid);
michael@0 348 }
michael@0 349
michael@0 350 void
michael@0 351 ProcessLink::OnChannelError()
michael@0 352 {
michael@0 353 AssertIOThread();
michael@0 354 MonitorAutoLock lock(*mChan->mMonitor);
michael@0 355 mChan->OnChannelErrorFromLink();
michael@0 356 }
michael@0 357
michael@0 358 void
michael@0 359 ProcessLink::OnCloseChannel()
michael@0 360 {
michael@0 361 AssertIOThread();
michael@0 362
michael@0 363 mTransport->Close();
michael@0 364
michael@0 365 MonitorAutoLock lock(*mChan->mMonitor);
michael@0 366 mChan->mChannelState = ChannelClosed;
michael@0 367 mChan->mMonitor->Notify();
michael@0 368 }
michael@0 369
michael@0 370 bool
michael@0 371 ProcessLink::Unsound_IsClosed() const
michael@0 372 {
michael@0 373 return mTransport->Unsound_IsClosed();
michael@0 374 }
michael@0 375
michael@0 376 uint32_t
michael@0 377 ProcessLink::Unsound_NumQueuedMessages() const
michael@0 378 {
michael@0 379 return mTransport->Unsound_NumQueuedMessages();
michael@0 380 }
michael@0 381
michael@0 382 } // namespace ipc
michael@0 383 } // namespace mozilla

mercurial