Xuanwo opened a new pull request, #8937: URL: https://github.com/apache/opendal/pull/8937
# Which issue does this PR close? Closes #. # Rationale for this change In `bindings/nodejs`, async methods accepting `napi::bindgen_prelude::Buffer` accessed V8's `ArrayBuffer` backing store from Tokio worker threads after returning a `Promise` to JavaScript: - `Reader::read(buf)` wrote into `buf.as_mut()` while the future executed off the JS thread. If JavaScript detached (`ArrayBuffer.prototype.transfer()`) or shrank (`ArrayBuffer.prototype.resize()`) the backing store while the read was in flight, the worker thread wrote through a dangling or out-of-bounds pointer (use-after-free / data race). - `Operator::write(path, content, options)` and `Writer::write(content)` passed `Buffer` into an `async fn` and copied `buf.as_ref().to_owned()` only after the future began polling on a worker thread. Detaching or mutating the backing `ArrayBuffer` immediately after calling `write()` could cause a use-after-free or data race. # What changes are included in this PR? - Add `ReadTarget` and `ReadIntoBuffer` in `bindings/nodejs/src/lib.rs` so `Reader::read` captures the requested byte length on the JS thread, reads into an owned `opendal::Buffer` on the async worker, and re-queries the live destination `Buffer` via `napi_get_buffer_info` on the JS thread at Promise resolution before copying bytes. If the destination buffer was detached or shrunk below the requested length while the read was in flight, the Promise rejects with an error instead of writing into invalid memory. - Add `OwnedBuffer` in `bindings/nodejs/src/lib.rs` and use it in `Operator::write` and `Writer::write` so `Buffer` inputs are copied into an owned `Vec<u8>` synchronously on the JS thread during argument conversion before spawning the async task. - Preserve the existing TypeScript signatures in `generated.d.ts` (`read(buf: Buffer): Promise<bigint>`, `write(..., content: Buffer | string, ...)`). - Add regression tests in `bindings/nodejs/tests/suites/async.suite.mjs` covering in-flight `ArrayBuffer` detachment, resizable `ArrayBuffer` grow/shrink, subarrays with non-zero byte offsets, zero-length/pre-detached buffers, async read error cleanup, and immediate post-call detachment/mutation on `Operator.write` and `Writer.write`. # Are there any user-facing changes? `Reader.read(buf)` now rejects with an error if the destination `Buffer`'s underlying `ArrayBuffer` is detached or shrunk while the read is in flight, rather than risking memory corruption. `Operator.write` and `Writer.write` snapshot `Buffer` bytes synchronously before returning the Promise. Public TypeScript and JavaScript API signatures are unchanged. # Breaking changes # AI Usage Statement - Harness: pi - Model: Gemini 4 Neon - Effort: high - Role: Investigated async `Buffer` lifecycle across `bindings/nodejs`, implemented the JS-thread buffer snapshotting and resolution-time copy-back fix, added Node.js regression tests, and verified builds, lints, and tests. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
