Skip to content

Commit

Permalink
feat: support nodejs (#9)
Browse files Browse the repository at this point in the history
* feat: support nodejs

* chore: remove useless code
  • Loading branch information
fu050409 authored Dec 1, 2024
1 parent beaba06 commit 5867664
Show file tree
Hide file tree
Showing 23 changed files with 555 additions and 131 deletions.
12 changes: 12 additions & 0 deletions .changes/nodejs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"eval-stack": minor:feat
---

Add support for Deno.js, and other changes:

- Use deno instead of node for executing JavaScript files, deny all permissions by default.
- Allow stderr to be piped since we can capture it for error messages.
- Disable core dumps by default.
- Set CPU time limits using `libc`.
- Use seccomp to restrict sys calls and fs operations.
- Prevent process to create subprocesses.
3 changes: 3 additions & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
"NEWNS",
"prctl",
"PRIVS",
"pycache",
"rlim",
"rlimit",
"rustc",
"rustup",
"seccomp",
"seccompiler",
"serde",
"setrlimit",
"SIGBUS",
Expand Down
125 changes: 86 additions & 39 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ anyhow = "1.0.92"
libc = "0.2.161"
tokio = { version = "1.41.0", features = ["full"] }

nix = { version = "0.29.0", features = ["user"], optional = true }
serde = { version = "1.0.215", features = ["derive"], optional = true }
seccompiler = "0.4.0"
which = "7.0.0"

[features]
default = ["serde"]
rerun = ["nix"]
serde = ["dep:serde"]
21 changes: 17 additions & 4 deletions src/case.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{fs::create_dir_all, path::PathBuf, time::Duration};

use anyhow::Result;
use which::which;

use crate::{
compile::{compile, Language},
Expand Down Expand Up @@ -31,7 +32,8 @@ where
.to_string_lossy()
.to_string();
let exec_path = match &language {
Language::Python => PathBuf::from("python"),
Language::Python => which("python")?,
Language::NodeJs => which("deno")?,
_ => workspace.join("out"),
};

Expand All @@ -52,9 +54,20 @@ where
}]);
};

let args = vec![source_file_path.as_str()];
let py_args = vec![source_file_path.as_str()];
let deno_args = vec![
"run",
format!("--v8-flags=--max-old-space-size={}", options.memory_limit).leak(),
"--deny-read=*",
"--deny-write=*",
"--deny-env=*",
"--deny-run=*",
"--deny-ffi=*",
source_file_path.as_str(),
];
let args = match language {
Language::Python => Some(&args),
Language::Python => Some(&py_args),
Language::NodeJs => Some(&deno_args),
_ => None,
}
.map(|v| &**v);
Expand All @@ -73,7 +86,7 @@ where
workspace.join("test.out"),
)
.await?;
if options.fast_fail && !matches!(result.status, JudgeStatus::Accepted) {
if options.fail_fast && !matches!(result.status, JudgeStatus::Accepted) {
results.push(result);
break;
}
Expand Down
Loading

0 comments on commit 5867664

Please sign in to comment.