silicube 0.3.1

A library for sandboxed code execution
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Box lifecycle management for isolate
//!
//! Manages the initialization, use, and cleanup of Isolate sandbox boxes.

use std::path::{Path, PathBuf};

use tokio::process::Command;
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
use tracing::{debug, instrument, warn};

use crate::isolate::IsolateError;
use crate::isolate::command::{IsolateAction, IsolateCommand};

/// An Isolate sandbox
///
/// Represents an initialized isolate box that can be used to run sandboxed code.
///
/// # Cleanup
///
/// **Important:** Always call [`cleanup()`](Self::cleanup) explicitly before dropping
/// the box. The `Drop` implementation attempts best-effort cleanup via a spawned
/// thread, but this is unreliable and may not complete before process exit.
///
/// ```rust,ignore
/// let mut sandbox = IsolateBox::init(0, "isolate").await?;
/// // ... use the sandbox ...
/// sandbox.cleanup().await?; // Always cleanup explicitly!
/// ```
///
/// If you want RAII-style cleanup, consider using [`into_guard()`](Self::into_guard)
/// which returns a guard that will log a warning if dropped without explicit cleanup.
#[derive(Debug)]
pub struct IsolateBox {
    /// Box ID
    id: u32,

    /// Path to the box directory
    box_path: PathBuf,

    /// Path to the isolate binary
    isolate_path: PathBuf,

    /// Whether the box is initialized
    initialized: bool,

    /// Whether cgroup support is enabled
    cgroup: bool,

    /// Pool permit (if acquired from a pool)
    _permit: Option<OwnedSemaphorePermit>,
}

impl IsolateBox {
    /// Initialize a new isolate box
    #[instrument(skip(isolate_path))]
    pub async fn init(
        id: u32,
        isolate_path: impl Into<PathBuf>,
        cgroup: bool,
    ) -> Result<Self, IsolateError> {
        let isolate_path = isolate_path.into();

        // Run `isolate --init`
        let cmd = IsolateCommand::new(&isolate_path, id)
            .action(IsolateAction::Init)
            .cgroup(cgroup);
        let args = cmd.build();

        debug!(?args, "initializing isolate box");

        let program = args
            .first()
            .ok_or_else(|| IsolateError::CommandFailed("empty command arguments".to_string()))?;
        let output = Command::new(program)
            .args(&args[1..])
            .output()
            .await
            .map_err(IsolateError::SpawnFailed)?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(IsolateError::InitFailed {
                id,
                message: stderr.to_string(),
            });
        }

        // Parse box path from stdout
        let stdout = String::from_utf8_lossy(&output.stdout);
        let box_path = PathBuf::from(stdout.trim());

        if !box_path.exists() {
            return Err(IsolateError::InitFailed {
                id,
                message: format!("box path does not exist: {}", box_path.display()),
            });
        }

        debug!(?box_path, "box initialized");

