<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>eshikafe.github.io</title>
    <description>Personal page</description>
    <link>https://eshikafe.github.io/</link>
    <atom:link href="https://eshikafe.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sat, 01 Jul 2023 10:45:57 +0000</pubDate>
    <lastBuildDate>Sat, 01 Jul 2023 10:45:57 +0000</lastBuildDate>
    <generator>Jekyll v3.9.3</generator>
    
      <item>
        <title>Deploying Your Rust Apps in the Cloud</title>
        <description>&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;In this article, I provide a step-by-step guide on how you can easily deploy your Rust application in the cloud with &lt;a href=&quot;https://aws.amazon.com/ec2/&quot;&gt;Amazon AWS EC2&lt;/a&gt;. Depending on your system requirements, you can deploy your application in the cloud to save you the hassle of managing the hardware infrastructure yourself so you can focus on what matters - your application and the customers they serve.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;I am assuming you already have an AWS account, if not please create one and login as root user. We will be using the AWS EC2 Free Tier &lt;em&gt;t3.micro&lt;/em&gt; compute, as our virtual server in the cloud.&lt;/p&gt;

&lt;h2 id=&quot;step-1&quot;&gt;Step 1&lt;/h2&gt;

&lt;p&gt;After logging in as root to your AWS account, switch to the new view and nagivate to EC2 as shown in the screenshot below. Select &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Services&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Compute&lt;/code&gt; and then click on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EC2&lt;/code&gt;.
&lt;img src=&quot;https://i.imgur.com/Y1pffNX.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step-2&quot;&gt;Step 2&lt;/h2&gt;

&lt;p&gt;Click on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Launch instances&lt;/code&gt; as shown below.
&lt;img src=&quot;https://i.imgur.com/40nqhzZ.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step-3&quot;&gt;Step 3&lt;/h2&gt;

&lt;p&gt;Under choose AMI, first check the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Free tier only&lt;/code&gt; box on the left side of the window and then select any image. I selected &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ubuntu Server 20.04 LTS&lt;/code&gt; image. You can experiment with any of the images.
&lt;img src=&quot;https://i.imgur.com/kD3zs7B.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step-4&quot;&gt;Step 4&lt;/h2&gt;

&lt;p&gt;Make sure you select the t3.micro (it is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;free tier&lt;/code&gt; instance type) and then click on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Review and Launch&lt;/code&gt; then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Launch&lt;/code&gt;. This will display a window asking you to select or create a key pair. Select the applicable option and then click on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Launch Instance&lt;/code&gt;&lt;br /&gt;
&lt;img src=&quot;https://i.imgur.com/2b2UzFh.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step-5&quot;&gt;Step 5&lt;/h2&gt;

&lt;p&gt;Give your virtual server a name. Mine is named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;server-01&lt;/code&gt;. Wait for your server to change &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Instance state&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Running&lt;/code&gt; before you connect to it.
&lt;img src=&quot;https://i.imgur.com/SE9bQwx.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step-6&quot;&gt;Step 6&lt;/h2&gt;

&lt;p&gt;Now, you can connect to your virtual server using &lt;a href=&quot;https://www.putty.org/&quot;&gt;PuTTy&lt;/a&gt;, Powershell, Bash (if you are using Linux), CMD or any client application that supports &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SSH&lt;/code&gt;. I am using PuTTy. To connect to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;compute&lt;/code&gt; or virtual server, ssh to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Public IPv4 DNS&lt;/code&gt; address specified under the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Details&lt;/code&gt; tab.
&lt;img src=&quot;https://i.imgur.com/U4QAbAw.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step-7&quot;&gt;Step 7&lt;/h2&gt;

&lt;p&gt;The user name is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ubuntu&lt;/code&gt;. The virtual server has access to the internet and you can install any package you need. Now, let’s install the Rust compiler.
&lt;img src=&quot;https://i.imgur.com/0yt7Dy8.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step-8&quot;&gt;Step 8&lt;/h2&gt;

