<?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>Algorithm.co.il &#187; Testing</title>
	<atom:link href="http://www.algorithm.co.il/blogs/category/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.algorithm.co.il/blogs</link>
	<description>Algorithms, for the heck of it</description>
	<lastBuildDate>Tue, 21 Jun 2011 20:37:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Fuzz-Testing With Nose</title>
		<link>http://www.algorithm.co.il/blogs/programming/python/fuzz-testing-with-nose/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/python/fuzz-testing-with-nose/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 07:49:01 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[fuzzing]]></category>
		<category><![CDATA[nose]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=587</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python/fuzz-testing-with-nose/' addthis:title='Fuzz-Testing With Nose'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>A few days ago, I found a in my website, plnnr.com. The bug was in a new feature I added to the algorithm. The first thing I did was write a small unit-test to reproduce the bug. With that unit-test &#8230; <a href="http://www.algorithm.co.il/blogs/programming/python/fuzz-testing-with-nose/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python/fuzz-testing-with-nose/' addthis:title='Fuzz-Testing With Nose' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>A few days ago, I found a in my website, <a href="http://plnnr.com">plnnr.com</a>. The bug was in a new feature I added to the algorithm. The first thing I did was write a small unit-test to reproduce the bug. With that unit-test in hand, I then worked to fix the bug, and got this unit-test to pass.</p>
<p>As I previously persumed this feature to be (relatively :) bug free, I decided that more testing was in order. This time however, a single test-case would not be enough &#8211; I needed to make sure that the trip-generation algorithm works in many cases. Enter <a href="http://en.wikipedia.org/wiki/Fuzz_testing">fuzzing</a>.</p>
<p>Plnnr.com generates trips according to trip preferences. Why not generate the trip preferences with a fuzzer, and then check if the planning algorithm chokes on them? While fuzzing is usually used to generate invalid input with the goal of causing the program to crash, in this case I&#8217;m generating valid input with the goal of causing the planning algorithm to fail.</p>
<p>Usually fuzzing is done with one of two techniques &#8211; exhaustive fuzzing, that goes systematically (possibly selectively) over the input space and random fuzzing, which picks inputs at random &#8211; or &#8220;somewhat&#8221; randomly. In my case, the input space consists of &#8220;world data&#8221; &#8211; locations of attractions, restaurants, etc, and trip preferences &#8211; intensity, required attractions, and so on. Since the input space is so large and &#8220;unstructured&#8221;, I found it much easier to go with random fuzzing.</p>
<p>In each test-case, I will generate a &#8220;random world&#8221;, and random trip preferences for that world.<br />
Here is some sample code that shows how this might look:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">trip_prefs.<span style="color: black;">num_days</span> = <span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
trip_prefs.<span style="color: black;">intensity</span> = <span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> randbit<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    trip_prefs.<span style="color: black;">schedule_lunch</span> = <span style="color: #008000;">True</span></pre></div></div>

<p>Where randbit is defined like so:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> randbit<span style="color: black;">&#40;</span>prob = <span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">random</span>.<span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span>  prob</pre></div></div>

<p>This is all very well, but tests need to be reproducible. If a fuzzer-generated test case fails and I can&#8217;t recreate it to analyze the error and later verify that it is fixed, it isn&#8217;t of much use. To solve this issue, the input generation function receives some value, and sets the random seed with this parameter. Now, generating test cases is just a matter of generating a sequence of random values. Here is my code to do that:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> FuzzTestBase<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    __test__ = <span style="color: #008000;">False</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> run_single_fuzz<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, random_seed<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">pass</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> fuzz_test<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">random</span>.<span style="color: black;">seed</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        random_seeds = <span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">random</span>.<span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>NUM_FUZZ_TESTS<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> seed <span style="color: #ff7700;font-weight:bold;">in</span> random_seeds:
            <span style="color: #ff7700;font-weight:bold;">yield</span> <span style="color: #008000;">self</span>.<span style="color: black;">run_single_fuzz</span>, seed</pre></div></div>

<p>FuzzTestBase is a base-class for actual test classes. Each test class just needs to define its own version of run_single_fuzz, and in it call random.seed(random_seed) and log random_seed.</p>
<p>This code uses <a href="http://somethingaboutorange.com/mrl/projects/nose/0.11.3/">nose</a>&#8216;s ability to test generators: it assumes that a test generator yields test functions and their parameters.</p>
<p>A few interesting issues:<br />
* I generate the random seeds beforehand, so that calling random.seed() in the actual test case doesn&#8217;t affect the seed sequence.<br />
* Originally I used just random.random() as a seed instead of str(random.random()). The problem with that is that this way it&#8217;s not reproducible. random.random() returns a floating point value x, for which usually x != eval(str(x)):</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">In <span style="color: black;">&#91;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#93;</span>: x = <span style="color: #dc143c;">random</span>.<span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">11</span><span style="color: black;">&#93;</span>: x == <span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
Out<span style="color: black;">&#91;</span><span style="color: #ff4500;">11</span><span style="color: black;">&#93;</span>: <span style="color: #008000;">False</span></pre></div></div>

<p>Even though x == eval(repr(x)) for that case, there&#8217;s still room for error. Unlike floating point numbers, it&#8217;s harder to go wrong with string equality. So str(random.random()) is just a cheap way to generate random strings.</p>
<p>I&#8217;d recommend that if your testing mostly consists of selected test cases based on what you think is possible user behavior, you might want to add some fuzzed inputs. I originally started the fuzz-testing described in this blog-post to better test for a specific bug. After adding the fuzz-testing, I found another bug I didn&#8217;t know was there. This just goes to show how useful fuzzing is as a testing tool. The fact that it&#8217;s so easy to implement is just a bonus.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/python/fuzz-testing-with-nose/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>My Bad Memory, High Load, and Python</title>
		<link>http://www.algorithm.co.il/blogs/personal/my-bad-memory-high-load-and-python/</link>
		<comments>http://www.algorithm.co.il/blogs/personal/my-bad-memory-high-load-and-python/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 09:57:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Computer trouble]]></category>
		<category><![CDATA[High Load]]></category>
		<category><![CDATA[memtest]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Voodoo]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=109</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/personal/my-bad-memory-high-load-and-python/' addthis:title='My Bad Memory, High Load, and Python'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>About a month ago the new Ubuntu 8.04 was released and I wanted a clean install. I downloaded an image and burned it to a CD. Just before installing, I tried &#8220;check CD for defects&#8221; and found a few. Turns &#8230; <a href="http://www.algorithm.co.il/blogs/personal/my-bad-memory-high-load-and-python/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/personal/my-bad-memory-high-load-and-python/' addthis:title='My Bad Memory, High Load, and Python' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>About a month ago the new Ubuntu 8.04 was released and I wanted a clean install. I downloaded an image and burned it to a CD. Just before installing, I tried &#8220;check CD for defects&#8221; and found a few. Turns out (*) this was because of bad memory &#8211; and <a href="http://www.memtest.org/">memtest</a> confirmed it.<br />
So I went to the shop, replaced the bad memory, and also bought two new sticks. I went home to install the new Ubuntu, and after the installation, Firefox crashed. After rebooting back to memtest, I saw this:</p>
<p><a href="http://www.algorithm.co.il/sitecode/bad_memtest.jpg"><img src="http://www.algorithm.co.il/sitecode/bad_memtest.jpg" alt="memory errors in memtest" border="2" width="200"/></a></p>
<p>Back at the computer shop, they asked me to reproduce the errors. Just firing up the computer and booting directly into memtest didn&#8217;t seem to do the trick, so I suspected that I had to overwork my computer a bit to reproduce this.</p>
<p>Since I was at the lab, I didn&#8217;t want to muck around too much.<br />
So I thought, &#8220;what&#8217;s the quickest way to give your CPU a run around the block?&#8221;<br />
That&#8217;s right &#8211; a tight loop:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
    <span style="color: #ff7700;font-weight:bold;">pass</span></pre></div></div>

<p>However, this snippet doesn&#8217;t really play with the memory.</p>
<p>The next simplest thing to do, that also jiggles some ram is the following (and one of my favorites):</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">In <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>: x = <span style="color: #ff4500;">4</span><span style="color: #66cc66;">**</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>: y = <span style="color: #ff4500;">4</span><span style="color: #66cc66;">**</span>x</pre></div></div>

<p>I will talk about this peculiar piece of code at a later post.</p>
<p>In any case, this snippet also didn&#8217;t reproduce the error. It is also quite unwieldy, as it raises a MemoryError after some time. Later at home I tried two more scripts.<br />
The first is a variation on the one above:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">x = <span style="color: #ff4500;">4</span><span style="color: #66cc66;">**</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        y = <span style="color: #ff4500;">4</span><span style="color: #66cc66;">**</span>x
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">MemoryError</span>:
        <span style="color: #ff7700;font-weight:bold;">pass</span></pre></div></div>

<p>I ran a few of those in parallel. However, my Ubuntu machine actually killed the processes running this one by one.</p>
<p>The second is smarter. It allocates some memory and then just copies it around:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">copy</span>
megabytes = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
x1 = <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;a&quot;</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">1000</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1000</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>megabytes<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
    x2 = <span style="color: #dc143c;">copy</span>.<span style="color: black;">deepcopy</span><span style="color: black;">&#40;</span>x1<span style="color: black;">&#41;</span></pre></div></div>

<p>After both of these scripts didn&#8217;t reproduce the problem and it still persisted arbitrarily, I returned the computer to the lab. Turns out that the two replacement sticks and the two new sticks weren&#8217;t exactly identical, and that was the cause of the problem. So now my memory is well again.</p>
<p>As for the scripts above, I once wrote a similar script at work. I was asked to help with testing some software in some stress testing. The goal was to simulate a heavily used computer. A few lines of Python later and the testing environment was ready.</p>
<p>Footnotes:<br />
(*) &#8211; Finding out that it was a memory issue wasn&#8217;t as easy as it sounds. I didn&#8217;t think of running memtest. I checked the image on my HD with md5, and the hash didn&#8217;t match. I downloaded a second image, and again the hash didn&#8217;t match. I checked twice.<br />
At this point I was really surprised: not only the second check didn&#8217;t match the published md5, it also didn&#8217;t match the first check. Some hours and plenty of voodoo later, a friend suggested running memtest, and the culprit was found.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/personal/my-bad-memory-high-load-and-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Some Assembly Required No. 1</title>
		<link>http://www.algorithm.co.il/blogs/challenges/some-assembly-required-no-1/</link>
		<comments>http://www.algorithm.co.il/blogs/challenges/some-assembly-required-no-1/#comments</comments>
		<pubDate>Sat, 12 Apr 2008 20:17:05 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Challenges]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[vial]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/index.php/programming/some-assembly-required-no-1/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/challenges/some-assembly-required-no-1/' addthis:title='Some Assembly Required No. 1'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>I&#8217;ve been working on some of the instruction tests in vial, and I wanted to test the implementation of LOOP variants. My objective was to make sure the vial version is identical to the real CPU version (as discussed here). &#8230; <a href="http://www.algorithm.co.il/blogs/challenges/some-assembly-required-no-1/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/challenges/some-assembly-required-no-1/' addthis:title='Some Assembly Required No. 1' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on some of the instruction tests in vial, and I wanted to test the implementation of LOOP variants. My objective was to make sure the vial version is identical to the real CPU version (<a href="http://www.algorithm.co.il/blogs/index.php/programming/issues-in-writing-a-vm-part-1/">as discussed here</a>). To achieve this, I had to cover all of the essential behaviors of LOOP.</p>
<p>Well, using the framework <a href="http://www.ragestorm.net/blogs/?p=58">Gil and I wrote</a>, I hacked up some code that should cover the relevant cases:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">code_template = <span style="color: #483d8b;">&quot;&quot;&quot;
mov edx, ecx ; control the start zf
mov ecx, eax ; number of iterations
mov eax, 0 ; will hold the result, also an iteration counter
loop_start:
&nbsp;
    cmp eax, ebx    ; check if we need to change zf
    setz dh
    xor dh, dl      ; if required, invert zf
    inc eax         ; count the iteration
    cmp dh, 0       ; set zf
&nbsp;
    loop%s loop_start
&quot;&quot;&quot;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> loop_kind <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">''</span>,<span style="color: #483d8b;">'z'</span>,<span style="color: #483d8b;">'nz'</span><span style="color: black;">&#93;</span>:
    code_text = code_template <span style="color: #66cc66;">%</span> loop_kind
    c = FuncObject<span style="color: black;">&#40;</span>code_text<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> start_zf_value <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> num_iters <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">4</span>,<span style="color: #ff4500;">10</span><span style="color: black;">&#93;</span>:
            <span style="color: #ff7700;font-weight:bold;">for</span> when_zf_changes <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">15</span><span style="color: black;">&#93;</span>:
                c<span style="color: black;">&#40;</span>num_iters, when_zf_changes, start_zf_value<span style="color: black;">&#41;</span>
                c.<span style="color: black;">check</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Note that c(&#8230;) executes the code both on vial&#8217;s VM, and on the real cpu. c.check() compares their return value (EAX) and flags after the execution. I also wanted to avoid other kinds of jumps in this test.</p>
<p>To check that the code ran the same number of times, I returned EAX as the number of iterations.<br />
All the games with edx are there to make sure that I&#8217;m testing different zf conditions.</p>
<p><strong>The challenge for today:</strong><br />
Can you write a shorter assembly snippet that tests the same thing?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/challenges/some-assembly-required-no-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Issues in writing a VM &#8211; Part 1</title>
		<link>http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-1/</link>
		<comments>http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-1/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 21:03:20 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Assembly]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Compilation]]></category>
		<category><![CDATA[Disassembly]]></category>
		<category><![CDATA[Distorm]]></category>
		<category><![CDATA[fuzzing]]></category>
		<category><![CDATA[vial]]></category>
		<category><![CDATA[VM]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/index.php/programming/issues-in-writing-a-vm-part-1/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-1/' addthis:title='Issues in writing a VM &#8211; Part 1'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Arkon and I decided to write a VM for vial. First though, a short explanation on what is vial: vial is a project aimed at writing a general disassembler that outputs expression trees instead of text. On top of vial, &#8230; <a href="http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-1/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-1/' addthis:title='Issues in writing a VM &#8211; Part 1' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Arkon and I decided to write a VM for vial. First though, a short explanation on what is vial:<br />
vial is a project aimed at writing a general disassembler that outputs expression trees instead of text. On top of vial, we intend to write various code-analysis tools. The expression trees in the output should be an accurate description of the all of the code&#8217;s actions.<br />
(note: the x86 disassembler behind vial is Arkon&#8217;s <a href="http://www.ragestorm.net/distorm/">diStorm</a>.)</p>
<p>So why do we need a VM? Apart from it being &#8216;nice and all&#8217;, it is <strong>critical</strong> for testing.</p>
<p>Some time ago, <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/writing-a-quad-interpreter/">I described</a> writing a VM <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/manually-fuzzing-my-own-compiler/">to test</a> a compiler I wrote as university homework. It is a similar issue here.<br />
The disassembler is written according to the x86 specification. If we just check its output against this specification, we are not doing much to verify the code&#8217;s correctness. This is evident when you try to implement such a testing module &#8211; you end up writing another disassembler, and testing it against the original one. There has to be a different test method, one that does not directly rely on the specification.</p>
<p>Enter the VM. If you write a program, you can disassemble it, and then try to execute the disassembly. If it yields the same output as the original program &#8211; your test passed.<br />
This is a good testing method, because it can be easily automated, reach good code coverage, and it tests against known values.<br />
Consider the following illustration:</p>
<p><img src="http://www.algorithm.co.il/sitecode/vm_explanation.png" alt="Testing Process" /></p>
<p>We are testing here a complete process on the left hand, against a known valid value, the original program&#8217;s output, on the right hand. All of the boxes on the left hand are tested along the way. Of course, one test may miss. For example, both the VM and the disassembler may generate wrong output for register overflows. We can try to cover as many such cases as possible by writing good tests for this testing framework. In this case, good tests are either c programs, or binary programs. This is essentially what I was doing when I <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/manually-fuzzing-my-own-compiler/">manually fuzzed my own compiler</a>.</p>
<p>Once the VM is finished, we can start writing various optimizations for the disassembler&#8217;s generated output. We can test these optimizations by checking the VM&#8217;s output on the optimized code against the output on the original code. This makes the VM a critical milestone on the road ahead.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

