forked from Blendman974/faceclaw
Passing a JS ArrayBuffer to a Java ByteBuffer parameter leaks it in @nativescript/android 9.0.4 through 9.1.1: JsArgConverter wraps the backing store with NewDirectByteBuffer and takes a JNI global ref that it never deletes. The ref keeps the ByteBuffer reachable, so makeInstanceWeakAndCheckIfAlive always answers "alive", the JS finalizer re-arms, and V8 never frees the ArrayBuffer. submitSurfaceFrame passed a fresh copy of each frame this way, so every frame leaked a full surface (576x260, 640x452 or 640x480 bytes) plus one global ref, including frames that Java then discards as unchanged. The draw-list buffer and buzzer payloads leaked the same way. Each payload now goes through a reused Java direct ByteBuffer (app/native/java-direct-buffer.ts): JS writes into it through an ArrayBuffer.from view, and the Java object itself is passed, which takes NativeScript's ordinary object path. Every consumer (SurfaceCompositor applyAndComposite, playBuzzerSequence) copies before returning, so one buffer per JS context is safe. Covered: the communicator bridge (frames, draw lists, buzzer, which also carries EvenHub tones), the preview-only display, and all eight worker apps. Workers submit through the Android getActiveDisplay() (frames) and worker-buzzer (tones), so the reused buffers live there rather than in each worker; the worker sources are shared with iOS, whose active display takes the ArrayBuffer itself. The smaller one-off crossings (glyph and image atlas registration, the gray preview helper, firmware and file writes) are left as they are. tests/java-direct-buffer.test.cjs covers the helper against a fake ByteBuffer, and the Android worker-buzzer test stubs it. (Rebased onto main by James Babcock: worker changes moved into active-display.ts and worker-buzzer.ts.) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
45 lines
2.4 KiB
JavaScript
45 lines
2.4 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { loader } = require('./helpers/load-typescript.cjs');
|
|
|
|
const payload = new Uint8Array([5, 4, 1, 0xb8, 0x0b, 25, 80, 0]);
|
|
|
|
test('iOS game worker forwards an immutable byte payload through its host and ignores closed windows', async () => {
|
|
const played = [], worker = { postMessage() {} };
|
|
const load = loader({ global: { isIOS: true, postMessage: data => worker.onmessage({ data }) } }, {
|
|
'../../graphics/image': {}, './chrome-layer': { windowIcon() {} },
|
|
'../../assistant/tool-registry': { toolRegistry: { removeAppTools() {} } },
|
|
'./geometry': { appViewportSize: () => ({ width: 576, height: 260 }) },
|
|
'../../native/frame-timings': {}, './worker-state': {},
|
|
'./shell': { shell: { registerWindow() {} } },
|
|
});
|
|
const { WorkerAppHost } = load('app/ui/shell/worker-window.ts');
|
|
const bridge = load('app/native/worker-buzzer.ts');
|
|
const host = new WorkerAppHost({ appId: 'blocks', worker, configureSurface: async () => {},
|
|
requestShellRender() {}, removeSurface() {}, playBuzzerSequence: bytes => played.push(bytes) });
|
|
bridge.playWorkerBuzzerSequence(payload);
|
|
assert.equal(played.length, 0);
|
|
const window = host.openWindow({ windowId: 'blocks', title: 'Blocks' });
|
|
const backing = new Uint8Array(payload.length + 2); backing.set(payload, 1);
|
|
bridge.playWorkerBuzzerSequence(backing.subarray(1, -1)); backing.fill(0);
|
|
assert.deepEqual(played, [payload]);
|
|
window.close(); bridge.playWorkerBuzzerSequence(payload);
|
|
assert.equal(played.length, 1);
|
|
});
|
|
|
|
test('Android game bridge retains direct native playback and skips a missing communicator', () => {
|
|
let active = null;
|
|
const played = [];
|
|
// Stands in for the reused Java direct buffer: the payload reaches Java as a
|
|
// copy, never as the caller's own ArrayBuffer.
|
|
const directBuffer = { JavaDirectBuffer: class { load(bytes) { return { bytes: bytes.slice() }; } } };
|
|
const bridge = loader({ global: { isIOS: false },
|
|
com: { faceclaw: { app: { FaceclawBleCommunicator: { getActive: () => active } } } } },
|
|
{ './java-direct-buffer': directBuffer })('app/native/worker-buzzer.ts');
|
|
bridge.playWorkerBuzzerSequence(payload);
|
|
active = { playBuzzerSequence: buffer => played.push(buffer.bytes) };
|
|
const backing = new Uint8Array(payload.length + 2); backing.set(payload, 1);
|
|
bridge.playWorkerBuzzerSequence(backing.subarray(1, -1)); backing.fill(0);
|
|
assert.deepEqual(played, [payload]);
|
|
});
|