aboutsummaryrefslogtreecommitdiff
path: root/gcc/ada/exp_smem.adb
diff options
context:
space:
mode:
authorHristian Kirtchev <kirtchev@adacore.com>2018-01-11 08:54:12 +0000
committerPierre-Marie de Rodat <pmderodat@gcc.gnu.org>2018-01-11 08:54:12 +0000
commit6560f85165c084768a77dda7086269604781ce5e (patch)
treed03f65e8902bd3c00fd505ba13d5b1edf9dd9239 /gcc/ada/exp_smem.adb
parentc9e8030617cb310758929ee622cc8bff2372bb55 (diff)
downloadgcc-6560f85165c084768a77dda7086269604781ce5e.zip
gcc-6560f85165c084768a77dda7086269604781ce5e.tar.gz
gcc-6560f85165c084768a77dda7086269604781ce5e.tar.bz2
[Ada] Secondary stack leaks during object initialization
This patch modifies the transient scope mechanism to prevent secondary stack leaks during object initialization. The modifications are as follows: 1) Prior to this change, the secondary stack was never managed within type initialization procedures, for reasons unknown. It is speculated that the controlled type model used at that time may have influenced this decision. The secondary stack is now managed within type initialization procedures in order to recover the memory once individual components or whole objects are initialized. 2) A transient scope now delegates the secondary stack management to an enclosing scope if there is no suitable context to wrap. This ensures that the requirement to manage the secondary stack is not lost when the scope was established for that purpose in mind. 3) A previous mechanism which examined the definition of a type (recursively) to determine whether the type will involve the secondary stack was removed because a) the mechanism could not detect this need with certainty, and b) the trigger for secondary stack usage is now moved to the resolution of function calls, which is always accurate. ------------ -- Source -- ------------ -- types.ads with Ada.Finalization; use Ada.Finalization; package Types is type Ctrl is new Controlled with record Id : Integer; end record; procedure Initialize (Obj : in out Ctrl); function Make_Ctrl return Ctrl; function Make_Ctrl_From (Obj : Ctrl) return Ctrl; type Constr is array (1 .. 3) of Ctrl; type Unconstr is array (Integer range <>) of Ctrl; function Make_Constr return Constr; function Make_Unconstr (Low : Integer; High : Integer) return Unconstr; type Rec_1 is new Controlled with record Comp : Ctrl := Make_Ctrl; end record; type Rec_2 is new Controlled with record Comp : Ctrl := Make_Ctrl_From (Make_Ctrl); end record; type Rec_3 is new Controlled with record Comp : Constr := Make_Constr; end record; type Rec_4 is new Controlled with record Comp : Unconstr (1 .. 3) := Make_Unconstr (1, 3); end record; type Rec_5 is record Comp : Integer := 1 + Make_Ctrl.Id; end record; type Rec_6 is record Comp : Boolean := (for all X in 1 .. Make_Ctrl.Id => X = Make_Ctrl.Id); end record; end Types; -- types.adb package body Types is Id_Gen : Integer := 0; procedure Initialize (Obj : in out Ctrl) is begin Id_Gen := Id_Gen + 1; Obj.Id := Id_Gen; end Initialize; function Make_Constr return Constr is Result : constant Constr := (others => Make_Ctrl); begin return Result; end Make_Constr; function Make_Ctrl return Ctrl is Result : Ctrl; begin return Result; end Make_Ctrl; function Make_Ctrl_From (Obj : Ctrl) return Ctrl is Result : Ctrl; begin Result.Id := Obj.Id; return Result; end Make_Ctrl_From; function Make_Unconstr (Low : Integer; High : Integer) return Unconstr is Result : constant Unconstr (Low .. High) := (others => Make_Ctrl); begin return Result; end Make_Unconstr; end Types; -- maker.ads generic type Obj_Typ is private; procedure Maker (Count : Positive); -- maker.adb procedure Maker (Count : Positive) is procedure Create is Obj : Obj_Typ; pragma Warnings (Off, Obj); begin null; end Create; begin for Iter in 1 .. Count loop Create; end loop; end Maker; -- leaks.adb with Maker; with Types; use Types; with Maker; with Types; use Types; procedure Leaks is procedure Make_1 is new Maker (Rec_1); procedure Make_2 is new Maker (Rec_2); procedure Make_3 is new Maker (Rec_3); procedure Make_4 is new Maker (Rec_4); procedure Make_5 is new Maker (Rec_5); procedure Make_6 is new Maker (Rec_6); begin Make_1 (5_000); Make_2 (5_000); Make_3 (5_000); Make_4 (5_000); Make_5 (5_000); Make_6 (5_000); end Leaks; ---------------------------- -- Compilation and output -- ---------------------------- $ gnatmake -q leaks.adb $ valgrind ./leaks > leaks.txt 2>&1 $ grep -c "still reachable" leaks.txt 0 2018-01-11 Hristian Kirtchev <kirtchev@adacore.com> gcc/ada/ * exp_aggr.adb (Convert_Aggr_In_Object_Decl): Update the call to Establish_Transient_Scope. (Convert_To_Assignments): Update the call to Establish_Transient_Scope. (Expand_Array_Aggregate): Update the call to Establish_Transient_Scope. * exp_ch6.adb (Expand_Call_Helper): Update the call to Establish_Transient_Scope. (Make_Build_In_Place_Call_In_Object_Declaration): Update the call to Establish_Transient_Scope. * exp_ch7.adb (Establish_Transient_Scope): Restructured. Delegate the management of the secondary stack to an enclosing scope if there is no suitable construct to wrap, and the transient scope was intended to manage the secondary stack. (Find_Node_To_Be_Wrapped): Restructured. A case_statement_alternative is a valid boundary for a transient expression which comes from the statements of the alternative, otherwise alternatives cannot be wrapped. Assignments of controlled objects which have controlled actions suppressed now stop the traversal as there is no point in looking for an enclosing construct. Add several N_xxx_Body choices to the termination conditions for completeness. * exp_ch7.ads (Establish_Transient_Scope): Update the parameter profile and the associated comment on usage. * exp_smem.adb (Add_Shared_Var_Lock_Procs): Update the call to Establish_Transient_Scope. (Add_Write_After): Update the call to Establish_Transient_Scope. * sem_res.adb (Check_Initialization_Call): Removed. (Resolve_Actuals): Account for additional cases where finalization actions are required by utilizing predicate Needs_Finalization rather than Is_Controlled. (Resolve_Call): Type initialization procedures can now utilize transient scopes to manage the secondary stack, thus preventing leaks during initialization. Remove the previous kludgy algorithm which attempts to manage the secondary stack at the object creation site. From-SVN: r256513
Diffstat (limited to 'gcc/ada/exp_smem.adb')
-rw-r--r--gcc/ada/exp_smem.adb10
1 files changed, 6 insertions, 4 deletions
diff --git a/gcc/ada/exp_smem.adb b/gcc/ada/exp_smem.adb
index 387b32f..a5cbab5 100644
--- a/gcc/ada/exp_smem.adb
+++ b/gcc/ada/exp_smem.adb
@@ -6,7 +6,7 @@
-- --
-- B o d y --
-- --
--- Copyright (C) 1998-2014, Free Software Foundation, Inc. --
+-- Copyright (C) 1998-2018, Free Software Foundation, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
@@ -207,7 +207,7 @@ package body Exp_Smem is
else
Insert_Action (N, Vde);
- Establish_Transient_Scope (N, Sec_Stack => False);
+ Establish_Transient_Scope (N, Manage_Sec_Stack => False);
end if;
-- Mark object as locked in the current (transient) scope
@@ -255,13 +255,15 @@ package body Exp_Smem is
---------------------
procedure Add_Write_After (N : Node_Id) is
- Loc : constant Source_Ptr := Sloc (N);
Ent : constant Entity_Id := Entity (N);
+ Loc : constant Source_Ptr := Sloc (N);
Par : constant Node_Id := Insert_Node;
+
begin
if Present (Shared_Var_Procs_Instance (Ent)) then
if Nkind (Insert_Node) = N_Function_Call then
- Establish_Transient_Scope (Insert_Node, Sec_Stack => False);
+ Establish_Transient_Scope (Insert_Node, Manage_Sec_Stack => False);
+
Store_After_Actions_In_Scope (New_List (
Build_Shared_Var_Proc_Call (Loc, Ent, Name_Write)));
else