&lt;p&gt;Install Rust and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;buid-essential&lt;/code&gt; (sudo apt install build-essential).
&lt;img src=&quot;https://i.imgur.com/1i1tawo.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step-9&quot;&gt;Step 9&lt;/h2&gt;

&lt;p&gt;We will be deploying two types of application on the virtual server - a telecoms application (a &lt;a href=&quot;https://en.wikipedia.org/wiki/GPRS_Tunnelling_Protocol&quot;&gt;GTPv2-C&lt;/a&gt; server) and a web application based on the &lt;a href=&quot;https://github.com/seanmonstar/warp&quot;&gt;warp&lt;/a&gt; crate. GTPv2C protocol is used in the &lt;a href=&quot;https://www.3gpp.org/&quot;&gt;3GPP&lt;/a&gt; based 3G, 4G and 5G networks for signaling and it uses port 2123 and the &lt;a href=&quot;https://en.wikipedia.org/wiki/User_Datagram_Protocol&quot;&gt;UDP&lt;/a&gt; transport protocol. We will configure our Rust GTP server to listen on UDP port 2123 in compilance with the &lt;a href=&quot;https://www.arib.or.jp/english/html/overview/doc/STD-T63v9_60/5_Appendix/Rel8/29/29274-8b0.pdf&quot;&gt;3GPP TS 29.274&lt;/a&gt; (GTPv2-C) standard.&lt;/p&gt;

&lt;p&gt;The web server will listen on TCP port 3030.&lt;/p&gt;

&lt;p&gt;Configure the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Inbound rules&lt;/code&gt; as shown below. AWS EC2 will apply these rules to our virtual server and it will allow our Rust applications to send and receive messages using these rules.
&lt;img src=&quot;https://i.imgur.com/sBnezgP.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step-10&quot;&gt;Step 10&lt;/h2&gt;

&lt;p&gt;Create your web server application using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo new web_server&lt;/code&gt; and edit with nano as shown below. The original code is available here:https://github.com/seanmonstar/warp and it has been slightly modified for this tutorial. Run the web server with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo run&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After this step, you will have two Rust applications running in the cloud. You can use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ps aux | grep target&lt;/code&gt; to check the two processes running (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gtp&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;web_server&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/rP0uJb2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step-11&quot;&gt;Step 11&lt;/h2&gt;

&lt;p&gt;Open another PuTTy session and connect to the virtual server. Clone the GTPv2-C server from https://github.com/eshikafe/ngc and edit the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ngc/common/gtp/src/main.rs&lt;/code&gt; so that the server listens and accepts conections from any IP address (i.e &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0&lt;/code&gt;) as shown below. Run the application with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo run&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/bS1uYtr.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In the screen shot below, a simple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Python&lt;/code&gt; script (the client) sends a message to the gtp server application using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Public IP address&lt;/code&gt; and port 2123. The script sends a GTPv2-C &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Create Session Request&lt;/code&gt; message from my Windows PC, through the internet, to the Rust GTPv2-C server application running in the cloud. This implies that you can deploy a telecom application (based on Rust) in the cloud to serve multiple devices concurrently and efficiently. The sample gtp application uses the &lt;a href=&quot;https://tokio.rs/&quot;&gt;tokio&lt;/a&gt; crate to achieve concurrency. Concurrency is a key requirement for a 4G/5G telecom server application.
&lt;img src=&quot;https://i.imgur.com/3RaFT6J.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;step-12&quot;&gt;Step 12&lt;/h2&gt;

&lt;p&gt;Lastly, open a web browser and paste the following URL in the address bar:&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{Public IPv4 DNS}:3030/hello/Rustacean&lt;/code&gt;. Replace &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Public IPv4 DNS&lt;/code&gt; with the domain assigned to your compute. You should see the message below.
&lt;img src=&quot;https://i.imgur.com/qIlyhO4.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You can also use curl to test your web server as shown below.
&lt;img src=&quot;https://i.imgur.com/SfZwGE3.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;To deploy your applications securely in the cloud, you need to understand how the &lt;a href=&quot;https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-security-groups.html&quot;&gt;AWS EC2 security group&lt;/a&gt; works. Please read the AWS EC2 security group documentation before you deploy your application as a production system in the cloud.&lt;/p&gt;

&lt;h2 id=&quot;reference&quot;&gt;Reference&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=7mgBvbqUVgw&amp;amp;t=1267s&quot;&gt;AWS Tutorial for Beginners. Full Crash Course by Neal Davis&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Sat, 05 Mar 2022 00:00:00 +0000</pubDate>
        <link>https://eshikafe.github.io/2022/deploying-your-rust-apps-in-the-cloud/</link>
        <guid isPermaLink="true">https://eshikafe.github.io/2022/deploying-your-rust-apps-in-the-cloud/</guid>
        
        
      </item>
    
      <item>
        <title>Rust-y Memory. How Safe?</title>
        <description>&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/iyit6v5.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;The &lt;a href=&quot;https://www.rust-lang.org/&quot;&gt;Rust&lt;/a&gt; programming language is said to be memory-safe but how does it achieve this? Why should we even care about memory safety? It turns out that the major cause of security vulnerabilities in modern day software systems (including Desktop and mobile applications) is due to memory safety issues and to address this, there is a general consensus, which I personally agree to, that a memory-safe systems programming language like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rust&lt;/code&gt; is required. This is one of the reasons big tech companies have decided to &lt;a href=&quot;https://foundation.rust-lang.org/members/&quot;&gt;invest in Rust&lt;/a&gt; and re-engineer some of their key products and services with it.&lt;/p&gt;

&lt;h2 id=&quot;how-does-rust-achieve-memory-safety&quot;&gt;How does &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rust&lt;/code&gt; Achieve Memory Safety?&lt;/h2&gt;

&lt;p&gt;Memory safety is achieved through three key concepts: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ownership&lt;/code&gt; (a language feature the compiler uses to free and allocate memory based on the scope of a variable binding), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;borrow checking&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lifetimes&lt;/code&gt;. All these analyses are done during compile-time.&lt;/p&gt;

&lt;p&gt;For simplicity, I will focus more on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ownership&lt;/code&gt; concept and show us how the compiler guarantees memory safety at compile-time using the scope of a variable binding to determine when to allocate and deallocate memory on the stack.&lt;/p&gt;

&lt;p&gt;Before we proceed, let’s review how our code is analyzed by the Rust compiler (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustc&lt;/code&gt;) for memory safety.&lt;/p&gt;

&lt;h2 id=&quot;code-compilation-with-memory-management&quot;&gt;Code Compilation With Memory Management&lt;/h2&gt;

&lt;p&gt;In simple terms, a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memory&lt;/code&gt; is a storage space (e.g &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RAM&lt;/code&gt;) on your computer where instructions to be executed by the computer’s CPU are stored. These instructions are the lines of Rust code you have written and compiled with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustc&lt;/code&gt; (the Rust compiler) or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo&lt;/code&gt; into a machine executable file (e.g &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#ms-dos-stub-image-only&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.exe&lt;/code&gt;&lt;/a&gt; file format on Windows and &lt;a href=&quot;https://refspecs.linuxfoundation.org/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elf&lt;/code&gt;&lt;/a&gt; on Linux). The executable file tells the Operating System how to load your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rust&lt;/code&gt; program into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memory&lt;/code&gt; for execution by the CPU.&lt;/p&gt;

&lt;p&gt;Now, let’s compile a very simple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rust&lt;/code&gt; program with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustc&lt;/code&gt; and examine how memory management (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ownership&lt;/code&gt;) works in Rust.&lt;/p&gt;

&lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;// hello.rs&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rust_edition&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2021&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello, Rust&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;{message} {rust_edition}!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Compiling the above code with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustc .\hello.rs&lt;/code&gt; on Windows will produce an executable file,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello.exe&lt;/code&gt; (about 148KB in size), in the same directory as the .rs file.&lt;/p&gt;

&lt;p&gt;Next, let’s see how the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello.exe&lt;/code&gt; was produced by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustc&lt;/code&gt; and how the memory-safety checks were done.&lt;/p&gt;

&lt;p&gt;The Rust compiler, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustc&lt;/code&gt;, performed several analyses on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello.rs&lt;/code&gt; file before it produced the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello.exe&lt;/code&gt; executable file. I will give a high-level overview of this process so we understand how memory safety is achieved by the compiler and at what phase of the compilation process it is done.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/72SEKSw.png&quot; alt=&quot;&quot; /&gt;
Figure 1: An simplified view of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustc&lt;/code&gt; compilation process&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phase 1&lt;/strong&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello.rs&lt;/code&gt; was translated to basic tokens using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustc_lexer&lt;/code&gt; crate and then to Rust &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tokens&lt;/code&gt; using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustc_parser&lt;/code&gt; crate. Tokens are easier for the compiler to work with than the text format of your .rs file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phase 2&lt;/strong&gt;:&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Tokens&lt;/code&gt; were translated to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AST&lt;/code&gt; (Abstract Syntax Tree) format. Syntax analysis is done here. Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo inspect --unpretty=ast-tree .\hello.rs&lt;/code&gt; to examine the AST output.
&lt;img src=&quot;https://i.imgur.com/wHwJRPz.png&quot; alt=&quot;&quot; /&gt;
Figure 2: AST tree of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello.rs&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo-inspect&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phase 3&lt;/strong&gt; The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;println!&lt;/code&gt; macro was refined (or desugared) to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::io::_print&lt;/code&gt; statement and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;core::fmt::Arguments&lt;/code&gt; function calls.The data type of our expressions were inferred and checked. We can say that type safety is guarateed at this phase of the code compilation. The HIR (High-Level Intermediate Representation) is the output of this stage. You can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo inspect --unpretty=hir .\hello.rs&lt;/code&gt; to view the HIR representation of your Rust code.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/kSPdFvv.png&quot; alt=&quot;&quot; /&gt;
Figure 3:&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;println!&lt;/code&gt; macro desugared in the HIR.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phase 4&lt;/strong&gt; HIR was converted to MIR (Mid-level Intermediate Representation). Ownership, borrow-checking and optimizations are done here. In fact, the MIR shows the scope of each variable and helps the compiler know at what point a variable binding (or ownership) is out of scope and when to dellocate memory from the stack. How the compiler tracks the scope of each variable is indicated in the screenshot below. In fact, the assembly code generated by &lt;a href=&quot;https://llvm.org/&quot;&gt;LLVM&lt;/a&gt; (in phase 6) is the machine representation of the MIR (after the ownership, borrow checking and optimizations have been done). In the next phase, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;asm&lt;/code&gt; .S file is examined to see how the variables are allocated on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stack&lt;/code&gt; when they are in scope and how they are deallocated when they are out of scope. In this phase, memory safety is guaranteed by the compiler.
&lt;img src=&quot;https://i.imgur.com/9BSWZ32.png&quot; alt=&quot;&quot; /&gt;
Figure 4: MIR representation of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello.rs&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phase 5 and 6&lt;/strong&gt; This is the code generation phase - LLVM was used to generate the final executable file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello.exe&lt;/code&gt; from the optimized and memory-safe MIR representation. Further optimizations can also be done by LLVM.&lt;/p&gt;

&lt;p&gt;Let’s briefly examine the assembly code (.S file) generated by LLVM. You can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustc --emit asm .\hello.rs&lt;/code&gt; to generate the file. To keep things simple, I will only examine how the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ownership&lt;/code&gt; memory-safety feature was achieved by the allocation and deallocation of memory on the stack.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/E1VoudA.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Figure 5: A simplified memory layout of the stack from the perspective of the compiler generated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;The assembly code for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; function is shown above (in Figure 5). The entry point is not our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello::main&lt;/code&gt; function. As we will see later, Rust has a runtime (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::rt::lang_start_internal&lt;/code&gt;). This runtime handles a lot of complexities for us that we don’t need to bother about when writing our Rust code.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Line 516: The compiler generated a main function is the entry point for our program.&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Line 518: 40 bytes of memory is allocated on the stack.&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rsp&lt;/code&gt; is the 64-bit stack pointer register for x86_64. It always points to the top the stack.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Line 523: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_ZN5hello4main17h0767239aa2b5c6caE&lt;/code&gt; is the mangled symbol for our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello.rs&lt;/code&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main()&lt;/code&gt; function. The address of our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello::main&lt;/code&gt; function is stored in %rcx and then passed to the runtime function as a reference in line 103.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;Line 524: This line calls the Rust runtime function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::rt::lang_start&lt;/code&gt; (defined in line 94) and subsequently, the internal runtime function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::rt::lang_start_internal&lt;/code&gt; (defined in line 104). Rust has a runtime that executes our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello.rs main()&lt;/code&gt; function.
&lt;img src=&quot;https://i.imgur.com/gx2fnqx.png&quot; alt=&quot;&quot; /&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Figure 6: Rust runtime function executes our hello::main function&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/hSGodss.png&quot; alt=&quot;&quot; /&gt;
Figure 7: Execution of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello::main&lt;/code&gt; (part 1)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/oWxVc3T.png&quot; alt=&quot;&quot; /&gt;
Figure 8: Execution of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello::main&lt;/code&gt; (part 2)&lt;/p&gt;

&lt;p&gt;Figures 7 and 8 show how sufficient memory (200 bytes) was first allocated on the stack by the compiler before allocating memory to the two local variables &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rust_edition&lt;/code&gt; (an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i32&lt;/code&gt;), and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;message&lt;/code&gt; (a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;str&lt;/code&gt;). Before returning from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello::main&lt;/code&gt; function, the compiler deallocates memory on the stack and frees up resources.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;We have seen how Rust guarantees memory safety during compile-time by examining the compilation phases and how memory is allocated and deallocated on the stack in assembly code. A very simple Rust program (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello.rs&lt;/code&gt;) was used to examine the output of each compilation phase and the memory management of the stack in assembly code.&lt;/p&gt;

&lt;p&gt;We also learnt that the Rust compiler generates a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; function for us as the entry point of our program. This main function uses the Rust runtime to execute the main function of our Rust program.&lt;/p&gt;

&lt;p&gt;In summary, Rust tries to guaranty memory safety during compile time and it does a pretty good job in ensuring this.&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://rustc-dev-guide.rust-lang.org/overview.html&quot;&gt;Overview of the Compiler&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://doc.rust-lang.org/1.8.0/book/ownership.html&quot;&gt;Ownership&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://doc.rust-lang.org/1.8.0/book/references-and-borrowing.html&quot;&gt;References and Borrowing&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/cd/E19253-01/817-5477/817-5477.pdf&quot;&gt;x86 Assembly Language Reference Manual&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.cs.princeton.edu/courses/archive/spr16/cos217/lectures/15_AssemblyFunctions.pdf&quot;&gt;Assembly Language: Function Calls&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cs.brown.edu/courses/cs033/docs/guides/x64_cheatsheet.pdf&quot;&gt;x64 Cheat Sheet&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://courses.cs.washington.edu/courses/cse351/17au/lectures/1&quot;&gt;The Stack &amp;amp; Procedures&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Fri, 04 Feb 2022 00:00:00 +0000</pubDate>
        <link>https://eshikafe.github.io/2022/rusty-memory-how-safe/</link>
        <guid isPermaLink="true">https://eshikafe.github.io/2022/rusty-memory-how-safe/</guid>
        
        
      </item>
    
  </channel>
</rss>
