]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Preserve capability validity in address arithmetic
authorDaniel King <dmking@adacore.com>
Tue, 1 Aug 2023 13:39:39 +0000 (14:39 +0100)
committerMarc Poulhiès <poulhies@adacore.com>
Tue, 5 Sep 2023 11:05:12 +0000 (13:05 +0200)
On CHERI targets where System.Address is a capability, arithmetic on
addresses should avoid converting to integers and instead use the
operations defined in System.Storage_Elements to perform the arithmetic
directly on the System.Address object. This preserves the capability's
validity throughout the calculation, ensuring that the resulting capability
can be dereferenced.

gcc/ada/

* libgnat/s-carsi8.adb: Use operations from
System.Storage_Elements for address arithmetic.
* libgnat/s-carun8.adb: Likewise
* libgnat/s-casi128.adb: Likewise
* libgnat/s-casi16.adb: Likewise
* libgnat/s-casi32.adb: Likewise
* libgnat/s-casi64.adb: Likewise
* libgnat/s-caun128.adb: Likewise
* libgnat/s-caun16.adb: Likewise
* libgnat/s-caun32.adb: Likewise
* libgnat/s-caun64.adb: Likewise
* libgnat/s-geveop.adb: Likewise

gcc/ada/libgnat/s-carsi8.adb
gcc/ada/libgnat/s-carun8.adb
gcc/ada/libgnat/s-casi128.adb
gcc/ada/libgnat/s-casi16.adb
gcc/ada/libgnat/s-casi32.adb
gcc/ada/libgnat/s-casi64.adb
gcc/ada/libgnat/s-caun128.adb
gcc/ada/libgnat/s-caun16.adb
gcc/ada/libgnat/s-caun32.adb
gcc/ada/libgnat/s-caun64.adb
gcc/ada/libgnat/s-geveop.adb

index 839f157a2eeba05b3c872edb4e05303ba09d2b11..3946d474dd95211082d1c602ddc16bf0f5bab2f6 100644 (file)
@@ -30,6 +30,7 @@
 ------------------------------------------------------------------------------
 
 with System.Address_Operations; use System.Address_Operations;
+with System.Storage_Elements;   use System.Storage_Elements;
 
 with Ada.Unchecked_Conversion;
 
@@ -94,8 +95,8 @@ package body System.Compare_Array_Signed_8 is
          for J in 0 .. Words_To_Compare - 1 loop
             if LeftP (J) /= RightP (J) then
                return Compare_Array_S8_Unaligned
-                        (AddA (Left,  Address (4 * J)),
-                         AddA (Right, Address (4 * J)),
+                        (Left  + Storage_Offset (4 * J),
+                         Right + Storage_Offset (4 * J),
                          4, 4);
             end if;
          end loop;
@@ -108,8 +109,8 @@ package body System.Compare_Array_Signed_8 is
          --    * Words_To_Compare = Compare_Len / 4
          --    * Bytes_Compared_As_Words = Words_To_Compare * 4
          return Compare_Array_S8_Unaligned
-                  (AddA (Left,  Address (Bytes_Compared_As_Words)),
-                   AddA (Right, Address (Bytes_Compared_As_Words)),
+                        (Left  + Storage_Offset (Bytes_Compared_As_Words),
+                         Right + Storage_Offset (Bytes_Compared_As_Words),
                    Left_Len  - Bytes_Compared_As_Words,
                    Right_Len - Bytes_Compared_As_Words);
       end;
index b20e4e1b92221994c9adc0bf2bdbd6059513ae60..e6938def56a7e9c3211320d7894fa7179c700eff 100644 (file)
@@ -30,6 +30,7 @@
 ------------------------------------------------------------------------------
 
 with System.Address_Operations; use System.Address_Operations;
+with System.Storage_Elements;   use System.Storage_Elements;
 
 with Ada.Unchecked_Conversion;
 
@@ -93,8 +94,8 @@ package body System.Compare_Array_Unsigned_8 is
          for J in 0 .. Words_To_Compare - 1 loop
             if LeftP (J) /= RightP (J) then
                return Compare_Array_U8_Unaligned
-                        (AddA (Left,  Address (4 * J)),
-                         AddA (Right, Address (4 * J)),
+                        (Left  + Storage_Offset (4 * J),
+                         Right + Storage_Offset (4 * J),
                          4, 4);
             end if;
          end loop;
@@ -107,8 +108,8 @@ package body System.Compare_Array_Unsigned_8 is
          --    * Words_To_Compare = Compare_Len / 4
          --    * Bytes_Compared_As_Words = Words_To_Compare * 4
          return Compare_Array_U8_Unaligned
-                  (AddA (Left,  Address (Bytes_Compared_As_Words)),
-                   AddA (Right, Address (Bytes_Compared_As_Words)),
+                  (Left  + Storage_Offset (Bytes_Compared_As_Words),
+                   Right + Storage_Offset (Bytes_Compared_As_Words),
                    Left_Len  - Bytes_Compared_As_Words,
                    Right_Len - Bytes_Compared_As_Words);
       end;
index 2b0caac75b236700033abdc356cb9cc1b5a88733..91569e1091d0603320bdc16361a75c6fe9ff6c2b 100644 (file)
@@ -30,6 +30,7 @@
 ------------------------------------------------------------------------------
 
 with System.Address_Operations; use System.Address_Operations;
+with System.Storage_Elements;   use System.Storage_Elements;
 
 with Ada.Unchecked_Conversion;
 
@@ -80,8 +81,8 @@ package body System.Compare_Array_Signed_128 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 16);
-            R := AddA (R, 16);
+            L := L + Storage_Offset (16);
+            R := R + Storage_Offset (16);
          end loop;
 
       --  Case of going by unaligned quadruple words
@@ -97,8 +98,8 @@ package body System.Compare_Array_Signed_128 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 16);
-            R := AddA (R, 16);
+            L := L + Storage_Offset (16);
+            R := R + Storage_Offset (16);
          end loop;
       end if;
 
index fa529c9d559c67dbf8f752ae5b498fdefbac4542..8aa5502dc03ce682b494962236d05dba174ca485 100644 (file)
@@ -30,6 +30,7 @@
 ------------------------------------------------------------------------------
 
 with System.Address_Operations; use System.Address_Operations;
+with System.Storage_Elements;   use System.Storage_Elements;
 
 with Ada.Unchecked_Conversion;
 
@@ -82,8 +83,8 @@ package body System.Compare_Array_Signed_16 is
            and then W (L).all = W (R).all
          loop
             Clen := Clen - 2;
-            L := AddA (L, 4);
-            R := AddA (R, 4);
+            L := L + Storage_Offset (4);
+            R := R + Storage_Offset (4);
          end loop;
       end if;
 
@@ -100,8 +101,8 @@ package body System.Compare_Array_Signed_16 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 2);
-            R := AddA (R, 2);
+            L := L + Storage_Offset (2);
+            R := R + Storage_Offset (2);
          end loop;
 
       --  Case of going by unaligned half words
@@ -117,8 +118,8 @@ package body System.Compare_Array_Signed_16 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 2);
-            R := AddA (R, 2);
+            L := L + Storage_Offset (2);
+            R := R + Storage_Offset (2);
          end loop;
       end if;
 
index 7ed9ec5c5199c0a3bf6667e05f7aa4dcf66adf41..f42d5e06db75a96e5e920eb0e6ba722caea4e633 100644 (file)
@@ -30,6 +30,7 @@
 ------------------------------------------------------------------------------
 
 with System.Address_Operations; use System.Address_Operations;
+with System.Storage_Elements;   use System.Storage_Elements;
 
 with Ada.Unchecked_Conversion;
 
@@ -83,8 +84,8 @@ package body System.Compare_Array_Signed_32 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 4);
-            R := AddA (R, 4);
+            L := L + Storage_Offset (4);
+            R := R + Storage_Offset (4);
          end loop;
 
       --  Case of going by unaligned words
@@ -100,8 +101,8 @@ package body System.Compare_Array_Signed_32 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 4);
-            R := AddA (R, 4);
+            L := L + Storage_Offset (4);
+            R := R + Storage_Offset (4);
          end loop;
       end if;
 
index f0211107baf97c447786d7a5a77de85e0c8619d2..d0c8f1c1859e05e1fd6d02b224e34521af6f938e 100644 (file)
@@ -30,6 +30,7 @@
 ------------------------------------------------------------------------------
 
 with System.Address_Operations; use System.Address_Operations;
+with System.Storage_Elements;   use System.Storage_Elements;
 
 with Ada.Unchecked_Conversion;
 
@@ -83,8 +84,8 @@ package body System.Compare_Array_Signed_64 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 8);
-            R := AddA (R, 8);
+            L := L + Storage_Offset (8);
+            R := R + Storage_Offset (8);
          end loop;
 
       --  Case of going by unaligned double words
@@ -100,8 +101,8 @@ package body System.Compare_Array_Signed_64 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 8);
-            R := AddA (R, 8);
+            L := L + Storage_Offset (8);
+            R := R + Storage_Offset (8);
          end loop;
       end if;
 
index 00f2d8cfd78a9054d2bc17396f2cfdc56678f8ef..85b350b50b85835dcc4d75fed6b6db171febe7e0 100644 (file)
@@ -30,6 +30,7 @@
 ------------------------------------------------------------------------------
 
 with System.Address_Operations; use System.Address_Operations;
+with System.Storage_Elements;   use System.Storage_Elements;
 
 with Ada.Unchecked_Conversion;
 
@@ -79,8 +80,8 @@ package body System.Compare_Array_Unsigned_128 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 16);
-            R := AddA (R, 16);
+            L := L + Storage_Offset (16);
+            R := R + Storage_Offset (16);
          end loop;
 
       --  Case of going by unaligned quadruple words
@@ -96,8 +97,8 @@ package body System.Compare_Array_Unsigned_128 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 16);
-            R := AddA (R, 16);
+            L := L + Storage_Offset (16);
+            R := R + Storage_Offset (16);
          end loop;
       end if;
 
index 43bf35b907a79e5c189ed7b5e62b717e9ea26ec5..a082e61bf8e96348aad1abfce27eee6a70634824 100644 (file)
@@ -30,6 +30,7 @@
 ------------------------------------------------------------------------------
 
 with System.Address_Operations; use System.Address_Operations;
+with System.Storage_Elements;   use System.Storage_Elements;
 
 with Ada.Unchecked_Conversion;
 
@@ -82,8 +83,8 @@ package body System.Compare_Array_Unsigned_16 is
            and then W (L).all = W (R).all
          loop
             Clen := Clen - 2;
-            L := AddA (L, 4);
-            R := AddA (R, 4);
+            L := L + Storage_Offset (4);
+            R := R + Storage_Offset (4);
          end loop;
       end if;
 
@@ -100,8 +101,8 @@ package body System.Compare_Array_Unsigned_16 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 2);
-            R := AddA (R, 2);
+            L := L + Storage_Offset (2);
+            R := R + Storage_Offset (2);
          end loop;
 
       --  Case of going by unaligned half words
@@ -117,8 +118,8 @@ package body System.Compare_Array_Unsigned_16 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 2);
-            R := AddA (R, 2);
+            L := L + Storage_Offset (2);
+            R := R + Storage_Offset (2);
          end loop;
       end if;
 
index 0a5ca12144e9f95f501a144652c64986c2421e1e..72ac399cd99466856ee47c940d65a44d4660d0fa 100644 (file)
@@ -30,6 +30,7 @@
 ------------------------------------------------------------------------------
 
 with System.Address_Operations; use System.Address_Operations;
+with System.Storage_Elements;   use System.Storage_Elements;
 
 with Ada.Unchecked_Conversion;
 
@@ -83,8 +84,8 @@ package body System.Compare_Array_Unsigned_32 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 4);
-            R := AddA (R, 4);
+            L := L + Storage_Offset (4);
+            R := R + Storage_Offset (4);
          end loop;
 
       --  Case of going by unaligned words
@@ -100,8 +101,8 @@ package body System.Compare_Array_Unsigned_32 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 4);
-            R := AddA (R, 4);
+            L := L + Storage_Offset (4);
+            R := R + Storage_Offset (4);
          end loop;
       end if;
 
index cca2069a62b3d83869d36972315da8fd7b2c1e74..e42469756545945b5f3df5412b232285f43b86d6 100644 (file)
@@ -30,6 +30,7 @@
 ------------------------------------------------------------------------------
 
 with System.Address_Operations; use System.Address_Operations;
+with System.Storage_Elements;   use System.Storage_Elements;
 
 with Ada.Unchecked_Conversion;
 
@@ -82,8 +83,8 @@ package body System.Compare_Array_Unsigned_64 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 8);
-            R := AddA (R, 8);
+            L := L + Storage_Offset (8);
+            R := R + Storage_Offset (8);
          end loop;
 
       --  Case of going by unaligned double words
@@ -99,8 +100,8 @@ package body System.Compare_Array_Unsigned_64 is
             end if;
 
             Clen := Clen - 1;
