A mechanized formal specification for the P4 programming language, using the SpecTec framework. This reuses parts of the Petr4 codebase, especially the parser and numerics implementation.
-
Install
opamversion 2.0.5 or higher.$ apt-get install opam $ opam init
-
Create OCaml switch for version 5.1.0 Install
duneversion 3.16.1,bignumversion v0.17.0,menhirversion 20240715,coreversion v0.17.1,core_unixversion v0.17.0, andbisect_ppxversion 2.8.3 viaopam.$ opam switch create 5.1.0 $ eval $(opam env) $ opam install dune bignum menhir core core_unix bisect_ppx yojson ppx_deriving_yojson
$ make build-specThis creates an executable p4spectec in the project root.
You may also need libgmp-dev and pkg-config, if the error message says so.
$ ./p4spectec elab spec/*.watsupCurrently, the P4-SpecTec specification defines the static semantics of P4. Given a P4 program, below command runs the typing rules of the P4 language.
$ ./p4spectec run-sl spec/*.watsup -i p4c/p4include -p [FILENAME].p4$ ./p4spectec testgen spec/*.watsup -i p4c/p4include -seed [SEED DIR] -gen [GEN DIR] -fuel [NUM]This will generate P4 programs in the directory [GEN DIR] using the seed files in the directory [SEED DIR].
[NUM] is the number of fuzz cycles to run.
After the fuzz loop, you may find the generated P4 programs in the directory [GEN DIR], with the log file fuzz.log,
query files for mutations query.log and an initial coverage file boot.cov.
In later runs with the same seed directory, you can use warm boot to skip the pre-loop phase.
$ ./p4spectec testgen spec/*.watsup -i p4c/p4include -seed [SEED DIR] -gen [GEN DIR] -fuel [NUM] -warm [COV FILE]At a high level, the fuzz loop will:
To see what values are derived from a given P4 program and a phantom id, run:
$ ./p4spectec testgen-dbg spec/*.watsup -i p4c/p4include -p [FILENAME].p4 -pid [PID] -debug [DEBUG DIR]
P4-SpecTec is an open-source project. Please feel free to contribute by opening issues or pull requests.
P4-SpecTec is released under the Apache 2.0 license.