Bug 1445249 - Part 2 - avoid calling OtherPid() where it might return an invalid pid on android; r?jld draft
authorAlex Gaynor <agaynor@mozilla.com>
Tue, 13 Mar 2018 12:54:35 -0400
changeset 767011 ee0523f336cccc812ab3e22237ffab8bf402efc5
parent 767010 7c6cf0bc3e4f63d26ddfeb4b3b1a653e765a6bda
child 767012 f411f55f8d7443025f9b104e88f95576e4143cae
push id102475
push userbmo:agaynor@mozilla.com
push dateTue, 13 Mar 2018 19:30:50 +0000
reviewersjld
bugs1445249
milestone61.0a1
Bug 1445249 - Part 2 - avoid calling OtherPid() where it might return an invalid pid on android; r?jld MozReview-Commit-ID: EXio3oNJy4U
ipc/glue/ProtocolUtils.cpp
ipc/glue/ProtocolUtils.h
--- a/ipc/glue/ProtocolUtils.cpp
+++ b/ipc/glue/ProtocolUtils.cpp
@@ -592,16 +592,22 @@ IToplevelProtocol::~IToplevelProtocol()
     RefPtr<DeleteTask<Transport>> task = new DeleteTask<Transport>(mTrans.release());
     XRE_GetIOMessageLoop()->PostTask(task.forget());
   }
 }
 
 base::ProcessId
 IToplevelProtocol::OtherPid() const
 {
+  return OtherPidMaybeInvalid();
+}
+
+base::ProcessId
+IToplevelProtocol::OtherPidMaybeInvalid() const
+{
   return mOtherPid;
 }
 
 void
 IToplevelProtocol::SetOtherProcessId(base::ProcessId aOtherPid)
 {
   mOtherPid = aOtherPid;
 }
@@ -716,18 +722,29 @@ IToplevelProtocol::CreateSharedMemory(si
   if (!segment) {
     return nullptr;
   }
   int32_t id = GetSide() == ParentSide ? ++mLastShmemId : --mLastShmemId;
   Shmem shmem(
     Shmem::IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead(),
     segment.get(),
     id);
+
+  base::ProcessId pid =
+#ifdef ANDROID
+    // We use OtherPidMaybeInvalid() because on Android this method is actually
+    // called on an unconnected protocol, but Android's shared memory
+    // implementation doesn't actually use the PID.
+    OtherPidMaybeInvalid();
+#else
+    OtherPid();
+#endif
+
   Message* descriptor = shmem.ShareTo(
-    Shmem::IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead(), OtherPid(), MSG_ROUTING_CONTROL);
+    Shmem::IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead(), pid, MSG_ROUTING_CONTROL);
   if (!descriptor) {
     return nullptr;
   }
   Unused << GetIPCChannel()->Send(descriptor);
 
   *aId = shmem.Id(Shmem::IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead());
   Shmem::SharedMemory* rawSegment = segment.get();
   mShmemMap.AddWithID(segment.forget().take(), *aId);
--- a/ipc/glue/ProtocolUtils.h
+++ b/ipc/glue/ProtocolUtils.h
@@ -427,16 +427,18 @@ protected:
     virtual void ReplaceEventTargetForActorInternal(
       IProtocol* aActor,
       nsIEventTarget* aEventTarget) override;
 
     virtual already_AddRefed<nsIEventTarget>
     GetActorEventTargetInternal(IProtocol* aActor) override;
 
   private:
+    base::ProcessId OtherPidMaybeInvalid() const;
+
     ProtocolId mProtocolId;
     UniquePtr<Transport> mTrans;
     base::ProcessId mOtherPid;
     IDMap<IProtocol*> mActorMap;
     int32_t mLastRouteId;
     IDMap<Shmem::SharedMemory*> mShmemMap;
     Shmem::id_t mLastShmemId;
     bool mIsMainThreadProtocol;