Skip to content

Commit 1686cd0

Browse files
Merge pull request #9 from flaryer/use_heng_core
use heng core
2 parents 6f65220 + 522f575 commit 1686cd0

27 files changed

+2291
-434
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
## 部署
1010

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

1313
### Docker
1414

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

5656
### 沙盒部分
5757

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

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

6262
### 语言
6363

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

7474
#### 沙盒运行支持
7575

76-
使用魔改 `nsjail`
76+
使用 `nsjail``Heng-Core`
7777

7878
#### 语言支持
7979

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

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

102-
可能无法检测 `OutpuLimitExceeded`
103102

104103
## 其他
105104

config.example.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[controller]
2-
host = "http://localhost:8000"
2+
host = "http://localhost:8000/c"
33
AccessKey = "ox00000000"
44
SecrectKey = "0xffffffff"
55
[self]
@@ -9,6 +9,8 @@ version = "0.0.1"
99
[nsjail]
1010
path = "/usr/bin/nsjail"
1111
configFile = "./jailConfig.cfg"
12+
[hc]
13+
path = "/usr/bin/hc"
1214
[language]
1315
c = "/usr/bin/gcc"
1416
cpp = "/usr/bin/g++"

jailConfig.cfg

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ log_level: FATAL
55
hostname: "HC-sandbox"
66

77
# keep_caps: true
8+
cap: "CAP_SETUID"
9+
cap: "CAP_SETGID"
10+
cap: "CAP_KILL"
811
# clone_newipc: false
912
# clone_newuser: false
1013
envar: "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:."
@@ -15,87 +18,94 @@ mount_proc: true
1518
# rlimit_fsize: 256
1619

1720
mount {
18-
src: '/usr'
19-
dst: '/usr'
20-
is_bind: true
21-
mandatory: true
21+
src: '/usr'
22+
dst: '/usr'
23+
is_bind: true
24+
mandatory: true
2225
}
2326

2427
mount {
25-
src: '/etc'
26-
dst: '/etc'
27-
is_bind: true
28-
mandatory: true
28+
src: '/etc'
29+
dst: '/etc'
30+
is_bind: true
31+
mandatory: true
2932
}
3033

3134
# mount {
32-
# src: "/dev"
33-
# dst: "/dev"
34-
# is_bind: true
35+
# src: "/dev"
36+
# dst: "/dev"
37+
# is_bind: true
3538
# }
3639

3740
mount {
38-
src: "/dev/random"
39-
dst: "/dev/random"
40-
is_bind: true
41+
src: "/dev/random"
42+
dst: "/dev/random"
43+
is_bind: true
4144
}
4245
mount {
43-
src: "/dev/urandom"
44-
dst: "/dev/urandom"
45-
is_bind: true
46+
src: "/dev/urandom"
47+
dst: "/dev/urandom"
48+
is_bind: true
4649
}
4750

4851
# mount {
49-
# src: "/proc/self/fd/0"
50-
# dst: "/dev/stdin"
51-
# is_bind: true
52-
# is_symlink: true
52+
# src: "/proc/self/fd/0"
53+
# dst: "/dev/stdin"
54+
# is_bind: true
55+
# is_symlink: true
5356
# }
5457
# mount {
55-
# src: "/proc/self/fd/1"
56-
# dst: "/dev/stdout"
57-
# is_bind: true
58-
# is_symlink: true
58+
# src: "/proc/self/fd/1"
59+
# dst: "/dev/stdout"
60+
# is_bind: true
61+
# is_symlink: true
5962
# }
6063
# mount {
61-
# src: "/proc/self/fd/2"
62-
# dst: "/dev/stderr"
63-
# is_bind: true
64-
# is_symlink: true
64+
# src: "/proc/self/fd/2"
65+
# dst: "/dev/stderr"
66+
# is_bind: true
67+
# is_symlink: true
6568
# }
6669

6770
mount {
68-
src: "/lib"
69-
dst: "/lib"
70-
is_bind: true
71+
src: "/lib"
72+
dst: "/lib"
73+
is_bind: true
7174
}
7275

