-
Notifications
You must be signed in to change notification settings - Fork 488
Description
Different sections of the WebAssembly spec are inconsistent about whether the 'align' parameter of load/store instructions equals the actual number of bytes to align to (i.e. 1, 2, 4, 8) or the base-2 exponent of that value (1, 2, 3, 4). Comparing against the observed behaviour of V8 and wast2wasm
, and assuming these are implementing the spec as intended, the most likely interpretation seems to be:
- Within the abstract syntax,
memarg.align
refers to the exponent part (1, 2, 3, 4); this is backed up by the Validation section ("The alignment 2^(memarg.align) must not be larger than the width of t divided by 8"). - The binary format contains the exponent part encoded as
u32
: for example, ani64.load
instruction with the default alignment hint of 8 bytes is encoded as0x29 0x03 0x00
, not0x29 0x08 0x00
. (This is consistent with the Binary Format section of the spec, which shows this value being transferred directly intomemarg.align
during decoding). - In the text format, however, the align parameter (where specified) is given as the actual byte value:
i64.load align=8
, noti64.load align=3
. As such, there's an implicit 'log2' conversion needed when settingmemarg.align
during decoding. The productions in the Text Format section don't include this detail, and just show the value being taken directly from thealign=
token, or from the N(=1/2/4/8) in memargN.
The most direct fix (albeit not a particularly pretty one) would be to change the outputs of the two alignN
rules to log2(a) and log2(N), along with a note that a
must be a power of 2.
(As an aside: the fact that this parameter exists in the spec but with no defined semantics seems like a bug to me. It implies that the people who put it there have "secret sauce" knowledge about how to use this parameter to achieve a performance optimisation, which isn't accessible to an independent developer attempting a clean-room implementation of a WebAssembly producer or consumer. Obviously, this is a Bad Thing for an open standard.)