{"id":97065,"date":"2017-09-22T07:00:00","date_gmt":"2017-09-22T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=97065"},"modified":"2019-03-13T01:17:05","modified_gmt":"2019-03-13T08:17:05","slug":"20170922-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170922-00\/?p=97065","title":{"rendered":"What does it mean when I get an access violation at a very low address when entering a critical section?"},"content":{"rendered":"<p><b>Warning<\/b>: This article talks about implementation details which can change at any time. The information provided is for debugging and diagnostic purposes only. <\/p>\n<p>A customer found that their server program occasionally crashes in the internal function <code>Rtlp&shy;Wait&shy;On&shy;Critical&shy;Section<\/code> trying to dereference the address <code>0x00000014<\/code>. <\/p>\n<pre>\n7789dde3 ff4014          inc     dword ptr [eax+14h]\n<\/pre>\n<p>The dereference was due to a null pointer in the <code>EAX<\/code> register. This was particularly difficult to debug because the problem usually didn&#8217;t surface until the program had been running continuously for a week or more. <\/p>\n<p>The customer chased the null pointer backwards and found that it came from the <code>Debug&shy;Info<\/code> field of the <code>RTL_<\/code><code>CRITICAL_<\/code><code>SECTION<\/code> structure. <\/p>\n<pre>\ntypedef struct _RTL_CRITICAL_SECTION\n{\n                                             \/\/ value in memory:\n     PRTL_CRITICAL_SECTION_DEBUG DebugInfo;  \/\/ 0x00000000\n     LONG LockCount;                         \/\/ 0xFFFFFFFC\n     LONG RecursionCount;                    \/\/ 0x00000000\n     PVOID OwningThread;                     \/\/ 0x00000000\n     PVOID LockSemaphore;                    \/\/ 0x00005CDC\n     ULONG SpinCount;                        \/\/ 0x00000000\n} RTL_CRITICAL_SECTION, *PRTL_CRITICAL_SECTION;\n<\/pre>\n<p>The customer confirmed that, yes, the <code>Debug&shy;Info<\/code> of the critical section they were trying to enter was indeed null. <\/p>\n<p>Although the customer didn&#8217;t do it in their application (at least not knowingly), they did try a test application which passed the <code>CRITICAL_<\/code><code>SECTION_<\/code><code>NO_<\/code><code>DEBUG_<\/code><code>INFO<\/code> flag to the <code>Initialize&shy;Critical&shy;Section&shy;Ex<\/code> function, in the hopes of inducing a null pointer for the <code>Debug&shy;Info<\/code>, but it didn&#8217;t work. When initialized in that way, the <code>Debug&shy;Info<\/code> was set to <code>0xFFFFFFFF<\/code>. <\/p>\n<p>Is it possible that this is a critical section that was initialized with the traditional <code>Initialize&shy;Critical&shy;Section<\/code> function, but the attempt to allocate the debug info failed, so the kernel left it null? <\/p>\n<p>No, that&#8217;s not why the the <code>Debug&shy;Info<\/code> is null. If a critical section has no debug info (either explicitly requested as such with the <code>CRITICAL_<\/code><code>SECTION_<\/code><code>NO_<\/code><code>DEBUG_<\/code><code>INFO<\/code> flag, or because the system couldn&#8217;t allocate any debug info), then the <code>Debug&shy;Info<\/code> is set to the special value <code>0xFFFFFFFF<\/code>. The <code>Debug&shy;Info<\/code> for a valid initialized critical section is never null. <\/p>\n<p>So what does it mean when the <code>Debug&shy;Info<\/code> is null? The most likely reason is that you are using an uninitialized critical section. Either you never initialized it, or you deleted an initialized critical section (which resets it back to the uninitialized state). <\/p>\n<p>Other evidence that you have an uninitialized critical section is that the critical section is locked, yet has no owner. Furthermore, the spin count is zero, which occurs only on uniprocessor systems. I suspect the server they are running the program on has more than one core. (Heck, my <i>phone<\/i> has more than one core.) <\/p>\n<p><b>Bonus reading<\/b>: <a HREF=\"https:\/\/docs.microsoft.com\/en-us\/windows-hardware\/drivers\/debugger\/displaying-a-critical-section\">Displaying a critical section in the debugger<\/a>. <\/p>\n<p><b>Related<\/b>: <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20050701-11\/?p=35123\">I hope you werent using those undocumented critical section fields<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>It probably means that you&#8217;re entering a critical section that is not initialized.<\/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-97065","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It probably means that you&#8217;re entering a critical section that is not initialized.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/97065","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=97065"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/97065\/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=97065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=97065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=97065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}