Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions crates/perry-runtime/src/object/global_this.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,6 @@ fn global_this_fetch_option_string_ptr(init: f64, name: &[u8]) -> *const crate::
crate::value::js_get_string_pointer_unified(value) as *const crate::StringHeader
}

fn global_this_body_string_ptr(value: f64) -> *const crate::StringHeader {
if matches!(
value.to_bits(),
crate::value::TAG_UNDEFINED | crate::value::TAG_NULL
) {
return std::ptr::null();
}
crate::value::js_get_string_pointer_unified(value) as *const crate::StringHeader
}

fn global_this_headers_handle_from_value(value: f64) -> f64 {
if matches!(
value.to_bits(),
Expand Down Expand Up @@ -237,7 +227,19 @@ pub(crate) extern "C" fn global_this_response_thunk(
body: f64,
init: f64,
) -> f64 {
let body_ptr = global_this_body_string_ptr(body);
// Route the body through the registered body-init helper (stdlib
// `js_response_body_init_ptr`) so a binary body — Buffer / Uint8Array /
// ArrayBuffer — copies its raw bytes instead of being stringified to a
// zero-filled payload (#5435). String bodies fall back to the ordinary
// coercion. Mirrors the Request thunk's body handling above.
let body_ptr = if matches!(
body.to_bits(),
crate::value::TAG_UNDEFINED | crate::value::TAG_NULL
) {
std::ptr::null()
} else {
super::global_fetch::call_global_body_init_ptr(body)
};
let status = global_this_fetch_option(init, b"status");
let status = if status.to_bits() == crate::value::TAG_UNDEFINED {
0.0
Expand Down
67 changes: 67 additions & 0 deletions crates/perry-stdlib/src/fetch/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ use perry_runtime::js_get_string_pointer_unified;
/// (`c.json`/`c.text`) are unaffected.
#[no_mangle]
pub extern "C" fn js_response_body_init_ptr(value: f64) -> i64 {
// A binary body — Buffer / Uint8Array / typed array / ArrayBuffer — must
// copy its RAW bytes. Such a value is a BufferHeader/TypedArrayHeader
// pointer, NOT a StringHeader; passing it straight to `string_from_header`
// read the byte length but data at the wrong offset (zero-fill), so the
// payload came back all zeroes (#5435). Materialize the bytes into a heap
// StringHeader so `js_response_new`'s lossless byte read recovers them.
if let Some(bytes) = unsafe { body_value_buffer_bytes(value) } {
return unsafe { js_string_from_bytes(bytes.as_ptr(), bytes.len() as u32) } as i64;
}
// A non-integral or out-of-range value can't be a stream, so skip the
// registry probe for the common cases.
if value.is_finite()
Expand All @@ -48,6 +57,64 @@ pub extern "C" fn js_response_body_init_ptr(value: f64) -> i64 {
js_get_string_pointer_unified(value)
}

/// Read a body `*const StringHeader` losslessly as raw bytes (no UTF-8
/// validation). Mirrors `string_from_header` but never round-trips through
/// `str::from_utf8`, so a binary body materialized by `js_response_body_init_ptr`
/// (a Buffer / Uint8Array / ArrayBuffer copied verbatim into a StringHeader)
/// keeps every byte instead of being dropped when the bytes aren't valid UTF-8.
/// Refs #5435. `None` for a null/sub-page pointer (no body).
pub(crate) unsafe fn body_bytes_from_header(ptr: *const StringHeader) -> Option<Vec<u8>> {
if ptr.is_null() || (ptr as usize) < 0x1000 {
return None;
}
let len = (*ptr).byte_len as usize;
let data_ptr = (ptr as *const u8).add(std::mem::size_of::<StringHeader>());
Some(std::slice::from_raw_parts(data_ptr, len).to_vec())
}

/// If `value` is a binary body — a typed array, Buffer/Uint8Array, or
/// ArrayBuffer — return a copy of its raw bytes; `None` for anything else
/// (strings, stream handles, numbers) so callers fall back to string coercion.
///
/// A typed array / buffer is laid out as `BufferHeader` (length at offset 0,
/// data at offset 8) or `TypedArrayHeader`, NOT `StringHeader` (data at offset
/// 20). Feeding such a pointer straight to `string_from_header` read the byte
/// length correctly but the data at the wrong offset — zero-filled padding —
/// so `new Response(new Uint8Array([1,2,3]))` came back all zeroes (#5435).
/// Materializing the real bytes here lets the body round-trip byte-for-byte.
pub(crate) unsafe fn body_value_buffer_bytes(value: f64) -> Option<Vec<u8>> {
let jsval = JSValue::from_bits(value.to_bits());
// A typed array / Buffer / ArrayBuffer body is a POINTER_TAG value; decode
// it via the shared `JSValue` accessors rather than reconstructing the tag
// mask here. A raw untagged heap pointer (no NaN-box tag) is also accepted.
// A string body is STRING_TAG, so `is_pointer()` is false and it falls
// through to the ordinary string coercion.
let addr = if jsval.is_pointer() {
jsval.as_pointer::<u8>() as usize
} else {
let bits = value.to_bits();
if (bits >> 48) == 0 && bits >= 0x10000 {
bits as usize
} else {
return None;
}
};
if addr < 0x1000 {
return None;
}
if perry_runtime::typedarray::lookup_typed_array_kind(addr).is_some() {
let ta = addr as *const perry_runtime::typedarray::TypedArrayHeader;
return perry_runtime::typedarray::typed_array_bytes(ta).map(|b| b.to_vec());
}
if perry_runtime::buffer::is_registered_buffer(addr) {
let ptr = addr as *const perry_runtime::buffer::BufferHeader;
let len = (*ptr).length as usize;
let data = perry_runtime::buffer::buffer_data(ptr);
return Some(std::slice::from_raw_parts(data, len).to_vec());
}
None
}

lazy_static::lazy_static! {
static ref FORM_DATA_METHOD_VALUE_CACHE: Mutex<HashMap<(usize, &'static str), u64>> =
Mutex::new(HashMap::new());
Expand Down
6 changes: 3 additions & 3 deletions crates/perry-stdlib/src/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,10 +1217,10 @@ pub unsafe extern "C" fn js_response_new(
status_text_ptr: *const StringHeader,
headers_handle: f64,
) -> f64 {
let body_opt = string_from_header(body_ptr);
// Lossless raw-byte read so binary bodies survive byte-for-byte (#5435).
let body_opt = dispatch::body_bytes_from_header(body_ptr);
let body_present = body_opt.is_some();
let body_str = body_opt.unwrap_or_default();
let body = body_str.into_bytes();
let body = body_opt.unwrap_or_default();
// NaN / 0.0 are the codegen "no status field" sentinels. Node defaults
// missing status to 200; any explicit value is truncated toward zero
// then range-checked against 200..=599 (199.9 → RangeError, 599.9 →
Expand Down
29 changes: 29 additions & 0 deletions test-files/test_gap_response_binary_body_5435.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Test new Response(Uint8Array/ArrayBuffer) binary body round-trips (#5435)
// Expected output:
// uint8array body bytes: [1,2,3,4,5]
// arraybuffer body bytes: [1,2,3,4,5]
// binary body bytes: [255,0,137,80,78,71]
// string body text: hello

async function main(): Promise<void> {
const data = new Uint8Array([1, 2, 3, 4, 5]);

const rb = new Response(data as BodyInit);
const got = new Uint8Array(await rb.arrayBuffer());
console.log("uint8array body bytes: " + JSON.stringify(Array.from(got)));

const ra = new Response(data.buffer as BodyInit);
const gotA = new Uint8Array(await ra.arrayBuffer());
console.log("arraybuffer body bytes: " + JSON.stringify(Array.from(gotA)));

// Non-UTF-8 bytes (PNG header-ish) must survive verbatim, not be dropped.
const bin = new Uint8Array([255, 0, 137, 80, 78, 71]);
const rbin = new Response(bin as BodyInit);
const gotBin = new Uint8Array(await rbin.arrayBuffer());
console.log("binary body bytes: " + JSON.stringify(Array.from(gotBin)));

// String bodies must still round-trip unchanged.
const rs = new Response("hello");
console.log("string body text: " + (await rs.text()));
}
void main();
Loading