Implements TARGET_MEMTAG_CAN_TAG_ADDRESSES and TARGET_MEMTAG_TAG_BITSIZE
for the RISC-V back end, allowing -fsanitize=hwaddress if the target
machine supports the pointer masking extension.

------

gcc/ChangeLog:

        * config/riscv/riscv.cc (riscv_can_tag_addresses): New function.
        (RISCV_HWASAN_TAG_SIZE): New definition.
        (riscv_memtag_tag_bitsize): New function.
        (TARGET_MEMTAG_CAN_TAG_ADDRESSES): New definition.
        (TARGET_MEMTAG_TAG_BITSIZE): Likewise.

Signed-off-by: Maximilian Ciric <[email protected]>
---
 gcc/config/riscv/riscv.cc | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 949616a63..f23690d03 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -16415,6 +16415,38 @@ riscv_prefetch_offset_address_p (rtx x, machine_mode 
mode)
   return true;
 }
 
+
+/* Implement TARGET_MEMTAG_CAN_TAG_ADDRESSES.
+ * Enables -fsanitize=hwaddress if true.  */
+
+bool
+riscv_can_tag_addresses ()
+{
+  /* Tagging is possible when a pointer masking extension is available.  */
+  bool target_has_pointer_masking_p = TARGET_SMMPM
+                                  || TARGET_SMNPM
+                                  || TARGET_SSNPM
+                                  || TARGET_SSPM
+                                  || TARGET_SUPM;
+  return TARGET_64BIT && target_has_pointer_masking_p;
+}
+
+
+/* libsanitizer expects 8 tag bits.  */
+
+#define RISCV_HWASAN_TAG_SIZE 8
+
+
+/* Implement TARGET_MEMTAG_TAG_BITSIZE.
+ * Communicates how many unused pointer bits are used for tagging.  */
+
+unsigned char
+riscv_memtag_tag_bitsize ()
+{
+  return RISCV_HWASAN_TAG_SIZE;
+}
+
+
 /* Initialize the GCC target structure.  */
 #undef TARGET_ASM_ALIGNED_HI_OP
 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
@@ -16834,6 +16866,12 @@ riscv_prefetch_offset_address_p (rtx x, machine_mode 
mode)
 #define TARGET_GET_FUNCTION_VERSIONS_DISPATCHER \
   riscv_get_function_versions_dispatcher
 
+#undef TARGET_MEMTAG_CAN_TAG_ADDRESSES
+#define TARGET_MEMTAG_CAN_TAG_ADDRESSES riscv_can_tag_addresses
+
+#undef TARGET_MEMTAG_TAG_BITSIZE
+#define TARGET_MEMTAG_TAG_BITSIZE riscv_memtag_tag_bitsize
+
 #undef TARGET_DOCUMENTATION_NAME
 #define TARGET_DOCUMENTATION_NAME "RISC-V"
 
-- 
2.34.1

Reply via email to