inital commit 1
This commit is contained in:
commit
07738cacaf
76 files changed
+9477
No files matched your search
@@ -0,0 +1,3 @@
|
||||
FROM ubuntu:16.04
|
||||
RUN apt-get update && apt-get install -y cmake python python3 libseccomp-dev gcc g++
|
||||
WORKDIR /src
|
||||
@@ -0,0 +1,3 @@
|
||||
FROM ubuntu:18.04
|
||||
RUN apt-get update && apt-get install -y cmake python python3 python3-pip libseccomp-dev gcc g++ strace && pip3 install setuptools --upgrade
|
||||
WORKDIR /src
|
||||
@@ -0,0 +1,71 @@
|
||||
const fs = require("fs")
|
||||
const path = require("path")
|
||||
const execSync = require("child_process").execSync
|
||||
|
||||
var out_base_path = '/tmp/judge'
|
||||
var compile_base_path = ""
|
||||
var compile_c = function(name){
|
||||
out_path =path.join(out_base_path,'main')
|
||||
execSync(`gcc -o ${out_path} ${path.join(this.compile_base_path,name)}`)
|
||||
return out_path
|
||||
}
|
||||
|
||||
var compile_cpp = function(name){
|
||||
out_path =path.join(out_base_path,'main')
|
||||
execSync(`g++ -o ${out_path} ${path.join(this.compile_base_path,name)}`)
|
||||
return out_path
|
||||
}
|
||||
|
||||
var make_input = content =>{
|
||||
fs.writeFileSync('/tmp/judge/input',content,{'encoding':'utf-8'})
|
||||
return '/tmp/judge/input'
|
||||
}
|
||||
|
||||
var read_input = read_path => {
|
||||
return fs.readFileSync(read_path,{'encoding':'utf-8'})
|
||||
}
|
||||
|
||||
var output_path = ()=>{
|
||||
return '/tmp/judge/output'
|
||||
}
|
||||
|
||||
var error_path = ()=>{
|
||||
return '/tmp/judge/error'
|
||||
}
|
||||
|
||||
var mkdir = _path => {
|
||||
execSync(`mkdir -p ${_path}`)
|
||||
}
|
||||
|
||||
var rmdir = _path => {
|
||||
execSync(`rm -r ${_path}`)
|
||||
}
|
||||
|
||||
var baseconfig = ()=>{
|
||||
return {
|
||||
max_cpu_time:1000,
|
||||
max_real_time:8000,
|
||||
max_memory:128*1024*1024,
|
||||
max_stack:128*1024*1024,
|
||||
max_process_number:8,
|
||||
max_output_size:128*1024*1024,
|
||||
exe_path:"/dev/null",
|
||||
input_path:"/dev/null",
|
||||
output_path:"/dev/null",
|
||||
error_path:"/dev/null",
|
||||
args:[],
|
||||
env:[],
|
||||
log_path:"/dev/null",
|
||||
seccomp_rule_name:null,
|
||||
gid:0,
|
||||
uid:0,
|
||||
info:false,
|
||||
debug:false,
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
compile_c,compile_cpp,make_input,read_input,mkdir,rmdir,baseconfig,compile_base_path,
|
||||
output_path,
|
||||
error_path
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
const assert = require("assert")
|
||||
const path = require("path")
|
||||
var judger = require("../../bindings/NodeJS")
|
||||
var base = require('./base.js')
|
||||
var signal = require("os").constants.signals
|
||||
|
||||
|
||||
var compile_base_path = base.compile_base_path = path.join(__dirname,"../test_src","integration")
|
||||
|
||||
|
||||
|
||||
describe('#常用测试',function(){
|
||||
|
||||
|
||||
before( ()=>{
|
||||
//mkdir /tmp/judge
|
||||
base.mkdir('/tmp/judge')
|
||||
})
|
||||
after( ()=>{
|
||||
base.rmdir('/tmp/judge')
|
||||
})
|
||||
|
||||
it('args must be a list 1',()=>{
|
||||
assert.throws(()=>judger.run({args:{},output_path:"/dev/null"}),/args must be a list$/)
|
||||
})
|
||||
|
||||
it('args must be a list 2',()=>{
|
||||
assert.throws(()=>judger.run({args:"1234",output_path:"/dev/null"}),/args must be a list$/)
|
||||
})
|
||||
|
||||
it('test_env_must_be_list',()=>{
|
||||
assert.throws(()=>judger.run({env:"1234",output_path:"/dev/null"}),/env must be a list$/)
|
||||
})
|
||||
it('test_seccomp_rule_can_be_none',()=>{
|
||||
let ret = judger.run({ output_path:"/dev/null",exe_path:"/bin/ls",args:["/"],env:["a=b"] })
|
||||
assert.strictEqual(ret.result,0)
|
||||
})
|
||||
|
||||
it('test normal.c',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["exe_path"] = base.compile_c("normal.c")
|
||||
config["input_path"] = base.make_input("judger_test")
|
||||
config["output_path"] = config["error_path"] = base.output_path()
|
||||
let ret = judger.run(config)
|
||||
let output = "judger_test\nHello world"
|
||||
let content = base.read_input( base.output_path())
|
||||
assert.strictEqual(content,output)
|
||||
})
|
||||
|
||||
it('test math.c' ,()=>{
|
||||
let config = base.baseconfig()
|
||||
config["exe_path"] = base.compile_c("math.c")
|
||||
config["output_path"] = config["error_path"] = base.output_path()
|
||||
let ret = judger.run(config)
|
||||
assert.strictEqual(ret.result,judger.RESULT_SUCCESS)
|
||||
let content = base.read_input( base.output_path())
|
||||
assert.strictEqual(content,"abs 1024")
|
||||
})
|
||||
|
||||
it('test_args',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["exe_path"] = base.compile_c("args.c")
|
||||
config["args"] = ["test", "hehe", "000"]
|
||||
config["output_path"] = config["error_path"] = base.output_path()
|
||||
|
||||
let result = judger.run(config)
|
||||
let output = "argv[0]: /tmp/judge/main\nargv[1]: test\nargv[2]: hehe\nargv[3]: 000\n"
|
||||
assert.strictEqual(result.result, judger.RESULT_SUCCESS)
|
||||
assert.strictEqual(output, base.read_input(config["output_path"]))
|
||||
|
||||
})
|
||||
|
||||
it('test env',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["exe_path"] = base.compile_c("env.c")
|
||||
config["output_path"] = config["error_path"] = base.output_path()
|
||||
config["env"] = ["env=judger_test","test=judger"]
|
||||
let result = judger.run(config)
|
||||
let output = "judger_test\njudger\n"
|
||||
assert.strictEqual(result["result"], judger.RESULT_SUCCESS)
|
||||
assert.strictEqual(output, base.read_input(config["output_path"]))
|
||||
|
||||
})
|
||||
|
||||
it('test real time',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["exe_path"] = base.compile_c("sleep.c")
|
||||
config["max_real_time"] = 3
|
||||
let result = judger.run(config)
|
||||
//assert.strictEqual(result["result"], judger.RESULT_REAL_TIME_LIMIT_EXCEEDED)
|
||||
assert.strictEqual(result["signal"], signal.SIGKILL)
|
||||
assert.ok(result["real_time"] >= config["max_real_time"])
|
||||
|
||||
})
|
||||
|
||||
it('test cpu time',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["exe_path"] = base.compile_c("while1.c")
|
||||
let result= judger.run(config)
|
||||
|
||||
assert.strictEqual(result["result"], judger.RESULT_CPU_TIME_LIMIT_EXCEEDED)
|
||||
assert.strictEqual(result["signal"], signal.SIGKILL)
|
||||
assert.ok(result["cpu_time"] >= config["max_cpu_time"])
|
||||
|
||||
})
|
||||
|
||||
it('test memory1',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["max_memory"] = 64 * 1024 * 1024
|
||||
config["exe_path"] = base.compile_c("memory1.c")
|
||||
let result= judger.run(config)
|
||||
//# malloc succeeded
|
||||
assert.ok(result["memory"] > 80 * 1024 * 1024)
|
||||
assert.strictEqual(result["result"], judger.RESULT_MEMORY_LIMIT_EXCEEDED)
|
||||
})
|
||||
|
||||
it('test memory2',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["max_memory"] = 64 * 1024 * 1024
|
||||
config["exe_path"] = base.compile_c("memory2.c")
|
||||
let result= judger.run(config)
|
||||
//# malloc failed, return 1
|
||||
assert.strictEqual(result["exit_code"], 1)
|
||||
//# malloc failed, so it should use a little memory
|
||||
assert.ok(result["memory"] < 12 * 1024 * 1024)
|
||||
assert.strictEqual(result["result"], judger.RESULT_RUNTIME_ERROR)
|
||||
})
|
||||
|
||||
it('test memory3',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["max_memory"] = 512 * 1024 * 1024
|
||||
config["exe_path"] = base.compile_c("memory3.c")
|
||||
let result= judger.run(config)
|
||||
assert.strictEqual(result["result"], judger.RESULT_SUCCESS)
|
||||
assert.ok(result["memory"] >= 102400000 * 4)
|
||||
})
|
||||
it('test re1',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["exe_path"] = base.compile_c("re1.c")
|
||||
let result= judger.run(config)
|
||||
//# re1.c return 25
|
||||
assert.strictEqual(result["exit_code"], 25)
|
||||
|
||||
})
|
||||
it('test re2',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["exe_path"] = base.compile_c("re2.c")
|
||||
let result= judger.run(config)
|
||||
assert.strictEqual(result["result"], judger.RESULT_RUNTIME_ERROR)
|
||||
assert.strictEqual(result["signal"], signal.SIGSEGV)
|
||||
})
|
||||
it('test child proc cpu time limit',()=>{
|
||||
let config = base.baseconfig()
|
||||
|
||||
config["exe_path"] = base.compile_c("child_proc_cpu_time_limit.c")
|
||||
let result= judger.run(config)
|
||||
assert.strictEqual(result["result"], judger.RESULT_CPU_TIME_LIMIT_EXCEEDED)
|
||||
|
||||
})
|
||||
it('test child proc real time limit',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["exe_path"] = base.compile_c("child_proc_real_time_limit.c")
|
||||
config["max_real_time"] = 3
|
||||
let result= judger.run(config)
|
||||
assert.strictEqual(result["result"], judger.RESULT_REAL_TIME_LIMIT_EXCEEDED)
|
||||
assert.strictEqual(result["signal"], signal.SIGKILL)
|
||||
})
|
||||
it('test stdout and stderr',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["exe_path"] = base.compile_c("stdout_stderr.c")
|
||||
config["output_path"] = config["error_path"] = base.output_path()
|
||||
let result= judger.run(config)
|
||||
assert.strictEqual(result["result"], judger.RESULT_SUCCESS)
|
||||
let output = "stderr\n+++++++++++++++\n--------------\nstdout\n"
|
||||
assert.strictEqual(output, base.read_input(config["output_path"]))
|
||||
})
|
||||
it('test uid and gid',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["exe_path"] = base.compile_c("uid_gid.c")
|
||||
config["output_path"] = config["error_path"] = base.output_path()
|
||||
config["uid"] = 65534
|
||||
config["gid"] = 65534
|
||||
let result= judger.run(config)
|
||||
assert.strictEqual(result["result"], judger.RESULT_SUCCESS)
|
||||
output = "uid=65534(nobody) gid=65534(nogroup) groups=65534(nogroup)\nuid 65534\ngid 65534\n"
|
||||
assert.strictEqual(output, base.read_input(config["output_path"]))
|
||||
|
||||
})
|
||||
it('test gcc random',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["max_memory"] = judger.UNLIMITED
|
||||
config["exe_path"] = "/usr/bin/gcc"
|
||||
config["args"] = [path.join(compile_base_path,'gcc_random.c'),
|
||||
"-o", '/tmp/judge/gcc_random']
|
||||
let result= judger.run(config)
|
||||
assert.ok(result["real_time"] >= 2000)
|
||||
|
||||
})
|
||||
it('test cpp meta',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["exe_path"] = "/usr/bin/g++"
|
||||
config["max_memory"] = 1024 * 1024 * 1024
|
||||
config["args"] = [path.join(compile_base_path,'cpp_meta.cpp'),
|
||||
"-o",'/tmp/judge/cpp_meta']
|
||||
let result= judger.run(config)
|
||||
assert.strictEqual(result["result"], judger.RESULT_CPU_TIME_LIMIT_EXCEEDED)
|
||||
assert.ok(result["cpu_time"] > 1500)
|
||||
assert.ok(result["real_time"] >= 2000)
|
||||
})
|
||||
it('test output size',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["exe_path"] = base.compile_c("output_size.c")
|
||||
config["output_path"] = base.output_path()
|
||||
config["max_output_size"] = 1000 * 10
|
||||
let result= judger.run(config)
|
||||
assert.strictEqual(result["result"], judger.RESULT_RUNTIME_ERROR)
|
||||
assert.ok(base.read_input("/tmp/judge/fsize_test").length <= 1000*10)
|
||||
})
|
||||
it('test stack size',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["max_memory"] = 256 * 1024 * 1024
|
||||
config["exe_path"] = base.compile_c("stack.c")
|
||||
config["max_stack"] = 16 * 1024 * 1024
|
||||
config["output_path"] = config["error_path"] = base.output_path()
|
||||
|
||||
let result= judger.run(config)
|
||||
assert.strictEqual(result["result"], judger.RESULT_RUNTIME_ERROR)
|
||||
|
||||
config["max_stack"] = 128 * 1024 * 1024
|
||||
result= judger.run(config).result
|
||||
assert.strictEqual(result["result"], judger.RESULT_SUCCESS)
|
||||
assert.strictEqual("big stack", base.read_input(config["output_path"]))
|
||||
|
||||
})
|
||||
it('test writev',()=>{
|
||||
let config = base.baseconfig()
|
||||
config["exe_path"] = base.compile_cpp("writev.cpp")
|
||||
config["seccomp_rule_name"] = "c_cpp"
|
||||
config["input_path"] = base.make_input("111" * 10000 + "\n")
|
||||
config["output_path"] = config["error_path"] = base.output_path()
|
||||
|
||||
let result= judger.run(config)
|
||||
assert.strictEqual(result["result"], judger.RESULT_SUCCESS)
|
||||
})
|
||||
})
|
||||
Whitespace-only changes.
@@ -0,0 +1,13 @@
|
||||
# coding=utf-8
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import _judger
|
||||
from unittest import TestCase, main
|
||||
|
||||
from testcase.integration.test import IntegrationTest
|
||||
from testcase.seccomp.test import SeccompTest
|
||||
|
||||
ver = _judger.VERSION
|
||||
print("Judger version %d.%d.%d" % ((ver >> 16) & 0xff, (ver >> 8) & 0xff, ver & 0xff))
|
||||
print(sys.version)
|
||||
main()
|
||||
Whitespace-only changes.
@@ -0,0 +1,84 @@
|
||||
# coding=utf-8
|
||||
from __future__ import print_function
|
||||
import os
|
||||
import random
|
||||
import shutil
|
||||
import copy
|
||||
from unittest import TestCase
|
||||
|
||||
|
||||
class RunResult(object):
|
||||
cpu_time_limited = 1
|
||||
real_time_limit_exceeded = 2
|
||||
memory_limit_exceeded = 3
|
||||
runtime_error = 4
|
||||
system_error = 5
|
||||
|
||||
|
||||
class BaseTestCase(TestCase):
|
||||
BAD_SYSTEM_CALL = 31
|
||||
|
||||
def init_workspace(self, language):
|
||||
base_workspace = "/tmp"
|
||||
workspace = os.path.join(base_workspace, language)
|
||||
shutil.rmtree(workspace, ignore_errors=True)
|
||||
os.makedirs(workspace)
|
||||
return workspace
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.workspace, ignore_errors=True)
|
||||
|
||||
def rand_str(self):
|
||||
return "".join([random.choice("123456789abcdef") for _ in range(12)])
|
||||
|
||||
def _compile_c(self, src_name, extra_flags=None):
|
||||
path = os.path.dirname(os.path.abspath(__file__))
|
||||
exe_path = os.path.join(self.workspace, src_name.split("/")[-1].split(".")[0])
|
||||
flags = " "
|
||||
if extra_flags:
|
||||
flags += " ".join(extra_flags)
|
||||
cmd = ("gcc {0} -g -O0 -static -o {1}" + flags).format(os.path.join(path, src_name), exe_path)
|
||||
if os.system(cmd):
|
||||
raise AssertionError("compile error, cmd: {0}".format(cmd))
|
||||
return exe_path
|
||||
|
||||
def _compile_cpp(self, src_name):
|
||||
path = os.path.dirname(os.path.abspath(__file__))
|
||||
exe_path = os.path.join(self.workspace, src_name.split("/")[-1].split(".")[0])
|
||||
cmd = "g++ {0} -g -O0 -o {1}".format(os.path.join(path, src_name), exe_path)
|
||||
if os.system(cmd):
|
||||
raise AssertionError("compile error, cmd: {0}".format(cmd))
|
||||
return exe_path
|
||||
|
||||
def make_input(self, content):
|
||||
path = os.path.join(self.workspace, self.rand_str())
|
||||
with open(path, "w") as f:
|
||||
f.write(content)
|
||||
return path
|
||||
|
||||
def output_path(self):
|
||||
return os.path.join(self.workspace, self.rand_str())
|
||||
|
||||
def output_content(self, path):
|
||||
with open(path, "r") as f:
|
||||
return f.read()
|
||||
|
||||
@property
|
||||
def base_config(self):
|
||||
config = {"max_cpu_time": 1000,
|
||||
"max_real_time": 3000,
|
||||
"max_memory": 128 * 1024 * 1024,
|
||||
"max_stack": 32 * 1024 * 1024,
|
||||
"max_process_number": 10,
|
||||
"max_output_size": 1024 * 1024,
|
||||
"exe_path": "/bin/ls",
|
||||
"input_path": "/dev/null",
|
||||
"output_path": "/dev/null",
|
||||
"error_path": "/dev/null",
|
||||
"args": [],
|
||||
"env": ["env=judger_test", "test=judger"],
|
||||
"log_path": "judger_test.log",
|
||||
"seccomp_rule_name": None,
|
||||
"uid": 0,
|
||||
"gid": 0}
|
||||
return config
|
||||
Whitespace-only changes.
@@ -0,0 +1,337 @@
|
||||
# coding=utf-8
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import signal
|
||||
import os
|
||||
import resource
|
||||
import _judger
|
||||
|
||||
from .. import base
|
||||
|
||||
|
||||
class IntegrationTest(base.BaseTestCase):
|
||||
def setUp(self):
|
||||
print("Running", self._testMethodName)
|
||||
self.workspace = self.init_workspace("integration")
|
||||
|
||||
def _compile_c(self, src_name, extra_flags=None):
|
||||
return super(IntegrationTest, self)._compile_c("../../test_src/integration/" + src_name, extra_flags)
|
||||
|
||||
def _compile_cpp(self, src_name):
|
||||
return super(IntegrationTest, self)._compile_cpp("../../test_src/integration/" + src_name)
|
||||
|
||||
def test_args_must_be_list(self):
|
||||
with self.assertRaisesRegexp(ValueError, "args must be a list"):
|
||||
_judger.run(max_cpu_time=1000, max_real_time=2000,
|
||||
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
|
||||
max_process_number=200, max_output_size=10000, exe_path="1.out",
|
||||
input_path="1.in", output_path="1.out", error_path="1.out",
|
||||
args="12344", env=["a=b"], log_path="1.log",
|
||||
seccomp_rule_name="1.so", uid=0, gid=0)
|
||||
|
||||
with self.assertRaisesRegexp(ValueError, "args must be a list"):
|
||||
_judger.run(max_cpu_time=1000, max_real_time=2000,
|
||||
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
|
||||
max_process_number=200, max_output_size=10000, exe_path="1.out",
|
||||
input_path="1.in", output_path="1.out", error_path="1.out",
|
||||
args={"k": "v"}, env=["a=b"], log_path="1.log",
|
||||
seccomp_rule_name="1.so", uid=0, gid=0)
|
||||
|
||||
def test_args_item_must_be_string(self):
|
||||
with self.assertRaisesRegexp(ValueError, "args item must be a string"):
|
||||
_judger.run(max_cpu_time=1000, max_real_time=2000,
|
||||
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
|
||||
max_process_number=200, max_output_size=10000, exe_path="1.out",
|
||||
input_path="1.in", output_path="1.out", error_path="1.out",
|
||||
args=["1234", 1234], env=["a=b"], log_path="1.log",
|
||||
seccomp_rule_name="1.so", uid=0, gid=0)
|
||||
|
||||
with self.assertRaisesRegexp(ValueError, "args item must be a string"):
|
||||
_judger.run(max_cpu_time=1000, max_real_time=2000,
|
||||
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
|
||||
max_process_number=200, max_output_size=10000, exe_path="1.out",
|
||||
input_path="1.in", output_path="1.out", error_path="1.out",
|
||||
args=["1234", None], env=["a=b"], log_path="1.log",
|
||||
seccomp_rule_name="1.so", uid=0, gid=0)
|
||||
|
||||
if sys.version_info >= (3, 5):
|
||||
args = ["哈哈哈".encode("utf-8")]
|
||||
else:
|
||||
args = [u"哈哈哈"]
|
||||
with self.assertRaisesRegexp(ValueError, "args item must be a string"):
|
||||
_judger.run(max_cpu_time=1000, max_real_time=2000,
|
||||
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
|
||||
max_process_number=200, max_output_size=10000, exe_path="1.out",
|
||||
input_path="1.in", output_path="1.out", error_path="1.out",
|
||||
args=args, env=["a="], log_path="1.log",
|
||||
seccomp_rule_name="1.so", uid=0, gid=0)
|
||||
|
||||
def test_env_must_be_list(self):
|
||||
with self.assertRaisesRegexp(ValueError, "env must be a list"):
|
||||
_judger.run(max_cpu_time=1000, max_real_time=2000,
|
||||
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
|
||||
max_process_number=200, max_output_size=10000, exe_path="1.out",
|
||||
input_path="1.in", output_path="1.out", error_path="1.out",
|
||||
args=["1234"], env="1234", log_path="1.log",
|
||||
seccomp_rule_name="1.so", uid=0, gid=0)
|
||||
|
||||
with self.assertRaisesRegexp(ValueError, "env must be a list"):
|
||||
_judger.run(max_cpu_time=1000, max_real_time=2000,
|
||||
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
|
||||
max_process_number=200, max_output_size=10000, exe_path="1.out",
|
||||
input_path="1.in", output_path="1.out", error_path="1.out",
|
||||
args=["1234"], env={"k": "v"}, log_path="1.log",
|
||||
seccomp_rule_name="1.so", uid=0, gid=0)
|
||||
|
||||
def test_env_item_must_be_string(self):
|
||||
with self.assertRaisesRegexp(ValueError, "env item must be a string"):
|
||||
_judger.run(max_cpu_time=1000, max_real_time=2000,
|
||||
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
|
||||
max_process_number=200, max_output_size=10000, exe_path="1.out",
|
||||
input_path="1.in", output_path="1.out", error_path="1.out",
|
||||
args=["1234"], env=["1234", 1234], log_path="1.log",
|
||||
seccomp_rule_name="1.so", uid=0, gid=0)
|
||||
|
||||
with self.assertRaisesRegexp(ValueError, "env item must be a string"):
|
||||
_judger.run(max_cpu_time=1000, max_real_time=2000,
|
||||
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
|
||||
max_process_number=200, max_output_size=10000, exe_path="1.out",
|
||||
input_path="1.in", output_path="1.out", error_path="1.out",
|
||||
args=["1234"], env=["a=b", None], log_path="1.log",
|
||||
seccomp_rule_name="1.so", uid=0, gid=0)
|
||||
|
||||
if sys.version_info >= (3, 5):
|
||||
env = ["哈哈哈".encode("utf-8")]
|
||||
else:
|
||||
env = [u"哈哈哈"]
|
||||
with self.assertRaisesRegexp(ValueError, "env item must be a string"):
|
||||
_judger.run(max_cpu_time=1000, max_real_time=2000,
|
||||
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
|
||||
max_process_number=200, max_output_size=10000, exe_path="1.out",
|
||||
input_path="1.in", output_path="1.out", error_path="1.out",
|
||||
args=["1234"], env=env, log_path="1.log",
|
||||
seccomp_rule_name="1.so", uid=0, gid=0)
|
||||
|
||||
def test_seccomp_rule_can_be_none(self):
|
||||
_judger.run(max_cpu_time=1000, max_real_time=2000,
|
||||
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
|
||||
max_process_number=200, max_output_size=10000, exe_path="/bin/ls",
|
||||
input_path="/dev/null", output_path="/dev/null", error_path="/dev/null",
|
||||
args=["12344"], env=["a=b"], log_path="/dev/null",
|
||||
seccomp_rule_name="c_cpp", uid=0, gid=0)
|
||||
|
||||
_judger.run(max_cpu_time=1000, max_real_time=2000,
|
||||
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
|
||||
max_process_number=200, max_output_size=10000, exe_path="/bin/ls",
|
||||
input_path="/dev/null", output_path="/dev/null", error_path="/dev/null",
|
||||
args=["12344"], env=["a=b"], log_path="/dev/null",
|
||||
seccomp_rule_name=None, uid=0, gid=0)
|
||||
|
||||
def test_normal(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("normal.c")
|
||||
config["input_path"] = self.make_input("judger_test")
|
||||
config["output_path"] = config["error_path"] = self.output_path()
|
||||
result = _judger.run(**config)
|
||||
output = "judger_test\nHello world"
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
self.assertEqual(output, self.output_content(config["output_path"]))
|
||||
|
||||
config["exe_path"] = self._compile_c("math.c")
|
||||
config["input_path"] = "/dev/null"
|
||||
config["output_path"] = config["error_path"] = self.output_path()
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
self.assertEqual("abs 1024", self.output_content(config["output_path"]))
|
||||
|
||||
def test_args(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("args.c")
|
||||
config["args"] = ["test", "hehe", "000"]
|
||||
config["output_path"] = config["error_path"] = self.output_path()
|
||||
result = _judger.run(**config)
|
||||
output = "argv[0]: /tmp/integration/args\nargv[1]: test\nargv[2]: hehe\nargv[3]: 000\n"
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
self.assertEqual(output, self.output_content(config["output_path"]))
|
||||
|
||||
def test_env(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("env.c")
|
||||
config["output_path"] = config["error_path"] = self.output_path()
|
||||
result = _judger.run(**config)
|
||||
output = "judger_test\njudger\n"
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
self.assertEqual(output, self.output_content(config["output_path"]))
|
||||
|
||||
def test_real_time(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("sleep.c")
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_REAL_TIME_LIMIT_EXCEEDED)
|
||||
self.assertEqual(result["signal"], signal.SIGKILL)
|
||||
self.assertTrue(result["real_time"] >= config["max_real_time"])
|
||||
|
||||
def test_cpu_time(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("while1.c")
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_CPU_TIME_LIMIT_EXCEEDED)
|
||||
self.assertEqual(result["signal"], signal.SIGKILL)
|
||||
self.assertTrue(result["cpu_time"] >= config["max_cpu_time"])
|
||||
|
||||
def test_memory1(self):
|
||||
config = self.base_config
|
||||
config["max_memory"] = 64 * 1024 * 1024
|
||||
config["exe_path"] = self._compile_c("memory1.c")
|
||||
result = _judger.run(**config)
|
||||
# malloc succeeded
|
||||
self.assertTrue(result["memory"] > 80 * 1024 * 1024)
|
||||
self.assertEqual(result["result"], _judger.RESULT_MEMORY_LIMIT_EXCEEDED)
|
||||
|
||||
def test_memory2(self):
|
||||
config = self.base_config
|
||||
config["max_memory"] = 64 * 1024 * 1024
|
||||
config["exe_path"] = self._compile_c("memory2.c")
|
||||
result = _judger.run(**config)
|
||||
# malloc failed, return 1
|
||||
self.assertEqual(result["exit_code"], 1)
|
||||
# malloc failed, so it should use a little memory
|
||||
self.assertTrue(result["memory"] < 12 * 1024 * 1024)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
|
||||
def test_memory3(self):
|
||||
config = self.base_config
|
||||
config["max_memory"] = 512 * 1024 * 1024
|
||||
config["exe_path"] = self._compile_c("memory3.c")
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
self.assertTrue(result["memory"] >= 102400000 * 4)
|
||||
|
||||
def test_memory4(self):
|
||||
"""parent process memory should not affect child process"""
|
||||
a = ["test" for i in range(2000000)]
|
||||
# get self maxrss
|
||||
max_rss = resource.getrusage(resource.RUSAGE_SELF)[2]
|
||||
self.assertTrue(max_rss > 28000)
|
||||
result = _judger.run(**self.base_config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
self.assertTrue(result["memory"] < 3000000)
|
||||
del a
|
||||
|
||||
def test_memory_limit_check_only(self):
|
||||
config = self.base_config
|
||||
config["memory_limit_check_only"] = 1
|
||||
config["max_memory"] = 64 * 1024 * 1024
|
||||
config["exe_path"] = self._compile_c("memory2.c")
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["exit_code"], 0)
|
||||
self.assertTrue(result["memory"] > 256 * 1024 * 1024)
|
||||
self.assertEqual(result["result"], _judger.RESULT_MEMORY_LIMIT_EXCEEDED)
|
||||
|
||||
def test_re1(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("re1.c")
|
||||
result = _judger.run(**config)
|
||||
# re1.c return 25
|
||||
self.assertEqual(result["exit_code"], 25)
|
||||
|
||||
def test_re2(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("re2.c")
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
self.assertEqual(result["signal"], signal.SIGSEGV)
|
||||
|
||||
def test_child_proc_cpu_time_limit(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("child_proc_cpu_time_limit.c")
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_CPU_TIME_LIMIT_EXCEEDED)
|
||||
|
||||
def test_child_proc_real_time_limit(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("child_proc_real_time_limit.c")
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_REAL_TIME_LIMIT_EXCEEDED)
|
||||
self.assertEqual(result["signal"], signal.SIGKILL)
|
||||
|
||||
def test_stdout_and_stderr(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("stdout_stderr.c")
|
||||
config["output_path"] = config["error_path"] = self.output_path()
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
output = "stderr\n+++++++++++++++\n--------------\nstdout\n"
|
||||
self.assertEqual(output, self.output_content(config["output_path"]))
|
||||
|
||||
def test_uid_and_gid(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("uid_gid.c")
|
||||
config["output_path"] = config["error_path"] = self.output_path()
|
||||
config["uid"] = 65534
|
||||
config["gid"] = 65534
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
output = "uid=65534(nobody) gid=65534(nogroup) groups=65534(nogroup)\nuid 65534\ngid 65534\n"
|
||||
self.assertEqual(output, self.output_content(config["output_path"]))
|
||||
|
||||
def test_gcc_random(self):
|
||||
config = self.base_config
|
||||
config["max_memory"] = _judger.UNLIMITED
|
||||
config["exe_path"] = "/usr/bin/gcc"
|
||||
config["args"] = ["../test_src/integration/gcc_random.c",
|
||||
"-o", os.path.join(self.workspace, "gcc_random")]
|
||||
result = _judger.run(**config)
|
||||
self.assertTrue(result["real_time"] >= 2000)
|
||||
|
||||
def test_cpp_meta(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = "/usr/bin/g++"
|
||||
config["max_memory"] = 1024 * 1024 * 1024
|
||||
config["args"] = ["../test_src/integration/cpp_meta.cpp",
|
||||
"-o", os.path.join(self.workspace, "cpp_meta")]
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_CPU_TIME_LIMIT_EXCEEDED)
|
||||
self.assertTrue(result["cpu_time"] > 1500)
|
||||
self.assertTrue(result["real_time"] >= 2000)
|
||||
|
||||
def test_output_size(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("output_size.c")
|
||||
config["max_output_size"] = 1000 * 10
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
with open("/tmp/fsize_test", "r") as f:
|
||||
self.assertEqual(len(f.read()), 10000)
|
||||
|
||||
def test_stack_size(self):
|
||||
config = self.base_config
|
||||
config["max_memory"] = 256 * 1024 * 1024
|
||||
config["exe_path"] = self._compile_c("stack.c")
|
||||
config["output_path"] = config["error_path"] = self.output_path()
|
||||
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
|
||||
config["max_stack"] = 128 * 1024 * 1024
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
self.assertEqual("big stack", self.output_content(config["output_path"]))
|
||||
|
||||
def test_writev(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_cpp("writev.cpp")
|
||||
config["seccomp_rule_name"] = "c_cpp"
|
||||
config["input_path"] = self.make_input("111" * 10000 + "\n")
|
||||
config["output_path"] = config["error_path"] = self.output_path()
|
||||
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
|
||||
def test_get_time(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("time.c")
|
||||
config["seccomp_rule_name"] = "c_cpp"
|
||||
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
Whitespace-only changes.
@@ -0,0 +1,174 @@
|
||||
# coding=utf-8
|
||||
from __future__ import print_function
|
||||
import _judger
|
||||
import signal
|
||||
import shutil
|
||||
import os
|
||||
|
||||
from .. import base
|
||||
|
||||
|
||||
class SeccompTest(base.BaseTestCase):
|
||||
def setUp(self):
|
||||
print("Running", self._testMethodName)
|
||||
self.workspace = self.init_workspace("integration")
|
||||
|
||||
def _compile_c(self, src_name, extra_flags=None):
|
||||
return super(SeccompTest, self)._compile_c("../../test_src/seccomp/" + src_name, extra_flags)
|
||||
|
||||
def test_fork(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("fork.c")
|
||||
config["output_path"] = config["error_path"] = self.output_path()
|
||||
result = _judger.run(**config)
|
||||
|
||||
# without seccomp
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
|
||||
# with general seccomp
|
||||
config["seccomp_rule_name"] = "general"
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
self.assertEqual(result["signal"], self.BAD_SYSTEM_CALL)
|
||||
|
||||
# with c_cpp seccomp
|
||||
config["seccomp_rule_name"] = "c_cpp"
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
self.assertEqual(result["signal"], self.BAD_SYSTEM_CALL)
|
||||
|
||||
def test_execve(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("execve.c")
|
||||
config["output_path"] = config["error_path"] = self.output_path()
|
||||
result = _judger.run(**config)
|
||||
# without seccomp
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
self.assertEqual("Helloworld\n", self.output_content(config["output_path"]))
|
||||
|
||||
# with general seccomp
|
||||
config["seccomp_rule_name"] = "general"
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
self.assertEqual(result["signal"], self.BAD_SYSTEM_CALL)
|
||||
|
||||
# with c_cpp seccomp
|
||||
config["seccomp_rule_name"] = "c_cpp"
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
self.assertEqual(result["signal"], self.BAD_SYSTEM_CALL)
|
||||
|
||||
def test_write_file_using_open(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("write_file_open.c")
|
||||
config["output_path"] = config["error_path"] = self.output_path()
|
||||
path = os.path.join(self.workspace, "file1.txt")
|
||||
config["args"] = [path, "w"]
|
||||
result = _judger.run(**config)
|
||||
# without seccomp
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
self.assertEqual("", self.output_content(path))
|
||||
|
||||
# with general seccomp
|
||||
config["seccomp_rule_name"] = "general"
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
self.assertEqual(result["signal"], self.BAD_SYSTEM_CALL)
|
||||
|
||||
# with c_cpp seccomp
|
||||
config["seccomp_rule_name"] = "c_cpp"
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
self.assertEqual(result["signal"], self.BAD_SYSTEM_CALL)
|
||||
|
||||
def test_read_write_file_using_open(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("write_file_open.c")
|
||||
config["output_path"] = config["error_path"] = self.output_path()
|
||||
path = os.path.join(self.workspace, "file2.txt")
|
||||
config["args"] = [path, "w+"]
|
||||
result = _judger.run(**config)
|
||||
# without seccomp
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
self.assertEqual("", self.output_content(path))
|
||||
|
||||
# with general seccomp
|
||||
config["seccomp_rule_name"] = "general"
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
self.assertEqual(result["signal"], self.BAD_SYSTEM_CALL)
|
||||
|
||||
# with c_cpp seccomp
|
||||
config["seccomp_rule_name"] = "c_cpp"
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
self.assertEqual(result["signal"], self.BAD_SYSTEM_CALL)
|
||||
|
||||
def test_write_file_using_openat(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("write_file_openat.c")
|
||||
config["output_path"] = config["error_path"] = self.output_path()
|
||||
path = os.path.join(self.workspace, "file3.txt")
|
||||
config["args"] = [path, "w"]
|
||||
result = _judger.run(**config)
|
||||
# without seccomp
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
self.assertEqual("", self.output_content(path))
|
||||
|
||||
# with general seccomp
|
||||
config["seccomp_rule_name"] = "general"
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
self.assertEqual(result["signal"], self.BAD_SYSTEM_CALL)
|
||||
|
||||
# with c_cpp seccomp
|
||||
config["seccomp_rule_name"] = "c_cpp"
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
self.assertEqual(result["signal"], self.BAD_SYSTEM_CALL)
|
||||
|
||||
def test_read_write_file_using_openat(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("write_file_openat.c")
|
||||
config["output_path"] = config["error_path"] = self.output_path()
|
||||
path = os.path.join(self.workspace, "file4.txt")
|
||||
config["args"] = [path, "w+"]
|
||||
result = _judger.run(**config)
|
||||
# without seccomp
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
self.assertEqual("", self.output_content(path))
|
||||
|
||||
# with general seccomp
|
||||
config["seccomp_rule_name"] = "general"
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
self.assertEqual(result["signal"], self.BAD_SYSTEM_CALL)
|
||||
|
||||
# with c_cpp seccomp
|
||||
config["seccomp_rule_name"] = "c_cpp"
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
self.assertEqual(result["signal"], self.BAD_SYSTEM_CALL)
|
||||
|
||||
def test_sysinfo(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("sysinfo.c")
|
||||
result = _judger.run(**config)
|
||||
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
|
||||
def test_exceveat(self):
|
||||
config = self.base_config
|
||||
config["exe_path"] = self._compile_c("execveat.c")
|
||||
config["output_path"] = config["error_path"] = self.output_path()
|
||||
result = _judger.run(**config)
|
||||
if "syscall not found" in self.output_content(config["output_path"]):
|
||||
print("execveat syscall not found, test ignored")
|
||||
return
|
||||
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
|
||||
|
||||
# with general seccomp
|
||||
config["seccomp_rule_name"] = "general"
|
||||
result = _judger.run(**config)
|
||||
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
|
||||
self.assertEqual(result["signal"], self.BAD_SYSTEM_CALL)
|
||||
@@ -0,0 +1,16 @@
|
||||
#! /bin/bash
|
||||
set -ex
|
||||
dir=$PWD
|
||||
python -V
|
||||
gcc -v
|
||||
g++ -v
|
||||
|
||||
for py in python2 python3; do
|
||||
cd $dir
|
||||
rm -rf build && mkdir build && cd build && cmake ..
|
||||
make
|
||||
make install
|
||||
cd ../bindings/Python && rm -rf build
|
||||
$py setup.py install
|
||||
cd ../../tests/Python_and_core && $py test.py
|
||||
done
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int j;
|
||||
for (j = 0; j < argc; j++)
|
||||
printf("argv[%d]: %s\n", j, argv[j]);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/resource.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int status;
|
||||
int pid;
|
||||
|
||||
if ((pid = fork()) < 0) {
|
||||
perror("fork error");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (pid == 0) {
|
||||
while (1) {};
|
||||
}
|
||||
else {
|
||||
struct rusage resource_usage;
|
||||
if (wait4(pid, &status, 0, &resource_usage) == -1) {
|
||||
perror("wait4 error!");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/resource.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int status;
|
||||
int pid;
|
||||
|
||||
if ((pid = fork()) < 0) {
|
||||
perror("fork error");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (pid == 0) {
|
||||
sleep(10);
|
||||
}
|
||||
else {
|
||||
struct rusage resource_usage;
|
||||
if (wait4(pid, &status, 0, &resource_usage) == -1) {
|
||||
perror("wait4 error!");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
std::vector<int> Primes;
|
||||
|
||||
template <int toTest, int factor> // factor should be odd
|
||||
class IsPrime
|
||||
{
|
||||
public:
|
||||
enum {
|
||||
result = ( toTest == 2 )
|
||||
|| toTest % factor
|
||||
&& IsPrime < toTest , factor - 2 >::result
|
||||
};
|
||||
};
|
||||
|
||||
template<int toTest>
|
||||
class IsPrime<toTest, 1>
|
||||
{
|
||||
public:
|
||||
enum {result = ( toTest == 2 ) || ( toTest & 1 ) };
|
||||
};
|
||||
|
||||
template <int upperBound> // upperBound should be odd or 2
|
||||
class PrimePick : public PrimePick < upperBound - 2 >
|
||||
{
|
||||
public:
|
||||
enum {
|
||||
isPrime = IsPrime < upperBound, ( upperBound >> 1 ) | 1 >::result
|
||||
};
|
||||
PrimePick<upperBound>() {
|
||||
if ( isPrime )
|
||||
Primes.push_back ( upperBound );
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
class PrimePick<2>
|
||||
{
|
||||
public:
|
||||
PrimePick<2>() {
|
||||
Primes.push_back ( 2 );
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
class PrimePick<1> : public PrimePick<2> {};
|
||||
|
||||
template <int upperBound> // upperBound should be odd or 2
|
||||
class Primxx : public Primxx < upperBound - 2 >
|
||||
{
|
||||
public:
|
||||
enum {
|
||||
isPrime = IsPrime < upperBound, ( upperBound >> 1 ) | 1 >::result
|
||||
};
|
||||
Primxx<upperBound>() {
|
||||
if ( isPrime )
|
||||
Primes.push_back ( upperBound );
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
class Primxx<2>
|
||||
{
|
||||
public:
|
||||
Primxx<2>() {
|
||||
Primes.push_back ( 2 );
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
class Primxx<1> : public Primxx<2> {};
|
||||
|
||||
template <int upperBound> // upperBound should be odd or 2
|
||||
class Primxa : public Primxa < upperBound - 2 >
|
||||
{
|
||||
public:
|
||||
enum {
|
||||
isPrime = IsPrime < upperBound, ( upperBound >> 1 ) | 1 >::result
|
||||
};
|
||||
Primxa<upperBound>() {
|
||||
if ( isPrime )
|
||||
Primes.push_back ( upperBound );
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
class Primxa<2>
|
||||
{
|
||||
public:
|
||||
Primxa<2>() {
|
||||
Primes.push_back ( 2 );
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
class Primxa<1> : public Primxa<2> {};
|
||||
|
||||
template <int upperBound> // upperBound should be odd or 2
|
||||
class Primxb : public Primxb < upperBound - 2 >
|
||||
{
|
||||
public:
|
||||
enum {
|
||||
isPrime = IsPrime < upperBound, ( upperBound >> 1 ) | 1 >::result
|
||||
};
|
||||
Primxb<upperBound>() {
|
||||
if ( isPrime )
|
||||
Primes.push_back ( upperBound );
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
class Primxb<2>
|
||||
{
|
||||
public:
|
||||
Primxb<2>() {
|
||||
Primes.push_back ( 2 );
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
class Primxb<1> : public Primxb<2> {};
|
||||
|
||||
int main()
|
||||
{
|
||||
PrimePick<1601> PrimeInitializer;
|
||||
Primxx<1601> test1;
|
||||
Primxa<1601> test2;
|
||||
Primxb<1601> test3;
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
cout << a + b;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
printf("%s\n%s\n", getenv("env"), getenv("test"));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
#include </dev/urandom>
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("abs %d", abs(-1024));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int size = 80 * 1024 * 1024;
|
||||
int *a = NULL;
|
||||
a = (int *)malloc(size);
|
||||
memset(a, 1, size);
|
||||
free(a);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int size = 256 * 1024 * 1024;
|
||||
int *a = NULL;
|
||||
a = (int *)malloc(size);
|
||||
if (a == NULL) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
memset(a, 1, size);
|
||||
free(a);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int arr[102400000];
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
memset(arr, 1, sizeof(arr));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char input[100];
|
||||
scanf("%s", input);
|
||||
printf("%s\n", input);
|
||||
printf("Hello world");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
int main() {
|
||||
FILE *f = fopen("/tmp/fsize_test", "w");
|
||||
if(f == NULL) {
|
||||
return 1;
|
||||
}
|
||||
int i;
|
||||
for(i = 0;i < 10000; i++) {
|
||||
if (fprintf(f, "%s", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") <= 0) {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
int main()
|
||||
{
|
||||
return 25;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
raise(SIGSEGV);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
sleep(4000);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int big_stack[1024 * 1024 * 20] = {0};
|
||||
printf("big stack");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
fprintf(stderr, "stderr\n");
|
||||
fprintf(stdout, "--------------\n");
|
||||
fprintf(stdout, "stdout\n");
|
||||
fprintf(stderr, "+++++++++++++++\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main () {
|
||||
clock();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("uid %d\ngid %d\n", getuid(), getgid());
|
||||
system("/usr/bin/id");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
int main()
|
||||
{
|
||||
while(1){};
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
string s;
|
||||
cin >> s;
|
||||
cout << s;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char *argv[] = {"/bin/echo", "Helloworld", NULL};
|
||||
char *env[] = {NULL};
|
||||
|
||||
execve("/bin/echo", argv, env);
|
||||
perror("execve");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/syscall.h>
|
||||
|
||||
|
||||
int execveat_(int fd, const char *path, char **argv, char **envp, int flags)
|
||||
{
|
||||
#ifdef __NR_execveat
|
||||
return syscall(__NR_execveat, fd, path, argv, envp, flags);
|
||||
#endif
|
||||
}
|
||||
|
||||
int main() {
|
||||
#ifndef __NR_execveat
|
||||
printf("syscall not found");
|
||||
return 0;
|
||||
#else
|
||||
char *envp[] = {"test=1", NULL};
|
||||
char *argv[] = {"hello", NULL};
|
||||
|
||||
execveat_(1, "/bin/true", argv, envp, 0);
|
||||
printf("failed %d", errno);
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
pid_t pid = fork();
|
||||
if (pid > 0) {
|
||||
printf("i'm parent");
|
||||
}
|
||||
else if (pid == 0) {
|
||||
printf("i'm children");
|
||||
}
|
||||
else {
|
||||
printf("fork failed");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// maybe used in qsort function
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
printf("%ld", sysconf(_SC_PAGE_SIZE));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE *f = fopen(argv[1], argv[2]);
|
||||
if (f == NULL) {
|
||||
return errno;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int flags;
|
||||
if (!strcmp(argv[2], "w")) {
|
||||
flags = O_WRONLY | O_CREAT;
|
||||
}
|
||||
else {
|
||||
flags = O_RDWR | O_CREAT;
|
||||
}
|
||||
int fd = openat(0, argv[1], flags, 0755);
|
||||
if (fd < 0) {
|
||||
return errno;
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user