{"id":5403,"date":"2013-01-30T07:00:00","date_gmt":"2013-01-30T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/01\/30\/why-doesnt-heapvalidate-detect-corruption-in-the-managed-heap\/"},"modified":"2013-01-30T07:00:00","modified_gmt":"2013-01-30T07:00:00","slug":"why-doesnt-heapvalidate-detect-corruption-in-the-managed-heap","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130130-00\/?p=5403","title":{"rendered":"Why doesn&#8217;t HeapValidate detect corruption in the managed heap?"},"content":{"rendered":"<p>A customer had a program that was corrupting the managed heap by p\/invoking incorrectly. The problem didn&#8217;t show up until the next garbage collection pass, at which point the CLR got all freaked-out-like. &#8220;According to <a href=\"http:\/\/support.microsoft.com\/kb\/286470\"> Knowledge Base article 286470<\/a>, the <code>GFlags<\/code> tool is supposed to catch heap corruption, but it doesn&#8217;t catch squat.&#8221;<\/p>\n<p> Depending on your point of view, this is either a case of the customer <a href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2006\/01\/16\/513311.aspx\"> not understanding what things mean in context<\/a> or of the KB article author <a href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/05\/12\/10163578.aspx\"> looking at the world through kernel-colored glasses<\/a>. <\/p>\n<p> The <code>GFlags<\/code> tool, pageheap, full pageheap, and the <code>Heap&shy;Validate<\/code> function all operate on heaps, but the sense of the word <i>heap<\/i> here is &#8220;heaps created by the <code>Heap&shy;Create<\/code> function.&#8221; If your program <a href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2005\/05\/19\/420038.aspx\"> does a <code>Virtual&shy;Alloc<\/code> and then carves out sub-allocations from it<\/a>, well, it&#8217;s not like <code>GFlags<\/code> and <code>Heap&shy;Validate<\/code> are psychic and can magically reverse-engineer your code in order to understand your custom heap implementation and be able to determine whether your custom heap is corrupted. <\/p>\n<p> Clearly no such function could be written, because that&#8217;s even harder than the Halting Problem! One property of a non-corrupted heap is that it will not send the heap manager into an infinite loop. Therefore, proving that the heap is not corrupted, given no information about the heap implementation other than the code itself, would require proving that the next heap call will return. And that&#8217;s just <i>one<\/i> of the things the imaginary <i>ValidateAnyHeap<\/i> function would have to do. (We try to limit ourselves to one impossible thing at a time.) <\/p>\n<p> The <code>Heap&shy;Validate<\/code> function only knows how to validate heaps created by the <code>Heap&shy;Create<\/code> function. It does not have magic insight into custom heap implementations. The <code>GFlags<\/code> program modifies the behavior of heaps created by the <code>Heap&shy;Create<\/code> function, because it naturally does not know what debugging features you&#8217;ve added to your custom heap implementation, so it doesn&#8217;t know what it needs to do to turn them on and off. <\/p>\n<p> As far as the kernel folks are concerned, &#8220;heap&#8221; means &#8220;something created by the <code>Heap&shy;Create<\/code> function.&#8221; Anything else is just an imposter. <\/p>\n<p> If you are looking for corruption in a custom heap implementation, then you need to go ask the authors of that custom heap implementation if they provided any debugging facilities for that heap. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer had a program that was corrupting the managed heap by p\/invoking incorrectly. The problem didn&#8217;t show up until the next garbage collection pass, at which point the CLR got all freaked-out-like. &#8220;According to Knowledge Base article 286470, the GFlags tool is supposed to catch heap corruption, but it doesn&#8217;t catch squat.&#8221; Depending on [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-5403","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer had a program that was corrupting the managed heap by p\/invoking incorrectly. The problem didn&#8217;t show up until the next garbage collection pass, at which point the CLR got all freaked-out-like. &#8220;According to Knowledge Base article 286470, the GFlags tool is supposed to catch heap corruption, but it doesn&#8217;t catch squat.&#8221; Depending on [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/5403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=5403"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/5403\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=5403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=5403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=5403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}