Skip to content

src: use per-type tags for cppgc wrappers - #67131

Open
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:src-cppgc-wrapper-tags
Open

mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:src-cppgc-wrapper-tags

Conversation

@mcollina

@mcollina mcollina commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Building with --experimental-enable-pointer-compression --experimental-pointer-compression-shared-cage currently fails to compile. The shared cage turns on the V8 sandbox, and V8 only defines CppHeapPointerTag::kDefaultTag when the sandbox is off:

../src/cppgc_helpers-inl.h:18:43: error: 'kDefaultTag' is not a member of 'v8::CppHeapPointerTag'

V8 asks embedders that use the sandbox to give each type its own tag. Swapping in a different single tag would compile, but the tag would protect nothing: CppgcMixin::Unwrap() reads the raw pointer from the internal field and never checks it.

This PR:

  • adds node::CppgcWrapperTag in src/node_v8_embedder.h, with one tag per cppgc wrapper type (ContextifyContext, ContextifyScript) and one for the deprecated SetCppgcReference()
  • adds a SET_CPPGC_TAG() macro. CppgcMixin::Wrap<T>() uses T::kCppHeapPointerTag, so a wrapper without a tag fails to compile
  • unwraps through v8::Object::Unwrap<T::kCppHeapPointerTag, T>(). With the sandbox on, unwrapping an object as the wrong type now returns nullptr. The isolate comes from v8::Isolate::GetCurrent(), so ASSIGN_OR_RETURN_UNWRAP_CPPGC and its callers stay the same
  • updates the tests that used kDefaultTag directly

It also fixes three addon tests that crashed with the sandbox, because they created ArrayBuffers over memory outside it:

  • cppgc-object now lets V8 allocate its counters
  • null-buffer-neuter only checks for a nullptr data pointer without the sandbox, because empty buffers point into the sandbox
  • worker-buffer-callback/test-free-called expects the main thread buffer to be freed too, because the sandbox copies the data and frees the original right away

Tested on main with a pointer-compression + shared-cage + sandbox build (default, addons, embedding, cctest) and with a default build (default, addons, embedding, cctest). All pass.

——-

AI generated, reviewed by me.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. v8 engine Issues and PRs related to the V8 dependency. vm Issues and PRs related to the vm subsystem. labels Oct 30, 2026
@mcollina
mcollina marked this pull request as ready for review October 30, 2026 07:55
@mcollina
mcollina requested a review from legendecas October 30, 2026 09:01
V8 only defines CppHeapPointerTag::kDefaultTag when the sandbox is
disabled, so building with
--experimental-pointer-compression-shared-cage (which enables the
sandbox) failed to compile.

Give each cppgc-managed wrapper type its own tag, declared with the new
SET_CPPGC_TAG() macro, and unwrap through v8::Object::Unwrap() with
that tag instead of reading the raw pointer from the internal field.
With the sandbox enabled, unwrapping an object as the wrong type now
yields nullptr. The deprecated SetCppgcReference() gets its own tag.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
With the V8 sandbox, ArrayBuffer backing stores must live inside the
sandbox. Let V8 allocate the counters in the cppgc-object addon instead
of wrapping a static array, skip the nullptr data check for empty
buffers in null-buffer-neuter, and account for the eagerly freed main
thread buffer in worker-buffer-callback.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollina force-pushed the src-cppgc-wrapper-tags branch from e3c855d to 76f24fb Compare October 30, 2026 09:04
@codecov

codecov Bot commented Oct 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.68%. Comparing base (98dbf60) to head (76f24fb).
⚠️ Report is 49 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67131      +/-   ##
==========================================
+ Coverage   90.65%   90.68%   +0.03%     
==========================================
  Files         797      797              
  Lines      278268   278805     +537     
  Branches    53517    53618     +101     
==========================================
+ Hits       252261   252838     +577     
+ Misses      16386    16334      -52     
- Partials     9621     9633      +12     
Files with missing lines Coverage Δ
src/cppgc_helpers-inl.h 82.35% <100.00%> (-0.51%) ⬇️
src/cppgc_helpers.h 60.00% <ø> (ø)
src/env.cc 84.43% <100.00%> (+0.13%) ⬆️
src/node_contextify.h 72.22% <ø> (ø)

... and 51 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. v8 engine Issues and PRs related to the V8 dependency. vm Issues and PRs related to the vm subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants