From patchwork Tue May 21 07:30:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 90532 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 133C9384B06A for ; Tue, 21 May 2024 07:33:48 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id ED5DF3858C31 for ; Tue, 21 May 2024 07:30:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ED5DF3858C31 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org ED5DF3858C31 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716276657; cv=none; b=uzAj+dYso5LqGTRPTjhyrjxlTxKUfD1gvNRDcGA5+/7kKPFjCGnpDU97oYSJ1w9gUTmZnFw3jxX6czA1nSWHsm+QlraJ03nOf3zkKv6VEHTVVNdXCPfTzoq8h/9nanN/hlgzuaPROUGkIYTCyVl57yYzh07JhiSh7saawSp66hY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716276657; c=relaxed/simple; bh=b1uXOilsI1R/AkjksZ0fKH0HgWWTvowq8tinK2kwkQs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=WXJ1gXCR9qU49VU9JflC1EXDMyn0Ij2hSgDMfSLv/hynJg8AVo9rgXERtTflLnP+9GywGBzZPFcFabBWgsvptiDAIo/dYFSVvGnG/zk9P76TiTIH5IiWBh2nu8Y2aFozn0NIG7xwGYFpU4p1uUwp4kO7WtFOZyD82PdkCD/BzRU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-41fe54cb0e3so25560945e9.2 for ; Tue, 21 May 2024 00:30:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1716276651; x=1716881451; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SWuhrllePJ+czO6Lu3JOe75P44p828egcVLH0Z+RdNs=; b=Xx6ooF3Go57SzksQXGjg1XO7/WGqa0bpyBfHLovl9qWkl39XYLCx00DbYsjpXPhlAc RuyKhvGEM4MiIx0k/DKT0HFWXTe0jBqCrEnGyijzejUcNhgM06WYe3uZFCiqmA/HB37m 2h8v2T6Av6nPPhyNA5lKZNQvWJqD4VRFDUUlfjMM09bRKgEWkY1TZfESxMkhgN1EZJ02 ElDcFI/FR9uugkeNyjnPxHN+e8xERC7kGllrssPcxcLHQHS78zMWJVnyxR11kdfXyxdj 5xFlk/Ojrfj61HmJcnYtENP14aiFvB+uMO01ePkXN5n8+NcJrf8R7ruHZnz2WZl33aPQ 71Ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716276651; x=1716881451; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SWuhrllePJ+czO6Lu3JOe75P44p828egcVLH0Z+RdNs=; b=wJiatqBxqaVCuTKJmEiXWpbv8IAa3AX6LmWO6pVClF9c4JgHhN0zNL6dmvJ9ZwPjv8 M+95JQ1zGfhfM4yl3VM9r5EMEyWg50GtJnzpa+G3IJ4e2gR0T7D+hHeiu6MFtf+j5G/8 lZzk2mDO8ThjwhpPW1IFbryN+d/mMp1SXMijjqJXWmHv4MoIlZ3heHd6cnfSaB06Gnpp vsDOsZkfKCZPVxGCnMC2a7FwIOesaCQMi2YwMt7MinCSpiPjJp7m8BXsSO3pnpMwI3W3 sRHhN0Tn0z2fqsMFmtJLj6B/+ifvt2xTY6A4jR/0F5HMcK2VXPYWkDWBJyOxeoxY0vJE tbFA== X-Gm-Message-State: AOJu0YzspIoJostbg7U9TsxwyqLVu5OBP2np9WiXk3wLUfIg6JE0CFkz P+10DDYMRgzoFlgmtyYSCRKuZ4Dg57eAFYTwkfjCTC5y2seshTIODlyjBVHWt5i0tw63XgHWzdI = X-Google-Smtp-Source: AGHT+IE7aVbNrIuCgt96qAJkc9iLnHKMxw2buRD4GcEL3PiF3LV/iUEXE6QH1zCEyXWJrc46sWk6Ww== X-Received: by 2002:a05:600c:154b:b0:420:1fab:177f with SMTP id 5b1f17b1804b1-4201fab19e5mr164087535e9.30.1716276651706; Tue, 21 May 2024 00:30:51 -0700 (PDT) Received: from localhost.localdomain ([2001:861:3382:1a90:de37:8b1c:1f33:2610]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-41fccee9292sm453333645e9.37.2024.05.21.00.30.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 00:30:51 -0700 (PDT) From: =?utf-8?q?Marc_Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Piotr Trojanek 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 Message-ID: <20240521073035.314024-5-poulhies@adacore.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240521073035.314024-1-poulhies@adacore.com> References: <20240521073035.314024-1-poulhies@adacore.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org From: Piotr Trojanek 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