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
334// This must be a tuple because it is referenced from generated code in `graph.rs`.
335type DylibResult = (
336    Vec<(&'static str, Option<u32>, Dfir<'static>)>,
337    Vec<(&'static str, Option<u32>, Dfir<'static>)>,
338    Hooks<&'static str>,
339    InlineHooks<&'static str>,
340);
341
342/// A single instance of a compiled Hydro simulation, which provides methods to interactively
343/// execute the simulation, feed inputs, and receive outputs.
344pub struct CompiledSimInstance<'a> {
345    func: SimLoaded<'a>,
346    externals_port_registry: SimExternalPortRegistry,
347    dylib_result: Option<DylibResult>,
348    log: bool,
349}
350
351impl<'a> CompiledSimInstance<'a> {
352    async fn run(self, thunk: impl AsyncFnOnce() + RefUnwindSafe) {
353        self.run_without_launching(async |instance| {
354            instance.launch();
355            thunk().await;
356        })
357        .await;
358    }
359
360    async fn run_without_launching(
361        mut self,
362        thunk: impl AsyncFnOnce(CompiledSimInstance) + RefUnwindSafe,
363    ) {
364        let mut external_out: HashMap<usize, UnboundedReceiverStream<Bytes>> = HashMap::new();
365        let mut external_in: HashMap<usize, UnboundedSender<Bytes>> = HashMap::new();
366        let mut cluster_external_out: HashMap<usize, Vec<UnboundedReceiverStream<Bytes>>> =
367            HashMap::new();
368        let mut cluster_external_in: HashMap<usize, Vec<UnboundedSender<Bytes>>> = HashMap::new();
369
370        let dylib_result = unsafe {
371            (self.func)(
372                colored::control::SHOULD_COLORIZE.should_colorize(),
373                &mut external_out,
374                &mut external_in,
375                &mut cluster_external_out,
376                &mut cluster_external_in,
377                if self.log {
378                    println_handler
379                } else {
380                    null_handler
381                },
382                if self.log {
383                    eprintln_handler
384                } else {
385                    null_handler
386                },
387            )
388        };
389
390        let registered = &self.externals_port_registry.registered;
391
392        let mut input_senders = HashMap::new();
393        let mut output_receivers = HashMap::new();
394        let mut cluster_input_senders = HashMap::new();
395        let mut cluster_output_receivers = HashMap::new();
396
397        for sim_port in registered.values() {
398            let usize_key = sim_port.into_inner();
399            if let Some(sender) = external_in.remove(&usize_key) {
400                input_senders.insert(*sim_port, Rc::new(sender));
401            }
402            if let Some(receiver) = external_out.remove(&usize_key) {
403                output_receivers.insert(*sim_port, Rc::new(Mutex::new(receiver)));
404            }
405            if let Some(senders) = cluster_external_in.remove(&usize_key) {
406                cluster_input_senders.insert(*sim_port, senders.into_iter().map(Rc::new).collect());
407            }
408            if let Some(receivers) = cluster_external_out.remove(&usize_key) {
409                cluster_output_receivers.insert(
410                    *sim_port,
411                    receivers
412                        .into_iter()
413                        .map(|r| Rc::new(Mutex::new(r)))
414                        .collect(),
415                );
416            }
417        }
418
419        self.dylib_result = Some(dylib_result);
420
421        let local_set = tokio::task::LocalSet::new();
422        local_set
423            .run_until(CURRENT_SIM_CONNECTIONS.scope(
424                RefCell::new(SimConnections {
425                    input_senders,
426                    output_receivers,
427                    cluster_input_senders,
428                    cluster_output_receivers,
429                    external_registered: self.externals_port_registry.registered.clone(),
430                }),
431                async move {
432                    thunk(self).await;
433                },
434            ))
435            .await;
436    }
437
438    /// Launches the simulation, which will asynchronously simulate the Hydro program. This should
439    /// be invoked but before receiving any messages.
440    fn launch(self) {
441        tokio::task::spawn_local(self.schedule_with_maybe_logger::<std::io::Empty>(None));
442    }
443
444    /// Returns a future that schedules simulation with the given logger for reporting the
445    /// simulation trace.
446    pub fn schedule_with_logger<W: std::io::Write>(
447        self,
448        log_writer: W,
449    ) -> impl use<W> + Future<Output = ()> {
450        self.schedule_with_maybe_logger(Some(log_writer))
451    }
452
453    fn schedule_with_maybe_logger<W: std::io::Write>(
454        mut self,
455        log_override: Option<W>,
456    ) -> impl use<W> + Future<Output = ()> {
457        let (async_dfirs, tick_dfirs, hooks, inline_hooks) = self.dylib_result.take().unwrap();
458
459        let not_ready_observation = async_dfirs
460            .iter()
461            .map(|(lid, c_id, _)| (serde_json::from_str(lid).unwrap(), *c_id))
462            .collect();
463
464        let mut launched = LaunchedSim {
465            async_dfirs: async_dfirs
466                .into_iter()
467                .map(|(lid, c_id, dfir)| (serde_json::from_str(lid).unwrap(), c_id, dfir))
468                .collect(),
469            possibly_ready_ticks: vec![],
470            not_ready_ticks: tick_dfirs
471                .into_iter()
472                .map(|(lid, c_id, dfir)| (serde_json::from_str(lid).unwrap(), c_id, dfir))
473                .collect(),
474            possibly_ready_observation: vec![],
475            not_ready_observation,
476            hooks: hooks
477                .into_iter()
478                .map(|((lid, cid), hs)| ((serde_json::from_str(lid).unwrap(), cid), hs))
479                .collect(),
480            inline_hooks: inline_hooks
481                .into_iter()
482                .map(|((lid, cid), hs)| ((serde_json::from_str(lid).unwrap(), cid), hs))
483                .collect(),
484            log: if self.log {
485                if let Some(w) = log_override {
486                    LogKind::Custom(w)
487                } else {
488                    LogKind::Stderr
489                }
490            } else {
491                LogKind::Null
492            },
493        };
494
495        async move { launched.scheduler().await }
496    }
497}
498
499impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> Clone for SimReceiver<T, O, R> {
500    fn clone(&self) -> Self {
501        *self
502    }
503}
504
505impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> Copy for SimReceiver<T, O, R> {}
506
507impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> SimReceiver<T, O, R> {
508    async fn with_stream<Out>(
509        &self,
510        thunk: impl AsyncFnOnce(&mut Pin<&mut dyn Stream<Item = T>>) -> Out,
511    ) -> Out {
512        let receiver = CURRENT_SIM_CONNECTIONS.with(|connections| {
513            let connections = &mut *connections.borrow_mut();
514            connections
515                .output_receivers
516                .get(connections.external_registered.get(&self.0).unwrap())
517                .unwrap()
518                .clone()
519        });
520
521        let mut receiver_stream = receiver.lock().await;
522        thunk(&mut pin!(
523            &mut receiver_stream
524                .by_ref()
525                .map(|b| bincode::deserialize(&b).unwrap())
526        ))
527        .await
528    }
529
530    /// Asserts that the stream has ended and no more messages can possibly arrive.
531    pub fn assert_no_more(self) -> impl Future<Output = ()>
532    where
533        T: Debug,
534    {
535        FutureTrackingCaller {
536            future: async move {
537                self.with_stream(async |stream| {
538                    if let Some(next) = stream.next().await {
539                        return Err(format!(
540                            "Stream yielded unexpected message: {:?}, expected termination",
541                            next
542                        ));
543                    }
544                    Ok(())
545                })
546                .await
547            },
548        }
549    }
550}
551
552impl<T: Serialize + DeserializeOwned> SimReceiver<T, TotalOrder, ExactlyOnce> {
553    /// Receives the next message from the external bincode stream. This will wait until a message
554    /// is available, or return `None` if no more messages can possibly arrive.
555    pub async fn next(&self) -> Option<T> {
556        self.with_stream(async |stream| stream.next().await).await
557    }
558
559    /// Collects all remaining messages from the external bincode stream into a collection. This
560    /// will wait until no more messages can possibly arrive.
561    pub async fn collect<C: Default + Extend<T>>(self) -> C {
562        self.with_stream(async |stream| stream.collect().await)
563            .await
564    }
565
566    /// Asserts that the stream yields exactly the expected sequence of messages, in order.
567    /// This does not check that the stream ends, use [`Self::assert_yields_only`] for that.
568    pub fn assert_yields<T2: Debug, I: IntoIterator<Item = T2>>(
569        &self,
570        expected: I,
571    ) -> impl use<'_, T, T2, I> + Future<Output = ()>
572    where
573        T: Debug + PartialEq<T2>,
574    {
575        FutureTrackingCaller {
576            future: async {
577                let mut expected: VecDeque<T2> = expected.into_iter().collect();
578
579                while !expected.is_empty() {
580                    if let Some(next) = self.next().await {
581                        let next_expected = expected.pop_front().unwrap();
582                        if next != next_expected {
583                            return Err(format!(
584                                "Stream yielded unexpected message: {:?}, expected: {:?}",
585                                next, next_expected
586                            ));
587                        }
588                    } else {
589                        return Err(format!(
590                            "Stream ended early, still expected: {:?}",
591                            expected
592                        ));
593                    }
594                }
595
596                Ok(())
597            },
598        }
599    }
600
601    /// Asserts that the stream yields only the expected sequence of messages, in order,
602    /// and then ends.
603    pub fn assert_yields_only<T2: Debug, I: IntoIterator<Item = T2>>(
604        &self,
605        expected: I,
606    ) -> impl use<'_, T, T2, I> + Future<Output = ()>
607    where
608        T: Debug + PartialEq<T2>,
609    {
610        ChainedFuture {
611            first: self.assert_yields(expected),
612            second: self.assert_no_more(),
613            first_done: false,
614        }
615    }
616}
617
618pin_project_lite::pin_project! {
619    // A future that tracks the location of the `.await` call for better panic messages.
620    //
621    // `#[track_caller]` is important for us to create assertion methods because it makes
622    // the panic backtrace show up at that method (instead of inside the call tree within
623    // that method). This is e.g. what `Option::unwrap` uses. Unfortunately, `#[track_caller]`
624    // does not work correctly for async methods (or `dyn Future` either), so we have to
625    // create these concrete future types that (1) have `#[track_caller]` on their `poll()`
626    // method and (2) have the `panic!` triggered in their `poll()` method (or in a directly
627    // nested concrete future).
628    struct FutureTrackingCaller<F: Future<Output = Result<(), String>>> {
629        #[pin]
630        future: F,
631    }
632}
633
634impl<F: Future<Output = Result<(), String>>> Future for FutureTrackingCaller<F> {
635    type Output = ();
636
637    #[track_caller]
638    fn poll(
639        mut self: Pin<&mut Self>,
640        cx: &mut std::task::Context<'_>,
641    ) -> std::task::Poll<Self::Output> {
642        match ready!(self.as_mut().project().future.poll(cx)) {
643            Ok(()) => std::task::Poll::Ready(()),
644            Err(e) => panic!("{}", e),
645        }
646    }
647}
648
649pin_project_lite::pin_project! {
650    // A future that first awaits the first future, then the second, propagating caller info.
651    //
652    // See [`FutureTrackingCaller`] for context.
653    struct ChainedFuture<F1: Future<Output = ()>, F2: Future<Output = ()>> {
654        #[pin]
655        first: F1,
656        #[pin]
657        second: F2,
658        first_done: bool,
659    }
660}
661
662impl<F1: Future<Output = ()>, F2: Future<Output = ()>> Future for ChainedFuture<F1, F2> {
663    type Output = ();
664
665    #[track_caller]
666    fn poll(
667        mut self: Pin<&mut Self>,
668        cx: &mut std::task::Context<'_>,
669    ) -> std::task::Poll<Self::Output> {
670        if !self.first_done {
671            ready!(self.as_mut().project().first.poll(cx));
672            *self.as_mut().project().first_done = true;
673        }
674
675        self.as_mut().project().second.poll(cx)
676    }
677}
678
679impl<T: Serialize + DeserializeOwned> SimReceiver<T, NoOrder, ExactlyOnce> {
680    /// Collects all remaining messages from the external bincode stream into a collection,
681    /// sorting them. This will wait until no more messages can possibly arrive.
682    pub async fn collect_sorted<C: Default + Extend<T> + AsMut<[T]>>(self) -> C
683    where
684        T: Ord,
685    {
686        self.with_stream(async |stream| {
687            let mut collected: C = stream.collect().await;
688            collected.as_mut().sort();
689            collected
690        })
691        .await
692    }
693
694    /// Asserts that the stream yields exactly the expected sequence of messages, in some order.
695    /// This does not check that the stream ends, use [`Self::assert_yields_only_unordered`] for that.
696    pub fn assert_yields_unordered<T2: Debug, I: IntoIterator<Item = T2>>(
697        &self,
698        expected: I,
699    ) -> impl use<'_, T, T2, I> + Future<Output = ()>
700    where
701        T: Debug + PartialEq<T2>,
702    {
703        FutureTrackingCaller {
704            future: async {
705                self.with_stream(async |stream| {
706                    let mut expected: Vec<T2> = expected.into_iter().collect();
707
708                    while !expected.is_empty() {
709                        if let Some(next) = stream.next().await {
710                            let idx = expected.iter().enumerate().find(|(_, e)| &next == *e);
711                            if let Some((i, _)) = idx {
712                                expected.swap_remove(i);
713                            } else {
714                                return Err(format!(
715                                    "Stream yielded unexpected message: {:?}",
716                                    next
717                                ));
718                            }
719                        } else {
720                            return Err(format!(
721                                "Stream ended early, still expected: {:?}",
722                                expected
723                            ));
724                        }
725                    }
726
727                    Ok(())
728                })
729                .await
730            },
731        }
732    }
733
734    /// Asserts that the stream yields only the expected sequence of messages, in some order,
735    /// and then ends.
736    pub fn assert_yields_only_unordered<T2: Debug, I: IntoIterator<Item = T2>>(
737        &self,
738        expected: I,
739    ) -> impl use<'_, T, T2, I> + Future<Output = ()>
740    where
741        T: Debug + PartialEq<T2>,
742    {
743        ChainedFuture {
744            first: self.assert_yields_unordered(expected),
745            second: self.assert_no_more(),
746            first_done: false,
747        }
748    }
749}
750
751impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> SimSender<T, O, R> {
752    fn with_sink<Out>(
753        &self,
754        thunk: impl FnOnce(&dyn Fn(T) -> Result<(), tokio::sync::mpsc::error::SendError<Bytes>>) -> Out,
755    ) -> Out {
756        let sender = CURRENT_SIM_CONNECTIONS.with(|connections| {
757            let connections = &mut *connections.borrow_mut();
758            connections
759                .input_senders
760                .get(connections.external_registered.get(&self.0).unwrap())
761                .unwrap()
762                .clone()
763        });
764
765        thunk(&move |t| sender.send(bincode::serialize(&t).unwrap().into()))
766    }
767}
768
769impl<T: Serialize + DeserializeOwned, O: Ordering> SimSender<T, O, ExactlyOnce> {
770    /// Sends several messages to the external bincode sink. The messages will be asynchronously
771    /// processed as part of the simulation, in non-deterministic order.
772    pub fn send_many_unordered<I: IntoIterator<Item = T>>(&self, iter: I) {
773        self.with_sink(|send| {
774            for t in iter {
775                send(t).unwrap();
776            }
777        })
778    }
779}
780
781impl<T: Serialize + DeserializeOwned> SimSender<T, TotalOrder, ExactlyOnce> {
782    /// Sends a message to the external bincode sink. The message will be asynchronously processed
783    /// as part of the simulation.
784    pub fn send(&self, t: T) {
785        self.with_sink(|send| send(t)).unwrap();
786    }
787
788    /// Sends several messages to the external bincode sink. The messages will be asynchronously
789    /// processed as part of the simulation.
790    pub fn send_many<I: IntoIterator<Item = T>>(&self, iter: I) {
791        self.with_sink(|send| {
792            for t in iter {
793                send(t).unwrap();
794            }
795        })
796    }
797}
798
799impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> Clone
800    for SimClusterReceiver<T, O, R>
801{
802    fn clone(&self) -> Self {
803        *self
804    }
805}
806
807impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> Copy
808    for SimClusterReceiver<T, O, R>
809{
810}
811
812impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> SimClusterReceiver<T, O, R> {
813    async fn with_member_stream<Out>(
814        &self,
815        member_id: u32,
816        thunk: impl AsyncFnOnce(&mut Pin<&mut dyn Stream<Item = T>>) -> Out,
817    ) -> Out {
818        let receiver = CURRENT_SIM_CONNECTIONS.with(|connections| {
819            let connections = &mut *connections.borrow_mut();
820            let receivers = connections
821                .cluster_output_receivers
822                .get(connections.external_registered.get(&self.0).unwrap())
823                .unwrap();
824            receivers[member_id as usize].clone()
825        });
826
827        let mut lock = receiver.lock().await;
828        thunk(&mut pin!(
829            lock.by_ref().map(|b| bincode::deserialize(&b).unwrap())
830        ))
831        .await
832    }
833}
834
835impl<T: Serialize + DeserializeOwned> SimClusterReceiver<T, TotalOrder, ExactlyOnce> {
836    /// Receives the next value from a specific cluster member.
837    pub async fn next(&self, member_id: u32) -> Option<T> {
838        self.with_member_stream(member_id, async |stream| stream.next().await)
839            .await
840    }
841
842    /// Collects all remaining values from a specific cluster member into a collection.
843    pub async fn collect<C: Default + Extend<T>>(self, member_id: u32) -> C {
844        self.with_member_stream(member_id, async |stream| stream.collect().await)
845            .await
846    }
847}
848
849impl<T: Serialize + DeserializeOwned> SimClusterReceiver<T, NoOrder, ExactlyOnce> {
850    /// Collects all remaining values from a specific cluster member, sorted.
851    pub async fn collect_sorted<C: Default + Extend<T> + AsMut<[T]>>(self, member_id: u32) -> C
852    where
853        T: Ord,
854    {
855        self.with_member_stream(member_id, async |stream| {
856            let mut collected: C = stream.collect().await;
857            collected.as_mut().sort();
858            collected
859        })
860        .await
861    }
862}
863
864impl<T: Serialize + DeserializeOwned, O: Ordering, R: Retries> SimClusterSender<T, O, R> {
865    fn with_sink<Out>(
866        &self,
867        thunk: impl FnOnce(
868            &dyn Fn(u32, T) -> Result<(), tokio::sync::mpsc::error::SendError<Bytes>>,
869        ) -> Out,
870    ) -> Out {
871        let senders = CURRENT_SIM_CONNECTIONS.with(|connections| {
872            let connections = &mut *connections.borrow_mut();
873            connections
874                .cluster_input_senders
875                .get(connections.external_registered.get(&self.0).unwrap())
876                .unwrap()
877                .clone()
878        });
879
880        thunk(&move |member_id: u32, t: T| {
881            let payload = bincode::serialize(&t).unwrap();
882            senders[member_id as usize].send(Bytes::from(payload))
883        })
884    }
885}
886
887impl<T: Serialize + DeserializeOwned> SimClusterSender<T, TotalOrder, ExactlyOnce> {
888    /// Sends a value to a specific cluster member.
889    pub fn send(&self, member_id: u32, t: T) {
890        self.with_sink(|send| send(member_id, t)).unwrap();
891    }
892
893    /// Sends multiple values to specific cluster members.
894    pub fn send_many<I: IntoIterator<Item = (u32, T)>>(&self, iter: I) {
895        self.with_sink(|send| {
896            for (member_id, t) in iter {
897                send(member_id, t).unwrap();
898            }
899        })
900    }
901}
902
903enum LogKind<W: std::io::Write> {
904    Null,
905    Stderr,
906    Custom(W),
907}
908
909// via https://www.reddit.com/r/rust/comments/t69sld/is_there_a_way_to_allow_either_stdfmtwrite_or/
910impl<W: std::io::Write> std::fmt::Write for LogKind<W> {
911    fn write_str(&mut self, s: &str) -> Result<(), std::fmt::Error> {
912        match self {
913            LogKind::Null => Ok(()),
914            LogKind::Stderr => {
915                eprint!("{}", s);
916                Ok(())
917            }
918            LogKind::Custom(w) => w.write_all(s.as_bytes()).map_err(|_| std::fmt::Error),
919        }
920    }
921}
922
923/// A running simulation, which manages the async DFIR and tick DFIRs, and makes decisions
924/// about scheduling ticks and choices for non-deterministic operators like batch.
925struct LaunchedSim<W: std::io::Write> {
926    async_dfirs: Vec<(LocationId, Option<u32>, Dfir<'static>)>,
927    possibly_ready_ticks: Vec<(LocationId, Option<u32>, Dfir<'static>)>,
928    not_ready_ticks: Vec<(LocationId, Option<u32>, Dfir<'static>)>,
929    possibly_ready_observation: Vec<(LocationId, Option<u32>)>,
930    not_ready_observation: Vec<(LocationId, Option<u32>)>,
931    hooks: Hooks<LocationId>,
932    inline_hooks: InlineHooks<LocationId>,
933    log: LogKind<W>,
934}
935
936impl<W: std::io::Write> LaunchedSim<W> {
937    async fn scheduler(&mut self) {
938        loop {
939            tokio::task::yield_now().await;
940            let mut any_made_progress = false;
941            for (loc, c_id, dfir) in &mut self.async_dfirs {
942                if dfir.run_tick().await {
943                    any_made_progress = true;
944                    let (now_ready, still_not_ready): (Vec<_>, Vec<_>) = self
945                        .not_ready_ticks
946                        .drain(..)
947                        .partition(|(tick_loc, tick_c_id, _)| {
948                            let LocationId::Tick(_, outer) = tick_loc else {
949                                unreachable!()
950                            };
951                            outer.as_ref() == loc && tick_c_id == c_id
952                        });
953
954                    self.possibly_ready_ticks.extend(now_ready);
955                    self.not_ready_ticks.extend(still_not_ready);
956
957                    let (now_ready_obs, still_not_ready_obs): (Vec<_>, Vec<_>) = self
958                        .not_ready_observation
959                        .drain(..)
960                        .partition(|(obs_loc, obs_c_id)| obs_loc == loc && obs_c_id == c_id);
961
962                    self.possibly_ready_observation.extend(now_ready_obs);
963                    self.not_ready_observation.extend(still_not_ready_obs);
964                }
965            }
966
967            if any_made_progress {
968                continue;
969            } else {
970                use bolero::generator::*;
971
972                let (ready_tick, mut not_ready_tick): (Vec<_>, Vec<_>) = self
973                    .possibly_ready_ticks
974                    .drain(..)
975                    .partition(|(name, cid, _)| {
976                        self.hooks
977                            .get(&(name.clone(), *cid))
978                            .unwrap()
979                            .iter()
980                            .any(|hook| {
981                                hook.current_decision().unwrap_or(false)
982                                    || hook.can_make_nontrivial_decision()
983                            })
984                    });
985
986                self.possibly_ready_ticks = ready_tick;
987                self.not_ready_ticks.append(&mut not_ready_tick);
988
989                let (ready_obs, mut not_ready_obs): (Vec<_>, Vec<_>) = self
990                    .possibly_ready_observation
991                    .drain(..)
992                    .partition(|(name, cid)| {
993                        self.hooks
994                            .get(&(name.clone(), *cid))
995                            .into_iter()
996                            .flatten()
997                            .any(|hook| {
998                                hook.current_decision().unwrap_or(false)
999                                    || hook.can_make_nontrivial_decision()
1000                            })
1001                    });
1002
1003                self.possibly_ready_observation = ready_obs;
1004                self.not_ready_observation.append(&mut not_ready_obs);
1005
1006                if self.possibly_ready_ticks.is_empty()
1007                    && self.possibly_ready_observation.is_empty()
1008                {
1009                    break;
1010                } else {
1011                    let next_tick_or_obs = (0..(self.possibly_ready_ticks.len()
1012                        + self.possibly_ready_observation.len()))
1013                        .any();
1014
1015                    if next_tick_or_obs < self.possibly_ready_ticks.len() {
1016                        let next_tick = next_tick_or_obs;
1017                        let mut removed = self.possibly_ready_ticks.remove(next_tick);
1018
1019                        match &mut self.log {
1020                            LogKind::Null => {}
1021                            LogKind::Stderr => {
1022                                if let Some(cid) = &removed.1 {
1023                                    eprintln!(
1024                                        "\n{}",
1025                                        format!("Running Tick (Cluster Member {})", cid)
1026                                            .color(colored::Color::Magenta)
1027                                            .bold()
1028                                    )
1029                                } else {
1030                                    eprintln!(
1031                                        "\n{}",
1032                                        "Running Tick".color(colored::Color::Magenta).bold()
1033                                    )
1034                                }
1035                            }
1036                            LogKind::Custom(writer) => {
1037                                writeln!(
1038                                    writer,
1039                                    "\n{}",
1040                                    "Running Tick".color(colored::Color::Magenta).bold()
1041                                )
1042                                .unwrap();
1043                            }
1044                        }
1045
1046                        let mut asterisk_indenter = |_line_no, write: &mut dyn std::fmt::Write| {
1047                            write.write_str(&"*".color(colored::Color::Magenta).bold())?;
1048                            write.write_str(" ")
1049                        };
1050
1051                        let mut tick_decision_writer = indenter::indented(&mut self.log)
1052                            .with_format(indenter::Format::Custom {
1053                                inserter: &mut asterisk_indenter,
1054                            });
1055
1056                        let hooks = self.hooks.get_mut(&(removed.0.clone(), removed.1)).unwrap();
1057                        run_hooks(&mut tick_decision_writer, hooks);
1058
1059                        let run_tick_future = removed.2.run_tick();
1060                        if let Some(inline_hooks) =
1061                            self.inline_hooks.get_mut(&(removed.0.clone(), removed.1))
1062                        {
1063                            let mut run_tick_future_pinned = pin!(run_tick_future);
1064
1065                            loop {
1066                                tokio::select! {
1067                                    biased;
1068                                    r = &mut run_tick_future_pinned => {
1069                                        assert!(r);
1070                                        break;
1071                                    }
1072                                    _ = async {} => {
1073                                        bolero_generator::any::scope::borrow_with(|driver| {
1074                                            for hook in inline_hooks.iter_mut() {
1075                                                if hook.pending_decision() {
1076                                                    if !hook.has_decision() {
1077                                                        hook.autonomous_decision(driver);
1078                                                    }
1079
1080                                                    hook.release_decision(&mut tick_decision_writer);
1081                                                }
1082                                            }
1083                                        });
1084                                    }
1085                                }
1086                            }
1087                        } else {
1088                            assert!(run_tick_future.await);
1089                        }
1090
1091                        self.possibly_ready_ticks.push(removed);
1092                    } else {
1093                        let next_obs = next_tick_or_obs - self.possibly_ready_ticks.len();
1094                        let mut default_hooks = vec![];
1095                        let hooks = self
1096                            .hooks
1097                            .get_mut(&self.possibly_ready_observation[next_obs])
1098                            .unwrap_or(&mut default_hooks);
1099
1100                        run_hooks(&mut self.log, hooks);
1101                    }
1102                }
1103            }
1104        }
1105    }
1106}
1107
1108fn run_hooks(tick_decision_writer: &mut impl std::fmt::Write, hooks: &mut Vec<Box<dyn SimHook>>) {
1109    let mut remaining_decision_count = hooks.len();
1110    let mut made_nontrivial_decision = false;
1111
1112    bolero::generator::bolero_generator::any::scope::borrow_with(|driver| {
1113        // first, scan manual decisions
1114        hooks.iter_mut().for_each(|hook| {
1115            if let Some(is_nontrivial) = hook.current_decision() {
1116                made_nontrivial_decision |= is_nontrivial;
1117                remaining_decision_count -= 1;
1118            } else if !hook.can_make_nontrivial_decision() {
1119                // if no nontrivial decision is possible, make a trivial one
1120                // (we need to do this in the first pass to force nontrivial decisions
1121                // on the remaining hooks)
1122                hook.autonomous_decision(driver, false);
1123                remaining_decision_count -= 1;
1124            }
1125        });
1126
1127        hooks.iter_mut().for_each(|hook| {
1128            if hook.current_decision().is_none() {
1129                made_nontrivial_decision |= hook.autonomous_decision(
1130                    driver,
1131                    !made_nontrivial_decision && remaining_decision_count == 1,
1132                );
1133                remaining_decision_count -= 1;
1134            }
1135
1136            hook.release_decision(tick_decision_writer);
1137        });
1138    });
1139}