From f5481fc443060065cde9ed7bc6f3fd83bc1ecc4c Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Thu, 8 Jan 2015 16:27:22 -0500 Subject: [PATCH] ubsan.c (do_ubsan_in_current_function): New. gcc/ * ubsan.c (do_ubsan_in_current_function): New. (pass_ubsan::gate): Use it. * ubsan.h: Declare it. * convert.c (convert_to_integer): Use it. gcc/c-family/ * c-ubsan.c (ubsan_maybe_instrument_array_ref): Use do_ubsan_in_current_function. (ubsan_maybe_instrument_reference_or_call): Likewise. * c-ubsan.h: Declare it. gcc/cp/ * cp-gimplify.c (cp_genericize): Use do_ubsan_in_current_function. * decl.c (compute_array_index_type): Likewise. * init.c (build_vec_init): Likewise. * typeck.c (cp_build_binary_op): Likewise. From-SVN: r219360 --- gcc/ChangeLog | 7 +++++++ gcc/c-family/ChangeLog | 7 +++++++ gcc/c-family/c-ubsan.c | 8 ++------ gcc/c-family/c-ubsan.h | 3 +++ gcc/convert.c | 4 +--- gcc/cp/ChangeLog | 7 +++++++ gcc/cp/cp-gimplify.c | 4 +--- gcc/cp/decl.c | 5 +---- gcc/cp/init.c | 5 +---- gcc/cp/typeck.c | 4 +--- gcc/ubsan.c | 14 +++++++++++--- gcc/ubsan.h | 1 + 12 files changed, 43 insertions(+), 26 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index ff6abddfe50..f3db7fdfb3c 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2015-01-08 Jason Merrill + + * ubsan.c (do_ubsan_in_current_function): New. + (pass_ubsan::gate): Use it. + * ubsan.h: Declare it. + * convert.c (convert_to_integer): Use it. + 2015-01-08 Jakub Jelinek PR target/64338 diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index d6f09dcf044..acbe684ada1 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,10 @@ +2015-01-08 Jason Merrill + + * c-ubsan.c (ubsan_maybe_instrument_array_ref): Use + do_ubsan_in_current_function. + (ubsan_maybe_instrument_reference_or_call): Likewise. + * c-ubsan.h: Declare it. + 2015-01-08 Mike Stump * c-common.c (c_common_attribute_table): Add no_sanitize_thread. diff --git a/gcc/c-family/c-ubsan.c b/gcc/c-family/c-ubsan.c index f2db4942db4..c195c7ff763 100644 --- a/gcc/c-family/c-ubsan.c +++ b/gcc/c-family/c-ubsan.c @@ -364,9 +364,7 @@ void ubsan_maybe_instrument_array_ref (tree *expr_p, bool ignore_off_by_one) { if (!ubsan_array_ref_instrumented_p (*expr_p) - && current_function_decl != NULL_TREE - && !lookup_attribute ("no_sanitize_undefined", - DECL_ATTRIBUTES (current_function_decl))) + && do_ubsan_in_current_function ()) { tree op0 = TREE_OPERAND (*expr_p, 0); tree op1 = TREE_OPERAND (*expr_p, 1); @@ -386,9 +384,7 @@ static tree ubsan_maybe_instrument_reference_or_call (location_t loc, tree op, tree ptype, enum ubsan_null_ckind ckind) { - if (current_function_decl == NULL_TREE - || lookup_attribute ("no_sanitize_undefined", - DECL_ATTRIBUTES (current_function_decl))) + if (!do_ubsan_in_current_function ()) return NULL_TREE; tree type = TREE_TYPE (ptype); diff --git a/gcc/c-family/c-ubsan.h b/gcc/c-family/c-ubsan.h index b1fe5f1d820..fb379c83c15 100644 --- a/gcc/c-family/c-ubsan.h +++ b/gcc/c-family/c-ubsan.h @@ -31,4 +31,7 @@ extern void ubsan_maybe_instrument_array_ref (tree *, bool); extern void ubsan_maybe_instrument_reference (tree); extern void ubsan_maybe_instrument_member_call (tree, bool); +/* Declare this here as well as in ubsan.h. */ +extern bool do_ubsan_in_current_function (void); + #endif /* GCC_C_UBSAN_H */ diff --git a/gcc/convert.c b/gcc/convert.c index 0491986bd1e..fc1b93c225b 100644 --- a/gcc/convert.c +++ b/gcc/convert.c @@ -885,9 +885,7 @@ convert_to_integer (tree type, tree expr) case REAL_TYPE: if (flag_sanitize & SANITIZE_FLOAT_CAST - && current_function_decl != NULL_TREE - && !lookup_attribute ("no_sanitize_undefined", - DECL_ATTRIBUTES (current_function_decl))) + && do_ubsan_in_current_function ()) { expr = save_expr (expr); tree check = ubsan_instrument_float_cast (loc, type, expr, expr); diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 46d795705d9..8afaafa6b84 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2015-01-08 Jason Merrill + + * cp-gimplify.c (cp_genericize): Use do_ubsan_in_current_function. + * decl.c (compute_array_index_type): Likewise. + * init.c (build_vec_init): Likewise. + * typeck.c (cp_build_binary_op): Likewise. + 2015-01-08 Jason Merrill * init.c (build_vec_init): Call ubsan_instrument_bounds to check diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c index a23076f6c36..33dce296433 100644 --- a/gcc/cp/cp-gimplify.c +++ b/gcc/cp/cp-gimplify.c @@ -1350,9 +1350,7 @@ cp_genericize (tree fndecl) cp_genericize_tree (&DECL_SAVED_TREE (fndecl)); if (flag_sanitize & SANITIZE_RETURN - && current_function_decl != NULL_TREE - && !lookup_attribute ("no_sanitize_undefined", - DECL_ATTRIBUTES (current_function_decl))) + && do_ubsan_in_current_function ()) cp_ubsan_maybe_instrument_return (fndecl); /* Do everything else. */ diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 65ccf7745d2..dd000c39cb1 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -8595,10 +8595,7 @@ compute_array_index_type (tree name, tree size, tsubst_flags_t complain) stabilize_vla_size (itype); if (flag_sanitize & SANITIZE_VLA - && current_function_decl != NULL_TREE - && !lookup_attribute ("no_sanitize_undefined", - DECL_ATTRIBUTES - (current_function_decl))) + && do_ubsan_in_current_function ()) { /* We have to add 1 -- in the ubsan routine we generate LE_EXPR rather than LT_EXPR. */ diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 6f324926aef..3bd470b986a 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -3575,10 +3575,7 @@ build_vec_init (tree base, tree maxindex, tree init, /* Don't check an array new when -fno-exceptions. */ } else if (flag_sanitize & SANITIZE_BOUNDS - && current_function_decl - && !lookup_attribute ("no_sanitize_undefined", - DECL_ATTRIBUTES - (current_function_decl))) + && do_ubsan_in_current_function ()) { /* Make sure the last element of the initializer is in bounds. */ finish_expr_stmt diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index b4af8b0d073..fb0ffd1438d 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -4973,9 +4973,7 @@ cp_build_binary_op (location_t location, if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE)) && !processing_template_decl - && current_function_decl != 0 - && !lookup_attribute ("no_sanitize_undefined", - DECL_ATTRIBUTES (current_function_decl)) + && do_ubsan_in_current_function () && (doing_div_or_mod || doing_shift)) { /* OP0 and/or OP1 might have side-effects. */ diff --git a/gcc/ubsan.c b/gcc/ubsan.c index 5c9355aacb8..7dae9670338 100644 --- a/gcc/ubsan.c +++ b/gcc/ubsan.c @@ -1648,6 +1648,16 @@ instrument_object_size (gimple_stmt_iterator *gsi, bool is_lhs) gsi_insert_before (gsi, g, GSI_SAME_STMT); } +/* True if we want to play UBSan games in the current function. */ + +bool +do_ubsan_in_current_function () +{ + return (current_function_decl != NULL_TREE + && !lookup_attribute ("no_sanitize_undefined", + DECL_ATTRIBUTES (current_function_decl))); +} + namespace { const pass_data pass_data_ubsan = @@ -1679,9 +1689,7 @@ public: | SANITIZE_NONNULL_ATTRIBUTE | SANITIZE_RETURNS_NONNULL_ATTRIBUTE | SANITIZE_OBJECT_SIZE) - && current_function_decl != NULL_TREE - && !lookup_attribute ("no_sanitize_undefined", - DECL_ATTRIBUTES (current_function_decl)); + && do_ubsan_in_current_function (); } virtual unsigned int execute (function *); diff --git a/gcc/ubsan.h b/gcc/ubsan.h index 5d9d90933a2..9b8a59b6c84 100644 --- a/gcc/ubsan.h +++ b/gcc/ubsan.h @@ -38,6 +38,7 @@ enum ubsan_print_style { UBSAN_PRINT_ARRAY }; +extern bool do_ubsan_in_current_function (void); extern bool ubsan_expand_bounds_ifn (gimple_stmt_iterator *); extern bool ubsan_expand_null_ifn (gimple_stmt_iterator *); extern bool ubsan_expand_objsize_ifn (gimple_stmt_iterator *); -- 2.30.2