<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CodeItNow &#187; MockItNow</title>
	<atom:link href="http://www.rorydriscoll.com/category/mockitnow/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rorydriscoll.com</link>
	<description></description>
	<lastBuildDate>Mon, 23 Jan 2012 01:50:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>MockItNow: Throwing Exceptions</title>
		<link>http://www.rorydriscoll.com/2009/01/15/mockitnow-throwing-exceptions/</link>
		<comments>http://www.rorydriscoll.com/2009/01/15/mockitnow-throwing-exceptions/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 08:53:44 +0000</pubDate>
		<dc:creator>Rory</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[MockItNow]]></category>

		<guid isPermaLink="false">http://www.rorydriscoll.com/?p=162</guid>
		<description><![CDATA[I&#8217;ve made a small update to MockItNow to allow you to throw exceptions when replaying function calls. You basically record the function call as normal, and provide the exception object that you want to throw during the replay using the EXPECT_THROW macro. You can also make a function default to throwing an exception at registration [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve made a small update to MockItNow to allow you to throw exceptions when replaying function calls. You basically record the function call as normal, and provide the exception object that you want to throw during the replay using the EXPECT_THROW macro. You can also make a function default to throwing an exception at registration time using REGISTER_THROW.</p>
<p>If you want to see a couple of examples of this feature, take a look at the bottom of the sample file <a href="http://code.google.com/p/mockitnow/source/browse/trunk/Examples/TestExamples.cpp">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rorydriscoll.com/2009/01/15/mockitnow-throwing-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Minor Update to MockItNow</title>
		<link>http://www.rorydriscoll.com/2008/11/15/minor-update-to-mockitnow/</link>
		<comments>http://www.rorydriscoll.com/2008/11/15/minor-update-to-mockitnow/#comments</comments>
		<pubDate>Sat, 15 Nov 2008 18:01:07 +0000</pubDate>
		<dc:creator>Rory</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[MockItNow]]></category>

		<guid isPermaLink="false">http://www.rorydriscoll.com/?p=69</guid>
		<description><![CDATA[This is just a quick note to say that I&#8217;ve updated MockItNow on Google Code to allow you to define storage types on a per-class basis using the DECLARE_STORAGE_TYPE macro. I did this so that the Mocker can deal with abstract class parameters. Please note that the macro must be declared at global scope because it [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a quick note to say that I&#8217;ve updated MockItNow on Google Code to allow you to define storage types on a per-class basis using the DECLARE_STORAGE_TYPE macro. I did this so that the Mocker can deal with abstract class parameters. Please note that the macro must be declared at global scope because it uses partial template specialization.</p>
<p>I updated the download, and the source. You can see the new test at the end of the file <a href="http://code.google.com/p/mockitnow/source/browse/trunk/Examples/TestExamples.cpp">here</a>, and the only other affected file is <a href="http://code.google.com/p/mockitnow/source/browse/trunk/Library/Storage.h">Storage.h</a>.</p>
<p>Thanks to Lance for pointing this problem out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rorydriscoll.com/2008/11/15/minor-update-to-mockitnow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MockItNow: Sample Code Added</title>
		<link>http://www.rorydriscoll.com/2008/05/20/mockitnow-sample-code-added/</link>
		<comments>http://www.rorydriscoll.com/2008/05/20/mockitnow-sample-code-added/#comments</comments>
		<pubDate>Wed, 21 May 2008 06:21:40 +0000</pubDate>
		<dc:creator>Rory</dc:creator>
				<category><![CDATA[MockItNow]]></category>

		<guid isPermaLink="false">http://www.rorydriscoll.com/?p=18</guid>
		<description><![CDATA[I&#8217;ve just uploaded a sample project for MockItNow to Google Code. Get it here. This file includes the Examples project, the latest version of MockItNow, and UnitTest++. The projects are provided in Visual Studio 2005 format, and should compile out of the box. Please note that this is a slightly modified version of UnitTest++, since [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just uploaded a sample project for MockItNow to Google Code. Get it <a href="http://code.google.com/p/mockitnow/downloads/list">here</a>.</p>
<p>This file includes the Examples project, the latest version of MockItNow, and <a href="http://unittest-cpp.sourceforge.net/">UnitTest++</a>. The projects are provided in Visual Studio 2005 format, and should compile out of the box.</p>
<p>Please note that this is a slightly modified version of UnitTest++, since the current release doesn&#8217;t catch std::exception in all cases. I&#8217;m going to work with Charles at some point to integrate these changes back into the main branch of UnitTest++.</p>
<p>I&#8217;ve provided three configurations for the Examples project: Debug, Release and Final. Debug and Release configurations both run the unit tests, but Final has optimizations turned on, and so does not run the tests.</p>
<p>If you&#8217;re just interested in seeing what the the test code looks like when using MockItNow, you can browse the <a href="http://code.google.com/p/mockitnow/source/browse/trunk/Examples/TestExamples.cpp">example file</a>.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rorydriscoll.com/2008/05/20/mockitnow-sample-code-added/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MockItNow: Redirecting Function Calls in C++</title>
		<link>http://www.rorydriscoll.com/2008/05/19/mockitnow-redirecting-function-calls-in-c/</link>
		<comments>http://www.rorydriscoll.com/2008/05/19/mockitnow-redirecting-function-calls-in-c/#comments</comments>
		<pubDate>Tue, 20 May 2008 03:43:23 +0000</pubDate>
		<dc:creator>Rory</dc:creator>
				<category><![CDATA[MockItNow]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Mocking framework]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.rorydriscoll.com/?p=9</guid>
		<description><![CDATA[This is the first in a short series of articles I&#8217;m going to write about the way MockItNow works. I&#8217;m going to start off with something pretty meaty: How does MockItNow intercept the function calls and redirect them? I tried a few ways of intercepting function calls and recording their parameters, and maybe one day [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first in a short series of articles I&#8217;m going to write about the way MockItNow works. I&#8217;m going to start off with something pretty meaty: How does MockItNow intercept the function calls and redirect them?</p>
<p>I tried a few ways of intercepting function calls and recording their parameters, and maybe one day I&#8217;ll explain ways not to do it, but for now, read on if you&#8217;re interested in how it works at the moment.</p>
<p><span id="more-9"></span></p>
<h2>How Does MockItNow Intercept Function Calls and Redirect Them?</h2>
<p>The short answer to that question is:</p>
<blockquote><p>MockItNow uses a compiler flag to hook a call to a naked function at the beginning of every function call, before the prologue. This hook function jumps to a predetermined interceptor function and manipulates the stack pointer to cause the program to return back two levels in the stack when the interceptor function returns.</p></blockquote>
<p>If I&#8217;d read this eight months ago then I&#8217;d be pretty confused right now. I knew roughly what a stack frame is, but special hooks? Prologues? Naked functions? Manipulating the stack pointer? That was all mumbo jumbo to me. If some or all of those are also mumbo jumbo to you, then you might want to read on. I&#8217;m going to explain what these things are, and why I needed to learn about them.</p>
<h2>First Things First</h2>
<p>I think it&#8217;s going to help to start off with an idea of what&#8217;s going on with the program when the mocker is intercepting and redirecting function calls. In the following scenario, we are calling a function <em>Bar</em> from a function <em>Foo</em>. <em>Bar</em> is going to get intercepted by the mocker and redirected to a replay function.</p>
<ol>
<li><em>Foo</em> calls <em>Bar.</em></li>
<li><em>Bar</em> calls compiler hook function <em>_penter.</em></li>
<li><em>_penter </em>locates the replay function address and sets it into a variable <em>jumpAddress.</em></li>
<li><em>_penter </em>moves the stack pointer.</li>
<li><em>_penter</em> jumps to <em>Replay.</em></li>
<li><em>Replay</em> makes a note of the function call and returns</li>
<li>Program flow returns back to <em>Foo</em></li>
</ol>
<p>Below I&#8217;ve written out a simplified view of the assembly code that this scenario would generate. I&#8217;ve interleaved the instructions to illustrate the program flow through the various functions. To help out with identifying which function is which, I&#8217;ve colored them all differently.</p>
<p>The important thing for the MockItNow is that we must never execute the body of <em>Bar</em>, which is highlighted in the second green section. If you&#8217;re smart and like this kind of thing, you might be able to work out why the body of <em>Bar</em> will never be called. If not, don&#8217;t worry &#8211; I&#8217;m about to explain.</p>
<p><img src="http://www.rorydriscoll.com/wp-content/uploads/2008/05/assemblyfull.jpg" alt="Assembly Full" /></p>
<p>There are a few important things to note here:</p>
<p>1. The call to <em>_penter</em> occurs before the prologue of <em>Bar</em> executes.<br />
2. <em>_penter</em> adds four bytes to the stack pointer in its epilogue.<br />
3. Instead of calling <em>ret</em> at the end of the <em>_penter</em>, it uses <em>jmp</em> to jump to <em>Replay</em>.</p>
<p>In the next few sections, I&#8217;m going to explain some details behind why these points are important. I&#8217;m also going to fill in a little bit of background knowledge that I had to learn in order to work this out.</p>
<h2>Stack Frames, Prologues and Epilogues</h2>
<p>If the assembly listing didn&#8217;t put you off, you may be wondering at this point, what is a prologue? Well, as you can see in the assembly above, each function performs the exact same two operations at the start of the function:</p>
<pre>   push ebp
   mov ebp,esp</pre>
<p>This is the prologue! Note that if a function has any local variables then there will be another line in the prologue to allocate space for them, but in this case we don&#8217;t have any, so I&#8217;ll ignore it.</p>
<p>So what&#8217;s going on here? The point of the prologue is set up the current stack frame. The stack frame basically stores all the information about the current function we are in. It is here that your stack variables get stored, along with any other context the program needs to call and pass arguments to other functions, and to restore the previous stack frame.</p>
<p>All the prologue does is to store the previous stack frame location (<em>ebp</em>) by pushing it onto the stack and sets <em>ebp</em> to store the current stack frame location (<em>esp</em>).</p>
<p>You&#8217;ll probably also notice that apart from the <em>_penter</em> function, all the functions do the exact same thing at the end:</p>
<pre>   mov esp,ebp
   pop ebp
   ret</pre>
<p>This is called the epilogue, and it just moves the stack pointer back to where the old base pointer is stored in the stack frame, and then pops the base pointer, which restores it. One important thing to remember here is that when you call pop, it restores whatever is at the current stack pointer location into the register provided, and moves the stack pointer.</p>
<p>I suspect that most programmers aren&#8217;t even aware of prologues and epilogues because the compiler automatically generates a standard prologue and epilogue for each function you write. It even works out the size of all your stack variables so that it can make the stack frame big enough. I certainly never had to deal with it until I started looking into MockItNow.</p>
<p>There is, however, one type of function for which the compiler won&#8217;t auto-generate prologues and epilogues. These functions are called naked functions. You can declare your function as naked using __declspec(naked) before the function name.</p>
<p>You&#8217;ll almost never want to use a naked function unless, of course, you need to customize either the prologue or epilogue. This is exactly what needs to happen in the _penter function in order to divert the function call.</p>
<h2>Manipulating the Instruction Pointer</h2>
<p>There are a couple more things I need to explain before I can go through an example of how MockItNow actually works: I need to explain what happens to the stack when the <em>call</em> and <em>ret</em> instructions are encountered, and also difference between <em>call</em> and <em>jmp</em>.</p>
<p>I&#8217;ll start my explanations with a quick introduction to the instruction pointer register (<em><span style="text-decoration: underline;">eip</span></em>) and how it is changes as a program runs. The instruction pointer stores the address of the instruction that the program is currently executing. Normally when an instruction is executed, the instruction pointer is moved to the address of the next instruction. The <em>call</em>, <em>ret</em> and <em>jmp</em> instructions are all exceptions to this rule though, since their sole purpose is to manipulate instruction pointer in very specific ways.</p>
<p>When you <em>call</em> a function, the instruction pointer moves to the next instruction as usual. The value of the instruction pointer register then gets pushed onto the stack, and the program sets the register to the address of the first instruction in the function you are calling. This means that the next instruction to get executed is the first instruction of the function you have called.</p>
<p>So how does the program know which address to copy into the instruction pointer register when you call a function? For most calls, the answer is easy: The linker tells it! If you take a look at the disassembly for a function call, you&#8217;ll normally see something like this:</p>
<pre>   call Foo (123456h)</pre>
<p>The number in brackets is the address that the instruction pointer is going to jump to when the call instruction is processed. Sometimes you may see the program make a function call using a register like <em>eax</em>. This happens if you are calling a virtual function because the function address wasn&#8217;t known at link time.</p>
<p>Beware that if you have incremental linking turned on then address you see in the <em>call</em> instruction probably isn&#8217;t the address of the actual function, but actually the address inside the incremental link table. This descrepency between the real function address and the call address is why MockItNow doesn&#8217;t work when incremental linking is turned on.</p>
<p>I suspect that you&#8217;ve guessed by now that the reason the <em>call</em> instruction pushed the old instruction pointer register onto the stack is so that the <em>ret</em> instruction can pop it back off. This is exactly what it does to return program flow back to the caller function.</p>
<p>You&#8217;ve probably also guessed that the difference between <em>call</em> and<em> jmp</em> is that <em>jmp</em> doesn&#8217;t push the instruction pointer onto the stack before changing it. We&#8217;re going to exploit this difference in a bit.</p>
<h2>Putting it All Together</h2>
<p>Hopefully everything I outlined above is clear, because now I&#8217;m going to show a simplified view of the state of the stack as the program in the scenario above executes, and we&#8217;ll see why the body of <em>Bar</em> never gets executed.</p>
<p>Remember, the contents of the stack are based on the address of stack pointer, so if you change the stack pointer, you change the contents of the stack. Also, <em>ebp</em> is the base pointer for that function&#8217;s stack frame, and <em>eip</em> is the next instruction to execute within the function.</p>
<p>I&#8217;m going to assume that <em>Foo</em> is called directly from the <em>Test</em> function, so the first thing that gets pushed onto the stack is the <em>Test</em> function return address and base pointer. The stack is represented by the table below each significant event. The top of the stack is the top of the table.</p>
<p><img src="http://www.rorydriscoll.com/wp-content/uploads/2008/05/assembly1.jpg" alt="Assembly 1" /></p>
<p>There&#8217;s nothing out of the ordinary here so far. This is what you&#8217;d expect to see for a function call to <em>Bar</em>. It just pushed the return address for the instruction pointer onto the stack.</p>
<p><img src="http://www.rorydriscoll.com/wp-content/uploads/2008/05/assembly2.jpg" alt="Assembly 2" /></p>
<p>Now things are looking a bit interesting. We have the return address for <em>Bar</em> on top of the return address for <em>Foo</em>. We&#8217;re going to manipulate this in a bit.</p>
<p><img src="http://www.rorydriscoll.com/wp-content/uploads/2008/05/assembly3.jpg" alt="Assembly 3" /></p>
<p>There&#8217;s nothing out of the ordinary here so far for <em>_penter</em>. It has done the standard prologue, and it has done the first two instructions of the standard epliogue.</p>
<p><img src="http://www.rorydriscoll.com/wp-content/uploads/2008/05/assembly4.jpg" alt="Assembly 4" /></p>
<p>Finally, this is the part where the good stuff happens! The stack pointer was incremented by four bytes, which basically removes the return address for <em>Bar</em> without restoring the instruction pointer. There is no trace left in the stack that <em>Bar</em> was ever called! The program now uses the <em>jmp</em> instruction to move program exectution to the <em>Replay</em> function. It uses <em>jmp</em> rather than <em>call</em> so that the instruction pointer register doesn&#8217;t get stored onto the stack.</p>
<p><img src="http://www.rorydriscoll.com/wp-content/uploads/2008/05/assembly5.jpg" alt="Assembly 5" /></p>
<p>This function has the standard prologue and epilogue, but before the <em>ret</em> instruction is executed, I wanted to highlight the fact that the address on the the top of the stack is an address inside Foo.</p>
<p><img src="http://www.rorydriscoll.com/wp-content/uploads/2008/05/assembly6.jpg" alt="Assembly 6" /></p>
<p>We have now jumped up two levels in the stack. I write &#8216;two levels&#8217; because we were never really in <em>Bar</em>. Instead of returning back <em>_penter</em>, or to <em>Bar</em>, we have returned <em>Foo</em>. This is really important since the <em>Replay </em>function may be returning a value, and the next instructions in <em>Foo</em> will retrieve that return value.</p>
<p><img src="http://www.rorydriscoll.com/wp-content/uploads/2008/05/assembly7.jpg" alt="Assembly 7" /></p>
<p>The rest is just some cleanup!</p>
<p>I hope that you can see how manipulating the stack caused the instruction pointer to return back from <em>Replay</em> to <em>Foo</em>. The body of <em>Bar</em> was never executed.</p>
<p>If <em>Bar</em> has a return value, we return a preset value from <em>Replay</em>, and <em>Foo</em> never knows that <em>Bar</em> was intercepted! As long as <em>Replay</em> has the same calling convention as <em>Bar</em>, you don&#8217;t have to worry about how the return values are propagated back.</p>
<h2>Summary</h2>
<p>Looking back to the beginning of the article, I highlighted three important points. I hope you have a better understanding now I highlighted these points in particular.</p>
<p>1. <em>Bar</em> never gets the chance to change the state of the stack. This means we don&#8217;t have to worry about removing the stack frame when we return from <em>_penter</em>.<br />
2. Adding four bytes to the stack pointer removes the return address back into <em>Bar</em> from the stack without changing the instruction pointer. This makes it appear as though <em>_penter</em> was called directly from <em>Foo</em>.<br />
3. Because the <em>call</em> instruction wasn&#8217;t used to enter the <em>Record</em> function from <em>_penter</em>, the instruction pointer was never saved to the stack, so the return address still points back to <em>Foo</em>.</p>
<p>I hope this was informative and perhaps even a little bit interesting. Next time I&#8217;m going to write about some of the the challenges I encountered when working out how to store function arguments and return values.</p>
<h2>References</h2>
<p><a href="http://en.wikipedia.org/wiki/Function_prologue">http://en.wikipedia.org/wiki/Function_prologue</a><br />
<a href="http://msdn.microsoft.com/en-us/library/c63a9b7h.aspx">http://msdn.microsoft.com/en-us/library/c63a9b7h.aspx</a><br />
<a href="http://www.swansontec.com/sregisters.html">http://www.swansontec.com/sregisters.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rorydriscoll.com/2008/05/19/mockitnow-redirecting-function-calls-in-c/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

