Skip to content

Define precise order of sections - #597

Merged
lukewagner merged 6 commits into
masterfrom
precise-ordering
Mar 31, 2016
Merged

lukewagner merged 6 commits into
masterfrom
precise-ordering

Conversation

@lukewagner

Copy link
Copy Markdown
Member

This PR defines the order of sections so that an engine can walk down an optional list of sections and easily match section ids when decoding a module.

A rationale for the particular order in this PR:

  • Signatures section needed by everything so it goes first.
  • Imports section as early as possible to allow a streaming module loader to fire off parallel fetches asap.
  • Indirect Function Table must go after Function Signatures
  • Export Table goes after all declarations that may possibly get exported one day (e.g., the Indirect Function Table)
  • The Start Function doesn't declare anything, so it goes after exports.
  • Function bodies go before Data segments so that parallel compilation may occur while data is being streamed, but after everything else since this is the meat of the module.
  • Names at the end since they don't semantically block anything.

Two other details in this PR to note/discuss:

  • With inline everything, there isn't a need for an end section. To be symmetric, this PR mirrors the current choice in function bodies to have byte length, not an explicit end marker, end the module.
  • Unknown sections are said to be allowed between any two sections and there isn't any caveat that tries to catch out-of-order sections: if the order of two sections is flipped, the first one will just get ignored. Happy to tighten this down if people wanted but it seemed like more work which might not actually help anyone.

@Cellule

Cellule commented Mar 9, 2016

Copy link
Copy Markdown

In your PR Description you forgot about Function Signatues which I believe goes between Import and Indirect Function from looking at the change.

if the order of two sections is flipped, the first one will just get ignored

That means a silent failure for a known section. I would have preferred to trap if 2 known sections are swapped to help catch possible bugs.
Or maybe we could export a list of ignored/skip sections (obviously not needed in the short term however).

Cellule added a commit to Cellule/ChakraCore that referenced this pull request Mar 9, 2016
…#597)

Removed the subsequent dependency and replaced it with a check for function signatures and bodies.
Cellule added a commit to Cellule/ChakraCore that referenced this pull request Mar 9, 2016
…#597)

Removed the subsequent dependency and replaced it with a check for function signatures and bodies.
Cellule added a commit to Cellule/ChakraCore that referenced this pull request Mar 9, 2016
…#597)

Removed the subsequent dependency and replaced it with a check for function signatures and bodies.
@lukewagner

Copy link
Copy Markdown
Member Author

In your PR Description you forgot about Function Signatues which I believe goes between Import and
Indirect Function from looking at the change.

Correct, I was more just giving rationale; the proposed order is what's listed in the list in BinaryEncoding.md.

That means a silent failure for a known section. I would have preferred to trap if 2 known sections are
swapped to help catch possible bugs.

Agreed, this is what I was on the fence about as well. So we could define this by saying that any given section id is either "known" or "unknown" and only "unknown" sections can be skipped. That's easy enough; sound good to everyone else?

@lukewagner

Copy link
Copy Markdown
Member Author

@titzer @binji comments?

@binji

binji commented Mar 11, 2016

Copy link
Copy Markdown
Member

Mostly a bikeshed, but is there any rationale for the location of the memory module? It seems more natural to me for that to be first.

@ghost

ghost commented Mar 11, 2016

Copy link
Copy Markdown

@binji The memory limits can be used to optimize the compiled code so need to be before the function bodies to avoid blocking compilation, but that's the only 'rationale' that comes to mind.

@lukewagner

Copy link
Copy Markdown
Member Author

I liked the principle of "imports as early as possible" followed by all the exportable definitions (incl. memory), followed by the export table. But this still allows the memory section to be in any one of 3 positions (relative to function_signatures and function_table), so you're right, that much is arbitrary.

@ghost

ghost commented Mar 11, 2016

Copy link
Copy Markdown

Might there be some utility in retaining an 'end' style section marker that indicates the end of the execution-semantic sections? This might give the runtime more confidence to start execution before the entire file has been received, given that the tail might have a lot of meta information, and not increase the latency when adding meta information.

@lukewagner

Copy link
Copy Markdown
Member Author

Given that the engine will know itself which sections it understands and that unknown sections are ignored, I wouldn't think the engine would need any extra hints. For example, regardless of the end section, an engine could start executing (assuming it's reading from a network stream) right after the data segments section finished (before the name section).

@ghost

ghost commented Mar 11, 2016

Copy link
Copy Markdown

@lukewagner Might there ever be sections added that have a semantic effect on execution, so that the data segments is not the last etc? Also since unknown sections can come anywhere, how can it know that the optional data segments section is not still to come? If a sections directory could be added at the start then this would also resolve the matter.

@lukewagner

Copy link
Copy Markdown
Member Author

If new sections were added in the future, they would continue to be ignored by older engines (b/c they would be unknown to those engines). Newer engines that recognized those sections would know to wait for them. There's only one data section so with a precise ordering, after you've seen it, you can know there's nothing semantically-visible left.

@ghost

ghost commented Mar 11, 2016

Copy link
Copy Markdown

