Isolate Integration
English | 简体中文
A Rust interface for the ioi/isolate sandbox program.
Uses the tokio async runtime to manage sandbox lifecycle and execute commands.
Prerequisites
First, ensure that isolate is installed on your system. Please refer to ioi/isolate.
For isolate configuration, see the INSTALLATION section of the isolate document.
If you need to use cgroup related features (recommended, such as the --cg-mem option), ensure your system supports cgroup v2.
Basic Usage
For isolate usage, option meanings, etc., please refer to the isolate document.
More examples can be found in the examples directory.
Here's an example:
use ;
async
Advanced Configuration
Directory Binding
use DirectoryRule;
let sandbox = new
// Bind external directory to sandbox
.with_directory_rule
// Create temporary directory
.with_directory_rule
// Bind system directory (read-only, no execution)
.with_directory_rule
// Mount filesystem
.with_directory_rule;
Environment Variables
use EnvRule;
let sandbox = new
// Inherit environment variable
.with_env_rule
// Set environment variable
.with_env_rule
// Inherit all environment variables
.with_env_rule;
Resource Limits
let limits = new
.with_time_limit // CPU time limit
.with_wall_time_limit // Wall clock time limit
.with_memory_limit // Memory limit
.with_cg_memory_limit // Control group memory limit
.with_open_files_limit // Open files limit
.with_file_size_limit // File size limit
.with_process_limit; // Process limit
Special Options
let sandbox = new
.use_cgroups // Enable control groups
.disable_cgroups // Disable control groups
.share_network // Share network namespace
.no_default_dirs // Don't bind default directories
.verbose; // Verbose output
Build and Run Examples
See the complete example in examples/sandbox_usage.rs.
Run the example:
API Quick Reference
IsolateSandbox
Main sandbox class, provides the following methods:
new(box_id: u32)- Create a new sandbox instanceinit(&self, limits: &ResourceLimits)- Initialize the sandboxrun<I, S>(&self, program: &str, args: I, limits: &ResourceLimits)- Run a commandcleanup(&self)- Clean up the sandboxdisable_cgroups(self)- Disable control groups (if not needed)
ResourceLimits
Resource limit configuration:
with_time_limit(seconds: f64)- Set CPU time limitwith_memory_limit(kb: u32)- Set memory limitwith_wall_time_limit(seconds: f64)- Set wall clock time limitwith_process_limit(count: u32)- Set process limit. Unlike isolate's--processesoption, unlimited processes are allowed when this limit is not specified.
DirectoryRule
Directory binding rules:
bind(inside, outside)- Bind external directorybind_same(path)- Bind to the same pathtmp(path)- Create temporary directoryfilesystem(name)- Mount filesystem
ExecutionResult
Execution result contains:
exit_code: Option<i32>- Exit codesignal: Option<i32>- Signaltime_used: f64- CPU timewall_time_used: f64- Wall clock timememory_used: u32- Memory usagekilled: bool- Whether killedstdout: String- Standard outputstderr: String- Standard error