7376
mount {
74-
src: "/bin"
75-
dst: "/bin"
76-
is_bind: true
77+
src: "/bin"
78+
dst: "/bin"
79+
is_bind: true
7780
}
7881

7982
mount {
80-
src: "/sbin"
81-
dst: "/sbin"
82-
is_bind: true
83+
src: "/sbin"
84+
dst: "/sbin"
85+
is_bind: true
8386
}
8487

8588
mount {
86-
src: "/lib64"
87-
dst: "/lib64"
88-
is_bind: true
89+
src: "/lib64"
90+
dst: "/lib64"
91+
is_bind: true
8992
}
9093

9194
mount {
92-
src: "/var"
93-
dst: "/var"
94-
is_bind: true
95+
src: "/var"
96+
dst: "/var"
97+
is_bind: true
9598
}
9699

97100
mount {
98-
src: "/run"
99-
dst: "/run"
100-
is_bind: true
101+
src: "/run"
102+
dst: "/run"
103+
is_bind: true
104+
}
105+
106+
mount {
107+
src: '/sys/fs/cgroup'
108+
dst: '/sys/fs/cgroup'
109+
is_bind: true
110+
mandatory: true
101111
}

prepare-centos8.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ npm i -g cnpm --registry=https://registry.npm.taobao.org && source /etc/profile
6868
git clone -b v0.4.0 --depth=1 --single-branch https://github.com.cnpmjs.org/ThinkSpiritLab/ojcmp.git ~/ojcmp \
6969
&& cd ~/ojcmp && cargo build --release && cp target/release/ojcmp /usr/bin
7070

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

74+
git clone --depth=1 --single-branch https://github.com.cnpmjs.org/ThinkSpiritLab/Heng-Core.git ~/Heng-Core \
75+
&& cd ~/Heng-Core && make && cp ~/Heng-Core/hc /usr/bin/hc && cd ~
76+
7477
cp -r ~/.rustup/toolchains/`ls ~/.rustup/toolchains/ | grep "stable"` /usr/local/rustup && ln -s /usr/local/rustup/bin/rustc /usr/bin/rustc
7578

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

prepare-docker.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ npm install -g npm
3636
git clone -b v0.4.0 --depth=1 --single-branch https://github.com/ThinkSpiritLab/ojcmp.git ~/ojcmp \
3737
&& cd ~/ojcmp && cargo build --release && cp target/release/ojcmp /usr/bin && cd ~
3838

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

42+
git clone --depth=1 --single-branch https://github.com/ThinkSpiritLab/Heng-Core.git ~/Heng-Core \
43+
&& cd ~/Heng-Core && make && cp ~/Heng-Core/hc /usr/bin/hc && cd ~
44+
4245
cp -r ~/.rustup/toolchains/`ls ~/.rustup/toolchains/ | grep "stable"` /usr/local/rustup && ln -s /usr/local/rustup/bin/rustc /usr/bin/rustc
4346

4447
cp $HCDIR/Tools/testlib.h /testlib.h
@@ -50,6 +53,7 @@ rm -rf /var/cache/yum
5053
rm -rf /var/cache/dnf
5154
rm -rf ~/ojcmp
5255
rm -rf ~/nsjail
56+
rm -rf ~/Heng-Core
5357
rm -rf ~/.cargo
5458
rm -rf ~/.rustup/
5559
rm -rf /usr/local/rustup/share/doc

prepare-ubuntu20.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ npm i -g cnpm --registry=https://registry.npm.taobao.org && source /etc/profile
6464
git clone -b v0.4.0 --depth=1 --single-branch https://github.com.cnpmjs.org/ThinkSpiritLab/ojcmp.git ~/ojcmp \
6565
&& cd ~/ojcmp && cargo build --release && cp target/release/ojcmp /usr/bin
6666

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

