playground_api/endpoints/
miri.rs

1use super::Edition;
2use serde::{Deserialize, Serialize};
3use std::borrow::Cow;
4
5/// A request structure for running Rust code under Miri, the Rust interpreter for detecting undefined behavior.
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
7pub struct MiriRequest<'a> {
8    /// The Rust source code to be interpreted.
9    pub code: Cow<'a, str>,
10
11    /// The Rust edition to use (e.g., 2021, 2024).
12    pub edition: Edition,
13
14    /// Whether the request is for running tests instead of the main function.
15    pub tests: bool,
16
17    /// Optional memory aliasing model used by Miri (e.g., `stacked` or `tree`).
18    #[serde(rename = "aliasingModel")]
19    pub aliasing_model: Option<AliasingModel>,
20}
21
22impl<'a> MiriRequest<'a> {
23    /// Creates a new [`MiriRequest`] with the provided parameters.
24    ///
25    /// # Parameters
26    /// - `code`: The Rust source code.
27    /// - `edition`: The Rust edition to use.
28    /// - `tests`: Whether to run tests.
29    /// - `aliasing_model`: Optional aliasing model for memory interpretation.
30    pub fn new(
31        code: Cow<'a, str>,
32        edition: Edition,
33        tests: bool,
34        aliasing_model: Option<AliasingModel>,
35    ) -> Self {
36        Self {
37            code,
38            edition,
39            tests,
40            aliasing_model,
41        }
42    }
43}
44
45impl<'b> super::Request for MiriRequest<'b> {
46    fn endpoint<'a>(&self) -> super::Endpoints<'a> {
47        super::Endpoints::Miri
48    }
49}
50
51impl<'a> Default for MiriRequest<'a> {
52    /// Returns a default [`MiriRequest`] using `Edition2024`, no tests,
53    /// and the `Stacked` aliasing model.
54    fn default() -> Self {
55        Self {
56            code: Cow::Borrowed("fn main() { println!(\"Hello, world!\"); }"),
57            edition: Edition::Edition2024,
58            tests: false,
59            aliasing_model: Some(AliasingModel::Stacked),
60        }
61    }
62}
63
64/// The response returned after executing a Miri request.
65#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
66pub struct MiriResponse<'a> {
67    /// Indicates whether execution was successful.
68    pub success: bool,
69
70    /// Additional detail about how the process exited.
71    pub exit_detail: Cow<'a, str>,
72
73    /// The standard output from the Miri execution.
74    pub stdout: Cow<'a, str>,
75
76    /// The standard error from the Miri execution.
77    pub stderr: Cow<'a, str>,
78}
79
80impl<'a> super::Response for MiriResponse<'a> {}
81
82/// The aliasing model used by Miri to simulate pointer and memory behavior.
83#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
84#[cfg_attr(feature = "poise-bot", derive(poise::ChoiceParameter))]
85#[serde(rename_all = "lowercase")]
86pub enum AliasingModel {
87    /// Uses the Stacked Borrows model for aliasing checks.
88    Stacked,
89
90    /// Uses the Tree Borrows model for aliasing checks.
91    Tree,
92}