Skip to content

[js-api] Integration with the ResizableArrayBuffer and GrowableSharedArrayBuffer proposal #1292

Open

Description

@syg

The ResizableArrayBuffer and GrowableSharedArrayBuffer proposal is a TC39 proposal that is currently stage 2.

There was some discussion of how the proposal might integrate with the Wasm/JS API. It seems better to move the discussion here to get a concrete plan and to get more Wasm eyes on the issue.

As a starting point, I suggest the following changes to the WebAssembly.Memory API:

  • Memory will have a new resizableBuffer attribute.
    • Returns a ResizableArrayBuffer or GrowableSharedArrayBuffer instance.
    • If an ArrayBuffer was vended by the buffer attribute on the same instance, that ArrayBuffer instance will be detached. SharedArrayBuffers and GrowableSharedArrayBuffer will alias the same memory.
    • If the Memory was created with a maximum size, that size will be reflected on the ResizableArrayBuffer or GrowableSharedArrayBuffer instance.
    • Otherwise, the maximum size is 2^32.
  • Memory's buffer attribute will detach any ResizableArrayBuffer buffers vended by the resizableBuffer attribute.
  • ResizableArrayBuffers gotten via Memory's resizableBuffer attribute will:
    • Throw on transfer(), since they can't be detached.
    • Throw on resize() calls that are shrinks.
    • Throw on resize() calls that are not in multiples of the wasm page size. (Thanks @conrad-watt)
  • GrowableArrayBuffers gotten via Memory's resizableBuffer attribute will:
    • Throw on grow() calls that are not in multiples of the wasm page size.

Thoughts welcome.

