diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-08-26 08:44:02 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-08-26 08:44:02 +0200 |
commit | fc82b68cc1ee2778f2bfc53c5de47e5b36ac8b7c (patch) | |
tree | 95454db7a9799f2ad2c16205fbc305e7ff7a09c0 /gcc/rust/backend | |
parent | ff7d6bfed2e7bc08fba1cc456daf464e6176dd46 (diff) | |
download | gcc-fc82b68cc1ee2778f2bfc53c5de47e5b36ac8b7c.zip gcc-fc82b68cc1ee2778f2bfc53c5de47e5b36ac8b7c.tar.gz gcc-fc82b68cc1ee2778f2bfc53c5de47e5b36ac8b7c.tar.bz2 |
backend: Add overflow checks to every arithmetic operation
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 865ad25..e13c08c 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 @@ -2378,7 +2410,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 ceba51c..96c4e7f 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 eced909..77d8474 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 |