public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Matthew Malcomson <matmal01@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc(refs/vendors/ARM/heads/morello)] morello: Drop capability metadata when extracting a MEM_REF offset
Date: Fri, 10 Dec 2021 16:49:18 +0000 (GMT)	[thread overview]
Message-ID: <20211210164918.7F82D385801D@sourceware.org> (raw)

https://gcc.gnu.org/g:7a28f6ce56b5b02340f29c9862bfcda67f2f7e7a

commit 7a28f6ce56b5b02340f29c9862bfcda67f2f7e7a
Author: Stam Markianos-Wright <stam.markianos-wright@arm.com>
Date:   Tue Nov 2 11:38:15 2021 +0000

    morello: Drop capability metadata when extracting a MEM_REF offset
    
    `mem_ref_offset` is used throughout the compiler to extract the constant
    offset second parameter to of a MEM_REF as a `poly_offset_int`.
    
    This is then always used as part of arithmetic offset calculations.
    
    The fact that `mem_ref_offset` returns a 128-bit constant capability
    value (with null metadata: all the upper bits are zero) sometimes causes
    an issue in the arithmetic if, e.g. the offset value is involved in a
    shift left, where the upper bits of the constant get shifted into the upper
    metadata bits instead of overflowing.
    Similar issues, but more rarely, can also happen with all other arithmetic
    that can cause such an overflow.
    
    This patch ensures that `mem_ref_offset` only returns the lower constant
    capability value bits, thus making the arithmetic safer. It also asserts
    that all MEM_REF second operands accessed through `mem_ref_offset` must
    have null metadata: any failure of this would indicate an arithmetic
    overflow into the metadata caused somewhere earlier in the compiler.
    It also fixes a minor sign-extension bug in the metadata extraction
    utility function.

Diff:
---
 gcc/tree.c | 7 +++++--
 gcc/tree.h | 5 ++++-
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/gcc/tree.c b/gcc/tree.c
index 4b4f4fc3e61..982f198fd95 100644
--- a/gcc/tree.c
+++ b/gcc/tree.c
@@ -5041,8 +5041,11 @@ build_simple_mem_ref_loc (location_t loc, tree ptr)
 poly_offset_int
 mem_ref_offset (const_tree t)
 {
-  return poly_offset_int::from (wi::to_poly_wide (TREE_OPERAND (t, 1)),
-				SIGNED);
+  if (capability_type_p (TREE_TYPE (TREE_OPERAND (t, 1))))
+    gcc_assert (tree_constant_capability_metadata (TREE_OPERAND (t, 1)) == 0);
+  return poly_offset_int::from (wi::to_poly_wide
+				 (fold_drop_capability (TREE_OPERAND (t, 1))),
+							SIGNED);
 }
 
 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
diff --git a/gcc/tree.h b/gcc/tree.h
index 4a9e35361af..3519219927e 100644
--- a/gcc/tree.h
+++ b/gcc/tree.h
@@ -6373,7 +6373,10 @@ inline wide_int
 tree_constant_capability_metadata (const_tree t)
 {
   gcc_assert (TREE_CODE (t) == INTEGER_CST);
-  return wi::bit_and (wi::to_wide (t, TYPE_CAP_PRECISION (TREE_TYPE(t))),
+  return wi::bit_and (wide_int_storage::from
+		       (wi::to_wide (t, TYPE_NONCAP_PRECISION (TREE_TYPE(t))),
+				     TYPE_CAP_PRECISION (TREE_TYPE(t)),
+				     UNSIGNED),
 		      wi::mask (TYPE_NONCAP_PRECISION (TREE_TYPE(t)), 1,
 				TYPE_CAP_PRECISION (TREE_TYPE(t))));
 }


                 reply	other threads:[~2021-12-10 16:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20211210164918.7F82D385801D@sourceware.org \
    --to=matmal01@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /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).