<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vijesh Salian</title>
    <description>The latest articles on DEV Community by Vijesh Salian (@vijesh_salian).</description>
    <link>https://dev.to/vijesh_salian</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F55287%2Fa20ce703-b4cd-49fc-8180-45c95e4fde77.jpg</url>
      <title>DEV Community: Vijesh Salian</title>
      <link>https://dev.to/vijesh_salian</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vijesh_salian"/>
    <language>en</language>
    <item>
      <title>Recursion - How to overflow the stack and how not to</title>
      <dc:creator>Vijesh Salian</dc:creator>
      <pubDate>Fri, 19 Jul 2019 07:30:27 +0000</pubDate>
      <link>https://dev.to/vijesh_salian/recursion-how-to-overflow-the-stack-and-how-not-to-a8k</link>
      <guid>https://dev.to/vijesh_salian/recursion-how-to-overflow-the-stack-and-how-not-to-a8k</guid>
      <description>&lt;p&gt;Recursive functions are functions that calls itself. If you were new to recursive function, it may have hurt your brain. Today's blog is in the similar lines. It may help you understand recursion better.&lt;br&gt;
Fun exercise: Try searching 'recursion' on Google.&lt;/p&gt;

&lt;p&gt;We will create a recursive function to find the factorial of a number. A factorial of a number is the product of all the integers before it down until 1. The factorial of 1 is 1. And that is the base case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;rec&lt;/span&gt; &lt;span class="n"&gt;fact&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
 &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
 &lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;fact&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any recursive function written by humans must have base case or a terminating case. Recursive functions have a tendency to get sucked into a black hole which in turn is a recursive sucker. Luckily when we write recursive functions on computers, the call-stack comes to rescue us from the black hole.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;What is the call stack?&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;A call stack is where function calls are stored. When a function is called, the control needs to go into the function. When the function completes executing, the control needs to go back to the function it was called from. The call stack keeps track of the current executing function and the function it needs to go back to when it finishes executing. The data structure the call stack uses for this is, you guessed it, the stack.&lt;/p&gt;

&lt;p&gt;In the title of this blog, I use the word stack. For the purposes of this blog I use stack and call stack interchangeably. A call stack is made up of stack frames. Every stack frame represents a function call. The top-most stack frame represents the latest.&lt;/p&gt;

&lt;p&gt;Call stack of the running factorial function. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zqkah65kpu3e21m9qve.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zqkah65kpu3e21m9qve.png" width="389" height="142"&gt;&lt;/a&gt;&lt;br&gt;
Courtesy: Visual Studio 2017&lt;/p&gt;

&lt;p&gt;The number of frames the stack can hold is finite. When the stack can take no more frames, it is said to be overflowed. That's when you get the infamous stack overflow exception. It is important to learn to break things in order to prevent it from breaking. &lt;/p&gt;

&lt;p&gt;Let's get the stack overflow exception deliberately.&lt;br&gt;
Call the above function with a very large number. Say a hundred thousand.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fact&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that is how you overflow the stack.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Tail Recursion&lt;/b&gt;&lt;br&gt;
Tail recursion is a recursion of a function where it does not consumes stack space and hence prevents stack overflow. If the recursive function is made tail-recursive then it is more efficient than a non-tail-recursive function because every function call does not need to go on stack and pop when the call is done. And it prevents the ugly stack overflow.&lt;/p&gt;

&lt;p&gt;Let's create a recursive function to find the sum of integers from 1 to a given n. That will be a parameter to the function. The base case where the recursion stops is when n is zero. Here is the function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;rec&lt;/span&gt; &lt;span class="n"&gt;sumOfIntegers&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
 &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
 &lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sumOfIntegers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you debug this function, you will notice that every function call consumes a stack space. This function is not tail-recursive. If you notice in the recursive function above, there is a base case and a recursive case. Recursive case is where all the recursion action takes place. The last expression of the recursive case is an addition. When the recursive call is made, it still has to come back after a series of calls to do the addition. If we make the last expression just a recursive call, then we give a chance to the compiler to optimize the recursion so as to not consume stack space. So we can make the above function tail-recursive if we can make the last expression of the recursive case to be just a recursive function call without any other additional evaluation. Here is the tail-recursive version of the sumOfIntegers&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;sumOfIntegersTr&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
 &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;rec&lt;/span&gt; &lt;span class="n"&gt;sumrec&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
   &lt;span class="n"&gt;sumrec&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="n"&gt;sumrec&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function sumOfIntegersTr takes n as an argument. Notice that it is not a recursive function. It defines a nested function sumrec which is recursive. Right after the definition of sumrec, it invokes sumrec.&lt;/p&gt;

&lt;p&gt;The recursive function sumrec, looks to be tail recursive because the last expression in its recursive case is just a function call.&lt;br&gt;
sumrec takes 2 argument. n, the obvious one and sum, which it uses to accumulate the sum of number. The first invocation of sumrec is done by the outer function sumOfIntegersTr. It is invoked with n and 0, 0 is the initial value for sum.This way it does the addition in the argument to the function call. The base case returns the accumulated sum. Pretty neat. Debug and take a look at the call-stack to verify.&lt;/p&gt;

&lt;p&gt;Now, lets make that factorial function tail-recursive. Remember that the last expression of the recursive case must be just a function call. I will also be using an accumulator named factorial to hold the value of the factorial. Just like sum in sumrec.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;factTr&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
 &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;rec&lt;/span&gt; &lt;span class="n"&gt;factRec&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nc"&gt;I&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
   &lt;span class="n"&gt;factRec&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nc"&gt;I&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;*&lt;/span&gt;&lt;span class="n"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="n"&gt;factRec&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nc"&gt;I&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the 'I' suffixed to the the integer? It just means that the number is a Big Integer. Its a type to hold large integers. Go ahead and take a look at the call stack, it should consume only one stack frame for any number of calls.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed reading this. Until next time. Cheers.&lt;/p&gt;

</description>
      <category>fsharp</category>
      <category>recursion</category>
      <category>code</category>
      <category>stack</category>
    </item>
    <item>
      <title>Broken array covariance</title>
      <dc:creator>Vijesh Salian</dc:creator>
      <pubDate>Fri, 28 Jun 2019 15:16:12 +0000</pubDate>
      <link>https://dev.to/vijesh_salian/broken-array-covariance-46p4</link>
      <guid>https://dev.to/vijesh_salian/broken-array-covariance-46p4</guid>
      <description>&lt;p&gt;&lt;i&gt;Array covariance is broken?&lt;/i&gt; Yes it is!&lt;/p&gt;

&lt;p&gt;The code examples I show here applies to C# and it applies to Java as well, because I think Java was the first to make(break?) this. C# unwillingly had to do the same in its language.&lt;/p&gt;

&lt;p&gt;Variance (Covariance, contravariance, invariance) is a hard topic to explain. But I am going to try anyway. &lt;/p&gt;

&lt;p&gt;When designing a language with types, the designers must set certain rules about how types and its related types are substitutable. Any operation dealing with types is subjected to those rules. An operation could be covariant, contravariant or invariant. Note that an operation could not just be a function/method, but could also be an assignment. &lt;/p&gt;

&lt;p&gt;Consider these classes. &lt;code&gt;Small&lt;/code&gt; and &lt;code&gt;Little&lt;/code&gt; are both subclasses of &lt;code&gt;Big&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Big&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Small&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Big&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Little&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Big&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If I can assign a &lt;code&gt;Small&lt;/code&gt; type to a &lt;code&gt;Big&lt;/code&gt; type, then that assignment operation was covariant with those types.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Big&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Small&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Big&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Little&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The converse is not true for the above operation. Contraviance is the converse of covariance. Covariance feels more intuitive than contravariance. The assigment operation is not contravariant with those types. If you try to do this, the compiler should warn you, because the rules of variance are well defined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Small&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Big&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;These are very simplified examples. If you would like me to cover this topic elaborately in another post, please let me know in the comments.&lt;/p&gt;

&lt;p&gt;In this post I am going to talk only about broken array covariance. &lt;/p&gt;

&lt;p&gt;The language designers decided to make arrays covariant. The following code is perfectly compile-able.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Big&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;bigArray&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Small&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above assigment operation is legal, because it obeys the rules of variance. &lt;/p&gt;

&lt;p&gt;Now consider the following code. We are using the &lt;code&gt;bigArray&lt;/code&gt; defined in the previous code. This code also perfectly legal. Because a &lt;code&gt;Small&lt;/code&gt; object is assignable to a &lt;code&gt;Big&lt;/code&gt; type. It satifies the covariance rules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;bigArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Small&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Little&lt;/code&gt; assigned to &lt;code&gt;Big&lt;/code&gt; also follows the covariance rules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;bigArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Little&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here's where things go wrong. The above code is still perfectly valid code. The poor compiler does not know that the runtime disagrees.&lt;/p&gt;

