Skip to content

[spec] br_table has unspecified semantics for negative branch target #1570

Closed

Description

@kazzmir
(module
  (func (export "test") 
    (block $default
      (block $1
        (block $2
          (br_table $1 $2 $default (i32.const -5))))))

  )

(assert_return (invoke "test"))

The spec says l* for the br_table instruction here is [$1 $2], with indices [0, 1], and l_N is $default. The two rules for br_table based on the value i, which is -5 here, are:

br l_i ;; if i < len(l*)
br l_N ;; otherwise

In this case, i < len(l*) is true since -5 < 2, which would mean the machine would try to lookup l*[-5], which is meaningless.

The reference spec interpreter seems use the $default label here, which suggests it requires that i >= 0 before the br l_i rule is invoked.

br l_i ;; if i >= 0 && i < len(l*)

Activity

  1. kazzmir commented on Dec 30, 2022

    @kazzmir
    Author

    Also just a minor thing I guess, but the spec uses a condition for the br l_i rule that is tautological in that l_i is defined to be the same as l*[i].

    image

    image

    I guess the condition should be (if 0 <= i < |l*|)

  2. kazzmir commented on Dec 30, 2022

    @kazzmir
    Author

    Well I see why it works in the reference interpreter actually. It is because the interpreter uses I32.ge_u, so any negative number will most likely be larger than the length of the list of targets.

          | BrTable (xs, x), Num (I32 i) :: vs' ->
            if I32.ge_u i (Lib.List32.length xs) then
              vs', [Plain (Br x) @@ e.at]
            else
              vs', [Plain (Br (Lib.List32.nth xs i)) @@ e.at]
    
  3. titzer commented on Dec 31, 2022

    @titzer
    Contributor

    Yes, the input value to br_table is interpreted as a 32-bit unsigned int.

  4. kazzmir commented on Dec 31, 2022

    @kazzmir
    Author

    I agree, therefore the spec seems to not specify this.

  5. rossberg commented on Jan 19, 2023

    @rossberg
    Member

    The abstract syntax for const is supposed to only allow positive numbers, and as noted in that section, all signed uses perform explicit reinterpretation via the signed meta operator.

    However, there is indeed a bug there, because the AST grammar for const specifies iN instead of uN for integer immediates. Fixed in #1577.

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