Skip to main content

hydro_lang/sim/
compiled.rs

1//! Interfaces for compiled Hydro simulators and concrete simulation instances.
2
3use core::{fmt, panic};
4use std::cell::RefCell;
5use std::collections::{HashMap, VecDeque};
6use std::fmt::Debug;
7use std::panic::RefUnwindSafe;
8use std::path::Path;
9use std::pin::{Pin, pin};
10use std::rc::Rc;
11use std::task::ready;
12
13use bytes::Bytes;
14use colored::Colorize;
15use dfir_rs::scheduled::graph::Dfir;
16use futures::{Stream, StreamExt};
17use libloading::Library;
18use serde::Serialize;
19use serde::de::DeserializeOwned;
20use tempfile::TempPath;
21use tokio::sync::Mutex;
22use tokio::sync::mpsc::UnboundedSender;
23use tokio_stream::wrappers::UnboundedReceiverStream;
24
25use super::runtime::{Hooks, InlineHooks};
26use super::{SimClusterReceiver, SimClusterSender, SimReceiver, SimSender};
27use crate::compile::builder::ExternalPortId;
28use crate::live_collections::stream::{ExactlyOnce, NoOrder, Ordering, Retries, TotalOrder};
29use crate::location::dynamic::LocationId;
30use crate::sim::graph::{SimExternalPort, SimExternalPortRegistry};
31use crate::sim::runtime::SimHook;
32
33struct SimConnections {
34    input_senders: HashMap<SimExternalPort, Rc<UnboundedSender<Bytes>>>,
35    output_receivers: HashMap<SimExternalPort, Rc<Mutex<UnboundedReceiverStream<Bytes>>>>,
36    cluster_input_senders: HashMap<SimExternalPort, Vec<Rc<UnboundedSender<Bytes>>>>,
37    cluster_output_receivers:
38        HashMap<SimExternalPort, Vec<Rc<Mutex<UnboundedReceiverStream<Bytes>>>>>,
39    external_registered: HashMap<ExternalPortId, SimExternalPort>,
40}
41
42tokio::task_local! {
43    static CURRENT_SIM_CONNECTIONS: RefCell<SimConnections>;
44}
45
46/// A handle to a compiled Hydro simulation, which can be instantiated and run.
47pub struct CompiledSim {
48    pub(super) _path: TempPath,
49    pub(super) lib: Library,
50    pub(super) externals_port_registry: SimExternalPortRegistry,
51}
52
53#[sealed::sealed]
54/// A trait implemented by closures that can instantiate a compiled simulation.
55///
56/// This is needed to ensure [`RefUnwindSafe`] so instances can be created during fuzzing.
57pub trait Instantiator<'a>: RefUnwindSafe + Fn() -> CompiledSimInstance<'a> {}
58#[sealed::sealed]
59impl<'a, T: RefUnwindSafe + Fn() -> CompiledSimInstance<'a>> Instantiator<'a> for T {}
60
61fn null_handler(_args: fmt::Arguments) {}
62
63fn println_handler(args: fmt::Arguments) {
64    println!("{}", args);
65}
66
67fn eprintln_handler(args: fmt::Arguments) {
68    eprintln!("{}", args);
69}
70
71/// Creates a simulation instance, returning:
72/// - A list of async DFIRs to run (all process / cluster logic outside a tick)
73/// - A list of tick DFIRs to run (where the &'static str is for the tick location id)
74/// - A mapping of hooks for non-deterministic decisions at tick-input boundaries
75/// - A mapping of inline hooks for non-deterministic decisions inside ticks
76type SimLoaded<'a> = libloading::Symbol<
77    'a,
78    unsafe extern "Rust" fn(
79        should_color: bool,
80        external_out: &mut HashMap<usize, UnboundedReceiverStream<Bytes>>,
81        external_in: &mut HashMap<usize, UnboundedSender<Bytes>>,
82        cluster_external_out: &mut HashMap<usize, Vec<UnboundedReceiverStream<Bytes>>>,
83        cluster_external_in: &mut HashMap<usize, Vec<UnboundedSender<Bytes>>>,
84        println_handler: fn(fmt::Arguments<'_>),
85        eprintln_handler: fn(fmt::Arguments<'_>),
86    ) -> (
87        Vec<(&'static str, Option<u32>, Dfir<'static>)>,
88        Vec<(&'static str, Option<u32>, Dfir<'static>)>,
89        Hooks<&'static str>,
90        InlineHooks<&'static str>,
91    ),
92>;
93
94impl CompiledSim {
95    /// Executes the given closure with a single instance of the compiled simulation.
96    pub fn with_instance<T>(&self, thunk: impl FnOnce(CompiledSimInstance) -> T) -> T {
97        self.with_instantiator(|instantiator| thunk(instantiator()), true)
98    }
99
100    /// Executes the given closure with an [`Instantiator`], which can be called to create
101    /// independent instances of the simulation. This is useful for fuzzing, where we need to
102    /// re-execute the simulation several times with different decisions.
103    ///
104    /// The `always_log` parameter controls whether to log tick executions and stream releases. If
105    /// it is `true`, logging will always be enabled. If it is `false`, logging will only be
106    /// enabled if the `HYDRO_SIM_LOG` environment variable is set to `1`.
107    pub fn with_instantiator<T>(
108        &self,
109        thunk: impl FnOnce(&dyn Instantiator) -> T,
110        always_log: bool,
111    ) -> T {
112        let func: SimLoaded = unsafe { self.lib.get(b"__hydro_runtime").unwrap() };
113        let log = always_log || std::env::var("HYDRO_SIM_LOG").is_ok_and(|v| v == "1");
114        thunk(
115            &(|| CompiledSimInstance {
116                func: func.clone(),
117                externals_port_registry: self.externals_port_registry.clone(),
118                dylib_result: None,
119                log,
120            }),
121        )
122    }
123
124    /// Uses a fuzzing strategy to explore possible executions of the simulation. The provided
125    /// closure will be repeatedly executed with instances of the Hydro program where the
126    /// batching boundaries, order of messages, and retries are varied.
127    ///
128    /// During development, you should run the test that invokes this function with the `cargo sim`
129    /// command, which will use `libfuzzer` to intelligently explore the execution space. If a
130    /// failure is found, a minimized test case will be produced in a `sim-failures` directory.
131    /// When running the test with `cargo test` (such as in CI), if a reproducer is found it will
132    /// be executed, and if no reproducer is found a small number of random executions will be
133    /// performed.
134    pub fn fuzz(&self, mut thunk: impl AsyncFn() + RefUnwindSafe) {
135        let caller_fn = crate::compile::ir::backtrace::Backtrace::get_backtrace(0)
136            .elements()
137            .into_iter()
138            .find(|e| {
139                !e.fn_name.starts_with("hydro_lang::sim::compiled")
140                    && !e.fn_name.starts_with("hydro_lang::sim::flow")
141                    && !e.fn_name.starts_with("fuzz<")
142                    && !e.fn_name.starts_with("<hydro_lang::sim")
143            })
144            .unwrap();
145
146        let caller_path = Path::new(&caller_fn.filename.unwrap()).to_path_buf();
147        let repro_folder = caller_path.parent().unwrap().join("sim-failures");
148
149        let caller_fuzz_repro_path = repro_folder
150            .join(caller_fn.fn_name.replace("::", "__"))
151            .with_extension("bin");
152
153        if std::env::var("BOLERO_FUZZER").is_ok() {
154            let corpus_dir = std::env::current_dir().unwrap().join(".fuzz-corpus");
155            std::fs::create_dir_all(&corpus_dir).unwrap();
156            let libfuzzer_args = format!(
157                "{} {} -artifact_prefix={}/ -handle_abrt=0",
158                corpus_dir.to_str().unwrap(),
159                corpus_dir.to_str().unwrap(),
160                corpus_dir.to_str().unwrap(),
161            );
162
163            std::fs::create_dir_all(&repro_folder).unwrap();
164
165            if !std::env::var("HYDRO_NO_FAILURE_OUTPUT").is_ok_and(|v| v == "1") {
166                unsafe {
167                    std::env::set_var(
168                        "BOLERO_FAILURE_OUTPUT",
169                        caller_fuzz_repro_path.to_str().unwrap(),
170                    );
171                }
172            }
173
174            unsafe {
175                std::env::set_var("BOLERO_LIBFUZZER_ARGS", libfuzzer_args);
176            }
177
178            self.with_instantiator(
179                |instantiator| {
180                    bolero::test(bolero::TargetLocation {
181                        package_name: "",
182                        manifest_dir: "",
183                        module_path: "",
184                        file: "",
185                        line: 0,
186                        item_path: "<unknown>::__bolero_item_path__",
187                        test_name: None,
188                    })
189                    .run_with_replay(move |is_replay| {
190                        let mut instance = instantiator();
191
192                        if instance.log {
193                            eprintln!(
194                                "{}",
195                                "\n==== New Simulation Instance ===="
196                                    .color(colored::Color::Cyan)
197                                    .bold()
198                            );
199                        }
200
201                        if is_replay {
202                            instance.log = true;
203                        }
204
205                        tokio::runtime::Builder::new_current_thread()
206                            .build()
207                            .unwrap()
208                            .block_on(async { instance.run(&mut thunk).await })
209                    })
210                },
211                false,
212            );
213        } else if let Ok(existing_bytes) = std::fs::read(&caller_fuzz_repro_path) {
214            self.fuzz_repro(existing_bytes, async |compiled| {
215                compiled.launch();
216                thunk().await
217            });
218        } else {
219            eprintln!(
220                "Running a fuzz test without `cargo sim` and no reproducer found at {}, defaulting to 8192 iterations with random inputs.",
221                caller_fuzz_repro_path.display()
222            );
223            self.with_instantiator(
224                |instantiator| {
225                    bolero::test(bolero::TargetLocation {
226                        package_name: "",
227                        manifest_dir: "",
228                        module_path: "",
229                        file: ".",
230                        line: 0,
231                        item_path: "<unknown>::__bolero_item_path__",
232                        test_name: None,
233                    })
234                    .with_iterations(8192)
235                    .run(move || {
236                        let instance = instantiator();
237                        tokio::runtime::Builder::new_current_thread()
238                            .build()
239                            .unwrap()
240                            .block_on(async { instance.run(&mut thunk).await })
241                    })
242                },
243                false,
244            );
245        }
246    }
247
248    /// Executes the given closure with a single instance of the compiled simulation, using the
249    /// provided bytes as the source of fuzzing decisions. This can be used to manually reproduce a
250    /// failure found during fuzzing.
251    pub fn fuzz_repro<'a>(
252        &'a self,
253        bytes: Vec<u8>,
254        thunk: impl AsyncFnOnce(CompiledSimInstance) + RefUnwindSafe,
255    ) {
256        self.with_instance(|instance| {
257            bolero::bolero_engine::any::scope::with(
258                Box::new(bolero::bolero_engine::driver::object::Object(
259                    bolero::bolero_engine::driver::bytes::Driver::new(bytes, &Default::default()),
260                )),
261                || {
262                    tokio::runtime::Builder::new_current_thread()
263                        .build()
264                        .unwrap()
265                        .block_on(async { instance.run_without_launching(thunk).await })
266                },
267            )
268        });
269    }
270
271    /// Exhaustively searches all possible executions of the simulation. The provided
272    /// closure will be repeatedly executed with instances of the Hydro program where the
273    /// batching boundaries, order of messages, and retries are varied.
274    ///
275    /// Exhaustive searching is feasible when the inputs to the Hydro program are finite and there
276    /// are no dataflow loops that generate infinite messages. Exhaustive searching provides a
277    /// stronger guarantee of correctness than fuzzing, but may take a long time to complete.
278    /// Because no fuzzer is involved, you can run exhaustive tests with `cargo test`.
279    ///
280    /// Returns the number of distinct executions explored.
281    pub fn exhaustive(&self, mut thunk: impl AsyncFnMut() + RefUnwindSafe) -> usize {
282        if std::env::var("BOLERO_FUZZER").is_ok() {
283            eprintln!(
284                "Cannot run exhaustive tests with a fuzzer. Please use `cargo test` instead of `cargo sim`."
285            );
286            std::process::abort();
287        }
288
289        let mut count = 0;
290        let count_mut = &mut count;
291
292        self.with_instantiator(
293            |instantiator| {
294                bolero::test(bolero::TargetLocation {
295                    package_name: "",
296                    manifest_dir: "",
297                    module_path: "",
298                    file: "",
299                    line: 0,
300                    item_path: "<unknown>::__bolero_item_path__",
301                    test_name: None,
302                })
303                .exhaustive()
304                .run_with_replay(move |is_replay| {
305                    *count_mut += 1;
306
307                    let mut instance = instantiator();
308                    if instance.log {
309                        eprintln!(
310                            "{}",
311                            "\n==== New Simulation Instance ===="
312                                .color(colored::Color::Cyan)
313                                .bold()
314                        );
315                    }
316
317                    if is_replay {
318                        instance.log = true;
319                    }
320
321                    tokio::runtime::Builder::new_current_thread()
322                        .build()
323                        .unwrap()
324                        .block_on(async { instance.run(&mut thunk).await })
325                })
326            },
327            false,
328        );
329
330        count
331    }
332}
333
334type DylibResult = (
335    Vec<(&'static str, Option<u32>, Dfir<'static>)>,
336    Vec<(&'static str, Option<u32>, Dfir<'static>)>,
337    Hooks<&'static str>,
338    InlineHooks<&'static str>,
339);
340
341/// A single instance of a compiled Hydro simulation, which provides methods to interactively
342/// execute the simulation, feed inputs, and receive outputs.
343pub struct CompiledSimInstance<'a> {
344    func: SimLoaded<'a>,
345    externals_port_registry: SimExternalPortRegistry,
346    dylib_result: Option<DylibResult>,
347    log: bool,
348}
349
350impl<'a> CompiledSimInstance<'a> {
351    async fn run(self, thunk: impl AsyncFnOnce() + RefUnwindSafe) {
352        self.run_without_launching(async |instance| {
353            instance.launch();
354            thunk().await;
355        })
356        .await;
357    }
358
359    async fn run_without_launching(
360        mut self,
361        thunk: impl AsyncFnOnce(CompiledSimInstance) + RefUnwindSafe,
362    ) {
363        let mut external_out: HashMap<usize, UnboundedReceiverStream<Bytes>> = HashMap::new();
364        let mut external_in: HashMap<usize, UnboundedSender<Bytes>> = HashMap::new();
365        let mut cluster_external_out: HashMap<usize, Vec<UnboundedReceiverStream<Bytes>>> =
366            HashMap::new();
367        let mut cluster_external_in: HashMap<usize, Vec<UnboundedSender<Bytes>>> = HashMap::new();
368
369        let dylib_result = unsafe {
370            (self.func)(
371                colored::control::SHOULD_COLORIZE.should_colorize(),
372                &mut external_out,
373                &mut external_in,
374                &mut cluster_external_out,
375                &mut cluster_external_in,
376                if self.log {
377                    println_handler
378                } else {
379                    null_handler
380                },
381                if self.log {
382                    eprintln_handler
383                } else {
384                    null_handler
385                },
386            )
387        };
388
389        let registered = &self.externals_port_registry.registered;
390
391        let mut input_senders = HashMap::new();
392        let mut output_receivers = HashMap::new();
393        let mut cluster_input_senders = HashMap::new();
394        let mut cluster_output_receivers = HashMap::new();
395
396        for (_port_id, sim_port) in registered {
397            let usize_key = sim_port.into_inner();
398            if let Some(sender) = external_in.remove(&usize_key) {
399                input_senders.insert(*sim_port, Rc::new(sender));
400            }
401            if let Some(receiver) = external_out.remove(&usize_key) {
402                output_receivers.insert(*sim_port, Rc::new(Mutex::new(receiver)));
403            }
404            if let Some(senders) = cluster_external_in.remove(&usize_key) {
405                cluster_input_senders.insert(*sim_port, senders.into_iter().map(Rc::new).collect());
406            }
407            if let Some(receivers) = cluster_external_out.remove(&usize_key) {
408                cluster_output_receivers.insert(
409                    *sim_port,
410                    receivers
411                        .into_iter()
412                        .map(|r| Rc::new(Mutex::new(r)))
413                        .collect(),
414                );
415            }
416        }
417
418        self.dylib_result = Some(dylib_result);
419
420        let local_set = tokio::task::LocalSet::new();
421        local_set
422            .run_until(CURRENT_SIM_CONNECTIONS.scope(
423                RefCell::new(SimConnections {
424                    input_senders,
425                    output_receivers,
426                    cluster_input_senders,
427                    cluster_output_receivers,
428                    external_registered: self.externals_port_registry.registered.clone(),
429                }),
430                async move {
431                    thunk(self).await;
432                },
433            ))
434            .await;
435    }
436
437    /// Launches the simulation, which will asynchronously simulate the Hydro program. This should
438    /// be invoked but before receiving any messages.
439    fn launch(self) {
440        tokio::task::spawn_local(self.schedule_with_maybe_logger::<std::io::Empty>(None));
441    }
442
443    /// Returns a future that schedules simulation with the given logger for reporting the
444    /// simulation trace.
445    pub fn schedule_with_logger<W: std::io::Write>(
446        self,
447        log_writer: W,
448    ) -> impl use<W> + Future<Output = ()> {
449        self.schedule_with_maybe_logger(Some(log_writer))
450    }
451
452    fn schedule_with_maybe_logger<W: std::io::Write>(
453        mut self,
454        log_override: Option<W>,
455    ) -> impl use<W> + Future<Output = ()> {
456        let (async_dfirs, tick_dfirs, hooks, inline_hooks) = self.dylib_result.take().unwrap();
457
458        let not_ready_observation = async_dfirs
459            .iter()
460            .map(|(lid, c_id, _)| (serde_json::from_str(lid).unwrap(), *c_id))
461            .collect();
462
463        let mut launched = LaunchedSim {
464            async_dfirs: async_dfirs
465                .into_iter()
466                .map(|(lid, c_id, dfir)| (serde_json::from_str(lid).unwrap(), c_id, dfir))
467                .collect(),
468            possibly_ready_ticks: vec![],
469            not_ready_ticks: tick_dfirs
470                .into_iter()
471                .map(|(lid, c_id, dfir)| (serde_json::from_str(lid).unwrap(), c_id, dfir))
472                .collect(),
473            possibly_ready_observation: vec![],
474            not_ready_observation,
475            hooks: hooks
476                .into_iter()
477                .map(|((lid, cid), hs)| ((serde_json::from_str(lid).unwrap(), cid), hs))
478                .collect(),
479            inline_hooks: inline_hooks
480                .into_iter()
481                .map(|((lid, cid), hs)| ((serde_json::from_str(lid).unwrap(), cid), hs))
482                .collect(),
483            log: if self.log {
484                if let Some(w) = log_override {
485                    LogKind::Custom(w)
486                } else {
487                    LogKind::Stderr
488                }
489            } else {
490                LogKind::Null
491            },
492        };
493
494        async move { launched.scheduler().await }
495    }
496}
497
498impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> Clone for SimReceiver<T, O, R> {
499    fn clone(&self) -> Self {
500        *self
501    }
502}
503
504impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> Copy for SimReceiver<T, O, R> {}
505
506impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> SimReceiver<T, O, R> {
507    async fn with_stream<Out>(
508        &self,
509        thunk: impl AsyncFnOnce(&mut Pin<&mut dyn Stream<Item = T>>) -> Out,
510    ) -> Out {
511        let receiver = CURRENT_SIM_CONNECTIONS.with(|connections| {
512            let connections = &mut *connections.borrow_mut();
513            connections
514                .output_receivers
515                .get(connections.external_registered.get(&self.0).unwrap())
516                .unwrap()
517                .clone()
518        });
519
520        let mut receiver_stream = receiver.lock().await;
521        thunk(&mut pin!(
522            &mut receiver_stream
523                .by_ref()
524                .map(|b| bincode::deserialize(&b).unwrap())
525        ))
526        .await
527    }
528
529    /// Asserts that the stream has ended and no more messages can possibly arrive.
530    pub fn assert_no_more(self) -> impl Future<Output = ()>
531    where
532        T: Debug,
533    {
534        FutureTrackingCaller {
535            future: async move {
536                self.with_stream(async |stream| {
537                    if let Some(next) = stream.next().await {
538                        return Err(format!(
539                            "Stream yielded unexpected message: {:?}, expected termination",
540                            next
541                        ));
542                    }
543                    Ok(())
544                })
545                .await
546            },
547        }
548    }
549}
550
551impl<T: Serialize + DeserializeOwned> SimReceiver<T, TotalOrder, ExactlyOnce> {
552    /// Receives the next message from the external bincode stream. This will wait until a message
553    /// is available, or return `None` if no more messages can possibly arrive.
554    pub async fn next(&self) -> Option<T> {
555        self.with_stream(async |stream| stream.next().await).await
556    }
557
558    /// Collects all remaining messages from the external bincode stream into a collection. This
559    /// will wait until no more messages can possibly arrive.
560    pub async fn collect<C: Default + Extend<T>>(self) -> C {
561        self.with_stream(async |stream| stream.collect().await)
562            .await
563    }
564
565    /// Asserts that the stream yields exactly the expected sequence of messages, in order.
566    /// This does not check that the stream ends, use [`Self::assert_yields_only`] for that.
567    pub fn assert_yields<T2: Debug, I: IntoIterator<Item = T2>>(
568        &self,
569        expected: I,
570    ) -> impl use<'_, T, T2, I> + Future<Output = ()>
571    where
572        T: Debug + PartialEq<T2>,
573    {
574        FutureTrackingCaller {
575            future: async {
576                let mut expected: VecDeque<T2> = expected.into_iter().collect();
577
578                while !expected.is_empty() {
579                    if let Some(next) = self.next().await {
580                        let next_expected = expected.pop_front().unwrap();
581                        if next != next_expected {
582                            return Err(format!(
583                                "Stream yielded unexpected message: {:?}, expected: {:?}",
584                                next, next_expected
585                            ));
586                        }
587                    } else {
588                        return Err(format!(
589                            "Stream ended early, still expected: {:?}",
590                            expected
591                        ));
592                    }
593                }
594
595                Ok(())
596            },
597        }
598    }
599
600    /// Asserts that the stream yields only the expected sequence of messages, in order,
601    /// and then ends.
602    pub fn assert_yields_only<T2: Debug, I: IntoIterator<Item = T2>>(
603        &self,
604        expected: I,
605    ) -> impl use<'_, T, T2, I> + Future<Output = ()>
606    where
607        T: Debug + PartialEq<T2>,
608    {
609        ChainedFuture {
610            first: self.assert_yields(expected),
611            second: self.assert_no_more(),
612            first_done: false,
613        }
614    }
615}
616
617pin_project_lite::pin_project! {
618    // A future that tracks the location of the `.await` call for better panic messages.
619    //
620    // `#[track_caller]` is important for us to create assertion methods because it makes
621    // the panic backtrace show up at that method (instead of inside the call tree within
622    // that method). This is e.g. what `Option::unwrap` uses. Unfortunately, `#[track_caller]`
623    // does not work correctly for async methods (or `dyn Future` either), so we have to
624    // create these concrete future types that (1) have `#[track_caller]` on their `poll()`
625    // method and (2) have the `panic!` triggered in their `poll()` method (or in a directly
626    // nested concrete future).
627    struct FutureTrackingCaller<F: Future<Output = Result<(), String>>> {
628        #[pin]
629        future: F,
630    }
631}
632
633impl<F: Future<Output = Result<(), String>>> Future for FutureTrackingCaller<F> {
634    type Output = ();
635
636    #[track_caller]
637    fn poll(
638        mut self: Pin<&mut Self>,
639        cx: &mut std::task::Context<'_>,
640    ) -> std::task::Poll<Self::Output> {
641        match ready!(self.as_mut().project().future.poll(cx)) {
642            Ok(()) => std::task::Poll::Ready(()),
643            Err(e) => panic!("{}", e),
644        }
645    }
646}
647
648pin_project_lite::pin_project! {
649    // A future that first awaits the first future, then the second, propagating caller info.
650    //
651    // See [`FutureTrackingCaller`] for context.
652    struct ChainedFuture<F1: Future<Output = ()>, F2: Future<Output = ()>> {
653        #[pin]
654        first: F1,
655        #[pin]
656        second: F2,
657        first_done: bool,
658    }
659}
660
661impl<F1: Future<Output = ()>, F2: Future<Output = ()>> Future for ChainedFuture<F1, F2> {
662    type Output = ();
663
664    #[track_caller]
665    fn poll(
666        mut self: Pin<&mut Self>,
667        cx: &mut std::task::Context<'_>,
668    ) -> std::task::Poll<Self::Output> {
669        if !self.first_done {
670            ready!(self.as_mut().project().first.poll(cx));
671            *self.as_mut().project().first_done = true;
672        }
673
674        self.as_mut().project().second.poll(cx)
675    }
676}
677
678impl<T: Serialize + DeserializeOwned> SimReceiver<T, NoOrder, ExactlyOnce> {
679    /// Collects all remaining messages from the external bincode stream into a collection,
680    /// sorting them. This will wait until no more messages can possibly arrive.
681    pub async fn collect_sorted<C: Default + Extend<T> + AsMut<[T]>>(self) -> C
682    where
683        T: Ord,
684    {
685        self.with_stream(async |stream| {
686            let mut collected: C = stream.collect().await;
687            collected.as_mut().sort();
688            collected
689        })
690        .await
691    }
692
693    /// Asserts that the stream yields exactly the expected sequence of messages, in some order.
694    /// This does not check that the stream ends, use [`Self::assert_yields_only_unordered`] for that.
695    pub fn assert_yields_unordered<T2: Debug, I: IntoIterator<Item = T2>>(
696        &self,
697        expected: I,
698    ) -> impl use<'_, T, T2, I> + Future<Output = ()>
699    where
700        T: Debug + PartialEq<T2>,
701    {
702        FutureTrackingCaller {
703            future: async {
704                self.with_stream(async |stream| {
705                    let mut expected: Vec<T2> = expected.into_iter().collect();
706
707                    while !expected.is_empty() {
708                        if let Some(next) = stream.next().await {
709                            let idx = expected.iter().enumerate().find(|(_, e)| &next == *e);
710                            if let Some((i, _)) = idx {
711                                expected.swap_remove(i);
712                            } else {
713                                return Err(format!(
714                                    "Stream yielded unexpected message: {:?}",
715                                    next
716                                ));
717                            }
718                        } else {
719                            return Err(format!(
720                                "Stream ended early, still expected: {:?}",
721                                expected
722                            ));
723                        }
724                    }
725
726                    Ok(())
727                })
728                .await
729            },
730        }
731    }
732
733    /// Asserts that the stream yields only the expected sequence of messages, in some order,
734    /// and then ends.
735    pub fn assert_yields_only_unordered<T2: Debug, I: IntoIterator<Item = T2>>(
736        &self,
737        expected: I,
738    ) -> impl use<'_, T, T2, I> + Future<Output = ()>
739    where
740        T: Debug + PartialEq<T2>,
741    {
742        ChainedFuture {
743            first: self.assert_yields_unordered(expected),
744            second: self.assert_no_more(),
745            first_done: false,
746        }
747    }
748}
749
750impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> SimSender<T, O, R> {
751    fn with_sink<Out>(
752        &self,
753        thunk: impl FnOnce(&dyn Fn(T) -> Result<(), tokio::sync::mpsc::error::SendError<Bytes>>) -> Out,
754    ) -> Out {
755        let sender = CURRENT_SIM_CONNECTIONS.with(|connections| {
756            let connections = &mut *connections.borrow_mut();
757            connections
758                .input_senders
759                .get(connections.external_registered.get(&self.0).unwrap())
760                .unwrap()
761                .clone()
762        });
763
764        thunk(&move |t| sender.send(bincode::serialize(&t).unwrap().into()))
765    }
766}
767
768impl<T: Serialize + DeserializeOwned, O: Ordering> SimSender<T, O, ExactlyOnce> {
769    /// Sends several messages to the external bincode sink. The messages will be asynchronously
770    /// processed as part of the simulation, in non-deterministic order.
771    pub fn send_many_unordered<I: IntoIterator<Item = T>>(&self, iter: I) {
772        self.with_sink(|send| {
773            for t in iter {
774                send(t).unwrap();
775            }
776        })
777    }
778}
779
780impl<T: Serialize + DeserializeOwned> SimSender<T, TotalOrder, ExactlyOnce> {
781    /// Sends a message to the external bincode sink. The message will be asynchronously processed
782    /// as part of the simulation.
783    pub fn send(&self, t: T) {
784        self.with_sink(|send| send(t)).unwrap();
785    }
786
787    /// Sends several messages to the external bincode sink. The messages will be asynchronously
788    /// processed as part of the simulation.
789    pub fn send_many<I: IntoIterator<Item = T>>(&self, iter: I) {
790        self.with_sink(|send| {
791            for t in iter {
792                send(t).unwrap();
793            }
794        })
795    }
796}
797
798impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> Clone
799    for SimClusterReceiver<T, O, R>
800{
801    fn clone(&self) -> Self {
802        *self
803    }
804}
805
806impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> Copy
807    for SimClusterReceiver<T, O, R>
808{
809}
810
811impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> SimClusterReceiver<T, O, R> {
812    async fn with_member_stream<Out>(
813        &self,
814        member_id: u32,
815        thunk: impl AsyncFnOnce(&mut Pin<&mut dyn Stream<Item = T>>) -> Out,
816    ) -> Out {
817        let receiver = CURRENT_SIM_CONNECTIONS.with(|connections| {
818            let connections = &mut *connections.borrow_mut();
819            let receivers = connections
820                .cluster_output_receivers
821                .get(connections.external_registered.get(&self.0).unwrap())
822                .unwrap();
823            receivers[member_id as usize].clone()
824        });
825
826        let mut lock = receiver.lock().await;
827        thunk(&mut pin!(
828            lock.by_ref().map(|b| bincode::deserialize(&b).unwrap())
829        ))
830        .await
831    }
832}
833
834impl<T: Serialize + DeserializeOwned> SimClusterReceiver<T, TotalOrder, ExactlyOnce> {
835    /// Receives the next value from a specific cluster member.
836    pub async fn next(&self, member_id: u32) -> Option<T> {
837        self.with_member_stream(member_id, async |stream| stream.next().await)
838            .await
839    }
840
841    /// Collects all remaining values from a specific cluster member into a collection.
842    pub async fn collect<C: Default + Extend<T>>(self, member_id: u32) -> C {
843        self.with_member_stream(member_id, async |stream| stream.collect().await)
844            .await
845    }
846}
847
848impl<T: Serialize + DeserializeOwned> SimClusterReceiver<T, NoOrder, ExactlyOnce> {
849    /// Collects all remaining values from a specific cluster member, sorted.
850    pub async fn collect_sorted<C: Default + Extend<T> + AsMut<[T]>>(self, member_id: u32) -> C
851    where
852        T: Ord,
853    {
854        self.with_member_stream(member_id, async |stream| {
855            let mut collected: C = stream.collect().await;
856            collected.as_mut().sort();
857            collected
858        })
859        .await
860    }
861}
862
863impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> SimClusterSender<T, O, R> {
864    fn with_sink<Out>(
865        &self,
866        thunk: impl FnOnce(
867            &dyn Fn(u32, T) -> Result<(), tokio::sync::mpsc::error::SendError<Bytes>>,
868        ) -> Out,
869    ) -> Out {
870        let senders = CURRENT_SIM_CONNECTIONS.with(|connections| {
871            let connections = &mut *connections.borrow_mut();
872            connections
873                .cluster_input_senders
874                .get(connections.external_registered.get(&self.0).unwrap())
875                .unwrap()
876                .clone()
877        });
878
879        thunk(&move |member_id: u32, t: T| {
880            let payload = bincode::serialize(&t).unwrap();
881            senders[member_id as usize].send(Bytes::from(payload))
882        })
883    }
884}
885
886impl<T: Serialize + DeserializeOwned> SimClusterSender<T, TotalOrder, ExactlyOnce> {
887    /// Sends a value to a specific cluster member.
888    pub fn send(&self, member_id: u32, t: T) {
889        self.with_sink(|send| send(member_id, t)).unwrap();
890    }
891
892    /// Sends multiple values to specific cluster members.
893    pub fn send_many<I: IntoIterator<Item = (u32, T)>>(&self, iter: I) {
894        self.with_sink(|send| {
895            for (member_id, t) in iter {
896                send(member_id, t).unwrap();
897            }
898        })
899    }
900}
901
902enum LogKind<W: std::io::Write> {
903    Null,
904    Stderr,
905    Custom(W),
906}
907
908// via https://www.reddit.com/r/rust/comments/t69sld/is_there_a_way_to_allow_either_stdfmtwrite_or/
909impl<W: std::io::Write> std::fmt::Write for LogKind<W> {
910    fn write_str(&mut self, s: &str) -> Result<(), std::fmt::Error> {
911        match self {
912            LogKind::Null => Ok(()),
913            LogKind::Stderr => {
914                eprint!("{}", s);
915                Ok(())
916            }
917            LogKind::Custom(w) => w.write_all(s.as_bytes()).map_err(|_| std::fmt::Error),
918        }
919    }
920}
921
922/// A running simulation, which manages the async DFIR and tick DFIRs, and makes decisions
923/// about scheduling ticks and choices for non-deterministic operators like batch.
924struct LaunchedSim<W: std::io::Write> {
925    async_dfirs: Vec<(LocationId, Option<u32>, Dfir<'static>)>,
926    possibly_ready_ticks: Vec<(LocationId, Option<u32>, Dfir<'static>)>,
927    not_ready_ticks: Vec<(LocationId, Option<u32>, Dfir<'static>)>,
928    possibly_ready_observation: Vec<(LocationId, Option<u32>)>,
929    not_ready_observation: Vec<(LocationId, Option<u32>)>,
930    hooks: Hooks<LocationId>,
931    inline_hooks: InlineHooks<LocationId>,
932    log: LogKind<W>,
933}
934
935impl<W: std::io::Write> LaunchedSim<W> {
936    async fn scheduler(&mut self) {
937        loop {
938            tokio::task::yield_now().await;
939            let mut any_made_progress = false;
940            for (loc, c_id, dfir) in &mut self.async_dfirs {
941                if dfir.run_tick().await {
942                    any_made_progress = true;
943                    let (now_ready, still_not_ready): (Vec<_>, Vec<_>) = self
944                        .not_ready_ticks
945                        .drain(..)
946                        .partition(|(tick_loc, tick_c_id, _)| {
947                            let LocationId::Tick(_, outer) = tick_loc else {
948                                unreachable!()
949                            };
950                            outer.as_ref() == loc && tick_c_id == c_id
951                        });
952
953                    self.possibly_ready_ticks.extend(now_ready);
954                    self.not_ready_ticks.extend(still_not_ready);
955
956                    let (now_ready_obs, still_not_ready_obs): (Vec<_>, Vec<_>) = self
957                        .not_ready_observation
958                        .drain(..)
959                        .partition(|(obs_loc, obs_c_id)| obs_loc == loc && obs_c_id == c_id);
960
961                    self.possibly_ready_observation.extend(now_ready_obs);
962                    self.not_ready_observation.extend(still_not_ready_obs);
963                }
964            }
965
966            if any_made_progress {
967                continue;
968            } else {
969                use bolero::generator::*;
970
971                let (ready_tick, mut not_ready_tick): (Vec<_>, Vec<_>) = self
972                    .possibly_ready_ticks
973                    .drain(..)
974                    .partition(|(name, cid, _)| {
975                        self.hooks
976                            .get(&(name.clone(), *cid))
977                            .unwrap()
978                            .iter()
979                            .any(|hook| {
980                                hook.current_decision().unwrap_or(false)
981                                    || hook.can_make_nontrivial_decision()
982                            })
983                    });
984
985                self.possibly_ready_ticks = ready_tick;
986                self.not_ready_ticks.append(&mut not_ready_tick);
987
988                let (ready_obs, mut not_ready_obs): (Vec<_>, Vec<_>) = self
989                    .possibly_ready_observation
990                    .drain(..)
991                    .partition(|(name, cid)| {
992                        self.hooks
993                            .get(&(name.clone(), *cid))
994                            .into_iter()
995                            .flatten()
996                            .any(|hook| {
997                                hook.current_decision().unwrap_or(false)
998                                    || hook.can_make_nontrivial_decision()
999                            })
1000                    });
1001
1002                self.possibly_ready_observation = ready_obs;
1003                self.not_ready_observation.append(&mut not_ready_obs);
1004
1005                if self.possibly_ready_ticks.is_empty()
1006                    && self.possibly_ready_observation.is_empty()
1007                {
1008                    break;
1009                } else {
1010                    let next_tick_or_obs = (0..(self.possibly_ready_ticks.len()
1011                        + self.possibly_ready_observation.len()))
1012                        .any();
1013
1014                    if next_tick_or_obs < self.possibly_ready_ticks.len() {
1015                        let next_tick = next_tick_or_obs;
1016                        let mut removed = self.possibly_ready_ticks.remove(next_tick);
1017
1018                        match &mut self.log {
1019                            LogKind::Null => {}
1020                            LogKind::Stderr => {
1021                                if let Some(cid) = &removed.1 {
1022                                    eprintln!(
1023                                        "\n{}",
1024                                        format!("Running Tick (Cluster Member {})", cid)
1025                                            .color(colored::Color::Magenta)
1026                                            .bold()
1027                                    )
1028                                } else {
1029                                    eprintln!(
1030                                        "\n{}",
1031                                        "Running Tick".color(colored::Color::Magenta).bold()
1032                                    )
1033                                }
1034                            }
1035                            LogKind::Custom(writer) => {
1036                                writeln!(
1037                                    writer,
1038                                    "\n{}",
1039                                    "Running Tick".color(colored::Color::Magenta).bold()
1040                                )
1041                                .unwrap();
1042                            }
1043                        }
1044
1045                        let mut asterisk_indenter = |_line_no, write: &mut dyn std::fmt::Write| {
1046                            write.write_str(&"*".color(colored::Color::Magenta).bold())?;
1047                            write.write_str(" ")
1048                        };
1049
1050                        let mut tick_decision_writer = indenter::indented(&mut self.log)
1051                            .with_format(indenter::Format::Custom {
1052                                inserter: &mut asterisk_indenter,
1053                            });
1054
1055                        let hooks = self.hooks.get_mut(&(removed.0.clone(), removed.1)).unwrap();
1056                        run_hooks(&mut tick_decision_writer, hooks);
1057
1058                        let run_tick_future = removed.2.run_tick();
1059                        if let Some(inline_hooks) =
1060                            self.inline_hooks.get_mut(&(removed.0.clone(), removed.1))
1061                        {
1062                            let mut run_tick_future_pinned = pin!(run_tick_future);
1063
1064                            loop {
1065                                tokio::select! {
1066                                    biased;
1067                                    r = &mut run_tick_future_pinned => {
1068                                        assert!(r);
1069                                        break;
1070                                    }
1071                                    _ = async {} => {
1072                                        bolero_generator::any::scope::borrow_with(|driver| {
1073                                            for hook in inline_hooks.iter_mut() {
1074                                                if hook.pending_decision() {
1075                                                    if !hook.has_decision() {
1076                                                        hook.autonomous_decision(driver);
1077                                                    }
1078
1079                                                    hook.release_decision(&mut tick_decision_writer);
1080                                                }
1081                                            }
1082                                        });
1083                                    }
1084                                }
1085                            }
1086                        } else {
1087                            assert!(run_tick_future.await);
1088                        }
1089
1090                        self.possibly_ready_ticks.push(removed);
1091                    } else {
1092                        let next_obs = next_tick_or_obs - self.possibly_ready_ticks.len();
1093                        let mut default_hooks = vec![];
1094                        let hooks = self
1095                            .hooks
1096                            .get_mut(&self.possibly_ready_observation[next_obs])
1097                            .unwrap_or(&mut default_hooks);
1098
1099                        run_hooks(&mut self.log, hooks);
1100                    }
1101                }
1102            }
1103        }
1104    }
1105}
1106
1107fn run_hooks(tick_decision_writer: &mut impl std::fmt::Write, hooks: &mut Vec<Box<dyn SimHook>>) {
1108    let mut remaining_decision_count = hooks.len();
1109    let mut made_nontrivial_decision = false;
1110
1111    bolero::generator::bolero_generator::any::scope::borrow_with(|driver| {
1112        // first, scan manual decisions
1113        hooks.iter_mut().for_each(|hook| {
1114            if let Some(is_nontrivial) = hook.current_decision() {
1115                made_nontrivial_decision |= is_nontrivial;
1116                remaining_decision_count -= 1;
1117            } else if !hook.can_make_nontrivial_decision() {
1118                // if no nontrivial decision is possible, make a trivial one
1119                // (we need to do this in the first pass to force nontrivial decisions
1120                // on the remaining hooks)
1121                hook.autonomous_decision(driver, false);
1122                remaining_decision_count -= 1;
1123            }
1124        });
1125
1126        hooks.iter_mut().for_each(|hook| {
1127            if hook.current_decision().is_none() {
1128                made_nontrivial_decision |= hook.autonomous_decision(
1129                    driver,
1130                    !made_nontrivial_decision && remaining_decision_count == 1,
1131                );
1132                remaining_decision_count -= 1;
1133            }
1134
1135            hook.release_decision(tick_decision_writer);
1136        });
1137    });
1138}