From f2c4cfadb13dd5eef1979069317e5c6ee224c908 Mon Sep 17 00:00:00 2001 From: Tristan Gingold Date: Fri, 23 Nov 2018 04:28:44 +0100 Subject: Add --max-stack-alloc option, check stack allocation of complex object. Fix #692 --- src/grt/grt-lib.adb | 13 +++++++++++++ src/grt/grt-lib.ads | 8 ++++++++ src/grt/grt-options.adb | 16 ++++++++++++++++ 3 files changed, 37 insertions(+) (limited to 'src/grt') diff --git a/src/grt/grt-lib.adb b/src/grt/grt-lib.adb index 7597bcfc4..0bb9c2e36 100644 --- a/src/grt/grt-lib.adb +++ b/src/grt/grt-lib.adb @@ -275,6 +275,19 @@ package body Grt.Lib is return Ghdl_I64_Exp_1 (V, E); end Ghdl_I64_Exp; + procedure Ghdl_Check_Stack_Allocation (Size : Ghdl_Index_Type) + is + Bt : Backtrace_Addrs; + begin + if Size >= Max_Stack_Allocation then + Save_Backtrace (Bt, 1); + Error_S ("declaration of a too large object ("); + Diag_C (Natural (Size / 1024)); + Diag_C (" KB)"); + Error_E_Call_Stack (Bt); + end if; + end Ghdl_Check_Stack_Allocation; + function C_Malloc (Size : Ghdl_Index_Type) return Ghdl_Ptr; pragma Import (C, C_Malloc, "malloc"); diff --git a/src/grt/grt-lib.ads b/src/grt/grt-lib.ads index 646cdd5fb..167ea98e5 100644 --- a/src/grt/grt-lib.ads +++ b/src/grt/grt-lib.ads @@ -70,6 +70,11 @@ package Grt.Lib is function Ghdl_I32_Exp (V : Ghdl_I32; E : Std_Integer) return Ghdl_I32; function Ghdl_I64_Exp (V : Ghdl_I64; E : Std_Integer) return Ghdl_I64; + -- Called before allocation of large (complex) objects. + procedure Ghdl_Check_Stack_Allocation (Size : Ghdl_Index_Type); + + Max_Stack_Allocation : Ghdl_Index_Type := 128 * 1024; + function Ghdl_Malloc (Size : Ghdl_Index_Type) return Ghdl_Ptr; -- Allocate and clear SIZE bytes. @@ -122,6 +127,9 @@ private "__ghdl_direction_check_failed"); pragma Export (C, Ghdl_Program_Error, "__ghdl_program_error"); + pragma Export (C, Ghdl_Check_Stack_Allocation, + "__ghdl_check_stack_allocation"); + pragma Export (C, Ghdl_Malloc, "__ghdl_malloc"); pragma Export (C, Ghdl_Malloc0, "__ghdl_malloc0"); pragma Export (C, Ghdl_Deallocate, "__ghdl_deallocate"); diff --git a/src/grt/grt-options.adb b/src/grt/grt-options.adb index 5b154e4a5..d93ad9e58 100644 --- a/src/grt/grt-options.adb +++ b/src/grt/grt-options.adb @@ -56,6 +56,7 @@ package body Grt.Options is P (" X is expressed as a time value, without spaces: 1ns, ps..."); P (" --stop-delta=X stop the simulation cycle after X delta"); P (" --expect-failure invert exit status"); + P (" --max-stack-alloc=X error if variables are larger than X KB"); P (" --no-run do not simulate, only elaborate"); P (" --unbuffered disable buffering on stdout, stderr and"); P (" files opened in write or append mode (TEXTIO)."); @@ -272,6 +273,21 @@ package body Grt.Options is Warning ("option --stack-size is deprecated"); elsif Len >= 17 and then Option (1 .. 17) = "--stack-max-size=" then Warning ("option --stack-max-size is deprecated"); + elsif Len >= 18 and then Option (1 .. 18) = "--max-stack-alloc=" then + declare + Ok : Boolean; + Pos : Natural; + Val : Integer_64; + begin + Extract_Integer (Option (19 .. Len), Ok, Val, Pos); + if not Ok or else Pos <= Len then + Error_S ("bad value in '"); + Diag_C (Option); + Error_E ("'"); + else + Lib.Max_Stack_Allocation := Ghdl_Index_Type (Val * 1024); + end if; + end; elsif Len >= 11 and then Option (1 .. 11) = "--activity=" then if Option (12 .. Len) = "none" then Flag_Activity := Activity_None; -- cgit v1.2.3