&lt;p&gt;If you run the program, it will end up with an exception trying to assign &lt;code&gt;Little&lt;/code&gt; to &lt;code&gt;Big&lt;/code&gt;. This is why the array covariance is broken. The problem is, even though the type is &lt;code&gt;Big&lt;/code&gt;, the underlying storage is of &lt;code&gt;Small&lt;/code&gt;. &lt;code&gt;Small&lt;/code&gt; and &lt;code&gt;Little&lt;/code&gt; are not substitutable with each other. &lt;/p&gt;

&lt;p&gt;Could the language designers have avoided this problem? May be. Let's create &lt;code&gt;List&lt;/code&gt; instead of array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Big&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bigs&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Small&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above code does not compile. The problem is completely avoided by the compiler here to keep the types safe.&lt;/p&gt;

&lt;p&gt;If you are working with array with related types, before inserting an object into an array, check its type first to avoid runtime errors. Next time when you are reviewing code with arrays, remember array covariance is broken!&lt;/p&gt;

&lt;p&gt;Hope you found this interesting. Thank you for your attention.&lt;/p&gt;

&lt;p&gt;Cheers 🍺&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>java</category>
      <category>array</category>
      <category>covariance</category>
    </item>
    <item>
      <title>.NET Assembly Roundtripping</title>
      <dc:creator>Vijesh Salian</dc:creator>
      <pubDate>Tue, 25 Jun 2019 10:08:03 +0000</pubDate>
      <link>https://dev.to/vijesh_salian/net-assembly-roundtripping-37m1</link>
      <guid>https://dev.to/vijesh_salian/net-assembly-roundtripping-37m1</guid>
      <description>&lt;h3&gt;&lt;i&gt;What the heck is Roundtripping?&lt;/i&gt;&lt;/h3&gt;

&lt;p&gt;
Assembly roundtripping is the act of decompiling an assembly to its IL( Intermediate Language) to edit it and then recompiling it to generate the assembly back.
&lt;/p&gt;

&lt;p&gt;In this post I am going to demonstrate how to roundtrip a .NET assembly.&lt;br&gt;
To roundtrip, we need to &lt;br&gt;
&lt;b&gt;&lt;/b&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Decompile to IL&lt;/li&gt;
&lt;li&gt;Edit IL&lt;/li&gt;
&lt;li&gt;Recompile to assembly
&lt;/li&gt;
&lt;/ol&gt;


&lt;h5&gt;Tools required&lt;/h5&gt;
&lt;ul&gt;
&lt;li&gt;ildasm&lt;/li&gt;
&lt;li&gt;Visual Studio Developer Command Prompt&lt;/li&gt;
&lt;li&gt;csc.exe - The C# compiler&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Here is the C# file we are going to use for our demo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The answer is {0}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I am going to name this file as roundtrip.cs.&lt;/p&gt;

&lt;p&gt;In the VS developer command prompt, navigate to the location of the file roundtrip.cs. Now, I am going to compile this cs file into an assembly, an exe in this case. Here is the command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;csc.exe roundtrip.cs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There is an assembly created. roundtrip.exe. If you run this program you should get the following output. Nothing out of normal here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The answer is 30
Hello, World!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;Decompile to IL&lt;/h5&gt;

&lt;p&gt;Let's create an IL file for roundtrip.exe using ildasm with the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ildasm /out=roundtrip.il roundtrip.exe
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will create a file roundtrip.il with IL code for roundtrip.exe.&lt;/p&gt;

&lt;p&gt;Open this roundtrip.il in any editor. You will find all the IL code in the file. The main method in our C# file has a corresponding section in the IL. Look for the following line the IL file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.method public hidebysig static void  Main() cil managed
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this section you should find a set of instructions that correspond to declaring and initializing the integers with 10, 20 respectively. There is a string that we are using. You will find that too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.locals init (int32 V_0,
             int32 V_1)
    IL_0000:  nop
    IL_0001:  ldc.i4.s   10
    IL_0003:  stloc.0
    IL_0004:  ldc.i4.s   20
    IL_0006:  stloc.1
    IL_0007:  ldstr      "The answer is {0}"
    IL_000c:  ldloc.0
    IL_000d:  ldloc.1
    IL_000e:  add
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;Edit IL&lt;/h5&gt;

&lt;p&gt;Now for the edit part, let change the add operation to a multiplication. Replace add to mul&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IL_000e:  mul
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There is a Hello World string, let's change that string to Hello Dev.&lt;/p&gt;

