<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://sdasgup3.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://sdasgup3.github.io/" rel="alternate" type="text/html" /><updated>2025-03-28T00:58:04+00:00</updated><id>https://sdasgup3.github.io/feed.xml</id><title type="html">About - Sandeep Dasgupta</title><subtitle>Research Scientist at UIUC</subtitle><author><name>Sandeep Dasgupta</name><email>sandigame.123@gmail.com</email></author><entry><title type="html">My Reading List</title><link href="https://sdasgup3.github.io/Reading_List/" rel="alternate" type="text/html" title="My Reading List" /><published>2021-04-01T00:00:00+00:00</published><updated>2021-04-01T00:00:00+00:00</updated><id>https://sdasgup3.github.io/Reading_List</id><content type="html" xml:base="https://sdasgup3.github.io/Reading_List/"><![CDATA[<p>My reading-list organised based on various topics.</p>
<table class="tg">
<thead>
  <tr>
    <th class="tg-9ydz">Topics</th>
    <th class="tg-9ydz">Video</th>
    <th class="tg-9ydz">Papers</th>
    <th class="tg-9ydz">Blogs</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td class="tg-jbrg" rowspan="2">Symbolic execution</td>
    <td class="tg-de2y"><a href="https://www.youtube.com/watch?v=QrtGOrSrVPQ" target="_blank" rel="noopener noreferrer">Dynamic Symbolic Execution</a></td>
    <td class="tg-de2y">X</td>
    <td class="tg-de2y">X</td>
  </tr>
  <tr>
    <td class="tg-de2y">X</td>
    <td class="tg-g4tm">X</td>
    <td class="tg-aokl">X</td>
  </tr>
  <tr>
    <td class="tg-z3tv" rowspan="2">Dataflow Analysis</td>
    <td class="tg-0lax"><a href="https://www.youtube.com/watch?v=OROXJ9-wUQE" target="_blank" rel="noopener noreferrer">Dataflow Analysis</a></td>
    <td class="tg-0lax">X</td>
    <td class="tg-0lax">X</td>
  </tr>
  <tr>
    <td class="tg-0lax">X</td>
    <td class="tg-0lax">X</td>
    <td class="tg-0lax">X</td>
  </tr>
  <tr>
    <td class="tg-z3tv" rowspan="2">Instruction Scheduling</td>
    <td class="tg-0lax">X</td>
    <td class="tg-0lax"><a href="https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.57.8084&rep=rep1&type=pdf" target="_blank" rel="noopener noreferrer">Swing Modulo Scheduling: A Lifetime-Sensitive Approach</a></td>
    <td class="tg-0lax">X</td>
  </tr>
  <tr>
    <td class="tg-0lax">X</td>
    <td class="tg-0lax"><a href="https://llvm.org/pubs/2005-06-17-LattnerMSThesis.pdf" target="_blank" rel="noopener noreferrer">Thesis: AN IMPLEMENTATION OF SWING MODULO SCHEDULING WITH EXTENSIONS FOR SUPERBLOCKS</a></td>
    <td class="tg-0lax">X</td>
  </tr>
</tbody>
</table>]]></content><author><name>Sandeep Dasgupta</name><email>sandigame.123@gmail.com</email></author><category term="LLVM" /><category term="Compilers" /><category term="Static/Dymanic/Symbolic Execution" /><summary type="html"><![CDATA[My reading-list organised based on various topics.]]></summary></entry><entry><title type="html">X86 FAQs &amp;amp; Binary Analysis Tools</title><link href="https://sdasgup3.github.io/All_About_Binary/" rel="alternate" type="text/html" title="X86 FAQs &amp;amp; Binary Analysis Tools" /><published>2018-05-14T00:00:00+00:00</published><updated>2018-05-14T00:00:00+00:00</updated><id>https://sdasgup3.github.io/All_About_Binary</id><content type="html" xml:base="https://sdasgup3.github.io/All_About_Binary/"><![CDATA[<p>X86 FAQs &amp; Binary Analysis Tools.</p>
<h1 id="tools">Tools</h1>
<h3 id="objdump">objdump</h3>
<pre><code>objdump -d -M=x86-64,att --no-show-raw-insn ./a.out
</code></pre>
<h3 id="calling-c-from-assembly">Calling C from Assembly</h3>
<pre><code>main:
  pushq   %rax
  movq $-1, %rdi
  movq $-1, %rax
  movl    $65, %edi
  callq   putchar
  xorl    %eax, %eax
  popq    %rcx
  retq
// as test.s -o test.o
// gcc test.o
</code></pre>
<h3 id="some-useful-gcc-options">Some useful gcc options</h3>
<pre><code>// Command used by compiler explorer V0.1
gcc test.c -02 -c -S -o - -masm=att | c++filt | grep -vE '\s+\.'

-fno-asynchronous-unwind-tables: disable CFI directives on gas assembler output


-march=haswell // Targetting ISA
</code></pre>
<h1 id="articles">Articles</h1>
<h2 id="ida">Ida</h2>
<ul>
<li><a href="https://www.usualsuspect.re/article/ida-tricks-handling-dynamic-imports">IDA Tricks - Handling dynamic imports</a></li>
<li><a href="https://leanpub.com/IDAPython-Book">Beginner's Guide</a></li>
</ul>
<h2 id="tutorial-on-x86-assembly-programming-syntaxsemantics">Tutorial on x86 assembly programming (Syntax/Semantics)</h2>
<ul>
<li><a href="https://www.nayuki.io/page/a-fundamental-introduction-to-x86-assembly-programming#5-memory-addressing-reading-writing">A fundamental introduction</a></li>
<li><a href="https://www3.nd.edu/~dthain/courses/cse40243/fall2015/intel-intro.html">Introduction to X86-64 Assembly for Compiler Writers</a></li>
<li><a href="https://docs.oracle.com/cd/E26502_01/html/E28388/toc.html">x86 Assembly Language Reference Manual
</a></li>
<li><a href="https://software.intel.com/en-us/articles/introduction-to-x64-assembly">Introduction to x64 Assembly</a></li>
</ul>
<h2 id="calling-conventions">Calling Conventions</h2>
<ul>
<li><a href="https://en.wikibooks.org/wiki/X86_Disassembly/Floating_Point_Numbers">x86 Disassembly/Floating Point Numbers</a></li>
</ul>
<h2 id="adressing-modes">Adressing modes</h2>
<p>X86-64 is a complex instruction set (CISC), so the MOV instruction has many different variants that move different types of data between different cells.</p>
<p>MOV, like most instructions, has a single letter suffix that determines the amount of data to be moved. The following names are used to describe data values of various sizes:</p>
<table>
<thead>
<tr>
<th align="center">Suffix</th>
<th align="center">Name</th>
<th align="center">Size</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">B</td>
<td align="center">BYTE</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">W</td>
<td align="center">WORD</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">L</td>
<td align="center">LONG</td>
<td align="center">4</td>
</tr>
<tr>
<td align="center">Q</td>
<td align="center">QUADWORD</td>
<td align="center">8</td>
</tr>
</tbody>
</table>
<p>It is possible to leave off the suffix, and the assembler will attempt to choose the right size based on the arguments. However, this is not recommended, as it can have unexpected effects.</p>
<p>The arguments to MOV can have one of several addressing modes. Here is an example of using each kind of addressing mode to load a 64-bit value into %rax:</p>
<table>
<thead>
<tr>
<th align="center">Mode</th>
<th align="center">Example</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">Global Symbol</td>
<td align="center">MOVQ x, %rax</td>
</tr>
<tr>
<td align="center">Immediate</td>
<td align="center">MOVQ $56, %rax</td>
</tr>
<tr>
<td align="center">Register</td>
<td align="center">MOVQ %rbx, %rax</td>
</tr>
<tr>
<td align="center">Indirect</td>
<td align="center">MOVQ (%rsp), %rax</td>
</tr>
<tr>
<td align="center">Base-Relative</td>
<td align="center">MOVQ -8(%rbp), %rax</td>
</tr>
<tr>
<td align="center">Offset-Scaled-Base-Relative</td>
<td align="center">MOVQ -16(%rbx,%rcx,8), %rax</td>
</tr>
</tbody>
</table>
<p><code>-16(%rbx,%rcx,8)</code> refers to the value at the address <code>-16+%rbx+%rcx*8</code></p>
<p>For the most part, the same addressing modes may be used to store data into registers and memory locations. However, not all modes are supported. For example, it is not possible to use base-relative for both arguments of MOV: MOVQ -8(%rbx), -8(%rbx).</p>
<h1 id="faqs">FAQs</h1>
<h2 id="how-main-works">How main works</h2>
<ul>
<li><a href="http://www.tldp.org/LDP/LG/issue84/hawk.html">How main() is executed on Linux </a></li>
<li><a href="https://eli.thegreenplace.net/2012/08/13/how-statically-linked-programs-run-on-linux/">How statically linked programs run on Linux</a></li>
<li><a href="https://www.ibm.com/developerworks/library/l-gas-nasm/index.html">Linux assemblers: A comparison of GAS and NASM</a></li>
<li><a href="https://eli.thegreenplace.net/2013/07/24/displaying-all-argv-in-x64-assembly">Displaying all argv in x64 assembly</a></li>
</ul>
<h2 id="why-eax-is-made-zero-before-printf">Why %eax is made zero before printf</h2>
<p>Code in C:</p>
<pre><code class="language-C">printf(&quot;%d&quot;, 1);
Output:
</code></pre>
<p>Assembly</p>
<pre><code class="language-asm">movl    $1, %esi
leaq    LC0(%rip), %rdi
movl    $0, %eax  ; WHY?
call    _printf
</code></pre>
<p>From the x86_64 System V ABI:</p>
<pre><code>  Register    Usage
  %rax        temporary register; with variable arguments
            passes information about the number of vector
            registers used; 1st return register

