Skip to content

Function body maximum size #1138

Closed
Closed

Description

@Cellule

Among the browser vendors we agreed upon a limit of function body size to allow in webassembly module of 128KB.

However, the benchmark Godot has a function of 151975Bytes or 148KB (function 2187).

For some reason Chrome and Firefox allow it.
Now we end up in Edge where we invalidate this module.

It makes me think that we didn't understand the limit correctly.
According to v8/module-decoder.cc, it looks like it is checking the size in bytes.

Can someone from the other browser vendors help me understand this ?

Activity

  1. lukewagner commented on Oct 18, 2017

    @lukewagner
    Member

    D'oh, this particular limit check wasn't factored into the shared validation code and thus was only being checked by WebAssembly.validate in FF. That's an awfully big function; I'll see if perhaps Godot isn't enabling optimizations in their build. But if it is just some giant interpreter loop or something, perhaps this is evidence we've set the limit too low.

  2. lukewagner commented on Oct 18, 2017

    @lukewagner
    Member

    Also it appears that function index 21555 is even bigger, with 219504 bytes.

  3. binji commented on Oct 19, 2017

    @binji
    Member

    It makes me think that we didn't understand the limit correctly.
    According to v8/module-decoder.cc, it looks like it is checking the size in bytes.

    Unfortunately, it looks like DecodeWasmFunction is only used by SyncDecodeWasmFunction which is only used by a unittest. The limit is also used in the streaming decoder, but it's not clear why it's not erroring out there (likely because the streaming decoder is not used here). I filed a v8 bug: https://crbug.com/v8/6959.

  4. jfbastien commented on Oct 19, 2017

    @jfbastien
    Member

    FWIW we don't apply all the limits because some didn't make sense to our implementation. The ones we apply are here: https://github.com/WebKit/webkit/blob/master/Source/JavaScriptCore/wasm/WasmLimits.h

    In particular, I dropped function size because it's a ridiculously low limit which basic wasm programs exceed (e.g. the benchmarks from the PLDI paper trips it).

  5. Cellule commented on Oct 19, 2017

    @Cellule
    Author

    Do we want to revisit the limits that we use and make them part of the spec (either js or web spec)?
    I agree that 128kb is too low.

  6. littledan commented on Oct 19, 2017

    @littledan

    In general, if Wasm programs should work consistently across browsers, having any sort of restrictions be written in the specification and with conformance tests seems like a good idea to me. Otherwise, there's a natural "race to the bottom" (or top, here?) where browsers try to be compatible with each other as much as possible and permit what all the other ones permit. This sort of dynamic is responsible for a lot of the mess that we have in JS and the rest of the web platform.

    I don't understand the details of this particular limit; is there any past discussion that was written down anywhere about it?

  7. rossberg commented on Oct 19, 2017

    @rossberg
    Member

    @littledan, a race to the top is usually a good thing when it comes to arbitrary limits. Every language implementation has tons of implicit limitations, almost no language even documents their possibility (C is the only exception that comes to mind, and only for some hand-selected cases). Picking concrete numbers on the spec level makes no sense in most cases, because the limits are not static but depend on many complex platform-dependent and implementation-specific variables.

  8. lukewagner commented on Oct 19, 2017

    @lukewagner
    Member

    If it doesn't make sense in the spec, documenting the limits somewhere for toolchains to refer to makes sense. Maybe a new .md in webassembly/tool-conventions?

    Anyhow yeah, sounds like 128kb is way too low. I think having a chosen limit is better than each engine having its own implicit limit unless we think all engines will be able to handle functions up to the maxModuleSize (1gb). I doubt that, especially given the subtle limits that show up like the 32mb local jump range on ARM. How about 1mb?

  9. binji commented on Oct 19, 2017

    @binji
    Member

    It might be nice to have some more information about the largest functions we've seen before making this choice.

  10. lukewagner commented on Oct 20, 2017

    @lukewagner
    Member

    @kripken What are the biggest you've ever seen? Alon was telling me he was thinking of bringing the "outlining" feature (which splits up crazy-big functions) to wasm. With this, we wouldn't necessarily need to accommodate the biggest possible functions.

  11. kripken commented on Oct 20, 2017

    @kripken
    Member

    I've been thinking about this, but I don't yet have a good idea for something like outlining in wasm (the asm.js approach was quite hackish and we had a bunch of bugs there). I may end up just emitting a warning in the meantime.

    Size-wise, I don't have good data on this, none of the biggest ones were recent, and I measured them as lines in the text format anyhow.

    Could we do 64MB? I've never seen a wasm module that big, let alone a single function, so it seems like a safe bound. Or is there a benefit to a lower value? (I'm not sure why we need a bound at all so I'm missing some background I guess.)

  12. lukewagner commented on Oct 20, 2017

    @lukewagner
    Member

    Assuming there is a small factor expansion between wasm bytecode and machine code (I've seen 2-4x), and given ARM(32,64)'s 32mb local-jump limit, I'd be surprised if engines didn't fail in practice well before 32mb. It's possible of course to handle >32mb functions if you design for it, but I expect not all engines do. Happy to hear from other engines though.

  13. RyanLamansky commented on Oct 21, 2017

    @RyanLamansky

    One engine's data point: my .NET-based implementation inherits all of its limitations from the .NET framework itself, which doesn't seem to have any hard limit on function size (at least none that I've heard of or have been able to find after some quick searches).

  14. titzer commented on Oct 23, 2017

    @titzer

    I've fixed the V8 bug, and we now enforce the function size limit.

  15. titzer commented on Oct 23, 2017

    @titzer

    I still think it is a good idea that engines have a "gentleman's agreement" to enforce a static size limit for functions. I'm OK bumping up the 128k limit, but I think we should probably pick a reasonable one before the race to the top picks up :)

  16. 29 remaining items

  17. kripken commented on Nov 3, 2017

    @kripken
    Member

    The Go language is planning to manage the call stack manually, i.e., without using call or separate wasm functions. Approaches like that might hit imposed function size limits (they might be able to split up the one big function into chunks, but it's not straightforward).

  18. titzer commented on Nov 6, 2017

    @titzer

    @kripken Seems like that is just a prototype. It would be truly unfortunate, and probably have extremely bad performance, to generate one large function. There was discussion earlier in that thread about ignoring the stack machine and emulating a register machine. Seems like that work is preliminary and we shouldn't base decisions on it here.

  19. kripken commented on Nov 6, 2017

    @kripken
    Member

    @titzer Agreed, yeah. I just mentioned it as it seemed an interesting case for non-standard code generation (given their requirement to support things like bidirectional longjmp, a single big function or an interpreter may be their only options, all of which are not that good obviously).

  20. icefoxen commented on Jan 16, 2018

    @icefoxen

    Maybe there should be a specified minimum size instead of a maximum size?

  21. lars-t-hansen commented on Feb 5, 2018

    @lars-t-hansen
    Contributor

    The proposal is for a maximum number of functions and a maximum function body size, but the product of those two is a large number of bytecodes (7TB total code section size). Do we want to place an additional limit on the code section size?

    (Not sure about this myself. We may end up with a hard limit in SpiderMonkey anyway to accomodate streaming compilation, but this is under discussion, and I'm not sure about the wisdom of legislating it.)

  22. littledan commented on Mar 4, 2018

    @littledan

    Have the limits been added to any sort of specification document? I'm having trouble finding them.

    If they haven't been added yet: We've talked in the above thread about putting the limits in an appendix. Would the implementation limits appendix be an appropriate place, or should it be some other document? Or just in tests?

  23. icefoxen commented on Mar 4, 2018

    @icefoxen

    A way to query the limits imposed by the implementation would also be nice. I am reminded of https://www.khronos.org/registry/vulkan/specs/1.0/man/html/VkPhysicalDeviceLimits.html for example.

  24. jfbastien commented on Mar 4, 2018

    @jfbastien
    Member

    Not done, tracked here: WebAssembly/meetings#100

  25. rossberg commented on Mar 5, 2018

    @rossberg
    Member

    Would the implementation limits appendix be an appropriate place

    No, since these are Web-specific limitations. The obvious place would be the Web API spec.

  26. HarikrishnanBalagopal commented on Dec 25, 2023

    @HarikrishnanBalagopal

    Golang seems to have hardcoded a limit of 2^16 bytes = 64KB on function size (WASM page size).
    Have made an issue documenting the details golang/go#64856

    @binji I have gone through the https://github.com/binji/raw-wasm demos repo and implemented a runtime in JS that could run those demos. I haven't found a reason why the function size would have to be limited to just 1 WASM page.

    It would be very helpful if someone knowledgeable on the topic could comment on why this limit exists and how to work around it. Thank you!

  27. titzer commented on Dec 26, 2023

    @titzer

    The agreed limitation size for function bodies is 7,654,321 bytes. I am not sure why Golang has a 1 page limitation; the 64KiB page size applies to memories, so that seems weird.

    We decided to limit function size because in practice all engines have some non-linearity in their execution tiers, and a function size check ends up somewhere (e.g. the frontend of an optimizing tier). We decided to standardize limitations across engines and tools (at a rather generous size) in order to avoid incompatibilities and overall help the ecosystem. There are similar reasons for the other limitations on modules, e.g. the number of parameter/return types for a function. We generally chose powers of 10 to make it clear it's not a limitation of the binary format. IIRC @dschuff proposed 7654321 bytes for function sizes (offline); I posted it above, and we went with it.

  28. dschuff commented on Jan 2, 2024

    @dschuff
    Member

    I'm not sure how we ended up with powers of 10 for everything except module size, function size, and number of pages. I think I just proposed moving the limit from the core to the embedder spec, not the specific number :)
    Which is actually worth bringing up in this case; the limits (specified in https://webassembly.github.io/spec/js-api/index.html#limits) are actually part of the JS embedder specification, not the core specification. So other (non-JS) embeddings could have different limits. It could be that the golang target was initially written to target some other runtime (presumably a smaller one) than JS.

  29. sunfishcode commented on Feb 5, 2025

    @sunfishcode
    Member

    As discussed above, the function body size was raised to 7,654,321 bytes. The core spec discussion of limits is here. The limits defined in the js-api are here. The questions asked above seem answered, so I'll close this now; if there are further questions, please file new issues!

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