-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Judger): 🚧 draft timelife of a runner
- Loading branch information
Showing
9 changed files
with
211 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod config; | ||
mod daemon; | ||
mod plugin; | ||
mod spec; | ||
mod stage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::{ | ||
language::spec::Spec, | ||
sandbox::{Corpse, MonitorKind}, | ||
}; | ||
|
||
pub enum AssertionMode { | ||
SkipSpace, | ||
SkipContinousSpace, | ||
Exact, | ||
} | ||
|
||
pub enum AssertResult { | ||
Accept, | ||
WrongAnswer, | ||
RuntimeError, | ||
TimeLimitExceeded, | ||
MemoryLimitExceeded, | ||
OutputLimitExceeded, | ||
RealTimeLimitExceeded, | ||
CompileError, | ||
SystemError, | ||
} | ||
pub struct AssertRunner { | ||
pub spec: Arc<Spec>, | ||
pub corpse: Corpse, | ||
} | ||
|
||
impl AssertRunner { | ||
pub fn new(spec: Arc<Spec>, corpse: Corpse) -> Self { | ||
Self { spec, corpse } | ||
} | ||
fn assert_output(&self, output: &[u8], mode: AssertionMode) -> AssertResult { | ||
todo!() | ||
} | ||
pub fn get_result(&self, output: &[u8], mode: AssertionMode) -> AssertResult { | ||
match self.corpse.status() { | ||
Ok(status) => match status.success() { | ||
true => self.assert_output(output, mode), | ||
false => AssertResult::WrongAnswer, | ||
}, | ||
Err(reason) => match reason { | ||
MonitorKind::Cpu => AssertResult::TimeLimitExceeded, | ||
MonitorKind::Memory => AssertResult::MemoryLimitExceeded, | ||
MonitorKind::Output => AssertResult::OutputLimitExceeded, | ||
MonitorKind::Walltime => AssertResult::RealTimeLimitExceeded, | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::{path::PathBuf, sync::Arc, time::Duration}; | ||
|
||
use crate::{ | ||
filesystem::MountHandle, | ||
language::spec::Spec, | ||
sandbox::{Context, Limit, Process}, | ||
Result, | ||
}; | ||
|
||
use super::JudgeRunner; | ||
|
||
pub struct CompileRunner { | ||
spec: Arc<Spec>, | ||
handle: MountHandle, | ||
} | ||
|
||
impl CompileRunner { | ||
pub fn new(spec: Arc<Spec>, handle: MountHandle) -> Self { | ||
Self { spec, handle } | ||
} | ||
pub async fn run(self) -> Result<Option<JudgeRunner>> { | ||
let ctx = CompileCtx { | ||
spec: self.spec.clone(), | ||
path: self.handle.get_path().to_path_buf(), | ||
}; | ||
let process = Process::new(ctx)?; | ||
let corpse = process.wait(Vec::new()).await?; | ||
if !corpse.success() { | ||
log::debug!("compile failed {:?}", corpse.status()); | ||
return Ok(None); | ||
} | ||
|
||
let runner = JudgeRunner::new(self.handle, self.spec); | ||
Ok(Some(runner)) | ||
} | ||
} | ||
|
||
struct CompileCtx { | ||
spec: Arc<Spec>, | ||
path: PathBuf, | ||
} | ||
|
||
impl Limit for CompileCtx { | ||
fn get_cpu(&mut self) -> crate::sandbox::Cpu { | ||
todo!() | ||
} | ||
fn get_memory(&mut self) -> crate::sandbox::Memory { | ||
todo!() | ||
} | ||
fn get_output(&mut self) -> u64 { | ||
todo!() | ||
} | ||
fn get_walltime(&mut self) -> Duration { | ||
todo!() | ||
} | ||
} | ||
|
||
impl Context for CompileCtx { | ||
type FS = PathBuf; | ||
fn get_fs(&mut self) -> Self::FS { | ||
self.path.clone() | ||
} | ||
fn get_args(&mut self) -> impl Iterator<Item = &std::ffi::OsStr> { | ||
self.spec.compile_command.iter().map(|arg| arg.as_os_str()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use std::{path::PathBuf, sync::Arc, time::Duration}; | ||
|
||
use crate::{ | ||
filesystem::MountHandle, | ||
language::spec::Spec, | ||
sandbox::{Context, Cpu, Limit, Memory, Process}, | ||
Result, | ||
}; | ||
|
||
use super::assert::AssertRunner; | ||
|
||
pub struct JudgeRunner { | ||
filesystem: MountHandle, | ||
spec: Arc<Spec>, | ||
} | ||
|
||
impl JudgeRunner { | ||
pub fn new(filesystem: MountHandle, spec: Arc<Spec>) -> Self { | ||
Self { filesystem, spec } | ||
} | ||
pub async fn run(self, (mem, cpu): (Memory, Cpu), input: Vec<u8>) -> Result<AssertRunner> { | ||
let ctx = JudgeCtx { | ||
spec: self.spec.clone(), | ||
path: self.filesystem.get_path().to_path_buf(), | ||
limit: self.spec.get_judge_limit(cpu, mem), | ||
}; | ||
let process = Process::new(ctx)?; | ||
let corpse = process.wait(input).await?; | ||
drop(self.filesystem); | ||
Ok(AssertRunner::new(self.spec, corpse)) | ||
} | ||
} | ||
|
||
struct JudgeCtx { | ||
spec: Arc<Spec>, | ||
path: std::path::PathBuf, | ||
limit: (Cpu, Memory, u64, Duration), | ||
} | ||
|
||
impl Limit for JudgeCtx { | ||
fn get_cpu(&mut self) -> Cpu { | ||
self.limit.0.clone() | ||
} | ||
fn get_memory(&mut self) -> Memory { | ||
self.limit.1.clone() | ||
} | ||
fn get_output(&mut self) -> u64 { | ||
self.limit.2 | ||
} | ||
fn get_walltime(&mut self) -> Duration { | ||
self.limit.3 | ||
} | ||
} | ||
|
||
impl Context for JudgeCtx { | ||
type FS = PathBuf; | ||
fn get_fs(&mut self) -> Self::FS { | ||
self.path.clone() | ||
} | ||
fn get_args(&mut self) -> impl Iterator<Item = &std::ffi::OsStr> { | ||
self.spec.judge_command.iter().map(|s| s.as_ref()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
mod assert; | ||
mod compile; | ||
mod judge; | ||
|
||
pub use compile::CompileRunner; | ||
pub use judge::JudgeRunner; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters