summaryrefslogtreecommitdiffstats
path: root/ipc/glue/RawShmem.cpp
blob: bfb47c08325464d8f61b038c1475ab1a3f4b2c80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "RawShmem.h"
#include "mozilla/ipc/ProtocolUtils.h"

namespace mozilla::ipc {

UnsafeSharedMemoryHandle::UnsafeSharedMemoryHandle()
    : mHandle(ipc::SharedMemoryBasic::NULLHandle()), mSize(0) {}

UnsafeSharedMemoryHandle::UnsafeSharedMemoryHandle(
    UnsafeSharedMemoryHandle&& aOther) noexcept
    : mHandle(std::move(aOther.mHandle)), mSize(aOther.mSize) {
  aOther.mHandle = ipc::SharedMemoryBasic::NULLHandle();
  aOther.mSize = 0;
}

UnsafeSharedMemoryHandle& UnsafeSharedMemoryHandle::operator=(
    UnsafeSharedMemoryHandle&& aOther) noexcept {
  if (this == &aOther) {
    return *this;
  }

  mHandle = std::move(aOther.mHandle);
  mSize = aOther.mSize;
  aOther.mHandle = ipc::SharedMemoryBasic::NULLHandle();
  aOther.mSize = 0;
  return *this;
}

Maybe<std::pair<UnsafeSharedMemoryHandle, WritableSharedMemoryMapping>>
UnsafeSharedMemoryHandle::CreateAndMap(size_t aSize) {
  if (aSize == 0) {
    return Some(std::make_pair(UnsafeSharedMemoryHandle(),
                               WritableSharedMemoryMapping()));
  }

  RefPtr<ipc::SharedMemoryBasic> shm = MakeAndAddRef<ipc::SharedMemoryBasic>();
  if (NS_WARN_IF(!shm->Create(aSize)) || NS_WARN_IF(!shm->Map(aSize))) {
    return Nothing();
  }

  // TODO(bug 1797039): At the moment the handle/mapping distinction is
  // implemented on top of a single class SharedMemoryBasic. It leads to a few
  // awkward or sub-optimal things such as how the following few lines clone
  // then close the handle. It would be better to separate the underlying
  // implementation or steal the handle to avoid cloning it.
  auto handle = shm->CloneHandle();

  shm->CloseHandle();
  auto size = shm->Size();

  return Some(std::make_pair(UnsafeSharedMemoryHandle(std::move(handle), size),
                             WritableSharedMemoryMapping(std::move(shm))));
}

WritableSharedMemoryMapping::WritableSharedMemoryMapping(
    RefPtr<ipc::SharedMemoryBasic>&& aRef)
    : mRef(aRef) {}

Maybe<WritableSharedMemoryMapping> WritableSharedMemoryMapping::Open(
    UnsafeSharedMemoryHandle aHandle) {
  if (aHandle.mSize == 0) {
    return Some(WritableSharedMemoryMapping(nullptr));
  }

  RefPtr<ipc::SharedMemoryBasic> shm = MakeAndAddRef<ipc::SharedMemoryBasic>();
  if (NS_WARN_IF(!shm->SetHandle(std::move(aHandle.mHandle),
                                 ipc::SharedMemory::RightsReadWrite)) ||
      NS_WARN_IF(!shm->Map(aHandle.mSize))) {
    return Nothing();
  }

  shm->CloseHandle();

  return Some(WritableSharedMemoryMapping(std::move(shm)));
}

size_t WritableSharedMemoryMapping::Size() const {
  if (!mRef) {
    return 0;
  }

  return mRef->Size();
}

Span<uint8_t> WritableSharedMemoryMapping::Bytes() {
  if (!mRef) {
    return Span<uint8_t>();
  }

  uint8_t* mem = static_cast<uint8_t*>(mRef->memory());
  return Span(mem, mRef->Size());
}

}  // namespace mozilla::ipc

namespace IPC {
auto ParamTraits<mozilla::ipc::UnsafeSharedMemoryHandle>::Write(
    IPC::MessageWriter* aWriter, paramType&& aVar) -> void {
  IPC::WriteParam(aWriter, std::move(aVar.mHandle));
  IPC::WriteParam(aWriter, aVar.mSize);
}

auto ParamTraits<mozilla::ipc::UnsafeSharedMemoryHandle>::Read(
    IPC::MessageReader* aReader, paramType* aVar) -> bool {
  return IPC::ReadParam(aReader, &aVar->mHandle) &&
         IPC::ReadParam(aReader, &aVar->mSize);
}

}  // namespace IPC