For calls that may call functions that use varargs or
stdargs (prototype-less calls or calls to functions
containing ellipsis (. . . ) in the declaration) %al is
used as hidden argument to specify the number of vector
registers used. The contents of %al do not need to
match exactly the number of registers, but must be an
upper bound on the number of vector registers used
and is in the range 0–8 inclusive.
</code></pre>
<p>printf is a function with variable arguments, and the number
of vector registers used is zero. Note that printf must
check only %al, because the caller is allowed to leave
garbage in the higher bytes of %rax.</p>
<h3 id="reference">Reference</h3>
<ul>
<li><a href="https://stackoverflow.com/questions/6212665/why-is-eax-zeroed-before-a-call-to-printf/6212835#6212835">link</a></li>
</ul>]]></content><author><name>Sandeep Dasgupta</name><email>sandigame.123@gmail.com</email></author><category term="Binary" /><summary type="html"><![CDATA[X86 FAQs &amp; Binary Analysis Tools.]]></summary></entry><entry><title type="html">Decimal-Hexadecimal-2s Complement Binary Converter</title><link href="https://sdasgup3.github.io/two_complement_calculator/" rel="alternate" type="text/html" title="Decimal-Hexadecimal-2s Complement Binary Converter" /><published>2017-11-01T00:00:00+00:00</published><updated>2017-11-01T00:00:00+00:00</updated><id>https://sdasgup3.github.io/two_complement_calculator</id><content type="html" xml:base="https://sdasgup3.github.io/two_complement_calculator/"><![CDATA[<p>Getting rid of my dependecies on online convertors.
The online version coming soon...</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">#!/usr/bin/python
</span>
<span class="c1">##############################################################################################################
#   Extract information about a number.                                                                         #
#                                                                                                            #
#   Example Usage: python infonum.py --bit 4 0xf                                                             #
#   Output:                                                                                                  #
#           Base 10: -1                                                                                      #
#	    Base 16: f                                                                                       #
#	    2's Compliment binary: 1111                                                                      #
#   Example Usage: python infonum.py --bit 64 -1                                                             #
#   Output:                                                                                                  #
#	Base 10: -1                                                                                          #
#	Base 16: ff ff ff ff ff ff ff ff                                                                     #
#	2's Compliment binary: 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111       #
#                                                                                                            #
##############################################################################################################
</span>

<span class="kn">import</span> <span class="nn">argparse</span>
<span class="kn">import</span> <span class="nn">pdb</span>
<span class="kn">import</span> <span class="nn">re</span>

<span class="n">BIT</span><span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="mi">64</span><span class="p">)</span>
<span class="n">NEGATE</span> <span class="o">=</span> <span class="p">{</span><span class="s">'1'</span><span class="p">:</span> <span class="s">'0'</span><span class="p">,</span> <span class="s">'0'</span><span class="p">:</span> <span class="s">'1'</span><span class="p">}</span>

<span class="c1"># Convert a hex number (in 2's negate) to Decmal
</span><span class="k">def</span> <span class="nf">toDec</span><span class="p">(</span><span class="n">hexstr</span><span class="p">):</span>
    <span class="n">msb4bits</span> <span class="o">=</span> <span class="n">hexstr</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">msb4bits</span><span class="p">,</span> <span class="mi">16</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">n</span> <span class="o">&gt;=</span> <span class="mi">8</span><span class="p">:</span>
        <span class="n">p</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="o">*</span><span class="nb">pow</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="n">BIT</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">addend</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">8</span><span class="p">)</span> <span class="o">+</span> <span class="n">hexstr</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span> <span class="mi">16</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span> <span class="n">p</span> <span class="o">+</span> <span class="n">addend</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">hexstr</span><span class="p">,</span> <span class="mi">16</span><span class="p">))</span>


<span class="c1"># Convert a decimal number to  2's negate Hex
</span><span class="k">def</span> <span class="nf">toHex</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>

    <span class="n">num</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">num</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="s">'0'</span>

    <span class="n">M</span> <span class="o">=</span> <span class="s">'0123456789abcdef'</span>  <span class="c1"># like a map
</span>    <span class="n">ans</span> <span class="o">=</span> <span class="s">''</span>

    <span class="n">chunks</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">BIT</span><span class="p">)</span> <span class="o">/</span> <span class="nb">int</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">chunks</span><span class="p">):</span>
        <span class="n">n</span> <span class="o">=</span> <span class="n">num</span> <span class="o">&amp;</span> <span class="mi">15</span>       <span class="c1"># this means num &amp; 1111b
</span>        <span class="n">c</span> <span class="o">=</span> <span class="n">M</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>          <span class="c1"># get the hex char
</span>        <span class="n">ans</span> <span class="o">=</span> <span class="n">c</span> <span class="o">+</span> <span class="n">ans</span>
        <span class="n">num</span> <span class="o">=</span> <span class="n">num</span> <span class="o">&gt;&gt;</span> <span class="mi">4</span>
    <span class="k">return</span> <span class="n">ans</span>

<span class="k">def</span> <span class="nf">twocomplement</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">size_in_bits</span><span class="p">):</span>
    <span class="n">number</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">number</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">negate</span><span class="p">(</span><span class="nb">bin</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">number</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">2</span><span class="p">:]).</span><span class="n">rjust</span><span class="p">(</span><span class="n">size_in_bits</span><span class="p">,</span> <span class="s">'1'</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">bin</span><span class="p">(</span><span class="n">number</span><span class="p">)[</span><span class="mi">2</span><span class="p">:].</span><span class="n">rjust</span><span class="p">(</span><span class="n">size_in_bits</span><span class="p">,</span> <span class="s">'0'</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">negate</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="k">return</span> <span class="s">''</span><span class="p">.</span><span class="n">join</span><span class="p">(</span><span class="n">NEGATE</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">value</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">prettybinary</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">separator</span><span class="p">):</span>
    <span class="n">ans</span> <span class="o">=</span> <span class="s">''</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">value</span><span class="p">)):</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">i</span> <span class="o">%</span> <span class="n">separator</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">ans</span> <span class="o">=</span> <span class="n">ans</span> <span class="o">+</span> <span class="s">' '</span> <span class="o">+</span> <span class="n">value</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">ans</span> <span class="o">=</span> <span class="n">ans</span> <span class="o">+</span> <span class="n">value</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">ans</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span>

    <span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="p">.</span><span class="n">ArgumentParser</span><span class="p">()</span>

    <span class="n">parser</span><span class="p">.</span><span class="n">add_argument</span><span class="p">(</span>
        <span class="s">"--bit"</span><span class="p">,</span>
        <span class="n">default</span><span class="o">=</span><span class="mi">64</span><span class="p">,</span>
        <span class="n">help</span><span class="o">=</span><span class="s">"Bit width of the number"</span><span class="p">,</span>
        <span class="n">required</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">parser</span><span class="p">.</span><span class="n">add_argument</span><span class="p">(</span>
        <span class="s">"num"</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">'?'</span><span class="p">,</span>
        <span class="n">help</span><span class="o">=</span><span class="s">"Number to be analyzed"</span><span class="p">)</span>

    <span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="p">.</span><span class="n">parse_args</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">args</span><span class="p">.</span><span class="n">bit</span><span class="p">:</span>
        <span class="n">BIT</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">args</span><span class="p">.</span><span class="n">bit</span><span class="p">)</span>
        <span class="k">print</span><span class="p">(</span><span class="s">"Using {} bit"</span><span class="p">.</span><span class="nb">format</span><span class="p">(</span><span class="n">BIT</span><span class="p">))</span>

    <span class="n">num</span> <span class="o">=</span> <span class="p">(</span><span class="n">args</span><span class="p">.</span><span class="n">num</span><span class="p">)</span>
    <span class="n">matchObj</span> <span class="o">=</span> <span class="n">re</span><span class="p">.</span><span class="n">match</span><span class="p">(</span> <span class="sa">r</span><span class="s">'0x(.*)|0X(.*)'</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">re</span><span class="p">.</span><span class="n">M</span><span class="o">|</span><span class="n">re</span><span class="p">.</span><span class="n">I</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">matchObj</span><span class="p">:</span>
        <span class="c1"># Hex input
</span>        <span class="n">hexnum</span> <span class="o">=</span>  <span class="n">matchObj</span><span class="p">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">decimalnum</span> <span class="o">=</span> <span class="n">toDec</span><span class="p">(</span><span class="n">hexnum</span><span class="p">)</span>
        <span class="n">hexnum</span> <span class="o">=</span>  <span class="n">toHex</span><span class="p">(</span><span class="n">decimalnum</span><span class="p">)</span> <span class="c1"># To get the right padding
</span>        <span class="n">binarynum</span> <span class="o">=</span> <span class="n">twocomplement</span><span class="p">(</span><span class="n">decimalnum</span><span class="p">,</span> <span class="n">BIT</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># Decimal input
</span>        <span class="n">decimalnum</span> <span class="o">=</span> <span class="n">num</span>
        <span class="n">hexnum</span> <span class="o">=</span>  <span class="n">toHex</span><span class="p">(</span><span class="n">decimalnum</span><span class="p">)</span>
        <span class="n">binarynum</span> <span class="o">=</span> <span class="n">twocomplement</span><span class="p">(</span><span class="n">decimalnum</span><span class="p">,</span> <span class="n">BIT</span><span class="p">)</span>


    <span class="k">print</span><span class="p">(</span><span class="s">"</span><span class="se">\t</span><span class="s">Base 10: {}"</span><span class="p">.</span><span class="nb">format</span><span class="p">(</span><span class="n">decimalnum</span><span class="p">))</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"</span><span class="se">\t</span><span class="s">Base 16: {}"</span><span class="p">.</span><span class="nb">format</span><span class="p">(</span><span class="n">prettybinary</span><span class="p">(</span><span class="n">hexnum</span><span class="p">,</span><span class="mi">2</span><span class="p">)))</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"</span><span class="se">\t</span><span class="s">2's Compliment binary: {}"</span><span class="p">.</span><span class="nb">format</span><span class="p">(</span><span class="n">prettybinary</span><span class="p">(</span><span class="n">binarynum</span><span class="p">,</span> <span class="mi">8</span><span class="p">)))</span>

</code></pre></div></div>]]></content><author><name>Sandeep Dasgupta</name><email>sandigame.123@gmail.com</email></author><category term="bits" /><category term="Number System" /><summary type="html"><![CDATA[Getting rid of my dependecies on online convertors. The online version coming soon...]]></summary></entry><entry><title type="html">x86-64 Stack Frame Layout</title><link href="https://sdasgup3.github.io/x86_64_Stack_Frame_Layout/" rel="alternate" type="text/html" title="x86-64 Stack Frame Layout" /><published>2017-10-12T00:00:00+00:00</published><updated>2017-10-12T00:00:00+00:00</updated><id>https://sdasgup3.github.io/x86_64_Stack_Frame_Layout</id><content type="html" xml:base="https://sdasgup3.github.io/x86_64_Stack_Frame_Layout/"><![CDATA[<p>x86-64 Stack Frame Layout</p>
<h1 id="process-stack">Process Stack</h1>
<pre><code>High Addres --&gt; -----------
                |         |
                |         |
                |         |
                |         |
                |         |
                -----------
  Oxffffff08    | foo     |  &lt;-- XSP
                -----------
  Oxffffff00    |         |