        Ok(Self {
            id,
            box_path,
            isolate_path,
            initialized: true,
            cgroup,
            _permit: None,
        })
    }

    /// Get the box ID
    pub fn id(&self) -> u32 {
        self.id
    }

    /// Get the path to the box directory (where files can be placed)
    pub fn path(&self) -> &Path {
        &self.box_path
    }

    /// Get the host path to a file inside the box
    ///
    /// Returns an error if the path contains path traversal attempts.
    pub fn file_path(&self, name: &str) -> Result<PathBuf, IsolateError> {
        // Reject path traversal attempts
        if name.contains("..") || name.starts_with('/') {
            return Err(IsolateError::InvalidPath(format!(
                "path traversal not allowed: {}",
                name
            )));
        }
        Ok(self.box_path.join("box").join(name))
    }

    /// Get the sandbox-internal path for a file inside the box
    ///
    /// Returns the path as seen from inside the isolate sandbox, where the box
    /// directory is mounted at `/box/`. Use this for isolate `--stdin`,
    /// `--stdout`, and `--stderr` flags which are opened inside the sandbox.
    pub fn sandbox_path(&self, name: &str) -> Result<PathBuf, IsolateError> {
        if name.contains("..") || name.starts_with('/') {
            return Err(IsolateError::InvalidPath(format!(
                "path traversal not allowed: {}",
                name
            )));
        }
        Ok(PathBuf::from("/box").join(name))
    }

    /// Get the path to the isolate binary
    pub fn isolate_path(&self) -> &Path {
        &self.isolate_path
    }

    /// Write a file into the box
    #[instrument(skip(self, content))]
    pub async fn write_file(&self, name: &str, content: &[u8]) -> Result<(), IsolateError> {
        let path = self.file_path(name)?;

        // Ensure parent directory exists
        if let Some(parent) = path.parent() {
            tokio::fs::create_dir_all(parent).await?;
        }

        tokio::fs::write(&path, content).await?;
        debug!(?path, len = content.len(), "wrote file to box");
        Ok(())
    }

    /// Read a file from the box
    #[instrument(skip(self))]
    pub async fn read_file(&self, name: &str) -> Result<Vec<u8>, IsolateError> {
        let path = self.file_path(name)?;
        let content = tokio::fs::read(&path).await?;
        debug!(?path, len = content.len(), "read file from box");
        Ok(content)
    }

    /// Check if a file exists in the box
    pub async fn file_exists(&self, name: &str) -> Result<bool, IsolateError> {
        let path = self.file_path(name)?;
        Ok(tokio::fs::metadata(&path).await.is_ok())
    }

    /// Clean up the box
    ///
    /// This method should always be called before dropping the box to ensure
    /// proper resource cleanup. The return value indicates whether cleanup
    /// succeeded and should be checked.
    ///
    /// # Errors
    ///
    /// Returns an error if the isolate cleanup command fails.
    #[must_use = "cleanup errors should be handled"]
    #[instrument(skip(self))]
    pub async fn cleanup(&mut self) -> Result<(), IsolateError> {
        if !self.initialized {
            return Ok(());
        }

        let cmd = IsolateCommand::new(&self.isolate_path, self.id)
            .action(IsolateAction::Cleanup)
            .cgroup(self.cgroup);
        let args = cmd.build();

        debug!(?args, "cleaning up isolate box");

        let program = args
            .first()
            .ok_or_else(|| IsolateError::CommandFailed("empty command arguments".to_string()))?;
        let output = Command::new(program)
            .args(&args[1..])
            .output()
            .await
            .map_err(IsolateError::SpawnFailed)?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            warn!(id = self.id, stderr = %stderr, "cleanup failed");
            return Err(IsolateError::CleanupFailed {
                id: self.id,
                message: stderr.to_string(),
            });
        }

        self.initialized = false;
        debug!("box cleaned up");
        Ok(())
    }

    /// Attach a pool permit to this box
    pub(crate) fn with_permit(mut self, permit: OwnedSemaphorePermit) -> Self {
        self._permit = Some(permit);
        self
    }

    /// Check if the box is still initialized (not yet cleaned up)
    pub fn is_initialized(&self) -> bool {
        self.initialized
    }
}

impl Drop for IsolateBox {
    fn drop(&mut self) {
        if self.initialized {
            // Log a warning - best-effort cleanup in Drop is unreliable
            // Callers should explicitly call cleanup() before dropping
            warn!(
                box_id = self.id,
                box_path = %self.box_path.display(),
                "IsolateBox dropped without explicit cleanup! \
                 Call cleanup() before dropping to ensure proper resource release. \
                 Attempting best-effort cleanup via spawned thread (may not complete)."
            );

            // Try to cleanup synchronously via a blocking thread
            // This is best-effort cleanup on drop - the thread may not complete
            // before process exit, leading to leaked sandbox resources
            let isolate_path = self.isolate_path.clone();
            let id = self.id;
            let cgroup = self.cgroup;

            std::thread::spawn(move || {
                let cmd = IsolateCommand::new(&isolate_path, id)
                    .action(IsolateAction::Cleanup)
                    .cgroup(cgroup);
                let args = cmd.build();

                if let Some(program) = args.first() {
                    match std::process::Command::new(program)
                        .args(&args[1..])
                        .output()
                    {
                        Ok(output) if output.status.success() => {
                            debug!(box_id = id, "best-effort cleanup succeeded");
                        }
                        Ok(output) => {
                            let stderr = String::from_utf8_lossy(&output.stderr);
                            warn!(
                                box_id = id,
                                stderr = %stderr,
                                "best-effort cleanup failed"
                            );
                        }
                        Err(e) => {
                            warn!(box_id = id, error = %e, "best-effort cleanup spawn failed");
                        }
                    }
                }
            });
        }
    }
}

/// Pool of isolate boxes for concurrent execution
#[derive(Debug)]
pub struct BoxPool {
    /// Starting box ID
    start_id: u32,

    /// Number of boxes in the pool
    count: u32,

    /// Path to the isolate binary
    isolate_path: PathBuf,

    /// Whether cgroup support is enabled
    cgroup: bool,

    /// Semaphore to limit concurrent boxes
    semaphore: std::sync::Arc<Semaphore>,

    /// Next box ID to use (wraps around)
    next_id: std::sync::atomic::AtomicU32,
}

impl BoxPool {
    /// Create a new box pool
    pub fn new(start_id: u32, count: u32, isolate_path: impl Into<PathBuf>, cgroup: bool) -> Self {
        Self {
            start_id,
            count,
            isolate_path: isolate_path.into(),
            cgroup,
            semaphore: std::sync::Arc::new(Semaphore::new(count as usize)),
            next_id: std::sync::atomic::AtomicU32::new(start_id),
        }
    }

    /// Acquire a box from the pool
    #[instrument(skip(self))]
    pub async fn acquire(&self) -> Result<IsolateBox, IsolateError> {
        // Wait for a permit
        let permit = self
            .semaphore
            .clone()
            .acquire_owned()
            .await
            .map_err(|_| IsolateError::PoolExhausted)?;

        // Get next box ID
        let id = self
            .next_id
            .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        let id = self.start_id + (id - self.start_id) % self.count;

        debug!(id, "acquired box from pool");

        // Initialize the box
        let sandbox = IsolateBox::init(id, &self.isolate_path, self.cgroup).await?;

        Ok(sandbox.with_permit(permit))
    }

    /// Get the number of available boxes
    pub fn available(&self) -> usize {
        self.semaphore.available_permits()
    }

    /// Get the total number of boxes in the pool
    pub fn capacity(&self) -> u32 {
        self.count
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Note: These tests require the isolate binary and root privileges.
    // Run with: cargo test --features integration-tests -- --include-ignored

    #[tokio::test]
    #[cfg(feature = "integration-tests")]
    #[ignore = "requires root"]
    async fn test_box_init_cleanup() {
        let mut sandbox = IsolateBox::init(99, "isolate", false).await.unwrap();
        assert!(sandbox.path().exists());
        sandbox.cleanup().await.unwrap();
    }

    #[tokio::test]
    #[cfg(feature = "integration-tests")]
    #[ignore = "requires root"]
    async fn test_box_file_operations() {
        let mut sandbox = IsolateBox::init(98, "isolate", false).await.unwrap();

        sandbox
            .write_file("test.txt", b"hello world")
            .await
            .unwrap();
        let content = sandbox.read_file("test.txt").await.unwrap();
        assert_eq!(content, b"hello world");

        sandbox.cleanup().await.unwrap();
    }

    #[test]
    fn test_file_path_validation() {
        // Create a mock IsolateBox for path validation testing
        let sandbox = IsolateBox {
            id: 0,
            box_path: std::path::PathBuf::from("/tmp/box0"),
            isolate_path: std::path::PathBuf::from("isolate"),
            initialized: false,
            cgroup: false,
            _permit: None,
        };

        // Valid paths should work
        assert!(sandbox.file_path("main.cpp").is_ok());
        assert!(sandbox.file_path("subdir/file.txt").is_ok());

        // Path traversal should be rejected
        assert!(sandbox.file_path("../escape").is_err());
        assert!(sandbox.file_path("foo/../bar").is_err());
        assert!(sandbox.file_path("/absolute/path").is_err());
    }

    #[test]
    fn test_sandbox_path() {
        let sandbox = IsolateBox {
            id: 0,
            box_path: std::path::PathBuf::from("/var/local/lib/isolate/0"),
            isolate_path: std::path::PathBuf::from("isolate"),
            initialized: false,
            cgroup: false,
            _permit: None,
        };

        assert_eq!(
            sandbox.sandbox_path("stdin.txt").unwrap(),
            PathBuf::from("/box/stdin.txt")
        );
        assert_eq!(
            sandbox.sandbox_path("compile_stderr.txt").unwrap(),
            PathBuf::from("/box/compile_stderr.txt")
        );

        // Path traversal should be rejected
        assert!(sandbox.sandbox_path("../escape").is_err());
        assert!(sandbox.sandbox_path("/absolute/path").is_err());
    }
}