ipcz: Purge cross-platform multiprocess driver code

This removes cross-platform OSHandle and Memory types from
src/reference_drivers, in favor of POSIX/Linux-specific FileDescriptor
and MemfdMemory types.

Per the resolution discussed on https://crrev.com/c/3651566, upstream
ipcz tests will not support multiprocess operation across all platforms
due to the required complexity and its diminishing returns.

We'll still have cross-platform coverage within a single-process,
multi-threaded environment, with both synchronous and asynchronous
drivers, as well as with other simulated constraints like object
brokering and delegated shared memory allocation.

For multiprocess coverage we'll have Linux support only. This is
sufficient to validate that various bits of ipcz proper operate
correctly when interconnected nodes are living in different address
spaces. Since those bits are fully cross-platform, adding coverage
across other platforms would not effectively increase ipcz test
coverage in any meaningful way, despite requiring a large volume of
additional platform-specific, test-only code.

This also decouples the "blob" test object type from any specific
driver implementation, allowing Box API integration tests to continue
running on all platforms.

Bug: 1299283
Change-Id: Ie83e440fae504ae9edb6a342c33f2581ffd73cdc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3693033
Reviewed-by: Alex Gough <ajgo@chromium.org>
Commit-Queue: Ken Rockot <rockot@google.com>
Cr-Commit-Position: refs/heads/main@{#1011737}
NOKEYCHECK=True
GitOrigin-RevId: 822ad8174309286661ee9b68bfcb7256c1265986
diff --git a/src/BUILD.gn b/src/BUILD.gn
index bb5c274..0744b26 100644
--- a/src/BUILD.gn
+++ b/src/BUILD.gn
@@ -5,10 +5,6 @@
 import("//build_overrides/ipcz.gni")
 import("//testing/test.gni")
 
-# ipcz will not implement a multiprocess reference driver or its supporting
-# primitives for iOS or NaCl platforms. Note that this only affects ipcz tests.
-enable_ipcz_multiprocess_test_support = !is_ios && !is_nacl
-
 shared_library("ipcz_shared") {
   output_name = "ipcz"
   sources = [
@@ -132,9 +128,13 @@
 ipcz_source_set("reference_drivers") {
   testonly = true
 
-  public = [ "reference_drivers/single_process_reference_driver.h" ]
+  public = [
+    "reference_drivers/blob.h",
+    "reference_drivers/single_process_reference_driver.h",
+  ]
 
   sources = [
+    "reference_drivers/blob.cc",
     "reference_drivers/object.cc",
     "reference_drivers/object.h",
     "reference_drivers/random.cc",
@@ -142,48 +142,15 @@
     "reference_drivers/single_process_reference_driver.cc",
   ]
 
-  if (enable_ipcz_multiprocess_test_support) {
+  if (is_linux) {
     public += [
-      "reference_drivers/blob.h",
-      "reference_drivers/memory.h",
-      "reference_drivers/os_handle.h",
+      "reference_drivers/file_descriptor.h",
+      "reference_drivers/memfd_memory.h",
     ]
     sources += [
-      "reference_drivers/blob.cc",
-      "reference_drivers/memory.cc",
+      "reference_drivers/file_descriptor.cc",
+      "reference_drivers/memfd_memory.cc",
     ]
-
-    if (is_android) {
-      sources += [
-        "reference_drivers/memory_android.cc",
-        "reference_drivers/os_handle_posix.cc",
-        "reference_drivers/os_handle_posix.h",
-      ]
-    } else if (is_mac) {
-      sources += [
-        "reference_drivers/memory_mac.cc",
-        "reference_drivers/os_handle_mac.cc",
-        "reference_drivers/os_handle_mac.h",
-      ]
-    } else if (is_win) {
-      sources += [
-        "reference_drivers/memory_win.cc",
-        "reference_drivers/os_handle_win.cc",
-        "reference_drivers/os_handle_win.h",
-      ]
-    } else if (is_fuchsia) {
-      sources += [
-        "reference_drivers/memory_fuchsia.cc",
-        "reference_drivers/os_handle_fuchsia.cc",
-        "reference_drivers/os_handle_fuchsia.h",
-      ]
-    } else if (is_posix) {
-      sources += [
-        "reference_drivers/memory_posix.cc",
-        "reference_drivers/os_handle_posix.cc",
-        "reference_drivers/os_handle_posix.h",
-      ]
-    }
   }
 
   ipcz_deps = [
@@ -333,6 +300,7 @@
 
   sources = [
     "api_test.cc",
+    "box_test.cc",
     "connect_test.cc",
     "ipcz/block_allocator_test.cc",
     "ipcz/buffer_pool_test.cc",
@@ -360,13 +328,8 @@
     "util/stack_trace_test.cc",
   ]
 
-  if (enable_ipcz_multiprocess_test_support) {
-    sources += [
-      # Box tests rely on the Blob driver object, which itself requires
-      # multiprocess test support.
-      "box_test.cc",
-      "reference_drivers/memory_test.cc",
-    ]
+  if (is_linux) {
+    sources += [ "reference_drivers/memfd_memory_test.cc" ]
   }
 
   deps = [
diff --git a/src/box_test.cc b/src/box_test.cc
index 32851e1..bd39f3d 100644
--- a/src/box_test.cc
+++ b/src/box_test.cc
@@ -7,8 +7,6 @@
 
 #include "ipcz/ipcz.h"
 #include "reference_drivers/blob.h"
-#include "reference_drivers/memory.h"
-#include "reference_drivers/os_handle.h"
 #include "test/multinode_test.h"
 #include "testing/gtest/include/gtest/gtest.h"
 #include "util/ref_counted.h"
@@ -16,48 +14,63 @@
 namespace ipcz {
 namespace {
 
-using BoxTestNode = test::TestNode;
-using BoxTest = test::MultinodeTest<BoxTestNode>;
-
 using Blob = reference_drivers::Blob;
 
-// Creates a test driver Blob object with an inlined data payload and a shared
-// memory object with an embedded message.
-IpczDriverHandle CreateTestBlob(std::string_view inline_message,
-                                std::string_view shm_message) {
-  reference_drivers::Memory memory(shm_message.size());
-  auto mapping = memory.Map();
-  memcpy(mapping.base(), shm_message.data(), shm_message.size());
-  reference_drivers::OSHandle memory_handle = memory.TakeHandle();
-  return Blob::ReleaseAsHandle(
-      MakeRefCounted<Blob>(inline_message, absl::MakeSpan(&memory_handle, 1)));
-}
+class BoxTestNode : public test::TestNode {
+ protected:
+  // Creates a test driver Blob object with an inlined data payload and a shared
+  // memory object with an embedded message.
+  IpczDriverHandle CreateTestBlob(std::string_view inline_message,
+                                  std::string_view shm_message) {
+    IpczDriverHandle memory;
+    EXPECT_EQ(IPCZ_RESULT_OK,
+              GetDriver().AllocateSharedMemory(
+                  shm_message.size(), IPCZ_NO_FLAGS, nullptr, &memory));
 
-bool BlobContentsMatch(IpczDriverHandle blob_handle,
-                       std::string_view expected_inline_message,
-                       std::string_view expected_shm_message) {
-  Ref<Blob> blob = Blob::TakeFromHandle(blob_handle);
-  if (expected_inline_message != blob->message()) {
-    return false;
+    void* address;
+    IpczDriverHandle mapping;
+    EXPECT_EQ(IPCZ_RESULT_OK,
+              GetDriver().MapSharedMemory(memory, IPCZ_NO_FLAGS, nullptr,
+                                          &address, &mapping));
+    memcpy(address, shm_message.data(), shm_message.size());
+    EXPECT_EQ(IPCZ_RESULT_OK,
+              GetDriver().Close(mapping, IPCZ_NO_FLAGS, nullptr));
+
+    return Blob::ReleaseAsHandle(MakeRefCounted<Blob>(
+        GetDriver(), inline_message, absl::MakeSpan(&memory, 1)));
   }
 
-  ABSL_ASSERT(blob->handles().size() == 1);
-  ABSL_ASSERT(blob->handles()[0].is_valid());
-  reference_drivers::Memory memory = reference_drivers::Memory(
-      std::move(blob->handles()[0]), expected_shm_message.size());
+  bool BlobContentsMatch(IpczDriverHandle blob_handle,
+                         std::string_view expected_inline_message,
+                         std::string_view expected_shm_message) {
+    Ref<Blob> blob = Blob::TakeFromHandle(blob_handle);
+    if (expected_inline_message != blob->message()) {
+      return false;
+    }
 
-  auto new_mapping = memory.Map();
-  if (expected_shm_message != std::string_view(new_mapping.As<char>())) {
-    return false;
+    ABSL_ASSERT(blob->handles().size() == 1);
+    ABSL_ASSERT(blob->handles()[0] != IPCZ_INVALID_DRIVER_HANDLE);
+
+    void* address;
+    IpczDriverHandle mapping;
+    EXPECT_EQ(IPCZ_RESULT_OK,
+              GetDriver().MapSharedMemory(blob->handles()[0], IPCZ_NO_FLAGS,
+                                          nullptr, &address, &mapping));
+    std::string_view shm_message(static_cast<char*>(address),
+                                 expected_shm_message.size());
+    const bool matched = (shm_message == expected_shm_message);
+    EXPECT_EQ(IPCZ_RESULT_OK,
+              GetDriver().Close(mapping, IPCZ_NO_FLAGS, nullptr));
+    return matched;
   }
+};
 
-  return true;
-}
+using BoxTest = test::MultinodeTest<BoxTestNode>;
 
 TEST_P(BoxTest, BoxAndUnbox) {
   constexpr const char kMessage[] = "Hello, world?";
   IpczDriverHandle blob_handle =
-      Blob::ReleaseAsHandle(MakeRefCounted<Blob>(kMessage));
+      Blob::ReleaseAsHandle(MakeRefCounted<Blob>(GetDriver(), kMessage));
 
   IpczHandle box;
   EXPECT_EQ(IPCZ_RESULT_OK,
@@ -72,7 +85,7 @@
 }
 
 TEST_P(BoxTest, CloseBox) {
-  Ref<Blob> blob = MakeRefCounted<Blob>("meh");
+  Ref<Blob> blob = MakeRefCounted<Blob>(GetDriver(), "meh");
   Ref<Blob::RefCountedFlag> destroyed = blob->destruction_flag_for_testing();
   IpczDriverHandle blob_handle = Blob::ReleaseAsHandle(std::move(blob));
 
@@ -88,7 +101,7 @@
 TEST_P(BoxTest, Peek) {
   constexpr const char kMessage[] = "Hello, world?";
   IpczDriverHandle blob_handle =
-      Blob::ReleaseAsHandle(MakeRefCounted<Blob>(kMessage));
+      Blob::ReleaseAsHandle(MakeRefCounted<Blob>(GetDriver(), kMessage));
   IpczHandle box;
   EXPECT_EQ(IPCZ_RESULT_OK,
             ipcz().Box(node(), blob_handle, IPCZ_NO_FLAGS, nullptr, &box));
diff --git a/src/reference_drivers/blob.cc b/src/reference_drivers/blob.cc
index fe184aa..451ad6f 100644
--- a/src/reference_drivers/blob.cc
+++ b/src/reference_drivers/blob.cc
@@ -14,12 +14,19 @@
 
 Blob::RefCountedFlag::~RefCountedFlag() = default;
 
-Blob::Blob(std::string_view message, absl::Span<OSHandle> handles)
-    : message_(message),
+Blob::Blob(const IpczDriver& driver,
+           std::string_view message,
+           absl::Span<IpczDriverHandle> handles)
+    : driver_(driver),
+      message_(message),
       handles_(std::move_iterator(handles.begin()),
                std::move_iterator(handles.end())) {}
 
-Blob::~Blob() = default;
+Blob::~Blob() {
+  for (IpczDriverHandle handle : handles_) {
+    driver_.Close(handle, IPCZ_NO_FLAGS, nullptr);
+  }
+}
 
 IpczResult Blob::Close() {
   destruction_flag_for_testing_->set(true);
diff --git a/src/reference_drivers/blob.h b/src/reference_drivers/blob.h
index 3bb9695..ef9ad18 100644
--- a/src/reference_drivers/blob.h
+++ b/src/reference_drivers/blob.h
@@ -10,15 +10,14 @@
 #include <vector>
 
 #include "reference_drivers/object.h"
-#include "reference_drivers/os_handle.h"
 #include "third_party/abseil-cpp/absl/types/span.h"
 #include "util/ref_counted.h"
 
 namespace ipcz::reference_drivers {
 
 // A driver-managed object which packages an arbitrary collection of string data
-// and OS handles. Blobs are serializable by both reference drivers and are used
-// to exercise driver object boxing in tests.
+// and transmissible driver handles. Blobs are used to exercise driver object
+// boxing in tests.
 //
 // Note that unlike the transport and memory objects defined by the reference
 // drivers, a blob is not a type of object known to ipcz. Instead it is used to
@@ -38,13 +37,15 @@
     bool flag_ = false;
   };
 
-  explicit Blob(std::string_view message, absl::Span<OSHandle> handles = {});
+  Blob(const IpczDriver& driver,
+       std::string_view message,
+       absl::Span<IpczDriverHandle> handles = {});
 
   // Object:
   IpczResult Close() override;
 
   std::string& message() { return message_; }
-  std::vector<OSHandle>& handles() { return handles_; }
+  std::vector<IpczDriverHandle>& handles() { return handles_; }
 
   const Ref<RefCountedFlag>& destruction_flag_for_testing() const {
     return destruction_flag_for_testing_;
@@ -57,8 +58,9 @@
   ~Blob() override;
 
  private:
+  const IpczDriver& driver_;
   std::string message_;
-  std::vector<OSHandle> handles_;
+  std::vector<IpczDriverHandle> handles_;
   const Ref<RefCountedFlag> destruction_flag_for_testing_{
       MakeRefCounted<RefCountedFlag>()};
 };
diff --git a/src/reference_drivers/file_descriptor.cc b/src/reference_drivers/file_descriptor.cc
new file mode 100644
index 0000000..b00f44a
--- /dev/null
+++ b/src/reference_drivers/file_descriptor.cc
@@ -0,0 +1,47 @@
+// Copyright 2022 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "reference_drivers/file_descriptor.h"
+
+#include <errno.h>
+#include <unistd.h>
+
+#include <utility>
+
+#include "third_party/abseil-cpp/absl/base/macros.h"
+
+namespace ipcz::reference_drivers {
+
+FileDescriptor::FileDescriptor() = default;
+
+FileDescriptor::FileDescriptor(int fd) : fd_(fd) {}
+
+FileDescriptor::FileDescriptor(FileDescriptor&& other)
+    : fd_(std::exchange(other.fd_, -1)) {}
+
+FileDescriptor& FileDescriptor::operator=(FileDescriptor&& other) {
+  reset();
+  fd_ = std::exchange(other.fd_, -1);
+  return *this;
+}
+
+FileDescriptor::~FileDescriptor() {
+  reset();
+}
+
+void FileDescriptor::reset() {
+  int fd = std::exchange(fd_, -1);
+  if (fd >= 0) {
+    int rv = close(fd);
+    ABSL_ASSERT(rv == 0 || errno == EINTR);
+  }
+}
+
+FileDescriptor FileDescriptor::Clone() const {
+  ABSL_ASSERT(is_valid());
+  int dupe = dup(fd_);
+  return FileDescriptor(dupe);
+}
+
+}  // namespace ipcz::reference_drivers
diff --git a/src/reference_drivers/file_descriptor.h b/src/reference_drivers/file_descriptor.h
new file mode 100644
index 0000000..bd9385a
--- /dev/null
+++ b/src/reference_drivers/file_descriptor.h
@@ -0,0 +1,40 @@
+// Copyright 2022 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef IPCZ_SRC_REFERENCE_DRIVERS_FILE_DESCRIPTOR_H_
+#define IPCZ_SRC_REFERENCE_DRIVERS_FILE_DESCRIPTOR_H_
+
+namespace ipcz::reference_drivers {
+
+// Implements unique ownership of a single POSIX file descriptor.
+class FileDescriptor {
+ public:
+  FileDescriptor();
+  explicit FileDescriptor(int fd);
+
+  FileDescriptor(const FileDescriptor&) = delete;
+  FileDescriptor& operator=(const FileDescriptor&) = delete;
+
+  FileDescriptor(FileDescriptor&& other);
+  FileDescriptor& operator=(FileDescriptor&& other);
+
+  ~FileDescriptor();
+
+  void reset();
+
+  // Duplicates the underlying descriptor, returning a new FileDescriptor object
+  // to wrap it. This object must be valid before calling Clone().
+  FileDescriptor Clone() const;
+
+  bool is_valid() const { return fd_ != -1; }
+
+  int get() const { return fd_; }
+
+ private:
+  int fd_ = -1;
+};
+
+}  // namespace ipcz::reference_drivers
+
+#endif  // IPCZ_SRC_REFERENCE_DRIVERS_FILE_DESCRIPTOR_H_
diff --git a/src/reference_drivers/memfd_memory.cc b/src/reference_drivers/memfd_memory.cc
new file mode 100644
index 0000000..c5e839b
--- /dev/null
+++ b/src/reference_drivers/memfd_memory.cc
@@ -0,0 +1,83 @@
+// Copyright 2022 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "reference_drivers/memfd_memory.h"
+
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <utility>
+
+#include "third_party/abseil-cpp/absl/base/macros.h"
+
+namespace ipcz::reference_drivers {
+
+MemfdMemory::Mapping::Mapping() = default;
+
+MemfdMemory::Mapping::Mapping(void* base_address, size_t size)
+    : base_address_(base_address), size_(size) {}
+
+MemfdMemory::Mapping::Mapping(Mapping&& other)
+    : base_address_(std::exchange(other.base_address_, nullptr)),
+      size_(std::exchange(other.size_, 0)) {}
+
+MemfdMemory::Mapping& MemfdMemory::Mapping::operator=(Mapping&& other) {
+  Reset();
+  base_address_ = std::exchange(other.base_address_, nullptr);
+  size_ = std::exchange(other.size_, 0);
+  return *this;
+}
+
+MemfdMemory::Mapping::~Mapping() {
+  Reset();
+}
+
+void MemfdMemory::Mapping::Reset() {
+  if (base_address_) {
+    munmap(base_address_, size_);
+    base_address_ = nullptr;
+    size_ = 0;
+  }
+}
+
+MemfdMemory::MemfdMemory() = default;
+
+MemfdMemory::MemfdMemory(FileDescriptor fd, size_t size)
+    : fd_(std::move(fd)), size_(size) {}
+
+MemfdMemory::MemfdMemory(size_t size) {
+  int fd = memfd_create("/ipcz/mem", MFD_ALLOW_SEALING);
+  ABSL_ASSERT(fd >= 0);
+
+  int result = ftruncate(fd, size);
+  ABSL_ASSERT(result == 0);
+
+  result = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK);
+  ABSL_ASSERT(result == 0);
+
+  fd_ = FileDescriptor(fd);
+  size_ = size;
+}
+
+MemfdMemory::MemfdMemory(MemfdMemory&&) = default;
+
+MemfdMemory& MemfdMemory::operator=(MemfdMemory&&) = default;
+
+MemfdMemory::~MemfdMemory() = default;
+
+MemfdMemory MemfdMemory::Clone() {
+  ABSL_ASSERT(is_valid());
+  return MemfdMemory(fd_.Clone(), size_);
+}
+
+MemfdMemory::Mapping MemfdMemory::Map() {
+  ABSL_ASSERT(is_valid());
+  void* addr =
+      mmap(nullptr, size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd_.get(), 0);
+  ABSL_ASSERT(addr && addr != MAP_FAILED);
+  return Mapping(addr, size_);
+}
+
+}  // namespace ipcz::reference_drivers
diff --git a/src/reference_drivers/memfd_memory.h b/src/reference_drivers/memfd_memory.h
new file mode 100644
index 0000000..b989106
--- /dev/null
+++ b/src/reference_drivers/memfd_memory.h
@@ -0,0 +1,97 @@
+// Copyright 2022 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef IPCZ_SRC_REFERENCE_DRIVERS_MEMORY_H_
+#define IPCZ_SRC_REFERENCE_DRIVERS_MEMORY_H_
+
+#include "reference_drivers/file_descriptor.h"
+#include "third_party/abseil-cpp/absl/types/span.h"
+
+namespace ipcz::reference_drivers {
+
+// A basic driver memory implementation for ipcz, which uses Linux-specific
+// support for an anonymous file living entirely in RAM (see memfd_create()).
+class MemfdMemory {
+ public:
+  // An mmap'd region of a MemfdMemory's underlying file. Instances of this
+  // object should be acquired from MemfdMemory::Map().
+  class Mapping {
+   public:
+    Mapping();
+    Mapping(void* base_address, size_t size);
+    Mapping(Mapping&&);
+    Mapping& operator=(Mapping&&);
+    Mapping(const Mapping&) = delete;
+    Mapping& operator=(const Mapping&) = delete;
+    ~Mapping();
+
+    bool is_valid() const { return base_address_ != nullptr; }
+
+    size_t size() const { return size_; }
+    void* base() const { return base_address_; }
+
+    absl::Span<uint8_t> bytes() const {
+      return {static_cast<uint8_t*>(base()), size_};
+    }
+
+    template <typename T>
+    T* As() const {
+      return static_cast<T*>(base());
+    }
+
+    void Reset();
+
+   private:
+    void* base_address_ = nullptr;
+    size_t size_ = 0;
+  };
+
+  // Constructs an invalid MemfdMemory object which cannot be mapped.
+  MemfdMemory();
+
+  // Constructs a new MemfdMemory object over `descriptor`, a file descriptor
+  // which should have been previously taken from some other valid MemfdMemory
+  // object or otherwise acquried via memfd_create(). `size` must correspond to
+  // the size of the underlying memory file.
+  MemfdMemory(FileDescriptor fd, size_t size);
+
+  // Constructs a new MemfdMemory object over a newly allocated, anonymous
+  // shared memory file of at least `size` bytes.
+  explicit MemfdMemory(size_t size);
+
+  MemfdMemory(MemfdMemory&&);
+  MemfdMemory& operator=(MemfdMemory&&);
+  MemfdMemory(const MemfdMemory&) = delete;
+  MemfdMemory& operator=(const MemfdMemory&) = delete;
+  ~MemfdMemory();
+
+  size_t size() const { return size_; }
+  bool is_valid() const { return fd_.is_valid(); }
+  const FileDescriptor& descriptor() const { return fd_; }
+
+  // Invalidates this object and returns a FileDescrtiptor which can be used
+  // later to reconstruct an equivalent MemfdMemory object with the same size().
+  FileDescriptor TakeDescriptor() { return std::move(fd_); }
+
+  // Resets this object, closing its underlying memory file descriptor.
+  void reset() { fd_.reset(); }
+
+  // Returns a new MemfdMemory object with its own handle to the same underlying
+  // memory file as `this`. Must only be called on a valid MemfdMemory object
+  // (i.e. is_valid() must be true.)
+  MemfdMemory Clone();
+
+  // Maps the entire file owned by this object and returns a Mapping for it.
+  // Must only be called on a valid MemfdMemory object (i.e. is_valid() must be
+  // true.)
+  Mapping Map();
+
+ private:
+  FileDescriptor fd_;
+  size_t size_ = 0;
+};
+
+}  // namespace ipcz::reference_drivers
+
+#endif  // IPCZ_SRC_REFERENCE_DRIVERS_MEMORY_H_
diff --git a/src/reference_drivers/memory_test.cc b/src/reference_drivers/memfd_memory_test.cc
similarity index 63%
rename from src/reference_drivers/memory_test.cc
rename to src/reference_drivers/memfd_memory_test.cc
index 7017852..67d5d10 100644
--- a/src/reference_drivers/memory_test.cc
+++ b/src/reference_drivers/memfd_memory_test.cc
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "reference_drivers/memory.h"
+#include "reference_drivers/memfd_memory.h"
 
 #include <tuple>
 
@@ -11,13 +11,13 @@
 namespace ipcz::reference_drivers {
 namespace {
 
-using MemoryTest = testing::Test;
+using MemfdMemoryTest = testing::Test;
 
-TEST_F(MemoryTest, CreateAndMap) {
-  Memory memory(64);
+TEST_F(MemfdMemoryTest, CreateAndMap) {
+  MemfdMemory memory(64);
 
-  Memory::Mapping mapping0 = memory.Map();
-  Memory::Mapping mapping1 = memory.Map();
+  MemfdMemory::Mapping mapping0 = memory.Map();
+  MemfdMemory::Mapping mapping1 = memory.Map();
 
   int* data0 = mapping0.As<int>();
   int* data1 = mapping1.As<int>();
@@ -31,11 +31,11 @@
   EXPECT_EQ(42, data1[0]);
 }
 
-TEST_F(MemoryTest, CreateMapClose) {
-  Memory memory(64);
+TEST_F(MemfdMemoryTest, CreateMapClose) {
+  MemfdMemory memory(64);
 
-  Memory::Mapping mapping0 = memory.Map();
-  Memory::Mapping mapping1 = memory.Map();
+  MemfdMemory::Mapping mapping0 = memory.Map();
+  MemfdMemory::Mapping mapping1 = memory.Map();
 
   // Even with the memfd closed, the mappings above should persist.
   memory.reset();
@@ -48,12 +48,12 @@
   EXPECT_EQ(42, data1[0]);
 }
 
-TEST_F(MemoryTest, CreateCloneMapClose) {
-  Memory memory(64);
-  Memory clone = memory.Clone();
+TEST_F(MemfdMemoryTest, CreateCloneMapClose) {
+  MemfdMemory memory(64);
+  MemfdMemory clone = memory.Clone();
 
-  Memory::Mapping mapping0 = memory.Map();
-  Memory::Mapping mapping1 = clone.Map();
+  MemfdMemory::Mapping mapping0 = memory.Map();
+  MemfdMemory::Mapping mapping1 = clone.Map();
 
   memory.reset();
   clone.reset();
diff --git a/src/reference_drivers/memory.cc b/src/reference_drivers/memory.cc
deleted file mode 100644
index 041fa84..0000000
--- a/src/reference_drivers/memory.cc
+++ /dev/null
@@ -1,50 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "reference_drivers/memory.h"
-
-#include <utility>
-
-#include "build/build_config.h"
-#include "third_party/abseil-cpp/absl/base/macros.h"
-
-namespace ipcz::reference_drivers {
-
-Memory::Mapping::Mapping() = default;
-
-Memory::Mapping::Mapping(void* base_address, size_t size)
-    : base_address_(base_address), size_(size) {}
-
-Memory::Mapping::Mapping(Mapping&& other)
-    : base_address_(std::exchange(other.base_address_, nullptr)),
-      size_(std::exchange(other.size_, 0)) {}
-
-Memory::Mapping& Memory::Mapping::operator=(Mapping&& other) {
-  Reset();
-  base_address_ = std::exchange(other.base_address_, nullptr);
-  size_ = std::exchange(other.size_, 0);
-  return *this;
-}
-
-Memory::Mapping::~Mapping() {
-  Reset();
-}
-
-Memory::Memory() = default;
-
-Memory::Memory(OSHandle handle, size_t size)
-    : handle_(std::move(handle)), size_(size) {}
-
-Memory::Memory(Memory&&) = default;
-
-Memory& Memory::operator=(Memory&&) = default;
-
-Memory::~Memory() = default;
-
-Memory Memory::Clone() {
-  ABSL_ASSERT(is_valid());
-  return Memory(handle_.Clone(), size_);
-}
-
-}  // namespace ipcz::reference_drivers
diff --git a/src/reference_drivers/memory.h b/src/reference_drivers/memory.h
deleted file mode 100644
index eaf2485..0000000
--- a/src/reference_drivers/memory.h
+++ /dev/null
@@ -1,95 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef IPCZ_SRC_REFERENCE_DRIVERS_MEMORY_H_
-#define IPCZ_SRC_REFERENCE_DRIVERS_MEMORY_H_
-
-#include "reference_drivers/os_handle.h"
-#include "third_party/abseil-cpp/absl/types/span.h"
-
-namespace ipcz::reference_drivers {
-
-// Cross-platform abstraction for a shared memory region.
-class Memory {
- public:
-  // Cross-platform abstraction for an active mapping of a shared memory region.
-  //
-  // Instances of this object should be acquired from Memory::Map().
-  class Mapping {
-   public:
-    Mapping();
-    Mapping(void* base_address, size_t size);
-    Mapping(Mapping&&);
-    Mapping& operator=(Mapping&&);
-    Mapping(const Mapping&) = delete;
-    Mapping& operator=(const Mapping&) = delete;
-    ~Mapping();
-
-    bool is_valid() const { return base_address_ != nullptr; }
-
-    size_t size() const { return size_; }
-    void* base() const { return base_address_; }
-
-    absl::Span<uint8_t> bytes() const {
-      return {static_cast<uint8_t*>(base()), size_};
-    }
-
-    template <typename T>
-    T* As() const {
-      return static_cast<T*>(base());
-    }
-
-    void Reset();
-
-   private:
-    void* base_address_ = nullptr;
-    size_t size_ = 0;
-  };
-
-  // Constructs an invalid Memory object which cannot be mapped.
-  Memory();
-
-  // Constructs a new Memory object over `handle`, an OSHandle which should have
-  // been previously taken from some other valid Memory object. `size` must
-  // correspond to the size of that original region.
-  Memory(OSHandle handle, size_t size);
-
-  // Constructs a new Memory object over a newly allocated shared memory region
-  // of at least `size` bytes.
-  explicit Memory(size_t size);
-
-  Memory(Memory&&);
-  Memory& operator=(Memory&&);
-  Memory(const Memory&) = delete;
-  Memory& operator=(const Memory&) = delete;
-  ~Memory();
-
-  size_t size() const { return size_; }
-  bool is_valid() const { return handle_.is_valid(); }
-  const OSHandle& handle() const { return handle_; }
-
-  // Invalidates this Memory object and returns an OSHandle which can be used
-  // later to reconstruct an equivalent Memory object, given the same size().
-  OSHandle TakeHandle() { return std::move(handle_); }
-
-  // Resets this object, closing its handle to the underlying region.
-  void reset() { handle_.reset(); }
-
-  // Returns a new Memory object with its own handle to the same underlying
-  // region as `this`. Must only be called on a valid Memory object (i.e.
-  // is_valid() must be true.)
-  Memory Clone();
-
-  // Maps the entire region owned by Memory and returns a Mapping for it. Must
-  // only be called on a valid Memory object (i.e. is_valid() must be true.)
-  Mapping Map();
-
- private:
-  OSHandle handle_;
-  size_t size_ = 0;
-};
-
-}  // namespace ipcz::reference_drivers
-
-#endif  // IPCZ_SRC_REFERENCE_DRIVERS_MEMORY_H_
diff --git a/src/reference_drivers/memory_android.cc b/src/reference_drivers/memory_android.cc
deleted file mode 100644
index 355b20f..0000000
--- a/src/reference_drivers/memory_android.cc
+++ /dev/null
@@ -1,45 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "reference_drivers/memory.h"
-
-#include <sys/mman.h>
-#include <unistd.h>
-
-#include <cstddef>
-
-#include "reference_drivers/os_handle.h"
-#include "third_party/abseil-cpp/absl/base/macros.h"
-#include "third_party/ashmem/ashmem.h"
-
-namespace ipcz::reference_drivers {
-
-void Memory::Mapping::Reset() {
-  if (base_address_) {
-    munmap(base_address_, size_);
-    base_address_ = nullptr;
-    size_ = 0;
-  }
-}
-
-Memory::Memory(size_t size) {
-  const size_t page_size = static_cast<size_t>(sysconf(_SC_PAGESIZE));
-  const size_t rounded_size = (size + page_size - 1) & (page_size - 1);
-  int fd = ashmem_create_region("ipcz-memory", rounded_size);
-  ABSL_ASSERT(fd >= 0);
-  int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
-  ABSL_ASSERT(err == 0);
-  handle_ = OSHandle(fd);
-  size_ = size;
-}
-
-Memory::Mapping Memory::Map() {
-  ABSL_ASSERT(is_valid());
-  void* addr =
-      mmap(nullptr, size_, PROT_READ | PROT_WRITE, MAP_SHARED, handle_.fd(), 0);
-  ABSL_ASSERT(addr && addr != MAP_FAILED);
-  return Mapping(addr, size_);
-}
-
-}  // namespace ipcz::reference_drivers
diff --git a/src/reference_drivers/memory_fuchsia.cc b/src/reference_drivers/memory_fuchsia.cc
deleted file mode 100644
index e2c8e2c..0000000
--- a/src/reference_drivers/memory_fuchsia.cc
+++ /dev/null
@@ -1,54 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "reference_drivers/memory.h"
-
-#include <lib/zx/vmar.h>
-#include <zircon/syscalls.h>
-
-#include <cstddef>
-#include <cstdint>
-#include <utility>
-
-#include "reference_drivers/os_handle.h"
-#include "third_party/abseil-cpp/absl/base/macros.h"
-
-namespace ipcz::reference_drivers {
-
-void Memory::Mapping::Reset() {
-  if (base_address_) {
-    uintptr_t addr = reinterpret_cast<uintptr_t>(base_address_);
-    zx_status_t status = zx::vmar::root_self()->unmap(addr, size_);
-    ABSL_ASSERT(status == ZX_OK);
-  }
-}
-
-Memory::Memory(size_t size) {
-  const uint32_t page_size = zx_system_get_page_size();
-  const size_t rounded_size = (size + page_size - 1) & (page_size - 1);
-  zx::vmo vmo;
-  zx_status_t status = zx::vmo::create(rounded_size, 0, &vmo);
-  ABSL_ASSERT(status == ZX_OK);
-  const int kNoExec = ZX_DEFAULT_VMO_RIGHTS & ~ZX_RIGHT_EXECUTE;
-  status = vmo.replace(kNoExec, &vmo);
-  ABSL_ASSERT(status == ZX_OK);
-  handle_ = OSHandle(std::move(vmo));
-  size_ = size;
-}
-
-Memory::Mapping Memory::Map() {
-  ABSL_ASSERT(is_valid());
-  uintptr_t addr;
-  zx_vm_option_t options =
-      ZX_VM_REQUIRE_NON_RESIZABLE | ZX_VM_PERM_READ | ZX_VM_PERM_WRITE;
-  zx_status_t status = zx::vmar::root_self()->map(
-      options, /*vmar_offset=*/0, *zx::unowned_vmo(handle_.handle().get()), 0,
-      size_, &addr);
-  if (status != ZX_OK) {
-    return {};
-  }
-  return Mapping(reinterpret_cast<void*>(addr), size_);
-}
-
-}  // namespace ipcz::reference_drivers
diff --git a/src/reference_drivers/memory_mac.cc b/src/reference_drivers/memory_mac.cc
deleted file mode 100644
index 4f1c561..0000000
--- a/src/reference_drivers/memory_mac.cc
+++ /dev/null
@@ -1,47 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "reference_drivers/memory.h"
-
-#include <mach/mach_vm.h>
-
-#include "reference_drivers/os_handle.h"
-#include "third_party/abseil-cpp/absl/base/macros.h"
-
-namespace ipcz::reference_drivers {
-
-void Memory::Mapping::Reset() {
-  if (base_address_) {
-    kern_return_t kr = mach_vm_deallocate(
-        mach_task_self(), reinterpret_cast<mach_vm_address_t>(base_address_),
-        size_);
-    ABSL_ASSERT(kr == KERN_SUCCESS);
-  }
-}
-
-Memory::Memory(size_t size) {
-  mach_vm_size_t vm_size = size;
-  mach_port_t named_right;
-  kern_return_t kr = mach_make_memory_entry_64(
-      mach_task_self(), &vm_size, 0,
-      MAP_MEM_NAMED_CREATE | VM_PROT_READ | VM_PROT_WRITE, &named_right,
-      MACH_PORT_NULL);
-  ABSL_ASSERT(kr == KERN_SUCCESS);
-  ABSL_ASSERT(vm_size >= size);
-  handle_ = OSHandle(OSHandle::MachSendRight(named_right));
-  size_ = size;
-}
-
-Memory::Mapping Memory::Map() {
-  ABSL_ASSERT(is_valid());
-  mach_vm_address_t address = 0;
-  kern_return_t kr = mach_vm_map(mach_task_self(), &address, size_, 0,
-                                 VM_FLAGS_ANYWHERE, handle_.mach_send_right(),
-                                 0, FALSE, VM_PROT_READ | VM_PROT_WRITE,
-                                 VM_PROT_READ | VM_PROT_WRITE, VM_INHERIT_NONE);
-  ABSL_ASSERT(kr == KERN_SUCCESS);
-  return Mapping(reinterpret_cast<void*>(address), size_);
-}
-
-}  // namespace ipcz::reference_drivers
diff --git a/src/reference_drivers/memory_posix.cc b/src/reference_drivers/memory_posix.cc
deleted file mode 100644
index 37e7775..0000000
--- a/src/reference_drivers/memory_posix.cc
+++ /dev/null
@@ -1,47 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "reference_drivers/memory.h"
-
-#include <fcntl.h>
-#include <sys/mman.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include "reference_drivers/os_handle.h"
-#include "third_party/abseil-cpp/absl/base/macros.h"
-
-namespace ipcz::reference_drivers {
-
-void Memory::Mapping::Reset() {
-  if (base_address_) {
-    munmap(base_address_, size_);
-    base_address_ = nullptr;
-    size_ = 0;
-  }
-}
-
-Memory::Memory(size_t size) {
-  int fd = memfd_create("/ipcz/mem", MFD_ALLOW_SEALING);
-  ABSL_ASSERT(fd >= 0);
-
-  int result = ftruncate(fd, size);
-  ABSL_ASSERT(result == 0);
-
-  result = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK);
-  ABSL_ASSERT(result == 0);
-
-  handle_ = OSHandle(fd);
-  size_ = size;
-}
-
-Memory::Mapping Memory::Map() {
-  ABSL_ASSERT(is_valid());
-  void* addr =
-      mmap(nullptr, size_, PROT_READ | PROT_WRITE, MAP_SHARED, handle_.fd(), 0);
-  ABSL_ASSERT(addr && addr != MAP_FAILED);
-  return Mapping(addr, size_);
-}
-
-}  // namespace ipcz::reference_drivers
diff --git a/src/reference_drivers/memory_win.cc b/src/reference_drivers/memory_win.cc
deleted file mode 100644
index 77ef5b6..0000000
--- a/src/reference_drivers/memory_win.cc
+++ /dev/null
@@ -1,48 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "reference_drivers/memory.h"
-
-#include <windows.h>
-
-#include "reference_drivers/os_handle.h"
-#include "third_party/abseil-cpp/absl/base/macros.h"
-#include "util/safe_math.h"
-
-namespace ipcz::reference_drivers {
-
-void Memory::Mapping::Reset() {
-  if (base_address_) {
-    ::UnmapViewOfFile(base_address_);
-  }
-}
-
-Memory::Memory(size_t size) {
-  HANDLE h = ::CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE,
-                                 0, checked_cast<DWORD>(size), nullptr);
-  const HANDLE process = ::GetCurrentProcess();
-  HANDLE h2;
-
-  // NOTE: DuplicateHandle is called here to remove some permissions from the
-  // handle (at least WRITE_DAC, among others). This allows the handle to be
-  // duplicated to other processes under strict sandboxing conditions, which may
-  // be useful in some test scenarios.
-  BOOL ok = ::DuplicateHandle(process, h, process, &h2,
-                              FILE_MAP_READ | FILE_MAP_WRITE, FALSE, 0);
-  ::CloseHandle(h);
-  ABSL_ASSERT(ok);
-
-  handle_ = OSHandle(h2);
-  size_ = size;
-}
-
-Memory::Mapping Memory::Map() {
-  ABSL_ASSERT(is_valid());
-  void* addr = ::MapViewOfFile(handle_.handle(), FILE_MAP_READ | FILE_MAP_WRITE,
-                               0, 0, size_);
-  ABSL_ASSERT(addr);
-  return Mapping(addr, size_);
-}
-
-}  // namespace ipcz::reference_drivers
diff --git a/src/reference_drivers/os_handle.h b/src/reference_drivers/os_handle.h
deleted file mode 100644
index 1ed3802..0000000
--- a/src/reference_drivers/os_handle.h
+++ /dev/null
@@ -1,22 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef IPCZ_SRC_REFERENCE_DRIVERS_OS_HANDLE_H_
-#define IPCZ_SRC_REFERENCE_DRIVERS_OS_HANDLE_H_
-
-#include "build/build_config.h"
-
-#if BUILDFLAG(IS_WIN)
-#include "reference_drivers/os_handle_win.h"
-#elif BUILDFLAG(IS_MAC)
-#include "reference_drivers/os_handle_mac.h"
-#elif BUILDFLAG(IS_FUCHSIA)
-#include "reference_drivers/os_handle_fuchsia.h"
-#elif BUILDFLAG(IS_POSIX)
-#include "reference_drivers/os_handle_posix.h"
-#else
-#error "Unsupported platform"
-#endif
-
-#endif  // IPCZ_SRC_REFERENCE_DRIVERS_OS_HANDLE_H_
diff --git a/src/reference_drivers/os_handle_fuchsia.cc b/src/reference_drivers/os_handle_fuchsia.cc
deleted file mode 100644
index 41d817a..0000000
--- a/src/reference_drivers/os_handle_fuchsia.cc
+++ /dev/null
@@ -1,39 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "reference_drivers/os_handle.h"
-
-#include <lib/zx/handle.h>
-#include <zircon/status.h>
-
-#include <utility>
-
-#include "third_party/abseil-cpp/absl/base/macros.h"
-
-namespace ipcz::reference_drivers {
-
-OSHandle::OSHandle() = default;
-
-OSHandle::OSHandle(zx::handle handle) : handle_(std::move(handle)) {}
-
-OSHandle::OSHandle(OSHandle&& other) = default;
-
-OSHandle& OSHandle::operator=(OSHandle&& other) = default;
-
-OSHandle::~OSHandle() = default;
-
-void OSHandle::reset() {
-  handle_.reset();
-}
-
-OSHandle OSHandle::Clone() const {
-  ABSL_ASSERT(is_valid());
-
-  zx::handle dupe;
-  zx_status_t status = handle_.duplicate(ZX_RIGHT_SAME_RIGHTS, &dupe);
-  ABSL_ASSERT(status == ZX_OK);
-  return OSHandle(std::move(dupe));
-}
-
-}  // namespace ipcz::reference_drivers
diff --git a/src/reference_drivers/os_handle_fuchsia.h b/src/reference_drivers/os_handle_fuchsia.h
deleted file mode 100644
index 4f03657..0000000
--- a/src/reference_drivers/os_handle_fuchsia.h
+++ /dev/null
@@ -1,41 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef IPCZ_SRC_REFERENCE_DRIVERS_OS_HANDLE_FUCHSIA_H_
-#define IPCZ_SRC_REFERENCE_DRIVERS_OS_HANDLE_FUCHSIA_H_
-
-#include <lib/zx/handle.h>
-
-namespace ipcz::reference_drivers {
-
-// The Fuchsia OSHandle implementation can wrap any zx::handle.
-class OSHandle {
- public:
-  OSHandle();
-  explicit OSHandle(zx::handle handle);
-
-  OSHandle(const OSHandle&) = delete;
-  OSHandle& operator=(const OSHandle&) = delete;
-
-  OSHandle(OSHandle&& other);
-  OSHandle& operator=(OSHandle&& other);
-
-  ~OSHandle();
-
-  void reset();
-
-  // Duplicates the underlying handle, returning a new OSHandle to wrap it. The
-  // handle must be valid.
-  OSHandle Clone() const;
-
-  bool is_valid() const { return handle_.is_valid(); }
-  const zx::handle& handle() const { return handle_; }
-
- private:
-  zx::handle handle_;
-};
-
-}  // namespace ipcz::reference_drivers
-
-#endif  // IPCZ_SRC_REFERENCE_DRIVERS_OS_HANDLE_FUCHSIA_H_
diff --git a/src/reference_drivers/os_handle_mac.cc b/src/reference_drivers/os_handle_mac.cc
deleted file mode 100644
index b9f136c..0000000
--- a/src/reference_drivers/os_handle_mac.cc
+++ /dev/null
@@ -1,80 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "reference_drivers/os_handle.h"
-
-#include <errno.h>
-#include <mach/mach.h>
-#include <mach/mach_vm.h>
-#include <unistd.h>
-
-#include <utility>
-
-#include "third_party/abseil-cpp/absl/base/macros.h"
-
-namespace ipcz::reference_drivers {
-
-OSHandle::OSHandle() = default;
-
-OSHandle::OSHandle(Value value) : value_(value) {}
-
-OSHandle::OSHandle(OSHandle&& other)
-    : value_(std::exchange(other.value_, {})) {}
-
-OSHandle& OSHandle::operator=(OSHandle&& other) {
-  reset();
-  value_ = std::exchange(other.value_, {});
-  return *this;
-}
-
-OSHandle::~OSHandle() {
-  reset();
-}
-
-void OSHandle::reset() {
-  if (is_valid_fd()) {
-    int rv = close(fd());
-    ABSL_ASSERT(rv == 0 || errno == EINTR);
-  } else if (is_valid_mach_send_right()) {
-    kern_return_t kr =
-        mach_port_deallocate(mach_task_self(), mach_send_right());
-    ABSL_ASSERT(kr == KERN_SUCCESS);
-  } else if (is_valid_mach_receive_right()) {
-    kern_return_t kr = mach_port_mod_refs(
-        mach_task_self(), mach_receive_right(), MACH_PORT_RIGHT_RECEIVE, -1);
-    ABSL_ASSERT(kr == KERN_SUCCESS);
-  } else if (is_valid_mach_port_set()) {
-    kern_return_t kr = mach_port_mod_refs(
-        mach_task_self(), mach_receive_right(), MACH_PORT_RIGHT_PORT_SET, -1);
-    ABSL_ASSERT(kr == KERN_SUCCESS);
-  }
-
-  value_ = {};
-}
-
-OSHandle OSHandle::Clone() const {
-  ABSL_ASSERT(is_valid());
-
-  // Cloning of receive rights or port sets is not supported.
-  ABSL_ASSERT(!is_mach_receive_right() && !is_mach_port_set());
-
-  if (is_valid_fd()) {
-    int duped_fd = dup(fd());
-    ABSL_ASSERT(duped_fd >= 0);
-    return OSHandle(FileDescriptor(duped_fd));
-  }
-
-  if (is_valid_mach_send_right()) {
-    kern_return_t kr = mach_port_mod_refs(mach_task_self(), mach_send_right(),
-                                          MACH_PORT_RIGHT_SEND, 1);
-    if (kr != KERN_SUCCESS) {
-      return OSHandle();
-    }
-    return OSHandle(MachSendRight(mach_send_right()));
-  }
-
-  return {};
-}
-
-}  // namespace ipcz::reference_drivers
diff --git a/src/reference_drivers/os_handle_mac.h b/src/reference_drivers/os_handle_mac.h
deleted file mode 100644
index 8387347..0000000
--- a/src/reference_drivers/os_handle_mac.h
+++ /dev/null
@@ -1,103 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef IPCZ_SRC_REFERENCE_DRIVERS_OS_HANDLE_MAC_H_
-#define IPCZ_SRC_REFERENCE_DRIVERS_OS_HANDLE_MAC_H_
-
-#include <mach/mach.h>
-
-#include "third_party/abseil-cpp/absl/base/macros.h"
-#include "third_party/abseil-cpp/absl/types/variant.h"
-#include "util/strong_alias.h"
-
-namespace ipcz::reference_drivers {
-
-// The macOS implementation of OSHandle supports wrapping a single Mach send
-// right, or a POSIX file descriptor.
-class OSHandle {
- public:
-  using FileDescriptor = StrongAlias<class FileDescriptorTag, int>;
-  using MachSendRight = StrongAlias<class MachSendRightTag, mach_port_t>;
-  using MachReceiveRight = StrongAlias<class MachReceiveRightTag, mach_port_t>;
-  using MachPortSet = StrongAlias<class MachPortSetTag, mach_port_t>;
-  using Value = absl::variant<absl::monostate,
-                              FileDescriptor,
-                              MachSendRight,
-                              MachReceiveRight,
-                              MachPortSet>;
-
-  OSHandle();
-  explicit OSHandle(Value value);
-
-  OSHandle(const OSHandle&) = delete;
-  OSHandle& operator=(const OSHandle&) = delete;
-
-  OSHandle(OSHandle&& other);
-  OSHandle& operator=(OSHandle&& other);
-
-  ~OSHandle();
-
-  void reset();
-
-  // Duplicates the underlying handle, returning a new OSHandle to wrap it.
-  // The handle must be a valid file descriptor or Mach send right. Cloning of
-  // of Mach receive rights or port sets is not supported.
-  OSHandle Clone() const;
-
-  bool is_valid() const {
-    return is_valid_fd() || is_valid_mach_send_right() ||
-           is_valid_mach_receive_right() || is_valid_mach_port_set();
-  }
-
-  bool is_fd() const { return absl::holds_alternative<FileDescriptor>(value_); }
-  bool is_valid_fd() const { return is_fd() && fd() != -1; }
-
-  bool is_mach_send_right() const {
-    return absl::holds_alternative<MachSendRight>(value_);
-  }
-  bool is_valid_mach_send_right() const {
-    return is_mach_send_right() && mach_send_right() != MACH_PORT_NULL;
-  }
-
-  bool is_mach_receive_right() const {
-    return absl::holds_alternative<MachReceiveRight>(value_);
-  }
-  bool is_valid_mach_receive_right() const {
-    return is_mach_receive_right() && mach_receive_right() != MACH_PORT_NULL;
-  }
-
-  bool is_mach_port_set() const {
-    return absl::holds_alternative<MachPortSet>(value_);
-  }
-  bool is_valid_mach_port_set() const {
-    return is_mach_port_set() && mach_port_set() != MACH_PORT_NULL;
-  }
-
-  int fd() const {
-    ABSL_ASSERT(is_fd());
-    return absl::get<FileDescriptor>(value_).value();
-  }
-
-  mach_port_t mach_send_right() const {
-    ABSL_ASSERT(is_mach_send_right());
-    return absl::get<MachSendRight>(value_).value();
-  }
-
-  mach_port_t mach_receive_right() const {
-    ABSL_ASSERT(is_mach_receive_right());
-    return absl::get<MachReceiveRight>(value_).value();
-  }
-
-  mach_port_t mach_port_set() const {
-    ABSL_ASSERT(is_mach_port_set());
-    return absl::get<MachPortSet>(value_).value();
-  }
-
- private:
-  Value value_;
-};
-
-}  // namespace ipcz::reference_drivers
-
-#endif  // IPCZ_SRC_REFERENCE_DRIVERS_OS_HANDLE_MAC_H_
diff --git a/src/reference_drivers/os_handle_posix.cc b/src/reference_drivers/os_handle_posix.cc
deleted file mode 100644
index c96dbdf..0000000
--- a/src/reference_drivers/os_handle_posix.cc
+++ /dev/null
@@ -1,46 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "reference_drivers/os_handle.h"
-
-#include <errno.h>
-#include <unistd.h>
-
-#include <utility>
-
-#include "third_party/abseil-cpp/absl/base/macros.h"
-
-namespace ipcz::reference_drivers {
-
-OSHandle::OSHandle() = default;
-
-OSHandle::OSHandle(int fd) : fd_(fd) {}
-
-OSHandle::OSHandle(OSHandle&& other) : fd_(std::exchange(other.fd_, -1)) {}
-
-OSHandle& OSHandle::operator=(OSHandle&& other) {
-  reset();
-  fd_ = std::exchange(other.fd_, -1);
-  return *this;
-}
-
-OSHandle::~OSHandle() {
-  reset();
-}
-
-void OSHandle::reset() {
-  int fd = std::exchange(fd_, -1);
-  if (fd >= 0) {
-    int rv = close(fd);
-    ABSL_ASSERT(rv == 0 || errno == EINTR);
-  }
-}
-
-OSHandle OSHandle::Clone() const {
-  ABSL_ASSERT(is_valid());
-  int dupe = dup(fd_);
-  return OSHandle(dupe);
-}
-
-}  // namespace ipcz::reference_drivers
diff --git a/src/reference_drivers/os_handle_posix.h b/src/reference_drivers/os_handle_posix.h
deleted file mode 100644
index 0ab5efc..0000000
--- a/src/reference_drivers/os_handle_posix.h
+++ /dev/null
@@ -1,40 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef IPCZ_SRC_REFERENCE_DRIVERS_OS_HANDLE_POSIX_H_
-#define IPCZ_SRC_REFERENCE_DRIVERS_OS_HANDLE_POSIX_H_
-
-namespace ipcz::reference_drivers {
-
-// The POSIX OSHandle implementation wraps a file descriptor.
-class OSHandle {
- public:
-  OSHandle();
-  explicit OSHandle(int fd);
-
-  OSHandle(const OSHandle&) = delete;
-  OSHandle& operator=(const OSHandle&) = delete;
-
-  OSHandle(OSHandle&& other);
-  OSHandle& operator=(OSHandle&& other);
-
-  ~OSHandle();
-
-  void reset();
-
-  // Duplicates the underlying handle, returning a new OSHandle to wrap it. The
-  // handle must be valid.
-  OSHandle Clone() const;
-
-  bool is_valid() const { return fd_ != -1; }
-
-  int fd() const { return fd_; }
-
- private:
-  int fd_ = -1;
-};
-
-}  // namespace ipcz::reference_drivers
-
-#endif  // IPCZ_SRC_REFERENCE_DRIVERS_OS_HANDLE_POSIX_H_
diff --git a/src/reference_drivers/os_handle_win.cc b/src/reference_drivers/os_handle_win.cc
deleted file mode 100644
index 570f45e..0000000
--- a/src/reference_drivers/os_handle_win.cc
+++ /dev/null
@@ -1,54 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "reference_drivers/os_handle.h"
-
-#include <windows.h>
-
-#include <utility>
-
-#include "third_party/abseil-cpp/absl/base/macros.h"
-
-namespace ipcz::reference_drivers {
-
-OSHandle::OSHandle() = default;
-
-OSHandle::OSHandle(HANDLE handle) : handle_(handle) {}
-
-OSHandle::OSHandle(OSHandle&& other)
-    : handle_(std::exchange(other.handle_, INVALID_HANDLE_VALUE)) {}
-
-OSHandle& OSHandle::operator=(OSHandle&& other) {
-  reset();
-  handle_ = std::exchange(other.handle_, INVALID_HANDLE_VALUE);
-  return *this;
-}
-
-OSHandle::~OSHandle() {
-  reset();
-}
-
-void OSHandle::reset() {
-  HANDLE handle = std::exchange(handle_, INVALID_HANDLE_VALUE);
-  if (handle != INVALID_HANDLE_VALUE) {
-    ::CloseHandle(handle);
-  }
-}
-
-OSHandle OSHandle::Clone() const {
-  ABSL_ASSERT(is_valid());
-
-  HANDLE dupe;
-  BOOL result =
-      ::DuplicateHandle(::GetCurrentProcess(), handle_, ::GetCurrentProcess(),
-                        &dupe, 0, FALSE, DUPLICATE_SAME_ACCESS);
-  if (!result) {
-    return OSHandle();
-  }
-
-  ABSL_ASSERT(dupe != INVALID_HANDLE_VALUE);
-  return OSHandle(dupe);
-}
-
-}  // namespace ipcz::reference_drivers
diff --git a/src/reference_drivers/os_handle_win.h b/src/reference_drivers/os_handle_win.h
deleted file mode 100644
index 2506f95..0000000
--- a/src/reference_drivers/os_handle_win.h
+++ /dev/null
@@ -1,42 +0,0 @@
-// Copyright 2022 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef IPCZ_SRC_REFERENCE_DRIVERS_OS_HANDLE_WIN_H_
-#define IPCZ_SRC_REFERENCE_DRIVERS_OS_HANDLE_WIN_H_
-
-#include <windows.h>
-
-namespace ipcz::reference_drivers {
-
-// The Windows OSHandle implementation can wrap any HANDLE value.
-class OSHandle {
- public:
-  OSHandle();
-  explicit OSHandle(HANDLE handle);
-
-  OSHandle(const OSHandle&) = delete;
-  OSHandle& operator=(const OSHandle&) = delete;
-
-  OSHandle(OSHandle&& other);
-  OSHandle& operator=(OSHandle&& other);
-
-  ~OSHandle();
-
-  void reset();
-
-  // Duplicates the underlying handle, returning a new OSHandle to wrap it. The
-  // handle must be valid.
-  OSHandle Clone() const;
-
-  bool is_valid() const { return handle_ != INVALID_HANDLE_VALUE; }
-
-  HANDLE handle() const { return handle_; }
-
- private:
-  HANDLE handle_ = INVALID_HANDLE_VALUE;
-};
-
-}  // namespace ipcz::reference_drivers
-
-#endif  // IPCZ_SRC_REFERENCE_DRIVERS_OS_HANDLE_WIN_H_