Low Addres --&gt;  |         |

</code></pre>
<p>To push new data onto the stack we use the push instruction
<code>push %rax</code>
Is actually equivalent to this:</p>
<pre><code>sub $8, %rsp
mov %rax, (%rsp)
</code></pre><pre><code>High Addres --&gt; -----------
                |         |
                |         |
                |         |
                |         |
                |         |
                -----------
  Oxffffff08    | foo     |
                -----------
  Oxffffff00    | %rax val| &lt;-- XSP
                -----------
Low Addres --&gt;  |         |

</code></pre>
<p>Similarly, the pop instruction takes a value off the top of stack and places it in its operand, increasing the stack pointer afterwards. In other words, this:
<code>pop rax</code>
Is equivalent to this:</p>
<pre><code>mov (%rsp), %rax
add  $8, %rsp
</code></pre><pre><code>High Addres --&gt; -----------
                |         |
                |         |
                |         |
                |         |
                |         |
                -----------
  Oxffffff08    | foo     |  &lt;-- XSP
                -----------
  Oxffffff00    | %rax val| &lt;-- Still there
                -----------
Low Addres --&gt;  |         |

</code></pre>
<h1 id="stack-frames-amp-calling-convention">Stack frames &amp; Calling convention</h1>
<pre><code>int foobar(int a, int b, int c)
{
    int xx = a + 2;
    int yy = b + 3;
    int zz = c + 4;
    int sum = xx + yy + zz;

    return xx * yy * zz + sum;
}

int main()
{
    return foobar(77, 88, 99);
}
</code></pre>
<p>Right before the return statement, the stack frame for foobar looks like this:</p>
<p><img src="https://sdasgup3.github.io/assets/images/stackframe1.png" alt="" />
<img src="images/stackframe1.png" alt="" /></p>
<p>The green data were pushed onto the stack by the calling function, and the blue ones by foobar itself.</p>
<p>An x86-64 instruction may be at most 15 bytes in length. It consists of the following components in the given order, where the prefixes are at the least-significant (lowest) address in memory.</p>
<h1 id="argument-passing">Argument Passing</h1>
<p>According to the System V AMD 64 ABI, the first 6 integer or pointer arguments to a function are passed in registers. The order being:</p>
<pre><code> rdi:rsi:rdx:rcx:r8:r9
</code></pre>
<p>The 7th argument and onwards are passed on the stack.</p>
<pre><code>long myfunc(long a, long b, long c, long d,
            long e, long f, long g, long h)

{

}
rdi: a
rsi: b
rdx: c
rdx: d
r8: e
r9: f
g &amp; h are passed onto stack
</code></pre>
<p><img src="https://sdasgup3.github.io/assets/images/x64_frame_nonleaf.png" alt="" />
<img src="images/x64_frame_nonleaf.png" alt="" /></p>
<h2 id="reference">Reference</h2>
<ol>
<li><a href="https://eli.thegreenplace.net/2011/09/06/stack-frame-layout-on-x86-64#id8">Stack frame layout on x86-64</a></li>
</ol>]]></content><author><name>Sandeep Dasgupta</name><email>sandigame.123@gmail.com</email></author><category term="Syntax, X86/X86-64," /><summary type="html"><![CDATA[x86-64 Stack Frame Layout]]></summary></entry><entry><title type="html">LLVM Compiler Bugs related to UD</title><link href="https://sdasgup3.github.io/Compiler-Bugs/" rel="alternate" type="text/html" title="LLVM Compiler Bugs related to UD" /><published>2017-10-06T00:00:00+00:00</published><updated>2017-10-06T00:00:00+00:00</updated><id>https://sdasgup3.github.io/Compiler-Bugs</id><content type="html" xml:base="https://sdasgup3.github.io/Compiler-Bugs/"><![CDATA[<p>Here I am collecting some important points from various articiles mentioned in the reference section.</p>
<p>LLVM has three distinct kinds of undefined behavior. Together, they enable many desirable optimizations, and LLVM aggressively exploits these opportunities.</p>
<p>Undefined behavior in LLVM resembles undefined behavior in
C/C++: anything may happen to a program that executes it. The compiler may simply assume that undefined behavior does not oc- cur; this assumption places a corresponding obligation on the pro- gram developer (or on the compiler and language runtime, when a safe language is compiled to LLVM) to ensure that undefined op- erations are never executed. An instruction that executes undefined behavior can be replaced with an arbitrary sequence of instructions. When an instruction executes undefined behavior, all subsequent instructions can be considered undefined as well.</p>
<p>Following Table shows an arithmetic instructions have defined
behavior, following the LLVM IR specification. For example, the shl instruction is defined only when the shift amount is less than the bitwidth of the instruction.</p>
<p>Coming to the memory related instructions, the getelementptr instruction supports structured address computations: it uses a sequence of additions and multiplications to compute the address of a specific array element or structure field. For example, an array dereference in C such as val = a[b][c] can be translated to the following LLVM code:</p>
<pre><code>%ptr = getelementptr %a, %b, %c
%val = load %ptr
</code></pre>
<p>Unstructured memory accesses are supported by the <code>inttoptr</code> instruction. The load and store instructions support typed memory reads and writes. Out-of-bounds and unaligned loads and stores result in true undefined behavior, but a load from valid, uninitialized memory returns an undef.</p>
<table>
<thead>
<tr>
<th>Instruction</th>
<th>Definedness Constraint</th>
</tr>
</thead>
<tbody>
<tr>
<td>sdiv a, b</td>
<td>b != 0 ∧ (a ?= INT MIN ∨ b != −1)</td>
</tr>
<tr>
<td>udiv a, b</td>
<td>b != 0</td>
</tr>
<tr>
<td>srem a, b</td>
<td>b != 0 ∧ (a ?= INT MIN ∨ b != −1)</td>
</tr>
<tr>
<td>urem a, b</td>
<td>b != 0</td>
</tr>
<tr>
<td>shl a, b</td>
<td>b &lt;u B</td>
</tr>
<tr>
<td>lshr a, b</td>
<td>b &lt;u B</td>
</tr>
<tr>
<td>ashr a, b</td>
<td>b &lt;u B</td>
</tr>
</tbody>
</table>
<h2 id="undef">undef</h2>
<ul>
<li>Explicit	value	in	the	IR</li>
<li>Acts	like	a	free-floaLng	hardware	register
<ul>
<li>Takes	all	possible	bit	pakerns	at	the	specified	width</li>
<li>Can	take	a	different	value	every	Lme	it	is	used</li>
</ul>
</li>
<li>Comes	from	uniniLalized	variables</li>
<li><a href="http://sunfishcode.github.io/blog/2014/07/14/undef-introduction.html">Further	reading</a></li>
</ul>
<h2 id="poison">poison</h2>
<ul>
<li>Ephemeral	effect	of	math	instrucLons	that	violate
<ul>
<li>nsw	–	no	signed	wrap	for	add,	sub,	mul,	shl</li>
<li>nuw	–	no	unsigned	wrap	for	add,	sub,	mul,	shl</li>
<li>exact	–	no	remainder	for	sdiv,	udiv,	lshr,	ashr</li>
</ul>
</li>
<li>Designed	to	support	speculative	execuLon	of
operaLons	that	might	overflow. For example we may host loop invariant <code>x+1</code> outside the loop as signed add might overflow.</li>
<li>Poison	propagates	via	instrucLon	results</li>
<li>If	poison	reaches	a	side-effecting	instrucLon,	the
result	is	true	UB.</li>
</ul>
<h2 id="true-ud">True UD</h2>
<p>True	undefined	behavior</p>
<ul>
<li>Triggered	by
<ul>
<li>Divide	by	zero</li>
<li>Illegal	memory	accesses</li>
</ul>
</li>
</ul>
<h2 id="example-1">Example 1</h2>
<pre><code>%1 = add  %x, 1

