Re: valgrind

2022-03-25 Thread Reinoud Zandijk
On Tue, Mar 22, 2022 at 06:17:50PM +0100, Rhialto wrote:
> On Sun 20 Mar 2022 at 23:17:57 -0400, Mouse wrote:
> > clang is - or at least was last I checked - under the impression that
> > nested functions are little-used and thus are not worth supporting.
> 
> I guess they can think so because nested functions are a gcc extension.
> Quite a useful one, I think, having been taught programming with Algol
> 68 which of course does have nested functions; but an extension
> nevertheless.

AFAIR, it comes for GCC's support for pascal which has nested functions
defined. Its just that for 'C' it was an easy addition since the compiler
already suported it.

Reinoud



Re: valgrind

2022-03-25 Thread Andrew Cagney
On Fri, 25 Mar 2022 at 10:03, Reinoud Zandijk  wrote:

> > I guess they can think so because nested functions are a gcc extension.
> > Quite a useful one, I think, having been taught programming with Algol
> > 68 which of course does have nested functions; but an extension
> > nevertheless.
>
> AFAIR, it comes for GCC's support for pascal which has nested functions
> defined. Its just that for 'C' it was an easy addition since the compiler
> already suported it.

I suspect a bigger contributor may have been that all the cool
languages (and Ada) supported this feature; so why not GCC.   At the
time (pre-EGCS?) any language trying to use GCC as a front end really
struggled (except perhaps Ada), and Pascal more so than most.

However this is all hearsay.  My attempt at archology failed.  All I
found was a bug fix to the code in 1998.


Re: Nested functions [was Re: valgrind]

2022-03-25 Thread David Holland
On Thu, Mar 24, 2022 at 11:16:58PM -0400, Mouse wrote:
 > > The conclusion over the past ~25 years has been that there isn't;
 > > qsort and things like it work "well enough" and real uses for
 > > closures that really motivate the feature come up rarely enough that
 > > it doesn't happen.
 > 
 > Nested functions are not closures, or at least not what I know as
 > closures.  A nested function pointer (conceptually) goes invalid as
 > soon as anything it refers to goes out of scope, or at the latest as
 > soon as its smallest enclosing block exits (or possibly when its
 > smallest enclosing function returns).  The thing I know as a closure
 > would preserve the referred-to objects as long as the closure is
 > potentially callable.  This requires more reference tracking than C
 > typically makes possible.

There's a term for scope-restricted closures (that become invalid this
way) that I'm currently forgetting. They're still closures in most of
the important ways, and it's a useful concept for a language where
copying memory isn't free.

 > > There is no solution based on trampolines that'll pass security (or
 > > at least security-theatre) muster.  Unless maybe by doing something
 > > that's horrifying in other ways.
 > 
 > The safest alternative that comes to my mind is to have two stacks, one
 > for trampolines and one for everything else.  But that requires
 > something much like two stack pointers, including assist from the
 > setjmp/longjmp implementation and, if applicable, threads.

That's not s3kure! You can still get arbitrary code execution by
scribbling in it.

 > > (For example: you could declare a static limit on how many instances
 > > of the closure you'll ever produce, make a global array to stuff the
 > > data pointer in, and statically generate N trampoline entry points
 > > that read from that array and call the primary function.  But there
 > > are many other ways in which this is horrible.)
 > 
 > But there are use cases for which it is not a stupid implementation;
 > [...]

I think to keep it safe you need more code analysis than you're likely
to get with C code. But IDK.

-- 
David A. Holland
dholl...@netbsd.org