Rust CPU and Memory Profiling

CPU Profiling with perf

  • Add the following settings to Cargo.toml
1
2
[profile.release]
debug = true

Also run the code in release mode.

  • Install perf + FlameGraph
1
2
sudo pacman -Syu perf
git clone https://github.com/brendangregg/FlameGraph.git
Read more
Rust HTTP Framework Design: Axum 0.6 as an Example

Author: suikammd ihciah
An HTTP Server contains many internal components: protocol implementation (h1, h2, compression, and so on), connection state management (keepalive), request dispatching, middleware, business logic, and more. Users could implement all of these themselves, but everything other than the business logic is a fairly general capability. Decoupling these general capabilities from the user’s business logic gives us an HTTP framework.

Because hyper already provides a fairly complete HTTP protocol implementation in the Rust ecosystem, building an HTTP framework on hyper only requires providing capabilities such as routing, shared state, and middleware.

From the perspective of HTTP framework design, this article uses the new version of Axum as an example to analyze how an HTTP framework in Rust can provide sound abstractions and type constraints. Rust’s powerful type system allows us to write efficient and correct code.

Read more
Implementing a Simple Channel and Exploring Tokio's Channel Implementation

The core data structure for concurrent programming in Golang is the channel. Its central idea is to share memory by communicating rather than communicate by sharing memory. Channels are indeed useful and avoid the need to write various complex communication primitives directly. Rust naturally has corresponding implementations as well, and Tokio’s channel implementation is extremely clever and well worth studying. This article briefly introduces the basic concepts of channels, then implements a minimal mpsc unbuffered channel, and finally examines Tokio’s channel implementation.

Minimal Rust Channel Implementation

This section implements a minimal channel. This channel has an unlimited buffer, allows multiple writers, and never blocks writes to the channel. However, if the channel is empty, consumers attempting to consume data from it will be blocked. In other words, this is an mpsc unbuffered channel. This section refers to Chen Tian: Rust Programming First Course, Lesson 35. Chen Tian’s lesson is highly accessible and uses TDD to complete the entire channel design step by step, starting with interface test cases. I will take the same approach here, using TDD and aligning with the standard library’s mspc interface.

Read more
Implementing a Simple Rust Executor

Most of this article is based on aysnc-book and explains how to implement a simple executor. The project is available at simple-executor.

Asynchronous programming in Rust involves three important concepts: Future, executor, and reactor. When an executor polls a Future, it passes a waker to the Future. The reactor then waits for the events involved in the Future and, when an event completes, uses this waker to wake the executor so that it can continue executing the Future.

Future

The core of asynchronous programming in Rust is the Future trait. The Future trait has an associated type, Output, which is the result returned when the Future completes. The executor polls the Future step by step until it completes.

1
2
3
4
5
6
7
8
pub trait Future {
#[stable(feature = "futures_api", since = "1.36.0")]
type Output;

#[lang = "poll"]
#[stable(feature = "futures_api", since = "1.36.0")]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;
}

When a Future is submitted to the executor, the executor polls it. If it returns Poll::Ready(Result), the Future is finished. If it returns Poll::Pending, the Future needs to be submitted to the executor again the next time it can be polled. This is done by calling Waker’s wake method.

Read more
An Interesting Debugging Journey

The Tokio library provides the copy_bidirectional method, which makes it easy to copy data bidirectionally for data structures that implement AsyncRead+AsyncWrite. In my previous article (which is still being revised), I implemented a simple proxy based on WebSocket, using tokio_tungstenite as the WebSocket library.

Originally, I customized a function similar to bidirectional copying on the client side instead of directly using copy_bidirectional. This was because the WebSocketStream built on TcpStream by tokio_tungstenite did not implement AsyncRead+AsyncWrite. In addition, the WebSocketStream returned by tokio_tungstenite does not expose convenient read and write methods; it only implements Sink and Stream. However, it provides a split method that separates the stream’s reads and writes. The split read half blindly writes the data it reads to TcpStream, while the split write half blindly writes data read from TcpStream to its own stream. The resulting problem is that when reads and writes are separated, there is no graceful way to detect a Close packet and exit the read and write flows described above. Here is a simple illustration of the original implementation:

Read more
Implementing a SOCKS5 Proxy with Rust

The first step toward becoming familiar with a language might be writing a proxy with it. After studying Rust syntax for a long time, I still had little opportunity to use it at work, so I had to create a need for it myself. I hope to find a job writing Rust in the future, or one that gives me the freedom to learn new things.

What are this proxy’s features?

  • Based on Rust

The main purpose is to practice writing Rust. Rust is also safe and efficient.

  • Based on TLS WebSocket

Getting past the mysterious wall requires an encrypted connection; otherwise, it can easily get killed. A common approach, like the one used by Shadowsocks, is to define a custom encryption protocol, but that is rather troublesome. To keep things simple, I used TLS WebSocket directly for this purpose.

  • Pooled WebSocket connections

The benefits of connection pooling hardly need explaining. Eliminating connection establishment time makes the proxy faster.

The overall proxy flow looks like this:

Rust proxy pipeline

Source code: https://github.com/suikammd/rust-proxy

Read more
2021 ByteDance CTF Writeup

A beginner’s first CTF experience. Many thanks to Hai for the patient guidance!

The problem categories included SQL injection, SSRF, XSS, privilege bypass, information disclosure, and Android. Most categories created by ByteDance contained multiple problems of progressively increasing difficulty. For example, in SQL injection, the first problem showed you the Golang source code, so you could directly see how Gorm concatenated strings. The later problems gradually revealed less information to participants.

Useful tools:

  1. Use burp as a proxy to capture packets. Configure requests in repeater, where you can freely modify request parameters for testing.
  2. sqlmap is a powerful tool for solving SQL injection problems.
  3. requestbin.com lets you create bins at will to receive test requests.
  4. cloudflare worker lets you quickly create test services.
Read more
Rate Limiter

Rate limiters primarily prevent highly concurrent traffic from overwhelming a service. Common rate-limiting algorithms include counters, token buckets, and leaky buckets. A token bucket directly rejects requests when there are insufficient tokens, while a leaky bucket blocks requests when there are insufficient requests. This article implements a rate limiter that allows a moderate amount of burst traffic. This is just for fun, so please don’t criticize my crude implementation.

Read more
Understanding How Go Passes Slices to Functions

When I first started learning Go, what puzzled me most was passing slices to functions: had the slice changed or not? I recently discovered that some of my colleagues were not clear about this either, so I decided to take a closer look at whether modifying a slice passed to a function changes the original slice.

Slice structure

I’ll start with the structure of a slice. As you can see, a slice is actually backed by an array.

1
2
3
4
5
6
7
8
9
10
11
type slice struct {
array unsafe.Pointer
len int
cap int
}

type SliceHeader struct {
Data uintptr
Len int
Cap int
}
Read more
Lexical and Syntax Analysis with ANTLR

Recently, I have often worked with ANTLR. At first, I only half understood it and got things to work through all kinds of blind trial and error. After finishing that work, I wanted to systematically understand what a compiler frontend actually does: what lexical analysis and syntax analysis do, as well as the syntax of ANTLR lexers and parsers.

What Does a Compiler Frontend Actually Do?

From a fairly high-level perspective, a compiler frontend processes a string in two steps. The first step is lexical analysis, which parses the string into a sequence of tokens. The second step is syntax analysis, which matches the corresponding grammar rules according to the grammar.

  1. Lexical analysis

    Take the SPL query search id |stats count by id as an example. Lexical analysis parses this string into the token sequence search, id, stats, count, by, and |, while also obtaining the type corresponding to each token.

  2. Syntax analysis

    The result of lexical analysis serves as the input to the parser. After syntax analysis, the AST for this SPL query is determined.

Read more