&lt;p&gt;Before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IL_001a:  ldstr      "Hello, World!"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IL_001a:  ldstr      "Hello, Dev!"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Make sure to save the IL file.&lt;/p&gt;

&lt;h5&gt;Recompile to assembly&lt;/h5&gt;

&lt;p&gt;Now is the time to recompile. In the VS dev prompt, type the following command. Note that this time we use ILASM and not ILDASM. We pass the edited IL file to the command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ilasm /exe roundtrip.il
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If there are no mistakes made in the IL, then you should find the new roundtrip.exe. When you run it you should see the following output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The answer is 200
Hello, Dev!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Hope you found this interesting. Thank you for your attention.&lt;/p&gt;

&lt;p&gt;Cheers 🍺&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>assembly</category>
      <category>csharp</category>
      <category>ildasm</category>
    </item>
    <item>
      <title>Starting with Rust - Cargo</title>
      <dc:creator>Vijesh Salian</dc:creator>
      <pubDate>Wed, 05 Jun 2019 11:46:38 +0000</pubDate>
      <link>https://dev.to/vijesh_salian/starting-with-rust-cargo-cdj</link>
      <guid>https://dev.to/vijesh_salian/starting-with-rust-cargo-cdj</guid>
      <description>&lt;p&gt;In the last post, we saw how to compile a rust file ( .rs file) with the rust compiler. Here's that post in case you are interested.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/vijesh_salian" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gnDVCaAl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--TJ0-iPc4--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/55287/a20ce703-b4cd-49fc-8180-45c95e4fde77.jpg" alt="vijesh_salian image"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/vijesh_salian/starting-with-rust-2713" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Starting with Rust&lt;/h2&gt;
      &lt;h3&gt;Vijesh Salian ・ Jun  4 '19 ・ 3 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#rust&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#learning&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;In real world, applications that programmer writes can be made of several files. These files are grouped together in what we call a project. It is very common to have multiple projects in an application. In most cases, more than one developer is involved in building the application. Each project has a clear responsibility. Rust files serving a similar responsibility are generally in the same project. It is common that one Rust file could be dependent on another. It is also common that one Rust project could depend on another. If project X is dependent on project Y. Then Y is a dependency of X. A Rust project coud have multiple dependencies and it should list them and know them. The dependency however will not and should not know about the projects depending on it.  Sometimes a project may have a dependency which was written by developers outside your team/company. They can make that dependency available in a central repository so that you can download and use them.&lt;/p&gt;

&lt;p&gt;So, when working with Rust you will be compiling, downloading dependencies, building a lot. This is where Cargo comes in to picture. If you have been programming in other languages, all this may seem obvious and you possibly already know what Cargo can do. &lt;/p&gt;

&lt;p&gt;&lt;i&gt;How do I use Cargo?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;First lets check Cargo is installed correctly. Fire up your shell.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cargo --version
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you see a version number, you are good to go. If not, search engine is your friend.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cargo new hello_rust
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is a command to create a new project. We created a new project called hello_rust. This creates a directory with the project name. If you look into the directory you will find that Cargo initialized it with a git repo. You can choose not to do so if you want. Then you will find a Cargo.toml file. This file is the configuration file for your project. In the project directory you will also find another directory called src. This contains the Rust code files. &lt;/p&gt;

&lt;p&gt;Cargo.toml file contains metadata about the project and a list of dependencies if any.&lt;/p&gt;

&lt;p&gt;The src directory contains a Rust file which you are familiar with. &lt;/p&gt;

&lt;p&gt;&lt;i&gt;Okay, what else can I do with Cargo?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;Well, we can build and run our code with Cargo. Cargo knows how to make use of the rustc compiler.&lt;/p&gt;

&lt;p&gt;In your shell, be at your project directory and run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cargo run
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This commands builds and runs your projects with just one command.&lt;br&gt;
This may look trivial now, but as your projects grow bigger, the benefit of Cargo becomes more visible. By this, Cargo provides a uniform way of building and managing dependencies, for all projects big and small.&lt;/p&gt;

