Skip to content

Data segment overlaps #897

Closed
Closed

Description

@jfbastien

Data segments aren't well defined.

  • If they overlap, does it fail?
  • If so, what's the exception?
  • When is it thrown? Some of these issues can be known at Module time (if it was an i32.const), and others at Instance time (if it was a global).
  • If they're out of bounds, does it fail?

Activity

  1. binji commented on Dec 9, 2016

    @binji
    Member

    This is specified in JS.md, but probably should be mentioned elsewhere too.

    Segments may overlap and, if they do, the final value is the last value written in order.

    If, after evaluating the offset initializer expression of every Data and Element Segment, any of the segments do not fit in their respective Memory or Table, throw a RangeError.

  2. jfbastien commented on Dec 10, 2016

    @jfbastien
    MemberAuthor

    What if the initializer is a constant, and it's known at Module time that it's out of bounds? The failure still occurs at Instance time?

  3. jfbastien commented on Dec 10, 2016

    @jfbastien
    MemberAuthor

    Oh also, what if there's a Data section but no memory (neither imported, nor in a section)?

  4. jfbastien commented on Dec 10, 2016

    @jfbastien
    MemberAuthor

    I'm also not sure we specify that data segments are initialized before start is called.

  5. jfbastien commented on Dec 10, 2016

    @jfbastien
    MemberAuthor

    Oh, and if linking fails, is an imported memory changed by data segments? Or do data segments get populated after link succeeds, but before start is called?

  6. jfbastien commented on Dec 10, 2016

    @jfbastien
    MemberAuthor

    Oh and if initializing segments fails halfway, I assume it's visible up to when it failed? i.e. if the first segment is valid but the second isn't, then the first is set, second isn't?

    And what if a segment is only partly valid? i.e. it starts in range but ends out of range. Do we see a partial write, or none of that segment's write? I'd rather not do partial, but that's what wasm does when executing.

  7. added a commit that references this issue on Dec 10, 2016
  8. jfbastien commented on Dec 10, 2016

    @jfbastien
    MemberAuthor

    Another one: what if there's an empty data section, and no memory (neither imported, nor memory section)? Is that an error at Module creation time?

  9. jfbastien commented on Dec 10, 2016

    @jfbastien
    MemberAuthor

    This is fun. What if the memory exists but is zero-sized, and we initialize with an empty data segment?

  10. rossberg commented on Dec 12, 2016

    @rossberg
    Member

    @jfbastien, have you checked the spec interpreter and test suite?

    • If they overlap, does it fail?
    • If so, what's the exception?
    • When is it thrown? Some of these issues can be known at Module time (if it was an i32.const), and others at Instance time (if it was a global).

    What @binji says, no failure (a new module could be linked in at any later time, so that would require engines to track what parts of tables have been initialised).

    We are missing more tests for the element stuff. Contributions welcome.

    • If they're out of bounds, does it fail?

    Yes, that's a linking error. JS.md currently says it causes a RangeError, but see #889 for possible revision.

    What if the initializer is a constant, and it's known at Module time that it's out of bounds? The failure still occurs at Instance time?

    Yes, it's not worth making a special case and analysis for that.

    Oh also, what if there's a Data section but no memory (neither imported, nor in a section)?

    Validation error, see the the tests here:

    https://github.com/WebAssembly/spec/blob/master/interpreter/test/memory.wast#L26

    I'm also not sure we specify that data segments are initialized before start is called.
    Oh, and if linking fails, is an imported memory changed by data segments? Or do data segments get populated after link succeeds, but before start is called?

    Both JS.md and the interpreter are explicit that link-time type-checking is performed before initialisation. There is a simple test here that checks that the memory remains unchanged:

    https://github.com/WebAssembly/spec/blob/master/interpreter/test/linking.wast#L232

    Obviously, we could use more tests. Contributions welcome.

    Oh and if initializing segments fails halfway, I assume it's visible up to when it failed? i.e. if the first segment is valid but the second isn't, then the first is set, second isn't?
    And what if a segment is only partly valid? i.e. it starts in range but ends out of range. Do we see a partial write, or none of that segment's write? I'd rather not do partial, but that's what wasm does when executing.

    JS.md specifies that bounds checks for all data and elem segments occur before any of them take effect:

    If, after evaluating the offset initializer expression of every Data and Element Segment, any of the > segments do not fit in their respective Memory or Table, throw a RangeError.

    Apply all Data and Element segments to their respective Memory or Table in the order in which they appear in the module.

    The interpreter actually disagrees and evaluates them linearly, without prior checks. I'll change that to match JS.md.

    Another one: what if there's an empty data section, and no memory (neither imported, nor memory section)? Is that an error at Module creation time?

    Still a validation error, see the tests linked to above.

    This is fun. What if the memory exists but is zero-sized, and we initialize with an empty data segment?

    That's okay and implied by the wording, see also the test here:

    https://github.com/WebAssembly/spec/blob/master/interpreter/test/memory.wast#L6

  11. rossberg commented on Dec 12, 2016

    @rossberg
    Member

    Btw, the more interesting corner case is the one tested at

    https://github.com/WebAssembly/spec/blob/master/interpreter/test/memory.wast#L67

    It checks that a zero-size segment is okay even when it is completely out of bounds. The rationale there was that the check simply is supposed to rule out invalid memory writes, and this doesn't perform any. But arguably it would be cleaner to have a stricter check requiring that the end address is within bounds (which implies that the start address is). That's slightly simpler to test, too, so I'd suggest changing that.

  12. rossberg commented on Dec 12, 2016

    @rossberg
    Member

    See WebAssembly/spec#399 for the bounds checks before mutation issue. Includes the corner case change pointed out in the previous comment.

  13. added a commit that references this issue on Dec 12, 2016
  14. jfbastien commented on Dec 12, 2016

    @jfbastien
    MemberAuthor

    It checks that a zero-size segment is okay even when it is completely out of bounds. The rationale there was that the check simply is supposed to rule out invalid memory writes, and this doesn't perform any. But arguably it would be cleaner to have a stricter check requiring that the end address is within bounds (which implies that the start address is). That's slightly simpler to test, too, so I'd suggest changing that.

    I would make it a validation error to have zero-sized segments.

  15. rossberg commented on Dec 12, 2016

    @rossberg
    Member

    That would seem like an unnecessary special case to me.

  16. 3 remaining items

  17. saambarati commented on Dec 13, 2016

    @saambarati

    Also, we should come up with an answer to an elem_type with a num_elem of zero.

    IMO, this should also be a validation error.
    https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#element-section

  18. rossberg commented on Dec 14, 2016

    @rossberg
    Member

    Also, we should come up with an answer to an elem_type with a num_elem of zero.

    @saambarati, can you clarify what you mean by that? How is the elem_type dependent on the table size?

  19. saambarati commented on Dec 14, 2016

    @saambarati

    @rossberg-chromium I used the wrong name. I meant elem_segment not elem_type

  20. lukewagner commented on Dec 17, 2016

    @lukewagner
    Member

    Agreed with @rossberg-chromium's answers above which also match the SM impl with the already-noted exception of the weird zero-length segment rules. I'd rather not take special action to disallow zero-length anything since I can see this annoying more than helping. Rather, I think the "obvious" range check if (seg.offset > length || length - seg.offset < seg.length) fail() should reject out-of-bounds zero-length segments.

  21. rossberg commented on Dec 17, 2016

    @rossberg
    Member

    @lukewagner, I don't think the first half of the condition is even needed, it should be implied by the second (which I would write as seg.offset + seg.length > length).

    All assuming proper arithmetics, of course, not wrap-around.

  22. lukewagner commented on Dec 17, 2016

    @lukewagner
    Member

    All assuming proper arithmetics, of course, not wrap-around.

    Hehe, yes, I lifted my condition from C++ code :)

  23. rossberg commented on Jan 18, 2017

    @rossberg
    Member

    Seems like the only change we have consensus on is disallowing segments whose start address is out of bounds (which is a change in the zero-sized case -- the "weird" case mentioned above). Any disagreement with that change in particular?

  24. rossberg commented on Jan 19, 2017

    @rossberg
    Member

    Okay, there seems to be no disagreement on that point, so I took the liberty to land WebAssembly/spec#399, which makes the spec match the design doc while making the aforementioned small change.

  25. added this to the MVP milestone on Jan 31, 2017
  26. rossberg commented on Feb 6, 2017

    @rossberg
    Member

    This seems to be resolved.

  27. added a commit that references this issue on Dec 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions