1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use crate::error::{bail, Error};

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum BackendKind {
    Compiler,
    Interpreter,
}

impl core::fmt::Display for BackendKind {
    fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
        let name = match self {
            BackendKind::Compiler => "compiler",
            BackendKind::Interpreter => "interpreter",
        };

        fmt.write_str(name)
    }
}

impl BackendKind {
    fn from_os_str(s: &std::ffi::OsStr) -> Result<Option<BackendKind>, Error> {
        if s == "auto" {
            Ok(None)
        } else if s == "interpreter" {
            Ok(Some(BackendKind::Interpreter))
        } else if s == "compiler" {
            Ok(Some(BackendKind::Compiler))
        } else {
            Err(Error::from_static_str(
                "invalid value of POLKAVM_BACKEND; supported values are: 'interpreter', 'compiler'",
            ))
        }
    }
}

impl BackendKind {
    pub fn is_supported(self) -> bool {
        match self {
            BackendKind::Interpreter => true,
            BackendKind::Compiler => if_compiler_is_supported! {
                { true } else { false }
            },
        }
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum SandboxKind {
    Linux,
    Generic,
}

impl core::fmt::Display for SandboxKind {
    fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
        let name = match self {
            SandboxKind::Linux => "linux",
            SandboxKind::Generic => "generic",
        };

        fmt.write_str(name)
    }
}

impl SandboxKind {
    fn from_os_str(s: &std::ffi::OsStr) -> Result<Option<SandboxKind>, Error> {
        if s == "auto" {
            Ok(None)
        } else if s == "linux" {
            Ok(Some(SandboxKind::Linux))
        } else if s == "generic" {
            Ok(Some(SandboxKind::Generic))
        } else {
            Err(Error::from_static_str(
                "invalid value of POLKAVM_SANDBOX; supported values are: 'linux', 'generic'",
            ))
        }
    }
}

impl SandboxKind {
    pub fn is_supported(self) -> bool {
        if_compiler_is_supported! {
            {
                match self {
                    SandboxKind::Linux => cfg!(target_os = "linux"),
                    SandboxKind::Generic => true
                }
            } else {
                false
            }
        }
    }
}

#[derive(Clone)]
pub struct Config {
    pub(crate) backend: Option<BackendKind>,
    pub(crate) sandbox: Option<SandboxKind>,
    pub(crate) trace_execution: bool,
    pub(crate) allow_insecure: bool,
    pub(crate) worker_count: usize,
}

impl Default for Config {
    fn default() -> Self {
        Self::new()
    }
}

fn env_bool(name: &str) -> Result<Option<bool>, Error> {
    if let Some(value) = std::env::var_os(name) {
        if value == "1" || value == "true" {
            Ok(Some(true))
        } else if value == "0" || value == "false" {
            Ok(Some(false))
        } else {
            bail!("invalid value of {name}; must be either '1' or '0'")
        }
    } else {
        Ok(None)
    }
}

impl Config {
    /// Creates a new default configuration.
    pub fn new() -> Self {
        Config {
            backend: None,
            sandbox: None,
            trace_execution: false,
            allow_insecure: false,
            worker_count: 2,
        }
    }

    /// Creates a new default configuration and seeds it from the environment variables.
    pub fn from_env() -> Result<Self, Error> {
        let mut config = Self::new();
        if let Some(value) = std::env::var_os("POLKAVM_BACKEND") {
            config.backend = BackendKind::from_os_str(&value)?;
        }

        if let Some(value) = std::env::var_os("POLKAVM_SANDBOX") {
            config.sandbox = SandboxKind::from_os_str(&value)?;
        }

        if let Some(value) = env_bool("POLKAVM_TRACE_EXECUTION")? {
            config.trace_execution = value;
        }

        if let Some(value) = env_bool("POLKAVM_ALLOW_INSECURE")? {
            config.allow_insecure = value;
        }

        Ok(config)
    }

    /// Forces the use of a given backend.
    ///
    /// Default: `None` (automatically pick the best available backend)
    ///
    /// Corresponding environment variable: `POLKAVM_BACKEND` (`auto`, `compiler`, `interpreter`)
    pub fn set_backend(&mut self, backend: Option<BackendKind>) -> &mut Self {
        self.backend = backend;
        self
    }

    /// Gets the currently set backend, if any.
    pub fn backend(&self) -> Option<BackendKind> {
        self.backend
    }

    /// Forces the use of a given sandbox.
    ///
    /// Default: `None` (automatically pick the best available sandbox)
    ///
    /// Corresponding environment variable: `POLKAVM_SANDBOX` (`auto`, `linux`, `generic`)
    pub fn set_sandbox(&mut self, sandbox: Option<SandboxKind>) -> &mut Self {
        self.sandbox = sandbox;
        self
    }

    /// Gets the currently set sandbox, if any.
    pub fn sandbox(&self) -> Option<SandboxKind> {
        self.sandbox
    }

    /// Enables execution tracing.
    ///
    /// **Requires `set_allow_insecure` to be `true`.**
    ///
    /// Default: `false`
    ///
    /// Corresponding environment variable: `POLKAVM_TRACE_EXECUTION` (`true`, `false`)
    pub fn set_trace_execution(&mut self, value: bool) -> &mut Self {
        self.trace_execution = value;
        self
    }

    /// Returns whether the execution tracing is enabled.
    pub fn trace_execution(&self) -> bool {
        self.trace_execution
    }

    /// Enabling this makes it possible to enable other settings
    /// which can introduce unsafety or break determinism.
    ///
    /// Should only be used for debugging purposes and *never* enabled by default in production.
    ///
    /// Default: `false`
    ///
    /// Corresponding environment variable: `POLKAVM_ALLOW_INSECURE` (`true`, `false`)
    pub fn set_allow_insecure(&mut self, value: bool) -> &mut Self {
        self.allow_insecure = value;
        self
    }

    /// Sets the number of worker sandboxes that will be permanently kept alive by the engine.
    ///
    /// This doesn't limit the number of instances that can be instantiated at the same time;
    /// it will just tell the engine how many sandboxes should be cached between instantiations.
    ///
    /// For the Linux sandbox this will decide how many worker processes are kept alive.
    ///
    /// This only has an effect when using a recompiler. For the interpreter this setting will be ignored.
    ///
    /// Default: `2`
    pub fn set_worker_count(&mut self, value: usize) -> &mut Self {
        self.worker_count = value;
        self
    }
}

/// The type of gas metering.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum GasMeteringKind {
    /// Synchronous gas metering. This will immediately abort the execution if we run out of gas.
    Sync,
    /// Asynchronous gas metering. Has a lower performance overhead compared to synchronous gas metering,
    /// but will only periodically and asynchronously check whether we still have gas remaining while
    /// the program is running.
    ///
    /// With asynchronous gas metering the program can run slightly longer than it would otherwise,
    /// and the exact point *when* it is interrupted is not deterministic, but whether the computation
    /// as a whole finishes under a given gas limit will still be strictly enforced and deterministic.
    ///
    /// This is only a hint, and the VM might still fall back to using synchronous gas metering
    /// if asynchronous metering is not available.
    Async,
}

/// The configuration for a module.
#[derive(Clone)]
pub struct ModuleConfig {
    pub(crate) page_size: u32,
    pub(crate) gas_metering: Option<GasMeteringKind>,
}

impl Default for ModuleConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl ModuleConfig {
    /// Creates a new default module configuration.
    pub fn new() -> Self {
        ModuleConfig {
            page_size: 0x4000,
            gas_metering: None,
        }
    }

    /// Sets the page size used for the module.
    ///
    /// Default: `16384` (16k)
    pub fn set_page_size(&mut self, page_size: u32) -> &mut Self {
        self.page_size = page_size;
        self
    }

    /// Sets the type of gas metering to enable for this module.
    ///
    /// Default: `None`
    pub fn set_gas_metering(&mut self, kind: Option<GasMeteringKind>) -> &mut Self {
        self.gas_metering = kind;
        self
    }
}