<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Satyem</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/satyem/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/satyem/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 03:27:14 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Variable Argument Functions</title>
      <link>https://docs.microsoft.com/archive/blogs/satyem/variable-argument-functions</link>
      <pubDate>Mon, 11 Mar 2013 16:10:00 GMT</pubDate>
      <dc:creator><![CDATA[Satyendra Mishra [MSFT]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/satyem/2013/03/11/variable-argument-functions/</guid>
      <description><![CDATA[Usually the functions defined in C/C++ take constant number of arguments which are decided while...]]></description>
      <content:encoded><![CDATA[<p>Usually the functions defined in C/C++ take constant number of arguments which are decided while declaring the functions. Sometimes we do need the functions that can&nbsp;accept variable number of arguments, Printf() function is a great example of such functions.&nbsp; Let's see how these functions work in terms of&nbsp;stack building&nbsp;and how they are able to retrieve the arguments passed from the &nbsp;caller.</p>
<p>int&nbsp;Sum(int<span style="font-family: Times New Roman; font-size: small;"> n, ...) </span>is the declaration of one such variable argument functions. Just like any normal functions it can have any return type and any number of mandatory parameters of any type. The difference is in ellipsis ... which should be the last parameter to such a functions. Also there should be at least one mandatory parameter to the function, even if this placeholder parameter is not used it is required to access rest of the&nbsp;arguments.</p>
<p>The three macros that are used in function implementation to retrieve the&nbsp;arguments are <strong>va_start, va_arg, va_end</strong>. v<strong>a_list</strong> is the type that is used to hold the information of variable arguments. This should be set by va_start before accessing any of the variable parameters. va_arg then uses this type to access the parameters one by one. va_end is called to stop the processing, it actually resets the va_list type which was set by va_start to hold the variable parameter information. Lets check one simple example:</p>
<p>int Sum(int n, ...) <br />{ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int sum=0; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; va_list vaList; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; va_start( vaList, n); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;n; i++) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sum += va_arg(vaList, short); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; va_end (vaList); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return sum; <br />}</p>
<p>This program simply adds all the numbers passed to Sum function, the first mandatory parameter tells the number of variable&nbsp;arguments that are passed to the function.&nbsp;The implementation is simple, first the vaList is initialized&nbsp;using va_start function, then va_arg is used to fetch next variable argument and at the end va_end is called to reset the vaList.</p>
<p>There are few important things to notice here:</p>
<ul>
<li>The function takes an argument which reflects the number of variable arguments passed to the function. Such information is required because the variable argument function has no way to figure out the number of parameters passed on its own. Number of parameters need not be passed by first argument, there can be any other logic used in the function implementation to identify the number of parameters, but it should be part of funciton implemenation it self.</li>
</ul>
<p>&nbsp;</p>
<ul>
<li>va_arg takes a parameter which is the type of parameter that it is going to retrieve. This parameter is necessary because va_arg just goes one location up in the stack to search for the next&nbsp;parameter value. So it will fetch 8 bytes on 64bit&nbsp;process,&nbsp;and then return the right value using expected size. So this type needs to be used carefully. Incorrectly using the type parameter will result in&nbsp; wrong result. For example if I used BYTE in place of Short in the above function to va_arg, I will only be adding the first 1 byte value of all the numbers passed, which will be incorrect if i wanted to have all the numbers upto 2 bytes.</li>
</ul>
<p>&nbsp;</p>
<ul>
<li>va_start takes the name of the parameter which is immediately followed by ellipsis .... As mentioned earlier there can be more than one mandatory parameters to variable argument functions, and va_start expects the last mandatory parameter name so that it can identify from where in stack the variable parameter starts and it sets the va_list type to same location.</li>
</ul>
<p>&nbsp;Let''s take a look at all this in function disassembly and see how does it owrk, below is the disassembly of the above functin Sum(int n ,...). The comment line above the instructions explains the logic of program flow and variable parameter handling.</p>
<p>//Since the first four parameters are passed via registers, below four instructions will put these parameters to the stack homing space so that all the parameters are there on stack.</p>
<p><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp; 5 000007f6`17231020 894c2408&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;dword ptr [rsp+8],ecx </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp; 5 000007f6`17231024 4889542410&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;qword ptr [rsp+10h],rdx </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp; 5 000007f6`17231029 4c89442418&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;qword ptr [rsp+18h],r8 </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp; 5 000007f6`1723102e 4c894c2420&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;qword ptr [rsp+20h],r9</span></p>
<p>// Adjust the stack for local parameters <br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;5 000007f6`17231033 4883ec28&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sub&nbsp;&nbsp;&nbsp;&nbsp;rsp,28h </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp; 6 000007f6`17231037 c744240800000000&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; dword ptr [rsp+8],0</span></p>
<p>// The first variable parameter that was passed via rdx is at rsp+38.&nbsp; So rax now contains the location of&nbsp;first variable parameter. vaList will be set to this location as part of va_start call. <br /><span style="font-family: courier new,courier; font-size: small;">&nbsp;&nbsp;&nbsp; 8 000007f6`1723103f 488d442438&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lea&nbsp;&nbsp;&nbsp;&nbsp;rax,[rsp+38h]</span></p>
<p>&nbsp;// rsp+10 has the vaList local variable and it has been assigned rax which has the location of first variable parameter. <br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp; 8 000007f6`17231044 4889442410&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;qword ptr [rsp+10h],rax </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp; 9 000007f6`17231049 c744240400000000&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; dword ptr [rsp+4], </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp; 9 000007f6`17231051 eb0a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; jmp&nbsp;&nbsp;&nbsp;&nbsp; vararg!Sum+0x3d (000007f6`1723105d)</span></p>
<p>// The loop starts <br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp; 9 000007f6`17231053 8b442404&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;eax,dword ptr [rsp+4] </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp; 9 000007f6`17231057 ffc0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inc&nbsp;&nbsp;&nbsp;&nbsp; eax </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp; 9 000007f6`17231059 89442404&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;dword ptr [rsp+4],eax</span></p>
<p>&nbsp;// The loop condition check</p>
<p><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp; 9 000007f6`1723105d 8b442430&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;eax,dword ptr [rsp+30h] </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp; 9 000007f6`17231061 39442404&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmp&nbsp;&nbsp;&nbsp;&nbsp;dword ptr [rsp+4],eax </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp; 9 000007f6`17231065 7d53&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jge&nbsp;&nbsp;&nbsp;&nbsp;vararg!Sum+0x9a (000007f6`172310ba)</span> <br />&nbsp;</p>
<p><span style="font-family: courier new,courier;">&nbsp;&nbsp; 11 000007f6`17231067 33c0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xor&nbsp;&nbsp;&nbsp;&nbsp; eax,eax </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp; 11 000007f6`17231069 85c0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test&nbsp;&nbsp;&nbsp; eax,eax </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp; 11 000007f6`1723106b 7420&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;je&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vararg!Sum+0x6d (000007f6`1723108d)</span></p>
<p>&nbsp;//here we call va_arg. This first fetches the location of current parameter</p>
<p><span style="font-family: courier new,courier;">&nbsp;&nbsp; 11 000007f6`1723106d 488b442410&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;rax,qword ptr [rsp+10h]</span></p>
<p>&nbsp;// Adding 8 to current parameter location. So it will start pointing to next parameter.</p>
<p><span style="font-family: courier new,courier;">&nbsp;&nbsp; 11 000007f6`17231072 4883c008&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add&nbsp;&nbsp;&nbsp;&nbsp;rax,8</span></p>
<p>// Stored the location of next parameter to va_list</p>
<p><span style="font-family: courier new,courier;">&nbsp;&nbsp; 11 000007f6`17231076 4889442410&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;qword ptr [rsp+10h],rax </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp; 11 000007f6`1723107b 488b442410&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;rax,qword ptr [rsp+10h]</span></p>
<p>&nbsp;// Since the location already points to next parameter, we need to subtract 8 to get the value of current variable parameter.&nbsp;&nbsp;</p>
<p><span style="font-family: courier new,courier;">&nbsp;&nbsp; 11 000007f6`17231080 488b40f8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp; rax,qword ptr [rax-8]</span></p>
<p>&nbsp;// So now rax contains full 8 byte. But we are using Short type in va_arg macro, so only need ax part of&nbsp; it(i.e. 2 bytes) <br /><span style="font-family: courier new,courier;">&nbsp;&nbsp; 11 000007f6`17231084 0fb700&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; movzx&nbsp;&nbsp; eax,word ptr [rax] </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp; 11 000007f6`17231087 66890424&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;word ptr [rsp],ax </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp; 11 000007f6`1723108b eb1b&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; jmp&nbsp;&nbsp;&nbsp;&nbsp; vararg!Sum+0x88 (000007f6`172310a8)</span></p>
<p>// Some instructions which are not relevent to variable argument handling are reomved from here&nbsp;to shorten the output&nbsp;</p>
<p>//Adding to current sum.</p>
<p><span style="font-family: courier new,courier;">&nbsp;&nbsp; 11 000007f6`172310b0 03c8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add&nbsp;&nbsp;&nbsp;&nbsp; ecx,eax </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;11 000007f6`172310b2 8bc1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; eax,ecx </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp; 11 000007f6`172310b4 89442408&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;dword ptr [rsp+8],eax</span></p>
<p>&nbsp;//Looping again.</p>
<p><span style="font-family: courier new,courier;">&nbsp;&nbsp;12 000007f6`172310b8 eb99&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; jmp&nbsp;&nbsp;&nbsp;&nbsp; vararg!Sum+0x33 (000007f6`17231053)</span></p>
<p>&nbsp;// This is the code for resetting the vaList using va_end. As we know rsp+10 have vaList. <br /><span style="font-family: courier new,courier;">&nbsp;&nbsp; 13 000007f6`172310ba 48c744241000000000 mov&nbsp;&nbsp; qword ptr [rsp+10h],0 </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;15 000007f6`172310c3 8b442408&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;eax,dword ptr [rsp+8] </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp; 16 000007f6`172310c7 4883c428&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add&nbsp;&nbsp;rsp,28h </span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp; 16 000007f6`172310cb c3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ret</span></p>
<p>&nbsp;</p>
<p>This is an example from AMD64 process, as you can see the caller has no changes in it whether it is calling a normal functions or a variable argument function. It will pass all the parameters and will clean the stack when callee returns. So it is the responsibility of caller to clean up the stack, which is always the case in 64 bit functions. For 32 bit functions there are multiple calling conventions like STDCALL, CDECL etc. Among these CDECL is the one in which Caller cleans up the stack, so for 32 bit variable argument functions, the function should always be declared as __cdecl function.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Debugging Windows Service</title>
      <link>https://docs.microsoft.com/archive/blogs/satyem/debugging-windows-service</link>
      <pubDate>Sat, 23 Feb 2013 10:39:00 GMT</pubDate>
      <dc:creator><![CDATA[Satyendra Mishra [MSFT]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/satyem/2013/02/23/debugging-windows-service/</guid>
      <description><![CDATA[From Vista onwards all the services run in Session 0, and session 0 has no interaction with desktop,...]]></description>
      <content:encoded><![CDATA[<p>From Vista onwards all the services run in Session 0, and session 0 has no interaction with desktop, this is named as session isolation and was added for security reason to windows. This brings a problem when we try to debug a service start up from a user session, if the service is already running one can attach the debugger and start debugging it, but attaching the debugger at startup becomes a problem.</p>
<p>Image File Execution option is the way to go:</p>
<ul>
<li>Run regedit.exe</li>
<li>Goto HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options</li>
<li>Create a new key for your service executable (example: myService.exe)</li>
<li>Create a new string value under your exe. The name of the string value is 'Debugger', and the value is debugger path like&nbsp;'c:\debuggers\windbg.exe'</li>
</ul>
<p>There still is a problem with this, when the service starts it will be started under debugger but you wont be able to see the debugger because it runs in session 0 and session 0 has no interaction with desktop. There are two ways to solve this problem:</p>
<p>1. Change the services properties and allow it to interact with desktop.&nbsp; This can be done from registry or services.msc-&gt; service Properties or can be passed as an argument in CreateService API. However some services might not be happy if you change their property, in that case you should chose second option.</p>
<p>2. Second option is to configure remote debugger session. While setting the registry for IFEO as above, add the command line parameter -Server TCP:Port=&lt;ANY PORT&gt; to windbg.exe. &nbsp;(e.g. "C:\debugger\Windbg.exe" -server tcp:port=5555). Doing so will not only start the service under debugger but also start a server session for windbg, that can be connected from user session. Now from any user session you can start windbg and connect to the session 0 debugger using 'Connect to a remote session '&nbsp;option in windbg.</p>
<p>Before you can happily debug the service now, you need overcome one last problem. And the problem is services timeout, if you start debugging the service you will find that after 30 seconds service is automatically killed and you can't debug after that. So who killed the service? Answer is SCM. SCM starts the service and expects that service should signal within 30 seconds that it is running happily, if the service doesn't respond within that time(which it ofcourse will not, if you are debugging it from its startup), SCM assumes that something bad(like a hang or crash) happened with the service, and it will terminate the service. So the solution to this problem is to increase the timeout from 30 seconds to some other good value that is long enough for you to debug the service.</p>
<p>To increase the timeout, create a new DWORD value ServicePipeTimeout under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control, and set a new timeout value. SCM reads the value as in miliseconds. You would need to restart the machine inorder to&nbsp;make the setting effective. &nbsp;Make sure that you revert back the timeout value once you are done with debugging because this is a global setting and is applicable to all the services.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Switching between User and Kernel space</title>
      <link>https://docs.microsoft.com/archive/blogs/satyem/switching-between-user-and-kernel-space</link>
      <pubDate>Tue, 04 Sep 2012 12:03:00 GMT</pubDate>
      <dc:creator><![CDATA[Satyendra Mishra [MSFT]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/satyem/2012/09/04/switching-between-user-and-kernel-space/</guid>
      <description><![CDATA[Revising&nbsp;the concept of switching from User to Kernel mode. The different modes processor...]]></description>
      <content:encoded><![CDATA[<div class="post hentry" itemscope="itemscope" itemtype="http://schema.org/BlogPosting">
<div class="post-header">
<div class="post-header-line-1">Revising&nbsp;the concept of switching from User to Kernel mode. <br />The different modes processor run into on windows OS. that is kernel mode and user mode. <br />We also say it ring0 and ring3 execution...<br />So there are many places when the OS <br />switches from User Mode to Kernel Mode or from Lower Privilege level to high <br />privilege level and vice versa. Examples are:<br />&gt; Interrupts<br />&gt; Exceptions<br />&gt; System Calls<br /><br />I would go in details with System calls today. We know that when we call a windows API from User mode, ntdll makes the <br />transition to kernel mode API and after the kernel completes the function call results are returned back to User mode.</div>
<div class="post-header-line-1"><br />For our example let's take <br />ntdll!NtReadFile. Any user mode read file operation would result in this API and <br />subsequently transferred to Kernel mode. let's check what is inside this function. Below is the disassembly of this fucntion:</div>
<div class="post-header-line-1"><br />mov&nbsp;&nbsp;&nbsp;&nbsp; <br />r10,rcx<br />mov&nbsp;&nbsp;&nbsp;&nbsp; eax,3<br />syscall<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />ret<br /><br />You can notice the syscall instruction, this instruction is the one <br />that makes a fast call to kernel mode. Just for the information, this output is <br />from a X64 based PC. <br />X64 based CPU only support Syscall for 64bit mode and <br />not in compatibility mode.&nbsp; <br />So what processor does when it sees the Syscall <br />instruction. Here are the steps directly from intel manual.<br />For Syscall the processor Saves the RFlags to R11 and RIP of the next instruction to RCX.</div>
<div class="post-header-line-1"><br />Now to run the kernel mode code:<br />the processor should have following piece of information:</div>
<div class="post-header-line-1"><br />Target Code Segment (CS) Target Instruction (where the execution should start)<br />Stack Segment <br />and System Flags.<br />This all it gets from CPU specific Registers.</div>
<div style="text-align: left;" class="post-header-line-1"><br />CS = IA32_STAR[47:32]<br />RIP = IA32_LSTAR <br />(64bit Canonical address)<br />SS = IA32_STAR[47:32] +8<br />System Flags: processor <br />sets the system flags to the logical AND of its current value witht the <br />complement of the value in IA32_FMASK_MSR. <br /><br />IA32_Star* basically are <br />MSRs(Model Specific Registers) and you can check the value of those using rdmsr <br />windbg command if you know the index of these registers(which you can easily <br />find in the Processors's manuals). The index for IA32_star is C0000081 for <br />example. C0000082 for IA32_LSTAR and C0000084 for IA32_FMASK.<br /><br />when I checked on my box I found that RIP = IA32_LSTAR&nbsp; this is the address of <br />nt!KiSystemCall64. <br />So now you know where the call will go when Syscall instruction is executed. You should also notice that this is the kernel function <br />called for any system call and not just ntdll!ntreadfile. <br /><br />Once in kernel, the nt!nt!KiSystemCall64 decides what to do (which api to call based on <br />the parameter it received from user mode. I will explain next where this parameter is coming from?)<br />Kernel stack is stored in the TSS of every task and is fetched from there. The User stack and RIP , RFlags etc are stored in the <br />stack so that when the call returns to the user mode they can be restored. <br /><br /><br />When I said the argument is passed from user mode, to identify which <br />system call will be invoked in kernel mode, that argument basically is set into <br />Eax register right before the Syscall instruction. <br /><br /><span style="font-family: Verdana; font-size: xx-small;"><span style="font-family: Verdana; font-size: xx-small;">mov r10,rcx<br />mov <br />eax,3<br />syscall<br />ret<br /><br /><span style="font-family: inherit; font-size: small;">Then corresponding to this number <br />3 we would need the API from system service table. On 64bit OS windows keeps the <br />offset of System Service calls, these offsets are 32 bits and are relative to <br />nt!kIServiceTable. So if you have to find the address of a specific service <br />routine. pick out the offset from the array nt!KiServiceTable and add that to <br />the base address of nt!kiServiceTable.</span></span></span><br /><span style="font-family: inherit;">Hang on, there is a slight change in my above <br />statement. You should not directly add the offset to get the system service <br />address. Rather there is a trick used by Kernel. Kernel actually&nbsp;does not use all bits&nbsp;to store the offset. Last&nbsp;few bits are&nbsp;used for the number of arguments to <br />that system call. </span><span style="font-family: Verdana; font-size: xx-small;"><span style="font-family: Verdana; font-size: xx-small;"><span style="font-size: xx-small;"><br /><br /><span style="font-family: inherit;"><span style="font-size: small;">We can check here the user mode ReadFile ultimately <br />goes to Kernel Mode nt!NTReadFile. Ok so what next. What if Nt!ntReadfile takes <br />some arguments, where do they come from? How does kernel receive them from user <br />mode? How does the control goes back to User mode once the systme call completes <br />its job? We will look at all these details in the next post.<br /></span></span></span><br /></span></span></div>
<span style="font-size: small;"><span style="font-family: Verdana, Bold;"><span style="font-family: Verdana, Bold;"></span></span></span><span style="font-family: Verdana, Bold; font-size: xx-small;"><span style="font-family: Verdana, Bold; font-size: xx-small;"></span></span></div>
</div>]]></content:encoded>
    </item>
    <item>
      <title>Thread's Stack</title>
      <link>https://docs.microsoft.com/archive/blogs/satyem/threads-stack</link>
      <pubDate>Mon, 13 Aug 2012 20:52:00 GMT</pubDate>
      <dc:creator><![CDATA[Satyendra Mishra [MSFT]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/satyem/2012/08/13/threads-stack/</guid>
      <description><![CDATA[Let&rsquo;s talk about the thread&rsquo;s stack today. Each thread has its own user mode and kernel...]]></description>
      <content:encoded><![CDATA[<p><span style="font-size: small;" size="3">Let&rsquo;s talk about the thread&rsquo;s stack today. Each thread has its own user mode and kernel mode stack which grows on demand. When a binary is built, linker inserts the default size for the stack into the PE header of binary. These default settings can be overridden by giving different values with /STACK option. &nbsp;Below is&nbsp;a snip of PE header file of a test executable ThreadStack.exe:</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0:000&gt; !dh ThreadStack.exe -f</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">File Type: EXECUTABLE IMAGE</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">FILE HEADER VALUES</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp; 14C machine (i386)</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5 number of sections</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">4F8A2827 time date stamp Sat Apr 14 18:45:11 2012</span><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 file pointer to symbol table</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 number of symbols</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; E0 size of optional header</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp; 102 characteristics</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Executable</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 32 bit word machine</span><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/49/97/2627.Thread2.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-49-97/2627.Thread2.png"><img border="0" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/49/97/2627.Thread2.png" original-url="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-49-97/2627.Thread2.png" /></a></span></p>
<p><span style="font-size: small;" size="3">By default the size of stack is 1 MB, however full 1MB is not committed at the beginning but only reserved. As the stack grows, more memory is committed and the amount of memory that is committed is size of Stack commit (1000) as shown above.</span></p>
<p><span style="font-size: small;" size="3">After reserving the 1 MB of memory for stack, system commits first 2 pages (i.e. 8KB) and assigns a guard attribute to second page.&nbsp; Thread starts executing from the first page and as soon as it hits the guard page, system is notified. System then removes the guard attribute from the second page, commits another page of stack memory and assigns the guard attribute to that new committed page. As the stack grows, every time a new page is committed as soon as thread hits the guard page. The last three pages of stacks are treated a little differently; I will talk about that a little later.</span></p>
<p><span style="font-size: small;" size="3">Let&rsquo;s see the stack status of ThreadStack.exe program when the recursive function has iterated a few times. ThreadStack.exe is a simple recursive program that causes stack overflow:</span></p>
<p><span style="font-family: Calibri; font-size: small;" size="3" face="Calibri">&nbsp;/*ThreadStack.cpp*/</span><br />int RecurseFunction()</p>
<p>{</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int arr[200];</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //cin&gt;&gt;arr[0];</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RecurseFunction();</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1;</p>
<p>}</p>
<p>&nbsp;int main()</p>
<p>{</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RecurseFunction();</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1;</p>
<p>}</p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0:000&gt; kL</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">ChildEBP RetAddr&nbsp; </span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0024eef8 00f0102e ThreadStack!RecurseFunction</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0024f228 00f0102e ThreadStack!RecurseFunction+0xe</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0024f558 00f0102e ThreadStack!RecurseFunction+0xe</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0024f888 00f0102e ThreadStack!RecurseFunction+0xe</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0024fbb8 00f01048 ThreadStack!RecurseFunction+0xe</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0024fbc0 00f011d9 ThreadStack!main+0x8</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0024fc08 76c81114 ThreadStack!__tmainCRTStartup+0x10b</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0024fc14 776bb299 kernel32!BaseThreadInitThunk+0xe</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0024fc54 776bb26c ntdll!__RtlUserThreadStart+0x70</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0024fc6c 00000000 ntdll!_RtlUserThreadStart+0x1b</span></p>
<p><span style="font-size: small;" size="3">&nbsp;</span></p>
<p><span style="font-size: small;" size="3">The thread environment block contains the Thread&rsquo;s stack information:</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0:000&gt; !teb</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">TEB at 7ffdf000</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0:000&gt; dt _NT_TIB stackBase StackLimit 7ffdf000</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">ThreadStack!_NT_TIB</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;&nbsp; +0x004 StackBase&nbsp; : 0x00250000 Void</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;&nbsp; +0x008 StackLimit : 0x0024e000 Void</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0:000&gt; r esp</span></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">esp=0024eefc</span></p>
<p><span style="font-family: Times New Roman; font-size: small;" size="3" face="Times New Roman">&nbsp;</span></p>
<p><span style="font-size: small;" size="3">The stack base is the base address of first page from where the thread started andb stack limit is the last valid address on the current committed page being used. The snapshot of stack at this point can be represented as below</span></p>
<p><span style="font-size: small;" size="3"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/49/97/3808.Thread1.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-49-97/3808.Thread1.png"><img border="0" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/49/97/3808.Thread1.png" original-url="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-49-97/3808.Thread1.png" /></a></span></p>
<p></p>
<p><span style="font-size: small;" size="3">Let&rsquo;s check the status and protection attribute of these pages from debugger, as the above picture depicts the first two pages are committed and third page is committed but has a guard attribute set. The fourth page is reserved but not committed.</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0:000&gt; !vprot 0x0024f000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">BaseAddress:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0024f000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">AllocationBase:&nbsp;&nbsp;&nbsp; 00150000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">AllocationProtect: 00000004&nbsp; PAGE_READWRITE</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">RegionSize:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00001000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">State:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00001000&nbsp; MEM_COMMIT</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">Protect:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00000004&nbsp; PAGE_READWRITE</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">Type:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00020000&nbsp; MEM_PRIVATE</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0:000&gt; !vprot 0x0024e000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">BaseAddress:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0024e000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">AllocationBase:&nbsp;&nbsp;&nbsp; 00150000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">AllocationProtect: 00000004&nbsp; PAGE_READWRITE</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">RegionSize:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00002000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">State:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00001000&nbsp; MEM_COMMIT</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">Protect:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00000004&nbsp; PAGE_READWRITE</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">Type:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00020000&nbsp; MEM_PRIVATE</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0:000&gt; !vprot 0x0024d000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">BaseAddress:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0024d000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">AllocationBase:&nbsp;&nbsp;&nbsp; 00150000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">AllocationProtect: 00000004&nbsp; PAGE_READWRITE</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">RegionSize:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00001000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">State:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00001000&nbsp; MEM_COMMIT</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">Protect:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00000104&nbsp; PAGE_READWRITE + PAGE_GUARD</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">Type:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00020000&nbsp; MEM_PRIVATE</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0:000&gt; !vprot 0x0024c000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">BaseAddress:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0024c000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">AllocationBase:&nbsp;&nbsp;&nbsp; 00150000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">AllocationProtect: 00000004&nbsp; PAGE_READWRITE</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">RegionSize:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00001000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">State:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00002000&nbsp; MEM_RESERVE</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">Type:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00020000&nbsp; MEM_PRIVATE</span></p>
<p></p>
<p><span style="font-size: small;"><span style="font-family: arial,helvetica,sans-serif;" face="Times New Roman">&nbsp;</span><span style="font-family: Times New Roman;" face="Times New Roman"><span style="font-family: arial,helvetica,sans-serif;">This way the stack keeps </span><span style="font-family: times new roman,times;"><span style="font-family: arial,helvetica,sans-serif;">growing but in the last three pages system does things a little differently. When the program access the 3rd page from end as usual system is notified and system removes the Guard attribute from this page, commits the next page BUT this time it doesn&rsquo;t apply the guard page attribute to the newly committed page(second last page). Also after committing the new page system raises a stack Overflow exception which you can catch if you are running the program into debugger. Let&rsquo;s see this in our program threadstack.exe.</span></span></span></span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;</span><span style="font-family: Times New Roman;" face="Times New Roman">0:000&gt; g</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">(1284.96c): Stack overflow - code c00000fd (first chance)</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">First chance exceptions are reported before any exception handling.</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">This exception may be expected and handled.</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">eax=004720d0 ebx=7ffd9000 ecx=00000001 edx=776a6194 esi=00000000 edi=00000000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">eip=00f01029 esp=00152ed0 ebp=001531f8 iopl=0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nv up ei pl nz na po nc</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">cs=001b&nbsp; ss=0023&nbsp; ds=0023&nbsp; es=0023&nbsp; fs=003b&nbsp; gs=0000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; efl=00010202</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">ThreadStack!RecurseFunction+0x9:</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">00f01029 e8d7ffffff&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; call&nbsp;&nbsp;&nbsp; ThreadStack!ILT+0(?RecurseFunctionYAHXZ) (00f01005)</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0:000&gt; !error c00000fd</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">Error code: (NTSTATUS) 0xc00000fd (3221225725) - A new guard page for the stack cannot be created.</span></p>
<p></p>
<p><span style="font-family: arial,helvetica,sans-serif; font-size: small;"><span face="Times New Roman">&nbsp;</span><span face="Times New Roman">Below output shows that no guard attribute is set to second last page:</span></span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;</span><span style="font-family: Times New Roman;" face="Times New Roman">0:000&gt; !vprot 0x00151000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">BaseAddress:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00151000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">AllocationBase:&nbsp;&nbsp;&nbsp; 00150000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">AllocationProtect: 00000004&nbsp; PAGE_READWRITE</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">RegionSize:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 000ff000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">State:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00001000&nbsp; MEM_COMMIT</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">Protect:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00000004&nbsp; PAGE_READWRITE</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">Type:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00020000&nbsp; MEM_PRIVATE</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">0:000&gt; !vprot 0x00150000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">BaseAddress:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00150000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">AllocationBase:&nbsp;&nbsp;&nbsp; 00150000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">AllocationProtect: 00000004&nbsp; PAGE_READWRITE</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">RegionSize:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00001000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">State:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;00002000&nbsp; MEM_RESERVE</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">Type:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00020000&nbsp; MEM_PRIVATE</span></p>
<p></p>
<p><span style="font-family: arial,helvetica,sans-serif; font-size: small;"><span face="Times New Roman">&nbsp;</span><span face="Times New Roman">At this point the stacklimit would be the last address in second last page.</span></span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;</span><span style="font-family: Times New Roman;" face="Times New Roman">0:000&gt; dt _NT_TIB stackBase StackLimit 7ffdf000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">ThreadStack!_NT_TIB</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">StackBase:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00250000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;&nbsp; StackLimit:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00151000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;</span></p>
<p></p>
<p><span style="font-family: arial,helvetica,sans-serif; font-size: small;" face="Times New Roman">By running the program under debugger we can see that the stack has reached third last page. If you continue &lsquo;g&rsquo; the program would run and consume the rest of the space onto the stack. Once it consumes the second last page as well and tries to access the Last page the program would raise an access violation exception since the last page is just reserved and not committed. At this point the program would be terminated. As you can see the thread gets one page less than what was reserved for stack since the last page is never committed.</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">&nbsp;</span><span style="font-family: Times New Roman;" face="Times New Roman">0:000&gt; g</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">(1284.96c): Access violation - code c0000005 (first chance)</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">First chance exceptions are reported before any exception handling.</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">This exception may be expected and handled.</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">eax=004720d0 ebx=7ffd9000 ecx=00000001 edx=776a6194 esi=00000000 edi=00000000</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">eip=00f01029 esp=00150ef0 ebp=00151218 iopl=0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nv up ei pl nz na pe nc</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">cs=001b&nbsp; ss=0023&nbsp; ds=0023&nbsp; es=0023&nbsp; fs=003b&nbsp; gs=0000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; efl=00010206</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">ThreadStack!RecurseFunction+0x9:</span></p>
<p></p>
<p><span style="font-family: Times New Roman;" face="Times New Roman">00f01029 e8d7ffffff&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; call&nbsp;&nbsp;&nbsp; ThreadStack!ILT+0(?RecurseFunctionYAHXZ) (00f01005)</span></p>
<p></p>]]></content:encoded>
    </item>
    <item>
      <title>User Mode program access kernel mode address</title>
      <link>https://docs.microsoft.com/archive/blogs/satyem/user-mode-program-access-kernel-mode-address</link>
      <pubDate>Mon, 13 Aug 2012 20:50:00 GMT</pubDate>
      <dc:creator><![CDATA[Satyendra Mishra [MSFT]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/satyem/2012/08/13/user-mode-program-access-kernel-mode-address/</guid>
      <description><![CDATA[What happens when a user mode program access the kernel mode address. The access fault happens,...]]></description>
      <content:encoded><![CDATA[<p>What happens when a user mode program access the kernel mode address. The access fault happens, let's see how does the flow go in such scenario..</p>
<p>Actually all pages have a protection attribute that tells whether the page is owned by kernel or user. !pte shows that as K or U flag.</p>
<p>Now when CPU tries to access any page, it checks what privilege level is it running into. How is that checked? That it checks using code segment registers CPL (Current Privilege level value). you can always check the Privelege level the CPU is running into from cs register.</p>
<p><span style="font-family: courier new,courier;">//Cs in Kernel mode . check that p/l value is 0. That means ring 0</span><br /><span style="font-family: courier new,courier;">kd&gt; dg cs</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; P Si Gr Pr Lo</span><br /><span style="font-family: courier new,courier;">Sel&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Base&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Limit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Type&nbsp;&nbsp;&nbsp; l ze an es ng Flags</span><br /><span style="font-family: courier new,courier;">---- ----------------- ----------------- ---------- - -- -- -- -- --------</span><br /><span style="font-family: courier new,courier;">0010 00000000`00000000 00000000`00000000 Code RE Ac 0 Nb By P&nbsp; Lo 0000029b</span></p>
<p><span style="font-family: courier new,courier;">//Cs for user mode instruction. check that p/l value is 3. That means ring 3</span><br /><span style="font-family: courier new,courier;">kd&gt; dg cs</span><br /><span style="font-family: courier new,courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; P Si Gr Pr Lo</span><br /><span style="font-family: courier new,courier;">Sel&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Base&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Limit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Type&nbsp;&nbsp;&nbsp; l ze an es ng Flags</span><br /><span style="font-family: courier new,courier;">---- ----------------- ----------------- ---------- - -- -- -- -- --------</span><br /><span style="font-family: courier new,courier;">0033 00000000`00000000 00000000`00000000 Code RE Ac 3 Nb By P&nbsp; Lo 000002fb</span></p>
<p>&nbsp;For any memory address access, the Pvivilege level from CS and page protection is compared and if user mode tries to access any kernel mode address, cpu traps. MMAccessFult is the trap handler for such traps.</p>]]></content:encoded>
    </item>
    <item>
      <title>Network connectivity issues diagnosis step by step - Part II.</title>
      <link>https://docs.microsoft.com/archive/blogs/satyem/network-connectivity-issues-diagnosis-step-by-step-part-ii</link>
      <pubDate>Fri, 23 Dec 2011 15:13:00 GMT</pubDate>
      <dc:creator><![CDATA[Satyendra Mishra [MSFT]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/satyem/2011/12/23/network-connectivity-issues-diagnosis-step-by-step-part-ii/</guid>
      <description><![CDATA[In the previous post, we discussed how to make sure if Windows Firewall really dropped incoming...]]></description>
      <content:encoded><![CDATA[<p>In the previous post, we discussed how to make sure if Windows Firewall really dropped incoming packets. If the machine is domain joined then group policies might not allow you to turn off firewall. In that case logging or creating inbound rules should be of help to determine the packet drop by WF. There is one other, some what&nbsp;less recommended, way to turn off firewall if policies don't allow to turn it off from Firewall management snapin and that is turning off the BFE service. Its BFE which creates and manages all the firewall filters&nbsp;and turning BFE off removes the firewall filter. Keep&nbsp;in mind that turning off BFE will stop a few other dependent services as well so when&nbsp;you are done with your experiments you should turn on the those dependent services also with BFE.</p>
<p>That's all for windows Firewall. If there are&nbsp; third party firewalls on your system, they could also be the one causing packets drop. Each third party firewall has different configurations and troubleshooting, about which I can't comment&nbsp;much. However vista onwards most thirdparty firewall use BFE only, so you can see their filters and drop events in the BFE/WFP traces. Look out the command help of "netsh wfp capture" to learn about WFP tracing commands. These traces are&nbsp; little&nbsp; bit complex as they deal at filter level. Usually filter description gives&nbsp;some information about the filters source.This trace can be&nbsp;huge since&nbsp;it consists of all the communication esatblsihed with machine and all the filters present in the machine.</p>
<p style="text-align: left;">WFP auditing can also be used for WFP event log traces. You can enable WFP auditing using auditpol.exe. Below&nbsp; commands can be used to turn on and off WFP auditing. And the logs can&nbsp; be checked from Security Event logs.</p>
<p style="text-align: left;">auditpol /set /subcategory:"Filtering Platform Connection" /success:enable /failure:enable</p>
<p style="text-align: left;">auditpol /set /subcategory:"Filtering Platform Connection" /success: disable /failure: disable<br /><br />So firewall is mostly the reason for packet drops. But&nbsp;in some cases&nbsp;it could be IPSec also. IPSec mostly comes into picture if your machine is domain joined and group policies have pushed IPSec policies on the machine. Needless to say IPSec policies can be there on non-domain joined machine also if the Admin has created them locally, but I haven't seen much such cases. Also if there are no IPSec Policies on the machine, then IPSec is not involved in the communication irrespective of the IPSec/IKE service status(running /stopped). &nbsp;</p>
<p style="text-align: left;">Using Netmon you can check if any IKE packets are there in the capture between the two machine, if they are present then IPSes negotiation can be there between two machines and you should turn your attention towards IPSec Policies. IPSec rules can be seen from Firewall control panel(Wf.Msc)-&gt; Connection Security Rule.</p>
<p style="text-align: left;"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/49/97/8228.MMSA.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-49-97/8228.MMSA.png"><img border="0" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/49/97/8228.MMSA.png" original-url="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-49-97/8228.MMSA.png" /></a></p>
<p style="text-align: left;">If IPSec is being used, you should make sure the corresponding policies are present on both the machine(unless one machine&nbsp;acts as&nbsp;a&nbsp; boundary machine). Again turning off BFE would disable IPSEC but that should be done on both the machine if you are&nbsp; doing experiment. I would like again emphasize that BFE and other dependent services should be&nbsp; turned on again once you are done with verification.&nbsp; Ping utility comes often handy to verify the communication between machines. More than often ICMP packets are exempted in IPSEC policies by the admins, so ping can be&nbsp;helpful in verifying the connectivity and making sure if IPSEC&nbsp; is&nbsp; causing the problem.</p>
<p style="text-align: left;">There are also authentication issues while accessing shares but they are not related to connectivity between machines but rather to NTFS and SMB permissions.&nbsp;&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Where is my exception handler code in the function disassembly?</title>
      <link>https://docs.microsoft.com/archive/blogs/satyem/where-is-my-exception-handler-code-in-the-function-disassembly</link>
      <pubDate>Thu, 22 Dec 2011 14:52:00 GMT</pubDate>
      <dc:creator><![CDATA[Satyendra Mishra [MSFT]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/satyem/2011/12/22/where-is-my-exception-handler-code-in-the-function-disassembly/</guid>
      <description><![CDATA[This post&nbsp;discusses how&nbsp;compiler&nbsp;actually stores&nbsp;the&nbsp;exception filters and...]]></description>
      <content:encoded><![CDATA[<p>This post&nbsp;discusses how&nbsp;compiler&nbsp;actually stores&nbsp;the&nbsp;exception filters and exception handler in the stack required&nbsp;for exception handling mechanism provided by OS.&nbsp;&nbsp;Have a look at the disassembly of following main function&nbsp;and observe&nbsp;that&nbsp;on&nbsp;issuing <em>uf main in windbg,&nbsp;</em> the output&nbsp;doesn't show the code under <em>__except </em>block. So where&nbsp;are my exception filter function and excpetion handler.</p>
<p>int main()<br />{<br />&nbsp;int i=5;<br />&nbsp;__try<br />&nbsp;{<br />&nbsp;i = 5/(i-i);<br />&nbsp;}<br />&nbsp;__except(FilterFunction2(1))<br />&nbsp;{<br />&nbsp;&nbsp;printf("__Except block\n");<br />&nbsp;}</p>
<p>&nbsp;return 1;<br />}</p>
<p>0:000&gt; uf main<br />DivideByZero!main [c:\program files\microsoft visual studio 10.0\vc\homework\dividebyzero.cpp @ 13]:<br />&nbsp;&nbsp; 13 01061040 55&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; ebp<br />&nbsp;&nbsp; 13 01061041 8bec&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; ebp,esp<br />&nbsp;&nbsp; 13 01061043 6afe&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; 0FFFFFFFEh<br />&nbsp;&nbsp; 13 01061045 6830af0701&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; offset DivideByZero!__rtc_tzz+0x104 (0107af30)<br />&nbsp;&nbsp; 13 0106104a 68a0120601&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; offset DivideByZero!_except_handler4 (010612a0)<br />&nbsp;&nbsp; 13 0106104f 64a100000000&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; eax,dword ptr fs:[00000000h]<br />&nbsp;&nbsp; 13 01061055 50&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; eax<br />&nbsp;&nbsp; 13 01061056 83c4f4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add&nbsp;&nbsp;&nbsp;&nbsp; esp,0FFFFFFF4h<br />&nbsp;&nbsp; 13 01061059 53&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; ebx<br />&nbsp;&nbsp; 13 0106105a 56&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; esi<br />&nbsp;&nbsp; 13 0106105b 57&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; edi<br />&nbsp;&nbsp; 13 0106105c a100c00701&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; eax,dword ptr [DivideByZero!__security_cookie (0107c000)]<br />&nbsp;&nbsp; 13 01061061 3145f8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xor&nbsp;&nbsp;&nbsp;&nbsp; dword ptr [ebp-8],eax<br />&nbsp;&nbsp; 13 01061064 33c5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xor&nbsp;&nbsp;&nbsp;&nbsp; eax,ebp<br />&nbsp;&nbsp; 13 01061066 50&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; eax<br />&nbsp;&nbsp; 13 01061067 8d45f0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lea&nbsp;&nbsp;&nbsp;&nbsp; eax,[ebp-10h]<br />&nbsp;&nbsp; 13 0106106a 64a300000000&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; dword ptr fs:[00000000h],eax<br />&nbsp;&nbsp; 13 01061070 8965e8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; dword ptr [ebp-18h],esp<br />&nbsp;&nbsp; 14 01061073 c745e405000000&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; dword ptr [ebp-1Ch],5<br />&nbsp;&nbsp; 15 0106107a c745fc00000000&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; dword ptr [ebp-4],0<br />&nbsp;&nbsp; 17 01061081 8b4de4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; ecx,dword ptr [ebp-1Ch]<br />&nbsp;&nbsp; 17 01061084 2b4de4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sub&nbsp;&nbsp;&nbsp;&nbsp; ecx,dword ptr [ebp-1Ch]<br />&nbsp;&nbsp; 17 01061087 b805000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; eax,5<br />&nbsp;&nbsp; 17 0106108c 99&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdq<br />&nbsp;&nbsp; 17 0106108d f7f9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; idiv&nbsp;&nbsp;&nbsp; eax,ecx<br />&nbsp;&nbsp; 17 0106108f 8945e4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; dword ptr [ebp-1Ch],eax<br />&nbsp;&nbsp; 18 01061092 c745fcfeffffff&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; dword ptr [ebp-4],0FFFFFFFEh<br />&nbsp;&nbsp; 18 01061099 eb22&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; jmp&nbsp;&nbsp;&nbsp;&nbsp; DivideByZero!main+0x7d (010610bd)</p>
<p>DivideByZero!main+0x7d [c:\program files\microsoft visual studio 10.0\vc\homework\dividebyzero.cpp @ 24]:<br />&nbsp;&nbsp; 24 010610bd b801000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; eax,1<br />&nbsp;&nbsp; 25 010610c2 8b4df0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; ecx,dword ptr [ebp-10h]<br />&nbsp;&nbsp; 25 010610c5 64890d00000000&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; dword ptr fs:[0],ecx<br />&nbsp;&nbsp; 25 010610cc 59&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp; ecx<br />&nbsp;&nbsp; 25 010610cd 5f&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp; edi<br />&nbsp;&nbsp; 25 010610ce 5e&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp; esi<br />&nbsp;&nbsp; 25 010610cf 5b&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp; ebx<br />&nbsp;&nbsp; 25 010610d0 8be5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; esp,ebp<br />&nbsp;&nbsp; 25 010610d2 5d&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp; ebp<br />&nbsp;&nbsp; 25 010610d3 c3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ret</p>
<p>This code is compiled in VC, some of the below details might be different for different compilers.</p>
<p>Durig compilation if a function has exception handling&nbsp;blocks( __try and __except) in it, then compiler pushes an Extended Exception Registration Record to stack. One important thing here is that, for one function there will be only Extended Exception Resgistration Record pushed&nbsp;onto the stack, irrespective of number of __try block in the functions. Lets see what this record is:</p>
<p>0:000:x86&gt; dt PEH4_EXCEPTION_REGISTRATION_RECORD -r1<br />DivideByZero!PEH4_EXCEPTION_REGISTRATION_RECORD<br />Ptr32&nbsp;&nbsp;&nbsp; +0x000 SavedESP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Ptr32 Void<br />&nbsp;&nbsp; +0x004 ExceptionPointers : Ptr32 _EXCEPTION_POINTERS<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; +0x000 ExceptionRecord&nbsp; : Ptr32 _EXCEPTION_RECORD<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; +0x004 ContextRecord&nbsp;&nbsp;&nbsp; : Ptr32 _CONTEXT<br />&nbsp;&nbsp; +0x008 SubRecord&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : _EXCEPTION_REGISTRATION_RECORD<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; +0x000 Next&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Ptr32 _EXCEPTION_REGISTRATION_RECORD<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; +0x004 Handler&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Ptr32&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _EXCEPTION_DISPOSITION <br />&nbsp;&nbsp; +0x010 EncodedScopeTable : Uint4B<br />&nbsp;&nbsp; +0x014 TryLevel&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Uint4B</p>
<p>TryLevel is used as an index to the ScopeTable array&nbsp;to fetch out the right __except block( since there may be more than one nested try blocks). If you see from the above disassembly TryLevel is&nbsp;&nbsp; at ebp-4, hence [ebp-4] gets updated every time the compiler sees&nbsp;that a&nbsp;new try block is entered/exited. Now lets see where this record is pushed in the stack.</p>
<p>Below is the snip from start of main function:</p>
<p>&nbsp;&nbsp; 13 01061040 55&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; ebp<br />&nbsp;&nbsp; 13 01061041 8bec&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; ebp,esp</p>
<p>//This is try level,&nbsp;this value would be&nbsp;changed when&nbsp;a try block is entered.<br />&nbsp;&nbsp; 13 01061043 6afe&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; 0FFFFFFFEh</p>
<p>//This is the scope table we&nbsp;would talk about this in a moment.<br />&nbsp; &nbsp;13 01061045 6830af0701&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; offset DivideByZero!__rtc_tzz+0x104 (0107af30)</p>
<p>//This is common windows handler function called&nbsp;on exceptions. This in turn call the exception&nbsp;FilterFunction&nbsp;defined&nbsp;for the function.<br />&nbsp;&nbsp; 13 0106104a 68a0120601&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; offset DivideByZero!_except_handler4 (010612a0)</p>
<p>//This is Next Field of SubRecord struct shown above</p>
<p>&nbsp;&nbsp; 13 0106104f 64a100000000&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; eax,dword ptr fs:[00000000h]<br />&nbsp;&nbsp; 13 01061055 50&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; eax&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<p>//&nbsp;Here would come&nbsp;other two members(SavedEsp and Exception POinters) of the record.</p>
<p>&nbsp; 13 01061056 83c4f4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add&nbsp;&nbsp;&nbsp;&nbsp; esp,0FFFFFFF4h</p>
<p>For any exception raised,&nbsp;_except_handler4 function is invoked by OS and is passed&nbsp;the Exception Registration Record as a paremeter. This function subtracts an offset 8 (offset of SubRecord field)&nbsp;to&nbsp; get the Extended Exception Registration record&nbsp;that was pushed on to stack.</p>
<p>In Extended exception registration record, Scope table contains the actual Exception Filter Function and exception handler. Now lets take a look at the Scope Table:</p>
<p>0:000&gt; dt _EH4_SCOPETABLE<br />DivideByZero!_EH4_SCOPETABLE<br />&nbsp;&nbsp; +0x000 GSCookieOffset&nbsp;&nbsp; : Uint4B<br />&nbsp;&nbsp; +0x004 GSCookieXOROffset : Uint4B<br />&nbsp;&nbsp; +0x008 EHCookieOffset&nbsp;&nbsp; : Uint4B<br />&nbsp;&nbsp; +0x00c EHCookieXOROffset : Uint4B<br />&nbsp;&nbsp; +0x010 ScopeRecord&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : [1] _EH4_SCOPETABLE_RECORD</p>
<p>The ScopeRecord field is an array of _SCOPETABLE_RECORD which contains the Exception Filter Function and Exception Handler for each try block. Here TryLevel (discussed above)&nbsp;is used as an index to get the right Filter function and&nbsp; handler for corresponding _try block. ScopeRecord is at offset 0x10 of this structure. The scope table is at 0x0107af30, lets dump out the Scope record. Since&nbsp;for above main function TryLevel is 0, the corresponding excpetion Filter function and handler would be at&nbsp;index 0.&nbsp;</p>
<p>&nbsp;<span style="line-height: 115%; font-family: 'Calibri','sans-serif'; font-size: 8pt; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: Mangal; mso-bidi-theme-font: minor-bidi; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"><br /></span>0:000&gt; dd 0x0107af30+10 &nbsp;l4<br />0107af40&nbsp; fffffffe 0106109b 010610a6 00000000</p>
<p><br />0:000&gt; dt _EH4_SCOPETABLE_RECORD<br />DivideByZero!_EH4_SCOPETABLE_RECORD<br />&nbsp;&nbsp; +0x000 EnclosingLevel&nbsp;&nbsp; : Uint4B<br />&nbsp;&nbsp; +0x004 FilterFunc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Ptr32&nbsp;&nbsp;&nbsp;&nbsp; long <br />&nbsp;&nbsp; +0x008 u&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : &lt;unnamed-tag&gt;&nbsp;</p>
<p>&nbsp;FilterFunc is at offset 4 let's see what&nbsp; filter function is:</p>
<p>0:000&gt; uf 0106109b<br />DivideByZero!main+0x5b [c:\program files\microsoft visual studio 10.0\vc\homework\dividebyzero.cpp @ 19]:<br />&nbsp;&nbsp; 19 0106109b 6a01&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; 1<br />&nbsp;&nbsp; 19 0106109d e863ffffff&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; call&nbsp;&nbsp;&nbsp; DivideByZero!ILT+0(?FilterFunction2YAHHZ) (01061005)<br />&nbsp;&nbsp; 19 010610a2 83c404&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add&nbsp;&nbsp;&nbsp;&nbsp; esp,4<br />&nbsp;&nbsp; 19 010610a5 c3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ret</p>
<p>As you can compare with the code in the very begining, this is the assembly for Exception Filter Function(i.e. the code inside the __except()).</p>
<p>Now lets see where is our Exception handler Code(i.e the code insie __except {}). _EH4_SCOPETABLE_RECORD.u&nbsp;contains the address of exception handler block. Lets dump this:</p>
<p>0:000&gt; u 010610a6<br />DivideByZero!main+0x66 [c:\program files\microsoft visual studio 10.0\vc\homework\dividebyzero.cpp @ 19]:<br />010610a6 8b65e8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; esp,dword ptr [ebp-18h]<br />010610a9 68d87e0701&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp;&nbsp; offset DivideByZero!std::_Iosb&lt;int&gt;::end+0x4 (01077ed8)<br />010610ae e84e000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; call&nbsp;&nbsp;&nbsp; DivideByZero!printf (01061101)<br />010610b3 83c404&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add&nbsp;&nbsp;&nbsp;&nbsp; esp,4<br />010610b6 c745fcfeffffff&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; dword ptr [ebp-4],0FFFFFFFEh<br />010610bd b801000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; eax,1<br />010610c2 8b4df0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; ecx,dword ptr [ebp-10h]<br />010610c5 64890d00000000&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; dword ptr fs:[0],ecx<br />0:000&gt; u<br />DivideByZero!main+0x8c [c:\program files\microsoft visual studio 10.0\vc\homework\dividebyzero.cpp @ 25]:<br />010610cc 59&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp; ecx<br />010610cd 5f&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp; edi<br />010610ce 5e&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp; esi<br />010610cf 5b&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp; ebx<br />010610d0 8be5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp; esp,ebp<br />010610d2 5d&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp;&nbsp; ebp</p>
<p>Again if you compare, 010610a6 is the start address of code&nbsp;lines inside __except {} block.</p>
<p><br />&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Windbg loops around First chance and Second chance exceptions</title>
      <link>https://docs.microsoft.com/archive/blogs/satyem/windbg-loops-around-first-chance-and-second-chance-exceptions</link>
      <pubDate>Thu, 22 Dec 2011 14:50:00 GMT</pubDate>
      <dc:creator><![CDATA[Satyendra Mishra [MSFT]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/satyem/2011/12/22/windbg-loops-around-first-chance-and-second-chance-exceptions/</guid>
      <description><![CDATA[My program causes an exception and debugger catches it, but the debugger is stuck in loop of first...]]></description>
      <content:encoded><![CDATA[<p>My program causes an exception and debugger catches it, but the debugger is stuck in loop of first chance and second chance exception. Why isn't it get out of the loop and terminates the program if the exception is not handled?</p>
<p>A snip from debugger output</p>
<p>(2724.2708): Integer divide-by-zero - code c0000094 (first chance)<br />First chance exceptions are reported before any exception handling.<br />This exception may be expected and handled.<br />DivideByZero!main+0x15:<br />013516b5 f7f9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; idiv&nbsp;&nbsp;&nbsp; eax,ecx<br />0:000:x86&gt; g<br />(2724.2708): Integer divide-by-zero - code c0000094 (!!! second chance !!!)<br />DivideByZero!main+0x15:<br />013516b5 f7f9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; idiv&nbsp;&nbsp;&nbsp; eax,ecx<br />0:000:x86&gt; g<br />(2724.2708): Integer divide-by-zero - code c0000094 (first chance)<br />First chance exceptions are reported before any exception handling.<br />This exception may be expected and handled.<br />DivideByZero!main+0x15:<br />013516b5 f7f9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; idiv&nbsp;&nbsp;&nbsp; eax,ecx<br />0:000:x86&gt; g<br />(2724.2708): Integer divide-by-zero - code c0000094 (!!! second chance !!!)<br />DivideByZero!main+0x15:<br />013516b5 f7f9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; idiv&nbsp;&nbsp;&nbsp; eax,ecx</p>
<p>&nbsp;</p>
<p>When the exception is raised, windows kernel looks if the debugger is attached to the process. If yes, the process is broken into the debugger with First chance exception message. Continuing with 'g' goes with exception unhandled and the process is again broken into the debugger with Second chance exception message. However after first chance exception if&nbsp;you continue with 'gh' instead of 'g', you will&nbsp;see a loop of First chance exception only and no second chance.</p>
<p>After second chance exception continuing with 'g' goes with exception handled,&nbsp;and the kernel resumes the process again from the instruction where exception happened. Which again leads to the First chance exception and the cycle goes on...</p>
<p>However if after Second chance exception you continue with 'gn' instead of 'g', the debugger goes with exception not handled and program is terminated. You won't see an exception loop.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Network connectivity issues diagnosis step by step - Part I.</title>
      <link>https://docs.microsoft.com/archive/blogs/satyem/network-connectivity-issues-diagnosis-step-by-step-part-i</link>
      <pubDate>Wed, 21 Dec 2011 15:15:00 GMT</pubDate>
      <dc:creator><![CDATA[Satyendra Mishra [MSFT]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/satyem/2011/12/21/network-connectivity-issues-diagnosis-step-by-step-part-i/</guid>
      <description><![CDATA[Many a time we run into network problems between computers in home, offices, labs etc.&nbsp; which...]]></description>
      <content:encoded><![CDATA[<p>Many a time we run into network problems between computers in home, offices, labs etc.&nbsp; which sometimes are easy to diagnose and sometimes they are not. I have heard from customers&nbsp;multiple times&nbsp;that they are not able to access some share on the other machine or not able to connect Telnet to other machines etc. But they are not able to track where exactly the problem is and what it is?</p>
<p>This post discusses some diagnosis steps which would be useful to anyone doing network troubleshooting. The focus is majorly on the issues caused by&nbsp;firewalls or any other&nbsp;filters&nbsp;in the stack&nbsp;blocking the communications. They are very general steps which many people might already know about,&nbsp;however following these steps help narrow down the issue and track to the root cause. Let's take an example of two machines A and B, where A is not able to access some resources shared on B (say <a href="file://\\B\SharedFiles\&quot; data-mce-href=" sharedfiles="" b="">\\B\SharedFiles\</a>).</p>
<p>Very first thing one&nbsp;should try checking is whether or not DNS is able to resolve remote machine name. To verify this try accessing the remote machine with&nbsp; its IP and not with its name. If accessing with IP works that alarms you about DNS name resolution problem. There could be problems like stale DNS cache, duplicate name or any other issues with DNS. However if accessing with IP also doesn't work then further steps should be tried to narrow the issue further down. It's better to use IP address in further troubleshooting so that you don't mess up with any DNS issue again during investigation.</p>
<p>Now there could be multiple places where the connection setup could be failing like:</p>
<ol>
<li>The connection packet started from machine A could not leave the Machine A itself.</li>
<li>The connection packet reached machine B but B&nbsp;never replied.</li>
</ol>
<p>Any network traffic capture software(like netmon) can help you identify above points in your network setup.</p>
<p>Mostly the customers run into issue pointed out by number 2. However if its #1 then probable reasons could be Firewall outbound rules, or a misconfigured gateway on Machine A. Most of the firewall by default allow the outbound connections but you should double check the firewall outbound configuration in this case and make sure it is right.</p>
<p>If it is #2, i.e. in network capture software running on machine B, you can see an incoming packet on B from A but no outgoing packet from B to A, in this case the chances are that firewall or something else dropped the incoming packet. Network capture software usually sit at below layers on the stack so it reads the packets long before the packet reaches firewall and is dropped.</p>
<p>Now is the time to verify if the firewall really dropped the incoming packet. If you are not able to see any inbound rule in the firewall for the communication in question, you should create an inbound&nbsp;rule in firewall for the desired communication&nbsp;and then check if the connections succeeds. Different firewalls have different interfaces to create inbound rule. One thing you should remember while creating rules in Windows firewall is that you chose right profile in your inbound rule. Windows firewall has three profiles Public, Domain and Private and while creating rules you need to select profiles for which the rules should be applicable. If you are not sure about the profiles, for investigation purpose you can chose all profiles so that your new incoming rule is applicable to all profiles.</p>
<p>For troubleshooting sake you might also consider to turn off firewall and then try the connection. I have seen a few customers do a mistake while turning off Windows Firewall, instead off turning it off from windows firewall&nbsp;management console,&nbsp;they just stop the firewall service(net stop mpssvc). Note that stopping the service is not the right way to turn firewall off, you should go to firewall management console (Wf.msc) and then should turn firewall off from there for all the profiles. I should emphasize that if you are not sure about the firewall profiles, turn it off for all the three profiles.</p>
<p>Well if the creating an incoming firewall rule or turning&nbsp;the firewall&nbsp;off helps solves the issue, it was your firewall that dropped the packets. You can figure out the same thing by enabling firewall logging and peeking into the trace and check if it has a trace for the dropped packet of communication in question. This might be difficult if there are many connection established on the machine as the trace becomes large.</p>
<p>Till now I just talked about Windows firewall, there might be other third party firewall dropping the packets. Or there could be something else blocking the communication like IPSec. Let's discuss them in next post.....</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
  </channel>
</rss>