]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ubsan: Use -fno{,-}sanitize=float-divide-by-zero for float division by zero recovery...
authorJakub Jelinek <jakub@redhat.com>
Fri, 1 Oct 2021 12:27:32 +0000 (14:27 +0200)
committerJakub Jelinek <jakub@redhat.com>
Wed, 11 May 2022 05:58:30 +0000 (07:58 +0200)
We've been using
-f{,no-}sanitize-recover=integer-divide-by-zero to decide on the float
-fsanitize=float-divide-by-zero instrumentation _abort suffix.
This patch fixes it to use -f{,no-}sanitize-recover=float-divide-by-zero
for it instead.

2021-10-01  Jakub Jelinek  <jakub@redhat.com>
    Richard Biener  <rguenther@suse.de>

PR sanitizer/102515
gcc/c-family/
* c-ubsan.c (ubsan_instrument_division): Check the right
flag_sanitize_recover bit, depending on which sanitization
is done.
gcc/testsuite/
* c-c++-common/ubsan/float-div-by-zero-2.c: New test.

(cherry picked from commit 9c1a633d96926357155d4702b66f8a0ec856a81f)

gcc/c-family/c-ubsan.c
gcc/testsuite/c-c++-common/ubsan/float-div-by-zero-2.c [new file with mode: 0644]

index e4e2c7a1ce8c1dbe969f372f5eb888536c1e04ec..c0142a642ae6d227c01e3ec9bd9ff88dadebca2a 100644 (file)
@@ -41,6 +41,7 @@ ubsan_instrument_division (location_t loc, tree op0, tree op1)
 {
   tree t, tt;
   tree type = TREE_TYPE (op0);
+  enum sanitize_code flag = SANITIZE_DIVIDE;
 
   /* At this point both operands should have the same type,
      because they are already converted to RESULT_TYPE.
@@ -58,8 +59,11 @@ ubsan_instrument_division (location_t loc, tree op0, tree op1)
                     op1, build_int_cst (type, 0));
   else if (TREE_CODE (type) == REAL_TYPE
           && sanitize_flags_p (SANITIZE_FLOAT_DIVIDE))
-    t = fold_build2 (EQ_EXPR, boolean_type_node,
-                    op1, build_real (type, dconst0));
+    {
+      t = fold_build2 (EQ_EXPR, boolean_type_node,
+                      op1, build_real (type, dconst0));
+      flag = SANITIZE_FLOAT_DIVIDE;
+    }
   else
     return NULL_TREE;
 
@@ -95,7 +99,7 @@ ubsan_instrument_division (location_t loc, tree op0, tree op1)
                                     NULL_TREE);
       data = build_fold_addr_expr_loc (loc, data);
       enum built_in_function bcode
-       = (flag_sanitize_recover & SANITIZE_DIVIDE)
+       = (flag_sanitize_recover & flag)
          ? BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW
          : BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW_ABORT;
       tt = builtin_decl_explicit (bcode);
diff --git a/gcc/testsuite/c-c++-common/ubsan/float-div-by-zero-2.c b/gcc/testsuite/c-c++-common/ubsan/float-div-by-zero-2.c
new file mode 100644 (file)
index 0000000..61d050a
--- /dev/null
@@ -0,0 +1,18 @@
+/* { dg-do run } */
+/* { dg-shouldfail "ubsan" } */
+/* { dg-options "-fsanitize=float-divide-by-zero -fno-sanitize-recover=float-divide-by-zero -fsanitize-recover=integer-divide-by-zero" } */
+
+int
+main (void)
+{
+  volatile float a = 1.3f;
+  volatile double b = 0.0;
+  volatile int c = 4;
+  volatile float res;
+
+  res = a / b;
+
+  return 0;
+}
+
+/* { dg-output "division by zero" } */