I am trying to recognize each part of a wasm binary module according to webassembly's module structure doc. And I have done a part of those recognizations like shows below:
the code in red color was labelled as "magic code";
the code in dark orange was labelled as "version number";
the code in light yellow was labelled as an "id" of the first section;
the code in dark green was labelled as "payload length" which in "varuint7" format;
I am not sure about how to recognizing the next following parts, a little confusing about which part is the "payload data"? And what is the meaning of "The end of the last present section must coincide with the last byte of the module." which wrote in official doc.
I suggest looking at an existing WebAssembly binary parser to help understand. Here's the WebKit one.
Each section is decoded in a section-specific way as detailed in the binary format documents. You look at id
and payload_length
, and then based on the section ID you decode the appropriate section. From that page section numbers are:
Also, 0 is "custom".
Go to each of those section's description to know how to decode them.
"The end of the last present section must coincide with the last byte of the module." means that if you decode a section, and the decoded length doesn't match the payload data, then it's a validation error.