-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
New lint: Sync types which are generic over T, but T is not Send #6638
Comments
I've asked in
|
This is a weird example since it would get the trait automatically anyway, but that would have a |
Other exceptions include |
It seems to me that the synchronization bugs reported to RustSec DB are mostly the violation of "Send types which are generic over T, but T is not Send" and "Sync types which are generic over T, but T is not Sync." |
I support the general idea of having a lint that warns against unbounded Sync/Send implementations (this mistake was even found in the Rust official futures library), but the exact bound & how we detect them may require more discussion. |
While I filed this with a somewhat prescriptive description, I'm certainly not wedded to the set of constraints/suggested fixes I described. My real interest is in finding ways to capture the set of issues that we've seen really prevelantly in rustsec. |
I think it only makes sense to suggest bounds if the type is only used in fields that could have contributed to an automatic implementation, but something else forces this to be a manual implementation overall. struct Foo<T> {
raw: *const u8, // blocks auto-`Send`/`Sync`
value: T, // suggest `T: Sync for Sync`, `T: Send for Send`
}
struct Bar<T> {
raw: *const u8, // blocks auto-`Send`/`Sync`
value: &'static T, // suggest `T: Sync` for both `Send` and `Sync`
}
struct Baz<T> {
raw: *const T, // we can't guess the thread-safety here
} |
+1 to what @alex just said. We just saw a huge number of this class of vulnerability reported to RustSec, and as I was watching that I was wondering if there is some way to catch this class of error more automatically. Perhaps it'd be good to make a small summary post which includes a select set of specific examples of the vulnerabilities. |
FWIW one of the reporters of these issues has indicated that their team intends to OSS the analysis and port it to clippy: rustsec/advisory-db#807 (comment) |
What it does
Catches un-soudness due to implmenting
Sync
for a type that's generic overT
, butT
is notSend
. This often leads to unsoundness by allowing arbitrary types to be shared across threads.It should suggest adding a
T: Send
bound as a solution.Categories (optional)
clippy::correctness
(potentially alsoclippy::pedantic
because this may have false positives)What is the advantage of the recommended code over the original code
It is sound :-) This is (unfortunately) a relatively common issue, as you can see by going to https://rustsec.org/advisories/ and searching "Bound". Almost all of those issues have this root case. (There's even more that are currently PRs, awaiting review to become advisories: https://github.com/RustSec/advisory-db/pulls)
Drawbacks
May have false positives.
Example
Could be written as:
The text was updated successfully, but these errors were encountered: