ArgConverter::SetValue: invalid pointer for a JS number or boolean in an object slot, and a leaked adapter for a plain object from an unowned method
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 45/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- cpp, ios, javascript, objective-c
- 領域
- mobile-dev
調査の方向性
Start at ArgConverter::SetValue and its caller, then read ArgConverter::RetainOwnedResult and the boxing path in Interop::SetFFIParams; compare with the owned-return handling added in #569. Reproduce the table cases with a native caller and add the four tests from the issue, running them under AddressSanitizer. Done when object-typed slots always hold a valid NSNumber, NSString or nil and the WeakRef test clears after the autorelease pool drains.
索引モデルが issue の本文から書いたものです。
説明
Summary
ArgConverter::SetValue writes the result of a JS-implemented method, getter or block into the native return slot. For some combinations of JS value and native return type it writes nothing, or writes something that is not a valid value of that type, and native code then reads it.
| JS result | Native return type | What is written |
|---|---|---|
| number | object (id, a class, instancetype) |
nothing: the slot keeps whatever it held |
true |
object | the integer 1, which native code treats as an object pointer |
false |
object | 0, which reads as nil |
| string | instancetype |
nothing |
A string returned for an id or class-typed result is converted to an NSString, and null/undefined zero the slot; those are fine.
const X = NSObject.extend({
description() { return 42; }, // NSString* expected: the slot is not written
});
The caller then messages a garbage pointer, or 0x1. This is a mistake in the JS implementation, but the runtime should fail predictably and not hand native code an arbitrary pointer.
A plain JS object from a method that doesn't own its return leaks its adapter
A related gap in the same function. For a plain JS object (or an array, function or Date) returned where an object is expected, SetValue creates a DictionaryAdapter with alloc/init and writes it at +1. A CFAutorelease(adapter) next to it is commented out.
That +1 is right when the method owns its return (the copy, mutableCopy and new families, initializers), and since #569 those are balanced. For any other method the native caller doesn't own the result and never releases it, so the adapter is never freed and its Persistent keeps the JS object alive:
const X = NSObject.extend({ makeThing() { return {}; } }, {
exposedMethods: { makeThing: { returns: NSObject } },
});
// a native caller of -makeThing: the adapter and the object it wraps are never freed
Seen with a WeakRef on the returned object, which never clears after a native performSelector: call.
The fix is to hand the adapter over at +0 (autoreleased) when the method doesn't own its return. SetValue already reports whether it wrote an object the caller doesn't own; it would need to distinguish "wrote a +1 object" from "wrote nothing" so the caller can autorelease in the unowned case.
Suggested fix
In ArgConverter::SetValue, for object return types:
- box a JS number or boolean as an
NSNumber, as argument marshalling already does for a number passed to an object parameter (Interop::SetFFIParams), or write nil if boxing is not wanted there; - convert a string for
instancetypeas forid, or write nil; - in every other case that has no conversion, zero the slot.
The function has a TODO about reusing Interop::SetFFIParams; sharing that logic would fix all of these at once.
Whatever is boxed is returned at +0, so it also has to go through the owned-result retain added in #569 (ArgConverter::RetainOwnedResult), which today only recognises wrapped objects and strings.
Tests to add
- A JS method declared to return an object that returns a number,
true, andfalse: a native caller gets anNSNumber(or nil), never an invalid pointer. Run under AddressSanitizer. - A JS initializer (
instancetype) that returns a string: the native caller gets nil or anNSString, not an unwritten slot. - The same through an owned-return method (
copyWithZone:), checking the count of the boxed result. - A JS method that doesn't own its return, returning a fresh plain object to a native caller: a
WeakRefon the object clears once the caller's autorelease pool drains.
Found while reviewing #569; not reproduced in a spec yet, because in Debug builds some of these paths assert first.
- 主要言語
- JavaScript
- スター
- 154
- フォーク
- 46
- 平均マージ
- 4日 2時間
- マージ済み PR(30日)
- 30
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
NativeScript/ios のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
NativeScript/ios#577 ·
メンテナーはふだん 1 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 12/100
NativeScript/ios#571 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
NativeScript/ios#528 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
interop.FunctionReference function passed as a block, then as a function pointer, asserts対応中かも @edusperoni が 39 日前に担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 22/100
NativeScript/ios#503 ·
メンテナーはふだん 1 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 38/100
NativeScript/ios#459 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
NativeScript/ios の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
sfbrigade/pocket-gardens#60 ·
メンテナーはふだん 5 日以内に返信
-
The print preview leaves writing space short by the margins of hidden headers, footers and solutionsオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
PreTeXtBook/pretext#3329 ·
メンテナーはふだん 1 日以内に返信
-
could-have
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
-
01 type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 2 日以内に返信
-
accepted enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信