=&gt;
%1 = add nsw %x, 1

ERROR: Target is more poisonous than Source for i4 %1

Example:
%x i4 = 0x7 (7)
Source value: 0x8 (8, -8)
Target value: poison

</code></pre>
<h2 id="example-2">Example 2</h2>
<pre><code>%1 = add nsw %x, 1

=&gt;
%1 = add  %x, 1

Optimization is correct
</code></pre>
<h2 id="example-3">Example 3</h2>
<pre><code>%1 = add nsw %x, 1
%2 = icmp sgt %1, %x

=&gt;

%2 = true

Done: 1
Optimization is correct
</code></pre>
<h2 id="shl">shl</h2>
<p><code>&lt;result&gt; = shl &lt;ty&gt; &lt;op1&gt;, &lt;op2&gt;           ; yields ty:result</code></p>
<p>Both arguments to the ‘shl‘ instruction must be the same integer or vector of integer type. ‘op2‘ is treated as an unsigned value.</p>
<p>The value produced is op1 * 2^op2 mod 26n, where n is the width of the result. If op2 is (statically or dynamically) equal to or larger than the number of bits in op1, this instruction returns a poison value. If the arguments are vectors, each vector element of op1 is shifted by the corresponding shift amount in op2.</p>
<p>If the nuw keyword is present, then the shift produces a poison value if it shifts out any non-zero bits.
or</p>
<p><code>(a &lt;&lt; b) &gt;&gt;u b = a</code> where &gt;&gt;u is logical shift.</p>
<p>If the nsw keyword is present, then the shift produces a poison value it shifts out any bits that disagree with the resultant sign bit.
<code>(a&lt;&lt;b)&gt;&gt;b</code></p>
<p>for n = 4; 0111 &lt;&lt; 1 leads to poison as</p>
<pre><code>0111 &lt;&lt; 1 == 1110 &gt;&gt;1 == 1111 (!= 0111)
</code></pre>
<h2 id="sdiv">sdiv</h2>
<p>Division by zero is undefined behavior. For vectors, if any element of the divisor is zero, the operation has undefined behavior. Overflow also leads to undefined behavior; this is a rare case, but can occur, for example, by doing a 32-bit division of -2147483648 by -1.</p>
<p>If the exact keyword is present, the result value of the sdiv is a poison value if the result would be rounded.</p>
<h2 id="srem">srem</h2>
<p>Taking the remainder of a division by zero is undefined behavior. For vectors, if any element of the divisor is zero, the operation has undefined behavior. Overflow also leads to undefined behavior; this is a rare case, but can occur, for example, by taking the remainder of a 32-bit division of -2147483648 by -1. (The remainder doesn’t actually overflow, but this rule lets srem be implemented using instructions that return both the result of the division and the remainder.)</p>
<h1 id="bug-20186">Bug 20186</h1>
<p>The transformation of -(X/C) to X/(-C) is invalid if C == INT_MIN.</p>
<pre><code class="language-C">%a = sdiv %X, C
%r = sub 0, %a
=&gt;
%r = sdiv %X, -C

ERROR: Domain of definedness of Target is smaller than Source's for i4 %r

Example:
%X i4 = 0x8 (8, -8)
C i4 = 0x1 (1)
%a i4 = 0x8 (8, -8)
Source value: 0x8 (8, -8)
Target value: undef
</code></pre>
<h1 id="bug-20189">Bug 20189</h1>
<pre><code>%B = sub 0, %A
%C = sub nsw %x, %B
=&gt;
%C = add nsw %x, %A

ERROR: Target is more poisonous than Source for i4 %C

Example:
%A i4 = 0x8 (8, -8)
%x i4 = 0x8 (8, -8)
%B i4 = 0x8 (8, -8)
Source value: 0x0 (0) // -8 - (0 - (-8)) == -8 - (-8)
Target value: poison // -8 + -8
</code></pre>
<h1 id="bug-21242">Bug 21242</h1>
<pre><code>Pre: isPowerOf2(C1)
%r = mul nsw %x, C1

=&gt;

%r = shl nsw %x, log2(C1)

ERROR: Target is more poisonous than Source for i4 %r

Example:
%x i4 = 0x1 (1)
C1 i4 = 0x8 (8, -8)
Source value: 0x8 (8, -8)
Target value: poison

// Source : mul nsw 1 * -8 (defined)
// Target : shl nsw 0001, 3 (poison)
</code></pre>
<h1 id="bug-21243">Bug 21243</h1>
<pre><code>Pre: !WillNotOverflowSignedMul(C1, C2)
%Op0 = sdiv %X, C1
%r = sdiv %Op0, C2

=&gt;
%r = 0

ERROR: Mismatch in values of i4 %r

Example:
%X i4 = 0x8 (8, -8)
C1 i4 = 0x2 (2)
C2 i4 = 0x4 (4)
%Op0 i4 = 0xC (12, -4)
Source value: 0xF (15, -1)
Target value: 0x0 (0)

Source: 4/ (-8/2) = -1
Target : 0
</code></pre>
<h1 id="bug-21245">Bug 21245</h1>
<pre><code>Pre: C2 % (1&lt;&lt;C1) == 0
%s = shl nsw i4 %X, C1
%r = sdiv %s, C2
  =&gt;
%r = sdiv %X, (C2 / (1 &lt;&lt; C1))

ERROR: Mismatch in values of i4 %r

Example:
%X i4 = 0xF (15, -1)
C1 i4 = 0x3 (3)
C2 i4 = 0x8 (8, -8)
%s i4 = 0x8 (8, -8)
Source value: 0x1 (1)
Target value: 0xF (15, -1)

Source:
1111 shl 3 bit == 1000 == -8
-8/C2 = 1

Target:
-1 / 8/8 or -1/ -8/-8 == -1 (15 or -1)
</code></pre>
<h1 id="bug-21255">Bug 21255</h1>
<pre><code>%Op0 = lshr %X, C1
%r = udiv %Op0, C2
  =&gt;
%r = udiv %X, (C2 &lt;&lt; C1)

ERROR: Domain of definedness of Target is smaller than Source's for i4 %r

Example:
%X i4 = 0x0 (0)
C1 i4 = 0x4 (4)
C2 i4 = 0x1 (1)
%Op0 i4 = poison
Source value: 0x0 (0)
Target value: UB

Source: lshr 0, 4 (poison as shift amount &gt;= bitwidth)
Target: %x / 1 &lt;&lt; 4 ( == 0) i.e. UD
</code></pre>
<p>And bypassing the undef case:</p>
<pre><code>Pre: ((C2 &lt;&lt; C1) != 0)
%Op0 = lshr exact %X, C1
%r = udiv %Op0, C2
  =&gt;
%r = udiv %X, (C2 &lt;&lt; C1)


ERROR: Mismatch in values of i4 %r

Example:
%X i4 = 0x8 (8, -8)
C1 i4 = 0x2 (2)
C2 i4 = 0x9 (9, -7)
%Op0 i4 = 0x2 (2)
Source value: 0x0 (0)
Target value: 0x2 (2)

Source: (lshr exact -8,2 ) / 9 == 2/9 = 0
Target: 8 / (-7 &lt;&lt; 2) = 8 / (1001 &lt;&lt; 2) = 8 / 4 = 2
</code></pre>
<p>And finally we have:</p>
<pre><code>Pre: WillNotOverflowUnsignedShl(C2, C1)
%Op0 = lshr %X, C1
%r = udiv %Op0, C2
  =&gt;
%r = udiv %X, (C2 &lt;&lt; C1)

