Skip to content

Kotlin coroutines (J4) ​

Ready

This package comes with the J1 hook: when the hub proposes J1 for a container, this is part of it, and nothing changes until a player approves the proposal (Hook proposals). The cheat sheet has the steps by hand.

J4 makes a request's context follow Kotlin coroutines: from the coroutine a request starts, into withContext(Dispatchers.IO), async/launch children on Dispatchers.Default, back after every suspension — on whichever thread the dispatcher picks. It matters for Ktor and for Spring WebFlux, Micronaut, Quarkus or Vert.x services written with suspending functions. It is part of the JVM agent: the same jar and the same compose-only change.

Covers ​

Versions
kotlinx.coroutines1.3–1.10 (JVM)
DispatchersDefault, IO, Main-less servers' custom dispatchers (asCoroutineDispatcher() on any executor), runBlocking event loops, Ktor's and Reactor's coroutine bridges
ServersKtor (Netty and CIO engines; request boundaries from J3), Spring WebFlux / Micronaut / Quarkus / Vert.x with coroutines (boundaries from J3 or J2)

The sample is Ktor 3 on Netty with handlers that hop to Dispatchers.IO and Dispatchers.Default.

What the kernel links without the hook. Nothing: coroutine dispatchers run many requests' coroutines on a few shared threads.

How it gets in ​

From a registry (local development, or a build with network; the same release as the hook directory): Maven Central garden.ctf.vulnboxd:vbxtrace-agent:1.0.0: mvn dependency:unpack -Dartifact=garden.ctf.vulnboxd:vbxtrace-agent:1.0.0:zip:dist -DoutputDirectory=target/agent, then -javaagent:target/agent/vbxtrace/jvm/vbxtrace-agent.jar; see Local development.

The JVM agent's compose-only change; J4 needs nothing else:

diff
 services:
   <service>:
+    volumes:
+      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
+    environment:
+      JAVA_TOOL_OPTIONS: "-javaagent:/opt/vbxtrace/jvm/vbxtrace-agent.jar"

The hub proposes it when it detects a JVM service (see J1); org.jetbrains.kotlinx:kotlinx-coroutines-core and io.ktor rank it higher.

What it records ​

J4 emits no request events of its own; it makes the request current where the coroutine runs:

EventEmitted fromJoin key
context_enter / context_exitaround every piece of a coroutine a dispatcher runs (DispatchedTask.run: the coroutine's start on a dispatcher and every resumption after a suspension)context id
spawned, connected, openedJ1's JDK hooks inside the coroutine, with the request's contextchild pid, local port, pid + fd

Context following ​

  • A coroutine (launch, async, runBlocking, withContext, coroutineScope, supervisorScope, Ktor's per-call coroutine, …) captures the request current where it is created — or, when none is current there, its parent job's request (a child launched before its parent was ever dispatched); its children capture the same request.
  • The threads of Dispatchers.Default/IO are created lazily; they never inherit the request that happened to need a new one.
  • Whenever a dispatcher runs a piece of the coroutine, the request of the coroutine's Job is current on that thread, and the sensor is told (context_enter/context_exit).
  • Dispatchers.Unconfined and undispatched starts run in the caller's thread, where the caller's request is already current.

This does what a ThreadContextElement would do, but without adding an element to the service's coroutine contexts: no class has to be defined inside the service's class loader, and coroutineContext stays exactly what the service built (code that prints or inspects it sees no difference).

Known gaps ​

  • Coroutines created outside any request and later resumed by a request (a shared long-lived coroutine fed through a Channel or Flow) keep "no request"; the work they do for a request is not linked.
  • Kotlin/Native and Kotlin/JS: not JVMs, see C3 for Kotlin/Native.

Safety ​

Three hooks: the AbstractCoroutine constructor (records the creating request, only while one exists), CoroutineScheduler.createNewWorker (no inheritance into new dispatcher threads) and DispatchedTask.run (one map lookup while any task has a captured request, nothing otherwise — the normal state without a tracer). Reflection reads the coroutine's Job from the dispatched continuation; a kotlinx.coroutines version whose internals differ gets no events. Everything in J1's safety section applies.

Sample results: the recorded events match the golden event file, and the responses are byte-identical with the hook failing on every call (chaos) and without the hook (baseline). 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. J4 cannot be switched off separately from the agent.

Troubleshooting ​

  • Are the probes there? readelf -n /usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.so lists them; sudo bpftrace -l 'usdt:/usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.so:*' too.
  • Is the hook loaded? See J1. With VBXTRACE_DEBUG=1 the agent prints instrumented kotlinx/coroutines/DispatchedTask when kotlinx.coroutines loads.
  • Which links did a flow get? The hub shows each link's method; this package's are hook:J4.
  • A shaded / relocated kotlinx.coroutines (some fat jars relocate it to their own package): the hooks name the original package, so a relocated copy is not followed.

Samples ​

SampleServerEvents checked
ktorKtor 3, Netty enginekeep-alive indexes, status, spawn, file, connect; withContext(Dispatchers.IO) and async(Dispatchers.Default) file writes linked to their request (context switches are not compared: their interleaving across threads depends on timing)

Run it with hooks/run-samples.sh jvm --package j4.