Skip to content

Commit

Permalink
fix(Judger): 🐛 fix plugin(c, cpp) cannot create file during compile s…
Browse files Browse the repository at this point in the history
…tage after default to complie stage lockdown
  • Loading branch information
Eason0729 committed Jan 10, 2024
1 parent 7e4c0d4 commit dccabdf
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 37 deletions.
2 changes: 1 addition & 1 deletion judger/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ release-plugin:
cd plugins sh export-all.sh

build-nsjail:
cd nsjail-docker && make nsjail-3.1
cd nsjail-docker && sudo make nsjail-3.1
cp nsjail-docker/output/*-linux-musl/nsjail-* .

prepare:
Expand Down
36 changes: 18 additions & 18 deletions judger/plugins/c-11/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@
#include <spawn.h>
#include <errno.h>
#include <sys/wait.h>
#define handle(x,e) { \
if (e == x) \
{ \
printf("4: %m\n", errno); \
return 1; \
} \
}
#define CC "/usr/local/bin/g++"
#define SRC "/src/src.c"
#define SRC "/src/src.cpp"
#define OUT "/src/src.out"
#define MAX_SIZE 131072

int main()
{
FILE *source = fopen(SRC, "w");
if (source == NULL)
{
printf("2: %m\n", errno);
return 1;
}
else
printf("1: success create file!\n");
handle(source,NULL);

printf("1: success create file!\n");

char *code = malloc(MAX_SIZE * sizeof(char));
size_t len = fread(code, sizeof(char), MAX_SIZE, stdin);
Expand All @@ -29,15 +32,12 @@ int main()

char *args[] = {CC, "-x", "c", SRC, "-lm", "-o", OUT, NULL};
int pid, status;
if (execvp(CC, args) != -1)
{
printf("1: success execv!\n");
if (wait(NULL) != -1)
{
printf("0: success!\n");
return 0;
}
}
printf("4: %m\n", errno);

handle(chdir("/tmp"),-1);
handle(execvp(CC, args),-1);
printf("1: success execv!\n");

handle(wait(NULL),-1);
printf("0: success!\n");
return 1;
}
34 changes: 17 additions & 17 deletions judger/plugins/cpp-11/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
#include <spawn.h>
#include <errno.h>
#include <sys/wait.h>
#define handle(x,e) { \
if (e == x) \
{ \
printf("4: %m\n", errno); \
return 1; \
} \
}
#define CC "/usr/local/bin/g++"
#define SRC "/src/src.cpp"
#define OUT "/src/src.out"
Expand All @@ -13,13 +20,9 @@
int main()
{
FILE *source = fopen(SRC, "w");
if (source == NULL)
{
printf("2: %m\n", errno);
return 1;
}
else
printf("1: success create file!\n");
handle(source,NULL);

printf("1: success create file!\n");

char *code = malloc(MAX_SIZE * sizeof(char));
size_t len = fread(code, sizeof(char), MAX_SIZE, stdin);
Expand All @@ -29,15 +32,12 @@ int main()

char *args[] = {CC, SRC, "-lm", "-o", OUT, NULL};
int pid, status;
if (execvp(CC, args) != -1)
{
printf("1: success execv!\n");
if (wait(NULL) != -1)
{
printf("0: success!\n");
return 0;
}
}
printf("4: %m\n", errno);

handle(chdir("/tmp"),-1);
handle(execvp(CC, args),-1);
printf("1: success execv!\n");

handle(wait(NULL),-1);
printf("0: success!\n");
return 1;
}
1 change: 1 addition & 0 deletions judger/src/init/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn init() {
let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
default_panic(info);
log::error!("Panic at {}",info.location().map(|x|x.to_string()).unwrap_or_default());
std::process::exit(1);
}));
}
2 changes: 2 additions & 0 deletions judger/src/langs/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ impl<'a> CompiledArtifact<'a> {
time: u64,
memory: u64,
) -> Result<TaskResult, Error> {
log::debug!("Exit status: {}",self.process.status);
debug_assert!(self.process.succeed());
let spec = self.spec;
let mut limit = spec.judge_limit.clone().apply_platform();
Expand Down Expand Up @@ -227,6 +228,7 @@ impl<'a> CompiledArtifact<'a> {
time: u64,
memory: u64,
) -> Result<ExecResult, Error> {
log::debug!("Exit status: {}",self.process.status);
debug_assert!(self.process.succeed());
let spec = self.spec;
let mut limit = spec.judge_limit.clone().apply_platform();
Expand Down
4 changes: 3 additions & 1 deletion judger/src/sandbox/utils/nsjail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ impl MountBuilder {

self
}
pub fn done(self) -> CommonBuilder {
pub fn done(mut self) -> CommonBuilder {
self.cmds.push(Cow::Borrowed("--tmpfsmount"));
self.cmds.push(Cow::Borrowed("/tmp"));
CommonBuilder { cmds: self.cmds }
}
}
Expand Down

0 comments on commit dccabdf

Please sign in to comment.