Done
Optimization is correct!
</code></pre>
<h1 id="bug-21256">Bug 21256</h1>
<pre><code>%Op1 = sub 0, %X
%r = srem %Op0, %Op1
  =&gt;
%r = srem %Op0, %X


ERROR: Domain of definedness of Target is smaller than Source's for i4 %r

Example:
%X i4 = 0xF (15, -1)
%Op0 i4 = 0x8 (8, -8)
%Op1 i4 = 0x1 (1)
Source value: 0x0 (0)
Target value: undef

Source: -8 % (0 - (-1)) = -8 % 1 = 0
Target: -8 % -1 =  UD
</code></pre>
<h1 id="bug--31633">Bug  31633</h1>
<p>InstCombine currently folds  &quot;select %c, undef, %foo&quot; into %foo, because it assumes that undef can take any value that %foo may take.</p>
<pre><code>%y2 = add nsw i32 %y, 1
%s = select i1 %c, i32 undef, i32 %y2
%r = icmp sgt i32 %s, %y

=&gt;
%r = true

ERROR: Mismatch in values of i1 %r

Example:
%y i32 = 0x7FFFFFFF (2147483647)
%c i1 = 0x1 (1, -1)
%y2 i32 = poison
%s i32 = 0x00000000 (0)
Source value: 0x0 (0)
Target value: 0x1 (1, -1)

