-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Guard HIR lowered contracts with contract_checks
#144438
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
Open
dawidl022
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
dawidl022:contracts/guarded-lowering
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.
+459
−120
Open
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
use crate::LoweringContext; | ||
|
||
impl<'a, 'hir> LoweringContext<'a, 'hir> { | ||
pub(super) fn lower_contract( | ||
&mut self, | ||
body: impl FnOnce(&mut Self) -> rustc_hir::Expr<'hir>, | ||
contract: &rustc_ast::FnContract, | ||
) -> rustc_hir::Expr<'hir> { | ||
match (&contract.requires, &contract.ensures) { | ||
(Some(req), Some(ens)) => { | ||
// Lower the fn contract, which turns: | ||
// | ||
// { body } | ||
// | ||
// into: | ||
// | ||
// { | ||
// let __postcond = if contracts_checks() { | ||
// contract_check_requires(PRECOND); | ||
// Some(|ret_val| POSTCOND) | ||
// } else { | ||
// None | ||
// }; | ||
// contract_check_ensures(__postcond, { body }) | ||
// } | ||
|
||
let precond = self.lower_precond(req); | ||
let postcond_checker = self.lower_postcond_checker(ens); | ||
|
||
let contract_check = | ||
self.lower_contract_check_with_postcond(Some(precond), postcond_checker); | ||
|
||
let wrapped_body = | ||
self.wrap_body_with_contract_check(body, contract_check, postcond_checker.span); | ||
self.expr_block(wrapped_body) | ||
} | ||
(None, Some(ens)) => { | ||
// Lower the fn contract, which turns: | ||
// | ||
// { body } | ||
// | ||
// into: | ||
// | ||
// { | ||
// let __postcond = if contracts_check() { | ||
// Some(|ret_val| POSTCOND) | ||
// } else { | ||
// None | ||
// }; | ||
// __postcond({ body }) | ||
// } | ||
|
||
let postcond_checker = self.lower_postcond_checker(ens); | ||
let contract_check = | ||
self.lower_contract_check_with_postcond(None, postcond_checker); | ||
|
||
let wrapped_body = | ||
self.wrap_body_with_contract_check(body, contract_check, postcond_checker.span); | ||
self.expr_block(wrapped_body) | ||
} | ||
(Some(req), None) => { | ||
// Lower the fn contract, which turns: | ||
// | ||
// { body } | ||
// | ||
// into: | ||
// | ||
// { | ||
// if contracts_check() { | ||
// contract_requires(PRECOND); | ||
// } | ||
// body | ||
// } | ||
let precond = self.lower_precond(req); | ||
let precond_check = self.lower_contract_check_just_precond(precond); | ||
|
||
let body = self.arena.alloc(body(self)); | ||
|
||
// Flatten the body into precond check, then body. | ||
let wrapped_body = self.block_all( | ||
body.span, | ||
self.arena.alloc_from_iter([precond_check].into_iter()), | ||
Some(body), | ||
); | ||
self.expr_block(wrapped_body) | ||
} | ||
(None, None) => body(self), | ||
} | ||
} | ||
|
||
/// Lower the precondition check intrinsic. | ||
fn lower_precond(&mut self, req: &Box<rustc_ast::Expr>) -> rustc_hir::Stmt<'hir> { | ||
let lowered_req = self.lower_expr_mut(&req); | ||
let req_span = self.mark_span_with_reason( | ||
rustc_span::DesugaringKind::Contract, | ||
lowered_req.span, | ||
None, | ||
); | ||
let precond = self.expr_call_lang_item_fn_mut( | ||
req_span, | ||
rustc_hir::LangItem::ContractCheckRequires, | ||
&*arena_vec![self; lowered_req], | ||
); | ||
self.stmt_expr(req.span, precond) | ||
} | ||
|
||
fn lower_postcond_checker( | ||
&mut self, | ||
ens: &Box<rustc_ast::Expr>, | ||
) -> &'hir rustc_hir::Expr<'hir> { | ||
let ens_span = self.lower_span(ens.span); | ||
let ens_span = | ||
self.mark_span_with_reason(rustc_span::DesugaringKind::Contract, ens_span, None); | ||
let lowered_ens = self.lower_expr_mut(&ens); | ||
self.expr_call_lang_item_fn( | ||
ens_span, | ||
rustc_hir::LangItem::ContractBuildCheckEnsures, | ||
&*arena_vec![self; lowered_ens], | ||
) | ||
} | ||
|
||
fn lower_contract_check_just_precond( | ||
&mut self, | ||
precond: rustc_hir::Stmt<'hir>, | ||
) -> rustc_hir::Stmt<'hir> { | ||
let stmts = self.arena.alloc_from_iter([precond].into_iter()); | ||
|
||
let then_block_stmts = self.block_all(precond.span, stmts, None); | ||
let then_block = self.arena.alloc(self.expr_block(&then_block_stmts)); | ||
|
||
let precond_check = rustc_hir::ExprKind::If( | ||
self.expr_call_lang_item_fn( | ||
precond.span, | ||
rustc_hir::LangItem::ContractChecks, | ||
Default::default(), | ||
), | ||
then_block, | ||
None, | ||
); | ||
|
||
let precond_check = self.expr(precond.span, precond_check); | ||
self.stmt_expr(precond.span, precond_check) | ||
} | ||
|
||
fn lower_contract_check_with_postcond( | ||
&mut self, | ||
precond: Option<rustc_hir::Stmt<'hir>>, | ||
postcond_checker: &'hir rustc_hir::Expr<'hir>, | ||
) -> &'hir rustc_hir::Expr<'hir> { | ||
let stmts = self.arena.alloc_from_iter(precond.into_iter()); | ||
let span = match precond { | ||
Some(precond) => precond.span, | ||
None => postcond_checker.span, | ||
}; | ||
|
||
let postcond_checker = self.arena.alloc(self.expr_enum_variant_lang_item( | ||
postcond_checker.span, | ||
rustc_hir::lang_items::LangItem::OptionSome, | ||
&*arena_vec![self; *postcond_checker], | ||
)); | ||
let then_block_stmts = self.block_all(span, stmts, Some(postcond_checker)); | ||
let then_block = self.arena.alloc(self.expr_block(&then_block_stmts)); | ||
|
||
let none_expr = self.arena.alloc(self.expr_enum_variant_lang_item( | ||
postcond_checker.span, | ||
rustc_hir::lang_items::LangItem::OptionNone, | ||
Default::default(), | ||
)); | ||
let else_block = self.block_expr(none_expr); | ||
let else_block = self.arena.alloc(self.expr_block(else_block)); | ||
|
||
let contract_check = rustc_hir::ExprKind::If( | ||
self.expr_call_lang_item_fn( | ||
span, | ||
rustc_hir::LangItem::ContractChecks, | ||
Default::default(), | ||
), | ||
then_block, | ||
Some(else_block), | ||
); | ||
self.arena.alloc(self.expr(span, contract_check)) | ||
} | ||
|
||
fn wrap_body_with_contract_check( | ||
&mut self, | ||
body: impl FnOnce(&mut Self) -> rustc_hir::Expr<'hir>, | ||
contract_check: &'hir rustc_hir::Expr<'hir>, | ||
postcond_span: rustc_span::Span, | ||
) -> &'hir rustc_hir::Block<'hir> { | ||
let check_ident: rustc_span::Ident = | ||
rustc_span::Ident::from_str_and_span("__ensures_checker", postcond_span); | ||
let (check_hir_id, postcond_decl) = { | ||
// Set up the postcondition `let` statement. | ||
let (checker_pat, check_hir_id) = self.pat_ident_binding_mode_mut( | ||
postcond_span, | ||
check_ident, | ||
rustc_hir::BindingMode::NONE, | ||
); | ||
( | ||
check_hir_id, | ||
self.stmt_let_pat( | ||
None, | ||
postcond_span, | ||
Some(contract_check), | ||
self.arena.alloc(checker_pat), | ||
rustc_hir::LocalSource::Contract, | ||
), | ||
) | ||
}; | ||
|
||
// Install contract_ensures so we will intercept `return` statements, | ||
// then lower the body. | ||
self.contract_ensures = Some((postcond_span, check_ident, check_hir_id)); | ||
let body = self.arena.alloc(body(self)); | ||
|
||
// Finally, inject an ensures check on the implicit return of the body. | ||
let body = self.inject_ensures_check(body, postcond_span, check_ident, check_hir_id); | ||
|
||
// Flatten the body into precond, then postcond, then wrapped body. | ||
let wrapped_body = self.block_all( | ||
body.span, | ||
self.arena.alloc_from_iter([postcond_decl].into_iter()), | ||
Some(body), | ||
); | ||
wrapped_body | ||
} | ||
|
||
/// Create an `ExprKind::Ret` that is optionally wrapped by a call to check | ||
/// a contract ensures clause, if it exists. | ||
pub(super) fn checked_return( | ||
&mut self, | ||
opt_expr: Option<&'hir rustc_hir::Expr<'hir>>, | ||
) -> rustc_hir::ExprKind<'hir> { | ||
let checked_ret = | ||
if let Some((check_span, check_ident, check_hir_id)) = self.contract_ensures { | ||
let expr = opt_expr.unwrap_or_else(|| self.expr_unit(check_span)); | ||
Some(self.inject_ensures_check(expr, check_span, check_ident, check_hir_id)) | ||
} else { | ||
opt_expr | ||
}; | ||
rustc_hir::ExprKind::Ret(checked_ret) | ||
} | ||
|
||
/// Wraps an expression with a call to the ensures check before it gets returned. | ||
pub(super) fn inject_ensures_check( | ||
&mut self, | ||
expr: &'hir rustc_hir::Expr<'hir>, | ||
span: rustc_span::Span, | ||
cond_ident: rustc_span::Ident, | ||
cond_hir_id: rustc_hir::HirId, | ||
) -> &'hir rustc_hir::Expr<'hir> { | ||
let cond_fn = self.expr_ident(span, cond_ident, cond_hir_id); | ||
let call_expr = self.expr_call_lang_item_fn_mut( | ||
span, | ||
rustc_hir::LangItem::ContractCheckEnsures, | ||
arena_vec![self; *cond_fn, *expr], | ||
); | ||
self.arena.alloc(call_expr) | ||
} | ||
} |
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.
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.
The suggestion is for this code to be either:
or
depending on the value of the
-Z contract-checks
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks for the suggestion! I tried implementing this change in a branch, and the codegen test still fails (tests/codegen/cross-crate-inlining/leaf-inlining.rs) with the alignment contracts from #136578.
I have not looked into why the test is failing, but based on the name, I suspect the optimiser is not able to inline functions it was previously able to, because now they have the
if false ...
contract code, and maybe the inlining optimisation runs before theif false
elimination run, (or there is noif false
optimisation in the optimisation settings the test runs under, even though it is set to opt-level 3)?I'm happy to investigate more deeply why this test fails, so that we understand how to fix this optimisation problem.
My branch: https://github.com/dawidl022/rust/tree/contracts/guarded-lowering-compiler-flag-alignment-tests
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.
If you want to debug this further you should first check if the branch has been eliminated in the final optimized mir. If it has, you should check if rustc runs the inline pass after dead code elimination or just before.
I believe that llvm inline pass will still run, so this may not be a problem and we may need to adjust the tests. But it's worth checking with the library team.