-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Make too-large byte sizes a validity error in Layout
#99134
Make too-large byte sizes a validity error in Layout
#99134
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
I worry that this might have negative compiletime impact (c.f. #99117 and just how touchy this code is to insignificant changes), but there's no way to tell without trying it. |
cfd03ba
to
b34f5b9
Compare
This doesn't touch monomorphized methods, and all of it should be inlined while compiling core by LLVM (or maybe even MIR, now!) since it's all runtime nothing, so hopefully it'll not have perf impact. But let's check: @bors try @rust-timer queue |
Awaiting bors try build completion. @rustbot label: +S-waiting-on-perf |
⌛ Trying commit b34f5b9 with merge 31f194589df939278844135001202a5fb3a984dd... |
☀️ Try build successful - checks-actions |
Queued 31f194589df939278844135001202a5fb3a984dd with parent c396bb3, future comparison URL. |
Finished benchmarking commit (31f194589df939278844135001202a5fb3a984dd): comparison url. Instruction count
Max RSS (memory usage)Results
CyclesResults
If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf. Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @bors rollup=never Footnotes |
(it took me a while to write this so I didn't notice @scottmcm had already posted a minor analysis) Secondary: the externs stress test saw both the two regressions and the hugely significant boost, to incremental compilation. Odd that this would be the case. Both cases are an estimated cycles benefit, though, so /shrug Primary: mixed, but more green than red. All primary changes are opt builds; debug is unchanged. RSS: hey look at that, an unambiguous win, even if somewhat isolated. I think these results at a minimum support that this change isn't a negative. Changes in this file just tend to be noisier than in others. |
I'm worried that we're increasingly making things like this language-UB (at least at a current-implementation level), rather than "just" adding asserts that CTFE/MIRI understand. It seems like catching someone transmuting from/to Layout is probably not the intent here, so adding an ub_checking_assert or something like it feels like a better path here. (I think we could define that in std/core today? We support panic! in CTFE and we have the closure-based dispatch intrinsic to detect CTFE, which I think includes miri evaluation?) My main concern comes from this adding more noise to our IR and in general being harder to follow for folks working with these types, rather than just asserting in constructors. It also seems unfortunate to send a message that in order to get a little more checking for safety preconditions you have to make the leap to make violating them a full-blown validity error, not just around actual usage, which might escalate what would've been an assert of some kind into just UB. In general, it feels like just because we can define some range on a type doesn't mean that we should make it UB to be outside that range -- it's the equivalent of doing unchecked_add and such in cold code. To be clear, I'm all for improving the ability of MIRI and other tooling to catch these bugs, but I'm not convinced that this is the right approach. In the long run we're definitely going to want a sanitizer_assert! or similar, I expect, so I think we should invest in that now, rather than running with the (poor, annoying to use, very sharp tool) rustc attr-based hack around its lack. |
That might be a good argument to revert the Along those lines, perhaps we could add a |
I would much rather keep I think looking at just the MIRI detection misses a big part of the value, here. I'd really like to make https://doc.rust-lang.org/nightly/std/alloc/struct.Layout.html#method.align_to take a public Admittedly, that doesn't need a validity error for it, specifically, just restricted safe constructors. But honestly once the type exists the work to have the validity attributes is negligible, so I feel like they might as well exist to give LLVM nice I could close this PR and file an ACP for making |
I don't have any particular objections to the types existing (ValidSize and ValidAlign, etc.), but more so that it seems quite unfortunate if we need to escalate to validity to get this kind of checking. It seems to me that LLVM isn't really going to cooperate here for now anyway (based on #49572), so it's not clear that we gain much from validity. Ultimately, I suppose it doesn't matter that much - it's unlikely that users will practically encounter problems from the validity invariant here (vs. a safety invariant), but it just feels icky to me. I think it comes down to the fact that if we have merely a safety invariant, it's much easier for us to catch that at runtime (without miri) via asserts, but with a validity invariant it becomes increasingly likely that it just ends up UB and LLVM optimizes it out entirely. (Sort of like how C & C++ code, and probably Rust too, have problems with It might be that concern isn't really applicable to these types though, which are more constrained in use and less likely to get accidentally dereferenced or otherwise assert validity before the check. Based solely on the diff, r=me; I'll leave it up to you whether the argument I'm making feels convincing enough that it merits further discussion (probably in a libs meeting?). I'm a little iffy on it myself, only around 60-70% convinced this is going to be painful later. But we can also revert; we're not making stable guarantees here, so maybe that's OK. |
I'll close this for now, and decide whether to come back to it later after maybe proposing libs-api changes. |
I opened an ACP to expose the |
Now that #95295 made sizes over
isize::MAX
disallowed inLayout
, here's a PR to make it a validity error, and thus something that CTFE/MIRI can catch, like was done for alignment in #95361.r? @Mark-Simulacrum
cc @CAD97