%y2 overflows and becomes poison, but the select should return undef only, not poison.
</code></pre>
<h1 id="refereces">Refereces</h1>
<ul>
<li><a href="https://www.cs.utah.edu/~regehr/llvm-ub.pdf">Undefined Behavious in LLVM</a></li>
<li><a href="https://llvm.org/docs/LangRef.html">LLVM language referece manual</a></li>
<li><a href="https://dl.acm.org/citation.cfm?id=2737965">Provably correct peephole optimizations with alive</a></li>
<li><a href="https://rise4fun.com/Alive">Alive</a></li>
<li><a href="https://blog.regehr.org/archives/1510">Translation Validation of Bounded Exhaustive Test Cases</a></li>
</ul>]]></content><author><name>Sandeep Dasgupta</name><email>sandigame.123@gmail.com</email></author><category term="Compilers,  Bugs, Alive" /><summary type="html"><![CDATA[Here I am collecting some important points from various articiles mentioned in the reference section.]]></summary></entry><entry><title type="html">Lambda Calculus</title><link href="https://sdasgup3.github.io/Lambda-Calculus/" rel="alternate" type="text/html" title="Lambda Calculus" /><published>2017-09-12T00:00:00+00:00</published><updated>2017-09-12T00:00:00+00:00</updated><id>https://sdasgup3.github.io/Lambda-Calculus</id><content type="html" xml:base="https://sdasgup3.github.io/Lambda-Calculus/"><![CDATA[<p>Lambda Calculus.</p>
<h1 id="lambda-calculus">Lambda Calculus</h1>
<h2 id="syntax">syntax</h2>
<pre><code>E → ID
E → λ ID. E
E → E E
E → (E)
</code></pre>
<ul>
<li>The grammar is ambiguous like</li>
</ul>
<pre><code>xyz could be x(yz) or xy(z)
λx.yz could be (λx.y)z  or λx.(yz)
</code></pre>
<ul>
<li>The grammar rules are not changed to make it unambiguous, but some
disambiguation rules are added outside of the grammar.</li>
</ul>
<pre><code>E → E E is left assocative:  xyz == (xy)z
λ x.yz == λ x.(yz)
λx.λy.zw == λx.(λy.(zw))
</code></pre>
<p>Note:
<code>let x = e in e'</code>
is nothing but syntactic sugar for</p>
<p><code>(λ x . e') e</code></p>
<h2 id="semantics">Semantics</h2>
<ul>
<li>Every ID that we see in lambda calculus is called a <code>variable</code></li>
<li>E →  ID . E is called an abstraction
<ul>
<li>The ID is the variable of the abstraction (also metavariable)</li>
<li>E is called the body of the abstraction</li>
</ul>
</li>
<li>E → E E
<ul>
<li>This is called an application</li>
</ul>
</li>
<li>λ ID . E defines a new anonymous function
<ul>
<li>ID is the formal parameter of the function</li>
<li>E Body is the body of the function</li>
</ul>
</li>
<li>E → E1 E2, function application, is similar to calling function E1 and
setting its formal parameter to the actual parameter E2</li>
</ul>
<h3 id="examples">Examples</h3>
<h4 id="expl-i">Expl I</h4>
<ul>
<li>λ x . + x 1 == λ x . (+ x 1)
<ul>
<li>Represents a function that adds one to its argument</li>
</ul>
</li>
<li>(λ x . + x 1) 2
<ul>
<li>Represents calling the original function by supplying 2 for x and it would &quot;reduce&quot; to (+ 2 1) = 3</li>
</ul>
</li>
<li>Computing with lambda expressions involves rewriting; for each application, we replace all occurrences of the formal parameter <code>variable</code> in the function body with the value of the actual parameter (a lambda expression). It is easier to understand if we use the abstract-syntax tree of a lambda expression instead of just the text. Here's our simple example application again:</li>
</ul>
<p><code>(λx.x+1)3</code>
And here's the abstract-syntax tree (where λ is the abstraction operator, and apply is the application operator):</p>
<pre><code>        apply
        /   \
       λ     3
      / \
     x   +
        / \
       x   1
</code></pre>
<p>We rewrite the abstract syntax tree by finding applications of functions to arguments, and for each, replacing the formal parameter with the argument in the function body. To do this,</p>
<ul>
<li>we must find an apply node whose left child is a lambda node, since only lambda nodes represent functions.</li>
<li>The right subtree of the apply node is the argument.</li>
<li>The left subtree of the apply node (with a lambda at its root) is the function.</li>
<li>The left child of the lambda is the formal parameter.</li>
<li>The right child of the lambda is the function body.</li>
</ul>
<p>There is only one apply node in our example; the argument is 3, the function is λx.x+1; the formal parameter is x, and the function body is x+1. Here's the rewriting step:</p>
<pre><code>        apply      =&gt;      +
        /   \             / \
       λ     3           3   1
      / \
     x   +
        / \
       x   1
</code></pre>
<p>Here's an example with two applications:
(λx.x+1)((λy.y+2)3)</p>
<pre><code>        apply         =&gt;   apply     =&gt;  apply   =&gt;  +  =&gt;  6
       /     \             /   \         /   \      / \
      λ       apply       λ     +       λ     5    5   1
     / \       /  \      / \   / \     / \
    x   +     λ    3    x   + 3   2   x   +
       / \   / \           / \           / \
      x   1 y   +         x   1         x   1
               / \
              y   2
</code></pre>
<p>OR</p>
<pre><code>apply         =&gt;    +     =&gt;  +   =&gt;  +  =&gt;  6
/     \             / \       / \     / \
λ       apply     apply 1     +   1   5   1
/ \       /  \      / \       / \
x   +     λ    3    λ   3    3    2
/ \   / \       / \
x   1 y   +     y   +
       / \       / \
y   2     y   2

</code></pre>
<h4 id="expl-ii">Expl II</h4>
<p>Note that the result of rewriting a non-pure lambda expression can be a constant (as in the examples above), but the result can also be a lambda expression: a variable, or an abstraction, or an application. For a pure lambda expression, the result of rewriting will always itself be a lambda expression. Here are some more examples:</p>
<p>(λf.λx.fx)λy.y+1</p>
<pre><code>        apply      =&gt;   λ        =&gt;    λ        λx.x+1
       /     \         / \            / \
      λ       λ       x  apply       x   +
     / \     / \         /   \          / \
    f   λ   y   +       λ     x        x   1
       / \     / \     / \
      x  apply y  1   y   +
         /  \            / \
        f    x          y   1
</code></pre>
<p>Note that the result of the rewriting is a function. Also note that in this example, although there are initially two &quot;apply&quot; nodes, only one of them has a lambda node as its left child, so there is only one rewrite that can be done initially.</p>
<p>(λx.λy.x)(λz.z)</p>
<pre><code>           apply            λ         λy.λz.z
          /     \          / \
         λ       λ    =&gt;  y   λ
        / \     / \          / \
       x   λ   z   z        z   z
          / \
         y   x
</code></pre>
<p>(λx.λy.xy)(λz.z)</p>
<pre><code>  apply      =&gt;       λ     =&gt;        λ      λy.y
 /     \             / \             / \
λ       λ           y   apply       y   y
/ \     / \             /     \
x   λ   z   z           λ       y
   / \                 / \
  y  apply            z   z
  /     \
 x       y
</code></pre>
<h2 id="currying">Currying</h2>
<ul>
<li>Technique to translate the evaluation of a function that takes multiple arguments into a sequence of functions that each take a single argument</li>
<li>Define adding two parameters together with functions that only take one parameter:
<ul>
<li>λ x . λ y . ((+ x) y)</li>
<li>(λ x . λ y . ((+ x) y)) 1
<ul>
<li>λ y . ((+ 1) y)</li>
</ul>
</li>
<li>(λ x . λ y . ((+ x) y)) 10 20
<ul>
<li>(λ y . ((+ 10) y)) 20</li>
<li>((+ 10) 20) = 30</li>
</ul>
</li>
</ul>
</li>
<li>Example in the context of programming</li>
</ul>
<pre><code class="language-C++">  #include &lt;iostream&gt;
  using namespace std;

  int F(int a, int b, int c) { return a + b + c; }

  int F_curry() {
    auto f = [](int a) {
      return [a](int b) { return [a, b](int c) { return a + b + c; }; };
    };

    return ((f(1))(2))(3);
  }

  int main() {
    cout &lt;&lt; F(1, 2, 3) &lt;&lt; endl;
    cout &lt;&lt; F_curry() &lt;&lt; endl;
  }
</code></pre>
<h2 id="problems-with-the-naive-rewriting-rule">Problems with the naive rewriting rule</h2>
<h3 id="problem-1">Problem 1</h3>
<p>We don't, in general, want to replace all occurrences of x.</p>
<p>To see why, consider the following (non-pure) lambda expression:
<code>(λx.(x + ((λx.x+1)3)))2</code></p>
<p>This expression should reduce to 6;</p>
<pre><code>the inner expression: (λx.x+1)3 takes one argument, the value 3, and adds 1, producing 4. The outer expression is now: (λx.(x + 4))2 i.e., it takes one argument, the value 2, and adds 4, producing 6.
</code></pre>
<p>However, if we rewrite the outer application first, using the naive rewriting rule, here's what happens:</p>
<pre><code>  apply
   /\
  λ  2
 / \
x   +                        +
   / \                      / \
 x   apply     =&gt;          2  apply     =&gt;  +    =&gt; 5
      / \    (bad              / \         / \
     λ   3   application)     λ   3       2   +
    / \                      / \             / \
   x   +                    x   +           2   1
      / \                      / \
     x   1                    2   1
</code></pre>
<p>We get the wrong answer (5 instead of 6), because we replaced the occurrence of x in the inner expression with the value supplied as the parameter for the outer expression.</p>
<h3 id="problem-2">Problem 2</h3>
<p>Consider the (pure) lambda expression</p>
<p><code>((λx.λy.x)y)z</code></p>
<p>The expression λx.λy.x should simply return the first argument, so in this case the result of rewriting should be y. However, if we use the naive rewriting rule, replacing all occurrences of the formal parameter x with the argument y, we get:
<code>(λy.y)z</code></p>
<p>and now if we rewrite that expression we get
<code>z</code></p>
<p>i.e., we got the second argument instead of the first one!</p>
<p>This example illustrates what is called the <code>&quot;capture&quot; or &quot;name clash&quot; problem</code>.</p>
<h2 id="free-variable">Free variable</h2>
<p>A variable is free if it does not appear within the body of an abstraction with a metavariable of the same name</p>
<ul>
<li>x free in λ x . x y z? No</li>
<li>y free in λ x . x y z? Yes</li>
<li>x free in (λ x . (+ x (No) 1)) x (Yes)?</li>
<li>z free in λ x . λ y . λ z . z  y x? No</li>
<li>x free in (λ x . z foo) (λ y . y x)? Yes</li>
</ul>
<p>x is free in E if:</p>
<ol>
<li>E = x</li>
<li>E = λ y . E1, where y != x and x is free in E1</li>
<li>E = E1 E2, where x is free in E1 or E2</li>
</ol>
<ul>
<li>x free in x λ x . x == x Yes (λ x . x No) --&gt; Yes,  from rule 3</li>
<li>x free in (λ x . x y) x ? Yes</li>
<li>x free in λ x . y x ? No</li>
</ul>
<h2 id="bound-variables">Bound Variables</h2>
<ul>
<li>If an occurrence of x is free in E, then it is bound by λ x . in λ x . E</li>
<li>If an occurrence of x is bound by a particular λ x . in E, then x is bound by the same λ x . in λ z . E
<ul>
<li>Even if z == x</li>
<li>Example: λ x . λ x .  x
<ul>
<li>Can also be written as λ y . λ x .  x; So x is bound by λ x preceding it.</li>
</ul>
</li>
<li>If an occurrence of x is bound by a particular λ x . in E1, then that occurrence in E1 is tied by the same abstraction λ x . in E1 E2 and E2 E1</li>
</ul>
</li>
</ul>
<p>Example</p>
<ul>
<li>(λ x . x (λ y . x y z y) x) x y
<ul>
<li>(λ x . x (λ y . x y z y) x) x(Free) y(Free)</li>
</ul>
</li>
<li>(λ x . λ y . x y) (λ z . x(Free) z)</li>
<li>(λ x . x λ x . z x)
<ul>
<li>(λ x . x) (λ x . z(Free) x)</li>
</ul>
</li>
</ul>
<h2 id="alpha-reduction">Alpha Reduction</h2>
<p>To solve <a href="#Problem-2">Problem 2</a>, we use a technique called alpha-reduction. The basic idea is that formal parameter names are unimportant; so rename them as needed to avoid capture. Alpha-reduction is used to modify expressions of the form &quot;λx.M&quot;. It renames all the occurrences of x that are free in M to some other variable z that does not occur in M (and then λx is changed to λz). For example, consider λx.λy.x+y (this is of the form λx.M). Variable z is not in M, so we can rename x to z; i.e.,
<code>λx.λy.x+y alpha-reduces to λz.λy.z+y</code></p>
<h2 id="beta-reduction">Beta Reduction</h2>
<p>Defined by:</p>
<pre><code>(λx. e1) e2 ⇒ e1[e2/x]
</code></pre>
<p>where the notation e1[e2/x] denotes the result of substituting e2 <code>for all free occurrences of x in e1</code>.</p>
<p>(lambda z. (z z)) (lambda x. lambda y. (x y));
A: for apply</p>
<pre><code>    A                        A                 λ        λ
                          /     \              /\       /\
   / \                   λ      λ             w  A     w λ
  /   \                  /\     /\               /\     /\
 λ     λ        ==&gt;     x  λ   x  λ    -&gt;        λ w -&gt; y A
 /\   / \                  /\     /\            /\       /\
z  A  x  λ                w  A   y  A          x λ      w y
   / \    /\                  /\    /\           /\
 z   z   y A                 x w    x y         y A
            /\                                    /\
           x  y                                  x y
</code></pre>
<p><code>λw.(λy.wy)</code></p>
<h2 id="references">References</h2>
<ul>
<li><a href="https://adamdoupe.com/teaching/classes/cse340-principles-of-programming-languages-s16/">Lecture notes of Prof. Adam Doupé</a></li>
<li><a href="http://pages.cs.wisc.edu/~horwitz/CS704-NOTES/1.LAMBDA-CALCULUS.html#overview"> Lambda Calculus (Part I)</a></li>
</ul>]]></content><author><name>Sandeep Dasgupta</name><email>sandigame.123@gmail.com</email></author><category term="Lambda Calculus" /><summary type="html"><![CDATA[Lambda Calculus.]]></summary></entry><entry><title type="html">Compilers Leveraging Undefined Behavious</title><link href="https://sdasgup3.github.io/Leveraging-Undefined-Behavious/" rel="alternate" type="text/html" title="Compilers Leveraging Undefined Behavious" /><published>2017-09-12T00:00:00+00:00</published><updated>2017-09-12T00:00:00+00:00</updated><id>https://sdasgup3.github.io/Leveraging-Undefined-Behavious</id><content type="html" xml:base="https://sdasgup3.github.io/Leveraging-Undefined-Behavious/"><![CDATA[<p>One of the classic examples of compilers making use of undefined behaviour is as follows:
C standars says signed integers overflow is undefined.</p>
<p>Knowing this information help compiler to optimize <code>x+1&gt;x</code> to true. As compilers know that <code>INT_MAX+1</code> is undefined so it can safely make the optimization.</p>
<p>Had signed integer overflow been defined (with a definition of say wrap around), then we will not be able to do the optimization as x + 1 is not <code>&gt;</code> x if x == <code>INT_MAX</code> (under the wrap around defintion)</p>
<h1 id="leveraging-undefined-behavious-by-optimizing-compilers">Leveraging undefined behavious by optimizing compilers.</h1>
<p>Undefined behaviors facilitate optimizations by permitting a compiler to assume that programs will only execute defined operations.</p>
<h2 id="case-i">Case I</h2>
<pre><code class="language-C">#include &lt;iostream&gt;

int fermat() {
  const int MAX = 1000;
  int a=1,b=1,c=1;
  // Endless loop with no side effects is UB
  while (1) {
    if (((a*a*a) == ((b*b*b)+(c*c*c)))) return 1;
    a++;
    if (a&gt;MAX) { a=1; b++; }
    if (b&gt;MAX) { b=1; c++; }
    if (c&gt;MAX) { c=1;}
  }
  return 0;
}

