public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-10389] ubsan: Don't -fsanitize=null instrument __seg_fs/gs pointers [PR111736]
@ 2024-04-23 17:09 Uros Bizjak
  0 siblings, 0 replies; only message in thread
From: Uros Bizjak @ 2024-04-23 17:09 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:d6c62e4fb9a6d395599b7c78c831bace4bc7ff8f

commit r12-10389-gd6c62e4fb9a6d395599b7c78c831bace4bc7ff8f
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Mar 22 09:23:44 2024 +0100

    ubsan: Don't -fsanitize=null instrument __seg_fs/gs pointers [PR111736]
    
    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.
    
    (cherry picked from commit ddd4a3ca87410886b039cc225907b4f6e650082e)

Diff:
---
 gcc/testsuite/gcc.dg/ubsan/pr111736.c | 23 +++++++++++++++++++++++
 gcc/ubsan.cc                          | 19 +++++++++++++++++--
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/ubsan/pr111736.c b/gcc/testsuite/gcc.dg/ubsan/pr111736.c
new file mode 100644
index 00000000000..359b31828f0
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ubsan/pr111736.c
@@ -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;
+}
diff --git a/gcc/ubsan.cc b/gcc/ubsan.cc
index 0f5b372b195..4d8e7cd86c5 100644
--- a/gcc/ubsan.cc
+++ b/gcc/ubsan.cc
@@ -49,6 +49,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-cfg.h"
 #include "gimple-fold.h"
 #include "varasm.h"
+#include "target.h"
 
 /* Map from a tree to a VAR_DECL tree.  */
 
@@ -784,6 +785,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)
     {
@@ -1375,8 +1383,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;

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-04-23 17:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-23 17:09 [gcc r12-10389] ubsan: Don't -fsanitize=null instrument __seg_fs/gs pointers [PR111736] Uros Bizjak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).