๐ ๏ธWork in progress๐ ๏ธ
MIR is the mid-level internal representation of Rust program in rust compiler.
The KMIR
project defines MIR semantics formally in K and attempts build a scalable testing and verification tool for real world Rust programs.
- Install the
kup
package manager:bash <(curl https://kframework.org/install)
- install the latest version of KMIR.
kup install kmir
You can refer to kup -h
for more options. For example,
kup list kmir
: list available KMIR versions.kup install kmir --version 366f8fb
: install a paricular version of KMIR.
At the current stage, KMIR
is to be used as a command-line tool. We list the main functions in the following, while users are recommended to explore the latest options using kmir -h
or more options of a command using kmir {cmd} -h
.
The kmir parse
command invokes the parser generated by K and outputs a KAST representation of MIR abstract syntax parsed from a MIR file.
For example,
kmir parse demo/assert-true.mir
The kmir run
command will interpret a MIR program according to the predefined operational rules of MIR
. This command invokes the LLVM interpreter generated by K and execute the MIR code in the file.
For example,
kmir run demo/assert-true.mir
(Under development, command interface unstable )
KMIR provides the command kmir prove
to prove the claims in the provided specification file.
kmir prove demo/simple-spec.k
This proof results are by default stored in the same directory as the spec-file
. User could specify a specific directory using the flag --save-directory
.
kmir show-proof SIMPLE-SPEC.statement.1 --proof-dir demo
kmir view-proof SIMPLE-SPEC.statement.1 --proof-dir demo
Using the following command to clone this repository, including its submodules:
git clone --recurse-submodules git@github.com:runtimeverification/mir-semantics.git
Prerequsites: python 3.8.*
, pip >= 20.0.2
, poetry >= 1.3.2
.
Users are referred to the tool's installation page for more details:
- Installing Python
- Installing
pip
: It usually comes with Python installation. - Installing
poetry
The build system of KMIR
is a mixture of poetry
+ kbuild
+ make
:
poetry
handles Python dependencies, see pyproject.toml for Python-related configurationkbuild
handles K dependencies and build targets, see kbuild.tomlmake build
ties the two together
cd kmir
make build
source set_env.sh
pip install dist/*.whl
Alternatively, you can also install kmir
locally and running it with poetry
.
poetry install
In a local installation, we suggest calling kmir
via Poetry's poetry run
command. This ensures that you are calling the feshly built kmir
. For example:
poetry run kmir parse --definition-dir $(kbuild which llvm) ../demo/assert-true.mir --output pretty
Alternative, you can spawn a one-off shell that will allow you to interact with kmir
directly by executing the command
poetry shell
In the shell, execute kmir as
kmir parse --definition-dir $(kbuild which llvm) ../demo/assert-true.mir --output pretty
Additionally, the Haskell
build needs to be supplied when calling kmir prove
. For example,
poetry run kmir prove --definition-dir $(kbuild which llvm) --haskell-dir $(kbuild which haskell) --spec-file src/tests/integration/test-data/prove-rs/simple-spec.k
Note: Since
kmir
is just a thin Python script and does not do any heavy-lifting itself, it needs access the K definition of MIR. Thekbuild
tool handles compiling the K code and provides awhich
command to output the path to the compiled definition, which we give tokmir
. We also provide a script,set_env.sh
to set the path to the definitions,KMIR_LLVM_DIR
andKMIR_HASKELL_DIR
, globally. Consequently, the--definition-dir
can be omitted. Run the script as
source set_env.sh
We provide a Docker image for isolated testing, locally and in CI. NOTE: Mac Silicon hardware not supported.
From the root of the repository:
- Build the docker image (the
./deps/k_release
file pins the K version):
docker build . --tag kmir-tests --build-arg K_COMMIT=$(cat deps/k_release) --file .github/workflows/Dockerfile
- Run the integration tests in a container:
docker run --name kmir-container --rm --interactive --tty --detach --workdir /home/user kmir-tests &&
docker cp . kmir-container:/home/user &&
docker exec kmir-container chown -R user:user /home/user &&
docker exec --user user kmir-container make -C kmir test-integration
Note: you may need to run the docker
commands with sudo
, or start a superuser shell with sudo -s
.
We use a similar workflow in CI actions defined in the .github/
directory.
kmir
is a Python package and CLI tool that leverages the pyk
library to provide a Python interface for the K semantics. While the K semantics can be also used directly with K framework, the kmir
tool makes it more accessible for people not familiar with K.
The Python source code of the kmir
tool and library resides in kmir/src/
. The entry point to the tool is src/kmir/__main__.py
.
Use make
to run common tasks (see the Makefile for a complete list of available targets).
make build
: Build wheelmake check
: Check code stylemake format
: Format codemake test-unit
: Run unit testsmake test-integration
: Run integration testsmake test-integration TEST_ARGS="--update-expected-output"
: Run integration tests, updating expected results forshow-proof
tests
The K source code of the semantics resides in k-src
.
Working on the semantics roughly comprises the following steps:
- Pick a MIR/Rust program as a running example, or a several of them
- Modify K files
- Rebuild the K definition
- Testout the new definition on the running example
- Running integration tests
However, from step 3, the following alternative steps avoids rebuilding from scratch:
- Run
poetry run kbuild kompile llvm
to re-kompile the semantics - Run
poetry run kbuild kompile haskell
additionally if you are working on a proof - Test the running example with a targetted
kmir <command> <path to example.mir>
- Once happy with the step 3 goes, run a part of the integration tests by calling:
Modify the command as necessary to include the tests you want.
TEST_ARGS="-k test_handwritten" make test-integration-{parse, run, prove etc.}
- Once happy with the step 4 goes, run the complete integration test suite:
This time, do not include the
make test-integration-run
--kbuild-dir
option to re-kompile everything in a temporary directory.
Make sure the manual is updated with your new feature, the integration tests passed and the nix tests output updated in case there is K definition changes.
When there are changes to the K definition of MIR, the nix
tests should be re-executed to update the expected output. We provide a Makefile
to make
this update easy.
Before running the tests, it would expect a latest built kmir and kbuild
DIR's being updated (see the note above on the set_env.sh
script).
cd src/tests/nix
make
- You may refer to K tutorial to learn more about using K.
- MIR language reference