Skip to content

Rust other runtimes and threads (R2) ​

Ready

The hub recognises the services this package fits and proposes the compose part of the change, if any; the source change stays by hand, with the steps and examples in the proposal's review. Nothing changes until a player approves it (Hook proposals). The cheat sheet has the steps by hand.

Rust services that do not run on tokio: plain std threads, async-std and tide, smol, may coroutines (may_minihttp), and the io_uring runtimes glommio and monoio. They use the same vendored crate as R1, vbxtrace-hook, with these runtimes' cargo features. Getting it in is a source change that the hub shows as a full diff: copy the crate into vendor/vbxtrace, add one dependency line, change the server line, and wrap the calls whose effects should be linked.

Covers ​

VersionsFeature
std threads (thread per connection)any Rust ≥ 1.85none (always on)
async-std1.xasync-std
tide0.16tide
smol2.x (also async-executor / async-net / blocking)smol
may0.3may
may_minihttp0.1may-minihttp
monoio0.2 (io_uring, or epoll with its legacy driver)monoio
glommio0.9 (needs io_uring: see Known gaps)glommio

What the kernel already links without this hook: a server with a thread per connection (std TcpListener + thread::spawn) is linked exactly by the kernel. The thread owns exactly one client socket, so everything it does belongs to that connection. The hook only adds request boundaries on keep-alive connections, and carries the request into helper threads. Every async runtime here (async-std, smol, may, glommio, monoio) runs many connections per thread, so it needs the hook.

How it gets in ​

From a registry (local development, or a build with network; the same release as the hook directory): cargo add vbxtrace-hook@1.0.0 --features axum08 (crates.io; one feature per integration); see Local development.

Source change, with the same steps as R1: copy the hook directory's rust/ to vendor/vbxtrace, add vbxtrace-hook = { path = "vendor/vbxtrace/vbxtrace-hook", features = [...] } (and exclude vendor/vbxtrace from a [workspace]), and make sure the Dockerfile copies vendor/. The crate adds no crates the service does not have, so the build stays offline. Then, per runtime:

tide 0.16: a drop-in TCP listener that runs tide's own accept loop (async-h1 per connection) and begins a request around each app.respond(..):

diff
-    app.listen("0.0.0.0:8080").await?;
+    app.listen(vbxtrace_hook::tide::TracedListener::bind("0.0.0.0:8080")).await?;

may_minihttp: HttpServer has no per-connection hook, but may_minihttp's service factory runs the same accept loop and creates each connection's service from the connection's fd:

diff
-    let server = may_minihttp::HttpServer(Svc).start("0.0.0.0:8080").unwrap();
+    use may_minihttp::HttpServiceFactory;
+    let server = vbxtrace_hook::may::TracedServer(Svc).start("0.0.0.0:8080").unwrap();

A service that already has a factory wraps it: vbxtrace_hook::may::Traced(factory).start(addr).

Hand-written servers (std threads, async-std, smol, raw may, monoio, glommio): one line per accepted connection, and a request per message:

rust
let conn = vbxtrace_hook::ConnInfo::from_socket(&stream);      // any socket with an fd
// a raw protocol: the whole connection is one request
spawn(conn.session(handle(stream)));
// or per message (HTTP/1.1 keep-alive, a line protocol):
let request = conn.begin(vbxtrace_hook::Protocol::Http1);
let reply = request.bind(handle(msg)).await;                    // async
let reply = vbxtrace_hook::with_context(request.ctx(), || handle(msg));   // threads, may
request.end(status);

The hub proposes R2 when it finds a Cargo.toml/Cargo.lock with async-std, tide, smol, may, may_minihttp, glommio or monoio and no tokio framework, or a std-only Rust service. Templates: hooks/rust/r2/proposals/*.toml; tide.diff applies to the fixture hooks/rust/r1/tests/offline/tide and is built offline by the same test as R1's.

What it records ​

EventEmitted fromJoin key
request_begin / request_endtide::TracedListener, may::TracedServer/Traced, ConnInfo::begin/sessionaccepted socket's local port and peer, request index on the connection
spawnedprocess::CommandExt::{spawn,status,output}_traced (std)child pid
connectednet::connect (std), async_std::connect, smol::connect, monoio::connect, glommio::connect, net::note_connectedthe socket's local port
openedfs::* (std), async_std::fs::*, monoio::{open, create, OpenOptions}, glommio::{dma_,buffered_}{open,create}pid + fd
context_enter / context_exitthread::spawn/wrap, async_std::spawn_blocking, smol::unblock, scope, std output_tracedthe thread
  • io_uring (monoio, glommio). Opens, connects, reads and writes submitted through io_uring do not pass the syscall tracepoints the sensor otherwise sees. The hook's opened and connected are then the only record of which file or connection a request used. Wrap every open and connect in these runtimes.
  • open(2) flags. monoio computes them like std (File::create = 0x80241); glommio's BufferedFile likewise. glommio's DmaFile adds O_DIRECT, whose value differs between x86_64 and aarch64, so its flags are reported as unknown (0xffffffff).
  • may_minihttp gives no way to read the response status back, so its requests end with status 0.
  • tide requests end with the response's status (or the error's).

Context following ​

The request context is the same thread-local as in R1, set around every poll of a bound future. It works on every executor here: each poll runs on one thread. Helpers carry it into new work:

RuntimeInstead ofUse
stdthread::spawn(f), a pool's execute(f)vbxtrace_hook::thread::spawn(f), execute(vbxtrace_hook::thread::wrap(f))
async-stdtask::spawn, task::spawn_blockingvbxtrace_hook::async_std::{spawn, spawn_blocking}
smolsmol::spawn, smol::unblock, Executor::spawn(fut)vbxtrace_hook::smol::{spawn, unblock}, ex.spawn(vbxtrace_hook::bind(fut))
monoiomonoio::spawnvbxtrace_hook::monoio::spawn
glommioglommio::spawn_localvbxtrace_hook::glommio::spawn_local
maygo!(..)capture vbxtrace_hook::current() and run the body in with_context(ctx, ..)

may coroutines can move to another worker thread whenever they block. Inside a coroutine the context is therefore kept in a coroutine-local instead, and current(), with_context() and every wrapper use it automatically.

Threads that do a request's blocking work (thread::spawn, spawn_blocking, unblock, scope) are marked with context_enter/context_exit. The sensor then attributes everything such a thread does meanwhile, including unwrapped calls.

Known gaps ​

  • Processes started after the sensor attached, with the probes in the binary. The kernel switches the probes on in such a process through a counter on the file page that holds the .probes section. When the linker put that page into two LOAD segments (lld can, sharing it with the read-only-after-relocation data), the kernel's increment lands in the wrong mapping, so the sensor raises the counter itself in the process's own writable mapping (vulnboxd_sensor_usdt_semaphores_raised_total counts it). Linking with -C link-arg=-Wl,-z,separate-loadable-segments (in RUSTFLAGS or .cargo/config.toml) avoids it.
  • async-std's process API is unstable, and smol's process/fs have no traced versions. Use the std wrappers inside spawn_blocking / unblock (which also marks the thread).
  • glommio needs io_uring, and podman's (and Docker's) default seccomp profile blocks io_uring_setup: a glommio service in such a container does not start at all, hook or not. The service's compose entry needs security_opt: [seccomp=unconfined] (or a profile that allows io_uring) for that reason alone. Its sample runs in the sample harness with that option (podman_args in its sample.toml): events, chaos and baseline pass. glommio 0.9 builds its bundled liburing with git, make and a C compiler; it does not build with very recent GCC and kernel headers (Fedora 44), so its sample uses Debian bookworm.
  • monoio: under the harness (default seccomp) monoio's fusion driver falls back to epoll; with io_uring allowed (uring-samples.sh) the sample runs on io_uring. The hook's events are the same either way; both runs pass.
  • may: go! does not carry the context into the new coroutine; the raw may TCP stream's connect needs net::note_connected after it.
  • tide: only TCP listeners; tide's TLS and unix listeners are not wrapped. tide 0.17 betas are not tested.
  • Unwrapped opens/connects in async runtimes are not linked (as in R1).

Safety ​

The same guard as R1: wrappers call exactly what they replace and return its result; the hook's own work is panic-guarded and switches off after 5 internal errors; VBXTRACE=0 switches it off at start; with no tracer attached each hook point checks the core's semaphores and does nothing else (except counting requests per connection). Tested: the std-threads, tide, smol, may and monoio samples (through the sample harness) and the glommio and monoio samples on io_uring (tests/uring-samples.sh) produce their golden events, and their responses are byte-identical with every hook call failing (chaos) and without the hook. Overhead has not been measured yet.

Turning it off ​

Set VBXTRACE=0 in the service's environment and restart it, or revert the change that added the hook; vulnboxd's rollback covers a hook that breaks a service. Reverting removes vendor/vbxtrace, the dependency line and the wrapper calls.

Troubleshooting ​

  • Are the probes there? readelf -n target/release/<service> | grep -A3 vbxtrace, or sudo bpftrace -l 'usdt:/proc/<pid>/exe:vbxtrace:*' for the running service.
  • may requests have no spawned/opened. The work ran in a coroutine started with go! without with_context.
  • monoio/glommio file or connection missing from a flow. The open or connect is not wrapped; io_uring operations are invisible to the sensor otherwise.
  • Which links did a flow get? The hub shows each link's method; this package's are hook:R2.