Repository navigation
Closed
Description
Hello,
We are looking to solicit some feedback from the Kani Team for the following proposal
on future integration between Kani and CBMC.
Do let us know about any inaccuracies in the description of the status quo, or any clarifications/enhancements to the proposals.
Thank you,
Current Integration between Kani and CBMC
Bulk of code of interest is under kani/kani-driver. The following steps are traced from kani/kani-driver/src/main.rs.
- Translate
.rsfile into*.symtab.jsoncargo_buildinkani/kani-driver/src/call_cargo.rs
- Iterate through all the
*.symtab.jsonfiles produced in step 1, and convert them (link all of the symbol table files) into*.symtab.outby callingsymtab2gb.- In
symbol_table_to_gotocatkani/kani-driver/src/call_symtab.rs - The output at this level is (multiple?)
GOTO-binary
- In
- Link the binaries produced at step 2, into one
GOTO-binary.- In
link_goto_binaryatkani/kani-driver/src/call_goto_cc.rs. - The output at this level is a single
GOTO-binary.
- In
- Perform instrumentation (with
goto-instrument) run of file- But after a function has been set as the entry point - designated harness - by
goto-cc - In
run_goto_instrumentatkani/kani-driver/src/call_goto_instrument.rs - Perform
goto-modelvalidation, then perform series of instrumentations (drop unused functions, rewrite back edges, etc)
- But after a function has been set as the entry point - designated harness - by
- Kani runs
cbmcon binary (harness) and collects report- At
check_harnessinkani/kani-driver/src/main.rs check_harnesscallscbmcthrough call torun_cbmcofkani/kani-driver/src/call_cbmc.rs
- At
Considerations
- The Kani Team probably want something like a Rust <-> C++ bridge, kind of like
https://cxx.rs, so we can assume that they can call into the C++ functions.- This works by building the C++ and Rust files, along with the bridge. Demo: https://github.com/dtolnay/cxx/tree/master/demo
- For a first draft API, we probably need to make these calls easier to enter into.
goto_convertdemands exposing the concepts of asymbol_tablet,goto_modelt, andmessage_handler- Maybe not the
message_handler, looks like an implementation detail that could be hidden. - The
goto-binaryis effectively a serialised version of thegoto_modelt - Kani does have its own model of a CBMC symbol table at
kani/cprover_bindings/src/goto_program/symbol_table.rs
- Maybe not the
- In theory, linking with
goto-cccould be done programmatically, but at the moment too stateful (depends on state ofcompiletwhich depends on initial configuration done bygoto-gccpersonality). - However call to
goto-cc --functionmight be able to be skipped if a call toansi-c-entry-pointcan be made.- A bit hard, as for now it also depends on
configt, but in theory nothing binds it to it - could be refactored?
- A bit hard, as for now it also depends on
- Effectively,
CBMC::do_itis performing some initialisation that all leads to a selection of averifier, which is amultiorsingle-path-symex-checkert, then initialised with some options and the goto-model, and instructed to run, and report on the run results.- The initialisation depends on a
goto-programproduced throughget_goto_program, which in turn callsprocess_goto_program, which performs a number of transformations on thegoto-program, for instance removing inline assembly or skips, performing a reachability slice, etc. - Most of these transformations are also performed by
goto-instrument, but there are some problems:- Non-uniform interface between these transformation passes.
- Non-delineated boundaries between these transformations/unknown invariants between these different transformation passes.
- The initialisation depends on a
- There are two options for linking. Either calling the
linkingtclass, likesymtab2gbdoes, which operates on a symbol table level, or callinglink_goto_model, which operates at the level of two goto-models.
Proposals
Long-term Goal
- Standardisation of most operations around a
goto-modelt.cbmc,goto-instrument, etc., are going to be loading agoto-modeltin memory.- All transformations, analysis, linking, etc. will be working on the
goto-modelt.- Auxiliary functionality to translate a
symbol_tabletinto agoto-modelt(throughgoto_convertis also going to be exposed), to enable current producers ofsymbol_tablet(that now depend onsymtab2gb) to be able to easily convert into agoto-modeltfor further manipulation/transformations.
- Auxiliary functionality to translate a
- Subsequent transformations to the
goto-modelt(say, for instance,remove_skips) are going to be maintained as a versioned patch-set over the initial model.- This would allow rollbacks of transformations to be both possible and efficient in the future.
- This would also support scripting of the transformations and associated error handling without the need to start from scratch when a transformation has corrupted the goto-model.
- All tools standardise their interface around a
configandgoto-modelt.- This allows external tools to be able to invoke them with with a provided configuration and
goto-modelt.
- This allows external tools to be able to invoke them with with a provided configuration and
- This will allow CPROVER tools, to transition into being a library around
goto-modelton a long term basis.- This would allow users to write their own transformations/tools over
goto-modelt, both as parts of the CPROVER library, and as external tools.- The existing tools
cbmc,goto-instrument, etc, could then be repurposed to be thin CLI wrappers around the library.
- The existing tools
- External tools will be able to load
goto-modeltin the library, and extract a new one (along with analysis/verification reports, traces, etc.) from the new library.
- This would allow users to write their own transformations/tools over
- The clean-up of interfaces and internals of these tools will also offer the extra benefit of increased reliability/robustness for these tools when they are being used/invoked in binary form.
Step 1: API for invoking verification with CBMC
- We will start moving towards the ideals expressed at the Long-term Goal above, by attempting to refactor
cbmc(the binary) in a way that exposes a uniform verification interface.- Right now,
cbmc's entry point is the functiondoit, which for the most part does configuration management (by parsing command line arguments, etc) and loading/preprocessing of agoto-modeltwhich it then passes on a verifier (selected based on command line arguments). - Aim is to expose these verifiers on a more uniform interface, and make sure that they can be invoked on a
goto-modeltand aconfigtpassed to them, with no other dependencies.
- Right now,
- This allows us to deduplicate the main analysis engine from global state (which it now depends on, set up while the cbmc binary bootstraps) and make it easier to enter, both from within the CPROVER tools, and from external tools.
- At the end of this step, we will have exposed
cbmcin a way that enables programmatic access of the verification engine, instead of depending on the call to a binary version of it.
Step 2: API extended with goto-model transformations
- As part of the first step, we will have characterised the schema of the
goto-modeltrequired to be passed in to the verifiers.- This means that the minimum set of invariants required to analyse a
goto-modeltare going to be enforced before any analysis.
- This means that the minimum set of invariants required to analyse a
- This allows us to work backwards from that base, and start isolating other transformations on the
goto-modelt(for example, the ones provided bygoto-instrument) while always preserving the sequence of invariants demanded by these transformations, and ultimately, the verification engine.- We can also iron out the rough edges on their interfaces (make them uniform - right now some operate on the level of
goto-functions, others on the level ofgoto-modelt, there are differences in the arities of these functions, etc).
- We can also iron out the rough edges on their interfaces (make them uniform - right now some operate on the level of
- At the end of this step, we will have similarly exposed
goto-instrumentin a way that enables performing the instrumentations that it provides in a programmatic way, without needing to depend on multiple calls to the binary.
Step 3: API extended with compilation and linking
- After we can get all transformations to work and tools to be centred around a
goto-modelt, we can then move on adding construction ofgoto-modelts with compilation and linking supported by an API. - At the end of this step, we will have exposed the core functionality provided by
goto-ccso that it can be manipulated programmatically.
Step 4: API to enable programmatic construction of goto-modelt
- At this step, we have all we need to handle compilation/linking/transformations of existing
goto-modelts, so the next step is to provide an API that allows staged construction of agoto-modeltin a programmatic function.- The
goto-modeltis effectively a symbol table and a CFG (in the form ofgoto-functionst) - the API would offer the capability to create a newgoto-function, addgoto-instructionsto its body, and update the symbol table with the symbols it contains.
- The
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
NlightNFotis commentedon Jul 28, 2022
Tagging some people who might be interested in the above RFC:
@danielsn @tautschnig @feliperodri @jimgrundy
giltho commentedon Jul 28, 2022
Hi,
Writing this comment as an outsider, but it could be worth writing a C API (i.e just a thin C layer on top of the current code), instead of interfacing C++ with Rust directly.
It would enable this same API to be used with a lot more different languages, and therefore possibly also by other tools?
tautschnig commentedon Jul 28, 2022
This might require changing various classes to plain structs, and will prohibit exposing anything that uses the STL. Maybe those are blockers for Rust bindings anyhow, in which case we will have to reconsider this.
martin-cs commentedon Jul 29, 2022
@NlightNFotis this is a really well articulated plan but I feel like I am missing some context which will make it hard to C your RF. What problem are you solving? Is this performance? Is this having less code to maintain? Is this having more of the tool chain written in Rust? Is this having a more sophisticated workflow?
Some random, out of context, thoughts:
Before starting gnat2goto we looked at a lot of options of how to combine a front-end written in
!(C++)with the CPROVER code. That's whatsymtab2gbis. It was our best attempt at doing this. If you like I can try to dig out the notes from those meetings. We did consider the "wrap C++ in a foreign function interface" route but when you get into the details of C++ ABI stability and what that requires on the other language ... the horror was just too much. As @tautschnig wisely notes things like templated and the STL kinda ruin things. It /seemed/ like the best way of achieving that was as @giltho writing a shadow API in C, linking that to C++ and then writing an interface around that. But that requires writing and maintaining two interface layers and we figured it was easier to just implement one which did a very light serialisation and then write a simple de-serialiser.Another consequence of this discussion was identifying
goto-modeltas the pivotal interface. Our goal was to create these and hand them over to the C++. If you want to do a sequence of link and instrument and simplify and ... etc. then I would suggest writing that as a C++ tool and connecting at that level. Without knowing your context it is hard to say whether this is a good idea but it would reduce the API to just buildinggoto-modelt's which is a fraction of the code. Thegoto-modeltis the narrow point in the architecture diagram.( Although since then
goto-checkerhas been added and is generally a good abstraction of what the back-ends do. )@NlightNFotis said:
This sounds kinda scary. We have a very poor record of keeping patch-sets outside of develop alive.
This might be over-engineering. Transforms shouldn't be corrupting things. If they are you should throw it away and start again. See the massive discussion about "recovering from invariant failure" and the short answer of "don't; just start again.".
This is why I have been banging on about invariants and normal forms for goto-programs for years. It would be good to clarify what does and doesn't need to be done when creating a
goto-modelt.Yes! Absolutely! This was the original aim of the CPROVER project; to take CBMC and to split it up into a library of components and functions. This is why the box in the middle of the architecture diagram says "goto-programs". This is why the on disk format and the hand-over between tools are goto-function binaries. This is why there are loads of
.hfiles that just expose a one / a few C-style functions. This is why when they are well maintained (I struggle to callgoto-instrumentthis but it is a great example of the pattern), each of the tools is basically just handling command-line options, loading a goto-program and then calling a bunch of API functions. This was a great idea then and remains a great idea now. (Having a REPL or Python interface for calling the APIs black-box style would also be very exciting and possible with this model.)Minor point of slight pedantry. It is not a CFG. There is an interface to constructing those. It is a sequence of instructions. This is a different model and has some advantages and disadvantages.
Hope that is a useful C for your RF.
martin-cs commentedon Jul 29, 2022
Another thought. If you want a cross-language API to create
goto-modelts, either be serialisation or by wrapping, you need anexprtinterface. This is probably a bigger job than wrapping / interfacinggoto-modeltas it is very OO and so not in the "basically C" subset of C++ that is easy to interface to other languages. @smowton 's smart idea was to auto-generate it. I think his vision was to eventually auto-generatestd_expr.het al. along with bindings for other languages. There was a PR in CPROVER I think? Circa... 2016? 2017? The code he wrote definitely wound up in gnat2goto and is here:https://github.com/diffblue/gnat2goto/blob/master/irep_utils/src/irep_specs_to_ada.py
https://github.com/diffblue/gnat2goto/tree/master/gnat2goto/ireps/irep_specs
martin-cs commentedon Aug 1, 2022
@NlightNFotis If one of your goals is to simplify the interaction between Kani and CPROVER, one option would be to write a C++ program that does (Current Integration between Kani and CBMC) steps 2 to 5 inclusive in one program. This should be plugging together various existing APIs and should not be particularly long.
martin-cs commentedon Aug 1, 2022
#6495 may also be relevant
danielsn commentedon Aug 2, 2022
@NlightNFotis Would it be possible to give some (pseudocode) examples of what the initial API would look like, and what calls on it from an API consumer might look like?
danielsn commentedon Aug 2, 2022
There seem to be questions on two time-scales:
20 remaining items
danielsn commentedon Sep 27, 2022
Sounds like we're all in agreement about this: a failure enum sounds like a good solution here. If we're using Rust, there is an
Errortype which it could be conveniently translated into.NlightNFotis commentedon Oct 6, 2022
Hello, following up here on the first steps of the implementation of the Kani API.
We had an internal meeting yesterday where we ratified the following plan:
goto-model.C++, but the API we will expose to you will be inRust. That is, we will own theC++ -> Rustwrapped API, as that gives us more flexibility and control over some of the unknowns in the process, and allows us to tailor a better API experience for you (by simplifying the interface for you, allowing us to test the Rust side of things as well, or to pivot to a different implementation mechanism if need be).C++ -> Rustwrapper is going to be built withcxx.rs. We intend to take it as far as we can for now using that as the preferred implementation.There were some specific concerns that were raised in our last meeting, to which we have come up with some answers:
There is also a question that has been raised on our end that we would like an answer to:
feliperodri commentedon Nov 2, 2022
WIP: #7274
feliperodri commentedon Apr 6, 2023
@zhassan-aws did you have a chance to test the documentation of libcprover_rust? Should we close this issue?
zhassan-aws commentedon Apr 7, 2023
Yes, I reviewed the documentation. It looks good, but there are a few more APIs that are needed before we can fully rely on the crate for the Kani flow. More specifically, Kani makes the following calls to CBMC (Note that this has been updated recently, so is
slightly different than the steps described in the OP):
goto-ccon the goto binary produced by codegen and links the Kani C library, e.g.Then, for each harness in the Rust code, it performs the following steps:
goto-ccwith--function, e.g.goto-instrumentwith--add-library:goto-instrumentto replace empty functions and drop unused ones:goto-instrumentto transform loops:cbmc:We can close this issue as long as we have other issues to track adding this functionality to the API.
feliperodri commentedon Apr 8, 2023
Thank you @zhassan-aws! @NlightNFotis, could you create issues to track these requests and add the
kaniandaws-highlabels so we can track them as "maintenance"?NlightNFotis commentedon Apr 11, 2023
Hi Felipe, I will be adding new tickets based on what @zhassan-aws has outlined above.
In the meantime, there's also a ticket I had raise before, #7500
Are the steps there still valid, or should we close that in favour of the new tickets?
feliperodri commentedon Apr 11, 2023
@NlightNFotis let's close this one in favor of the new tickets.