Prostendo A simple yet classy theme for your Jekyll website or blog. https://gladdy.github.io/ Sat, 17 Nov 2018 01:30:57 +0000 Sat, 17 Nov 2018 01:30:57 +0000 Jekyll v3.7.4 A difference in error messages between GCC and Clang <p>C++ can be fairly complicated language and it doesn’t help that the compiler warnings aren’t always the most helpful. As an example, take the code below, it’s a distilled summary illustrating the problem, and it took me a fair amount of time to work out why g++ was rejecting it.</p> <figure class="highlight"><pre><code class="language-c--" data-lang="c++"><span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">T</span><span class="o">&gt;</span> <span class="k">class</span> <span class="nc">proc</span> <span class="p">{</span> <span class="k">struct</span> <span class="n">inner</span> <span class="p">{</span> <span class="k">enum</span> <span class="k">class</span> <span class="nc">mode</span> <span class="p">{</span> <span class="n">A</span><span class="p">,</span> <span class="n">B</span> <span class="p">}</span> <span class="n">m</span><span class="p">;</span> <span class="p">};</span> <span class="n">inner</span> <span class="o">*</span><span class="nf">impl</span><span class="p">(</span><span class="n">inner</span><span class="o">::</span><span class="n">mode</span> <span class="n">m</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nb">nullptr</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span><span class="o">:</span> <span class="kt">void</span> <span class="n">do_stuff</span><span class="p">()</span> <span class="p">{</span> <span class="k">auto</span> <span class="n">p</span> <span class="o">=</span> <span class="n">impl</span><span class="p">(</span><span class="n">inner</span><span class="o">::</span><span class="n">mode</span><span class="o">::</span><span class="n">A</span><span class="p">);</span> <span class="p">}</span> <span class="p">};</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">**</span><span class="n">argv</span><span class="p">)</span> <span class="p">{</span> <span class="n">proc</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">p</span><span class="p">;</span> <span class="n">p</span><span class="p">.</span><span class="n">do_stuff</span><span class="p">();</span> <span class="p">}</span></code></pre></figure> <h2 id="the-error-according-to-gcc">The error according to GCC</h2> <figure class="highlight"><pre><code class="language-bash" data-lang="bash">martijn@martijn-laptop:~/Software<span class="nv">$ </span>g++ file.cpp file.cpp:7:9: error: expected ‘<span class="p">;</span>’ at end of member declaration inner <span class="k">*</span> impl<span class="o">(</span>inner::mode m<span class="o">)</span> <span class="o">{</span> ^~~~ <span class="p">;</span> file.cpp:7:25: error: expected ‘<span class="o">)</span>’ before ‘m’ inner <span class="k">*</span> impl<span class="o">(</span>inner::mode m<span class="o">)</span> <span class="o">{</span> ~ ^~ <span class="o">)</span> file.cpp: In instantiation of ‘void proc&lt;T&gt;::do_stuff<span class="o">()</span> <span class="o">[</span>with T <span class="o">=</span> int]’: file.cpp:18:12: required from here file.cpp:12:6: error: expression cannot be used as a <span class="k">function </span>auto p <span class="o">=</span> impl<span class="o">(</span>inner::mode::A<span class="o">)</span><span class="p">;</span> ^</code></pre></figure> <h2 id="the-error-according-to-clang">The error according to clang</h2> <figure class="highlight"><pre><code class="language-bash" data-lang="bash">martijn@martijn-laptop:~/Software<span class="nv">$ </span>clang++ file.cpp file.cpp:7:14: error: missing <span class="s1">'typename'</span> prior to dependent <span class="nb">type </span>name <span class="s1">'inner::mode'</span> inner <span class="k">*</span> impl<span class="o">(</span>inner::mode m<span class="o">)</span> <span class="o">{</span> ^~~~~~~~~~~ typename 1 error generated.</code></pre></figure> <p>Short and to the point - but most importantly, it gives a valuable suggestion as to what is wrong in the code. In this case, because the <code class="highlighter-rouge">class proc</code> is actually templated, the type of <code class="highlighter-rouge">inner::mode</code> depends on what the type of T is (hence it is called a dependent type). What threw me off here is that the <code class="highlighter-rouge">struct inner</code> was only kept as an inner struct as an utility in the implementation of the class and in fact, does not depend on T at all. However, as it is an inner class, when it has to assign a symbol to <code class="highlighter-rouge">struct inner</code> it will assign it <code class="highlighter-rouge">proc&lt;T&gt;::inner</code>, for whatever values T gets instantiated as. This means that from the compilers perspective it has to be a dependent type - and hence the typename is required to resolve the ambiguity that might otherwise arise.</p> Fri, 16 Nov 2018 21:10:32 +0000 https://gladdy.github.io/code/2018/11/16/GCCErrorMessages.html https://gladdy.github.io/code/2018/11/16/GCCErrorMessages.html cpp code Setting up the Altera Quartus JTAG Programmer on Ubuntu 16.04 <h2 id="preparation">Preparation:</h2> <p>Run <code class="highlighter-rouge">dmesg -w</code> to show you the logstream of USB devices connecting and disconnecting</p> <h2 id="before-launching-quartus">Before launching Quartus</h2> <p>Before starting the JTAG programmer in Quartus, the <code class="highlighter-rouge">dmesg</code> entries should look like this:</p> <figure class="highlight"><pre><code class="language-c" data-lang="c"><span class="p">[</span><span class="mi">27810</span><span class="p">.</span><span class="mo">071135</span><span class="p">]</span> <span class="n">usb</span> <span class="mi">1</span><span class="o">-</span><span class="mi">1</span><span class="p">.</span><span class="mi">2</span><span class="o">:</span> <span class="n">new</span> <span class="n">high</span><span class="o">-</span><span class="n">speed</span> <span class="n">USB</span> <span class="n">device</span> <span class="n">number</span> <span class="mi">18</span> <span class="n">using</span> <span class="n">ehci</span><span class="o">-</span><span class="n">pci</span> <span class="p">[</span><span class="mi">27810</span><span class="p">.</span><span class="mi">163900</span><span class="p">]</span> <span class="n">usb</span> <span class="mi">1</span><span class="o">-</span><span class="mi">1</span><span class="p">.</span><span class="mi">2</span><span class="o">:</span> <span class="n">New</span> <span class="n">USB</span> <span class="n">device</span> <span class="n">found</span><span class="p">,</span> <span class="n">idVendor</span><span class="o">=</span><span class="mi">09</span><span class="n">fb</span><span class="p">,</span> <span class="n">idProduct</span><span class="o">=</span><span class="mi">6810</span> <span class="p">[</span><span class="mi">27810</span><span class="p">.</span><span class="mi">163905</span><span class="p">]</span> <span class="n">usb</span> <span class="mi">1</span><span class="o">-</span><span class="mi">1</span><span class="p">.</span><span class="mi">2</span><span class="o">:</span> <span class="n">New</span> <span class="n">USB</span> <span class="n">device</span> <span class="n">strings</span><span class="o">:</span> <span class="n">Mfr</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">Product</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">SerialNumber</span><span class="o">=</span><span class="mi">0</span></code></pre></figure> <h2 id="start-the-quartus-programmer">Start the Quartus Programmer</h2> <p>Starting the programmer will do 2 things:</p> <ul> <li>Load the JTAG server (<code class="highlighter-rouge">jtagd --user-start --config ~/.jtagd.conf</code>)</li> <li>Load the driver for the USB connection</li> </ul> <figure class="highlight"><pre><code class="language-c" data-lang="c"><span class="p">[</span><span class="mi">28122</span><span class="p">.</span><span class="mi">900173</span><span class="p">]</span> <span class="n">usb</span> <span class="mi">1</span><span class="o">-</span><span class="mi">1</span><span class="p">.</span><span class="mi">2</span><span class="o">:</span> <span class="n">new</span> <span class="n">high</span><span class="o">-</span><span class="n">speed</span> <span class="n">USB</span> <span class="n">device</span> <span class="n">number</span> <span class="mi">25</span> <span class="n">using</span> <span class="n">ehci</span><span class="o">-</span><span class="n">pci</span> <span class="p">[</span><span class="mi">28123</span><span class="p">.</span><span class="mo">010</span><span class="mi">811</span><span class="p">]</span> <span class="n">usb</span> <span class="mi">1</span><span class="o">-</span><span class="mi">1</span><span class="p">.</span><span class="mi">2</span><span class="o">:</span> <span class="n">New</span> <span class="n">USB</span> <span class="n">device</span> <span class="n">found</span><span class="p">,</span> <span class="n">idVendor</span><span class="o">=</span><span class="mi">09</span><span class="n">fb</span><span class="p">,</span> <span class="n">idProduct</span><span class="o">=</span><span class="mi">6010</span> <span class="p">[</span><span class="mi">28123</span><span class="p">.</span><span class="mo">010</span><span class="mi">818</span><span class="p">]</span> <span class="n">usb</span> <span class="mi">1</span><span class="o">-</span><span class="mi">1</span><span class="p">.</span><span class="mi">2</span><span class="o">:</span> <span class="n">New</span> <span class="n">USB</span> <span class="n">device</span> <span class="n">strings</span><span class="o">:</span> <span class="n">Mfr</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">Product</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">SerialNumber</span><span class="o">=</span><span class="mi">3</span> <span class="p">[</span><span class="mi">28123</span><span class="p">.</span><span class="mo">010</span><span class="mi">822</span><span class="p">]</span> <span class="n">usb</span> <span class="mi">1</span><span class="o">-</span><span class="mi">1</span><span class="p">.</span><span class="mi">2</span><span class="o">:</span> <span class="n">Product</span><span class="o">:</span> <span class="n">DE</span><span class="o">-</span><span class="n">SoC</span> <span class="p">[</span><span class="mi">28123</span><span class="p">.</span><span class="mo">010</span><span class="mi">825</span><span class="p">]</span> <span class="n">usb</span> <span class="mi">1</span><span class="o">-</span><span class="mi">1</span><span class="p">.</span><span class="mi">2</span><span class="o">:</span> <span class="n">Manufacturer</span><span class="o">:</span> <span class="n">Altera</span> <span class="p">[</span><span class="mi">28123</span><span class="p">.</span><span class="mo">010</span><span class="mi">828</span><span class="p">]</span> <span class="n">usb</span> <span class="mi">1</span><span class="o">-</span><span class="mi">1</span><span class="p">.</span><span class="mi">2</span><span class="o">:</span> <span class="n">SerialNumber</span><span class="o">:</span> <span class="n">DE</span><span class="o">-</span><span class="n">SoC</span><span class="o">-</span><span class="mo">003</span><span class="o">-</span><span class="mo">03503</span></code></pre></figure> <h2 id="unable-to-select-the-device-in-the-quartus-programmer">Unable to select the device in the Quartus programmer?</h2> <p>The most likely issue is that currently, you do not have permission to access the device, as by default, USB devices are only accessible as root. Luckily, (as long as you have root access in your system), you can fix this.</p> <ol> <li> <p>Figure out where the USB device lives on the filesystem. In UNIX, everything is a file, including USB devices. Go to <code class="highlighter-rouge">/dev/bus/usb</code>, where you will find several folders. The first level indicates the (internal) USB hub, the second level the device number. As seen before, my device has number 25, therefore there is only 1 candidate: <code class="highlighter-rouge">/dev/bus/usb/001/025</code>.</p> </li> <li> <p>Set read-write permissions for all users on that file, eg <code class="highlighter-rouge">sudo chmod 666 /dev/bus/usb/001/025</code>. This has to be done every time you plug in your FPGA, which can get very tedious.</p> </li> <li> <p>Luckily, the linux kernel devs have thought of this and added a feature called udev rules. These are commands that are run, as root, when a USB device of a specific signature connects. Create the file <code class="highlighter-rouge">/etc/udev/rules.d/52-usbblaster.rules</code>. Any number between 0 and 100 and name will do but usually the high and low 10 are reserved for system purposes. Do make sure it adheres to the format <code class="highlighter-rouge">{number}-{name}.rules</code>. In this file create one udev entry. Mine is shown below. Ensure that the idVendor and idProduct actually match the codes displayed in your <code class="highlighter-rouge">dmesg</code> output. The rest of the entry is self-explanatory. Finally, restart the udev service by <code class="highlighter-rouge">sudo service udev restart</code>, forcing it to reload the udev rules.</p> </li> <li> <p>Check whether your udev rules has worked. Replug your USB connection and verify that your device now has read and write permissions for all users, eg:</p> </li> </ol> <figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c"># The USB Blaster UDEV entry in /etc/udev/rules.d/52-usbblaster.rules</span> <span class="nv">SUBSYSTEM</span><span class="o">==</span><span class="s2">"usb"</span>, ATTR<span class="o">{</span>idVendor<span class="o">}==</span><span class="s2">"09fb"</span>, ATTR<span class="o">{</span>idProduct<span class="o">}==</span><span class="s2">"6010"</span>, <span class="nv">MODE</span><span class="o">=</span><span class="s2">"0666"</span></code></pre></figure> <figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nv">$ </span>tree /dev/bus/usb <span class="nb">.</span> ├── 001 │   ├── 001 │   ├── 002 │   ├── 003 │   ├── 005 │   ├── 013 │   └── 025 ├── 002 │   ├── 001 │   └── 002 ├── 003 │   └── 001 └── 004 └── 001</code></pre></figure> <figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c"># After successfully applying the udev rule or manually setting the permission</span> <span class="nb">ls</span> <span class="nt">-lh</span> /dev/bus/usb/001 crw-rw-r-- 1 root root 189, 0 Mar 18 21:22 001 crw-rw-r-- 1 root root 189, 1 Mar 18 21:22 002 crw-rw-r-- 1 root root 189, 2 Mar 18 21:22 003 crw-rw-r-- 1 root root 189, 4 Mar 18 21:22 005 crw-rw-r-- 1 root root 189, 12 Mar 18 21:22 013 crw-rw-rw- 1 root root 189, 35 Mar 18 21:27 036 <span class="c"># all users have read/write permission</span></code></pre></figure> <h2 id="any-further-issues">Any further issues?</h2> <p>I believe this should be a comprehensive guide for getting Altera FPGAs working on Ubuntu 16.04. If you still have issues, please leave a comment so we can resolve it and add it to the guide.</p> Sat, 18 Mar 2017 21:40:19 +0000 https://gladdy.github.io/2017/03/18/Altera-udev.html https://gladdy.github.io/2017/03/18/Altera-udev.html fpga hardware altera Context switching on an AVR <p>Modern operating systems are run on are complicated beasts. They consist of millions of lines of code, providing a wide range of functionality. At the core of these tasks is the ‘kernel’. This core section of the operating system has a single goal: providing a machine-independent abstraction of the hardware. The rest of the operating system and other applications can then be built on top of the kernel to use this interface, meaning that applications should run correctly on every machine type supported by the operating system kernel.</p> <p><a href="https://upload.wikimedia.org/wikipedia/commons/thumb/8/8f/Kernel_Layout.svg/220px-Kernel_Layout.svg.png" data-lightbox="kernel-large" data-title="Applications run on top of the kernel, which abstracts away the underlying hardware."> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/8f/Kernel_Layout.svg/220px-Kernel_Layout.svg.png" title="Applications run on top of the kernel, which abstracts away the underlying hardware." /> </a></p> <p>When using a computer, the functionality of the kernel is hidden away as much as possible - it’s merely there to provide services to applications. However, in order to really understand what the kernel does (or could be doing), it can be useful to implement a kernel for yourself.</p> <p>Kernels are big and complicated so it immediately raises a question: what section of a kernel should we create? The diagram above splits out the hardware into three sections, CPU, memory and devices (eg. keyboard, mouse, display or speakers). Let’s focus on the first one - the processor. From a high level overview processors are simple. You feed them instructions, and those get executed. The tricky bits are in the details. How do you make the CPU load instructions when it isn’t on yet? Now it’s on, do you always run your CPU at full speed, or do you occasionally turn sections off to preserve battery power? What do you do if this specific CPU does not support an instruction used in the program? Lastly, kernels can execute more processses simultaneoulsy than the machine as processors. How do you make (it appear to) do so?</p> <h3 id="context-switching">Context switching</h3> <p>The answer to the last question is deceptively simple. Assume we have some set of programs and a single processor that can execute one program. We start it off by telling it to execute the first program in the set. After a few milliseconds, we interrupt the processor and load the second program. Again, a few milliseconds later, we interrupt the processor and load the third program. This would work fine, assuming that programs only run for a few milliseconds - but in reality this is almost never the case. Therefore, after we’ve run every program in the set for a few milliseconds, we can start back at the beginning, giving each some more time before, again, being interrupted. We are essentially dividing up the full program execution of every program into little sections and interleaving those on a single processor, giving the appearance of all of them running simultaneously. However, the user application may have no idea that this is happening and therefore, we need to take care to preserve the state of the program. After each execution section, we store the state of the program to memory. This way, when the program gets its next bit of execution time, we can load the old state and start again where we left off. This makes the process transparent for the running program. As far as it knows, it simply never stopped executing as all values in the program are exactly the same, even though the processor has been executing a different program in the meantime.</p> <p>This process is known as context switching, in which ‘context’ refers to the state of the program. This state is described by the internal state of the processor, meaning the registers. These contain often-used variables and information about the current execution, eg. what is the next instruction I should be executing. The memory (RAM) used by the process is usually not included as the ‘context’ of the process, but that is because every process already operates on its own bit of memory - which does not cause clashes when swapping out the executing program during a context switch.</p> <h2 id="why-on-avr">Why on AVR?</h2> <p>To be continued with as much operating system as I can fit on an AVR - stay tuned.</p> Fri, 27 Jan 2017 14:50:19 +0000 https://gladdy.github.io/code/2017/01/27/AVR-contextswitch.html https://gladdy.github.io/code/2017/01/27/AVR-contextswitch.html c hardware avr code Hello again Thu, 26 Jan 2017 14:50:19 +0000 https://gladdy.github.io/2017/01/26/More-posts.html https://gladdy.github.io/2017/01/26/More-posts.html Demystifying Node.js <p>Node.js: it’s quick to write, easy to understand and performs very well in applications which are I/O bound for performance. The high level of abstraction offered by Node.js also has a disadvantage. Without looking at the C source code, which is something I cannot imagine many Node.js developers will do, the inner workings can appear to be almost magic. Node.js handles all the nitty-gritty low-level stuff for you: ensuring that the proper callbacks are executed at the proper time with proper values for the variables. This is great and all, but if the server application has to perform actual computation the performance suffers due to the interpreted and dynamic nature of JavaScript. The usual choice is to write the high-performance part a different language which can be compiled to an object file (C/C++) and then add the compiled program as an addon, a process which is documented <a href="https://nodejs.org/api/addons.html">here</a>.</p> <h3 id="why">Why?</h3> <p>As a little side weekend-long side project, I decided to rewrite the underlying code of a small subset of Node.js in C++ without the use of any external libraries. One might wonder: why? Node.js already works perfectly well, but as previously mentioned the high abstraction level distracts from the underlying and supporting mechanisms and one of the goals is to display a simplified version of the inner workings of Node.js. Furthermore, by writing the top-level code in C++ over JavaScript it becomes trivial to perform high-speed computation directly in the server application. Lastly, it’s similar to the early days of Facebooks’ HipHop PHP compiler, which took their entire PHP codebase and generated a single binary server application. This, optimized, single-binary approach obviously has advantages in performance, ease of dependency management and deployment. Lastly, C++ is known for being very complex which comes with the huge amount of control you have and it includes rules which might be hard to grasp. JavaScript is quite the opposite. However, C++ does not have to be hugely complex which is why I’m trying to mimic the easy-to-understand nature of JavaScript in vanilla C++ (that is, without any <code class="highlighter-rouge">#define</code>s to hide some syntax).</p> <h3 id="restrictions">Restrictions</h3> <p>My own restriction to avoid external libraries and keep the codebase simple has some consequences. It would have been easy to use Boost.ASIO or libuv (which is used internally by Node.js). However, not relying on libraries entails writing a custom asynchronous engine which can be very complicated and hard to understand. Therefore, I decided to use a very much simplified engine, which does not support arbitrary nesting of callback functions. This has some consequences for the designs, but the arising problem: keeping track of state can be solved by using an external structure containing some additional data for each socket connection. Furthermore, low-level concepts such as UNIX socket handling and threading in C++ will also appear in this design.</p> <h3 id="javascript-or-c">JavaScript or C++?</h3> <style type="text/css"> pre { font-size: 12px; } </style> <div class="row"> <div class="col-md-6"> <figure class="highlight"><pre><code class="language-c--" data-lang="c++"><span class="n">TCPServer</span> <span class="nf">server</span> <span class="p">([</span><span class="o">&amp;</span><span class="p">](</span><span class="n">Socket</span> <span class="o">&amp;</span><span class="n">socket</span><span class="p">)</span> <span class="p">{</span> <span class="n">console</span><span class="p">.</span><span class="n">log</span><span class="p">(</span><span class="s">"Client connected"</span><span class="p">);</span> <span class="n">console</span><span class="p">.</span><span class="n">log</span><span class="p">(</span><span class="s">"address: "</span> <span class="o">+</span> <span class="n">socket</span><span class="p">.</span><span class="n">remoteAddress</span><span class="p">());</span> <span class="n">console</span><span class="p">.</span><span class="n">log</span><span class="p">(</span><span class="s">"port: "</span> <span class="o">+</span> <span class="n">socket</span><span class="p">.</span><span class="n">remotePort</span><span class="p">());</span> <span class="n">socket</span><span class="p">.</span><span class="n">on</span><span class="p">(</span><span class="s">"data"</span><span class="p">,</span> <span class="p">[</span><span class="o">&amp;</span><span class="p">,</span> <span class="n">socket</span><span class="p">](</span><span class="n">Data</span> <span class="n">data</span><span class="p">)</span> <span class="k">mutable</span> <span class="p">{</span> <span class="c1">//Handle the received data </span> <span class="p">});</span> <span class="n">socket</span><span class="p">.</span><span class="n">on</span><span class="p">(</span><span class="s">"end"</span><span class="p">,</span> <span class="p">[</span><span class="o">&amp;</span><span class="p">,</span> <span class="n">socket</span><span class="p">](){</span> <span class="c1">//Handle the client disconnect </span> <span class="p">});</span> <span class="p">});</span> <span class="n">server</span><span class="p">.</span><span class="n">listen</span><span class="p">(</span><span class="mi">1338</span><span class="p">);</span></code></pre></figure> </div> <div class="col-md-6"> <figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kd">var</span> <span class="nx">server</span> <span class="o">=</span> <span class="nx">net</span><span class="p">.</span><span class="nx">createServer</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">socket</span><span class="p">){</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"Client connected"</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"address: "</span> <span class="o">+</span> <span class="nx">socket</span><span class="p">.</span><span class="nx">remoteAddress</span><span class="p">);</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"port: "</span> <span class="o">+</span> <span class="nx">socket</span><span class="p">.</span><span class="nx">remotePort</span><span class="p">);</span> <span class="nx">socket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">"data"</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">){</span> <span class="c1">//Handle client message</span> <span class="p">});</span> <span class="nx">socket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">"end"</span><span class="p">,</span> <span class="kd">function</span><span class="p">(){</span> <span class="c1">//Handle client abort</span> <span class="p">});</span> <span class="p">});</span> <span class="nx">server</span><span class="p">.</span><span class="nx">listen</span><span class="p">(</span><span class="mi">1338</span><span class="p">)</span></code></pre></figure> </div> </div> <div class="row text-center"> Spot the differences! </div> <p><br /> At first glance the snippet above appears to be JavaScript. It’s easy to write and easy to understand. Almost everything is specified in terms of callback functions. It features the familiar Node.js pattern of `socket.on(“event”, callback). However, it is not JavaScript. It’s C++. The most notable difference between JavaScript and C++ in this snippet is the syntax for functions, which are first-order citizens in JavaScript whereas C++ has to rely on its implementation of lambda functions. The syntax is quite a lot more verbose in C++, but in turn it offers a lot more control!</p> Sun, 26 Jul 2015 21:10:32 +0000 https://gladdy.github.io/code/2015/07/26/NodeDemystified.html https://gladdy.github.io/code/2015/07/26/NodeDemystified.html cpp nodejs code End-to-end encrypted messaging <h2 id="description-for-an-end-to-end-encrypted-datagram-service-supporting-multicast">Description for an end-to-end encrypted datagram service supporting multicast</h2> <h3 id="features">Features</h3> <ul> <li>low network overhead for the security offered <ul> <li>~10 bytes for every group member</li> <li>~250bytes for an encrypted public key for multicast</li> <li>~250bytes for the signature (similar to SSL/TLS)</li> </ul> </li> <li>supports multiple recipients with minimal bandwidth overhead</li> <li>anonymous message sources, unknown message content (as seen by the managing servers)</li> <li>signed messages</li> <li>only the endpoints know the group compositions, none of servers do</li> </ul> <h3 id="introduction">Introduction</h3> <h4 id="node-overview">Node overview</h4> <ul> <li>S : Source, the device sending the message</li> <li>P1 : First proxy (randomly chosen by S)</li> <li>P2 : Second proxy (responsible for the message division, specified by S to P1)</li> <li>CS : All user devices have a connection to this node in order to receive messages</li> <li>T : Target, the destination of the message</li> </ul> <h4 id="scrambling">Scrambling</h4> <ol> <li>Generate a temporary key</li> <li>Encode the payload with the key</li> <li>Prepend the key to the payload</li> <li>Encrypt the key and payload combination with a supplied public key</li> </ol> <h4 id="setup">Setup:</h4> <ul> <li>All devices keep a connection with the central server for receiving messages.</li> <li>All devices not behind a NAT (ie. servers) without bandwidth limits (ie. mobile devices) act as proxies.</li> <li>Get data from the central server: its public key and a list of proxies with their public keys.</li> <li>Generate a local key pair for signing.</li> </ul> <h3 id="operations">Operations</h3> <h4 id="creating-a-group">Creating a group</h4> <ol> <li>generate a key pair locally</li> <li>add yourself to your local recipient list, which contains the ids and public keys of each member</li> </ol> <h4 id="adding-a-user">Adding a user</h4> <h5 id="the-user-already-in-the-group">The user already in the group</h5> <ol> <li>prompt for a target id to add to the group</li> <li>prompt for a passphrase</li> <li>encode the group key pair and recipient list with the passphrase</li> <li>send the message to the target id</li> </ol> <h5 id="the-user-being-added">The user being added</h5> <ol> <li>Sees that he has received a message for which none of his private keys work</li> <li>prompt for a passphrase</li> <li>if it works, add the recipient list and private key to its cache</li> <li>for every recipient except itself send scrambled with private_key<sub>G</sub> <ul> <li>the updated recipient list</li> <li>the new users public key</li> </ul> </li> </ol> <h4 id="sending-a-message">Sending a message</h4> <ol> <li>Contents of the initial message, which are sent from S to random proxy P1 <ul> <li>The message, signed with the private_key<sub>S</sub>, encrypted with public_key<sub>G</sub></li> <li>S encrypted with public_key<sub>G</sub></li> <li>The recipients, encryped with the public_key<sub>CS</sub></li> <li>A specification for which P2 to use (random)</li> <li>public_key<sub>G</sub> encoded with public_key<sub>P2</sub></li> </ul> </li> <li>P1 forwards the message to P2, contents: <ul> <li>The message encrypted with public_key<sub>G</sub></li> <li>S encrypted with public_key<sub>G</sub></li> <li>The recipients, encryped with public_key<sub>CS</sub></li> <li>public_key<sub>G</sub> encoded with public_key<sub>P2</sub></li> </ul> </li> <li>P2 splits the message into one for every recipient, contents of a single message: <ul> <li>The message encrypted with public_key<sub>G</sub></li> <li>One of the recipients, still encrypted with public_key<sub>CS</sub></li> </ul> </li> <li>P2 scrambles the payload of every message using the groups public key (which gets decoded with the private key of P2)</li> <li>P2 scrambles the entire message with the public_key<sub>CS</sub></li> <li>P2 sends every message to CS (possibly through a randomized additional proxy if there is low traffic to avoid traffic pattern detection)</li> <li>CS descrambles and then decodes the destition of all messages and sends them to the different T</li> <li>T decodes the message using private_key<sub>G</sub> and descrambles (step 4), decodes again (step 1) and obtains the cleartext.</li> <li>T verifies whether the signature is correct and passes on the message to the receiving application</li> </ol> <h3 id="who-knows-what">Who knows what?</h3> <h4 id="s">S</h4> <ul> <li>Message contents</li> <li>All recipients</li> <li>Message signature</li> <li>Choice of P1 and P2</li> </ul> <h4 id="p1">P1</h4> <ul> <li>S</li> <li>Choice of P1 and P2</li> </ul> <h4 id="p2">P2</h4> <ul> <li>Choice of P1 and P2</li> <li>Group public key</li> </ul> <h4 id="cs">CS</h4> <ul> <li>Choice of P2</li> <li>A single recipient</li> </ul> <h4 id="t">T</h4> <ul> <li>Message contents</li> <li>Message origin</li> <li>Message signature</li> </ul> <h3 id="possible-problems">Possible problems:</h3> <ul> <li>Source bandwidth increases with group size (if every group member would use their own key pair for encryption) <ul> <li>Fixed by adding the shared group key pair</li> </ul> </li> <li>Keeping participants anonymous whilst verifying their authenticity <ul> <li>Fixed by the requirement for a passphrase when adding people (which has to be distributed over another secure channel)</li> </ul> </li> <li>Sending data to anonymous receivers <ul> <li>No need for anonymous receivers due unknown message contents and anonymous transmitters</li> </ul> </li> <li>Latency due to a triple proxy (P1, P2, CS) <ul> <li>can be shortened to a double proxy by sacrificing multicast possibilities (eliminating P2)</li> <li>But the double proxy still ensures anonymity</li> </ul> </li> <li>If you have control over the paths between P1, P2 and CS you can trace the path of the packet. <ul> <li>Their input and output content at P2 is entirely scrambled and combined with a high traffic level and a short, randomized wait, this prevents traffic flow analysis.</li> </ul> </li> <li>If you have control over all servers you can trace the path of the packet <ul> <li>Indeed. As long as not all the servers get hijacked the system is still anonymous though and due to the random choice of P1 and P2.</li> <li>The content of the packet will always remain unknown to the hijackers due to the end-to-end encryption.</li> </ul> </li> </ul> Sat, 04 Jul 2015 00:33:42 +0000 https://gladdy.github.io/thoughts/2015/07/04/encryption.html https://gladdy.github.io/thoughts/2015/07/04/encryption.html encryption thoughts FPGAs? <h3 id="vhdl-hardware-design-could-be-more-streamlined">VHDL: hardware design could be more streamlined!</h3> <p>This is a VHDL (VHSIC Hardware Description Language (Very High Speed Integrated Circuit) - so much for concise abbreviations) snippet which synthesizes to a module which simply passes on 4 wires to some external LED output, synchronized to a central clock.</p> <figure class="highlight"><pre><code class="language-vhdl" data-lang="vhdl"><span class="k">library</span> <span class="n">ieee</span><span class="p">;</span> <span class="k">use</span> <span class="n">ieee</span><span class="o">.</span><span class="n">std_logic_1164</span><span class="o">.</span><span class="k">all</span><span class="p">;</span> <span class="k">use</span> <span class="n">ieee</span><span class="o">.</span><span class="n">numeric_std</span><span class="o">.</span><span class="k">all</span><span class="p">;</span> <span class="k">ENTITY</span> <span class="n">io_led</span> <span class="k">IS</span> <span class="k">PORT</span><span class="p">(</span> <span class="n">clock</span> <span class="p">:</span> <span class="k">in</span> <span class="kt">std_logic</span><span class="p">;</span> <span class="n">reset</span> <span class="p">:</span> <span class="k">in</span> <span class="kt">std_logic</span><span class="p">;</span> <span class="n">leds_status</span> <span class="p">:</span> <span class="k">in</span> <span class="kt">std_logic_vector</span><span class="p">(</span><span class="mi">3</span> <span class="k">downto</span> <span class="mi">0</span><span class="p">);</span> <span class="n">leds_output</span> <span class="p">:</span> <span class="k">out</span> <span class="kt">std_logic_vector</span><span class="p">(</span><span class="mi">3</span> <span class="k">downto</span> <span class="mi">0</span><span class="p">)</span> <span class="p">);</span> <span class="k">END</span> <span class="n">io_led</span><span class="p">;</span> <span class="k">ARCHITECTURE</span> <span class="n">behaviour</span> <span class="k">OF</span> <span class="n">io_led</span> <span class="k">IS</span> <span class="k">BEGIN</span> <span class="k">PROCESS</span><span class="p">(</span><span class="n">clock</span><span class="p">)</span> <span class="k">BEGIN</span> <span class="k">IF</span> <span class="n">rising_edge</span><span class="p">(</span><span class="n">clock</span><span class="p">)</span> <span class="k">THEN</span> <span class="n">leds_output</span> <span class="o">&lt;=</span> <span class="n">leds_status</span><span class="p">;</span> <span class="k">END</span> <span class="k">IF</span><span class="p">;</span> <span class="k">END</span> <span class="k">PROCESS</span><span class="p">;</span> <span class="k">END</span><span class="p">;</span></code></pre></figure> Tue, 26 May 2015 14:54:32 +0000 https://gladdy.github.io/code/2015/05/26/Synthesis.html https://gladdy.github.io/code/2015/05/26/Synthesis.html vhdl hardware code The missing delete <figure class="highlight"><pre><code class="language-c--" data-lang="c++"><span class="kt">void</span> <span class="n">ClipShareRunner</span><span class="o">::</span><span class="n">readFromSocket</span><span class="p">(</span><span class="n">QString</span> <span class="n">str</span><span class="p">)</span> <span class="p">{</span> <span class="n">updatingClipboard</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span> <span class="n">QMimeData</span> <span class="o">*</span> <span class="n">mimeData</span> <span class="o">=</span> <span class="k">new</span> <span class="n">QMimeData</span><span class="p">();</span> <span class="cm">/* Process QString str and add to the information to the QMimeData object. */</span> <span class="n">QApplication</span><span class="o">::</span><span class="n">clipboard</span><span class="p">()</span><span class="o">-&gt;</span><span class="n">setMimeData</span><span class="p">(</span><span class="n">mimeData</span><span class="p">);</span> <span class="n">updatingClipboard</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span> <span class="p">}</span></code></pre></figure> <p>Any C/C++ programmer who glances over this piece of code will figure out quite quickly that it has some unintended consequences: a memory leak caused by <code class="highlighter-rouge">mimeData = new QMimeData()</code> without ever calling a corresponding <code class="highlighter-rouge">delete mimeData;</code>. That’s the general pattern for allocating persistent memory on the heap: <code class="highlighter-rouge">new</code> and <code class="highlighter-rouge">delete</code>. Modern C++ is very much fond of its RAII principle (Resource Acquisition Is Initialization), an obscure term for a simple principle. Put your <code class="highlighter-rouge">new</code>s in the constructor, put your <code class="highlighter-rouge">delete</code>s in your destructor and you are almost guaranteed to never ever have a resource leak.</p> <p>However, in this case you are not just passing the pointer for the new content of the clipboard, you are also (implicitly) passing on the ownership of the data the pointer points to. Therefore, it is not our responsibility any more to clean up after ourselves. If you attempt to and the object has already been moved, you are greeted by a familiar opponent: the double free error.</p> <p>We’re only left with <code class="highlighter-rouge">new</code>. The corresponding <code class="highlighter-rouge">delete</code> is nowhere to be found in our code. Its instruction will be buried somewhere deep inside some compiled library binary, never to be seen again by people: just a few bytes in a sea of apparent pseudo-randomness, making sure that your computer does not crash if you have placed things on the clipboard too many times. Thank you bytes.</p> <p>In retrospect, the reasoning behind also implicitly passing the ownership of the data and thereby the responsibility to delete it is completely sound: Only the operating system is able to keep track of whenever the clipboard and thereby its mimeData object have been reassigned. In current machines with huge surplusses of memory reallocating is not that efficient anymore: deleting the old object and just allocating a new one. Therefore there is no way to know for the application where the clipboard object has gone and as such it will not be able to clean up after itself.</p> Mon, 25 May 2015 23:25:23 +0000 https://gladdy.github.io/code/2015/05/25/MissingDelete.html https://gladdy.github.io/code/2015/05/25/MissingDelete.html cpp code