code_executor/
lib.rs

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
#[cfg(test)]
extern crate quickcheck;
#[cfg(test)]
#[macro_use(quickcheck)]
extern crate quickcheck_macros;

mod error;
pub mod runner;
mod sandbox;
mod util;

use std::time::Duration;

use bon::Builder;

pub use error::*;
use nix::sys::resource::Resource;
use runner::Runner;
use sandbox::RlimitConfig;

#[derive(Debug, Clone, Copy, Builder)]
pub struct CommandArgs<'a> {
    pub binary: &'a str,
    pub args: &'a [&'a str],
}

const DEFAULT_RLIMIT_CONFIGS: &[RlimitConfig] = &[
    RlimitConfig {
        resource: Resource::RLIMIT_STACK,
        soft_limit: 1024 * 1024 * 1024 * 1024,
        hard_limit: 1024 * 1024 * 1024 * 1024,
    },
    RlimitConfig {
        resource: Resource::RLIMIT_AS,
        soft_limit: 1024 * 1024 * 1024 * 1024,
        hard_limit: 1024 * 1024 * 1024 * 1024,
    },
    RlimitConfig {
        resource: Resource::RLIMIT_CPU,
        soft_limit: 60,
        hard_limit: 90,
    },
    // RlimitConfig {
    //     resource: Resource::RLIMIT_NPROC,
    //     soft_limit: 1,
    //     hard_limit: 1,
    // },
    RlimitConfig {
        resource: Resource::RLIMIT_FSIZE,
        soft_limit: 1024,
        hard_limit: 1024,
    },
];

const DEFAULT_SCMP_BLACK_LIST: &[&str] = &["fork", "vfork"];

pub const CPP_RUNNER: Runner = Runner {
    main_file: "main.cpp",
    compiler_args: Some(CommandArgs {
        binary: "g++",
        args: &["-o", "main", "main.cpp"],
    }),
    sandbox_config: sandbox::Config {
        scmp_black_list: DEFAULT_SCMP_BLACK_LIST,
        rlimit_configs: DEFAULT_RLIMIT_CONFIGS,
        time_limit: Duration::from_secs(2),
        args: CommandArgs {
            binary: "./main",
            args: &[],
        },
    },
};

pub const PYTHON_RUNNER: Runner = Runner {
    main_file: "main.py",
    compiler_args: None,
    sandbox_config: sandbox::Config {
        scmp_black_list: DEFAULT_SCMP_BLACK_LIST,
        rlimit_configs: DEFAULT_RLIMIT_CONFIGS,
        time_limit: Duration::from_secs(10),
        args: CommandArgs {
            binary: "python",
            args: &["main.py"],
        },
    },
};

pub const JAVA_RUNNER: Runner = Runner {
    main_file: "Main.java",
    compiler_args: Some(CommandArgs {
        binary: "javac",
        args: &["Main.java"],
    }),
    sandbox_config: sandbox::Config {
        scmp_black_list: DEFAULT_SCMP_BLACK_LIST,
        rlimit_configs: DEFAULT_RLIMIT_CONFIGS,
        time_limit: Duration::from_secs(4),
        args: CommandArgs {
            binary: "java",
            args: &["Main"],
        },
    },
};

#[cfg(test)]
mod tests {
    use std::path::Path;
    use std::{fs, io::Write};

    use anyhow::{Error, Result};
    use tempfile::NamedTempFile;

    use crate::{
        CPP_RUNNER, JAVA_RUNNER, PYTHON_RUNNER,
        runner::{self, Runner},
    };

    fn read_code(problem_path: &Path, runner: &Runner<'_>) -> Result<String> {
        fs::read_to_string(problem_path.join(runner.main_file)).map_err(Error::from)
    }

    #[test]
    fn test_hello_world() -> Result<()> {
        const CODE_PATH: &str = "test/hello-world";
        const CORRECT_OUTPUT: &str = "Hello World";

        let code_path = Path::new(CODE_PATH);
        let input = NamedTempFile::new()?;

        for runner in [CPP_RUNNER, JAVA_RUNNER, PYTHON_RUNNER] {
            let code = read_code(code_path, &runner)?;

            let metrics = runner.run(&code, input.path()).unwrap();
            assert_eq!(
                metrics.output,
                runner::Output::Success(CORRECT_OUTPUT.to_string())
            );
        }

        Ok(())
    }

    #[quickcheck]
    fn test_add(a: u64, b: u64) -> Result<()> {
        const CODE_PATH: &str = "test/add";

        let code_path = Path::new(CODE_PATH);
        let mut input = NamedTempFile::new()?;
        input.write_fmt(format_args!("{}\n{}\n", a, b))?;

        for runner in [CPP_RUNNER, JAVA_RUNNER, PYTHON_RUNNER] {
            let code = read_code(code_path, &runner)?;

            let metrics = runner.run(&code, input.path()).unwrap();
            assert_eq!(metrics.output, runner::Output::Success((a + b).to_string()));
        }

        Ok(())
    }
}