You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Semantics of ZST dangling pointers: Kani differs from Miri #1574
Dangling pointers in Rust are considered valid by Miri as long as they are not null, aligned, not deallocated, not out of bounds (if they have the provenance of some allocation) and we don't read or write more than zero bytes from/to the memory location they point to. By contrast, CBMC considers reading and writing from/to such dangling pointers to be UB, for example in memcmp(..., ..., 0).
This was originally discovered in #1489 but the same problem could occur with other functions than memcmp, so we should probably figure out a better way to deal with it. Previous discussion:
However, I should also note that these are just the current rules in Miri, not some final decision about Rust semantics. There are several threads in the UCG related to this question, e.g. rust-lang/unsafe-code-guidelines#93.
Yes Kani handles wrapping offset very well, but it fails at detecting overflow for non-wrapping offset operators.
Kani doesn't have checks for out-of-bound pointer before dereferencing them.
I'd be curious about the peformance impact of adding those, but probably quite expensive :/
Interesting, but then you indeed end up doing oob checks on dangling pointers which causes spurious errors.
In Kanillian, I model ZST pointers as "any non-0 usize with the right alignment".
In the case of a ZST pointer, it means that the pointer could in theory have value (usize::MAX), and therefore offsetting it with any other value than 0 creates an out of bound pointer that is caught
I don't think that model is perfect, but it does capture oob arithmetics for ZSTs and non-zst pointers without false-negative.
However, that means I'm not tracking pointer provenance, which is unsound
Reading threads and issues about ZST pointer arithmetics is such a rabbit hole, I've been reading these for the last 2 hours hahahaha
It does feel a bit like a "unfortunately, we're based on LLVM" case
It does feel a bit like a "unfortunately, we're based on LLVM" case
Only in the sense that if we weren't based on LLVM I would advocate even more strongly in favor of making offset(0) just always allowed, and ZST derefs always allowed for aligned non-null pointers.
changed the title [-]Semantics of dangling pointers: Kani differs from Miri[/-][+]Semantics of ZST dangling pointers: Kani differs from Miri[/+]on Apr 5, 2024
Activity
RalfJung commentedon Aug 23, 2022
They must also not be OOB, if they do have the provenance of some allocation.
I wonder, how do you model Rust's
wrapping_offsetin CBMC? That also requires keep track of provenance for invalid pointers.RalfJung commentedon Aug 23, 2022
However, I should also note that these are just the current rules in Miri, not some final decision about Rust semantics. There are several threads in the UCG related to this question, e.g. rust-lang/unsafe-code-guidelines#93.
fzaiser commentedon Aug 23, 2022
@RalfJung Thanks, added
I don't know about
wrapping_offset. Maybe @danielsn knows more?zhassan-aws commentedon Aug 23, 2022
Our modeling of the
arith_offsetintrinsic (which is called bywrapping_offset) is here:kani/kani-compiler/src/codegen_cprover_gotoc/codegen/intrinsic.rs
Line 1040 in cae7aca
As the doc comment says, we currently don't check that the resulting pointer stays within bounds of the object.
RalfJung commentedon Aug 23, 2022
Right, indeed it is okay for it to leave the bounds of the object.
But when the pointer is actually used, then it must be in-bounds of the offset it started out from. So this is an interesting test for provenance.
zhassan-aws commentedon Aug 23, 2022
Kani supposedly handles the out-of-bounds pointer dereference correctly, e.g.:
And uncommenting the
let _vline results in:giltho commentedon Aug 24, 2022
Yes Kani handles wrapping offset very well, but it fails at detecting overflow for non-wrapping offset operators.
Kani doesn't have checks for out-of-bound pointer before dereferencing them.
I'd be curious about the peformance impact of adding those, but probably quite expensive :/
giltho commentedon Aug 24, 2022
Actually, it's maybe not that expensive, Kanillian supports it and will correctly fail on that example
celinval commentedon Aug 24, 2022
For CBMC to detect that case, we probably just need to explicitly add the boundary check after the offset call.
celinval commentedon Aug 24, 2022
I believe this issue is related to #763. We used to enable CBMC's offset checks, but we decided to disable them.
zhassan-aws commentedon Aug 24, 2022
Correct: running @giltho's example with:
correctly reports the issue:
giltho commentedon Aug 24, 2022
Interesting, but then you indeed end up doing oob checks on dangling pointers which causes spurious errors.
In Kanillian, I model ZST pointers as "any non-0 usize with the right alignment".
In the case of a ZST pointer, it means that the pointer could in theory have value (usize::MAX), and therefore offsetting it with any other value than
0creates an out of bound pointer that is caughtI don't think that model is perfect, but it does capture oob arithmetics for ZSTs and non-zst pointers without false-negative.
However, that means I'm not tracking pointer provenance, which is unsound
RalfJung commentedon Aug 24, 2022
FWIW there are some arguments that might make us change MiniRust to allow UAF and OOB pointers for zero-sized accesses.
giltho commentedon Aug 24, 2022
Reading threads and issues about ZST pointer arithmetics is such a rabbit hole, I've been reading these for the last 2 hours hahahaha
It does feel a bit like a "unfortunately, we're based on LLVM" case
RalfJung commentedon Aug 24, 2022
Only in the sense that if we weren't based on LLVM I would advocate even more strongly in favor of making
offset(0)just always allowed, and ZST derefs always allowed for aligned non-null pointers.Also see this discussion.
[-]Semantics of dangling pointers: Kani differs from Miri[/-][+]Semantics of ZST dangling pointers: Kani differs from Miri[/+]celinval commentedon Nov 6, 2024
I believe this can be closed since we have updated Kani to always accept ZST access as decided in this issue: rust-lang/unsafe-code-guidelines#472