diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-08-26 08:44:02 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2023-01-31 14:16:49 +0100 |
commit | 9c87dc0afe059ce54280dd81ff773f910a6dffdf (patch) | |
tree | 83fb51fefaa069421de491008642cd29ae08c2c7 /gcc/rust/backend | |
parent | 41547275c5e9a9b7a92898106180b2d295e1ed36 (diff) | |
download | gcc-9c87dc0afe059ce54280dd81ff773f910a6dffdf.zip gcc-9c87dc0afe059ce54280dd81ff773f910a6dffdf.tar.gz gcc-9c87dc0afe059ce54280dd81ff773f910a6dffdf.tar.bz2 |
gccrs: backend: Add overflow checks to every arithmetic operation
gcc/rust/ChangeLog:
* backend/rust-compile-expr.cc (CompileExpr::visit): Insert overflow
checks logic.
(CompileExpr::array_copied_expr): Insert overflow checks logic.
* backend/rust-compile-item.cc (CompileItem::visit): Insert overflow
checks logic.
* backend/rust-compile-type.cc (TyTyResolveCompile::visit): Insert
overflow checks logic.
* rust-gcc.cc (Gcc_backend::arithmetic_or_logical_expression): Differentiate
existing function from `arithmetic_or_logical_expression_checked`.
This function does insert perform overflow checks.
(Gcc_backend::arithmetic_or_logical_expression_checked): New
function.
(is_overflowing_expr): New function. Check if an expression is an
overflowing one (ADD, SUB, MUL).
(fetch_overflow_builtins): New function.
* rust-backend.h: Declare `arithmetic_or_logical_expression_checked` in
abstract `Backend` class.
gcc/testsuite/ChangeLog:
* rust/debug/win64-abi.rs: Fix assertion to take into account
overflow builtins
* rust/compile/torture/macro-issue1426.rs: Moved to...
* rust/execute/torture/macro-issue1426.rs: ...here.
* rust/execute/torture/overflow1.rs: New test.
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.cc | 55 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-item.cc | 6 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-type.cc | 4 |
3 files changed, 55 insertions, 10 deletions
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 9db14a2..ea14673 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -26,6 +26,7 @@ #include "rust-compile-block.h" #include "rust-compile-implitem.h" #include "rust-constexpr.h" +#include "rust-gcc.h" #include "fold-const.h" #include "realmpfr.h" @@ -146,9 +147,26 @@ CompileExpr::visit (HIR::ArithmeticOrLogicalExpr &expr) return; } - translated - = ctx->get_backend ()->arithmetic_or_logical_expression (op, lhs, rhs, - expr.get_locus ()); + if (ctx->in_fn () && !ctx->const_context_p ()) + { + auto receiver_tmp = NULL_TREE; + auto receiver + = ctx->get_backend ()->temporary_variable (ctx->peek_fn ().fndecl, + NULL_TREE, TREE_TYPE (lhs), + lhs, true, expr.get_locus (), + &receiver_tmp); + auto check + = ctx->get_backend ()->arithmetic_or_logical_expression_checked ( + op, lhs, rhs, expr.get_locus (), receiver); + + ctx->add_statement (check); + translated = receiver->get_tree (expr.get_locus ()); + } + else + { + translated = ctx->get_backend ()->arithmetic_or_logical_expression ( + op, lhs, rhs, expr.get_locus ()); + } } void @@ -176,13 +194,27 @@ CompileExpr::visit (HIR::CompoundAssignmentExpr &expr) return; } - auto operator_expr - = ctx->get_backend ()->arithmetic_or_logical_expression (op, lhs, rhs, - expr.get_locus ()); - tree assignment - = ctx->get_backend ()->assignment_statement (lhs, operator_expr, - expr.get_locus ()); - ctx->add_statement (assignment); + if (ctx->in_fn () && !ctx->const_context_p ()) + { + auto tmp = NULL_TREE; + auto receiver + = ctx->get_backend ()->temporary_variable (ctx->peek_fn ().fndecl, + NULL_TREE, TREE_TYPE (lhs), + lhs, true, expr.get_locus (), + &tmp); + auto check + = ctx->get_backend ()->arithmetic_or_logical_expression_checked ( + op, lhs, rhs, expr.get_locus (), receiver); + ctx->add_statement (check); + + translated = ctx->get_backend ()->assignment_statement ( + lhs, receiver->get_tree (expr.get_locus ()), expr.get_locus ()); + } + else + { + translated = ctx->get_backend ()->arithmetic_or_logical_expression ( + op, lhs, rhs, expr.get_locus ()); + } } void @@ -2383,7 +2415,10 @@ CompileExpr::array_copied_expr (Location expr_locus, return error_mark_node; } + ctx->push_const_context (); tree capacity_expr = CompileExpr::Compile (elems.get_num_copies_expr (), ctx); + ctx->pop_const_context (); + if (!TREE_CONSTANT (capacity_expr)) { rust_error_at (expr_locus, "non const num copies %qT", array_type); diff --git a/gcc/rust/backend/rust-compile-item.cc b/gcc/rust/backend/rust-compile-item.cc index 6977dde..634b983 100644 --- a/gcc/rust/backend/rust-compile-item.cc +++ b/gcc/rust/backend/rust-compile-item.cc @@ -160,6 +160,9 @@ CompileItem::visit (HIR::Function &function) function.get_mappings ().get_nodeid (), &canonical_path); rust_assert (ok); + if (function.get_qualifiers ().is_const ()) + ctx->push_const_context (); + tree fndecl = compile_function (ctx, function.get_function_name (), function.get_self_param (), @@ -169,6 +172,9 @@ CompileItem::visit (HIR::Function &function) function.get_definition ().get (), canonical_path, fntype, function.has_function_return_type ()); reference = address_expression (fndecl, ref_locus); + + if (function.get_qualifiers ().is_const ()) + ctx->pop_const_context (); } void diff --git a/gcc/rust/backend/rust-compile-type.cc b/gcc/rust/backend/rust-compile-type.cc index a81aed7..fe1b7ce 100644 --- a/gcc/rust/backend/rust-compile-type.cc +++ b/gcc/rust/backend/rust-compile-type.cc @@ -370,7 +370,11 @@ TyTyResolveCompile::visit (const TyTy::ArrayType &type) { tree element_type = TyTyResolveCompile::compile (ctx, type.get_element_type ()); + + ctx->push_const_context (); tree capacity_expr = CompileExpr::Compile (&type.get_capacity_expr (), ctx); + ctx->pop_const_context (); + tree folded_capacity_expr = fold_expr (capacity_expr); translated |