-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Insert checks for enum discriminants when debug assertions are enabled #141759
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
base: master
Are you sure you want to change the base?
Conversation
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt This PR changes MIR cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @vakaras Some changes occurred in compiler/rustc_codegen_ssa Some changes occurred in compiler/rustc_codegen_cranelift cc @bjorn3 Some changes occurred to the CTFE machinery rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead. cc @rust-lang/rust-analyzer |
This comment has been minimized.
This comment has been minimized.
6d3fe75
to
a7dd718
Compare
This comment has been minimized.
This comment has been minimized.
a7dd718
to
4f3342e
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
54b6e74
to
b03960e
Compare
This comment has been minimized.
This comment has been minimized.
b03960e
to
228b656
Compare
This comment has been minimized.
This comment has been minimized.
228b656
to
d1d8f88
Compare
This comment has been minimized.
This comment has been minimized.
d1d8f88
to
93b24d7
Compare
This comment has been minimized.
This comment has been minimized.
93b24d7
to
c2a8415
Compare
This comment has been minimized.
This comment has been minimized.
c2a8415
to
d769d6b
Compare
This comment has been minimized.
This comment has been minimized.
d769d6b
to
68665ad
Compare
This comment has been minimized.
This comment has been minimized.
68665ad
to
1225079
Compare
This comment has been minimized.
This comment has been minimized.
1225079
to
c52f534
Compare
33890a1
to
c871c4c
Compare
Similar to the existing nullpointer and alignment checks, this checks for valid enum discriminants on creation of enums through unsafe transmutes. Essentially this sanitizes patterns like the following: ```rust let val: MyEnum = unsafe { std::mem::transmute<u32, MyEnum>(42) }; ``` An extension of this check will be done in a follow-up that explicitly sanitizes for extern enum values that come into Rust from e.g. C/C++. This check is similar to Miri's capabilities of checking for valid construction of enum values. This PR is inspired by saethlin@'s PR rust-lang#104862. Thank you so much for keeping this code up and the detailed comments! I also pair-programmed large parts of this together with vabr-g@.
This comment has been minimized.
This comment has been minimized.
c871c4c
to
5587fd7
Compare
@bors2 try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
Insert checks for enum discriminants when debug assertions are enabled Similar to the existing null-pointer and alignment checks, this checks for valid enum discriminants on creation of enums through unsafe transmutes. Essentially this sanitizes patterns like the following: ```rust let val: MyEnum = unsafe { std::mem::transmute<u32, MyEnum>(42) }; ``` An extension of this check will be done in a follow-up that explicitly sanitizes for extern enum values that come into Rust from e.g. C/C++. This check is similar to Miri's capabilities of checking for valid construction of enum values. This PR is inspired by saethlin@'s PR #104862. Thank you so much for keeping this code up and the detailed comments! I also pair-programmed large parts of this together with vabr-g@. r? `@saethlin`
This patch is finally ready for review! Let's see what the perf-impact of this is, but I wouldn't assume it is much, as this only emits checks for transmutes to enums. |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (7488b2b): comparison URL. Overall result: no relevant changes - no action neededBenchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf. @bors rollup=never Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)Results (secondary 3.9%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (secondary -0.4%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 754.321s -> 757.215s (0.38%) |
))), | ||
}); | ||
|
||
// Loop over the list of the discriminants and insert checks for equality. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this can be a very large amount of additional MIR. I worry about things like the transmute from usize
to ptr::Alignment
, for example -- adding another, what, at least 128 MIR statements every time that happens?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I was already thinking about this... my perception was that such enums are not super prevalent, but ptr::Alignment
does look relevant.
One idea I've had was that most enums have contiguous range (ptr::Alignment
is a bad example :/) and we could transform this list to a list of WrappingRange
s and then compare them. As I said that wouldn't work for ptr::Alignment
though...
We could also think about excluding everything with more than e.g. 10 variants and have an option to opt-in for all checks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
every time that happens?
I'm not sure it happens very often. Such transmutes are usually done in a helper function, so they should not be inserted very many times in a debug build.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the perf results suggest that it does not happen very often. At least currently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe to give some background: In the whole standard library compiled for MacOS I saw one (!) such check being emitted. For Linux it's two, so all-in-all I wouldn't believe that this is very prevalent.
This may change once we extend the check to e.g. reads through a union or pointer, but it still shouldn't be super often I assume.
// An empty enum that tries to be constructed from an inhabited value, this | ||
// is never correct. | ||
Variants::Empty => { | ||
// The enum layout is uninhabited but we construct it from sth inhabited. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this case already detected statically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it is. I was wondering whether I should put it here as it already creates a warning. But I thought that it would be good to have it for the sake of completeness. I am happy to remove it if you think that's better.
Similar to the existing null-pointer and alignment checks, this checks for valid enum discriminants on creation of enums through unsafe transmutes. Essentially this sanitizes patterns like the following:
An extension of this check will be done in a follow-up that explicitly sanitizes for extern enum values that come into Rust from e.g. C/C++.
This check is similar to Miri's capabilities of checking for valid construction of enum values.
This PR is inspired by saethlin@'s PR
#104862. Thank you so much for keeping this code up and the detailed comments!
I also pair-programmed large parts of this together with vabr-g@.
r? @saethlin