Commit bd5ed03a by Javier Miranda Committed by Pierre-Marie de Rodat

[Ada] Buffer reading overflow in dispatch table initialization

For tagged types not defined at library level that derive from library
level tagged types the compiler may generate code to initialize their
dispatch table of predefined primitives copying from the parent type
data stored in memory after the dispatch table of the parent; that is,
at runtime the initialization of dispatch tables overflows reading the
parent dispatch table.

This problem does not affect the execution of the program since the
target dispatch table always has enough space to store the extra data,
and after such copy the compiler generates code to complete the
initialization of the dispatch table.

The following test must compile and execute without errors.

package pkg_a is
   type Root is tagged null record;
end pkg_a;

with pkg_a;
procedure main is
   type Derived is new pkg_a.Root with null record;  -- Test
begin
   null;
end main;

Command: gnatmake -q main -fsanitize=address; ./main

2019-08-19  Javier Miranda  <miranda@adacore.com>

gcc/ada/

	PR ada/65696
	* exp_atag.ads, exp_atag.adb (Build_Inherit_Predefined_Prims):
	Adding formal to specify how many predefined primitives are
	inherited from the parent type.
	* exp_disp.adb (Number_Of_Predefined_Prims): New subprogram.
	(Make_Secondary_DT): Compute the number of predefined primitives
	of all tagged types (including tagged types not defined at
	library level).  Previously we unconditionally relied on the
	Max_Predef_Prims constant value when building the dispatch
	tables of tagged types not defined at library level (thus
	consuming more memory for their dispatch tables than required).
	(Make_DT): Compute the number of predefined primitives that must
	be inherited from their parent type when building the dispatch
	tables of tagged types not defined at library level. Previously
	we unconditionally relied on the Max_Predef_Prims constant value
	when building the dispatch tables of tagged types not defined at
	library level (thus copying more data than required from the
	parent type).

From-SVN: r274654
parent d403cfad
2019-08-19 Javier Miranda <miranda@adacore.com>
PR ada/65696
* exp_atag.ads, exp_atag.adb (Build_Inherit_Predefined_Prims):
Adding formal to specify how many predefined primitives are
inherited from the parent type.
* exp_disp.adb (Number_Of_Predefined_Prims): New subprogram.
(Make_Secondary_DT): Compute the number of predefined primitives
of all tagged types (including tagged types not defined at
library level). Previously we unconditionally relied on the
Max_Predef_Prims constant value when building the dispatch
tables of tagged types not defined at library level (thus
consuming more memory for their dispatch tables than required).
(Make_DT): Compute the number of predefined primitives that must
be inherited from their parent type when building the dispatch
tables of tagged types not defined at library level. Previously
we unconditionally relied on the Max_Predef_Prims constant value
when building the dispatch tables of tagged types not defined at
library level (thus copying more data than required from the
parent type).
2019-08-19 Bob Duff <duff@adacore.com>
* sem_ch13.adb (Record_Hole_Check): Procedure to check for holes
......
......@@ -742,9 +742,10 @@ package body Exp_Atag is
------------------------------------
function Build_Inherit_Predefined_Prims
(Loc : Source_Ptr;
Old_Tag_Node : Node_Id;
New_Tag_Node : Node_Id) return Node_Id
(Loc : Source_Ptr;
Old_Tag_Node : Node_Id;
New_Tag_Node : Node_Id;
Num_Predef_Prims : Int) return Node_Id
is
begin
return
......@@ -759,7 +760,7 @@ package body Exp_Atag is
New_Tag_Node)))),
Discrete_Range => Make_Range (Loc,
Make_Integer_Literal (Loc, Uint_1),
New_Occurrence_Of (RTE (RE_Max_Predef_Prims), Loc))),
Make_Integer_Literal (Loc, Num_Predef_Prims))),
Expression =>
Make_Slice (Loc,
......@@ -772,7 +773,7 @@ package body Exp_Atag is
Discrete_Range =>
Make_Range (Loc,
Make_Integer_Literal (Loc, 1),
New_Occurrence_Of (RTE (RE_Max_Predef_Prims), Loc))));
Make_Integer_Literal (Loc, Num_Predef_Prims))));
end Build_Inherit_Predefined_Prims;
-------------------------
......
......@@ -109,9 +109,10 @@ package Exp_Atag is
-- generated code handles primary and secondary dispatch tables of Typ.
function Build_Inherit_Predefined_Prims
(Loc : Source_Ptr;
Old_Tag_Node : Node_Id;
New_Tag_Node : Node_Id) return Node_Id;
(Loc : Source_Ptr;
Old_Tag_Node : Node_Id;
New_Tag_Node : Node_Id;
Num_Predef_Prims : Int) return Node_Id;
-- Build code that inherits the predefined primitives of the parent.
--
-- Generates: Predefined_DT (New_T).D (All_Predefined_Prims) :=
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment