<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Nest</title>
	<atom:link href="http://ridiculousfish.com/blog/archives/2006/02/05/nest/feed/" rel="self" type="application/rss+xml" />
	<link>http://ridiculousfish.com/blog/archives/2006/02/05/nest/</link>
	<description>serious code</description>
	<lastBuildDate>Tue, 09 Mar 2010 16:09:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: kgw</title>
		<link>http://ridiculousfish.com/blog/archives/2006/02/05/nest/comment-page-1/#comment-806</link>
		<dc:creator>kgw</dc:creator>
		<pubDate>Tue, 30 Jun 2009 22:01:15 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=30#comment-806</guid>
		<description>Snow Leopard will introduce &quot;blocks&quot; (known as closures in other languages) to gcc. This will allow for the creation of functions with runtime state, which allows for elegant qsort patterns, among other things. I can&#039;t wait!</description>
		<content:encoded><![CDATA[<p>Snow Leopard will introduce &#8220;blocks&#8221; (known as closures in other languages) to gcc. This will allow for the creation of functions with runtime state, which allows for elegant qsort patterns, among other things. I can&#8217;t wait!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Nekrasov</title>
		<link>http://ridiculousfish.com/blog/archives/2006/02/05/nest/comment-page-1/#comment-442</link>
		<dc:creator>Alex Nekrasov</dc:creator>
		<pubDate>Tue, 20 Feb 2007 08:19:57 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=30#comment-442</guid>
		<description>My understanding is that forbidding writable memory to be executable does not really save from exploits.

A buffer overrun can be used to put a pointer to exec() and format context so that exec() will call up a terminal or any other existing program (including mail and format command line utils). This context would need to be put where the return pointer had been.

This limits the possibilities somehow, but does not remove the threat completely.

If I&#039;m not making sense I&#039;d like to know abou tit.

thanks,
Alex</description>
		<content:encoded><![CDATA[<p>My understanding is that forbidding writable memory to be executable does not really save from exploits.</p>
<p>A buffer overrun can be used to put a pointer to exec() and format context so that exec() will call up a terminal or any other existing program (including mail and format command line utils). This context would need to be put where the return pointer had been.</p>
<p>This limits the possibilities somehow, but does not remove the threat completely.</p>
<p>If I&#8217;m not making sense I&#8217;d like to know abou tit.</p>
<p>thanks,<br />
Alex</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel B. Giffin</title>
		<link>http://ridiculousfish.com/blog/archives/2006/02/05/nest/comment-page-1/#comment-440</link>
		<dc:creator>Daniel B. Giffin</dc:creator>
		<pubDate>Wed, 31 May 2006 19:40:23 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=30#comment-440</guid>
		<description>If you&#039;re willing to change the calling conventions -- e.g., if you&#039;re compiling a different language -- you can just let your &quot;function pointers&quot; point to a data-structure that includes both a &quot;stack frame&quot; and a pointer to the code.  We call this a &quot;closure&quot;.

To be clear, this is essentially the solution that ridiculous_fish described:

&lt;blockquote&gt;
What if, instead of handing qsort() a pointer to the comparison_function() code, we instead passed a pointer to some data structure containing the stack frame of sort_ints() AND the address of comparison_function()? That data structure could turn around and call comparison_function() passing it the stack frame it needs. And the data structure could live on the sort_ints() stack, so we would have one per invocation of sort_ints(), which is just what we need.
&lt;/blockquote&gt;

The difference is that the trampoline code (and necessity for writable-and-executable memory) is no longer necessary: because of the different calling convention, the caller (e.g., qsort, still dumb as rocks) essentially sets the stack pointer to the one stored in the closure before jumping to the code.  The code is all created at compile-time and lives in your read-only section.

(A fun trick is to allocate those &quot;stack frames&quot; and closures on a garbage-collected heap, which lets you acutally return such inner functions back out of the dynamic context (control stack) that created them, to be called whenever you like.  So functions can create and return new, parameterized functions.  Or &quot;functions are first-class values.&quot;)

(Another fun trick is to &quot;capture&quot; the current &quot;heap-allocated&quot; stack data-structure and store it away for later, even returning it out of the dynamic context as above; called a &quot;continuation&quot;, this could be &quot;invoked&quot; (returned into, if you will) any time later (even multiple times!).  For example, you could implement a sort of multi-threading system this way.  Or a backtracking algorithm.)

(The above &quot;tricks&quot; are fundamental features of Scheme.  I believe (recent) Smalltalk does the same, with more explicit stack-frame introspection.)</description>
		<content:encoded><![CDATA[<p>If you&#8217;re willing to change the calling conventions &#8212; e.g., if you&#8217;re compiling a different language &#8212; you can just let your &#8220;function pointers&#8221; point to a data-structure that includes both a &#8220;stack frame&#8221; and a pointer to the code.  We call this a &#8220;closure&#8221;.</p>
<p>To be clear, this is essentially the solution that ridiculous_fish described:</p>
<blockquote><p>
What if, instead of handing qsort() a pointer to the comparison_function() code, we instead passed a pointer to some data structure containing the stack frame of sort_ints() AND the address of comparison_function()? That data structure could turn around and call comparison_function() passing it the stack frame it needs. And the data structure could live on the sort_ints() stack, so we would have one per invocation of sort_ints(), which is just what we need.
</p></blockquote>
<p>The difference is that the trampoline code (and necessity for writable-and-executable memory) is no longer necessary: because of the different calling convention, the caller (e.g., qsort, still dumb as rocks) essentially sets the stack pointer to the one stored in the closure before jumping to the code.  The code is all created at compile-time and lives in your read-only section.</p>
<p>(A fun trick is to allocate those &#8220;stack frames&#8221; and closures on a garbage-collected heap, which lets you acutally return such inner functions back out of the dynamic context (control stack) that created them, to be called whenever you like.  So functions can create and return new, parameterized functions.  Or &#8220;functions are first-class values.&#8221;)</p>
<p>(Another fun trick is to &#8220;capture&#8221; the current &#8220;heap-allocated&#8221; stack data-structure and store it away for later, even returning it out of the dynamic context as above; called a &#8220;continuation&#8221;, this could be &#8220;invoked&#8221; (returned into, if you will) any time later (even multiple times!).  For example, you could implement a sort of multi-threading system this way.  Or a backtracking algorithm.)</p>
<p>(The above &#8220;tricks&#8221; are fundamental features of Scheme.  I believe (recent) Smalltalk does the same, with more explicit stack-frame introspection.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PsyMar</title>
		<link>http://ridiculousfish.com/blog/archives/2006/02/05/nest/comment-page-1/#comment-439</link>
		<dc:creator>PsyMar</dc:creator>
		<pubDate>Fri, 26 May 2006 02:10:46 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=30#comment-439</guid>
		<description>All I have to say is that this coding style seems like a bit of a kludge in itself, although I guess it could be used to hide subroutines of functions from other functions -- but if you&#039;re going to use function pointers to get around that, which is what requires the trampoline, it completely destroys any OOP basis for doing it in the first place.  I don&#039;t like it.

The only solution I can think of is for the nested function to have its own copy of the variables of the containing function, along with a bool that is set when it&#039;s being called normally (not via a pointer) by the containing function; if it is, then it uses the containing function&#039;s memory storage for these (the reference distance from the stack pointer will be fixed), and if it isn&#039;t, then it uses the local space.</description>
		<content:encoded><![CDATA[<p>All I have to say is that this coding style seems like a bit of a kludge in itself, although I guess it could be used to hide subroutines of functions from other functions &#8212; but if you&#8217;re going to use function pointers to get around that, which is what requires the trampoline, it completely destroys any OOP basis for doing it in the first place.  I don&#8217;t like it.</p>
<p>The only solution I can think of is for the nested function to have its own copy of the variables of the containing function, along with a bool that is set when it&#8217;s being called normally (not via a pointer) by the containing function; if it is, then it uses the containing function&#8217;s memory storage for these (the reference distance from the stack pointer will be fixed), and if it isn&#8217;t, then it uses the local space.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Trevor</title>
		<link>http://ridiculousfish.com/blog/archives/2006/02/05/nest/comment-page-1/#comment-438</link>
		<dc:creator>Trevor</dc:creator>
		<pubDate>Wed, 17 May 2006 18:17:27 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=30#comment-438</guid>
		<description>&quot;How do other languages support them?&quot;

There is a reasonably strong correlation between languages that support lexically-scoped functions and those that run on some kind of virtual machine.  A decent virtual machine is the trivial answer to this problem, since you can architect the machine to make code insertion impossible (at least in traditional senses, e.g. writing data on the stack).

I can&#039;t see any method for doing this kind of thing if:
- you want to compile directly to hardware, and
- the pointer representation can&#039;t be made wider, and
- all code pages must be read-only, and
- code needs to be re-entrant</description>
		<content:encoded><![CDATA[<p>&#8220;How do other languages support them?&#8221;</p>
<p>There is a reasonably strong correlation between languages that support lexically-scoped functions and those that run on some kind of virtual machine.  A decent virtual machine is the trivial answer to this problem, since you can architect the machine to make code insertion impossible (at least in traditional senses, e.g. writing data on the stack).</p>
<p>I can&#8217;t see any method for doing this kind of thing if:<br />
- you want to compile directly to hardware, and<br />
- the pointer representation can&#8217;t be made wider, and<br />
- all code pages must be read-only, and<br />
- code needs to be re-entrant</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: masharpe</title>
		<link>http://ridiculousfish.com/blog/archives/2006/02/05/nest/comment-page-1/#comment-437</link>
		<dc:creator>masharpe</dc:creator>
		<pubDate>Wed, 29 Mar 2006 10:03:46 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=30#comment-437</guid>
		<description>In the Windows world, ATL generates code at run-time to solve a similar problem: passing a this-pointer to a window procedure.

http://www.rpi.edu/~pudeyo/articles/wndproc/#atl</description>
		<content:encoded><![CDATA[<p>In the Windows world, ATL generates code at run-time to solve a similar problem: passing a this-pointer to a window procedure.</p>
<p><a href="http://www.rpi.edu/~pudeyo/articles/wndproc/#atl" rel="nofollow">http://www.rpi.edu/~pudeyo/articles/wndproc/#atl</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yorick</title>
		<link>http://ridiculousfish.com/blog/archives/2006/02/05/nest/comment-page-1/#comment-436</link>
		<dc:creator>Yorick</dc:creator>
		<pubDate>Sat, 11 Mar 2006 10:23:57 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=30#comment-436</guid>
		<description>ABIs which implement C function pointers as pointers to descriptors rather than to the code directly indeed have no problem with this. For instance, the 64-bit PowerPC ABI used by Linux does this (I&#039;m not sure about Apple&#039;s). The cost is a slight overhead for each indirect function call. Thus the C call

  (*function_pointer)();

which translates to, in the 32-bit case,

  mtctr function_pointer
  bctrl

would instead be, with descriptors,

  ld r1,0(function_pointer)    ; code address
  mtctr r1
  ld r2,8(function_pointer)    ; data pointer (not sure about the register here)
  bctrl

but this is precisely what is desired for function closures. Not only are there no problems with non-executable stacks, but it is much more efficient since there is no need to use expensive cache flush and barrier instructions. And the &quot;trampoline&quot; (which is just another descriptor) is much smaller.

It is possible to have a secondary stack just for the closure data, as other people have pointed out, and the original paper, on which GCC&#039;s implementation is based, mentions this (http://people.debian.org/~aaronl/Usenix88-lexic.pdf). The problem is with longjmp() which must be aware of the system, or else the stacks will get out of sync.

Ironically, generating code at runtime is slightly easier on x86 than PowerPC because the former have coherent instruction and data caches, so no explicit cache flush is generally needed.</description>
		<content:encoded><![CDATA[<p>ABIs which implement C function pointers as pointers to descriptors rather than to the code directly indeed have no problem with this. For instance, the 64-bit PowerPC ABI used by Linux does this (I&#8217;m not sure about Apple&#8217;s). The cost is a slight overhead for each indirect function call. Thus the C call</p>
<p>  (*function_pointer)();</p>
<p>which translates to, in the 32-bit case,</p>
<p>  mtctr function_pointer<br />
  bctrl</p>
<p>would instead be, with descriptors,</p>
<p>  ld r1,0(function_pointer)    ; code address<br />
  mtctr r1<br />
  ld r2,8(function_pointer)    ; data pointer (not sure about the register here)<br />
  bctrl</p>
<p>but this is precisely what is desired for function closures. Not only are there no problems with non-executable stacks, but it is much more efficient since there is no need to use expensive cache flush and barrier instructions. And the &#8220;trampoline&#8221; (which is just another descriptor) is much smaller.</p>
<p>It is possible to have a secondary stack just for the closure data, as other people have pointed out, and the original paper, on which GCC&#8217;s implementation is based, mentions this (<a href="http://people.debian.org/~aaronl/Usenix88-lexic.pdf" rel="nofollow">http://people.debian.org/~aaronl/Usenix88-lexic.pdf</a>). The problem is with longjmp() which must be aware of the system, or else the stacks will get out of sync.</p>
<p>Ironically, generating code at runtime is slightly easier on x86 than PowerPC because the former have coherent instruction and data caches, so no explicit cache flush is generally needed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Samh</title>
		<link>http://ridiculousfish.com/blog/archives/2006/02/05/nest/comment-page-1/#comment-435</link>
		<dc:creator>Samh</dc:creator>
		<pubDate>Wed, 08 Mar 2006 23:57:40 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=30#comment-435</guid>
		<description>Looking at OpenBSD&#039;s man page for GCC, it appears they also disable trampolines by default, they have to be re-enabled with -ftrampolines. Also, to quote : &quot;trampoline code marks the smallest possible area around the trampoline stub executable using mprotect(2), since the stack area is by default non-executable.&quot;

http://www.openbsd.org/cgi-bin/man.cgi?query=gcc-local&amp;sektion=1</description>
		<content:encoded><![CDATA[<p>Looking at OpenBSD&#8217;s man page for GCC, it appears they also disable trampolines by default, they have to be re-enabled with -ftrampolines. Also, to quote : &#8220;trampoline code marks the smallest possible area around the trampoline stub executable using mprotect(2), since the stack area is by default non-executable.&#8221;</p>
<p><a href="http://www.openbsd.org/cgi-bin/man.cgi?query=gcc-local&amp;sektion=1" rel="nofollow">http://www.openbsd.org/cgi-bin/man.cgi?query=gcc-local&amp;sektion=1</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anon</title>
		<link>http://ridiculousfish.com/blog/archives/2006/02/05/nest/comment-page-1/#comment-434</link>
		<dc:creator>Anon</dc:creator>
		<pubDate>Sun, 12 Feb 2006 09:58:43 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=30#comment-434</guid>
		<description>OK, how about this: create a seperate stack, just for trampolines. Leave it write-protected, but executable. When you need to create a trampoline, you specifically enable write access, build the trampoline, and then lock it down again (perhaps this should be in priviliged code?).</description>
		<content:encoded><![CDATA[<p>OK, how about this: create a seperate stack, just for trampolines. Leave it write-protected, but executable. When you need to create a trampoline, you specifically enable write access, build the trampoline, and then lock it down again (perhaps this should be in priviliged code?).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nobody special</title>
		<link>http://ridiculousfish.com/blog/archives/2006/02/05/nest/comment-page-1/#comment-433</link>
		<dc:creator>nobody special</dc:creator>
		<pubDate>Thu, 09 Feb 2006 20:59:21 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=30#comment-433</guid>
		<description>what an interesting series of articles!

i would be interested in reading your comments on the following discussion regarding the differences between the GNU and NeXT objc runtime:

http://www.objc.net/blogger/

thanks.</description>
		<content:encoded><![CDATA[<p>what an interesting series of articles!</p>
<p>i would be interested in reading your comments on the following discussion regarding the differences between the GNU and NeXT objc runtime:</p>
<p><a href="http://www.objc.net/blogger/" rel="nofollow">http://www.objc.net/blogger/</a></p>
<p>thanks.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
