From ce6923c53da68bc3e0eabb1d071217402a104148 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Wed, 18 Sep 2013 10:01:40 +0000 Subject: [PATCH] re PR sanitizer/58411 (no_sanitize_undefined function attribute) 2013-09-18 Marek Polacek PR sanitizer/58411 * doc/extend.texi: Document no_sanitize_undefined attribute. * builtins.c (fold_builtin_0): Don't sanitize function if it has the no_sanitize_undefined attribute. From-SVN: r202682 --- gcc/ChangeLog | 7 +++++ gcc/builtins.c | 5 +++- gcc/c-family/ChangeLog | 7 +++++ gcc/c-family/c-common.c | 21 +++++++++++++ gcc/c/ChangeLog | 6 ++++ gcc/c/c-typeck.c | 2 ++ gcc/cp/ChangeLog | 6 ++++ gcc/cp/typeck.c | 2 ++ gcc/doc/extend.texi | 7 +++++ gcc/testsuite/ChangeLog | 5 ++++ gcc/testsuite/c-c++-common/ubsan/attrib-1.c | 33 +++++++++++++++++++++ 11 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/c-c++-common/ubsan/attrib-1.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index ea746bc884e..8460d0afd64 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2013-09-18 Marek Polacek + + PR sanitizer/58411 + * doc/extend.texi: Document no_sanitize_undefined attribute. + * builtins.c (fold_builtin_0): Don't sanitize function if it has the + no_sanitize_undefined attribute. + 2013-09-18 Nick Clifton * config/msp430/msp430.h (ASM_SPEC): Pass -md on to the assembler. diff --git a/gcc/builtins.c b/gcc/builtins.c index 0ab6d9b5d70..d19ca68baba 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -10313,7 +10313,10 @@ fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED) return fold_builtin_classify_type (NULL_TREE); case BUILT_IN_UNREACHABLE: - if (flag_sanitize & SANITIZE_UNREACHABLE) + if (flag_sanitize & SANITIZE_UNREACHABLE + && (current_function_decl == NULL + || !lookup_attribute ("no_sanitize_undefined", + DECL_ATTRIBUTES (current_function_decl)))) return ubsan_instrument_unreachable (loc); break; diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 3061b4a896d..1772ba56b49 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,10 @@ +2013-09-18 Marek Polacek + + PR sanitizer/58411 + * c-common.c (handle_no_sanitize_undefined_attribute): New function. + Declare it. + (struct attribute_spec c_common_att): Add no_sanitize_undefined. + 2013-09-14 Iain Sandoe PR target/48094 diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 62aa9fcec2b..8ecb70cfa7c 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -311,6 +311,8 @@ static tree handle_no_sanitize_address_attribute (tree *, tree, tree, int, bool *); static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree, int, bool *); +static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int, + bool *); static tree handle_noinline_attribute (tree *, tree, tree, int, bool *); static tree handle_noclone_attribute (tree *, tree, tree, int, bool *); static tree handle_leaf_attribute (tree *, tree, tree, int, bool *); @@ -722,6 +724,9 @@ const struct attribute_spec c_common_attribute_table[] = { "no_sanitize_address", 0, 0, true, false, false, handle_no_sanitize_address_attribute, false }, + { "no_sanitize_undefined", 0, 0, true, false, false, + handle_no_sanitize_undefined_attribute, + false }, { "warning", 1, 1, true, false, false, handle_error_attribute, false }, { "error", 1, 1, true, false, false, @@ -6575,6 +6580,22 @@ handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int, return NULL_TREE; } +/* Handle a "no_sanitize_undefined" attribute; arguments as in + struct attribute_spec.handler. */ + +static tree +handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int, + bool *no_add_attrs) +{ + if (TREE_CODE (*node) != FUNCTION_DECL) + { + warning (OPT_Wattributes, "%qE attribute ignored", name); + *no_add_attrs = true; + } + + return NULL_TREE; +} + /* Handle a "noinline" attribute; arguments as in struct attribute_spec.handler. */ diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index 8b0cc2f5170..59b71aa0e9d 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,9 @@ +2013-09-18 Marek Polacek + + PR sanitizer/58411 + * c-typeck.c (build_binary_op): Don't sanitize function if it has the + no_sanitize_undefined attribute. + 2013-09-13 Kai Tietz PR target/57848 diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index e52533ecd6d..7dc5527fc7c 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -10498,6 +10498,8 @@ build_binary_op (location_t location, enum tree_code code, if (flag_sanitize & SANITIZE_UNDEFINED && current_function_decl != 0 + && !lookup_attribute ("no_sanitize_undefined", + DECL_ATTRIBUTES (current_function_decl)) && (doing_div_or_mod || doing_shift)) { /* OP0 and/or OP1 might have side-effects. */ diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 7e2c13beb5e..c16d682459d 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2013-09-18 Marek Polacek + + PR sanitizer/58411 + * typeck.c (cp_build_binary_op): Don't sanitize function if it has the + no_sanitize_undefined attribute. + 2013-09-17 Paolo Carlini PR c++/58435 diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 6c48f242dd7..f7d6208022f 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -4887,6 +4887,8 @@ cp_build_binary_op (location_t location, if ((flag_sanitize & SANITIZE_UNDEFINED) && !processing_template_decl && current_function_decl != 0 + && !lookup_attribute ("no_sanitize_undefined", + DECL_ATTRIBUTES (current_function_decl)) && (doing_div_or_mod || doing_shift)) { /* OP0 and/or OP1 might have side-effects. */ diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index cb0306b72b9..1d0dfbe985d 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -2136,6 +2136,7 @@ attributes are currently defined for functions on all targets: @code{warn_unused_result}, @code{nonnull}, @code{gnu_inline}, @code{externally_visible}, @code{hot}, @code{cold}, @code{artificial}, @code{no_sanitize_address}, @code{no_address_safety_analysis}, +@code{no_sanitize_undefined}, @code{error} and @code{warning}. Several other attributes are defined for functions on particular target systems. Other attributes, including @code{section} are @@ -3500,6 +3501,12 @@ The @code{no_address_safety_analysis} is a deprecated alias of the @code{no_sanitize_address} attribute, new code should use @code{no_sanitize_address}. +@item no_sanitize_undefined +@cindex @code{no_sanitize_undefined} function attribute +The @code{no_sanitize_undefined} attribute on functions is used +to inform the compiler that it should not check for undefined behavior +in the function when compiling with the @option{-fsanitize=undefined} option. + @item regparm (@var{number}) @cindex @code{regparm} attribute @cindex functions that are passed arguments in registers on the 386 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 796e14343c2..2b1cad23bc8 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2013-09-18 Marek Polacek + + PR sanitizer/58411 + * c-c++-common/ubsan/attrib-1.c: New test. + 2013-09-17 Cong Hou * gcc.dg/vect/vect-reduc-dot-s16c.c: Add a test case with dot product diff --git a/gcc/testsuite/c-c++-common/ubsan/attrib-1.c b/gcc/testsuite/c-c++-common/ubsan/attrib-1.c new file mode 100644 index 00000000000..2e9141ca040 --- /dev/null +++ b/gcc/testsuite/c-c++-common/ubsan/attrib-1.c @@ -0,0 +1,33 @@ +/* PR sanitizer/58411 */ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=undefined -w" } */ + +__attribute__((no_sanitize_undefined)) int +f1 (int i) +{ + return 16 << i; +} + +int f2 (int i); +int f2 (int i) __attribute__((no_sanitize_undefined)); +int f2 (int i) __attribute__((no_sanitize_undefined)); +int f2 (int i); + +int +f2 (int i) +{ + return 1 / i; +} + +void f3 (void); +__typeof (f3) f3 __attribute__((__no_sanitize_undefined__)); + +void +f3 (void) +{ + __builtin_unreachable (); +} + +/* { dg-final { scan-assembler-not "__ubsan_handle_shift_out_of_bounds" } } */ +/* { dg-final { scan-assembler-not "__ubsan_handle_divrem_overflow" } } */ +/* { dg-final { scan-assembler-not "__ubsan_handle_builtin_unreachable" } } */