int main() {
  if (fermat())
    std::cout &lt;&lt; &quot;Fermat's Last Theorem has been disproved.\n&quot;;
  else
    std::cout &lt;&lt; &quot;Fermat's Last Theorem has not been disproved.\n&quot;;
}
</code></pre>
<p>Result:</p>
<pre><code>Fermat's Last Theorem has been disproved.
</code></pre>
<p>Despite the fact that this program does not contain any arithmetic overflows
(multiplier factors vary in the range from 1 to 1000, the sum of their cubes
does not exceed 2^31), the C++ standard defines an infinite loop as an
undefined action, without changing the external state. That’s why C++ compilers
are entitled to consider similar loops as finite.</p>
<p>The compiler can easily see that the only way out of the while(1) loop is the
return 1; statement, while the return 0; statement at the end of fermat()
cannot be reached. Therefore, it optimizes this function to</p>
<p>int fermat (void)
{
return 1;
}
In other words, the only possibility to write an infinite loop that could not be
removed by the compiler is to add a modification of the external state to the
loop body.</p>
<h2 id="case-ii">Case II</h2>
<pre><code class="language-C">int table[4];
bool exists_in_table(int v)
{
    for (int i = 0; i &lt;= 4; i++) {
        if (table[i] == v) return true;
    }
    return false;
}
</code></pre>
<p>First of all, you might notice the off-by-one error in the loop control. The
result is that the function reads one past the end of the table array before
giving up. A classical compiler wouldn't particularly care. It would just
generate the code to read the out-of-bounds array element (despite the fact
that doing so is a violation of the language rules), and it would return
true if the memory one past the end of the array happened to match.</p>
<p>A post-classical compiler, on the other hand, might perform the following
analysis:</p>
<ul>
<li>The first four times through the loop, the function might return true.</li>
<li>When i is 4, the code performs undefined behavior. Since undefined behavior
lets me do anything I want, I can totally ignore that case and proceed on the
assumption that i is never 4. (If the assumption is violated, then something
unpredictable happens, but that's okay, because undefined behavior grants
me permission to be unpredictable.)</li>
<li>The case where i is 5 never occurs, because in order to get there, I first
have to get through the case where i is 4, which I have already assumed cannot
happen.</li>
<li>Therefore, all legal code paths return true.  As a result, a post-classical
compiler can optimize the function to <code>C bool exists_in_table(int v) { return true; } </code></li>
</ul>
<h2 id="case-iii">Case III</h2>
<pre><code class="language-C">int foo(int x) {
    return x+1 &gt; x; // either true or UB due to signed overflow
}
</code></pre>
<p>may be compiled as (demo)</p>
<pre><code class="language-C">foo(int):
        movl    $1, %eax
        ret
</code></pre>
<p>Because in all legal cases true is returned.</p>
<h2 id="case-iv">Case IV</h2>
<pre><code class="language-C">std::size_t f(int x)
{
    std::size_t a;
    if(x) // either x nonzero or UB
        a = 42;
    return a;
}
</code></pre>
<p>May be compiled as (demo)</p>
<pre><code class="language-C">f(int):
        mov     eax, 42
        ret
</code></pre>
<p>Because in all legal cases 42 is returned.</p>
<pre><code class="language-C">bool p; // uninitialized local variable
if(p) // UB access to uninitialized scalar
    std::puts(&quot;p is true&quot;);
if(!p) // UB access to uninitialized scalar
    std::puts(&quot;p is false&quot;);
</code></pre>
<p>Possible output:</p>
<pre><code>p is true
p is false
</code></pre>
<p>The code will ub are optimized out.</p>
<h2 id="case-v">Case V</h2>
<pre><code class="language-C">int foo(int* p) {
    int x = *p;
    if(!p) return x; // Either UB above or this branch is never taken
    else return 0;
}
</code></pre>
<p>may be compiled as</p>
<pre><code>foo(int*):
        xorl    %eax, %eax
        ret

</code></pre>
<p>If p is null, the if access is UD; If p is non null, then <code>return 0</code> will
happen. So in legal cases, 0 is returned and so is the optimization.</p>
<h1 id="pointer-overflow">Pointer Overflow</h1>
<p>It is undefined behavior to perform pointer arithmetic where the result is outside of an object, with the exception that it is permissible to point one element past the end of an array:</p>
<pre><code>int a[10];
int *p1 = a - 1; // UB
int *p2 = a; // ok
int *p3 = a + 9; // ok
int *p4 = a + 10; // ok, but can't be dereferenced
int *p5 = a + 11; // UB
</code></pre>
<h2 id="interesting-case">Interesting case</h2>
<pre><code class="language-C">char buffer[BUFLEN];
char *buffer_end = buffer + BUFLEN;

/* ... */
unsigned int len;

if (buffer + len &gt;= buffer_end)
  die_a_gory_death(&quot;len is out of range\n&quot;);
</code></pre>
<p>Here, the programmer is trying to ensure that len (which might come from an untrusted source) fits within the range of buffer. There is a problem, though, in that if len is very large, the addition could cause an overflow, yielding a pointer value which is less than buffer. So a more diligent programmer might check for that case by changing the code to read:</p>
<pre><code class="language-C">if (buffer + len &gt;= buffer_end || buffer + len &lt; buffer)
  loud_screaming_panic(&quot;len is out of range\n&quot;);
</code></pre>
<p>This code should catch all cases; ensuring that len is within range. There is only one little problem: recent versions of GCC will optimize out the second test (returning the if statement to the first form shown above), making overflows possible again. So any code which relies upon this kind of test may, in fact, become vulnerable to a buffer overflow attack.</p>
<p>This behavior is allowed by the C standard, which states that, in a correct program, pointer addition will not yield a pointer value outside of the same object. So the compiler can assume that the test for overflow is always false and may thus be eliminated from the expression. It turns out that GCC is not alone in taking advantage of this fact: some research by GCC developers turned up other compilers (including PathScale, xlC, LLVM, TI Code Composer Studio, and Microsoft Visual C++ 2005) which perform the same optimization. So it seems that the GCC developers have a legitimate reason to be upset: CERT would appear to be telling people to avoid their compiler in favor of others - which do exactly the same thing.</p>
<p>The right solution to the problem, of course, is to write code which complies with the C standard. In this case, rather than doing pointer comparisons, the programmer should simply write something like:</p>
<pre><code class="language-C">if (len &gt;= BUFLEN)
    launch_photon_torpedoes(&quot;buffer overflow attempt thwarted\n&quot;);
</code></pre>
<h3 id="references">References</h3>
<ul>
<li><a href="https://blog.regehr.org/archives/161">John Regehr Blog: Compilers and Termination Revisited</a></li>
<li><a href="http://en.cppreference.com/w/c/language/behavior">cppreference.com</a></li>
<li><a href="https://blogs.msdn.microsoft.com/oldnewthing/20140627-00/?p=633/">Undefined behavior can result in time travel</a></li>
<li><a href="https://blog.regehr.org/archives/1395">Pointer Overflow Checking</a></li>
<li><a href="https://lwn.net/Articles/278137/">GCC &amp; pointer overflows</a></li>
</ul>]]></content><author><name>Sandeep Dasgupta</name><email>sandigame.123@gmail.com</email></author><category term="Undefined behaviour" /><summary type="html"><![CDATA[One of the classic examples of compilers making use of undefined behaviour is as follows: C standars says signed integers overflow is undefined.]]></summary></entry><entry><title type="html">Build SPEC2006 using LLVM’s cmake infrastructure</title><link href="https://sdasgup3.github.io/Build_SPEC2006_CMake/" rel="alternate" type="text/html" title="Build SPEC2006 using LLVM’s cmake infrastructure" /><published>2017-08-21T00:00:00+00:00</published><updated>2017-08-21T00:00:00+00:00</updated><id>https://sdasgup3.github.io/Build_SPEC2006_CMake</id><content type="html" xml:base="https://sdasgup3.github.io/Build_SPEC2006_CMake/"><![CDATA[<p>Instructions to build Spec2006 using LLVM's Cmake infrastructure.</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  <span class="c"># Setup the environment.</span>
  <span class="nb">export </span><span class="nv">LLVM_SRC</span><span class="o">=</span>&lt;Root of LLVM <span class="nb">source </span>code tree&gt;
  <span class="nb">export </span><span class="nv">LLVM_BLD</span><span class="o">=</span>&lt;Root of LLVM build  tree&gt;
  <span class="nb">export </span><span class="nv">SPEC_SRC</span><span class="o">=</span>&lt;Root of SPEC 2006 <span class="nb">source </span>code tree&gt;
  <span class="nv">TESTSUITE_BUILD_DIR</span><span class="o">=</span>&lt;Build <span class="nb">dir </span>of test-suite&gt;

  <span class="c"># Make SPEC source available to LLVM build system.</span>
  <span class="nb">mkdir</span> <span class="nv">$LLVM_SRC</span>/projects/test-suite/test-suite-externals
  <span class="nb">ln</span> <span class="nt">-s</span> <span class="nv">$SPEC_SRC</span> <span class="nv">$LLVM_SRC</span>/projects/test-suite/test-suite-externals/speccpu2006

  <span class="nb">mkdir</span> <span class="nv">$TESTSUITE_BUILD_DIR</span> <span class="o">&amp;&amp;</span> <span class="nb">cd</span> <span class="nv">$TESTSUITE_BUILD_DIR</span>
  <span class="c"># Configure</span>
  cmake <span class="nv">$LLVM_SRC</span>/projects/test-suite <span class="nt">-DCMAKE_C_COMPILER</span><span class="o">=</span>&lt;c compiler&gt; <span class="nt">-DCMAKE_CXX_COMPILER</span><span class="o">=</span>&lt;c++ compiler&gt;
  <span class="c"># Build the binaries.</span>
  <span class="nb">cd </span>External/SPEC/CINT2006/
  make <span class="nt">-j</span> 8
  <span class="c"># Run</span>
  lit <span class="nt">-v</span> <span class="nt">-j</span> 8 <span class="nb">.</span> <span class="nt">-o</span> results.json
