public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Marc Poulhiès" <poulhies@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: Piotr Trojanek <trojanek@adacore.com>
Subject: [COMMITTED 05/31] ada: Do not leak tagged type names when Discard_Names is enabled
Date: Tue, 21 May 2024 09:30:08 +0200	[thread overview]
Message-ID: <20240521073035.314024-5-poulhies@adacore.com> (raw)
In-Reply-To: <20240521073035.314024-1-poulhies@adacore.com>

From: Piotr Trojanek <trojanek@adacore.com>

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.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 .../implementation_defined_pragmas.rst        |  6 ++++
 gcc/ada/exp_disp.adb                          |  5 +--
 gcc/ada/exp_put_image.adb                     | 34 ++++++++++++++-----
 gcc/ada/gnat_rm.texi                          |  6 ++++
 4 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/gcc/ada/doc/gnat_rm/implementation_defined_pragmas.rst b/gcc/ada/doc/gnat_rm/implementation_defined_pragmas.rst
index 0661670e047..7e4dd935342 100644
--- a/gcc/ada/doc/gnat_rm/implementation_defined_pragmas.rst
+++ b/gcc/ada/doc/gnat_rm/implementation_defined_pragmas.rst
@@ -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
 ========================
 
diff --git a/gcc/ada/exp_disp.adb b/gcc/ada/exp_disp.adb
index 601d463a8b0..66be77c9ffc 100644
--- a/gcc/ada/exp_disp.adb
+++ b/gcc/ada/exp_disp.adb
@@ -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
diff --git a/gcc/ada/exp_put_image.adb b/gcc/ada/exp_put_image.adb
index 09fbfa75eeb..94299e39661 100644
--- a/gcc/ada/exp_put_image.adb
+++ b/gcc/ada/exp_put_image.adb
@@ -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
 
diff --git a/gcc/ada/gnat_rm.texi b/gcc/ada/gnat_rm.texi
index 4dbbb036a25..4ff1de42db2 100644
--- a/gcc/ada/gnat_rm.texi
+++ b/gcc/ada/gnat_rm.texi
@@ -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
-- 
2.43.2


  parent reply	other threads:[~2024-05-21  7:30 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-21  7:30 [COMMITTED 01/31] ada: Add new Mingw task priority mapping Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 02/31] ada: Follow-up fix to previous change for Text_Ptr Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 03/31] ada: Remove trailing NUL in minimal expansion of Put_Image attribute Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 04/31] ada: Remove conversion from String_Id to String and back to String_Id Marc Poulhiès
2024-05-21  7:30 ` Marc Poulhiès [this message]
2024-05-21  7:30 ` [COMMITTED 06/31] ada: Update documentation of warning messages Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 07/31] ada: Fix index entry for an implemented AI feature Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 08/31] ada: Sort list of implemented Ada 2012 features Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 09/31] ada: Fix formatting in " Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 10/31] ada: Remove some explicit yields in tasking run-time Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 11/31] ada: Simplify management of scopes while inlining Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 12/31] ada: Add elaboration switch tags to info messages Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 13/31] ada: Remove useless trampolines caused by Unchecked_Conversion Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 14/31] ada: Remove duplicate statement Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 15/31] ada: Fix layout in a list of aspects Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 16/31] ada: Missing constraint check for initial value of object with address clause Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 17/31] ada: Fix oversight in previous change Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 18/31] ada: Fix small inaccuracy for Size attribute applied to objects Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 19/31] ada: Fix crash on aliased constant with packed array type and -g switch Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 20/31] ada: Fix assembler error for gigantic library-level object on 64-bit Windows Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 21/31] ada: Remove unused dependencies from gnatbind object list Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 22/31] ada: Avoid temporary for conditional expression of discriminated record type Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 23/31] ada: Follow-up adjustment to earlier fix in Build_Allocate_Deallocate_Proc Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 24/31] ada: Minor typo fix in comment Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 25/31] ada: Fix crash with aliased array and if expression Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 26/31] ada: Fix strict aliasing violation in parameter passing Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 27/31] ada: Make detection of useless copy for return more robust Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 28/31] ada: Fix strict aliasing violation in parameter passing (continued) Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 29/31] ada: Fix internal error on discriminated record with Atomic aspect in Ada 2022 Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 30/31] ada: Simplify test for propagation of attributes to subtypes Marc Poulhiès
2024-05-21  7:30 ` [COMMITTED 31/31] ada: Streamline implementation of simple nonbinary modular operations Marc Poulhiès

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=20240521073035.314024-5-poulhies@adacore.com \
    --to=poulhies@adacore.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=trojanek@adacore.com \
    /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).