Segmentation violation in nested functions with kernel 1.35

D. Hugh Redelmeier hugh at mimosa.com
Fri Jul 1 06:06:04 UTC 2005


| From: Vincent De Groote <vincent.degroote at encelade.be>

| I'm a little surprised with that kind of changes.  In my knowledge, a
| callback to a nested function is a usual way of doing some things ...

Nested functions are not part of standard C.  They are another GNU C
extension.

I think that they are implemented with thunks on the stack.  So they
require the system to allow execution of code on the stack.  Just what
exec-shield forbids.

So there are two good reasons to avoid nested function definitions in
C.

A function pointer in C is implemented as a simple pointer to the
first instruction of the function.  But a nested function needs to
have access to the local variables of the enclosing function.  So (I
am guessing, but I am a compiler writer) the declaration of a local
function pushes code (machine instructions) on the stack that loads a
pointer to the stack frame of the enclosing function (not too hard
because the code is actually within the stack frame) and then branches
to the body of the nested function.  A pointer to the nested function 
actually points to the generated code on the stack.

This kludge is the reinvention of stuff that precursors of C (Algol
and its descendants) already had.  Dennis Ritchie, the designer of C
intentionally omitted this, so I find it odd that GNU re-introduced it
(actually, Dennis was continuing a decision make by Martin Richards in
the design of BCPL).  The old literature is full of mechanisms like
the "Dijkstra Display" to implement nested functions.




More information about the fedora-list mailing list