chillffi 0.4.0

A simple isolated dynamic FFI framework for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
//! Windows backend for [`super::Transport`].
//!
//! True process cloning via `RtlCloneUserProcess`; `ipc-channel` for both
//! planes, as on macOS. What differs is *who* the clone talks to:
//!
//! - **Control plane** (Runtime ↔ Main Zygote): one `IpcOneShotServer` set
//!   up in the Runtime; Main Zygote connects with `IpcSender::connect(name)`,
//!   hands over its `IpcSender<ZygoteCommand>` + `IpcReceiver<ZygoteReply>`,
//!   and the one-shot is dropped.
//!
//! - **Data plane** (Runtime ↔ Clone): the Runtime creates both channel pairs
//!   itself. Per clone: the Runtime listens on a one-shot server and passes
//!   its name in `SpawnClone`; Main Zygote only clones and answers with the
//!   pid; the clone opens its own short-lived setup server and tells the
//!   Runtime its name; the Runtime sends the clone-side ends there.
//!
//! # Why the Runtime is the rendezvous point
//!
//! `RtlCloneUserProcess` copies the memory of Main Zygote, and `ipc-channel`
//! keeps process-wide state in it: its cached pid (`CURRENT_PROCESS_ID`,
//! a `LazyLock`) is already resolved to the pid of Main Zygote by its own
//! bootstrap `send`. A clone that sends channel ends to a pipe owned by Main
//! Zygote takes that pipe for its own (`server pid == cached pid`) and
//! duplicates the handles into itself instead of into Main Zygote. A pipe
//! owned by the Runtime does not match the stale pid, so the same `send`
//! works. Main Zygote never sends channel ends after its bootstrap.
//!
//! # Why the Runtime creates the long-lived channels
//!
//! Pipe names are UUIDs, and the RNG behind them (`ProcessPrng`) is copied
//! together with the rest of the memory: clones of one Main Zygote draw the
//! same UUIDs. Names chosen by a clone would collide with those of its
//! siblings, and `ipc-channel` keeps received handles open, so a dead clone
//! would go on occupying its names. The names of the long-lived channels come
//! from the Runtime instead (an ordinary process), and the clone only creates
//! a setup server that lives for the duration of the bootstrap
//! (`low::decorrelateRandom` moves it to its own place in the stream).
// =================================================================================================
use super::Transport as TransportTrait;
use super::{
  CloneSide as CloneSideTrait, FFIRequest, FFIResponse,
  RuntimeSide as RuntimeSideTrait, ZygoteHandleBase
};
use crate::ffi::errors::FFIError;
use crate::platform::low;
use crate::worker::executeFFI;
use crate::worker::{takeLastErrno, takeLastOsError};
use crate::zygote::ZygoteFlag;
use fxhash::FxHashMap;
use ipc_channel::ipc::{self, IpcOneShotServer, IpcReceiver, IpcSender};
use libloading::Library;
use serde::{Deserialize, Serialize};
use std::env;
use std::io;
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::time::{Duration, Instant};
// =================================================================================================

/// Hidden startup flag of a legacy Command-based clone (kept for
/// compatibility — current path is `RtlCloneUserProcess`).
pub const CloneFlag: &str = "__zygoteClone";

/// Backend tag used in diagnostics.
const BackendName: &str = "windows-rtlcloneuserprocess";

/// How long a clone may take to greet the Runtime.
const CloneBootstrapTimeout: Duration = Duration::from_secs(20);

/// Exit codes of a clone that failed during its bootstrap. Nobody reads the
/// stderr of a clone reliably; the Runtime puts the code into its error.
const CloneExitSetupServer: u32 = 11;
const CloneExitConnect: u32 = 12;
const CloneExitHello: u32 = 13;
const CloneExitSetup: u32 = 14;

// =================================================================================================

/// Commands Runtime → Main Zygote.
#[derive(Serialize, Deserialize)]
pub enum ZygoteCommand
{
  /// Ask Main Zygote to `RtlCloneUserProcess` a clone.
  ///
  /// `bootstrapName` is the name of the [`IpcOneShotServer`] that the
  /// *Runtime* listens on: the clone connects to it directly and greets the
  /// Runtime, Main Zygote never touches the data channels.
  SpawnClone { bootstrapName: String }
}

/// Replies Main Zygote → Runtime.
#[derive(Serialize, Deserialize)]
pub enum ZygoteReply
{
  /// The clone process exists; only its pid. No channel ends pass through
  /// Main Zygote — the clone and the Runtime settle them between themselves.
  Cloned { pid: u32 },

  /// `cloneProcess()` failed inside Main Zygote.
  SpawnFailed
}

/// First message from Zygote after connecting to Runtime's [`IpcOneShotServer`].
#[derive(Serialize, Deserialize)]
struct BootstrapToRuntime
{
  /// Runtime → Main Zygote commands.
  commandTx: IpcSender<ZygoteCommand>,

  /// Main Zygote → Runtime replies.
  replyRx: IpcReceiver<ZygoteReply>
}

/// First message from a freshly cloned process → Runtime: "I am up, my
/// setup server is called `setupName`".
#[derive(Serialize, Deserialize)]
struct CloneHello
{
  /// Name of the clone's one-shot setup server.
  setupName: String
}

/// Runtime → clone, over the setup server of the clone: the clone-side ends
/// of the two data channels the Runtime has created.
#[derive(Serialize, Deserialize)]
struct CloneSetup
{
  /// Runtime → Clone requests.
  requestRx: IpcReceiver<FFIRequest>,

  /// Clone → Runtime responses.
  responseTx: IpcSender<FFIResponse>
}

// =================================================================================================

/// Windows Transport: `RtlCloneUserProcess` + ipc-channel.
pub struct Transport;

/// Runtime-side handle to the Main Zygote.
pub struct ZygoteHandle
{
  /// Common handle (process handle + Drop).
  pub base: ZygoteHandleBase,

  /// Runtime → Main Zygote commands.
  pub commandTx: IpcSender<ZygoteCommand>,

  /// Main Zygote → Runtime replies.
  pub replyRx: IpcReceiver<ZygoteReply>
}

/// Runtime-side data endpoint.
pub struct RuntimeSide
{
  /// Pid of the clone, to say what became of it when the channel breaks.
  pub pid: u32,

  /// Runtime → Clone requests.
  pub requestTx: IpcSender<FFIRequest>,

  /// Clone → Runtime responses.
  pub responseRx: IpcReceiver<FFIResponse>
}

/// Clone-side data endpoint.
pub struct CloneSide
{
  /// Runtime → Clone requests.
  pub requestRx: IpcReceiver<FFIRequest>,

  /// Clone → Runtime responses.
  pub responseTx: IpcSender<FFIResponse>
}

/// Clone spawn result: pid and Runtime-side data channel ends.
#[derive(Serialize, Deserialize)]
pub struct Bootstrap
{
  /// PID of the freshly created clone.
  pub pid: u32,

  /// Runtime → Clone requests.
  pub requestTx: IpcSender<FFIRequest>,

  /// Clone → Runtime responses.
  pub responseRx: IpcReceiver<FFIResponse>
}

// =================================================================================================

impl TransportTrait for Transport
{
  type RuntimeSide = RuntimeSide;
  type CloneSide = CloneSide;
  type Bootstrap = Bootstrap;
  type ZygoteHandle = ZygoteHandle;

  /// Short backend tag for diagnostics. Dispatched through the trait, so
  /// Clippy sees it as "never used" — silenced here.
  #[allow(dead_code)]
  fn name() -> &'static str
  {
    BackendName
  }

  /// Spawns the Main Zygote and bootstraps the control channel.
  fn spawnZygote() -> io::Result<Self::ZygoteHandle>
  {
    let (server, serverName): (
      IpcOneShotServer<BootstrapToRuntime>,
      String
    ) = IpcOneShotServer::new().map_err(io::Error::other)?;

    let currentExe: PathBuf = env::current_exe()?;
    let process: Child = Command::new(currentExe)
      .arg(ZygoteFlag)
      .arg(&serverName)
      .stdin(Stdio::null())
      .stdout(Stdio::inherit())
      .stderr(Stdio::inherit())
      .spawn()?;

    let (_rx, bootstrap): (
      IpcReceiver<BootstrapToRuntime>,
      BootstrapToRuntime
    ) = server.accept().map_err(|e| {
      io::Error::other(format!("zygote bootstrap accept: {e}"))
    })?;

    Ok(ZygoteHandle {
      base: ZygoteHandleBase { process },
      commandTx: bootstrap.commandTx,
      replyRx: bootstrap.replyRx
    })
  }

  /// Runtime asks Main Zygote to clone a process and hands the clone its
  /// channel ends (see the module docs).
  fn sendSpawnClone(handle: &Self::ZygoteHandle) -> io::Result<Self::Bootstrap>
  {
    // The long-lived channels: created here, named by the Runtime's RNG.
    let (requestTx, requestRx): (
      IpcSender<FFIRequest>,
      IpcReceiver<FFIRequest>
    ) = ipc::channel::<FFIRequest>()?;
    let (responseTx, responseRx): (
      IpcSender<FFIResponse>,
      IpcReceiver<FFIResponse>
    ) = ipc::channel::<FFIResponse>()?;

    let (server, serverName): (
      IpcOneShotServer<CloneHello>,
      String
    ) = IpcOneShotServer::new().map_err(io::Error::other)?;

    handle
      .commandTx
      .send(ZygoteCommand::SpawnClone { bootstrapName: serverName.clone() })
      .map_err(|e| {
        io::Error::new(
          io::ErrorKind::BrokenPipe,
          format!("SpawnClone send failed: {e}")
        )
      })?;

    let reply: ZygoteReply = handle.replyRx.recv().map_err(|e| {
      io::Error::new(
        io::ErrorKind::BrokenPipe,
        format!("SpawnClone reply failed: {e}")
      )
    })?;

    let pid: u32 = match reply
    {
      ZygoteReply::Cloned { pid } => pid,
      ZygoteReply::SpawnFailed => return Err(io::Error::other(
        "Main zygote failed to create a clone (RtlCloneUserProcess failed)"
      ))
    };
    if pid == 0
    {
      return Err(io::Error::other(
        "Main zygote failed to create a clone (pid=0)"
      ));
    }

    // The clone is up and has opened its setup server.
    let hello: CloneHello = acceptHello(server, &serverName, pid)?;

    // Hand it its ends. The clone owns the server of this pipe, so
    // ipc-channel duplicates the handles straight into the clone.
    let setupTx: IpcSender<CloneSetup> = IpcSender::connect(hello.setupName)
      .map_err(|e| {
        low::killProcess(pid);
        io::Error::other(format!(
          "connecting to the setup server of clone {pid} failed: {e} ({})",
          cloneStatus(pid)
        ))
      })?;
    setupTx
      .send(CloneSetup { requestRx, responseTx })
      .map_err(|e| {
        low::killProcess(pid);
        io::Error::other(format!(
          "sending the channels to clone {pid} failed: {e} ({})",
          cloneStatus(pid)
        ))
      })?;
    drop(setupTx);

    Ok(Bootstrap { pid, requestTx, responseRx })
  }

  /// PID stored in the bootstrap message.
  fn bootstrapPid(bootstrap: &Self::Bootstrap) -> u32
  {
    bootstrap.pid
  }

  /// Enters the Main Zygote command loop.
  ///
  /// `flag`: the `IpcOneShotServer` name passed as `argv[2]`.
  fn zygoteControlLoop(flag: Option<String>) -> !
  {
    let serverName: String =
      flag.expect("windows::zygoteControlLoop: missing IpcOneShotServer name");
    zygoteLoop(serverName)
  }

  /// In a freshly cloned process: prepares the data endpoint. Dispatched
  /// through the trait, so Clippy sees it as "never used" — silenced here.
  #[allow(dead_code)]
  fn cloneEnter(flag: Option<String>) -> io::Result<(Self::CloneSide, Self::Bootstrap)>
  {
    let serverName: String =
      flag.expect("windows::cloneEnter: missing IpcOneShotServer name");
    cloneBootstrapLoop(serverName)
  }

  /// Turns a bootstrap message into a Runtime-side data endpoint.
  fn runtimeConnect(bootstrap: Self::Bootstrap) -> io::Result<Self::RuntimeSide>
  {
    Ok(RuntimeSide {
      pid: bootstrap.pid,
      requestTx: bootstrap.requestTx,
      responseRx: bootstrap.responseRx
    })
  }
}

// =================================================================================================

impl RuntimeSideTrait for RuntimeSide
{
  /// Sends an FFI request to the clone.
  fn send(&self, request: &FFIRequest) -> Result<(), String>
  {
    self
      .requestTx
      .send(request.clone())
      .map_err(|e| {
        format!(
          "Zygote clone IPC failed while sending request: {e} ({})",
          cloneStatus(self.pid)
        )
      })
  }

  /// Receives an FFI response from the clone.
  fn recv(&self) -> Result<FFIResponse, String>
  {
    self
      .responseRx
      .recv()
      .map_err(|e| {
        format!(
          "Zygote clone IPC failed while reading response: {e} ({})",
          cloneStatus(self.pid)
        )
      })
  }
}

impl CloneSideTrait for CloneSide
{
  /// Dispatched through the trait, so Clippy sees it as "never used" —
  /// silenced here.
  #[allow(dead_code)]
  fn run(self, cache: &mut FxHashMap<String, Library>) -> !
  {
    let Self { requestRx, responseTx } = self;
    let mut libraryCache: FxHashMap<String, Library> = std::mem::take(cache);

    loop
    {
      let request: FFIRequest = match requestRx.recv()
      {
        Ok(r) => r,
        Err(_) => std::process::exit(0)
      };

      // catch_unwind: a panic inside the clone must not abort the process
      // (the Runtime would only see a dead channel). Convert to
      // FFIResponse::Err. Note: true AVs / SEH still kill the process —
      // that is intentional isolation.
      let response: FFIResponse =
        match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
          handleRequest(request, &mut libraryCache)
        })) {
          Ok(r) => r,
          Err(_) => FFIResponse::Err(FFIError::Other(
            "clone panicked while handling request".into()
          ))
        };

      if responseTx.send(response).is_err() {
        std::process::exit(0);
      }
    }
  }
}

// =================================================================================================

/// Human-readable fate of a clone, for error messages.
fn cloneStatus(pid: u32) -> String
{
  match low::processExitCode(pid)
  {
    None => format!("clone {pid} is running"),
    Some(code) => format!(
      "clone {pid} exited with code {code:#x}: {}",
      cloneExitReason(code)
    )
  }
}

/// What an exit code of a clone most likely means.
const fn cloneExitReason(code: u32) -> &'static str
{
  match code
  {
    CloneExitSetupServer => "could not create its setup server",
    CloneExitConnect => "could not connect to the Runtime",
    CloneExitHello => "could not send its greeting to the Runtime",
    CloneExitSetup => "did not receive its channels",
    1 => "killed by the Runtime",
    0xC000_0005 => "access violation",
    0xC000_0409 => "fast fail (abort, or a panic that cannot unwind)",
    u32::MAX => "gone before it could be queried",
    _ => "unknown"
  }
}

/// Waits for the greeting of the clone `pid`.
///
/// `IpcOneShotServer::accept` has no timeout, and a clone that died (or hung)
/// before connecting would block the Runtime forever. A watchdog thread
/// watches the clone and, once it exits or [`CloneBootstrapTimeout`] passes,
/// unblocks `accept` by connecting to the server and dropping the connection.
/// The failure then carries the reason, including the exit code of the clone.
fn acceptHello(
  server: IpcOneShotServer<CloneHello>,
  serverName: &str,
  pid: u32
) -> io::Result<CloneHello>
{
  let finished: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));
  let watchdogFinished: Arc<AtomicBool> = Arc::clone(&finished);
  let watchdogName: String = serverName.to_owned();

  let spawned: io::Result<thread::JoinHandle<Option<String>>> = thread::Builder::new()
    .name("chillffi-clone-watchdog".into())
    .spawn(move || {
      let deadline: Instant = Instant::now() + CloneBootstrapTimeout;
      while !watchdogFinished.load(Ordering::Acquire)
      {
        let reason: Option<String> = if low::processExitCode(pid).is_some()
        {
          Some(format!("{} before bootstrap", cloneStatus(pid)))
        }
        else if Instant::now() >= deadline
        {
          Some(format!("clone {pid} did not bootstrap within {CloneBootstrapTimeout:?}"))
        }
        else
        {
          None
        };
        if let Some(reason) = reason
        {
          low::killProcess(pid);
          // A connection that carries nothing makes `accept` fail.
          let _ = IpcSender::<CloneHello>::connect(watchdogName);
          return Some(reason);
        }
        thread::sleep(Duration::from_millis(10));
      }
      None
    });
  let watchdog: thread::JoinHandle<Option<String>> = match spawned
  {
    Ok(handle) => handle,
    Err(e) =>
    {
      low::killProcess(pid);
      return Err(io::Error::other(format!("clone watchdog spawn failed: {e}")));
    }
  };

  let accepted = server.accept();
  finished.store(true, Ordering::Release);
  let reason: Option<String> = watchdog.join().unwrap_or(None);

  match accepted
  {
    Ok((_rx, hello)) => Ok(hello),
    Err(e) =>
    {
      // Never leave a clone nobody owns.
      low::killProcess(pid);
      Err(io::Error::other(
        reason.unwrap_or_else(|| format!("clone {pid} bootstrap accept failed: {e}"))
      ))
    }
  }
}

// =================================================================================================

/// Handles an incoming request and performs an FFI operation using the library cache.
fn handleRequest(request: FFIRequest, cache: &mut FxHashMap<String, Library>) -> FFIResponse
{
  match executeFFI(request, cache)
  {
    Ok(v) => FFIResponse::Ok(v, takeLastErrno(), takeLastOsError()),
    Err(e) => FFIResponse::Err(e)
  }
}

// =================================================================================================

/// Main zygote loop.
fn zygoteLoop(serverName: String) -> !
{
  low::ignoreChildExits();

  // Resolve the ntdll CSR data block [CsrServerApiRoutine .. RtlpEnvironLookupTable)
  // and kernelbase!CtrlRoutine once, in the healthy zygote, BEFORE any clone is
  // spawned. Children inherit the cached addresses via CoW and use them in
  // `reconnectCsr()` to zero the stale block, call CsrClientConnectToServer
  // for BASESRV + USERSRV, and RtlRegisterThreadWithCsrss. No-op on Unix.
  low::resolveCsrPortHandle();

  let (commandTx, commandRx): (
    IpcSender<ZygoteCommand>,
    IpcReceiver<ZygoteCommand>
  ) = match ipc::channel::<ZygoteCommand>() {
    Ok(p) => p,
    Err(_) => std::process::exit(1)
  };
  let (replyTx, replyRx): (IpcSender<ZygoteReply>, IpcReceiver<ZygoteReply>) =
    match ipc::channel::<ZygoteReply>() {
      Ok(p) => p,
      Err(_) => std::process::exit(1)
    };

  let bootstrapTx: IpcSender<BootstrapToRuntime> =
    match IpcSender::connect(serverName) {
      Ok(tx) => tx,
      Err(_) => std::process::exit(1)
    };
  if bootstrapTx
    .send(BootstrapToRuntime { commandTx, replyRx })
    .is_err()
  {
    std::process::exit(1);
  }
  drop(bootstrapTx);

  loop
  {
    let cmd: ZygoteCommand = match commandRx.recv() {
      Ok(c) => c,
      Err(_) => std::process::exit(0)
    };

    match cmd
    {
      ZygoteCommand::SpawnClone { bootstrapName } =>
      {
        match low::cloneProcess() {
          Ok(result) => {
            let pid: low::ProcessId = result.pid;
            // Thread already running (no CREATE_SUSPENDED).
            low::closeCloneHandles(&result);
            let _ = replyTx.send(ZygoteReply::Cloned { pid });
          }
          Err(low::StatusProcessCloned) => {
            std::mem::forget(commandRx);
            std::mem::forget(replyTx);

            // We are the clone. The inherited ntdll CSR data block
            // (CsrPortHandle, CsrInitOnceDone, CsrPortHeap, CsrHeap, ...)
            // references the parent's CSR_PROCESS on the csrss.exe side.
            // Any Win32/basesrv call (reattachConsole, _stat64 in
            // handleRequest, etc.) AVs and we die with ERROR_BROKEN_PIPE
            // (109). reconnectCsr() zeroes the whole block, calls
            // CsrClientConnectToServer for BASESRV + USERSRV against
            // \Sessions\{sid}\Windows, and registers the current thread
            // with RtlRegisterThreadWithCsrss. Best-effort: if symbols
            // were never resolved, we proceed anyway — same failure mode
            // as before this fix.
            let csrOk: bool = low::reconnectCsr();

            // reattachConsole goes through Win32 → CSRSS. If CSR was
            // not reconnected (ARM64 without a resolved block), the
            // stale ALPC port makes FreeConsole/AttachConsole hang —
            // the clone never reaches cloneBootstrapLoop, and the
            // Runtime waits for the bootstrap until its watchdog fires.
            if csrOk {
              low::reattachConsole();
            }
            low::silenceCrashReporting();
            cloneBootstrapLoop(bootstrapName)
          }
          Err(_) => {
            let _ = replyTx.send(ZygoteReply::SpawnFailed);
          }
        }
      }
    }
  }
}

/// Exits a clone that failed during its bootstrap.
fn cloneExit(code: u32) -> !
{
  std::process::exit(code as i32)
}

/// Bootstrap loop in a freshly cloned process.
///
/// Nothing that was created before the clone is usable in it (its handle
/// table is empty), so it starts from scratch: opens a setup server, greets
/// the Runtime (`helloName`) with the name of it, and receives its data
/// channels there.
fn cloneBootstrapLoop(helloName: String) -> !
{
  // Before the first UUID is drawn: see the module docs.
  low::decorrelateRandom();

  let (setupServer, setupName): (
    IpcOneShotServer<CloneSetup>,
    String
  ) = match IpcOneShotServer::new() {
    Ok(v) => v,
    Err(e) => {
      eprintln!("[clone] setup server failed: {e}");
      cloneExit(CloneExitSetupServer)
    }
  };

  // The Runtime owns this server (see the module docs).
  let helloTx: IpcSender<CloneHello> = match IpcSender::connect(helloName) {
    Ok(tx) => tx,
    Err(e) => {
      eprintln!("[clone] connect to the Runtime failed: {e}");
      cloneExit(CloneExitConnect)
    }
  };
  if let Err(e) = helloTx.send(CloneHello { setupName })
  {
    eprintln!("[clone] greeting failed: {e}");
    cloneExit(CloneExitHello);
  }
  drop(helloTx);

  let (setupRx, setup): (IpcReceiver<CloneSetup>, CloneSetup) =
    match setupServer.accept() {
      Ok(v) => v,
      Err(e) => {
        eprintln!("[clone] receiving the channels failed: {e}");
        cloneExit(CloneExitSetup)
      }
    };
  // Free the name of the setup server: the next clone may draw the same one.
  drop(setupRx);

  let cache: &mut FxHashMap<String, Library> =
    Box::leak(Box::new(FxHashMap::default()));
  CloneSide {
    requestRx: setup.requestRx,
    responseTx: setup.responseTx
  }.run(cache)
}

/// Legacy Command-based clone entry (kept for compatibility).
pub fn runAsClone() -> !
{
  let serverName: String = env::args()
    .nth(2)
    .expect("zygote clone: missing IpcOneShotServer name (argv[2])");

  low::silenceCrashReporting();
  cloneBootstrapLoop(serverName)
}

// =================================================================================================