[JsRuntimeHost] napi_unwrap does not reject objects that were never wrapped (V8 port faults, QuickJS port confuses types)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
- #229 @bghgary による — マージされずにクローズ
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 34/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 停滞
- 技術スタック
- cpp, javascript
調査の方向性
Read Core/Node-API/Source/js_native_api_v8.cc at Unwrap and Core/Node-API/Source/js_native_api_quickjs.cc at napi_unwrap. Compare how each port handles objects that were never wrapped, then check the existing tests for unwrap behavior. Done means invalid objects return napi_invalid_arg without a crash or returning another object's native pointer in both ports.
索引モデルが issue の本文から書いたものです。
説明
Summary
napi_unwrap must fail when it is handed an object that was never wrapped. The V8 and QuickJS ports both violate that, so Napi::ObjectWrap<T>::Unwrap cannot safely be called on an object whose type has not already been established. On V8 it is an outright memory-safety hole.
This affects every Unwrap call site in a consumer, not one polyfill. I hit it in BabylonNative's Canvas polyfill (BabylonJS/BabylonNative#1844), where ctx.fill(Object.create(Path2D.prototype)) was an access violation.
V8 port — dereferences internal field 0 unchecked
Core/Node-API/Source/js_native_api_v8.cc, inline napi_status Unwrap(...) (~line 345). A [BABYLON-NATIVE-ADDITION] marked "Increase perf by using internal field instead of private property" replaced the private-property lookup, including its validity check:
// upstream
auto val = obj->GetPrivate(context, NAPI_PRIVATE_KEY(context, wrapper)).ToLocalChecked();
RETURN_STATUS_IF_FALSE(env, val->IsExternal(), napi_invalid_arg);
with a bare obj->GetAlignedPointerFromInternalField(0), whose result is then dereferenced (reference->Data()).
For any object that is not a wrapped instance, internal field 0 is not a Reference*. The read returns garbage and the dereference faults. Reproduction:
const impostor = Object.create(Path2D.prototype);
ctx.fill(impostor); // 0xC0000005
I confirmed this in a local V8 build: deterministic access violation, and it disappears when the unwrap is replaced with a checked lookup.
QuickJS port — walks the prototype chain
Core/Node-API/Source/js_native_api_quickjs.cc, napi_unwrap (~line 2495). After the fast path on js_wrap_class_id there is a "Fallback: search the prototype chain for a legacy wrapper object".
That returns some other object's native pointer — a type confusion rather than a crash. An object created with Object.create(RealType.prototype) unwraps to whatever instance is reachable on the chain. It does at least return napi_invalid_arg when nothing is found.
JSI port
No C API, and ObjectWrap<T>::Unwrap returns nullptr for a non-wrapped object (napi-inl.h:2268) rather than throwing. Safe, but inconsistent with the other two.
Why this is hard to work around downstream
While fixing the Canvas case I found no portable way to do a type check:
napi_type_tag_object/napi_check_object_type_tagexist only injs_native_api_v8.cc.Napi::Object::DefineProperty/PropertyDescriptorare absent from the JSI port, so a non-enumerable brand cannot be installed.GetInstanceData/SetInstanceData/AddCleanupHookare absent from the JSI port, so there is nowhere to keep per-EnvC++ state.Napi::ObjectWrap<T>::Value()throws on the QuickJS port, which rules out an identity check against a candidate instance.
I ended up branding each instance with a Napi::External<T> and validating the pointer against a registry of live addresses. That works, but every consumer having to invent this is a strong argument for fixing the ports.
Suggested fix
Restore the validity check in the V8 port (keep the internal-field fast path, but verify the field actually holds the wrapper before dereferencing), and drop the prototype-chain fallback in the QuickJS port so a non-wrapped object returns napi_invalid_arg.
Related: BabylonJS/JsRuntimeHost#225 (the napi_throw family reports failure on success), found while chasing the same PR.
- 主要言語
- C++
- スター
- 931
- フォーク
- 169
- 平均マージ
- 2日 17時間
- マージ済み PR(30日)
- 66
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
BabylonJS/BabylonNative のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
BabylonJS/BabylonNative#2138 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
BabylonJS/BabylonNative#2042 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
BabylonJS/BabylonNative#2052 ·
メンテナーはふだん 1 日以内に返信
-
testing
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
BabylonJS/BabylonNative#2136 ·
メンテナーはふだん 1 日以内に返信
-
Support useNDCHalfZRange (half-range NDC depth) so Babylon.js half-range depth visual tests can run対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
BabylonJS/BabylonNative#2134 ·
メンテナーはふだん 1 日以内に返信
BabylonJS/BabylonNative の issue をすべて見る
似ている issue
-
Make the HTTP server's keep-alive and headers timeouts configurable対応中かも @mattgodbolt が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
compiler-explorer/compiler-explorer#9902 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
Nixtla/statsforecast#1371 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
[Bug]: pip install -U cudaq-qec fails with a conflict that names cudaq-qec itself対応中かも @fedonman が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
task
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
SparseLinearAlgebra/spla#316 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信