comp.lang.ada
 help / color / mirror / Atom feed
From: Samuel Tardieu <sam@rfc1149.net>
Subject: Re: ada compiler?
Date: Fri, 16 Nov 2007 02:07:15 +0100
Date: 2007-11-16T02:10:01+01:00	[thread overview]
Message-ID: <87pryb2e2k.fsf@willow.rfc1149.net> (raw)
In-Reply-To: 1195058211.682783.288340@d55g2000hsg.googlegroups.com

>>>>> "Ludovic" == Ludovic Brenta <ludovic@ludovic-brenta.org> writes:

Ludovic> Does someone here know GNAT internals in sufficient detail as
Ludovic> to enlighten us? Also can someone explain what "reliable"
Ludovic> stack checking is in GNAT parlance?

You have two ways of doing stack checking:

 1- at the GCC level by checking the stack at the entry of every
    subprogram and comparing it to a thread/task specific marker; this
    is costly

 2- at the OS level by mapping a page after the stack which does not
    allow either reads or writes to the page (or, when this is not
    possible, which doesn't allow writes)

Let me describe how solution 2 is implemented, assuming the stack
grows downwards: (from higher to lower addresses)

     |
     | Stack for task T1
     |
     v
     |
     | Allowed space for stack for task T1
     |
     -
     |
     | Protected memory page
     |
     -
     |
     | Stack for task T2
     |
     v
     
If stack for task T1 slowly grows downwards, it will eventually reach
the protected memory page and raise a SIGSEGV to represent the access
violation.

However, if you allocate an object on stack T1 larger than the
remaining task space + the size of a protected memory page, then you
may end up with something looking like: (say a large T1Obj array is
allocated on the stack)

     |
     | Stack for task T1
     |
     | Return address for previous function call
     | Stack frame (I won't detail here what it is) for current funcall
     | Address of T1Obj[6]
     | Address of T1Obj[5]
     -
     | Address of T1Obj[4]
     | Address of T1Obj[3]   Protected memory page
     | Address of T1Obj[2]
     - 
     | Address of T1Obj[1]
     | Stack for task T2
     |
     v

You may then write T1Obj[1] without triggering a memory protection
error and overwrite the stack of task T2 without noticing. If you
modify only T1Obj[1] and return, you will leave T2 with a corrupted
task.

So to make this scheme work, every subprogram must use a stack area
smaller than a page size (typically 4KB, but this may vary from system
to system and you may also have pages of different sizes to reduce the
number of MMU [memory management unit] lookups that you need to map
your process virtual address space into the physical+swap address space).

I have not checked if GNAT knows how to mix those two schemes, i.e. if
it can generate a call to Stack_Check only in functions where the local
stack area is large enough to mandate the check and in subprograms
callable from other languages (because they may have had the same
problem and not done the check themselves -- in case of problems, the
program should be aborted as other stacks may already have been
corrupted).

An obvious improvement would be to increase the number of protected
memory pages between different stacks. However, this would reduce the
number of tasks that you can run simultaneously because on a 32 bits
system you have access to a maximum 4GB of addressable memory from any
processus (usually 3GB under Linux for example).

Does that answer your question?

  Sam
-- 
Samuel Tardieu -- sam@rfc1149.net -- http://www.rfc1149.net/



  parent reply	other threads:[~2007-11-16  1:07 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-09 22:25 ada compiler? Bill Cunningham
2007-11-09 22:39 ` anon
2007-11-09 22:59   ` Bill Cunningham
2007-11-09 23:28     ` anon
2007-11-09 23:50 ` Jeffrey R. Carter
2007-11-10  0:32   ` anon
2007-11-10  4:15     ` Jeffrey Creem
2007-11-10  5:08     ` tmoran
2007-11-10  6:12       ` anon
2007-11-10  8:35         ` tmoran
2007-11-10 20:20           ` anon
2007-11-10 22:15             ` Jeffrey Creem
2007-11-11  1:01               ` anon
2007-11-11  1:14                 ` (see below)
2007-11-11  2:21                 ` Georg Bauhaus
2007-11-11  4:08                   ` Jeffrey R. Carter
2007-11-13 13:24                     ` Ludovic Brenta
2007-11-14  7:00                       ` Jacob Sparre Andersen
2007-11-14  9:15                         ` Georg Bauhaus
2007-11-14 10:26                           ` Ludovic Brenta
2007-11-14 12:25                             ` Georg Bauhaus
2007-11-14 15:09                               ` Ludovic Brenta
2007-11-14 16:03                                 ` Georg Bauhaus
2007-11-14 16:36                                   ` Ludovic Brenta
2007-11-14 17:39                                     ` Georg Bauhaus
2007-11-14 20:40                                       ` Ludovic Brenta
2007-11-15  9:59                                         ` Georg Bauhaus
2007-11-14 17:52                                     ` Georg Bauhaus
2007-11-16  1:07                                     ` Samuel Tardieu [this message]
2007-11-16  1:58                                       ` tmoran
2007-11-16  9:41                                       ` Stack checking in GNAT (was: ada compiler?) Ludovic Brenta
2007-11-16 10:56                                         ` Stack checking in GNAT Samuel Tardieu
2007-11-16 13:04                                           ` Ludovic Brenta
2007-11-17  7:24                                             ` anon
2007-11-29  0:32                                         ` Robert A Duff
2007-11-17  1:37                                       ` ada compiler? Randy Brukardt
2007-11-17  1:18                                     ` Randy Brukardt
2007-11-29  0:41                                     ` Robert A Duff
2007-11-14 21:31                                 ` Niklas Holsti
2007-11-17  1:50                                   ` Randy Brukardt
2007-11-14 18:33                         ` anon
2007-11-12  5:13                   ` anon
2007-11-11 14:15                 ` Jeffrey Creem
2007-11-13  7:11                 ` Martin Krischik
2007-11-13  8:47                   ` anon
2007-11-13  9:34                     ` to gnat make or not to gnat make (Was: ada compiler?) Martin Krischik
2007-11-13 22:46                       ` anon
2007-11-13 23:14                         ` to gnat make or not to gnat make Simon Wright
2007-11-14  9:21                         ` to gnat make or not to gnat make (Was: ada compiler?) Georg Bauhaus
2007-11-13 22:41                     ` ada compiler? Simon Wright
2007-11-17  2:07                     ` Randy Brukardt
2007-11-17  7:51                       ` anon
2007-11-17  9:00                         ` Pascal Obry
2007-11-17 12:24                         ` Ludovic Brenta
2007-11-19 21:35                           ` Randy Brukardt
2007-11-10 15:03         ` (see below)
2007-11-12 16:38       ` Adam Beneschan
2007-11-13  3:16         ` John W. Kennedy
2007-11-13 16:46           ` Adam Beneschan
2007-11-10  3:07 ` Georg Bauhaus
2007-11-13  0:20 ` adaworks
2007-11-13 22:27   ` Simon Wright
2007-11-14 17:32   ` Britt Snodgrass
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox