-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for unsafe operations that Creusot can support #1379
Comments
I see no issue adding support for those! in fact it should really just be a matter of writing down the specifications and adding them to the standard library. Do you have a specific use case / set of methods in mind? |
For the string methods what specifications do you expect? Those methods have tricky safety obligations relating to the utf-8 validity of the contents. |
Worst case for those methods, I can give you the code to add those specifications locally to your project. |
Good question. I actually need much simpler (and stronger) specifications than the real ones. For // Requires that input is ASCII, which is trivially UTF-8.
#[requires(forall<i: Int> i < v@.len() ==> v@[i]@ < 128)]
// Ensures that it's the identity function.
#[ensures(result@.len() == v@.len())]
#[ensures(forall<i: Int> i < v@.len() ==> result@[i]@ == v@[i]@)]
pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str; A first issue here is that For #[ensures(
// Requires that only ASCII is pushed (in particular the prefix is not modified).
((*result)@.len() <= (^result)@.len()
&& forall<i: Int> i < (*result)@.len() ==> (^result)@[i]@ == (*result)@[i]@
&& forall<i: Int> (*result)@.len() <= i && i < (^result)@.len() ==> (^result)@[i]@ < 128)
==>
// Ensures that it's the identity function (trying to avoid talking about UTF-8).
((^self)@.len() == (*self)@.len() + (^result)@.len() - (*result)@.len()
&& forall<i: Int> i < (*self)@.len() ==> (^self)@[i]@ == (*self)@[i]@
&& forall<i: Int> (*self)@.len() <= i && i < (^self)@.len() ==>
(^self)@[i]@ == (^result)@[(*result)@.len() + i - (*self)@.len()]@)
)]
pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8>; A second issue here is that the function is unsafe but doesn't have requirements. As far as I can tell, the requirements can't be expressed in Creusot, since it's about the prophecy of the result, which is not in scope for
Yes, given that those contracts are stronger than the correctness contracts of those functions, I guess it makes sense to have them local to my project. |
This is what we refer to as "after_expiry" or a pledge. Tagging @arnaudgolfouse since we talked about this last week. |
I think that for this to work the |
To axiomatize the string methods you just need to add an
|
Thanks! I guess this is work in progress. I don't see any mention of
I'm not sure what you mean. This requirement is about ASCII, which is a subset of Unicode where the UTF-8 encoding is the same as the ASCII encoding, and ASCII values are also the same as their Unicode scalar values. So once
Thanks! I'll try this when I get time. |
What I meant, is that if we simply add an uninterpreted |
That may or may not be a problem for you at this juncture, but if we need to ensure that literal chars have accurate |
Ah ok, I misunderstood "literal". Indeed, it's not a problem for me. The project doesn't use any literal chars. It actually doesn't use any chars at all (they only come from the model of |
Creusot probably can't support unsafe operations with requirements mentioning the memory or aliasing model, but it should be able to support purely arithmetic safety requirements. For example
slice::get_unchecked()
,u64::unchecked_add()
, orString::from_utf8_unchecked()
. A related closed issue is #36 which actually mentionsget_unchecked
as something that could be soundly supported.This issue is about whether such unsafe operations would be something Creusot wants to support.
The text was updated successfully, but these errors were encountered: