-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Try enabling precondition checks on ptr::{read,write} #129498
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
Draft
saethlin
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
saethlin:ptr-read-write-precondition
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+22
−13
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,21 +30,29 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify { | |
} | ||
|
||
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { | ||
let def_id = body.source.def_id(); | ||
let ctx = InstSimplifyContext { | ||
tcx, | ||
local_decls: &body.local_decls, | ||
typing_env: body.typing_env(tcx), | ||
}; | ||
let preserve_ub_checks = | ||
attr::contains_name(tcx.hir_krate_attrs(), sym::rustc_preserve_ub_checks); | ||
let remove_ub_checks = if tcx.is_coroutine(def_id) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
false | ||
} else { | ||
tcx.has_attr(def_id, sym::rustc_no_ubchecks) | ||
}; | ||
for block in body.basic_blocks.as_mut() { | ||
for statement in block.statements.iter_mut() { | ||
let StatementKind::Assign(box (.., rvalue)) = &mut statement.kind else { | ||
continue; | ||
}; | ||
|
||
if !preserve_ub_checks { | ||
ctx.simplify_ub_check(rvalue); | ||
if remove_ub_checks { | ||
ctx.simplify_ub_check(rvalue, false); | ||
} else if !preserve_ub_checks { | ||
ctx.simplify_ub_check(rvalue, tcx.sess.ub_checks()); | ||
} | ||
ctx.simplify_bool_cmp(rvalue); | ||
ctx.simplify_ref_deref(rvalue); | ||
|
@@ -168,10 +176,10 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> { | |
} | ||
} | ||
|
||
fn simplify_ub_check(&self, rvalue: &mut Rvalue<'tcx>) { | ||
fn simplify_ub_check(&self, rvalue: &mut Rvalue<'tcx>, ub_checks: bool) { | ||
let Rvalue::NullaryOp(NullOp::UbChecks, _) = *rvalue else { return }; | ||
|
||
let const_ = Const::from_bool(self.tcx, self.tcx.sess.ub_checks()); | ||
let const_ = Const::from_bool(self.tcx, ub_checks); | ||
let constant = ConstOperand { span: DUMMY_SP, const_, user_ty: None }; | ||
*rvalue = Rvalue::Use(Operand::Constant(Box::new(constant))); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Oh, very cool! I'd love to revert my change to
mem::replace
to manually remove the two unnecessary checks and use something like this instead.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.
mem::replace
shouldn't need any special treatment currently.ptr::read
andptr::write
don't have UB checks. The point of this PR is to change that.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.
Oh, I see, they're currently under
debug_assertions
.I thought they were always on 'cause they failed a test in one of my PRs :P
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.
Hm. Whatever test that test was needs
ignore-std-debug-assertions
, so I guess that was a test suite problem not an implementation problem.