Skip to content

Commit

Permalink
Merge pull request #9 from flaryer/use_heng_core
Browse files Browse the repository at this point in the history
use heng core
  • Loading branch information
A0nameless0man authored Nov 19, 2021
2 parents 6f65220 + 522f575 commit 1686cd0
Show file tree
Hide file tree
Showing 27 changed files with 2,291 additions and 434 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

## 部署

对于 ubuntu,无论是否使用 docker,都不能跳过[启用cgroup的memory子系统](https://blog.csdn.net/SUKI547/article/details/112328873)
推荐关闭宿主机的 Swap

### Docker

Expand Down Expand Up @@ -55,9 +55,9 @@ npm run start # pm2 start ./dist/index.js --name judger

### 沙盒部分

本系统依赖魔改 `nsjail` 作为沙盒内核。需要在配置文件中指定对应路径。
本系统依赖 `nsjail``Heng-Core` 作为沙盒内核。需要在配置文件中指定对应路径。

它的仓库是 [https://github.com/flaryer/nsjail/tree/real_usr_time_kill](https://github.com/flaryer/nsjail/tree/real_usr_time_kill)
它的仓库是 <https://github.com/google/nsjail><https://github.com/ThinkSpiritLab/Heng-Core>

### 语言

Expand All @@ -73,7 +73,7 @@ npm run start # pm2 start ./dist/index.js --name judger

#### 沙盒运行支持

使用魔改 `nsjail`
使用 `nsjail``Heng-Core`

#### 语言支持

Expand All @@ -99,7 +99,6 @@ npm run start # pm2 start ./dist/index.js --name judger

对于 `Normal``Spj`,用户程序没有正常结束运行时,跳过执行结果判断程序。

可能无法检测 `OutpuLimitExceeded`

## 其他

Expand Down
4 changes: 3 additions & 1 deletion config.example.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[controller]
host = "http://localhost:8000"
host = "http://localhost:8000/c"
AccessKey = "ox00000000"
SecrectKey = "0xffffffff"
[self]
Expand All @@ -9,6 +9,8 @@ version = "0.0.1"
[nsjail]
path = "/usr/bin/nsjail"
configFile = "./jailConfig.cfg"
[hc]
path = "/usr/bin/hc"
[language]
c = "/usr/bin/gcc"
cpp = "/usr/bin/g++"
Expand Down
104 changes: 57 additions & 47 deletions jailConfig.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ log_level: FATAL
hostname: "HC-sandbox"

# keep_caps: true
cap: "CAP_SETUID"
cap: "CAP_SETGID"
cap: "CAP_KILL"
# clone_newipc: false
# clone_newuser: false
envar: "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:."
Expand All @@ -15,87 +18,94 @@ mount_proc: true
# rlimit_fsize: 256

mount {
src: '/usr'
dst: '/usr'
is_bind: true
mandatory: true
src: '/usr'
dst: '/usr'
is_bind: true
mandatory: true
}

mount {
src: '/etc'
dst: '/etc'
is_bind: true
mandatory: true
src: '/etc'
dst: '/etc'
is_bind: true
mandatory: true
}

# mount {
# src: "/dev"
# dst: "/dev"
# is_bind: true
# src: "/dev"
# dst: "/dev"
# is_bind: true
# }

mount {
src: "/dev/random"
dst: "/dev/random"
is_bind: true
src: "/dev/random"
dst: "/dev/random"
is_bind: true
}
mount {
src: "/dev/urandom"
dst: "/dev/urandom"
is_bind: true
src: "/dev/urandom"
dst: "/dev/urandom"
is_bind: true
}

# mount {
# src: "/proc/self/fd/0"
# dst: "/dev/stdin"
# is_bind: true
# is_symlink: true
# src: "/proc/self/fd/0"
# dst: "/dev/stdin"
# is_bind: true
# is_symlink: true
# }
# mount {
# src: "/proc/self/fd/1"
# dst: "/dev/stdout"
# is_bind: true
# is_symlink: true
# src: "/proc/self/fd/1"
# dst: "/dev/stdout"
# is_bind: true
# is_symlink: true
# }
# mount {
# src: "/proc/self/fd/2"
# dst: "/dev/stderr"
# is_bind: true
# is_symlink: true
# src: "/proc/self/fd/2"
# dst: "/dev/stderr"
# is_bind: true
# is_symlink: true
# }

mount {
src: "/lib"
dst: "/lib"
is_bind: true
src: "/lib"
dst: "/lib"
is_bind: true
}

mount {
src: "/bin"
dst: "/bin"
is_bind: true
src: "/bin"
dst: "/bin"
is_bind: true
}

mount {
src: "/sbin"
dst: "/sbin"
is_bind: true
src: "/sbin"
dst: "/sbin"
is_bind: true
}

mount {
src: "/lib64"
dst: "/lib64"
is_bind: true
src: "/lib64"
dst: "/lib64"
is_bind: true
}

mount {
src: "/var"
dst: "/var"
is_bind: true
src: "/var"
dst: "/var"
is_bind: true
}

mount {
src: "/run"
dst: "/run"
is_bind: true
src: "/run"
dst: "/run"
is_bind: true
}

mount {
src: '/sys/fs/cgroup'
dst: '/sys/fs/cgroup'
is_bind: true
mandatory: true
}
5 changes: 4 additions & 1 deletion prepare-centos8.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ npm i -g cnpm --registry=https://registry.npm.taobao.org && source /etc/profile
git clone -b v0.4.0 --depth=1 --single-branch https://github.com.cnpmjs.org/ThinkSpiritLab/ojcmp.git ~/ojcmp \
&& cd ~/ojcmp && cargo build --release && cp target/release/ojcmp /usr/bin

git clone -b real_usr_time_kill --depth=1 --single-branch https://github.com.cnpmjs.org/flaryer/nsjail.git ~/nsjail \
git clone --depth=1 --single-branch https://github.com.cnpmjs.org/google/nsjail.git ~/nsjail \
&& cd ~/nsjail && make && cp ~/nsjail/nsjail /usr/bin/nsjail

git clone --depth=1 --single-branch https://github.com.cnpmjs.org/ThinkSpiritLab/Heng-Core.git ~/Heng-Core \
&& cd ~/Heng-Core && make && cp ~/Heng-Core/hc /usr/bin/hc && cd ~

cp -r ~/.rustup/toolchains/`ls ~/.rustup/toolchains/ | grep "stable"` /usr/local/rustup && ln -s /usr/local/rustup/bin/rustc /usr/bin/rustc

cp $HCDIR/Tools/testlib.h /testlib.h
Expand Down
6 changes: 5 additions & 1 deletion prepare-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ npm install -g npm
git clone -b v0.4.0 --depth=1 --single-branch https://github.com/ThinkSpiritLab/ojcmp.git ~/ojcmp \
&& cd ~/ojcmp && cargo build --release && cp target/release/ojcmp /usr/bin && cd ~

git clone -b real_usr_time_kill --depth=1 --single-branch https://github.com/flaryer/nsjail.git ~/nsjail \
git clone --depth=1 --single-branch https://github.com/google/nsjail.git ~/nsjail \
&& cd ~/nsjail && make && cp ~/nsjail/nsjail /usr/bin/nsjail && cd ~

git clone --depth=1 --single-branch https://github.com/ThinkSpiritLab/Heng-Core.git ~/Heng-Core \
&& cd ~/Heng-Core && make && cp ~/Heng-Core/hc /usr/bin/hc && cd ~

cp -r ~/.rustup/toolchains/`ls ~/.rustup/toolchains/ | grep "stable"` /usr/local/rustup && ln -s /usr/local/rustup/bin/rustc /usr/bin/rustc

cp $HCDIR/Tools/testlib.h /testlib.h
Expand All @@ -50,6 +53,7 @@ rm -rf /var/cache/yum
rm -rf /var/cache/dnf
rm -rf ~/ojcmp
rm -rf ~/nsjail
rm -rf ~/Heng-Core
rm -rf ~/.cargo
rm -rf ~/.rustup/
rm -rf /usr/local/rustup/share/doc
Expand Down
7 changes: 4 additions & 3 deletions prepare-ubuntu20.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ npm i -g cnpm --registry=https://registry.npm.taobao.org && source /etc/profile
git clone -b v0.4.0 --depth=1 --single-branch https://github.com.cnpmjs.org/ThinkSpiritLab/ojcmp.git ~/ojcmp \
&& cd ~/ojcmp && cargo build --release && cp target/release/ojcmp /usr/bin

git clone -b real_usr_time_kill --depth=1 --single-branch https://github.com.cnpmjs.org/flaryer/nsjail.git ~/nsjail \
git clone --depth=1 --single-branch https://github.com.cnpmjs.org/google/nsjail.git ~/nsjail \
&& sed -i '/ .set_tid = 0,\| .set_tid_size = 0,\| .cgroup = 0,/d' ~/nsjail/subproc.cc \
&& cd ~/nsjail && make && cp ~/nsjail/nsjail /usr/bin/nsjail

git clone --depth=1 --single-branch https://github.com.cnpmjs.org/ThinkSpiritLab/Heng-Core.git ~/Heng-Core \
&& cd ~/Heng-Core && make && cp ~/Heng-Core/hc /usr/bin/hc && cd ~

cp -r ~/.rustup/toolchains/`ls ~/.rustup/toolchains/ | grep "stable"` /usr/local/rustup && ln -s /usr/local/rustup/bin/rustc /usr/bin/rustc

cp $HCDIR/Tools/testlib.h /testlib.h

cd $HCDIR && cnpm install && npm run build

echo -e "\033[5;31m Must see https://blog.csdn.net/SUKI547/article/details/112328873 \033[0m"
9 changes: 9 additions & 0 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export class JailConfig {
@IsNotEmpty()
configFile!: string;
}
export class MeterConfig {
@IsString()
@IsNotEmpty()
path!: string;
}
export class ControllerConfig {
@IsString()
@IsNotEmpty()
Expand Down Expand Up @@ -137,6 +142,10 @@ export class Config {
nsjail!: JailConfig;
@ValidateNested()
@IsNotEmpty()
@Type(() => MeterConfig)
hc!: MeterConfig;
@ValidateNested()
@IsNotEmpty()
@Type(() => JudgeFactoryConfig)
judger!: JudgeFactoryConfig;
}
Expand Down
30 changes: 10 additions & 20 deletions src/SelfTest/BOMB/BULL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,18 @@ const output = `
`;
const usrCode = `
#include <stdio.h>
#include <unistd.h>
int main(void) {
for (int i = 0; i < 1000000000; i++) {
write(1, "11111111111111111111111111111111111\\n", 37);
}
while(1) printf("1");
return 0;
}
`;

export const BOMBBULL = generateNormalSelfTest(
"BOMBBULL",
"c",
usrCode,
{},
[
{
type: "direct",
input,
output,
expectResultType: JudgeResultKind.TimeLimitExceeded,
count: false,
},
],
2000
);
export const BOMBBULL = generateNormalSelfTest("BOMBBULL", "c", usrCode, {}, [
{
type: "direct",
input,
output,
expectResultType: JudgeResultKind.OutpuLimitExceeded,
count: false,
},
]);
34 changes: 34 additions & 0 deletions src/SelfTest/BOMB/KILLTIMER.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { JudgeResultKind } from "heng-protocol";
import { generateNormalSelfTest } from "../util";

const input = `
`;
const output = `1
`;
const usrCode = `
#include <signal.h>
int main(void)
{
kill(1, SIGKILL); // init
kill(3, SIGKILL); // timer
return 0;
}
`;

export const KILLTIMER = generateNormalSelfTest(
"KILLTIMER",
"c",
usrCode,
{},
[
{
type: "direct",
input,
output,
expectResultType: JudgeResultKind.WrongAnswer,
count: false,
},
],
2000
);
4 changes: 3 additions & 1 deletion src/SelfTest/BOMB/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BOMBCMP } from "./CMP";
import { BOMBCOLE } from "./COLE";
import { BOMBCTLE } from "./CTLE";
import { BOMBFORKBOMB } from "./FORKBOMB";
import { KILLTIMER } from "./KILLTIMER";
import { BOMBSPJTLE } from "./SPJTLE";

export const BOMB = [
Expand All @@ -13,5 +14,6 @@ export const BOMB = [
BOMBCMP,
BOMBBADSPJ,
BOMBSPJTLE,
BOMBBULL
BOMBBULL,
KILLTIMER,
];
Loading

0 comments on commit 1686cd0

Please sign in to comment.