News for hackers. Stuff that matters.
Re:CBMC: C bounded model checker (2021) by quotemstr on Saturday May 04, 2024 @11:48AM (#40258500)
Wouldn't this project be more usefully done at the LLVM IR level so we could plug in any existing LLVM frontend and gain full support for language features like C++ templates?
Re:CBMC: C bounded model checker (2021) by SnowflakeOnIce on Saturday May 04, 2024 @12:13PM (#40258614)
It predates LLVM. That's one reason, anyway.
Re:CBMC: C bounded model checker (2021) by flohofwoe on Saturday May 04, 2024 @11:55AM (#40258536)
My guess is that LLVM IR preserves too little semantic information to implement such a feature.
Re:CBMC: C bounded model checker (2021) by rjmunro on Saturday May 04, 2024 @12:30PM (#40258698)
I think the LLVM level doesn't work as it throws away too much semantic information, but much of the CLANG front end can be used via it's API.

There is a fork of CBMC called ESBMC that does this and many other changes: https://ssvlab.github.io/esbmc/documentation.html

Re:CBMC: C bounded model checker (2021) by quotemstr on Saturday May 04, 2024 @12:37PM (#40258740)
I'm curious about what relevant details get thrown away in translation to LLVM IR and that we can't add back with annotations. It's a shame that it's not possible to use these analysis tools on modern C++ codebases.
Re:CBMC: C bounded model checker (2021) by rocqua on Saturday May 04, 2024 @01:58PM (#40259221)
Undefined behavior around void pointers springs to mind.
Re:CBMC: C bounded model checker (2021) by nickpsecurity on Saturday May 04, 2024 @01:02PM (#40258913)
Another problem with LLVM I’ve heard about is that it’s intermediate language or API or something is a moving, informally-specified target. People who know LLVM internals might weigh in on that claim. If true, it’s actually easier to target C or a subset of Rust just because it’s static and well-understood.

Two projects sought to mitigate these issues by going in different directions. One was a compiler backend that aimed to be easy to learn with well-specified IL. The other aimed to formalize LLVM’s IL.

http://c9x.me/compile/

https://github.com/AliveToolkit/alive2

There have also been typed, assembly languages to support verification from groups like FLINT. One can also compile language-specific analysis with a certified to LLVM IL compiler. Integrating pieces from different languages can have risks. That (IIRC) is being mitigated by people doing secure, abstract compilation.

Re:CBMC: C bounded model checker (2021) by sweetjuly on Saturday May 04, 2024 @11:45PM (#40262113)
Yes, the textual IR format is not an API. It's an internal implementation detail which changes somewhat rapidly. It's well specified but there's no guarantees that if you write some IR which works in LLVM 16 that it'll work as expected in LLVM 17, etc. Inside of LLVM itself IR and its related manipulation and analysis functions are "API" in so far as there's an expectation that if you change how the IR works that you won't break existing in-tree passes/that you'll work with maintainers to fix them.
Re:CBMC: C bounded model checker (2021) by nickpsecurity on Saturday May 04, 2024 @11:52PM (#40262133)
That makes more sense. Thanks!
Re:CBMC: C bounded model checker (2021) by tkz1312 on Saturday May 04, 2024 @02:11PM (#40259291)
klee is a similar tool that operates on llvm bytecode: http://klee-se.org/
Re:CBMC: C bounded model checker (2021) by xvilka on Saturday May 04, 2024 @11:38PM (#40262088)
It doesn't work on real software though. Anything non-trivial.
Re:CBMC: C bounded model checker (2021) by kldx on Sunday May 05, 2024 @12:42PM (#40266163)
VCLLVM was presented at ETAPS 2024. The goal is to compile both languages and specifications to LLVM IR.
Re:CBMC: C bounded model checker (2021) by IshKebab on Saturday May 04, 2024 @12:37PM (#40258738)
I've used this before. It's pretty great. Also used as the backend for Kani I believe (Amazon's Rust formal verification tool).
Re:CBMC: C bounded model checker (2021) by PartiallyTyped on Saturday May 04, 2024 @01:43PM (#40259132)
This is achieved by latching on to rustc, and then compiling to gotoc, which is then used with CBMC.

Removed due to my misunderstanding:

~~I don't think Kani counts as a "formal" verification tool, emphasis on "formal", but it is a verification tool for rust code and is powered by CBMC.~~

Re:CBMC: C bounded model checker (2021) by IshKebab on Saturday May 04, 2024 @02:04PM (#40259254)
I'm not sure what you're implying. Kani absolutely is formal verification. CBMC stands for C Bounded Model Checker and model checking is one of the main forms of formal verification.
Re:CBMC: C bounded model checker (2021) by PartiallyTyped on Saturday May 04, 2024 @02:15PM (#40259311)
Edit:

Seems like I misread the documentation...

~~What I was implying is that Kani just can't figure out all of the paths;~~ ~~Consider this example from Kani documentation [1]:~~

    fn estimate_size(x: u32) -> u32 {
       if x < 256 {
            if x < 128 {
                return 1;
            } else {
                return 3;
            }
        } else if x < 1024 {
            if x > 1022 {
                panic!("Oh no, a failing corner case!");
            } else {
                return 5;
            }
        } else {
            if x < 2048 {
                return 7;
            } else {
                return 9;
            }
        }
    }
~~Kani is very unlikely to find x=1023.~~

https://model-checking.github.io/kani/tutorial-first-steps.h...

Re:CBMC: C bounded model checker (2021) by accelbred on Saturday May 04, 2024 @02:20PM (#40259340)
Kani will find 1023 unless you constrain the input. The link you posted is saying a property test is unlikely to find it but Kani will find it.
Re:CBMC: C bounded model checker (2021) by IshKebab on Saturday May 04, 2024 @03:24PM (#40259707)
Yeah you misread that. Read it again, it's saying normal fuzzing is unlikely to find it but Kani finds it instantly.
Re:CBMC: C bounded model checker (2021) by PartiallyTyped on Saturday May 04, 2024 @03:28PM (#40259732)
Oh this is embarrassing.
Re:CBMC: C bounded model checker (2021) by mrnoone on Saturday May 04, 2024 @04:55PM (#40260208)
Some other tool (Coq-based) to formally verify unsafe Rust https://gitlab.mpi-sws.org/lgaeher/refinedrust-dev
Re:CBMC: C bounded model checker (2021) by PartiallyTyped on Saturday May 04, 2024 @01:41PM (#40259120)
CBMC also powers Kani <3
Re:CBMC: C bounded model checker (2021) by ranger_danger on Saturday May 04, 2024 @02:38PM (#40259450)
How does this compare to scan-build, clazy etc.?
Re:CBMC: C bounded model checker (2021) by IshKebab on Saturday May 04, 2024 @03:40PM (#40259800)
Those are different kinds of tools. Clazy is pretty much a linter. Scan-build is a traditional static analyser. It tries to find bugs or possible bugs but it's not trying to disprove the presence of bugs.

CBMC is a formal verification tool which tries to prove that there aren't any bugs (where "bugs" means things like UB).

Re:CBMC: C bounded model checker (2021) by xxmarkuski on Saturday May 04, 2024 @04:24PM (#40260048)
CBMC ist pretty cool, I've used it in my bachelor's thesis for verification of fairness properties in proportional voting. Another use case I've seen is using CBMC in multiparty computation. What you have there is methods that work on boolean circuits. Flange CBMC in front of this and you got multiparty computation of C code.
Re:CBMC: C bounded model checker (2021) by nanolith on Saturday May 04, 2024 @05:20PM (#40260372)
I have been using CBMC to formally verify C for six, make that seven years now (edit: time flies). The latest release is actually pretty good.

The secret to model checking is to understand that you're building and verifying a specification in code. It works best if specifications are built function by function. For each function, I'll build "shadow functions" that simplify any functions it calls with a range of potential non-deterministic behavior that matches that function's contract. In this way, formal specifications can be built from the ground up by first verifying a lower-level function and then replacing it with a shadow function that mimics its behavior in a non-deterministic but SMT solver friendly way.

There are things that model checking doesn't do well. Anything with recursion or looping that can't be easily broken down will not perform well with a model checker. While algorithms like balanced binary trees or sorting can be broken down in a way that can be model checked, other algorithms may require the sort of induction one gets with constructive proofs to sufficiently verify. There is an art to this which will lead to style changes in C and a slightly different way of building up functions from pieces that can be trivially model checked. Honestly, that's not a bad thing.

For the parts that can't be easily model checked, I'm working on a C parser library that can be formally verified, which will import C directly into Coq or Lean. I consider that the "last mile" before formal methods in firmware and system software is to the point that it's generally available.

Re:CBMC: C bounded model checker (2021) by zzz95 on Saturday May 04, 2024 @07:28PM (#40261131)
I am very curious about what kind of industry domains lets you have this kind of fun? Unless of course, you are in academia.
Re:CBMC: C bounded model checker (2021) by rurban on Sunday May 05, 2024 @01:45AM (#40262558)
Not fun, a serious tool, and extremely easy to use, unlike other formal methods. It works with your source code, not on some rewritten abstraction of it.

Embedded, automotive, space use it regularly.

I've setup cbmc and goto-analyzer for string searching algos here https://github.com/rurban/smart and really found some bugs in old published algos.

Re:CBMC: C bounded model checker (2021) by nanolith on Sunday May 05, 2024 @05:45PM (#40268760)
If the tool exists and has minimal overhead, I don't think it is a matter of permission but a matter of necessity. CBMC adds about 30% overhead over just unit testing, in my experience. It does require a different idiomatic programming style to use correctly, and there is a learning curve. Some intuition is required to learn how to use the tool effectively and efficiently, but this can be taught.

For system programming or firmware, it's incredibly useful. I've used this tool in conjunction with threat modeling and attack surface minimization to significantly improve the safety and security of software written in C. At this point, I would not consider working on any C project that did not make use of this tool.

Re:CBMC: C bounded model checker (2021) by tooltower on Sunday May 05, 2024 @01:29AM (#40262508)
> [...] other algorithms may require the sort of induction one gets with constructive proofs to sufficiently verify. There is an art to this which will lead to style changes in C and a slightly different way of building up functions from pieces that can be trivially model checked.

Is there a book or article that talks more about this? I.e. how to write code in a way that is more amenable to model-checking (bounded or otherwise)?

Re:CBMC: C bounded model checker (2021) by nanolith on Sunday May 05, 2024 @06:17AM (#40263658)
Unfortunately, I have not found any good tutorials. If it helps, I plan on writing one very soon.
Re:CBMC: C bounded model checker (2021) by drdrey on Sunday May 05, 2024 @06:37PM (#40269236)
where can we follow you to hear more about this?