]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ubsan: Don't -fsanitize=null instrument __seg_fs/gs pointers [PR111736]
authorJakub Jelinek <jakub@redhat.com>
Fri, 22 Mar 2024 08:23:44 +0000 (09:23 +0100)
committerJakub Jelinek <jakub@redhat.com>
Fri, 22 Mar 2024 08:24:42 +0000 (09:24 +0100)
On x86 and avr some address spaces allow 0 pointers (on avr actually
even generic as, but libsanitizer isn't ported to it and
I'm not convinced we should completely kill -fsanitize=null in that
case).
The following patch makes sure those aren't diagnosed for -fsanitize=null,
though they are still sanitized for -fsanitize=alignment.

2024-03-22  Jakub Jelinek  <jakub@redhat.com>

PR sanitizer/111736
* ubsan.cc (ubsan_expand_null_ifn, instrument_mem_ref): Avoid
SANITIZE_NULL instrumentation for non-generic address spaces
for which targetm.addr_space.zero_address_valid (as) is true.

* gcc.dg/ubsan/pr111736.c: New test.

gcc/testsuite/gcc.dg/ubsan/pr111736.c [new file with mode: 0644]
gcc/ubsan.cc

diff --git a/gcc/testsuite/gcc.dg/ubsan/pr111736.c b/gcc/testsuite/gcc.dg/ubsan/pr111736.c
new file mode 100644 (file)
index 0000000..359b318
--- /dev/null
@@ -0,0 +1,23 @@
+/* PR sanitizer/111736 */
+/* { dg-do compile { target i?86-*-* x86_64-*-* } } */
+/* { dg-options "-fsanitize=null,alignment -fdump-tree-optimized -ffat-lto-objects" } */
+/* { dg-final { scan-tree-dump-times "__ubsan_handle_type_mismatch" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-not "p_\[0-9]*.D. \[=!]= 0" "optimized" } } */
+
+#ifdef __x86_64__
+#define SEG __seg_fs
+#else
+#define SEG __seg_gs
+#endif
+
+int
+foo (int SEG *p, int *q)
+{
+  return *p;
+}
+
+__attribute__((no_sanitize("alignment"))) int
+bar (int SEG *p, int *q)
+{
+  return *p;
+}
index e17d7091408a12211c62c9eb7f47c3da8435196c..fb00dc24bf6081f4ffc635af3d7c9b328110f401 100644 (file)
@@ -858,6 +858,13 @@ ubsan_expand_null_ifn (gimple_stmt_iterator *gsip)
        }
     }
   check_null = sanitize_flags_p (SANITIZE_NULL);
+  if (check_null && POINTER_TYPE_P (TREE_TYPE (ptr)))
+    {
+      addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (ptr)));
+      if (!ADDR_SPACE_GENERIC_P (as)
+         && targetm.addr_space.zero_address_valid (as))
+       check_null = false;
+    }
 
   if (check_align == NULL_TREE && !check_null)
     {
@@ -1447,8 +1454,15 @@ instrument_mem_ref (tree mem, tree base, gimple_stmt_iterator *iter,
       if (align <= 1)
        align = 0;
     }
-  if (align == 0 && !sanitize_flags_p (SANITIZE_NULL))
-    return;
+  if (align == 0)
+    {
+      if (!sanitize_flags_p (SANITIZE_NULL))
+       return;
+      addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (base));
+      if (!ADDR_SPACE_GENERIC_P (as)
+         && targetm.addr_space.zero_address_valid (as))
+       return;
+    }
   tree t = TREE_OPERAND (base, 0);
   if (!POINTER_TYPE_P (TREE_TYPE (t)))
     return;