(Also, what is the process for bringing such a "normative PR" that's not a full proposal to the CG? Is it just an agenda item?)

Activity

  1. syg commented on Mar 2, 2021

    @syg
    ContributorAuthor

    cc @kripken for thoughts on the design for emscripten-generated code.

  2. conrad-watt commented on Mar 2, 2021

    @conrad-watt
    Contributor

    The fields of the memoryDescriptor currently correspond precisely to the components of the type of the memory in core Wasm. Would it be acceptable for all memories to have a working resizableBuffer attribute added, rather than keying it off a new flag in the memoryDescriptor? Otherwise, there'd be an issue of how to handle memories that aren't directly created in JS, but are instead exported from a Wasm instance.

    Also, there was some discussion that grow/resize would need to throw if grown by amounts which are not multiples of the Wasm page size, which seems fine.

    It's great that this feature is being added to JS! To add more context for people here, I've linked below some pretty big toolchain/performance limitations Wasm has been dealing with which would be significantly helped by Resizable/GrowableArrayBuffer integration.
    WebAssembly/design#1271
    WebAssembly/design#1296

  3. syg commented on Mar 2, 2021

    @syg
    ContributorAuthor

    Would it be acceptable for all memories to have a working resizableBuffer attribute added, rather than keying it off a new flag in the memoryDescriptor? Otherwise, there'd be an issue of how to handle memories that aren't directly created in JS, but are instead exported from a Wasm instance.

    Ah, good point about non-JS-created memories. I don't have a strong opinion, it's more that I'm not sure how an always-available resizableBuffer attribute works. Vending a ResizableArrayBuffer/GrowableSharedArrayBuffer is mutually exclusive with vending a plain ArrayBuffer/SharedArrayBuffer. So if resizableBuffer is always there, is it a "treat whichever attribute is accessed first as the intended Buffer class to use"? That is, if I access resizableBuffer first, then afterwards accessing buffer would throw, and vice versa. That's a pretty weird API, but maybe it's fine?

  4. syg commented on Mar 2, 2021

    @syg
    ContributorAuthor

    A third path would be another way to signal intent for what kind of buffer you want that's not on the MemoryDescriptor. I'm not fully clued in on the intent of the MemoryDescriptor, so that might be the wrong place to signal intent to begin with.

  5. conrad-watt commented on Mar 2, 2021

    @conrad-watt
    Contributor

    Vending a ResizableArrayBuffer/GrowableSharedArrayBuffer is mutually exclusive with vending a plain ArrayBuffer/SharedArrayBuffer.

    Does this have to be the case? Having the same Wasm memory backing both (e.g.) an ArrayBuffer and a ResizableArrayBuffer doesn't instinctively seem weirder to me than having an ArrayBuffer backed by a Wasm memory which is separately manipulated by Wasm operations. Presumably the ArrayBuffer would detach if the ResizableArrayBuffer ever calls resize, just as is already done for memory.grow in Wasm.

  6. syg commented on Mar 3, 2021

    @syg
    ContributorAuthor

    I feel somewhat strongly that aliasing buffers like that on the JS side is a bad idea.

    1. You're right that the buffer aliasing already exists for wasm-manipulated memory, but I feel like that's a pretty coarse-grained aliasing. It is still an invariant on the JS side that for non-shared buffers, if ta1.buffer !== ta2.buffer, their accesses can't alias. I'm pretty hesitant to break this invariant. It seems broadly useful for reasoning and source code transforms.

    2. As you point out, resizes would require detaching all aliased non-resizable buffers (I guess there would just be a single one). This is extra complexity in implementations and spec that I think would be harmful.

    Edit: Contra to #1292 (comment), I guess I do have a strong opinion if the two attributes are supposed to vend aliased-backing-store buffers. :P

  7. conrad-watt commented on Mar 3, 2021

    @conrad-watt
    Contributor

    In that case something like #1292 (comment) does seem like the immediate path of least resistance, although if buffer aliasing were to sneak into JS through some other avenue, I'd hope we could relax the throwing behaviour.

  8. conrad-watt commented on Mar 3, 2021

    @conrad-watt
    Contributor

    Alternatively, maybe the first vend of aResizableArrayBuffer could permanently detach the associated ArrayBuffer (including subsequent results of .buffer), while vending a GrowableSharedArrayBuffer is just allowed to alias with the associated SharedArrayBuffer, since consecutive accesses to .buffer of a shared memory can already give aliasing buffers (e.g. if there is a concurrent growth).

  9. syg commented on Mar 3, 2021

    @syg
    ContributorAuthor

    Cool, I like the suggestion in #1292 (comment).

  10. conrad-watt commented on Mar 3, 2021

    @conrad-watt
    Contributor

    With reference to the OP...

    • If the Memory was created with a maximum size, that size will be reflected on the ResizableArrayBuffer or GrowableSharedArrayBuffer instance.
    • Otherwise, some implementation-defined max size will be reflected.

    If I'm interpreting the JS proposal correctly, it's mandatory to provide a maximum size, but grow/resize is still allowed to fail arbitrarily even before reaching the max size, analogous to our memory.grow. In Wasm, a memory's max size is optional, but there's still a hard limit of 2^32 bytes, so from a purely semantic standpoint it would make sense for this to be the observable max in JS for a Wasm memory with no explicit max, rather than leaving it to be implementation-defined (especially if JS were to ever allow buffers larger than this in other contexts).

    Since growth can fail even for sizes below this, there's no issue in abstract, but would this approach mess with any engine/code heuristics?

    EDIT: there was a time when engines aligned on a hard limit of 2^31 bytes for memories, below the theoretical maximum. I interpreted the "implementation-defined" line of the OP more broadly, but is the intention of this line just to allow for a static limit like this to be exposed?

  11. syg commented on Mar 3, 2021

    @syg
    ContributorAuthor

    so from a purely semantic standpoint it would make sense for this to be the observable max in JS for a Wasm memory with no explicit max, rather than leaving it to be implementation-defined (especially if JS were to ever allow buffers larger than this in other contexts).

    That makes sense to me, I'll update the OP.

    Since growth can fail even for sizes below this, there's no issue in abstract, but would this approach mess with any engine/code heuristics?

    The engine implication to me is that Wasm memory-backed buffers need special casing. But they do already for special detachment semantics, special resize semantics, etc, so this seem to me that it'll add any more.

  12. conrad-watt commented on Mar 4, 2021

    @conrad-watt
    Contributor

    That makes sense to me, I'll update the OP.

    Just to be clear, after some pondering I think that it makes sense for the length in this case to be implementation defined up to any static limit that engines set on memory size which is lower than the spec max. For example, this used to be 2^31, is now moving to 2^32 (in line with the spec limit), and when we get 64-bit memories, may be somewhat lower than 2^64.

    Generally we've tried to have all Web engines agree to align on such a value, where it's necessary, but it's not documented as part of the "core spec".

    EDIT: the value is in fact fixed by the JS API, so that should be the number used for the max field. No need to say it's implementation defined after all!

  13. syg commented on Mar 4, 2021

    @syg
    ContributorAuthor

    when we get 64-bit memories, may be somewhat lower than 2^64.

    Oh, that's gonna be a tricky thing on the JS side. ArrayBuffer sizes aren't BigInts, nor can BigInts be property keys currently.

  14. syg commented on Mar 4, 2021

    @syg
    ContributorAuthor

    EDIT: the value is in fact fixed by the JS API, so that should be the number used for the max field. No need to say it's implementation defined after all!

    I'm reading that as 2^32 (max 65536 pages * 65536 byte page size), yeah?

  15. conrad-watt commented on Mar 4, 2021

    @conrad-watt
    Contributor

    Oh, that's gonna be a tricky thing on the JS side. ArrayBuffer sizes aren't BigInts, nor can BigInts be property keys currently.

    AFAIK we haven't explicitly discussed JS integration for the memory64 proposal yet, but that might be something that @aardappel can comment further on at some point.

    I'm reading that as 2^32 (max 65536 pages * 65536 byte page size), yeah?

    That's my reading 🎉

    (Also, what is the process for bringing such a "normative PR" that's not a full proposal to the CG? Is it just an agenda item?)

    BigInt integration (which IIUC was also purely a JS-API change) was handled as a regular proposal, but IIRC it moved through the phases very quickly because there were no core spec changes. Unless anyone comes up with objections here (@kripken dropped a 👍 above) it probably makes sense to add this to the agenda as a poll for phase 1. BigInt integration was proposed quite early on in the life of the committee so people may want to bikeshed a revised procedure for JS-API changes going forward.

  16. 17 remaining items

  17. eqrion commented on Feb 27, 2025

    @eqrion
    Collaborator

    @syg Ah, that makes sense now.

    @conrad-watt At least for SM we only ever do a moving-grow for non-shared memories when the maximum size is not present. All other cases only grow in place. In the case that we cannot reserve the maximum size, we do an exponential backoff to try to reserve as much as we can and then just work within that.

    I still think that option 2 makes the most sense here. It seems symmetrical with the fact that in-order to create a resizable array buffer you need to provide the maximumByteLength [1] and also the fact that resizable array buffers need to report a maximum size through this accessor [2]. If we allowed a wasm memory without a maximum to be viewed as a resizable array buffer, I'm not sure what we should report here.

    [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/ArrayBuffer#creating_a_resizable_arraybuffer
    [2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/maxByteLength

  18. syg commented on Feb 27, 2025

    @syg
    ContributorAuthor

    That's a great point on needing to expose some value for maximumByteLength, and makes me lean towards (2) as well.

  19. syg commented on Feb 27, 2025

    @syg
    ContributorAuthor

    At least for SM we only ever do a moving-grow for non-shared memories when the maximum size is present. All other cases only grow in place.

    @eqrion Do you mean when the maximum size is not present?

  20. eqrion commented on Feb 27, 2025

    @eqrion
    Collaborator

    At least for SM we only ever do a moving-grow for non-shared memories when the maximum size is present. All other cases only grow in place.

    @eqrion Do you mean when the maximum size is not present?

    Whoops, yes, thanks. I updated my comment.

  21. guest271314 commented on Feb 28, 2025

    @guest271314

    Make it plain.

    Or make it so as it is in JavaScript world.

  22. conrad-watt commented on Feb 28, 2025

    @conrad-watt
    Contributor

    If we allowed a wasm memory without a maximum to be viewed as a resizable array buffer, I'm not sure what we should report here.

    I'd expect that we'd simply report the maximum possible addressable Wasm memory size, but I'm sympathetic to the point that resizableArrayBuffers have an explicit max size. I'm fine with (2) in this case.

    I do expect that a reasonable implementation could do (1) internally in addition to (2), in the case that the declared Wasm maximum is ridiculously big.

  23. syg commented on Mar 14, 2025

    @syg
    ContributorAuthor

    Just found this unfortunate edge case around maximally large Wasm memories. So for 32-bit memories, the max number of pages is 65536, which means the max byte size is 65536^2, or UINT32_MAX+1. V8 stores byte lengths for TypedArrays, ArrayBuffers, and their stores as uintptr_t fields, which can't represent UINT32_MAX+1 on 32-bit architectures.

    I'll stipulate that those fields can't be widened, and it also seems super invasive to store some bit somewhere else to say "actually the length here is UINT32_MAX+1".

    @conrad-watt @eqrion Any opinions on what to do for this edge case? Our current preference is to basically fudge it as a willful violation. On 32-bit architectures, if you do a new Memory({initial, maximum: 65536}), toResizableBuffer().maxByteLength will be 65535 * 65536 (i.e. one page smaller).

    From a spec's POV this is just a double, so it's fine. But it seems eminently reasonable to represent byte lengths on 32-bit architectures as 32-bit ints and not 64-bit ints, so do we want to build in some implementation-definedness here for when the maximum is 65536?

  24. conrad-watt commented on Mar 14, 2025

    @conrad-watt
    Contributor

    Yuck (to the situation!). Currently, what happens in pure JS if you attempt to create a ResizableArrayBuffer of (max) size 2^32 on a 32-bit platform?

    EDIT: and is there different behaviour on a 64-bit platform?

  25. fgmccabe commented on Mar 14, 2025

    @fgmccabe
    Collaborator

    Isn't a 2^32 byte array impossible on a 32bit platform?

  26. syg commented on Mar 14, 2025

    @syg
    ContributorAuthor

    Currently, what happens in pure JS if you attempt to create a ResizableArrayBuffer of (max) size 2^32 on a 32-bit platform?

    It would throw a RangeError because you can't allocate that much memory.

    EDIT: and is there different behaviour on a 64-bit platform?

    The behavior of pure JS RABs? Depends on the engine whether they'll allow you to allocate that much, but it's not basically guaranteed to fail as in 32bit.

  27. conrad-watt commented on Mar 14, 2025

    @conrad-watt
    Contributor

    It would throw a RangeError because you can't allocate that much memory.

    Does this mean that on current 32-bit platforms, it's expected that we can successfully instantiate a Wasm module with a memory that has 2^16 pages set as its max size, but attempting to create a ResizeableArrayBuffer in pure JS with analogous max size (2^32 bytes) would always throw? I might have thought naively that they'd both succeed and fail under the same circumstances - would the Wasm memory use a different implementation strategy (like relocating/copying grows)? If so, does this cause any other issues when attempting to expose it as a ResizeableArrayBuffer?

    If creation of the Wasm memory would fail iff the creation of the analogous ResizeableArrayBuffer in pure JS would fail, then I'd hope that Wasm wouldn't present any additional special difficulties (since the case where the Wasm memory would overflow the uintptr_t couldn't happen, just as it couldn't in pure JS).

    EDIT: or is the issue exactly that the ResizeableArrayBuffer must throw because the max size in bytes can't be represented in uintptr_t, but Wasm doesn't throw/fail because the max size in pages can?

  28. syg commented on Mar 14, 2025

    @syg
    ContributorAuthor

    Does this mean that on current 32-bit platforms, it's expected that we can successfully instantiate a Wasm module with a memory that has 2^16 pages set as its max size, but attempting to create a ResizeableArrayBuffer in pure JS with analogous max size (2^32 bytes) would always throw? I might have thought naively that they'd both succeed and fail under the same circumstances - would the Wasm memory use a different implementation strategy (like relocating/copying grows)?

    Yeah it does. Wasm succeeds but JS will fail.

    The engine interpretation of "maximum" between Wasm memories and JS RABs kind of diverged over time.

    Wasm memory maximums passed in to the constructor (which I'll refer to as "requested maximum") are like true hints. If the engine can't allocate virtual memory up to the requested maximum, it backs off and tries until it gets a successful allocation. This lower, internal maximum is the true maximum and not exposed to users. This is a "fail late" strategy wrt in-place growing.

    RAB maximums passed to the constructor are like demands. If the engine can't allocate virtual memory up to the requested maximum, it throws at construction. IOW, if the internal maximum can't be the requested maximum, allocation fails. This is a "fail early" strategy wrt in-place growing.

    This difference in interpretation is kind of moot though for V8. Even if RABs adopted Wasm's "fail late" strategy, we still can't expose 2^32 without widening all length fields on 32bit, which isn't worth it for this edge case.

    Edit: And presumably it's also risky to do the flip side thing and make Wasm memories fail to allocate with 2^16 pages on 32bit, since that succeeds today.

  29. eqrion commented on Mar 14, 2025

    @eqrion
    Collaborator

    @syg We're not far enough into our implementation to have ran into this. From some cursory exploration right now, we also store our maxByteLength field as a uintptr_t and would run into the same issue. However, I don't think widening it would be an issue for us. We also are already using a 'IsWasm' bit on the array buffer object, and have been using that to override JS behavior tactically. We could probably use that here too.

    So nothing definitive, but I don't think that this will be an issue for us.

    I'm not opposed to there being an allowance for implementation defined maxByteLength, but would that be something that needs to exist on the wasm spec side, or the JS spec side?

  30. syg commented on Mar 14, 2025

    @syg
    ContributorAuthor

    I'm not opposed to there being an allowance for implementation defined maxByteLength, but would that be something that needs to exist on the wasm spec side, or the JS spec side?

    It would be in the JS/Wasm API spec.

  31. syg commented on Apr 22, 2025

    @syg
    ContributorAuthor

    Spinning off the maxByteLength question into #1895. Please continue the discussion there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions