From 1812818ae2e0515e4dde64667dc52075f1a6d1c5 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Fri, 25 Sep 2026 22:28:45 -0700 Subject: [PATCH] Stop leaking the remaining JS ArrayBuffers passed to Java The same NativeScript conversion leak as the frame path (see java-direct-buffer.ts) applied to every other ArrayBuffer handed to a Java ByteBuffer parameter: - Shell scenes (submitShellScene on the bridge and the preview display), submitted on every shell render, which the frame-path fix predates. These now go through a reused JavaDirectBuffer like frames do. - Glyph, firmware-glyph and image atlas registration, Mapbox image decode, the preview placeholder bitmap, firmware hash/write, binary file writes and the EvenHub privacy-policy download, plus the Kotlin protocol smoke test. These are rare or large, so they cross through a new copyToJavaByteBuffer: a fresh Java direct buffer per call that is freed normally, rather than one pinned for the life of the isolate. Co-Authored-By: Claude Opus 5.5 (1M context) --- app/apps/evenhub/privacy-policy.ts | 4 ++-- app/native/faceclaw-communicator.ts | 6 +++++- app/native/file-access.ts | 8 ++++---- app/native/firmware-files.ts | 9 +++++++-- app/native/gray-image-preview.ts | 11 ++++++----- app/native/image-bytes.ts | 4 +++- app/native/java-direct-buffer.ts | 17 +++++++++++++++++ app/native/kotlin-protocol-smoke.ts | 8 +++++--- app/native/preview-display.ts | 3 ++- app/native/texture-atlas.ts | 12 ++++++++---- tests/compass-texture-cache.test.cjs | 2 +- tests/java-direct-buffer.test.cjs | 13 ++++++++++++- 12 files changed, 72 insertions(+), 25 deletions(-) diff --git a/app/apps/evenhub/privacy-policy.ts b/app/apps/evenhub/privacy-policy.ts index 53fc2681..e5e0c0be 100644 --- a/app/apps/evenhub/privacy-policy.ts +++ b/app/apps/evenhub/privacy-policy.ts @@ -1,5 +1,6 @@ import { Application } from "@nativescript/core"; import { fetchWithUserAgent } from "../../util/http"; +import { copyToJavaByteBuffer } from "../../native/java-direct-buffer"; declare const android: any; declare const androidx: any; @@ -93,8 +94,7 @@ async function downloadAndOpenPdf(url: string, appName: string, activity: any): const file = new java.io.File(directory, `policy-${hash.toString(16)}.pdf`); const stream = new java.io.FileOutputStream(file); try { - const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength); - stream.getChannel().write(buffer); + stream.getChannel().write(copyToJavaByteBuffer(bytes)); } finally { stream.close(); } diff --git a/app/native/faceclaw-communicator.ts b/app/native/faceclaw-communicator.ts index 878243fc..37b5f479 100644 --- a/app/native/faceclaw-communicator.ts +++ b/app/native/faceclaw-communicator.ts @@ -138,6 +138,7 @@ export class FaceclawCommunicatorBridge { // immediately before the synchronous Java call that consumes them. private readonly framePixelsBuffer = new JavaDirectBuffer(640 * 480); private readonly frameDrawsBuffer = new JavaDirectBuffer(); + private readonly shellSceneBuffer = new JavaDirectBuffer(); private readonly buzzerBuffer = new JavaDirectBuffer(); private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>(); // Recent frame-finished outcomes from the Java side, so waitForFrameFinished @@ -545,7 +546,10 @@ export class FaceclawCommunicatorBridge { */ async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise { const snapshot = new Uint8Array(bytes); - await this.enqueueJavaCall(() => this.communicator.submitShellScene(snapshot.buffer, paintMs, frameId), true); + await this.enqueueJavaCall( + () => this.communicator.submitShellScene(this.shellSceneBuffer.load(snapshot), paintMs, frameId), + true, + ); } async setUnderlayDim(belowZOrder: number, factor: number): Promise { diff --git a/app/native/file-access.ts b/app/native/file-access.ts index b79f3d5f..5cf818e5 100644 --- a/app/native/file-access.ts +++ b/app/native/file-access.ts @@ -5,6 +5,7 @@ * reasonable for a sideloaded personal tool). */ import { Utils } from "@nativescript/core"; +import { copyToJavaByteBuffer } from "./java-direct-buffer"; declare const android: any; declare const java: any; @@ -150,10 +151,9 @@ export function writeBinaryFile(path: string, bytes: Uint8Array): boolean { if (parent) parent.mkdirs(); const stream = new java.io.FileOutputStream(file); try { - // An ArrayBuffer marshals to java.nio.ByteBuffer (the same conversion - // submitSurfaceFrame relies on), so write through the channel. - const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength); - stream.getChannel().write(buffer); + // Write through the channel from a Java direct buffer: a bulk copy, and + // passing the ArrayBuffer itself would leak it (java-direct-buffer.ts). + stream.getChannel().write(copyToJavaByteBuffer(bytes)); } finally { stream.close(); } diff --git a/app/native/firmware-files.ts b/app/native/firmware-files.ts index 59236354..5517af25 100644 --- a/app/native/firmware-files.ts +++ b/app/native/firmware-files.ts @@ -1,3 +1,8 @@ +import { copyToJavaByteBuffer } from './java-direct-buffer' + declare const com: any -export function firmwareSha256(buffer: ArrayBuffer): string { return String(com.faceclaw.app.FaceclawFirmwareUtil.sha256Hex(buffer)) } -export function writeFirmwareFile(path: string, buffer: ArrayBuffer): void { com.faceclaw.app.FaceclawFirmwareUtil.writeFile(path, buffer) } + +// The image crosses as a fresh Java buffer: passing a JS ArrayBuffer to Java +// leaks it (java-direct-buffer.ts). +export function firmwareSha256(buffer: ArrayBuffer): string { return String(com.faceclaw.app.FaceclawFirmwareUtil.sha256Hex(copyToJavaByteBuffer(new Uint8Array(buffer)))) } +export function writeFirmwareFile(path: string, buffer: ArrayBuffer): void { com.faceclaw.app.FaceclawFirmwareUtil.writeFile(path, copyToJavaByteBuffer(new Uint8Array(buffer))) } diff --git a/app/native/gray-image-preview.ts b/app/native/gray-image-preview.ts index 690c1e4b..fc8b1656 100644 --- a/app/native/gray-image-preview.ts +++ b/app/native/gray-image-preview.ts @@ -1,16 +1,17 @@ import { ImageSource } from "@nativescript/core"; import { type GrayImage } from "../graphics/image"; +import { copyToJavaByteBuffer } from "./java-direct-buffer"; declare const com: any; const PREVIEW_BRIGHTEN_GAMMA = 0.7; /** - * Build the phone-UI preview for a frame. The pixel buffer is passed to Java - * as an ArrayBuffer (marshalled to a ByteBuffer) and expanded to ARGB there; - * doing the per-pixel work in JS or copying element-by-element across the - * bridge costs ~150ms per frame. + * Build the phone-UI preview for a frame. The pixels are copied into a Java + * direct ByteBuffer and expanded to ARGB there; doing the per-pixel work in JS + * or copying element-by-element across the bridge costs ~150ms per frame, and + * passing the ArrayBuffer itself would leak it (java-direct-buffer.ts). */ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null { if (!global.isAndroid) { @@ -19,7 +20,7 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null { const baked = image.withDrawsBaked(); const bitmap = com.faceclaw.app.PreviewBitmapUtil.fromGray( - baked.pixels.buffer, + copyToJavaByteBuffer(baked.pixels), baked.width, baked.height, PREVIEW_BRIGHTEN_GAMMA, diff --git a/app/native/image-bytes.ts b/app/native/image-bytes.ts index 12c7fd5e..cb3f5a0b 100644 --- a/app/native/image-bytes.ts +++ b/app/native/image-bytes.ts @@ -1,7 +1,9 @@ import { grayImageFromPacket } from './image-files' import { type GrayImage } from '../graphics/image' +import { copyToJavaByteBuffer } from './java-direct-buffer' declare const com: any +/** The encoded bytes cross as a fresh Java buffer; passing the ArrayBuffer leaks it (java-direct-buffer.ts). */ export function decodeImageBytes(bytes: ArrayBuffer, width: number, height: number): GrayImage | null { - return grayImageFromPacket(com.faceclaw.app.ImageFileLoader.loadGrayFromBytes(bytes, width, height)) + return grayImageFromPacket(com.faceclaw.app.ImageFileLoader.loadGrayFromBytes(copyToJavaByteBuffer(new Uint8Array(bytes)), width, height)) } diff --git a/app/native/java-direct-buffer.ts b/app/native/java-direct-buffer.ts index 220164de..b882fd71 100644 --- a/app/native/java-direct-buffer.ts +++ b/app/native/java-direct-buffer.ts @@ -93,3 +93,20 @@ export class JavaDirectBuffer { return this.load(new Uint8Array(buffer)); } } + +/** + * Copy `bytes` into a fresh Java direct buffer (position 0, limit + * `bytes.length`) for a one-off crossing such as an atlas registration or a + * file write. Unlike a JavaDirectBuffer nothing is pinned between calls: the + * Java buffer is an ordinary object, freed once the JS side drops it. + */ +export function copyToJavaByteBuffer( + bytes: Uint8Array, + backend: DirectBufferBackend = nativeScriptBackend, +): JavaByteBufferLike { + // A zero-capacity direct buffer may have no address to view. + const javaBuffer = backend.allocateDirect(Math.max(bytes.length, 1)); + backend.viewOf(javaBuffer).set(bytes); + javaBuffer.limit(bytes.length); + return javaBuffer; +} diff --git a/app/native/kotlin-protocol-smoke.ts b/app/native/kotlin-protocol-smoke.ts index a99cc1e2..062ae21d 100644 --- a/app/native/kotlin-protocol-smoke.ts +++ b/app/native/kotlin-protocol-smoke.ts @@ -1,3 +1,5 @@ +import { copyToJavaByteBuffer } from './java-direct-buffer' + declare const com: any /** Development-only checks of NativeScript metadata and bulk ByteBuffer crossings. */ @@ -10,11 +12,11 @@ export function runKotlinProtocolSmokeTest(): void { const compositor = new native.SurfaceCompositor() compositor.configureScreen(3, 2) compositor.configureSurface('test', 0, 0, 3, 2, 0, 0) - const pixels = new Uint8Array([0, 16, 255, 32, 48, 64]) - compositor.submitSurface('test', new native.AndroidByteReader(pixels.buffer), 0, 0, 3, 2, '1') + const pixels = copyToJavaByteBuffer(new Uint8Array([0, 16, 255, 32, 48, 64])) + compositor.submitSurface('test', new native.AndroidByteReader(pixels), 0, 0, 3, 2, '1') assert(hex(compositor.composite().gray) === '0010ff203040', 'composition/ByteBuffer') assert(hex(native.BmpUtil.pack4bppFromGray8(compositor.composite().gray, 3, 2)) === '01f02340', 'packing') - const id = native.ImageAtlas.ensure('kotlin-smoke', 3, 2, new native.AndroidByteReader(pixels.buffer)) + const id = native.ImageAtlas.ensure('kotlin-smoke', 3, 2, new native.AndroidByteReader(pixels)) assert(id > 0 && native.ImageAtlas.get(id).width === 3, 'image atlas/reused buffer') console.log('FACECLAW_KOTLIN_PROTOCOL_PASS Android') } diff --git a/app/native/preview-display.ts b/app/native/preview-display.ts index 7ef1b618..53e3e927 100644 --- a/app/native/preview-display.ts +++ b/app/native/preview-display.ts @@ -54,6 +54,7 @@ export class PreviewDisplayTarget implements DisplayTarget { // (java-direct-buffer.ts). private readonly framePixelsBuffer = new JavaDirectBuffer(640 * 480); private readonly frameDrawsBuffer = new JavaDirectBuffer(); + private readonly shellSceneBuffer = new JavaDirectBuffer(); constructor() { const context = Utils.android.getApplicationContext(); @@ -109,7 +110,7 @@ export class PreviewDisplayTarget implements DisplayTarget { } async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise { - this.compositor.submitShellScene(new Uint8Array(bytes).buffer, paintMs, frameId); + this.compositor.submitShellScene(this.shellSceneBuffer.load(bytes), paintMs, frameId); } async setUnderlayDim(belowZOrder: number, factor: number): Promise { diff --git a/app/native/texture-atlas.ts b/app/native/texture-atlas.ts index f39d97d7..a934ed40 100644 --- a/app/native/texture-atlas.ts +++ b/app/native/texture-atlas.ts @@ -1,17 +1,21 @@ +import { copyToJavaByteBuffer } from './java-direct-buffer' + declare const com: any +// Byte payloads cross as fresh Java buffers: passing a JS ArrayBuffer to Java +// leaks it (java-direct-buffer.ts). + /** Android bridge to the process-wide shared Kotlin atlases. */ export function textureAtlasAvailable(): boolean { return !!global.isAndroid } export function textureFontId(key: string): number { return com.faceclaw.app.GlyphAtlas.fontId(key) } export function registerTextureGlyphs(buffer: ArrayBuffer, aa: boolean): void { - const reader = new com.faceclaw.app.AndroidByteReader(buffer) + const reader = new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(new Uint8Array(buffer))) if (aa) com.faceclaw.app.GlyphAtlas.registerAa(reader) else com.faceclaw.app.GlyphAtlas.register(reader) } export function registerFirmwareGlyphs(buffer: ArrayBuffer): void { - com.faceclaw.app.FwGlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(buffer)) + com.faceclaw.app.FwGlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(new Uint8Array(buffer)))) } export function textureImageId(key: string, width: number, height: number, pixels: Uint8Array): number { - const buffer = pixels.byteOffset === 0 && pixels.byteLength === pixels.buffer.byteLength ? pixels.buffer : pixels.slice().buffer - return com.faceclaw.app.ImageAtlas.ensure(key, width, height, new com.faceclaw.app.AndroidByteReader(buffer)) + return com.faceclaw.app.ImageAtlas.ensure(key, width, height, new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(pixels))) } diff --git a/tests/compass-texture-cache.test.cjs b/tests/compass-texture-cache.test.cjs index f4c6a3eb..740a313e 100644 --- a/tests/compass-texture-cache.test.cjs +++ b/tests/compass-texture-cache.test.cjs @@ -148,7 +148,7 @@ test('heading updates reuse the texture; brightness, layout and clipping changes test('the background registers once and uses a nine-byte image reference on later frames', () => { const registered = []; - const atlas = load('app/native/texture-atlas.ts', {}, '', { + const atlas = load('app/native/texture-atlas.ts', { './java-direct-buffer': { copyToJavaByteBuffer: (bytes) => bytes } }, '', { global: { isAndroid: true }, com: { faceclaw: { app: { AndroidByteReader: class { constructor(buffer) { this.buffer = buffer; } }, ImageAtlas: { ensure: (...args) => { registered.push(args); diff --git a/tests/java-direct-buffer.test.cjs b/tests/java-direct-buffer.test.cjs index e6112c7c..d6d2f67d 100644 --- a/tests/java-direct-buffer.test.cjs +++ b/tests/java-direct-buffer.test.cjs @@ -1,7 +1,7 @@ const test = require("node:test"); const assert = require("node:assert/strict"); -const { JavaDirectBuffer } = require("../.test-build/app/native/java-direct-buffer.js"); +const { JavaDirectBuffer, copyToJavaByteBuffer } = require("../.test-build/app/native/java-direct-buffer.js"); /** * A stand-in for java.nio.ByteBuffer.allocateDirect: fixed capacity, a @@ -109,3 +109,14 @@ test("a view on a larger buffer contributes only its own bytes", () => { const view = whole.subarray(10, 30); assert.deepEqual(consume(pool.load(view), 20), whole.slice(10, 30)); }); + +test("copyToJavaByteBuffer gives each crossing its own exact-length buffer", () => { + const backend = fakeBackend(); + const whole = frame(100, 5); + const first = copyToJavaByteBuffer(whole.subarray(10, 30), backend); + const second = copyToJavaByteBuffer(frame(7, 1), backend); + assert.notEqual(first, second); + assert.deepEqual(consume(first, 20), whole.slice(10, 30)); + assert.deepEqual(consume(second, 7), frame(7, 1)); + assert.equal(consume(copyToJavaByteBuffer(new Uint8Array(0), backend), 0).length, 0); +});