Repository: denoland/deno_core
Pull request #471 (2024-01-23 23:12:47)

fix: Fix segmentation fault in tests on CPUs with PKU support
Due to V8's PKU feature, only the thread that initialized the V8 platform or those spawned from it can access the isolates; other threads will get a segmentation fault. Since each test runs in its thread, they crash on newer CPUs that support PKU.

This issue has two possible solutions:
1. Initialize the platform once in the main thread.
2. Turn off the PKU feature during testing.

This issue was already encountered for the `deno test` command, which was resolved by initializing the platform in the main thread (denoland/deno#20495).

In the case of rust tests, the same solution cannot be used, as the default test runner does not allow running code on the main thread before any test runs.

My proposed solution is to add a feature flag to deno_core, which, when enabled, uses the unprotected platform. An alternative solution could be to call `deno_core::JsRuntime::init_platform` at the beginning of each test.

This PR will also be a step towards fixing denoland/deno#21439

CLAassistant: [![CLA assistant check](https://cla-assistant.io/pull/badge/not_signed)](https://cla-assistant.io/denoland/deno_core?pullRequest=471) <br/>Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our [Contributor License Agreement](https://cla-assistant.io/denoland/deno_core?pullRequest=471) before we can accept your contribution.<br/><sub>You have signed the CLA already but the status is still pending? Let us [recheck](https://cla-assistant.io/check/denoland/deno_core?pullRequest=471) it.</sub>

Patch:
diff --git a/.github/workflows/ci-job.yml b/.github/workflows/ci-job.yml
new file mode 100644
index 0000000..2b1d3e5
--- /dev/null
+++ b/.github/workflows/ci-job.yml
@@ -0,0 +1,76 @@
+name: CI Job
+on:
+  workflow_call:
+    inputs:
+      label:
+        required: true
+        type: string
+      os:
+        required: true
+        type: string
+      job:
+        required: true
+        type: string
+
+jobs:
+  job:
+    name: ${{ inputs.label }} ${{ inputs.job }}
+    runs-on: ${{ inputs.os }}
+    steps:
+      - run: echo ${{ inputs.job }}
+
+      - name: Clone repository
+        uses: actions/checkout@v3
+
+      - name: Install Rust
+        uses: dsherret/rust-toolchain-file@v1
+
+      - name: Install Deno
+        uses: denoland/setup-deno@v1
+
+      # https://github.com/marketplace/actions/rust-cache
+      - uses: Swatinem/rust-cache@v2
+        with:
+          prefix-key: "v0-rust"
+          shared-key: "${{ inputs.label }}-${{ inputs.job }}"
+          save-if: ${{ github.ref == 'refs/heads/main' }}
+
+      - name: Clean
+        if: github.ref == 'refs/heads/main'
+        run: cargo clean
+
+      - name: Lint
+        id: lint
+        if: inputs.job == 'lint'
+        uses: ./.github/workflows/ci-lint
+
+      - name: Lint (deps)
+        id: lint-deps
+        if: inputs.job == 'lint-deps'
+        uses: ./.github/workflows/ci-lint-deps
+
+      - name: Test
+        id: test
+        if: inputs.job == 'test'
+        uses: ./.github/workflows/ci-test
+
+      - name: Test (ops)
+        id: test-ops
+        if: inputs.job == 'test-ops'
+        uses: ./.github/workflows/ci-test-ops
+
+      - name: Test (miri)
+        id: test-miri
+        if: inputs.job == 'test-miri'
+        uses: ./.github/workflows/ci-test-miri
+
+      - name: Publish
+        id: publish
+        if: inputs.job == 'publish'
+        uses: ./.github/workflows/ci-publish
+
+    env:
+      CARGO_INCREMENTAL: 0
+      GH_ACTIONS: 1
+      RUST_BACKTRACE: full
+      RUSTFLAGS: -D warnings
diff --git a/.github/workflows/ci-lint-deps/action.yml b/.github/workflows/ci-lint-deps/action.yml
new file mode 100644
index 0000000..2519f10
--- /dev/null
+++ b/.github/workflows/ci-lint-deps/action.yml
@@ -0,0 +1,13 @@
+name: Lint Dependencies
+runs:
+  using: "composite"
+  steps:
+    - name: Install Rust (nightly)
+      uses: dtolnay/rust-toolchain@nightly
+    - name: Lint deps
+      shell: bash
+      run: |
+        cargo install cargo-machete --locked
+        cargo machete
+        cargo install cargo-udeps --locked
+        cargo +nightly udeps
diff --git a/.github/workflows/ci-lint/action.yml b/.github/workflows/ci-lint/action.yml
new file mode 100644
index 0000000..f0b5d67
--- /dev/null
+++ b/.github/workflows/ci-lint/action.yml
@@ -0,0 +1,21 @@
+name: Lint
+runs:
+  using: "composite"
+  steps:
+    - name: Format
+      shell: bash
+      run: |
+        cargo fmt -- --check
+        deno fmt --check ./core/ ./ops/ ./serde_v8/
+    - name: Lint
+      shell: bash
+      run: |
+        cargo clippy --locked --release --all-features --all-targets -- -D clippy::all
+    - name: Lint
+      shell: bash
+      run: |
+        deno lint
+    - name: Lint
+      shell: bash
+      run: |
+        tools/copyright_checker.js
diff --git a/.github/workflows/ci-publish/action.yml b/.github/workflows/ci-publish/action.yml
new file mode 100644
index 0000000..695f163
--- /dev/null
+++ b/.github/workflows/ci-publish/action.yml
@@ -0,0 +1,11 @@
+name: Publish workflow
+runs:
+  using: "composite"
+  steps:
+    - name: Cargo publish
+      env:
+        CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
+      run: cargo publish -p serde_v8 && cargo publish -p deno_ops && cargo publish -p deno_core
+    - name: Get tag version
+      id: get_tag_version
+      run: echo TAG_VERSION=${GITHUB_REF/refs\/tags\//} >> "$GITHUB_OUTPUT"
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index ee23b7f..ce33fd6 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1,99 +1,59 @@
-name: ci
+name: CI
 
 on: [push, pull_request]
 
 jobs:
-  rust:
-    name: deno_core-${{ matrix.os }}
-    if: |
-      (github.event_name == 'push' || !startsWith(github.event.pull_request.head.label, 'denoland:'))
-      && github.ref_name != 'deno_core'
-      && !startsWith(github.ref, 'refs/tags/deno/')
-    runs-on: ${{ matrix.os }}
-    timeout-minutes: 30
-    strategy:
-      matrix:
-        os: [macOS-latest, ubuntu-latest, windows-2019]
-
-    env:
-      CARGO_INCREMENTAL: 0
-      GH_ACTIONS: 1
-      RUST_BACKTRACE: full
-      RUSTFLAGS: -D warnings
-
+  success:
+    needs: top
+    runs-on: ubuntu-latest
     steps:
-      - name: Clone repository
-        uses: actions/checkout@v3
-
-      - name: Install Rust
-        uses: dsherret/rust-toolchain-file@v1
+      - name: Print success
+        run: echo Success ✅
 
-      - name: Install Rust (nightly)
-        if: contains(matrix.os, 'ubuntu')
-        uses: dtolnay/rust-toolchain@nightly
-
-      - name: Install Deno
-        uses: denoland/setup-deno@v1
-
-      - uses: Swatinem/rust-cache@v2
+  compute-jobs:
+    runs-on: ubuntu-latest
+    outputs:
+      matrix: ${{ steps.read-jobs.outputs.matrix }}
+    steps:
+      # https://github.com/marketplace/actions/query-yaml-config-as-outputs
+      - name: Read job configuration
+        uses: cloudposse/github-action-yaml-config-query@main
+        id: read-jobs
         with:
-          prefix-key: "v1-rust"
-          save-if: ${{ github.ref == 'refs/heads/main' }}
-
-      - name: Clean
-        if: github.ref == 'refs/heads/main'
-        run: cargo clean
-
-      - name: Format
-        if: contains(matrix.os, 'ubuntu')
-        run: |
-          cargo fmt -- --check
-          deno fmt --check ./core/ ./ops/ ./serde_v8/
-
-      - name: Lint
-        if: contains(matrix.os, 'ubuntu')
-        run: |
-          cargo clippy --locked --release --all-features --all-targets -- -D clippy::all
-          deno lint
-          tools/copyright_checker.js
-
-      - name: Lint deps
-        if: contains(matrix.os, 'ubuntu')
-        run: |
-          cargo install cargo-machete --locked
-          cargo machete
-          cargo install cargo-udeps --locked
-          cargo +nightly udeps
-
-      - name: Cargo test
-        run: cargo test --locked --release --all-features --bins --tests --examples && cargo test --doc
-
-      - name: Cargo test (miri)
-        if: contains(matrix.os, 'ubuntu')
-        run: |
-          # Install and run miri tests
-          rustup component add --toolchain nightly miri
-          # This somehow prints errors in CI that don't show up locally
-          MIRIFLAGS="-Zmiri-disable-isolation" RUSTFLAGS=-Awarnings cargo +nightly miri test -p deno_core
-
-      - name: Run examples
-        # Only regression tests here for now.
-        # Regression test for https://github.com/denoland/deno/pull/19615.
-        run: |-
-          cargo run -p deno_core --example op2
-          cargo run -p deno_core --example op2 --features include_js_files_for_snapshotting
-
-      - name: Cargo publish
-        if: |
-          contains(matrix.os, 'ubuntu') &&
-          github.repository == 'denoland/deno_core' &&
-          startsWith(github.ref, 'refs/tags/')
-        env:
-          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
-        run: cargo publish -p serde_v8 && cargo publish -p deno_ops && cargo publish -p deno_core
-
-      - name: Get tag version
-        if: contains(matrix.os, 'ubuntu') && startsWith(github.ref, 'refs/tags/')
-        id: get_tag_version
-        run: echo TAG_VERSION=${GITHUB_REF/refs\/tags\//} >> "$GITHUB_OUTPUT"
-
+          # Extract the jobs, then combine them with the os/uses line to build an array of 'includes' lines for the
+          # matrix below
+          # Test this using jq/yq: cat config.yaml | yq -o json | jq
+          query: "{ matrix: [. | to_entries[] | . as $entry | .value.jobs[] | { label: $entry.key, os: $entry.value.os, job: . }] }"
+          config: |-
+            linux:
+              os: ubuntu-latest
+              jobs: [lint, lint-deps, test, test-miri, test-ops]
+            macos:
+              os: macOS-latest
+              jobs: [lint, test]
+            windows:
+              os: windows-2019
+              jobs: [lint, test]
+
+  top:
+    name: build-${{ matrix.label }}
+    needs: compute-jobs
+    if: github.repository == 'denoland/deno_core'
+    uses: ./.github/workflows/ci-job.yml
+    with:
+      label: ${{ matrix.label }}
+      os: ${{ matrix.os }}
+      job: ${{ matrix.job }}
+    strategy:
+      matrix:
+        include: ${{ fromJSON(needs.compute-jobs.outputs.matrix) }}
+
+  publish:
+    name: publish
+    needs: top
+    if: github.repository == 'denoland/deno_core' && startsWith(github.ref, 'refs/tags/')
+    uses: ./.github/workflows/ci-job.yml
+    with:
+      label: linux
+      os: ubuntu-latest
+      job: publish
diff --git a/Cargo.lock b/Cargo.lock
index 800d9ef..c7bd4f7 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -272,7 +272,7 @@ dependencies = [
 
 [[package]]
 name = "deno_core"
-version = "0.249.0"
+version = "0.250.0"
 dependencies = [
  "anyhow",
  "bencher",
@@ -331,7 +331,7 @@ dependencies = [
 
 [[package]]
 name = "deno_ops"
-version = "0.125.0"
+version = "0.126.0"
 dependencies = [
  "pretty_assertions",
  "prettyplease",
@@ -1221,7 +1221,7 @@ dependencies = [
 
 [[package]]
 name = "serde_v8"
-version = "0.158.0"
+version = "0.159.0"
 dependencies = [
  "bencher",
  "bytes",
@@ -1230,11 +1230,19 @@ dependencies = [
  "serde",
  "serde_bytes",
  "serde_json",
+ "serde_v8_utilities",
  "smallvec",
  "thiserror",
  "v8",
 ]
 
+[[package]]
+name = "serde_v8_utilities"
+version = "0.0.0"
+dependencies = [
+ "v8",
+]
+
 [[package]]
 name = "sha-1"
 version = "0.10.0"
diff --git a/Cargo.toml b/Cargo.toml
index d65e329..d520ca7 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -18,9 +18,9 @@ repository = "https://github.com/denoland/deno_core"
 
 [workspace.dependencies]
 # Local dependencies
-deno_core = { version = "0.249.0", path = "./core" }
-deno_ops = { version = "0.125.0", path = "./ops" }
-serde_v8 = { version = "0.158.0", path = "./serde_v8" }
+deno_core = { version = "0.250.0", path = "./core" }
+deno_ops = { version = "0.126.0", path = "./ops" }
+serde_v8 = { version = "0.159.0", path = "./serde_v8" }
 
 v8 = { version = "0.83.1", default-features = false }
 deno_ast = { version = "=0.32.0", features = ["transpiling"] }
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 0566d0a..eeecf15 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -2,7 +2,7 @@
 
 [package]
 name = "deno_core"
-version = "0.249.0"
+version = "0.250.0"
 authors.workspace = true
 edition.workspace = true
 license.workspace = true
@@ -19,6 +19,7 @@ include_icu_data = []
 v8_use_custom_libcxx = ["v8/use_custom_libcxx"]
 include_js_files_for_snapshotting = []
 unsafe_runtime_options = []
+unsafe_use_unprotected_platform = []
 
 # Use the old, slower (but better tested) JoinSet driver
 op_driver_joinset = []
diff --git a/core/buffer_strategy.rs b/core/io/buffer_strategy.rs
similarity index 100%
rename from core/buffer_strategy.rs
rename to core/io/buffer_strategy.rs
diff --git a/core/io.rs b/core/io/buffers.rs
similarity index 80%
rename from core/io.rs
rename to core/io/buffers.rs
index e2ab5e6..9ed3728 100644
--- a/core/io.rs
+++ b/core/io/buffers.rs
@@ -174,6 +174,14 @@ impl From<BufView> for bytes::Bytes {
   }
 }
 
+/// BufMutViewWhole is equivalent to `BufMutView`, but cannot be split, preventing
+/// someone from accidentally holding a `BufView` down the road that is being actively
+/// mutated from JavaScript.
+pub struct BufMutViewWhole {
+  inner: BufMutViewInner,
+  cursor: usize,
+}
+
 /// BufMutView is a wrapper around an underlying contiguous chunk of writable
 /// bytes. It can be created from a `JsBuffer` or a `Vec<u8>` and implements
 /// `DerefMut<[u8]>` and `AsMut<[u8]>`.
@@ -196,6 +204,15 @@ enum BufMutViewInner {
   Bytes(BytesMut),
 }
 
+impl Default for BufMutView {
+  fn default() -> Self {
+    BufMutView {
+      inner: BufMutViewInner::Bytes(BytesMut::default()),
+      cursor: 0,
+    }
+  }
+}
+
 impl BufMutView {
   fn from_inner(inner: BufMutViewInner) -> Self {
     Self { inner, cursor: 0 }
@@ -412,18 +429,122 @@ impl From<BytesMut> for BufMutView {
   }
 }
 
-pub enum WriteOutcome {
-  Partial { nwritten: usize, view: BufView },
-  Full { nwritten: usize },
-}
+impl BufMutViewWhole {
+  fn from_inner(inner: BufMutViewInner) -> Self {
+    Self { inner, cursor: 0 }
+  }
+
+  pub fn new(len: usize) -> Self {
+    let bytes = BytesMut::zeroed(len);
+    Self::from_inner(BufMutViewInner::Bytes(bytes))
+  }
 
-impl WriteOutcome {
-  pub fn nwritten(&self) -> usize {
-    match self {
-      WriteOutcome::Partial { nwritten, .. } => *nwritten,
-      WriteOutcome::Full { nwritten } => *nwritten,
+  /// Get the length of the buffer view. This is the length of the underlying
+  /// buffer minus the cursor position.
+  pub fn len(&self) -> usize {
+    match &self.inner {
+      BufMutViewInner::JsBuffer(js_buf) => js_buf.len() - self.cursor,
+      BufMutViewInner::Bytes(bytes) => bytes.len() - self.cursor,
     }
   }
+
+  /// Is the buffer view empty?
+  pub fn is_empty(&self) -> bool {
+    self.len() == 0
+  }
+
+  /// Advance the internal cursor of the buffer view by `n` bytes.
+  pub fn advance_cursor(&mut self, n: usize) {
+    assert!(self.len() >= n);
+    self.cursor += n;
+  }
+
+  /// Reset the internal cursor of the buffer view to the beginning of the
+  /// buffer. Returns the old cursor position.
+  pub fn reset_cursor(&mut self) -> usize {
+    let old = self.cursor;
+    self.cursor = 0;
+    old
+  }
+
+  /// Turn this `BufMutView` into a `BufView`.
+  pub fn into_view(self) -> BufView {
+    let inner = match self.inner {
+      BufMutViewInner::JsBuffer(js_buf) => BufViewInner::JsBuffer(js_buf),
+      BufMutViewInner::Bytes(bytes) => BufViewInner::Bytes(bytes.into()),
+    };
+    BufView {
+      inner,
+      cursor: self.cursor,
+    }
+  }
+
+  /// Attempts to unwrap the underlying buffer into a [`BytesMut`], consuming the `BufMutView`. If
+  /// this buffer does not have a [`BytesMut`], returns `Self`.
+  pub fn maybe_unwrap_bytes(self) -> Result<BytesMut, Self> {
+    match self.inner {
+      BufMutViewInner::JsBuffer(_) => Err(self),
+      BufMutViewInner::Bytes(bytes) => Ok(bytes),
+    }
+  }
+
+  /// Adjust the length of the remaining buffer and ensure that the cursor continues to
+  /// stay in-bounds.
+  pub fn truncate(&mut self, size: usize) {
+    match &mut self.inner {
+      BufMutViewInner::Bytes(bytes) => bytes.truncate(size + self.cursor),
+      BufMutViewInner::JsBuffer(buffer) => buffer.truncate(size + self.cursor),
+    }
+    self.cursor = std::cmp::min(self.cursor, self.len());
+  }
+}
+
+impl Buf for BufMutViewWhole {
+  fn remaining(&self) -> usize {
+    self.len()
+  }
+
+  fn chunk(&self) -> &[u8] {
+    self.deref()
+  }
+
+  fn advance(&mut self, cnt: usize) {
+    self.advance_cursor(cnt)
+  }
+}
+
+impl Deref for BufMutViewWhole {
+  type Target = [u8];
+
+  fn deref(&self) -> &[u8] {
+    let buf = match &self.inner {
+      BufMutViewInner::JsBuffer(js_buf) => js_buf.deref(),
+      BufMutViewInner::Bytes(vec) => vec.deref(),
+    };
+    &buf[self.cursor..]
+  }
+}
+
+impl DerefMut for BufMutViewWhole {
+  fn deref_mut(&mut self) -> &mut [u8] {
+    let buf = match &mut self.inner {
+      BufMutViewInner::JsBuffer(js_buf) => js_buf.deref_mut(),
+      BufMutViewInner::Bytes(vec) => vec.deref_mut(),
+    };
+    &mut buf[self.cursor..]
+  }
+}
+
+impl From<JsBuffer> for BufMutViewWhole {
+  fn from(buf: JsBuffer) -> Self {
+    Self::from_inner(BufMutViewInner::JsBuffer(buf.into_parts()))
+  }
+}
+
+impl From<BytesMut> for BufMutViewWhole {
+  fn from(buf: BytesMut) -> Self {
+    Self::from_inner(BufMutViewInner::Bytes(buf))
+  }
 }
 
 #[cfg(test)]
diff --git a/core/io/mod.rs b/core/io/mod.rs
new file mode 100644
index 0000000..c131755
--- /dev/null
+++ b/core/io/mod.rs
@@ -0,0 +1,45 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+
+// Think of Resources as File Descriptors. They are integers that are allocated
+// by the privileged side of Deno which refer to various rust objects that need
+// to be persisted between various ops. For example, network sockets are
+// resources. Resources may or may not correspond to a real operating system
+// file descriptor (hence the different name).
+
+use anyhow::Error;
+use futures::Future;
+use std::pin::Pin;
+
+mod buffer_strategy;
+mod buffers;
+mod resource;
+mod resource_handle;
+mod resource_table;
+
+pub use buffer_strategy::AdaptiveBufferStrategy;
+pub use buffers::BufMutView;
+pub use buffers::BufMutViewWhole;
+pub use buffers::BufView;
+pub use resource::Resource;
+pub use resource_handle::ResourceHandle;
+pub use resource_handle::ResourceHandleFd;
+pub use resource_handle::ResourceHandleSocket;
+pub use resource_table::ResourceId;
+pub use resource_table::ResourceTable;
+
+/// Returned by resource shutdown methods
+pub type AsyncResult<T> = Pin<Box<dyn Future<Output = Result<T, Error>>>>;
+
+pub enum WriteOutcome {
+  Partial { nwritten: usize, view: BufView },
+  Full { nwritten: usize },
+}
+
+impl WriteOutcome {
+  pub fn nwritten(&self) -> usize {
+    match self {
+      WriteOutcome::Partial { nwritten, .. } => *nwritten,
+      WriteOutcome::Full { nwritten } => *nwritten,
+    }
+  }
+}
diff --git a/core/io/resource.rs b/core/io/resource.rs
new file mode 100644
index 0000000..752fd5c
--- /dev/null
+++ b/core/io/resource.rs
@@ -0,0 +1,286 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+
+// Think of Resources as File Descriptors. They are integers that are allocated
+// by the privileged side of Deno which refer to various rust objects that need
+// to be persisted between various ops. For example, network sockets are
+// resources. Resources may or may not correspond to a real operating system
+// file descriptor (hence the different name).
+
+use crate::error::not_supported;
+use crate::io::AsyncResult;
+use crate::io::BufMutView;
+use crate::io::BufView;
+use crate::io::WriteOutcome;
+use crate::ResourceHandle;
+use crate::ResourceHandleFd;
+use anyhow::Error;
+use std::any::type_name;
+use std::any::Any;
+use std::any::TypeId;
+use std::borrow::Cow;
+use std::rc::Rc;
+
+/// Resources are Rust objects that are attached to a [deno_core::JsRuntime].
+/// They are identified in JS by a numeric ID (the resource ID, or rid).
+/// Resources can be created in ops. Resources can also be retrieved in ops by
+/// their rid. Resources are not thread-safe - they can only be accessed from
+/// the thread that the JsRuntime lives on.
+///
+/// Resources are reference counted in Rust. This means that they can be
+/// cloned and passed around. When the last reference is dropped, the resource
+/// is automatically closed. As long as the resource exists in the resource
+/// table, the reference count is at least 1.
+///
+/// ### Readable
+///
+/// Readable resources are resources that can have data read from. Examples of
+/// this are files, sockets, or HTTP streams.
+///
+/// Readables can be read from from either JS or Rust. In JS one can use
+/// `Deno.core.read()` to read from a single chunk of data from a readable. In
+/// Rust one can directly call `read()` or `read_byob()`. The Rust side code is
+/// used to implement ops like `op_slice`.
+///
+/// A distinction can be made between readables that produce chunks of data
+/// themselves (they allocate the chunks), and readables that fill up
+/// bring-your-own-buffers (BYOBs). The former is often the case for framed
+/// protocols like HTTP, while the latter is often the case for kernel backed
+/// resources like files and sockets.
+///
+/// All readables must implement `read()`. If resources can support an optimized
+/// path for BYOBs, they should also implement `read_byob()`. For kernel backed
+/// resources it often makes sense to implement `read_byob()` first, and then
+/// implement `read()` as an operation that allocates a new chunk with
+/// `len == limit`, then calls `read_byob()`, and then returns a chunk sliced to
+/// the number of bytes read. Kernel backed resources can use the
+/// [deno_core::impl_readable_byob] macro to implement optimized `read_byob()`
+/// and `read()` implementations from a single `Self::read()` method.
+///
+/// ### Writable
+///
+/// Writable resources are resources that can have data written to. Examples of
+/// this are files, sockets, or HTTP streams.
+///
+/// Writables can be written to from either JS or Rust. In JS one can use
+/// `Deno.core.write()` to write to a single chunk of data to a writable. In
+/// Rust one can directly call `write()`. The latter is used to implement ops
+/// like `op_slice`.
+pub trait Resource: Any + 'static {
+  /// Returns a string representation of the resource which is made available
+  /// to JavaScript code through `op_resources`. The default implementation
+  /// returns the Rust type name, but specific resource types may override this
+  /// trait method.
+  fn name(&self) -> Cow<str> {
+    type_name::<Self>().into()
+  }
+
+  /// Read a single chunk of data from the resource. This operation returns a
+  /// `BufView` that represents the data that was read. If a zero length buffer
+  /// is returned, it indicates that the resource has reached EOF.
+  ///
+  /// If this method is not implemented, the default implementation will error
+  /// with a "not supported" error.
+  ///
+  /// If a readable can provide an optimized path for BYOBs, it should also
+  /// implement `read_byob()`.
+  fn read(self: Rc<Self>, limit: usize) -> AsyncResult<BufView> {
+    _ = limit;
+    Box::pin(futures::future::err(not_supported()))
+  }
+
+  /// Read a single chunk of data from the resource into the provided `BufMutView`.
+  ///
+  /// This operation returns the number of bytes read. If zero bytes are read,
+  /// it indicates that the resource has reached EOF.
+  ///
+  /// If this method is not implemented explicitly, the default implementation
+  /// will call `read()` and then copy the data into the provided buffer. For
+  /// readable resources that can provide an optimized path for BYOBs, it is
+  /// strongly recommended to override this method.
+  fn read_byob(
+    self: Rc<Self>,
+    mut buf: BufMutView,
+  ) -> AsyncResult<(usize, BufMutView)> {
+    Box::pin(async move {
+      let read = self.read(buf.len()).await?;
+      let nread = read.len();
+      buf[..nread].copy_from_slice(&read);
+      Ok((nread, buf))
+    })
+  }
+
+  /// Write an error state to this resource, if the resource supports it.
+  fn write_error(self: Rc<Self>, _error: Error) -> AsyncResult<()> {
+    Box::pin(futures::future::err(not_supported()))
+  }
+
+  /// Write a single chunk of data to the resource. The operation may not be
+  /// able to write the entire chunk, in which case it should return the number
+  /// of bytes written. Additionally it should return the `BufView` that was
+  /// passed in.
+  ///
+  /// If this method is not implemented, the default implementation will error
+  /// with a "not supported" error.
+  fn write(self: Rc<Self>, buf: BufView) -> AsyncResult<WriteOutcome> {
+    _ = buf;
+    Box::pin(futures::future::err(not_supported()))
+  }
+
+  /// Write an entire chunk of data to the resource. Unlike `write()`, this will
+  /// ensure the entire chunk is written. If the operation is not able to write
+  /// the entire chunk, an error is to be returned.
+  ///
+  /// By default this method will call `write()` repeatedly until the entire
+  /// chunk is written. Resources that can write the entire chunk in a single
+  /// operation using an optimized path should override this method.
+  fn write_all(self: Rc<Self>, view: BufView) -> AsyncResult<()> {
+    Box::pin(async move {
+      let mut view = view;
+      let this = self;
+      while !view.is_empty() {
+        let resp = this.clone().write(view).await?;
+        match resp {
+          WriteOutcome::Partial {
+            nwritten,
+            view: new_view,
+          } => {
+            view = new_view;
+            view.advance_cursor(nwritten);
+          }
+          WriteOutcome::Full { .. } => break,
+        }
+      }
+      Ok(())
+    })
+  }
+
+  /// The same as [`read_byob()`][Resource::read_byob], but synchronous.
+  fn read_byob_sync(self: Rc<Self>, data: &mut [u8]) -> Result<usize, Error> {
+    _ = data;
+    Err(not_supported())
+  }
+
+  /// The same as [`write()`][Resource::write], but synchronous.
+  fn write_sync(self: Rc<Self>, data: &[u8]) -> Result<usize, Error> {
+    _ = data;
+    Err(not_supported())
+  }
+
+  /// The shutdown method can be used to asynchronously close the resource. It
+  /// is not automatically called when the resource is dropped or closed.
+  ///
+  /// If this method is not implemented, the default implementation will error
+  /// with a "not supported" error.
+  fn shutdown(self: Rc<Self>) -> AsyncResult<()> {
+    Box::pin(futures::future::err(not_supported()))
+  }
+
+  /// Resources may implement the `close()` trait method if they need to do
+  /// resource specific clean-ups, such as cancelling pending futures, after a
+  /// resource has been removed from the resource table.
+  fn close(self: Rc<Self>) {}
+
+  /// Resources backed by a file descriptor or socket handle can let ops know
+  /// to allow for low-level optimizations.
+  fn backing_handle(self: Rc<Self>) -> Option<ResourceHandle> {
+    #[allow(deprecated)]
+    self.backing_fd().map(ResourceHandle::Fd)
+  }
+
+  /// Resources backed by a file descriptor can let ops know to allow for
+  /// low-level optimizations.
+  #[deprecated = "Use backing_handle"]
+  fn backing_fd(self: Rc<Self>) -> Option<ResourceHandleFd> {
+    None
+  }
+
+  fn size_hint(&self) -> (u64, Option<u64>) {
+    (0, None)
+  }
+}
+
+impl dyn Resource {
+  #[inline(always)]
+  fn is<T: Resource>(&self) -> bool {
+    self.type_id() == TypeId::of::<T>()
+  }
+
+  #[inline(always)]
+  #[allow(clippy::needless_lifetimes)]
+  pub fn downcast_rc<'a, T: Resource>(self: &'a Rc<Self>) -> Option<&'a Rc<T>> {
+    if self.is::<T>() {
+      let ptr = self as *const Rc<_> as *const Rc<T>;
+      // TODO(piscisaureus): safety comment
+      #[allow(clippy::undocumented_unsafe_blocks)]
+      Some(unsafe { &*ptr })
+    } else {
+      None
+    }
+  }
+}
+
+#[macro_export]
+macro_rules! impl_readable_byob {
+  () => {
+    fn read(
+      self: ::std::rc::Rc<Self>,
+      limit: ::core::primitive::usize,
+    ) -> AsyncResult<$crate::BufView> {
+      ::std::boxed::Box::pin(async move {
+        let mut vec = ::std::vec![0; limit];
+        let nread = self.read(&mut vec).await?;
+        if nread != vec.len() {
+          vec.truncate(nread);
+        }
+        let view = $crate::BufView::from(vec);
+        ::std::result::Result::Ok(view)
+      })
+    }
+
+    fn read_byob(
+      self: ::std::rc::Rc<Self>,
+      mut buf: $crate::BufMutView,
+    ) -> AsyncResult<(::core::primitive::usize, $crate::BufMutView)> {
+      ::std::boxed::Box::pin(async move {
+        let nread = self.read(buf.as_mut()).await?;
+        ::std::result::Result::Ok((nread, buf))
+      })
+    }
+  };
+}
+
+#[macro_export]
+macro_rules! impl_writable {
+  (__write) => {
+    fn write(
+      self: ::std::rc::Rc<Self>,
+      view: $crate::BufView,
+    ) -> $crate::AsyncResult<$crate::WriteOutcome> {
+      ::std::boxed::Box::pin(async move {
+        let nwritten = self.write(&view).await?;
+        ::std::result::Result::Ok($crate::WriteOutcome::Partial {
+          nwritten,
+          view,
+        })
+      })
+    }
+  };
+  (__write_all) => {
+    fn write_all(
+      self: ::std::rc::Rc<Self>,
+      view: $crate::BufView,
+    ) -> $crate::AsyncResult<()> {
+      ::std::boxed::Box::pin(async move {
+        self.write_all(&view).await?;
+        ::std::result::Result::Ok(())
+      })
+    }
+  };
+  () => {
+    $crate::impl_writable!(__write);
+  };
+  (with_all) => {
+    $crate::impl_writable!(__write);
+    $crate::impl_writable!(__write_all);
+  };
+}
diff --git a/core/io/resource_handle.rs b/core/io/resource_handle.rs
new file mode 100644
index 0000000..65b7a93
--- /dev/null
+++ b/core/io/resource_handle.rs
@@ -0,0 +1,110 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+use std::io::IsTerminal;
+
+/// Represents an underlying handle for a platform. On unix, everything is an `fd`. On Windows, everything
+/// is a Windows handle except for sockets (which are `SOCKET`s).
+#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
+#[allow(unused)]
+pub enum ResourceHandle {
+  /// A file handle/descriptor.
+  Fd(ResourceHandleFd),
+  /// A socket handle/file descriptor.
+  Socket(ResourceHandleSocket),
+}
+
+#[cfg(unix)]
+pub type ResourceHandleFd = std::os::fd::RawFd;
+#[cfg(unix)]
+pub type ResourceHandleSocket = std::os::fd::RawFd;
+#[cfg(windows)]
+pub type ResourceHandleFd = std::os::windows::io::RawHandle;
+#[cfg(windows)]
+pub type ResourceHandleSocket = std::os::windows::io::RawSocket;
+
+impl ResourceHandle {
+  /// Converts a file-like thing to a [`ResourceHandle`].
+  #[cfg(windows)]
+  pub fn from_fd_like(io: &impl std::os::windows::io::AsRawHandle) -> Self {
+    Self::Fd(io.as_raw_handle())
+  }
+
+  /// Converts a file-like thing to a [`ResourceHandle`].
+  #[cfg(unix)]
+  pub fn from_fd_like(io: &impl std::os::unix::io::AsRawFd) -> Self {
+    Self::Fd(io.as_raw_fd())
+  }
+
+  /// Converts a socket-like thing to a [`ResourceHandle`].
+  #[cfg(windows)]
+  pub fn from_socket_like(io: &impl std::os::windows::io::AsRawSocket) -> Self {
+    Self::Socket(io.as_raw_socket())
+  }
+
+  /// Converts a socket-like thing to a [`ResourceHandle`].
+  #[cfg(unix)]
+  pub fn from_socket_like(io: &impl std::os::unix::io::AsRawFd) -> Self {
+    Self::Socket(io.as_raw_fd())
+  }
+
+  /// Runs a basic validity check on the handle, but cannot fully determine if the handle is valid for use.
+  pub fn is_valid(&self) -> bool {
+    #[cfg(windows)]
+    {
+      match self {
+        // NULL or INVALID_HANDLE_VALUE
+        Self::Fd(handle) => {
+          !handle.is_null()
+            && *handle != -1_isize as std::os::windows::io::RawHandle
+        }
+        // INVALID_SOCKET
+        Self::Socket(socket) => {
+          *socket != -1_i64 as std::os::windows::io::RawSocket
+        }
+      }
+    }
+    #[cfg(unix)]
+    {
+      match self {
+        Self::Fd(fd) => *fd >= 0,
+        Self::Socket(fd) => *fd >= 0,
+      }
+    }
+  }
+
+  /// Returns this as a file-descriptor-like handle.
+  pub fn as_fd_like(&self) -> Option<ResourceHandleFd> {
+    match self {
+      Self::Fd(fd) => Some(*fd),
+      _ => None,
+    }
+  }
+
+  /// Returns this as a socket-like handle.
+  pub fn as_socket_like(&self) -> Option<ResourceHandleSocket> {
+    match self {
+      Self::Socket(socket) => Some(*socket),
+      _ => None,
+    }
+  }
+
+  /// Determines if this handle is a terminal. Analagous to [`std::io::IsTerminal`].
+  pub fn is_terminal(&self) -> bool {
+    match self {
+      Self::Fd(fd) if self.is_valid() => {
+        #[cfg(windows)]
+        {
+          // SAFETY: The resource remains open for the for the duration of borrow_raw
+          unsafe {
+            std::os::windows::io::BorrowedHandle::borrow_raw(*fd).is_terminal()
+          }
+        }
+        #[cfg(unix)]
+        {
+          // SAFETY: The resource remains open for the for the duration of borrow_raw
+          unsafe { std::os::fd::BorrowedFd::borrow_raw(*fd).is_terminal() }
+        }
+      }
+      _ => false,
+    }
+  }
+}
diff --git a/core/io/resource_table.rs b/core/io/resource_table.rs
new file mode 100644
index 0000000..747c0e8
--- /dev/null
+++ b/core/io/resource_table.rs
@@ -0,0 +1,223 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+use super::Resource;
+use super::ResourceHandle;
+use super::ResourceHandleFd;
+use super::ResourceHandleSocket;
+use crate::error::bad_resource_id;
+use crate::error::custom_error;
+use anyhow::Error;
+use std::borrow::Cow;
+use std::collections::BTreeMap;
+use std::iter::Iterator;
+use std::rc::Rc;
+
+/// A `ResourceId` is an integer value referencing a resource. It could be
+/// considered to be the Deno equivalent of a `file descriptor` in POSIX like
+/// operating systems. Elsewhere in the code base it is commonly abbreviated
+/// to `rid`.
+// TODO: use `u64` instead?
+pub type ResourceId = u32;
+/// Map-like data structure storing Deno's resources (equivalent to file
+/// descriptors).
+///
+/// Provides basic methods for element access. A resource can be of any type.
+/// Different types of resources can be stored in the same map, and provided
+/// with a name for description.
+///
+/// Each resource is identified through a _resource ID (rid)_, which acts as
+/// the key in the map.
+#[derive(Default)]
+pub struct ResourceTable {
+  index: BTreeMap<ResourceId, Rc<dyn Resource>>,
+  next_rid: ResourceId,
+}
+
+impl ResourceTable {
+  /// Returns the number of resources currently active in the resource table.
+  /// Resources taken from the table do not contribute to this count.
+  pub fn len(&self) -> usize {
+    self.index.len()
+  }
+
+  /// Returns whether this table is empty.
+  pub fn is_empty(&self) -> bool {
+    self.index.is_empty()
+  }
+
+  /// Inserts resource into the resource table, which takes ownership of it.
+  ///
+  /// The resource type is erased at runtime and must be statically known
+  /// when retrieving it through `get()`.
+  ///
+  /// Returns a unique resource ID, which acts as a key for this resource.
+  pub fn add<T: Resource>(&mut self, resource: T) -> ResourceId {
+    self.add_rc(Rc::new(resource))
+  }
+
+  /// Inserts a `Rc`-wrapped resource into the resource table.
+  ///
+  /// The resource type is erased at runtime and must be statically known
+  /// when retrieving it through `get()`.
+  ///
+  /// Returns a unique resource ID, which acts as a key for this resource.
+  pub fn add_rc<T: Resource>(&mut self, resource: Rc<T>) -> ResourceId {
+    let resource = resource as Rc<dyn Resource>;
+    self.add_rc_dyn(resource)
+  }
+
+  pub fn add_rc_dyn(&mut self, resource: Rc<dyn Resource>) -> ResourceId {
+    let rid = self.next_rid;
+    let removed_resource = self.index.insert(rid, resource);
+    assert!(removed_resource.is_none());
+    self.next_rid += 1;
+    rid
+  }
+
+  /// Returns true if any resource with the given `rid` exists.
+  pub fn has(&self, rid: ResourceId) -> bool {
+    self.index.contains_key(&rid)
+  }
+
+  /// Returns a reference counted pointer to the resource of type `T` with the
+  /// given `rid`. If `rid` is not present or has a type different than `T`,
+  /// this function returns `None`.
+  pub fn get<T: Resource>(&self, rid: ResourceId) -> Result<Rc<T>, Error> {
+    self
+      .index
+      .get(&rid)
+      .and_then(|rc| rc.downcast_rc::<T>())
+      .map(Clone::clone)
+      .ok_or_else(bad_resource_id)
+  }
+
+  pub fn get_any(&self, rid: ResourceId) -> Result<Rc<dyn Resource>, Error> {
+    self
+      .index
+      .get(&rid)
+      .map(Clone::clone)
+      .ok_or_else(bad_resource_id)
+  }
+
+  /// Replaces a resource with a new resource.
+  ///
+  /// Panics if the resource does not exist.
+  pub fn replace<T: Resource>(&mut self, rid: ResourceId, resource: T) {
+    let result = self
+      .index
+      .insert(rid, Rc::new(resource) as Rc<dyn Resource>);
+    assert!(result.is_some());
+  }
+
+  /// Removes a resource of type `T` from the resource table and returns it.
+  /// If a resource with the given `rid` exists but its type does not match `T`,
+  /// it is not removed from the resource table. Note that the resource's
+  /// `close()` method is *not* called.
+  ///
+  /// Also note that there might be a case where
+  /// the returned `Rc<T>` is referenced by other variables. That is, we cannot
+  /// assume that `Rc::strong_count(&returned_rc)` is always equal to 1 on success.
+  /// In particular, be really careful when you want to extract the inner value of
+  /// type `T` from `Rc<T>`.
+  pub fn take<T: Resource>(&mut self, rid: ResourceId) -> Result<Rc<T>, Error> {
+    let resource = self.get::<T>(rid)?;
+    self.index.remove(&rid);
+    Ok(resource)
+  }
+
+  /// Removes a resource from the resource table and returns it. Note that the
+  /// resource's `close()` method is *not* called.
+  ///
+  /// Also note that there might be a
+  /// case where the returned `Rc<T>` is referenced by other variables. That is,
+  /// we cannot assume that `Rc::strong_count(&returned_rc)` is always equal to 1
+  /// on success. In particular, be really careful when you want to extract the
+  /// inner value of type `T` from `Rc<T>`.
+  pub fn take_any(
+    &mut self,
+    rid: ResourceId,
+  ) -> Result<Rc<dyn Resource>, Error> {
+    self.index.remove(&rid).ok_or_else(bad_resource_id)
+  }
+
+  /// Removes the resource with the given `rid` from the resource table. If the
+  /// only reference to this resource existed in the resource table, this will
+  /// cause the resource to be dropped. However, since resources are reference
+  /// counted, therefore pending ops are not automatically cancelled. A resource
+  /// may implement the `close()` method to perform clean-ups such as canceling
+  /// ops.
+  #[deprecated = "This method may deadlock. Use take() and close() instead."]
+  pub fn close(&mut self, rid: ResourceId) -> Result<(), Error> {
+    self
+      .index
+      .remove(&rid)
+      .ok_or_else(bad_resource_id)
+      .map(|resource| resource.close())
+  }
+
+  /// Returns an iterator that yields a `(id, name)` pair for every resource
+  /// that's currently in the resource table. This can be used for debugging
+  /// purposes or to implement the `op_resources` op. Note that the order in
+  /// which items appear is not specified.
+  ///
+  /// # Example
+  ///
+  /// ```
+  /// # use deno_core::ResourceTable;
+  /// # let resource_table = ResourceTable::default();
+  /// let resource_names = resource_table.names().collect::<Vec<_>>();
+  /// ```
+  pub fn names(&self) -> impl Iterator<Item = (ResourceId, Cow<str>)> {
+    self
+      .index
+      .iter()
+      .map(|(&id, resource)| (id, resource.name()))
+  }
+
+  /// Retrieves the [`ResourceHandleFd`] for a given resource, for potential optimization
+  /// purposes within ops.
+  pub fn get_fd(&self, rid: ResourceId) -> Result<ResourceHandleFd, Error> {
+    let Some(handle) = self.get_any(rid)?.backing_handle() else {
+      return Err(bad_resource_id());
+    };
+    let Some(fd) = handle.as_fd_like() else {
+      return Err(bad_resource_id());
+    };
+    if !handle.is_valid() {
+      return Err(custom_error("ReferenceError", "null or invalid handle"));
+    }
+    Ok(fd)
+  }
+
+  /// Retrieves the [`ResourceHandleSocket`] for a given resource, for potential optimization
+  /// purposes within ops.
+  pub fn get_socket(
+    &self,
+    rid: ResourceId,
+  ) -> Result<ResourceHandleSocket, Error> {
+    let Some(handle) = self.get_any(rid)?.backing_handle() else {
+      return Err(bad_resource_id());
+    };
+    let Some(socket) = handle.as_socket_like() else {
+      return Err(bad_resource_id());
+    };
+    if !handle.is_valid() {
+      return Err(custom_error("ReferenceError", "null or invalid handle"));
+    }
+    Ok(socket)
+  }
+
+  /// Retrieves the [`ResourceHandle`] for a given resource, for potential optimization
+  /// purposes within ops.
+  pub fn get_handle(
+    &self,
+    rid: ResourceId,
+  ) -> ::std::result::Result<ResourceHandle, ::anyhow::Error> {
+    let Some(handle) = self.get_any(rid)?.backing_handle() else {
+      return Err(bad_resource_id());
+    };
+    if !handle.is_valid() {
+      return Err(custom_error("ReferenceError", "null or invalid handle"));
+    }
+    Ok(handle)
+  }
+}
diff --git a/core/lib.rs b/core/lib.rs
index 7dfa21f..db9eeea 100644
--- a/core/lib.rs
+++ b/core/lib.rs
@@ -2,7 +2,6 @@
 pub mod arena;
 mod async_cancel;
 mod async_cell;
-mod buffer_strategy;
 pub mod cppgc;
 pub mod error;
 mod error_codes;
@@ -23,7 +22,6 @@ mod ops_builtin_types;
 mod ops_builtin_v8;
 mod ops_metrics;
 mod path;
-mod resources;
 mod runtime;
 mod source_map;
 mod tasks;
@@ -82,8 +80,16 @@ pub use crate::inspector::InspectorMsgKind;
 pub use crate::inspector::InspectorSessionProxy;
 pub use crate::inspector::JsRuntimeInspector;
 pub use crate::inspector::LocalInspectorSession;
+pub use crate::io::AsyncResult;
 pub use crate::io::BufMutView;
+pub use crate::io::BufMutViewWhole;
 pub use crate::io::BufView;
+pub use crate::io::Resource;
+pub use crate::io::ResourceHandle;
+pub use crate::io::ResourceHandleFd;
+pub use crate::io::ResourceHandleSocket;
+pub use crate::io::ResourceId;
+pub use crate::io::ResourceTable;
 pub use crate::io::WriteOutcome;
 pub use crate::module_specifier::resolve_import;
 pub use crate::module_specifier::resolve_path;
@@ -124,13 +130,6 @@ pub use crate::ops_metrics::OpMetricsSource;
 pub use crate::ops_metrics::OpMetricsSummary;
 pub use crate::ops_metrics::OpMetricsSummaryTracker;
 pub use crate::path::strip_unc_prefix;
-pub use crate::resources::AsyncResult;
-pub use crate::resources::Resource;
-pub use crate::resources::ResourceHandle;
-pub use crate::resources::ResourceHandleFd;
-pub use crate::resources::ResourceHandleSocket;
-pub use crate::resources::ResourceId;
-pub use crate::resources::ResourceTable;
 pub use crate::runtime::stats;
 pub use crate::runtime::CompiledWasmModuleStore;
 pub use crate::runtime::CreateRealmOptions;
diff --git a/core/ops.rs b/core/ops.rs
index 81f5b14..49fe334 100644
--- a/core/ops.rs
+++ b/core/ops.rs
@@ -3,8 +3,8 @@
 use crate::error::AnyError;
 use crate::error::GetErrorClassFn;
 use crate::gotham_state::GothamState;
+use crate::io::ResourceTable;
 use crate::ops_metrics::OpMetricsFn;
-use crate::resources::ResourceTable;
 use crate::runtime::ContextState;
 use crate::runtime::JsRuntimeState;
 use crate::FeatureChecker;
diff --git a/core/ops_builtin.rs b/core/ops_builtin.rs
index 913287d..293ca78 100644
--- a/core/ops_builtin.rs
+++ b/core/ops_builtin.rs
@@ -1,13 +1,13 @@
 // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
-use crate::buffer_strategy::AdaptiveBufferStrategy;
 use crate::error::format_file_name;
 use crate::error::type_error;
+use crate::io::AdaptiveBufferStrategy;
 use crate::io::BufMutView;
 use crate::io::BufView;
+use crate::io::ResourceId;
 use crate::op2;
 use crate::ops_builtin_types;
 use crate::ops_builtin_v8;
-use crate::resources::ResourceId;
 use crate::JsBuffer;
 use crate::OpState;
 use crate::Resource;
diff --git a/core/resources.rs b/core/resources.rs
index de3d8f0..56a3c78 100644
--- a/core/resources.rs
+++ b/core/resources.rs
@@ -79,12 +79,12 @@ impl ResourceHandle {
       match self {
         // NULL or INVALID_HANDLE_VALUE
         Self::Fd(handle) => {
-          return !handle.is_null()
+          !handle.is_null()
             && *handle != -1_isize as std::os::windows::io::RawHandle
         }
         // INVALID_SOCKET
         Self::Socket(socket) => {
-          return *socket != -1_i64 as std::os::windows::io::RawSocket
+          *socket != -1_i64 as std::os::windows::io::RawSocket
         }
       }
     }
diff --git a/core/runtime/jsruntime.rs b/core/runtime/jsruntime.rs
index 1eb0b1a..a77d440 100644
--- a/core/runtime/jsruntime.rs
+++ b/core/runtime/jsruntime.rs
@@ -413,8 +413,15 @@ fn v8_init(
   };
   v8::V8::set_flags_from_string(&flags);
 
-  let v8_platform = v8_platform
-    .unwrap_or_else(|| v8::new_default_platform(0, false).make_shared());
+  let v8_platform = v8_platform.unwrap_or_else(|| {
+    if cfg!(any(test, feature = "unsafe_use_unprotected_platform")) {
+      // We want to use the unprotected platform for unit tests
+      v8::new_unprotected_default_platform(0, false)
+    } else {
+      v8::new_default_platform(0, false)
+    }
+    .make_shared()
+  });
   v8::V8::initialize_platform(v8_platform.clone());
   v8::V8::initialize();
 
diff --git a/ops/Cargo.toml b/ops/Cargo.toml
index c8bd561..0535d38 100644
--- a/ops/Cargo.toml
+++ b/ops/Cargo.toml
@@ -2,7 +2,7 @@
 
 [package]
 name = "deno_ops"
-version = "0.125.0"
+version = "0.126.0"
 authors.workspace = true
 edition.workspace = true
 license.workspace = true
diff --git a/serde_v8/Cargo.toml b/serde_v8/Cargo.toml
index 9f99aea..d3ab680 100644
--- a/serde_v8/Cargo.toml
+++ b/serde_v8/Cargo.toml
@@ -2,7 +2,7 @@
 
 [package]
 name = "serde_v8"
-version = "0.158.0"
+version = "0.159.0"
 authors.workspace = true
 edition.workspace = true
 license.workspace = true
@@ -23,6 +23,7 @@ thiserror.workspace = true
 v8.workspace = true
 
 [dev-dependencies]
+serde_v8_utilities = { path = "./utilities" }
 bencher.workspace = true
 serde_json.workspace = true
 serde_bytes.workspace = true
diff --git a/serde_v8/benches/de.rs b/serde_v8/benches/de.rs
index b7cd0bf..a355e98 100644
--- a/serde_v8/benches/de.rs
+++ b/serde_v8/benches/de.rs
@@ -5,9 +5,9 @@ use bencher::Bencher;
 
 use serde::Deserialize;
 
-use serde_v8::utils::js_exec;
-use serde_v8::utils::v8_do;
 use serde_v8::ByteString;
+use serde_v8_utilities::js_exec;
+use serde_v8_utilities::v8_do;
 
 #[derive(Debug, Deserialize, PartialEq)]
 struct MathOp {
diff --git a/serde_v8/benches/ser.rs b/serde_v8/benches/ser.rs
index 83274fa..faf96de 100644
--- a/serde_v8/benches/ser.rs
+++ b/serde_v8/benches/ser.rs
@@ -5,8 +5,8 @@ use bencher::Bencher;
 
 use serde::Serialize;
 
-use serde_v8::utils::v8_do;
 use serde_v8::ByteString;
+use serde_v8_utilities::v8_do;
 
 #[derive(Serialize)]
 struct MathOp {
diff --git a/serde_v8/lib.rs b/serde_v8/lib.rs
index 3bce2e5..5dabe48 100644
--- a/serde_v8/lib.rs
+++ b/serde_v8/lib.rs
@@ -6,7 +6,6 @@ mod magic;
 mod payload;
 mod ser;
 mod serializable;
-pub mod utils;
 
 pub use de::from_v8;
 pub use de::from_v8_cached;
diff --git a/serde_v8/magic/v8slice.rs b/serde_v8/magic/v8slice.rs
index c2f0be2..d04e8b9 100644
--- a/serde_v8/magic/v8slice.rs
+++ b/serde_v8/magic/v8slice.rs
@@ -107,7 +107,8 @@ where
     static V8_ONCE: std::sync::Once = std::sync::Once::new();
 
     V8_ONCE.call_once(|| {
-      let platform = v8::new_default_platform(0, false).make_shared();
+      let platform =
+        v8::new_unprotected_default_platform(0, false).make_shared();
       v8::V8::initialize_platform(platform);
       v8::V8::initialize();
     });
diff --git a/serde_v8/utilities/Cargo.toml b/serde_v8/utilities/Cargo.toml
new file mode 100644
index 0000000..fbdceda
--- /dev/null
+++ b/serde_v8/utilities/Cargo.toml
@@ -0,0 +1,11 @@
+# Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+
+[package]
+name = "serde_v8_utilities"
+version = "0.0.0"
+
+[lib]
+path = "lib.rs"
+
+[dependencies]
+v8.workspace = true
diff --git a/serde_v8/utils.rs b/serde_v8/utilities/lib.rs
similarity index 90%
rename from serde_v8/utils.rs
rename to serde_v8/utilities/lib.rs
index 6a97324..733dcce 100644
--- a/serde_v8/utils.rs
+++ b/serde_v8/utilities/lib.rs
@@ -11,7 +11,7 @@ pub fn js_exec<'s>(
 }
 
 pub fn v8_init() {
-  let platform = v8::new_default_platform(0, false).make_shared();
+  let platform = v8::new_unprotected_default_platform(0, false).make_shared();
   v8::V8::initialize_platform(platform);
   v8::V8::initialize();
 }


Test patch:
diff --git a/.github/workflows/ci-test-miri/action.yml b/.github/workflows/ci-test-miri/action.yml
new file mode 100644
index 0000000..5c56dbe
--- /dev/null
+++ b/.github/workflows/ci-test-miri/action.yml
@@ -0,0 +1,13 @@
+name: Test workflow
+runs:
+  using: "composite"
+  steps:
+    - name: Install Rust (nightly)
+      uses: dtolnay/rust-toolchain@nightly
+    - name: Cargo test (miri)
+      shell: bash
+      run: |
+        # Install and run miri tests
+        rustup component add --toolchain nightly miri
+        # This somehow prints errors in CI that don't show up locally
+        RUSTFLAGS=-Awarnings cargo +nightly miri test -p deno_core
diff --git a/.github/workflows/ci-test-ops/action.yml b/.github/workflows/ci-test-ops/action.yml
new file mode 100644
index 0000000..9c5e17b
--- /dev/null
+++ b/.github/workflows/ci-test-ops/action.yml
@@ -0,0 +1,8 @@
+name: Test workflow
+runs:
+  using: "composite"
+  steps:
+    - name: Cargo test (ops)
+      shell: bash
+      run: |-
+        cargo test --workspace --release -p deno_ops_compile_test_runner
diff --git a/.github/workflows/ci-test/action.yml b/.github/workflows/ci-test/action.yml
new file mode 100644
index 0000000..a8d17c5
--- /dev/null
+++ b/.github/workflows/ci-test/action.yml
@@ -0,0 +1,17 @@
+name: Test workflow
+runs:
+  using: "composite"
+  steps:
+    - name: Cargo test
+      shell: bash
+      run: |-
+        cargo test --workspace --release --all-features --bins --tests --examples --exclude deno_ops_compile_test_runner
+        cargo test --doc
+
+    - name: Run examples
+      shell: bash
+      # Only regression tests here for now.
+      # Regression test for https://github.com/denoland/deno/pull/19615.
+      run: |-
+        cargo run -p deno_core --example op2
+        cargo run -p deno_core --example op2 --features include_js_files_for_snapshotting
diff --git a/serde_v8/tests/de.rs b/serde_v8/tests/de.rs
index 122f96f..f8f925a 100644
--- a/serde_v8/tests/de.rs
+++ b/serde_v8/tests/de.rs
@@ -2,13 +2,13 @@
 use serde::Deserialize;
 use serde::Deserializer;
 
-use serde_v8::utils::js_exec;
-use serde_v8::utils::v8_do;
 use serde_v8::BigInt;
 use serde_v8::ByteString;
 use serde_v8::Error;
 use serde_v8::JsBuffer;
 use serde_v8::U16String;
+use serde_v8_utilities::js_exec;
+use serde_v8_utilities::v8_do;
 
 #[derive(Debug, Deserialize, PartialEq)]
 struct MathOp {
diff --git a/serde_v8/tests/magic.rs b/serde_v8/tests/magic.rs
index e3ed1d3..bec937f 100644
--- a/serde_v8/tests/magic.rs
+++ b/serde_v8/tests/magic.rs
@@ -2,9 +2,9 @@
 use serde::Deserialize;
 use serde::Serialize;
 
-use serde_v8::utils::js_exec;
-use serde_v8::utils::v8_do;
 use serde_v8::Result;
+use serde_v8_utilities::js_exec;
+use serde_v8_utilities::v8_do;
 
 #[derive(Deserialize)]
 struct MagicOp<'s> {
diff --git a/serde_v8/tests/ser.rs b/serde_v8/tests/ser.rs
index b61a758..b183166 100644
--- a/serde_v8/tests/ser.rs
+++ b/serde_v8/tests/ser.rs
@@ -1,9 +1,9 @@
 // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
 use serde::Serialize;
 use serde_json::json;
-use serde_v8::utils::js_exec;
-use serde_v8::utils::v8_do;
 use serde_v8::BigInt;
+use serde_v8_utilities::js_exec;
+use serde_v8_utilities::v8_do;
 
 #[derive(Debug, Serialize, PartialEq)]
 struct MathOp {
diff --git a/testing/Cargo.toml b/testing/Cargo.toml
index 6181e2f..62b5a99 100644
--- a/testing/Cargo.toml
+++ b/testing/Cargo.toml
@@ -15,6 +15,7 @@ path = "./lib.rs"
 
 [dev-dependencies]
 deno_core.workspace = true
+deno_core.features = ["unsafe_use_unprotected_platform"]
 pretty_assertions.workspace = true
 prettyplease.workspace = true
 testing_macros.workspace = true
diff --git a/testing/checkin/runner/mod.rs b/testing/checkin/runner/mod.rs
index aafb13b..dc1eab4 100644
--- a/testing/checkin/runner/mod.rs
+++ b/testing/checkin/runner/mod.rs
@@ -21,6 +21,7 @@ use self::testing::TestFunctions;
 mod ops;
 mod ops_async;
 mod ops_buffer;
+mod ops_io;
 mod testing;
 mod ts_module_loader;
 
@@ -34,6 +35,7 @@ deno_core::extension!(
     ops::op_stats_diff,
     ops::op_stats_dump,
     ops::op_stats_delete,
+    ops_io::op_pipe_create,
     ops_async::op_async_yield,
     ops_async::op_async_barrier_create,
     ops_async::op_async_barrier_await,
diff --git a/testing/checkin/runner/ops_io.rs b/testing/checkin/runner/ops_io.rs
new file mode 100644
index 0000000..33100b5
--- /dev/null
+++ b/testing/checkin/runner/ops_io.rs
@@ -0,0 +1,67 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+use deno_core::op2;
+use deno_core::AsyncRefCell;
+use deno_core::BufView;
+use deno_core::OpState;
+use deno_core::RcRef;
+use deno_core::Resource;
+use deno_core::ResourceId;
+use deno_core::WriteOutcome;
+use futures::FutureExt;
+use tokio::io::AsyncReadExt;
+use tokio::io::AsyncWriteExt;
+use tokio::io::DuplexStream;
+use tokio::io::ReadHalf;
+use tokio::io::WriteHalf;
+
+struct PipeResource {
+  tx: AsyncRefCell<WriteHalf<DuplexStream>>,
+  rx: AsyncRefCell<ReadHalf<DuplexStream>>,
+}
+
+impl Resource for PipeResource {
+  fn read_byob(
+    self: std::rc::Rc<Self>,
+    mut buf: deno_core::BufMutView,
+  ) -> deno_core::AsyncResult<(usize, deno_core::BufMutView)> {
+    async {
+      let mut lock = RcRef::map(self, |this| &this.rx).borrow_mut().await;
+      // Note that we're holding a slice across an await point, so this code is very much not safe
+      let res = lock.read(&mut buf).await?;
+      Ok((res, buf))
+    }
+    .boxed_local()
+  }
+
+  fn write(
+    self: std::rc::Rc<Self>,
+    buf: BufView,
+  ) -> deno_core::AsyncResult<deno_core::WriteOutcome> {
+    async {
+      let mut lock = RcRef::map(self, |this| &this.tx).borrow_mut().await;
+      let nwritten = lock.write(&buf).await?;
+      Ok(WriteOutcome::Partial {
+        nwritten,
+        view: buf,
+      })
+    }
+    .boxed_local()
+  }
+}
+
+#[op2]
+#[serde]
+pub fn op_pipe_create(op_state: &mut OpState) -> (ResourceId, ResourceId) {
+  let (s1, s2) = tokio::io::duplex(1024);
+  let (rx1, tx1) = tokio::io::split(s1);
+  let (rx2, tx2) = tokio::io::split(s2);
+  let rid1 = op_state.resource_table.add(PipeResource {
+    rx: AsyncRefCell::new(rx1),
+    tx: AsyncRefCell::new(tx1),
+  });
+  let rid2 = op_state.resource_table.add(PipeResource {
+    rx: AsyncRefCell::new(rx2),
+    tx: AsyncRefCell::new(tx2),
+  });
+  (rid1, rid2)
+}
diff --git a/testing/lib.rs b/testing/lib.rs
index 69c0873..9a149a0 100644
--- a/testing/lib.rs
+++ b/testing/lib.rs
@@ -37,6 +37,7 @@ unit_test!(
   microtask_test,
   ops_async_test,
   ops_buffer_test,
+  resource_test,
   serialize_deserialize_test,
   stats_test,
   tc39_test,
diff --git a/testing/unit/resource_test.ts b/testing/unit/resource_test.ts
new file mode 100644
index 0000000..37f69c0
--- /dev/null
+++ b/testing/unit/resource_test.ts
@@ -0,0 +1,22 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+import { assertArrayEquals, assertEquals, test } from "checkin:testing";
+
+const { op_pipe_create } = Deno.core.ensureFastOps();
+
+test(async function testPipe() {
+  const [p1, p2] = op_pipe_create();
+  assertEquals(3, await Deno.core.write(p1, new Uint8Array([1, 2, 3])));
+  const buf = new Uint8Array(10);
+  assertEquals(3, await Deno.core.read(p2, buf));
+  assertArrayEquals(buf.subarray(0, 3), [1, 2, 3]);
+});
+
+test(async function testPipeSmallRead() {
+  const [p1, p2] = op_pipe_create();
+  assertEquals(6, await Deno.core.write(p1, new Uint8Array([1, 2, 3, 4, 5, 6])));
+  const buf = new Uint8Array(1);
+  for (let i = 1; i <= 6; i++) {
+    assertEquals(1, await Deno.core.read(p2, buf));
+    assertArrayEquals(buf.subarray(0), [i]);
+  }
+});