mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
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>
112 lines
3.7 KiB
JavaScript
112 lines
3.7 KiB
JavaScript
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
|
|
const { JavaDirectBuffer } = require("../.test-build/app/native/java-direct-buffer.js");
|
|
|
|
/**
|
|
* A stand-in for java.nio.ByteBuffer.allocateDirect: fixed capacity, a
|
|
* position and a limit, and the relative bulk get() SurfaceCompositor uses.
|
|
* viewOf() plays ArrayBuffer.from: a view over the same memory, not a copy.
|
|
*/
|
|
function fakeBackend() {
|
|
const allocated = [];
|
|
return {
|
|
allocated,
|
|
allocateDirect(capacity) {
|
|
const memory = new Uint8Array(capacity);
|
|
const buffer = {
|
|
memory,
|
|
position: 0,
|
|
limitValue: capacity,
|
|
clear() {
|
|
this.position = 0;
|
|
this.limitValue = capacity;
|
|
return this;
|
|
},
|
|
limit(newLimit) {
|
|
if (newLimit > capacity) throw new Error("limit past capacity");
|
|
this.limitValue = newLimit;
|
|
if (this.position > newLimit) this.position = newLimit;
|
|
return this;
|
|
},
|
|
remaining() {
|
|
return this.limitValue - this.position;
|
|
},
|
|
get(dst, offset, length) {
|
|
if (length > this.remaining()) throw new Error("BufferUnderflowException");
|
|
dst.set(memory.subarray(this.position, this.position + length), offset);
|
|
this.position += length;
|
|
return this;
|
|
},
|
|
};
|
|
allocated.push(buffer);
|
|
return buffer;
|
|
},
|
|
viewOf(buffer) {
|
|
return buffer.memory;
|
|
},
|
|
};
|
|
}
|
|
|
|
/** Consume the way SurfaceCompositor.applyAndComposite does: exact remaining(), then drain. */
|
|
function consume(buffer, expectedBytes) {
|
|
assert.equal(buffer.remaining(), expectedBytes);
|
|
const out = new Uint8Array(expectedBytes);
|
|
buffer.get(out, 0, expectedBytes);
|
|
return out;
|
|
}
|
|
|
|
function frame(length, seed) {
|
|
const bytes = new Uint8Array(length);
|
|
for (let i = 0; i < length; i++) bytes[i] = (i * 31 + seed) & 0xff;
|
|
return bytes;
|
|
}
|
|
|
|
test("a loaded buffer exposes exactly the payload bytes to a Java-style reader", () => {
|
|
const backend = fakeBackend();
|
|
const pool = new JavaDirectBuffer(640 * 480, backend);
|
|
const shell = frame(640 * 480, 1);
|
|
assert.deepEqual(consume(pool.load(shell), shell.length), shell);
|
|
});
|
|
|
|
test("successive frames reuse one Java buffer, including smaller ones", () => {
|
|
const backend = fakeBackend();
|
|
const pool = new JavaDirectBuffer(640 * 480, backend);
|
|
for (let i = 0; i < 50; i++) {
|
|
const size = i % 2 === 0 ? 640 * 480 : 640 * 452;
|
|
const bytes = frame(size, i);
|
|
assert.deepEqual(consume(pool.load(bytes), size), bytes);
|
|
}
|
|
assert.equal(pool.allocations, 1);
|
|
assert.equal(backend.allocated.length, 1);
|
|
});
|
|
|
|
test("a payload larger than the buffer allocates once more, then reuses", () => {
|
|
const backend = fakeBackend();
|
|
const pool = new JavaDirectBuffer(0, backend);
|
|
consume(pool.load(frame(10, 0)), 10);
|
|
consume(pool.load(frame(9000, 0)), 9000);
|
|
consume(pool.load(frame(8000, 0)), 8000);
|
|
consume(pool.load(frame(9000, 0)), 9000);
|
|
assert.equal(pool.allocations, 2);
|
|
});
|
|
|
|
test("loadOptional passes null through and loads a draw list", () => {
|
|
const backend = fakeBackend();
|
|
const pool = new JavaDirectBuffer(0, backend);
|
|
assert.equal(pool.loadOptional(null), null);
|
|
assert.equal(pool.loadOptional(undefined), null);
|
|
assert.equal(pool.loadOptional(new ArrayBuffer(0)), null);
|
|
assert.equal(backend.allocated.length, 0);
|
|
const draws = frame(36, 7);
|
|
assert.deepEqual(consume(pool.loadOptional(draws.buffer), 36), draws);
|
|
});
|
|
|
|
test("a view on a larger buffer contributes only its own bytes", () => {
|
|
const backend = fakeBackend();
|
|
const pool = new JavaDirectBuffer(0, backend);
|
|
const whole = frame(100, 3);
|
|
const view = whole.subarray(10, 30);
|
|
assert.deepEqual(consume(pool.load(view), 20), whole.slice(10, 30));
|
|
});
|