Bug 1758813 [wpt PR 33142] - Implement RP sign out, a=testonly
[gecko.git] / ipc / glue / SharedMemory.h
blobd9d40806f5a6ff36cbd6f82f2350f85e7bd359c6
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_ipc_SharedMemory_h
8 #define mozilla_ipc_SharedMemory_h
10 #include "nsDebug.h"
11 #include "nsISupportsImpl.h" // NS_INLINE_DECL_REFCOUNTING
12 #include "mozilla/Attributes.h"
14 #include "base/process.h"
15 #include "chrome/common/ipc_message_utils.h"
18 // This is a low-level wrapper around platform shared memory. Don't
19 // use it directly; use Shmem allocated through IPDL interfaces.
21 namespace {
22 enum Rights { RightsNone = 0, RightsRead = 1 << 0, RightsWrite = 1 << 1 };
23 } // namespace
25 namespace mozilla {
27 namespace ipc {
28 class SharedMemory;
29 } // namespace ipc
31 namespace ipc {
33 class SharedMemory {
34 protected:
35 virtual ~SharedMemory() {
36 Unmapped();
37 Destroyed();
40 public:
41 enum SharedMemoryType { TYPE_BASIC, TYPE_UNKNOWN };
43 enum OpenRights {
44 RightsReadOnly = RightsRead,
45 RightsReadWrite = RightsRead | RightsWrite,
48 size_t Size() const { return mMappedSize; }
50 virtual void* memory() const = 0;
52 virtual bool Create(size_t size) = 0;
53 virtual bool Map(size_t nBytes, void* fixed_address = nullptr) = 0;
54 virtual void Unmap() = 0;
56 virtual void CloseHandle() = 0;
58 virtual SharedMemoryType Type() const = 0;
60 virtual bool WriteHandle(IPC::MessageWriter* aWriter) = 0;
61 virtual bool ReadHandle(IPC::MessageReader* aReader) = 0;
63 void Protect(char* aAddr, size_t aSize, int aRights) {
64 char* memStart = reinterpret_cast<char*>(memory());
65 if (!memStart) MOZ_CRASH("SharedMemory region points at NULL!");
66 char* memEnd = memStart + Size();
68 char* protStart = aAddr;
69 if (!protStart) MOZ_CRASH("trying to Protect() a NULL region!");
70 char* protEnd = protStart + aSize;
72 if (!(memStart <= protStart && protEnd <= memEnd))
73 MOZ_CRASH("attempt to Protect() a region outside this SharedMemory");
75 // checks alignment etc.
76 SystemProtect(aAddr, aSize, aRights);
79 // bug 1168843, compositor thread may create shared memory instances that are
80 // destroyed by main thread on shutdown, so this must use thread-safe RC to
81 // avoid hitting assertion
82 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SharedMemory)
84 static void SystemProtect(char* aAddr, size_t aSize, int aRights);
85 [[nodiscard]] static bool SystemProtectFallible(char* aAddr, size_t aSize,
86 int aRights);
87 static size_t SystemPageSize();
88 static size_t PageAlignedSize(size_t aSize);
90 protected:
91 SharedMemory();
93 // Implementations should call these methods on shmem usage changes,
94 // but *only if* the OS-specific calls are known to have succeeded.
95 // The methods are expected to be called in the pattern
97 // Created (Mapped Unmapped)* Destroy
99 // but this isn't checked.
100 void Created(size_t aNBytes);
101 void Mapped(size_t aNBytes);
102 void Unmapped();
103 void Destroyed();
105 // The size of the shmem region requested in Create(), if
106 // successful. SharedMemory instances that are opened from a
107 // foreign handle have an alloc size of 0, even though they have
108 // access to the alloc-size information.
109 size_t mAllocSize;
110 // The size of the region mapped in Map(), if successful. All
111 // SharedMemorys that are mapped have a non-zero mapped size.
112 size_t mMappedSize;
115 template <typename HandleImpl>
116 class SharedMemoryCommon : public SharedMemory {
117 public:
118 typedef HandleImpl Handle;
120 virtual Handle CloneHandle() = 0;
121 virtual bool IsHandleValid(const Handle& aHandle) const = 0;
122 virtual bool SetHandle(Handle aHandle, OpenRights aRights) = 0;
124 virtual bool WriteHandle(IPC::MessageWriter* aWriter) override {
125 Handle handle = CloneHandle();
126 if (!handle) {
127 return false;
129 IPC::WriteParam(aWriter, std::move(handle));
130 return true;
133 virtual bool ReadHandle(IPC::MessageReader* aReader) override {
134 Handle handle;
135 return IPC::ReadParam(aReader, &handle) && IsHandleValid(handle) &&
136 SetHandle(std::move(handle), RightsReadWrite);
140 } // namespace ipc
141 } // namespace mozilla
143 #endif // ifndef mozilla_ipc_SharedMemory_h