Bug 260017 - RangeError: Out of memory when creating (Shared)ArrayBuffer with big maxByteLength
Summary: RangeError: Out of memory when creating (Shared)ArrayBuffer with big maxByteL...
Status: RESOLVED WONTFIX
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebAssembly (show other bugs)
Version: Safari 16
Hardware: iPhone / iPad iOS 16
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2023-08-10 00:40 PDT by Joonas Lipping
Modified: 2023-08-17 10:26 PDT (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joonas Lipping 2023-08-10 00:40:27 PDT
When creating an ArrayBuffer or SharedArrayBuffer with a small length but a large maxByteLength, for example:

  new ArrayBuffer(64 * 1024, { maxByteLength: 4 * 1024 * 1024 * 1024 })

or

  new SharedArrayBuffer(64 * 1024, { maxByteLength: 4 * 1024 * 1024 * 1024 })

a RangeError: Out of memory occurs, as if we are trying to allocate the whole 4GiB immediately.

I would expect that the buffer would initially be created successfully with 64kiB of memory, but a subsequent grow() might fail, if it exceeds available memory.

If I reduce the requested maxByteLength to e.g. 4MiB instead, it successfully creates the buffer.

I think this only occurs if the device has less memory available than the maxByteLength that we give as an argument. On the iPhone SE (2020) that I have, the exception always occurs if I request maxByteLength as 4GiB, but on the laptop it's fine, presumably because the laptop has that much memory to spare.
Comment 1 Radar WebKit Bug Importer 2023-08-17 00:41:12 PDT
<rdar://problem/114012792>
Comment 2 Mark Lam 2023-08-17 08:07:19 PDT
I don't think there's anything in the spec that says a device must give you the requested maxByteLength.  There are many different constraints in a system as to why this is not workable.

Does any other phone devices behave differently and allow a request for 4G of max capacity?
Comment 3 Yusuke Suzuki 2023-08-17 10:26:56 PDT
Yes, this is expected behavior. Growable DharedArrayBuffer is designed to allocate virtual memory region with maxByteLength (to allow concurrent access to grown memory), and this means that we need to allocate that from the beginning (the spec itself is designed so) So large size can fail, and expected.