<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>What happens when you mark a section as DISCARDABLE?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/07/12/10329051.aspx</link><description>Nothing, unless you're a driver.</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: What happens when you mark a section as DISCARDABLE?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/07/12/10329051.aspx#10330325</link><pubDate>Mon, 16 Jul 2012 21:15:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10330325</guid><dc:creator>@BrianEE</dc:creator><description>&lt;p&gt;&amp;quot;If you only have one small frequently used function and the rarely used functions happen to be in the same page, then what does it matter?&amp;quot;&lt;/p&gt;
&lt;p&gt;It&amp;#39;s more efficient to group your frequently used functions on the same page, because then those pages will have a high usage and won&amp;#39;t ever get pushed to the pagefile. Other, less important functions might suffer pagein hits, but since those are outside of your performance critical paths, you take a slight speed improvement.&lt;/p&gt;
&lt;p&gt;This happens because when everything gets paged out (e.g. during a hibernate, during a long pause or context switch or during memory pressure) a fault in one high-usage function automatically pages in other high-usage functions so you don&amp;#39;t take the hit twice or three times when calling into them shortly later.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10330325" width="1" height="1"&gt;</description></item><item><title>re: What happens when you mark a section as DISCARDABLE?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/07/12/10329051.aspx#10330204</link><pubDate>Mon, 16 Jul 2012 15:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10330204</guid><dc:creator>Gabe</dc:creator><description>&lt;p&gt;Brian EE: Defense in depth -- by initializing your string to all-0s, you avoid a buffer overflow in the case of certain failures to properly terminate. Maybe you should have a standard initialization byte. You can set it to 0xff for debugging and leave it at 0x00 for all other times.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10330204" width="1" height="1"&gt;</description></item><item><title>re: What happens when you mark a section as DISCARDABLE?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/07/12/10329051.aspx#10330195</link><pubDate>Mon, 16 Jul 2012 15:29:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10330195</guid><dc:creator>Brian_EE</dc:creator><description>&lt;p&gt;@JamesJohnston: I would fail you for blindly using a zero there instead of some other value. If I initialize a string buffer with all zeros, and am writing some custom string processing function, how do I know my function is properly terminating the string?&lt;/p&gt;
&lt;p&gt;I believe that variables should be initialized with data that will maximize chances of discovering bugs and vulnerabilities. Because if you don&amp;#39;t discover them, then someone else will.&lt;/p&gt;
&lt;p&gt;Personally, if I had to memset a string buffer, the value 0xff would be my choice.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10330195" width="1" height="1"&gt;</description></item><item><title>re: What happens when you mark a section as DISCARDABLE?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/07/12/10329051.aspx#10329678</link><pubDate>Fri, 13 Jul 2012 16:31:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10329678</guid><dc:creator>JamesJohnston</dc:creator><description>&lt;p&gt;@alegr1: &amp;quot;You can make a file with a list of function names for the linker. See /ORDER option.&amp;quot;&lt;/p&gt;
&lt;p&gt;From the docs: &amp;quot;Ordering allows you to optimize your program&amp;#39;s paging behavior through swap tuning by grouping a function with the functions it calls. You can also group frequently called functions together. These techniques increase the probability that a called function is in memory when it is needed and will not have to be paged from disk.&amp;quot;&lt;/p&gt;
&lt;p&gt;Well, that&amp;#39;s exactly what I&amp;#39;m suggesting then. &amp;nbsp;The next question obviously is going to be how do you systematically keep the list up-to-date from a maintenance standpoint; what procedures should be followed?&lt;/p&gt;
&lt;p&gt;@voo: &amp;quot;This seems like something where PGO should be able to help quite easily - whether it does it is another thing.&amp;quot;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s kind of what I was thinking. &amp;nbsp;I&amp;#39;ve never really looked into PGO very much though so I don&amp;#39;t know if it does that. &amp;nbsp;The one time I experimented with it produced a very very modest performance gain, at the cost of greatly increased hassle in the build process. &amp;nbsp;Did not seem worth it.&lt;/p&gt;
&lt;p&gt;@Crescens2k: &amp;nbsp;&amp;quot;you can end up with all of the rarely used functions on the same page as a frequently used function&amp;quot;&lt;/p&gt;
&lt;p&gt;If you only have one small frequently used function and the rarely used functions happen to be in the same page, then what does it matter? &amp;nbsp;The memory system works in integer multiples of pages, not fractions of a page. &amp;nbsp;Your choices are to put rarely used functions in that space, or null / undefined data.&lt;/p&gt;
&lt;p&gt;&amp;quot;increasing the probability that the code you want is in memory already, not allowing you to page out single use functions&amp;quot;&lt;/p&gt;
&lt;p&gt;Then logically wouldn&amp;#39;t that increase the probability that the code you don&amp;#39;t need might be paged out?&lt;/p&gt;
&lt;p&gt;@dave: &amp;quot;I fail you on code review if you pull stupid stuff like this&amp;quot;&lt;/p&gt;
&lt;p&gt;Unless this is some very hot performance-critical piece of code... why?!? &amp;nbsp;How will zeroing 100 bytes hurt anybody? &amp;nbsp;I do this all the time. &amp;nbsp;It is far easier to rule out undefined behavior at a glance when reading code by just making every variable set to zero, rather than trying to understand the actual workings of the code. &amp;nbsp;Only when proven to be a bottleneck would I actually look at removing it. &amp;nbsp;99% of code in most products is not going to be a bottleneck. &amp;nbsp;Profilers are your friend - use them!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10329678" width="1" height="1"&gt;</description></item><item><title>re: What happens when you mark a section as DISCARDABLE?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/07/12/10329051.aspx#10329663</link><pubDate>Fri, 13 Jul 2012 15:37:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10329663</guid><dc:creator>asdbsd</dc:creator><description>&lt;p&gt;@dave: What exactly is &amp;quot;competent&amp;quot; about not knowing where you can safely hand-wave it away, and wasting your brain&amp;#39;s resources on thinking about stuff which doesn&amp;#39;t matter? That&amp;#39;s incompetent in my book. Mindless thinking about everything :) True professional spends all resources, even his own attention and brainpower, wisely.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10329663" width="1" height="1"&gt;</description></item><item><title>re: What happens when you mark a section as DISCARDABLE?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/07/12/10329051.aspx#10329638</link><pubDate>Fri, 13 Jul 2012 14:34:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10329638</guid><dc:creator>Matt</dc:creator><description>&lt;p&gt;@dave:&lt;/p&gt;
&lt;p&gt;Just as a case-in-point, Microsoft have had tons of critical security updates by failing to zero stack buffers in the past, and accidentally having stack-garbage treated as valid (&lt;a rel="nofollow" target="_new" href="http://www.iss.net/security_center/reference/vuln/HTML_IE_Uninitialized_Memory_Corruption.htm"&gt;www.iss.net/.../HTML_IE_Uninitialized_Memory_Corruption.htm&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;I am not aware of any security vulnerability being caused by excessive initialization of variables. Indeed, if it&amp;#39;s obviously superflous, the compiler will remove the memset (since memset is a compiler intrinsic for Visual Studio and why SecureZeroMemory had to be introduced).&lt;/p&gt;
&lt;p&gt;In your example, snprintf on its own isn&amp;#39;t equivalent because the bytes after the nul-terminated string are still not zero - so if you send buff to someone across a trust-boundary then removing the memset might just have leaked a whole load of data (like ASLR addresses).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10329638" width="1" height="1"&gt;</description></item><item><title>re: What happens when you mark a section as DISCARDABLE?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/07/12/10329051.aspx#10329634</link><pubDate>Fri, 13 Jul 2012 14:14:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10329634</guid><dc:creator>dave</dc:creator><description>&lt;p&gt;We&amp;#39;re getting OT here, but I claim that mindless programming, as exemplified by knee-jerk coding of function calls that you don&amp;#39;t need to code, is a major source of software problems. My aim is not to make life miserable for anyone, but to build competent and professional programmers. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10329634" width="1" height="1"&gt;</description></item><item><title>re: What happens when you mark a section as DISCARDABLE?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/07/12/10329051.aspx#10329620</link><pubDate>Fri, 13 Jul 2012 13:38:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10329620</guid><dc:creator>asdbsd</dc:creator><description>&lt;p&gt;@dave: Mind is a resource. There&amp;#39;s only so much of thinking that can be done until you&amp;#39;re tired. What&amp;#39;s the point of making someone&amp;#39;s life miserable and forcing them to think on stuff which doesn&amp;#39;t matter?&lt;/p&gt;
&lt;p&gt;It&amp;#39;s enough that they understand that maybe in some cases zeroing isn&amp;#39;t needed, but it hurts nobody. Knowing that, they can decide to just zero out everything and pay their attention to something important instead. That is the reason for those lines of code.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10329620" width="1" height="1"&gt;</description></item><item><title>re: What happens when you mark a section as DISCARDABLE?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/07/12/10329051.aspx#10329608</link><pubDate>Fri, 13 Jul 2012 12:56:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10329608</guid><dc:creator>dave</dc:creator><description>&lt;p&gt;@Matt&lt;/p&gt;
&lt;p&gt;snprintf is perfectly well-behaved. It never writes beyond end of buffer and it always zero-terminates the result (I agree my example code should have checked the return value). &amp;nbsp;&lt;/p&gt;
&lt;p&gt;See the C99 specification.&lt;/p&gt;
&lt;p&gt;Perhaps you&amp;#39;re getting confused with the ill-designed Windows equivalent which does not guarantee termination?&lt;/p&gt;
&lt;p&gt;I&amp;#39;m programming in standard C rather than a vendor-specific language, apologies for not saying so.&lt;/p&gt;
&lt;p&gt;As far as whether it&amp;#39;s a bottleneck: well, probably not, though reflexive zeroing every single buffer will likely be measurable. But really I want programmers to not write lines of code that have no good reason for existing. I want them to think.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10329608" width="1" height="1"&gt;</description></item><item><title>re: What happens when you mark a section as DISCARDABLE?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/07/12/10329051.aspx#10329603</link><pubDate>Fri, 13 Jul 2012 12:37:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10329603</guid><dc:creator>@asdbsd</dc:creator><description>&lt;p&gt;No. You can&amp;#39;t allocate a non-zeroed or mapped page from the kernel. It&amp;#39;s true that when you allocate a zeroed page, check that it&amp;#39;s full of zeros and come back to it later that you might be seeing DIFFERENT zeroes, but that&amp;#39;s not going to affect your app.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10329603" width="1" height="1"&gt;</description></item></channel></rss>