71+
git clone --depth=1 --single-branch https://github.com.cnpmjs.org/ThinkSpiritLab/Heng-Core.git ~/Heng-Core \
72+
&& cd ~/Heng-Core && make && cp ~/Heng-Core/hc /usr/bin/hc && cd ~
73+
7174
cp -r ~/.rustup/toolchains/`ls ~/.rustup/toolchains/ | grep "stable"` /usr/local/rustup && ln -s /usr/local/rustup/bin/rustc /usr/bin/rustc
7275

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

7578
cd $HCDIR && cnpm install && npm run build
76-
77-
echo -e "\033[5;31m Must see https://blog.csdn.net/SUKI547/article/details/112328873 \033[0m"

src/Config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ export class JailConfig {
5959
@IsNotEmpty()
6060
configFile!: string;
6161
}
62+
export class MeterConfig {
63+
@IsString()
64+
@IsNotEmpty()
65+
path!: string;
66+
}
6267
export class ControllerConfig {
6368
@IsString()
6469
@IsNotEmpty()
@@ -137,6 +142,10 @@ export class Config {
137142
nsjail!: JailConfig;
138143
@ValidateNested()
139144
@IsNotEmpty()
145+
@Type(() => MeterConfig)
146+
hc!: MeterConfig;
147+
@ValidateNested()
148+
@IsNotEmpty()
140149
@Type(() => JudgeFactoryConfig)
141150
judger!: JudgeFactoryConfig;
142151
}

src/SelfTest/BOMB/BULL.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,18 @@ const output = `
77
`;
88
const usrCode = `
99
#include <stdio.h>
10-
#include <unistd.h>
1110
int main(void) {
12-
for (int i = 0; i < 1000000000; i++) {
13-
write(1, "11111111111111111111111111111111111\\n", 37);
14-
}
11+
while(1) printf("1");
1512
return 0;
1613
}
1714
`;
1815

19-
export const BOMBBULL = generateNormalSelfTest(
20-
"BOMBBULL",
21-
"c",
22-
usrCode,
23-
{},
24-
[
25-
{
26-
type: "direct",
27-
input,
28-
output,
29-
expectResultType: JudgeResultKind.TimeLimitExceeded,
30-
count: false,
31-
},
32-
],
33-
2000
34-
);
16+
export const BOMBBULL = generateNormalSelfTest("BOMBBULL", "c", usrCode, {}, [
17+
{
18+
type: "direct",
19+
input,
20+
output,
21+
expectResultType: JudgeResultKind.OutpuLimitExceeded,
22+
count: false,
23+
},
24+
]);

src/SelfTest/BOMB/KILLTIMER.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { JudgeResultKind } from "heng-protocol";
2+
import { generateNormalSelfTest } from "../util";
3+
4+
const input = `
5+
`;
6+
const output = `1
7+
`;
8+
const usrCode = `
9+
#include <signal.h>
10+
11+
int main(void)
12+
{
13+
kill(1, SIGKILL); // init
14+
kill(3, SIGKILL); // timer
15+
return 0;
16+
}
17+
`;
18+
19+
export const KILLTIMER = generateNormalSelfTest(
20+
"KILLTIMER",
21+
"c",
22+
usrCode,
23+
{},
24+
[
25+
{
26+
type: "direct",
27+
input,
28+
output,
29+
expectResultType: JudgeResultKind.WrongAnswer,
30+
count: false,
31+
},
32+
],
33+
2000
34+
);

src/SelfTest/BOMB/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { BOMBCMP } from "./CMP";
44
import { BOMBCOLE } from "./COLE";
55
import { BOMBCTLE } from "./CTLE";
66
import { BOMBFORKBOMB } from "./FORKBOMB";
7+
import { KILLTIMER } from "./KILLTIMER";
78
import { BOMBSPJTLE } from "./SPJTLE";
89

910
export const BOMB = [
@@ -13,5 +14,6 @@ export const BOMB = [
1314
BOMBCMP,
1415
BOMBBADSPJ,
1516
BOMBSPJTLE,
16-
BOMBBULL
17+
BOMBBULL,
18+
KILLTIMER,
1719
];

0 commit comments

Comments
 (0)