@lukewagner But the data segments section is optional? So after reading all the function bodies, then seeing a big unknown section, and before a 'names' section, the runtime still does not know if it can start execution (there may be no data segments section to end the execution semantic sections)? The concern is that adding meta data in unknown sections will introduce latency that could be avoided.

The PR seems to define all sections as optional 'The shortest valid module is 8 bytes', but some depend on others. Could this is made clearer with a consistent text paragraph for each section stating the constraints under which it is required and that it is otherwise optional etc.

@lukewagner

Copy link
Copy Markdown
Member Author

I guess I'd like to see evidence of large non-semantic sections between spec-defined semantics-affecting sections (e.g., between bodies and data) to justify adding new "dividing" markers (which is a different proposal from an "end" marker which would literally be the last section). Such markers could also be added in the future.

I think many of the constraints are already present in the validation criteria: e.g. the number of function bodies has to match function signatures, function table indices have to be in range (implying there must be a section), etc. Are there any particular validation criteria that are missing?

@ghost

ghost commented Mar 12, 2016

Copy link
Copy Markdown

@lukewagner There is the 'evidence':

Facts:

  1. Projects such as the WebLL are adding source code meta information, which might be a significant size.
  2. You have worked on the 'names' section adding similar meta information, but much more limited and have described it as being potentially large.
  3. Placing such meta information after the execution-semantic information could reduce latency to starting execution.
  4. The data-segments section is optional (I've asked a few times and you have not refuted this).
  5. With the data-segments section optional, the runtime will not know if it is still to come after unknown sections so would need to continue waiting on the unknown sections and the end of the file before execution.

There is a technical use case of reducing latency, somewhat of core goal for wasm, and I would like it accommodated in the presence of source code meta information that helps make the code more accessible which I personally think is important.

My preference would be to add a mandatory sections directory which would address this use case by letting the runtime know early what sections follow from which it could determine when it had received all the execution-semantic information. This would also address uses cases that require random access into the binary encoding, without requiring a side-cache. But a section marker would also be an option that could be added now until a sections directory is added.

@lukewagner

Copy link
Copy Markdown
Member Author

Ah, I see your point now: in the case where no data section is present, an engine would have to consume until the end of the stream just to know it wasn't yet still to come. That makes sense and that's a good argument for a mandatory end section (which wasn't actually the last section and could be followed by any number of necessarily-unknown sections). I'll rebase and add that back to the PR.

@ghost

ghost commented Mar 12, 2016

Copy link
Copy Markdown

Thank you, very much appreciate your understanding.

@lukewagner

Copy link
Copy Markdown
Member Author

Rebased and pushed with that and above request for rejecting out-of-order known sections.

@ghost

ghost commented Mar 12, 2016

Copy link
Copy Markdown

If a module needs to be valid to be accepted and 'the end of the last present section must coincide with the last byte of the module' then might this block early execution? Might the runtime need to wait until the end of the file just to check that it ends on a section boundary before starting execution?

Would it be possible to define content after the end section as not affecting the validity of the module, rather invalid content after the end would just be silently ignored?

@lukewagner

Copy link
Copy Markdown
Member Author

That makes sense: if the point here is to allow execution to begin after "End" (while, e.g., "Names" is still being downloaded), then nothing after "End" can invalidate the whole module.

@lukewagner

Copy link
Copy Markdown
Member Author

Thinking about the practical implications a bit more:

  • The Names section would be used to name functions in Error callstacks and other reporting paths so to avoid a really unpleasant race condition for developers, an engine would want to block execution on use of the Name section, when present.
  • In the MVP, there's actually no real stream optimization opportunity b/c there are no streams, only an ArrayBuffer of code.
  • Post-MVP when Streams are added, it will be programmatically observable (w/o any timing dependence) whether validation completes at the End section or at the end of the module, so we'd need to specify and, given the above, specify the latter.
  • With a Streams API, content libraries should be able to strip unneeded sections themselves (and go even further than proposed already, aborting streams when they are no longer necessary).

So all this adds up, I think, to not having a before-the-end End section.

@binji

binji commented Mar 14, 2016

Copy link
Copy Markdown
Member

I was thinking about this some. When a section has many potential orderings because it has few current constraints (like memory), we'll want to place it earlier if we guess that future sections will depend on it.

For example, imagine we add a new section that depends on the memory section (perhaps a dynamic linking section), it will necessarily go after the all the sections that precede the memory section, even if it doesn't depend on them.

I guess what I'm saying is that it seems like we'll always be able to produce a topological sort (because no section can depend on a section that will be added later), but we won't necessary have a very good one unless we can reasonably predict future dependencies.

Maybe this doesn't matter too much? Or am I missing something?

@ghost

ghost commented Mar 14, 2016

Copy link
Copy Markdown

@lukewagner Perhaps reporting messages are not part of the spec, so a runtime has discretion over waiting for the names section or ignoring it. I seems possible to spec content after the end section as not causing a validation error and to still allow runtimes the discretion to wait?

Even for MVP the content will stream in and it seem possible for a runtime to choose to end the download after the end and to even not cache the content to save storage on a limited device, and a cloud content optimizer could also use it to strip a file without knowledge of which unknown sections affect execution.

@ghost

ghost commented Mar 14, 2016

Copy link
Copy Markdown

@lukewagner Another point, but getting a little off topic, is that my understanding of the 'stream' support is that it is a JS API in code that the web developer supplies and thus not matters that the browser vendor or the user have discretion in? I think this is a very significant distinction. Depending on a JS API to meet the 'meta-info-download/latency' use case would meet distinctly different use cases to it being under the control of the browser/user.

Further, it might be possible to articulate a design principle to the effect that the user being able to 'view source' should also give the user control over the download of the meta information to view the source, rather than it being at the discretion of the web app developer to enable or disable it on the client side (developers can still decide to include meta info or not on the server side).

@lukewagner

Copy link
Copy Markdown
Member Author

Oops, I let this drop while on vacation.

@binji That makes sense, but in the future we can always choose to loosen ordering requirements. FWIW, I'm expecting the section structure, and possibly order, to change again in one of our upcoming binary updates, but as this currently matches the precise format accepted by FF (and, though perhaps not exclusively, accepted by Chakra/V8), any objections to merging the current ordering and dealing with further changes in future PRs? @titzer @Cellule?

@JSStats In the MVP, the only APIs for compiling wasm will take ArrayBuffers which means that, from the wasm decoder/engine's pov, there is no streaming. The same functions which take ArrayBuffers in the MVP would be extended to take stream in the future (at least the Promise-returning APIs would). The stream read by the engine could come either from the network (e.g., fetch) or from user user code via a writable stream.

@titzer

titzer commented Mar 30, 2016

Copy link
Copy Markdown

V8 doesn't check the exact order of sections yet, but we will when the
order is fully specified.

On Wed, Mar 30, 2016 at 12:22 AM, Luke Wagner [email protected]
wrote:

Oops, I let this drop while on vacation.

@binji https://github.com/binji That makes sense, but in the future we
can always choose to loosen ordering requirements. FWIW, I'm expecting the
section structure, and possibly order, to change again in one of our
upcoming binary updates, but as this currently matches the precise format
accepted by FF (and, though perhaps not exclusively, accepted by
Chakra/V8), any objections to merging the current ordering and dealing with
further changes in future PRs? @titzer https://github.com/titzer
@Cellule https://github.com/Cellule?

@JSStats https://github.com/JSStats In the MVP, the only APIs for
compiling wasm will take ArrayBuffers which means that, from the wasm
decoder/engine's pov, there is no streaming. The same functions which take
ArrayBuffers in the MVP would be extended to take stream in the future
(at least the Promise-returning APIs would). The stream read by the
engine could come either from the network (e.g., fetch
https://fetch.spec.whatwg.org/) or from user user code via a writable
stream https://streams.spec.whatwg.org/#ws-model.

—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#597 (comment)

@ghost

ghost commented Mar 30, 2016

Copy link
Copy Markdown

@lukewagner Personally I think the higher level design is fundamentally too web developer centric and too anti-user. When making any design decision I would have asked: can this be designed to avoid exposing information about the runtime to the web, and can it be designed to put the user in control rather than the web code. This principle is absent in the design and it shows up here too, with the requirement that buffer or streaming be via software that the web developer supplies, software that the user has to just trust and that likely does not support features the user would want.

Adding an 'end' section would have given the user the option to reduce latency, and to reduce storage requirements, and to avoid exposing information about the validity of the following sections.

The higher level design of wasm could have been organised around privileged code, code that the community can scrutinised, just as a web browser is trusted, and this could have implemented the streaming options etc.

So as it stands a defensive web browser can just fail to report validation errors in meta information sections, as if they were not supported, but the web developers code might probe for a validation error to check for support and believing there is no support decide not to include the meta information and the code will be incomplete and the user loses out having not choice.

@lukewagner

Copy link
Copy Markdown
Member Author

@titzer so, good to merge?

@JSStats Non-web native environments have strictly more options for optimizing load time and can do what the web does and more. E.g., they can mmap the wasm file all at once and seek randomly around, using the section lengths to easily skip over ignored sections. If you want to continue this discussion, it seems like a new issue is the right place since this PR is focused on defining a precise order and what you're talking about is changing the rules for how and when things are validated.

@ghost

ghost commented Mar 30, 2016

Copy link
Copy Markdown

@lukewagner You are removing the 'end' section and not giving it an ordering, but I guess you are doing it in a context in which the meta data sections are validated, so I guess the discussion on this could be moved to a separate issue, sorry.

Just for the record, many of my points are web related. A file can't be mmapped if it has not been downloaded and the download latency can be significant on the web. A web app can not know if the user will need the meta information unless it prompts them or the browser exposes a personal setting to the app, and this does not seem a good approach to me and this is a web issue.

@titzer

titzer commented Mar 31, 2016

Copy link
Copy Markdown

lgtm

@lukewagner
lukewagner merged commit 1c9de01 into master Mar 31, 2016
@lukewagner
lukewagner deleted the precise-ordering branch March 31, 2016 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants