]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Do not leak tagged type names when Discard_Names is enabled
authorPiotr Trojanek <trojanek@adacore.com>
Fri, 22 Mar 2024 22:11:10 +0000 (23:11 +0100)
committerMarc Poulhiès <poulhies@adacore.com>
Tue, 21 May 2024 07:26:45 +0000 (09:26 +0200)
When both pragmas Discard_Names and No_Tagged_Streams apply to a tagged
type, the intended behavior is to prevent type names from leaking into
object code, as documented in GNAT RM.

However, while Discard_Names can be used as a configuration pragma,
No_Tagged_Streams must be applied to each type separately. This patch
enables the use of restriction No_Streams, which can be activated
globally, instead of No_Tagged_Streams on individual types.

When no tagged stream object can be created and allocated, then routines
that make use of the External_Tag won't be used.

gcc/ada/

* doc/gnat_rm/implementation_defined_pragmas.rst
(No_Tagged_Streams): Document how to avoid exposing entity names
for the entire partition.
* exp_disp.adb (Make_DT): Make use of restriction No_Streams.
* exp_put_image.adb (Build_Record_Put_Image_Procedure): Respect
Discard_Names in the generated Put_Image procedure.
* gnat_rm.texi: Regenerate.

gcc/ada/doc/gnat_rm/implementation_defined_pragmas.rst
gcc/ada/exp_disp.adb
gcc/ada/exp_put_image.adb
gcc/ada/gnat_rm.texi

index 0661670e0475159d104ce997896e4011a6bb75f7..7e4dd9353426fb8326a391547ddc33f32b94070c 100644 (file)
@@ -4000,6 +4000,12 @@ applied to a tagged type its Expanded_Name and External_Tag are initialized
 with empty strings. This is useful to avoid exposing entity names at binary
 level but has a negative impact on the debuggability of tagged types.
 
+Alternatively, when pragmas ``Discard_Names`` and ``Restrictions (No_Streams)``
+simultanously apply to a tagged type, its Expanded_Name and External_Tag are
+also initialized with empty strings. In particular, both these pragmas can be
+applied as configuration pragmas to avoid exposing entity names at binary
+level for the entire parition.
+
 Pragma Normalize_Scalars
 ========================
 
index 601d463a8b0303ee42149155065f5c3b7cb686ab..66be77c9ffca4d2c4e79e7672702e9ed8332c510 100644 (file)
@@ -4600,8 +4600,9 @@ package body Exp_Disp is
       --        streams.
 
       Discard_Names : constant Boolean :=
-                        Present (No_Tagged_Streams_Pragma (Typ))
-                          and then
+        (Present (No_Tagged_Streams_Pragma (Typ))
+           or else Restriction_Active (No_Streams))
+          and then
         (Global_Discard_Names or else Einfo.Entities.Discard_Names (Typ));
 
       --  The following name entries are used by Make_DT to generate a number
index 09fbfa75eeba59d579111abedb8e11329a42d217..94299e39661028c0f13300c88b8e7ca02e5a7be7 100644 (file)
@@ -44,6 +44,7 @@ with Sinfo.Nodes;    use Sinfo.Nodes;
 with Sinfo.Utils;    use Sinfo.Utils;
 with Snames;         use Snames;
 with Stand;
+with Stringt;        use Stringt;
 with Tbuild;         use Tbuild;
 with Ttypes;         use Ttypes;
 with Uintp;          use Uintp;
@@ -825,14 +826,31 @@ package body Exp_Put_Image is
               Make_Raise_Program_Error (Loc,
               Reason => PE_Explicit_Raise));
          else
-            Append_To (Stms,
-              Make_Procedure_Call_Statement (Loc,
-                Name => New_Occurrence_Of (RTE (RE_Put_Image_Unknown), Loc),
-                Parameter_Associations => New_List
-                  (Make_Identifier (Loc, Name_S),
-                   Make_String_Literal (Loc,
-                     Fully_Qualified_Name_String
-                       (Btyp, Append_NUL => False)))));
+            declare
+               Type_Name : String_Id;
+            begin
+               --  If aspect Discard_Names is enabled the intention is to
+               --  prevent type names from leaking into object file. Instead,
+               --  we emit string that is different from the ones from the
+               --  default implementations of the Put_Image attribute.
+
+               if Global_Discard_Names or else Discard_Names (Typ) then
+                  Start_String;
+                  Store_String_Chars ("(DISCARDED TYPE NAME)");
+                  Type_Name := End_String;
+               else
+                  Type_Name :=
+                    Fully_Qualified_Name_String (Btyp, Append_NUL => False);
+               end if;
+
+               Append_To (Stms,
+                 Make_Procedure_Call_Statement (Loc,
+                   Name => New_Occurrence_Of (RTE (RE_Put_Image_Unknown), Loc),
+                   Parameter_Associations => New_List
+                     (Make_Identifier (Loc, Name_S),
+                        Make_String_Literal (Loc,
+                          Type_Name))));
+            end;
          end if;
       elsif Is_Null_Record_Type (Btyp, Ignore_Privacy => True) then
 
index 4dbbb036a259e2cdde76009b5a2ece615f01d873..4ff1de42db2c9dbbb50b03c7100c7c5699f57fb9 100644 (file)
@@ -5535,6 +5535,12 @@ applied to a tagged type its Expanded_Name and External_Tag are initialized
 with empty strings. This is useful to avoid exposing entity names at binary
 level but has a negative impact on the debuggability of tagged types.
 
+Alternatively, when pragmas @code{Discard_Names} and @code{Restrictions (No_Streams)}
+simultanously apply to a tagged type, its Expanded_Name and External_Tag are
+also initialized with empty strings. In particular, both these pragmas can be
+applied as configuration pragmas to avoid exposing entity names at binary
+level for the entire parition.
+
 @node Pragma Normalize_Scalars,Pragma Obsolescent,Pragma No_Tagged_Streams,Implementation Defined Pragmas
 @anchor{gnat_rm/implementation_defined_pragmas pragma-normalize-scalars}@anchor{b0}
 @section Pragma Normalize_Scalars