-            L := AddA (L, 8);
-            R := AddA (R, 8);
+            L := L + Storage_Offset (8);
+            R := R + Storage_Offset (8);
          end loop;
       end if;
 
index 1221d356683a453bfbbdbda8f514ec86d7be15be..502ada299adfa5ac6a4cf679f309e8c63f3bdfb2 100644 (file)
@@ -36,9 +36,10 @@ with Ada.Unchecked_Conversion;
 
 package body System.Generic_Vector_Operations is
 
-   IU : constant Integer := Integer (Storage_Unit);
-   VU : constant Address := Address (Vectors.Vector'Size / IU);
-   EU : constant Address := Address (Element_Array'Component_Size / IU);
+   IU : constant Integer       := Integer (Storage_Unit);
+   VU : constant Storage_Count := Storage_Count (Vectors.Vector'Size / IU);
+   EU : constant Storage_Count :=
+          Storage_Count (Element_Array'Component_Size / IU);
 
    ----------------------
    -- Binary_Operation --
@@ -53,10 +54,10 @@ package body System.Generic_Vector_Operations is
       YA : Address := Y;
       --  Address of next element to process in R, X and Y
 
-      VI : constant Integer_Address := To_Integer (VU);
+      VI : constant Integer_Address := Integer_Address (VU);
 
       Unaligned : constant Integer_Address :=
-                    Boolean'Pos (ModA (OrA (OrA (RA, XA), YA), VU) /= 0) - 1;
+                    Boolean'Pos (OrA (OrA (RA, XA), YA) mod VU /= 0) - 1;
       --  Zero iff one or more argument addresses is not aligned, else all 1's
 
       type Vector_Ptr is access all Vectors.Vector;
@@ -73,23 +74,23 @@ package body System.Generic_Vector_Operations is
       --  Vector'Size > Storage_Unit
       --  VI > 0
       SA : constant Address :=
-             AddA (XA, To_Address
-                         ((Integer_Address (Length) / VI * VI) and Unaligned));
+             XA + Storage_Offset
+                    ((Integer_Address (Length) / VI * VI) and Unaligned);
       --  First address of argument X to start serial processing
 
    begin
       while XA < SA loop
          VP (RA).all := Vector_Op (VP (XA).all, VP (YA).all);
-         XA := AddA (XA, VU);
-         YA := AddA (YA, VU);
-         RA := AddA (RA, VU);
+         XA := XA + VU;
+         YA := YA + VU;
+         RA := RA + VU;
       end loop;
 
       while XA < X + Length loop
          EP (RA).all := Element_Op (EP (XA).all, EP (YA).all);
-         XA := AddA (XA, EU);
-         YA := AddA (YA, EU);
-         RA := AddA (RA, EU);
+         XA := XA + EU;
+         YA := YA + EU;
+         RA := RA + EU;
       end loop;
    end Binary_Operation;
 
@@ -105,10 +106,10 @@ package body System.Generic_Vector_Operations is
       XA : Address := X;
       --  Address of next element to process in R and X
 
-      VI : constant Integer_Address := To_Integer (VU);
+      VI : constant Integer_Address := Integer_Address (VU);
 
       Unaligned : constant Integer_Address :=
-                    Boolean'Pos (ModA (OrA (RA, XA), VU) /= 0) - 1;
+                    Boolean'Pos (OrA (RA, XA) mod VU /= 0) - 1;
       --  Zero iff one or more argument addresses is not aligned, else all 1's
 
       type Vector_Ptr is access all Vectors.Vector;
@@ -125,21 +126,21 @@ package body System.Generic_Vector_Operations is
       --  Vector'Size > Storage_Unit
       --  VI > 0
       SA : constant Address :=
-             AddA (XA, To_Address
-                         ((Integer_Address (Length) / VI * VI) and Unaligned));
+             XA + Storage_Offset
+                    ((Integer_Address (Length) / VI * VI) and Unaligned);
       --  First address of argument X to start serial processing
 
    begin
       while XA < SA loop
          VP (RA).all := Vector_Op (VP (XA).all);
-         XA := AddA (XA, VU);
-         RA := AddA (RA, VU);
+         XA := XA + VU;
+         RA := RA + VU;
       end loop;
 
       while XA < X + Length loop
          EP (RA).all := Element_Op (EP (XA).all);
-         XA := AddA (XA, EU);
-         RA := AddA (RA, EU);
+         XA := XA + EU;
+         RA := RA + EU;
       end loop;
    end Unary_Operation;