public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/ARM/heads/morello)] morello: Drop capability metadata when extracting a MEM_REF offset
@ 2021-12-10 16:49 Matthew Malcomson
  0 siblings, 0 replies; only message in thread
From: Matthew Malcomson @ 2021-12-10 16:49 UTC (permalink / raw)
  To: gcc-cvs

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))));
 }


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

only message in thread, other threads:[~2021-12-10 16:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-10 16:49 [gcc(refs/vendors/ARM/heads/morello)] morello: Drop capability metadata when extracting a MEM_REF offset Matthew Malcomson

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).