Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Petr4 allows lastIndex out of parser context #405

Open
pataei opened this issue Mar 14, 2023 · 0 comments
Open

Petr4 allows lastIndex out of parser context #405

pataei opened this issue Mar 14, 2023 · 0 comments
Labels
typechecker vs.P4 Petr4 limitation compared to P4

Comments

@pataei
Copy link
Collaborator

pataei commented Mar 14, 2023

P4 spec states that hs.lastIndex may only be used in a parser but Petr4 doesn't enforce this.

Petr4 code

The expression hs.lastIndex is an expression membership that is type-checked by function type_expression_member in Petr4 and type_expression_member calls the following helper for header stacks.

and type_expression_member_builtin env (ctx: Typed.ExprContext.t) tags typ (name: P4String.t) : Typed.Type.t =
  let open Typed.Type in
  let fail () =
    raise_unfound_member tags name.string in
  match typ with
  | Array { typ; _ } ->
     let idx_typ = Bit { width = 32 } in
     begin match name.string with
     | "size"
     | "lastIndex" ->
        idx_typ
     | "next"
     | "last" ->
        begin match ctx with
        | ParserState ->
           typ
        | _ -> failwith "can only use .last or .next within a parser"
        end
     | _ -> fail ()
     end
  | _ -> fail ()

While Petr4 limits next and last memberships to parser context, it does not do so for lastIndex.

P4 spec (v1.2.3)

Section operation on header stacks states that:
"hs.lastIndex: produces a 32-bit unsigned integer that encodes the index hs.nextIndex - 1. May only be used in a parser. If the nextIndex counter is 0, then evaluating this expression produces an undefined value."

Easy fix

This fix is easy, just add the check of context for lastIndex.

@pataei pataei added typechecker vs.P4 Petr4 limitation compared to P4 labels Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typechecker vs.P4 Petr4 limitation compared to P4
Projects
None yet
Development

No branches or pull requests

1 participant