</code></pre></div></div>]]></content><author><name>Sandeep Dasgupta</name><email>sandigame.123@gmail.com</email></author><category term="LLVM" /><category term="CMake" /><category term="SPEC CPU 2006 Benchmark" /><summary type="html"><![CDATA[Instructions to build Spec2006 using LLVM's Cmake infrastructure.]]></summary></entry><entry><title type="html">C++ Timers</title><link href="https://sdasgup3.github.io/C++-Timers/" rel="alternate" type="text/html" title="C++ Timers" /><published>2017-08-09T00:00:00+00:00</published><updated>2017-08-09T00:00:00+00:00</updated><id>https://sdasgup3.github.io/C++-Timers</id><content type="html" xml:base="https://sdasgup3.github.io/C++-Timers/"><![CDATA[<p>The following code illustrates the usage of various APIs to time execution in C++ code. Some of these are applicable in C as well.</p>
<pre><code class="language-C++">
#include &lt;chrono&gt;
#include &lt;cstdint&gt;
#include &lt;math.h&gt;  /* sqrt */
#include &lt;stdio.h&gt; /* printf */
#include &lt;sys/time.h&gt;
#include &lt;time.h&gt; /* clock_t, clock, CLOCKS_PER_SEC */

using namespace std::chrono;

// THe function to be timed.
int frequency_of_primes(int n) {
  int i, j;
  int freq = n - 1;
  for (i = 2; i &lt;= n; ++i)
    for (j = sqrt(i); j &gt; 1; --j)
      if (i % j == 0) {
        --freq;
        break;
      }
  return freq;
}

int main() {
  clock_t start1, end1;
  time_t start2, end2;
  timeval start3, end3;
  std::chrono::time_point&lt;std::chrono::system_clock&gt; start4, end4;
  std::chrono::high_resolution_clock::time_point start5, end5;

  /* Collect the start times using different methods. */
  // Method 1
  start1 = clock();
  // Method 2
  time(&amp;start2);
  // Method 3
  gettimeofday(&amp;start3, NULL);
  // Method 4
  start4 = std::chrono::system_clock::now();
  // Method 5
  start5 = std::chrono::high_resolution_clock::now();

  // The computaton to be timed.
  printf(&quot;Calculating...\n&quot;);
  int f = frequency_of_primes(999999);
  printf(&quot;The number of primes lower than 100,000 is: %d\n\n&quot;, f);

  /* Collect the end times using different methods. */
  // Method 1
  end1 = clock();
  // Method 2
  time(&amp;end2);
  // Method 3
  gettimeofday(&amp;end3, NULL);
  // Method 4
  end4 = std::chrono::system_clock::now();
  // Method 5
  end5 = std::chrono::high_resolution_clock::now();

  /* Compute elapsed times using different methods. */
  // Method 1
  long int elapsed_seconds1 = end1 - start1;
  // Method 2
  double elapsed_seconds2 = difftime(end2, start2);
  // Method 3
  double elapsed_seconds3 = (double(end3.tv_sec - start3.tv_sec)) * 1000000.00 +
                            double(end3.tv_usec - start3.tv_usec);
  // Method 4
  std::chrono::duration&lt;double&gt; elapsed_seconds4 = end4 - start4;
  // Method 5
  std::chrono::duration&lt;double&gt; elapsed_seconds5 = (end5 - start5);
  double elapsed_seconds6 =
      double(duration_cast&lt;nanoseconds&gt;(end5 - start5).count());

  /* Display the elapsed times */
  printf(&quot;Clock: %ld clicks (%f us).\n&quot;, elapsed_seconds1,
         (double(elapsed_seconds1)) * 1000000.00 / CLOCKS_PER_SEC);
  printf(&quot;Time: %f us).\n&quot;, elapsed_seconds2 * 1000000.00);
  printf(&quot;gettimeofday: %f us).\n&quot;, elapsed_seconds3);
  printf(&quot;chrono::system_clock %f us).\n&quot;,
         elapsed_seconds4.count() * 1000000.00);
  printf(&quot;chrono::high_resolution_clock %f us).\n&quot;,
         elapsed_seconds5.count() * 1000000.00);
  printf(&quot;chrono::high_resolution_clock %f us).\n&quot;, elapsed_seconds6);

  return 0;
}
</code></pre>]]></content><author><name>Sandeep Dasgupta</name><email>sandigame.123@gmail.com</email></author><summary type="html"><![CDATA[The following code illustrates the usage of various APIs to time execution in C++ code. Some of these are applicable in C as well.]]></summary></entry><entry><title type="html">Assembly Language Debugging with gdb</title><link href="https://sdasgup3.github.io/Assembly_Language_Debugging_with_gdb/" rel="alternate" type="text/html" title="Assembly Language Debugging with gdb" /><published>2016-12-16T00:00:00+00:00</published><updated>2016-12-16T00:00:00+00:00</updated><id>https://sdasgup3.github.io/Assembly_Language_Debugging_with_gdb</id><content type="html" xml:base="https://sdasgup3.github.io/Assembly_Language_Debugging_with_gdb/"><![CDATA[<p>Assembly Language Debugging with gdb.</p>
<h2 id="useful-commands">Useful commands</h2>
<ul>
<li>Signal Handlers</li>
</ul>
<pre><code>info signal SIGUSR1
handle SIGUSR1 noprint nostop
</code></pre>
<ul>
<li>Run gdb in Text User Interface (TUI) mode</li>
</ul>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>gdb <span class="nt">-tui</span> &lt;program&gt;
</code></pre></div></div>
<ul>
<li>Change the layout to Assembly</li>
</ul>
<pre><code>layout asm
</code></pre>
<ul>
<li>Or split the layout to C and Assembly</li>
</ul>
<pre><code>layout split
</code></pre>
<ul>
<li>Apply the following customization</li>
</ul>
<pre><code>set disassembly-flavor att/intel
set print asm-demangle
set disassemble-next-line on  //ask gdb to show us the next instruction every time
</code></pre>
<ul>
<li>Puts break point on main and invoke run</li>
</ul>
<pre><code>start
</code></pre>
<ul>
<li>Examine memory: x/FMT ADDRESS.
<ul>
<li>ADDRESS is an expression for the memory address to examine.</li>
<li>FMT == [NUM][FORMAT][SIZE]
<ul>
<li>[Format] ::= [ o(octal), x(hex), d(decimal), u(unsigned decimal), t(binary), f(float), a(address), i(instruction), c(char), s(string), z(hex, zero padded on the left)].</li>
<li>[Size] ::= [b(byte), h(halfword), w(word), g(giant, 8 bytes)]</li>
<li>The specified number of objects of the specified size are printed according to the format.  If a negative number is specified, memory is examined backward from the address.</li>
<li>Example<pre><code>x/10hb $rsp
x/Ni $pc
</code></pre></li>
</ul>
</li>
</ul>
</li>
<li>Use <code>nexti,stepi</code> instead of <code>next, step</code> which traverse the source lines</li>
<li>Printing registers</li>
</ul>
<pre><code>info registers
info all-registers
info registers regname …
info registers eflags
</code></pre>
<ul>
<li>Printing &amp; setting xmm registers</li>
</ul>
<pre><code>(gdb) print $xmm1
$1 = {
  v4_float = {0, 3.43859137e-038, 1.54142831e-044, 1.821688e-044},
  v2_double = {9.92129282474342e-303, 2.7585945287983262e-313},
  v16_int8 = &quot;\000\000\000\000\3706;\001\v\000\000\000\r\000\000&quot;,
  v8_int16 = {0, 0, 14072, 315, 11, 0, 13, 0},
  v4_int32 = {0, 20657912, 11, 13},
  v2_int64 = {88725056443645952, 55834574859},
  uint128 = 0x0000000d0000000b013b36f800000000
}
</code></pre>
<p>To set values of such registers, you need to tell GDB which view of the register you wish to change, as if you were assigning value to a struct member:</p>
<pre><code> (gdb) set $xmm1.uint128 = 0x000000000000000000000000FFFFFFFF
</code></pre>
<h2 id="references">References</h2>
<ul>
<li><a href="https://sourceware.org/gdb/onlinedocs/gdb/index.html#SEC_Contents">Debugging with GDB</a></li>
</ul>]]></content><author><name>Sandeep Dasgupta</name><email>sandigame.123@gmail.com</email></author><category term="Assembly" /><category term="Decompilation" /><summary type="html"><![CDATA[Assembly Language Debugging with gdb.]]></summary></entry></feed>