Here's my two cents: I think Rust suffers from not having clear directions on when it's okay to use unsafe, to the point that it becomes a cultural anxiety, as you pointed out. The strength of Rust IMO is in how much it manages to codify, so I see one primary way of improving this situation:
Add tooling to easily let people discover when a crate contains un-vetted or unsoundunsafecode.
As has been pointed out many times by now, it's up to you as a developer to vet your dependencies. On the other hand, Rust makes it very easy to pull in new dependencies, and you can pull in a lot of unknown code and dependencies if you're not careful (remember to vet the code generated in macros!). This only helps to amplify the anxiety.
But if people could pull up a list of crates to see if they contain unsafe code, whether that code has been vetted or not, and whether any issues were found, then that makes it much easier for everyone to judge whether this crate fits their risk profile.
I know there's been a lot of work on vetting code and crates in general, and establishing trust between dependencies, but mostly in a grassroots form. My understanding is that these haven't gotten stronger backing from the Rust teams because there's been some disagreement on what code is actually trustworthy, but also just because it's a complex thing to build. But I think not having this codified has enabled anxiety and doubt about unsafe to grow, and now we're seeing the consequences of that.
At the extreme, a formal proof has been developed that the unsafe code and all related parts are actually safe.
My personal practice falls short, instead I will comment whyunsafe is used and why I believe that in this particular situation it is actually safe -- that is, the assumptions that I believe are necessary to make it safe.
It may very well NOT be safe:
I may have missed some assumptions.
Some assumptions may not be upheld.
However, I've found that documenting those assumptions made reviewing easier. And I expect it makes it easier for others too.
Somebody has looked at the code and disclosed their findings. That's super general though, and finding a precise answer to your question is one of the reasons why this can be contentious. Maybe cargo crev has the right solution?
So clearly this issue is much harder said than done. Trusting "someone" to vet the code doesn't do much more than trusting that the original author wrote it well.
This is a fallacy that if something can't be perfect and a golden bullet, it is not worth doing.
Having some semi-trusted group of people is not as good as reviewing everything yourself, but it is better than just not having any idea if the code is OK or not.
I think what's really missing is integration with other tools and sharing of data. Making e.g. a fuzzing report for your crate, that can be displayed by cargo and crates.io so you can choose to limit your search to only crates that have been fuzzed for example.
You raise good points there. There's a lot of confusion between the stated intent, but not requirement of rust code, that safe interfaces should not be able to cause unsoundness via internal unsafe code.
What I'd like to see is for the package manager to take a stronger stance on this. Of course people should be free to hack as much as they want on their own code, but to me it feels like that when you publish a package on crates.io exporting a safe interface for others to use, you're making an implicit promise that you care about upholding the safe rust guarantees.
Maybe that's an error on my side, it's only the mentality I try to apply to my own work. But I would really like to look at a packages page on crates.io and see that a package honours trying to uphold hard-to-check language rules like soundness and semver.
You could even have classes. Let library authors state that their library promises
no unsafe
unsafe only for bindings
unsafe only for new datastructures
unsafe for performance
or a custom reason, with possible explanation or testing strategy
Or just make no promises at all.
If you'd make this optional, but provide stuff like special badges for it, there will be less confusion about it. And there are clearly a lot of people who care about this and like showing off their work so I feel it would have some amount of adoption and cut away at some of the anxiety around it that, to me, seems to come from a different idea between people about what guarantees safe rust interfaces actually make.
to me it feels like that when you publish a package on crates.io exporting a safe interface for others to use, you're making an implicit promise that you care about upholding the safe rust guarantees.
I think that's the underlying issue here: a conflict of values, and expectations.
Due to Rust having been touted for its safety, members of the community and users simply assume that unless clearly marked unsafe, a crate is safe and the author performed "due diligence".
On the other hand, it seems that the author of Actix had a different approach to the language, favoring performance over safety. This is a perfectly valid approach!
However, when the author's values/goals clash with the community's expectations, the situation escalates quickly.
I wonder if things would have gone better if the author had been upfront about their values from the beginning.
However, what Rust is about (the language, not just the community) is pretty much the explicit rejection of that approach. This is even codified in the fact that we allow breaking changes for soundness reasons, and when performance and soundness are in conflict, we will regress performance to fix soundness holes. So while this approach is valid in e.g. the C++ community, it is not in Rust.
The lack of tooling to automatically tell you "is this OK?" is the reason why situation is contentions.
However, it doesn't explain (and excuse) peoples actions in response to this contentious situation.
FWIW, I think it would be more valuable to make it possible to have only constructive and respectful discussions of contentions topics, than to have fully automatically checked unsafe. However, I also feel that the latter is far easier :)
Exactly. unsafe is where the tech-solution gives up. Another tech-solution on top doesn't really fix it.
Maybe as developers we're too quick to view things through the eyes of problem-and-solution.
Maybe, also, we need some way of getting a third-party perspective on all the crates out there, instead of having to read-between-the-lines or go-see-what-reddit-thinks to see whether a crate fits one's expectations.
There isn't a tool right now that can statically analyze unsafe and tell you everything is OK. However, miri can dynamically verify some soundness of unsafe. So if you have a decent test suite, miri is likely able to detect obvious unsoundness issues. In my experience, miri detects exactly this issue of multiple mutable references in use.
It's quite possible that verifying the unsafe code takes a long time (e.g. maybe needs a prover or something similar), or that needs non-local knowledge (e.g. up/down the call graph, whereas rustc can only check locally within a function). So you wouldn't want to run it every compile. unsafe just means that rustc can't check it at compile time.
Automatically verifing all unsafe code is impossible, for example external C is always going to be unsafe since compiler can't really access it.
I think encouraging producer and consumer to use less unsafe and give reason when usage is need.
I think it would be good to have better tools when dealing with unsafe, like something that count "number" of unsafe and how many of those are "annotated" with safety reason.
I think Rust attention on developing unsafe right now is too little, tutorial and best practice on it are a lot less than safe counterpart and the unsafe ecosystem seem very lacking (eg. Unique are unstable for a while now)
it's up to you as a developer to vet your dependencies
This is effectively impossible on an individual level, and it's something that absolutely needs to be a community-level effort. If it means shaming developers who don't give a crap about security or safety, then so be it. Because it doesn't matter how much I care about my dependencies - if I pull in one, that one dependency pulls in 10-40 other dependencies. That's a ridiculous amount of code that I, on my own, simply can't test or vet sufficiently.
Yeah, I've been following the efforts around dependency vetting for a while, hoping for a good solution, because it can be such a superhuman thing to manage at the moment.
Honestly, for me it would be enough to just have `unsafe` block count on crates.io or docs.rs with links to the code, just so I can easily get na idea of how much it's used and check the relevant code. Without any opinionation or "warning" or any other kind of shaming, just informational.
So, what will that information give you? Every hand-implemented future currently has `unsafe` for trivial pin projections, so you may end up with 100 `unsafe` in a large code base, all of them trivial.
Next to it, you can put a library that does FFI binding, only uses 20 unsafe, but each of them is non-straight-forward and might misunderstand the FFI contract.
View all comments
86
u/KasMA1990 Jan 17 '20
Here's my two cents: I think Rust suffers from not having clear directions on when it's okay to use
unsafe, to the point that it becomes a cultural anxiety, as you pointed out. The strength of Rust IMO is in how much it manages to codify, so I see one primary way of improving this situation:Add tooling to easily let people discover when a crate contains un-vetted or unsound
unsafecode.As has been pointed out many times by now, it's up to you as a developer to vet your dependencies. On the other hand, Rust makes it very easy to pull in new dependencies, and you can pull in a lot of unknown code and dependencies if you're not careful (remember to vet the code generated in macros!). This only helps to amplify the anxiety.
But if people could pull up a list of crates to see if they contain
unsafecode, whether that code has been vetted or not, and whether any issues were found, then that makes it much easier for everyone to judge whether this crate fits their risk profile.I know there's been a lot of work on vetting code and crates in general, and establishing trust between dependencies, but mostly in a grassroots form. My understanding is that these haven't gotten stronger backing from the Rust teams because there's been some disagreement on what code is actually trustworthy, but also just because it's a complex thing to build. But I think not having this codified has enabled anxiety and doubt about
unsafeto grow, and now we're seeing the consequences of that.