&lt;p&gt;If you want to quickly check if your code compiles, but don't want to run it, then use this command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cargo check
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you want to compile and build an executable, but not run it, then use this command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cargo build
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you want to ship your program, use this command to build with release mode to create a shippable program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cargo build --release
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;i&gt;Nice, so what else can I do with Cargo?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;If you want to know more about cargo, you will find the Cargo book extensive.&lt;br&gt;
Go here for the Cargo book. &lt;a href="https://doc.rust-lang.org/cargo/index.html"&gt;https://doc.rust-lang.org/cargo/index.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for your attention.&lt;br&gt;
Cheers 🍺&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Starting with Rust</title>
      <dc:creator>Vijesh Salian</dc:creator>
      <pubDate>Tue, 04 Jun 2019 15:37:36 +0000</pubDate>
      <link>https://dev.to/vijesh_salian/starting-with-rust-2713</link>
      <guid>https://dev.to/vijesh_salian/starting-with-rust-2713</guid>
      <description>&lt;p&gt;Programming has been my profession for over 10 years now, but I am proficient in only one language. Even though I may use other languages, I cannot call myself proficient in any other than my primary language. That does not make me very comfortable. Learning a different language gives you a much needed different perspective about programming. So I decided to learn other languages. There is a plethora of languages available. I decided to pick 2 languages that bring a slight counter-intuitiveness to my conditioned object-oriented-thinking mind. I picked F# and Rust. I am no language design enthusiast, so I won't be talking about what are the languages' good and bad parts. As a programmer I would like to know the language well enough to be able to build reliable systems. An expertise in the language is required to avoid known pitfalls. In this post, and in a following few, I will be writing about Rust. This post will cover introduction and installation of Rust.&lt;/p&gt;

&lt;p&gt;Rust began its journey from within Mozilla. &lt;a href="https://research.mozilla.org/rust/"&gt;https://research.mozilla.org/rust/&lt;/a&gt;. It is an open sourced programming language and is still sponsored by Mozilla. &lt;a href="https://github.com/rust-lang/rust"&gt;https://github.com/rust-lang/rust&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Rust is known to be a systems programming language. It is also a general purpose programming language from what I see. &lt;i&gt;So what can you build with Rust?&lt;/i&gt; Well, you can build web applications, distributed applications, embedded applications and many more. You can even build your own operating system if you want to. &lt;i&gt;What is so special about Rust?&lt;/i&gt; Rust was built from scratch as a systems programming language as an alternative to C and C++, but without the known pitfalls of those languages, especially around memory management. It is a modern language that addresses so many of the issues which makes building systems highly resilient which was really hard to achieve with other programming languages. It is said that if you write code that compiles in Rust, you would have written an efficient code without having to do micro-optimizations.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;How do you I get started with Rust&lt;/i&gt; &lt;br&gt;
Install Rust on your machine from here &lt;a href="https://www.rust-lang.org/tools/install"&gt;https://www.rust-lang.org/tools/install&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you are on Windows make sure to add Rust path in your PATH system variable.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rustup&lt;/code&gt; is a command line tool that manages Rust versions and other associated tools required for development.&lt;/p&gt;

&lt;p&gt;Update your Rust installation by running the following in your shell.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rustup update
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Run this to check the Rust version. If it shows a version then it has installed correctly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rustc --version
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note the -- before the word version. That is important; its an argument for the command. The first time I tried the command without -- and got this weird error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error: couldn't read version: The system cannot find the file specified. (os error 2)

error: aborting due to previous error
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So, if you see that error, then check the syntax once again, you may be missing --. Also note that I am on Windows 10. Depending on your operating system you may experience it differently.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Okay, now show me the code!&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;Let's do an obligatory Hello World program.&lt;/p&gt;

&lt;p&gt;Create a directory where you would like this program to reside.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir HelloRust
cd HelloRust
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Create a file called main.rs. Type the following code in and save the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello Rust"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Go to the shell and run the command to compile that rust code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rustc main.rs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And then run the main executable. On Windows it's the main.exe file. You should see Hello Rust.&lt;/p&gt;

&lt;p&gt;If you look at the code you will see that it defines a main function and prints a string. If you have been coding before, this syntax is obvious. There may be some things that may not seem familiar like the ! after the println. Well that is something we may need to cover at a later point in time. This post is not meant for that.&lt;/p&gt;

&lt;p&gt;If you would like to learn about Rust, the official documentation is fantastic. The official Rust website has a whole well-written book for free. You can find it here. &lt;a href="https://doc.rust-lang.org/book/title-page.html"&gt;https://doc.rust-lang.org/book/title-page.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you would like to know about my experiments with Rust, then follow this space. Thank your for your attention. &lt;/p&gt;

&lt;p&gt;Cheers! 🍺&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
