public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <rguenther@suse.de>
To: Jakub Jelinek <jakub@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] ubsan: Don't -fsanitize=null instrument __seg_fs/gs pointers [PR111736]
Date: Fri, 22 Mar 2024 09:08:23 +0100 (CET)	[thread overview]
Message-ID: <r0r47410-r722-5438-5030-90rs20q5379r@fhfr.qr> (raw)
In-Reply-To: <Zf07jfdjTLaF2+Wd@tucnak>

On Fri, 22 Mar 2024, Jakub Jelinek wrote:

> Hi!
> 
> 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.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

> 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/ubsan.cc.jj	2024-03-13 09:16:37.791885010 +0100
> +++ gcc/ubsan.cc	2024-03-22 08:11:50.093131678 +0100
> @@ -858,6 +858,13 @@ ubsan_expand_null_ifn (gimple_stmt_itera
>  	}
>      }
>    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,
>        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;
> --- gcc/testsuite/gcc.dg/ubsan/pr111736.c.jj	2024-03-21 13:50:49.482348296 +0100
> +++ gcc/testsuite/gcc.dg/ubsan/pr111736.c	2024-03-21 13:53:33.789091054 +0100
> @@ -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;
> +}
> 
> 	Jakub
> 
> 

-- 
Richard Biener <rguenther@suse.de>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)

      reply	other threads:[~2024-03-22  8:08 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-22  8:04 Jakub Jelinek
2024-03-22  8:08 ` Richard Biener [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=r0r47410-r722-5438-5030-90rs20q5379r@fhfr.qr \
    --to=rguenther@suse.de \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).