inital commit 1
This commit is contained in:
commit
07738cacaf
76 files changed
+9477
No files matched your search
+11
@@ -0,0 +1,11 @@
|
|||||||
|
.idea/
|
||||||
|
build/
|
||||||
|
.DS_Store
|
||||||
|
*.log
|
||||||
|
*.swp
|
||||||
|
*.so
|
||||||
|
__pycache__
|
||||||
|
*.pyc
|
||||||
|
cmake-build-debug/
|
||||||
|
.ycm_extra_conf.py
|
||||||
|
node_modules/
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
sudo: required
|
||||||
|
|
||||||
|
services:
|
||||||
|
- docker
|
||||||
|
|
||||||
|
script:
|
||||||
|
- docker build -t judger-test -f tests/Dockerfile-16.04 .
|
||||||
|
- docker run -it --rm -v $PWD:/src judger-test /bin/bash -c "chmod +x tests/runtest.sh && ./tests/runtest.sh"
|
||||||
|
|
||||||
|
- docker build -t judger-test -f tests/Dockerfile-18.04 .
|
||||||
|
- docker run -it --rm -v $PWD:/src judger-test /bin/bash -c "chmod +x tests/runtest.sh && ./tests/runtest.sh"
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
cmake_minimum_required(VERSION 2.5)
|
||||||
|
project(judger C)
|
||||||
|
|
||||||
|
#set(CMAKE_VERBOSE_MAKEFILE ON)
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/output)
|
||||||
|
|
||||||
|
set(CMAKE_C_FLAGS "-g -Wall -Werror -O3 -std=c99 -pie -fPIC")
|
||||||
|
|
||||||
|
# make judger lib
|
||||||
|
file(GLOB SOURCE "src/*.c" "src/rules/*.c")
|
||||||
|
add_executable(libjudger.so ${SOURCE})
|
||||||
|
target_link_libraries(libjudger.so pthread seccomp)
|
||||||
|
|
||||||
|
|
||||||
|
install(FILES output/libjudger.so
|
||||||
|
PERMISSIONS OWNER_EXECUTE OWNER_READ
|
||||||
|
DESTINATION /usr/lib/judger)
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
The Star And Thank Author License (SATA)
|
||||||
|
|
||||||
|
Copyright (c) <Qingdao University Online Judge Dev Team> <[email protected]>
|
||||||
|
|
||||||
|
Project Url: https://github.com/QingdaoU/OnlineJudge
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
And wait, the most important, you shall star/+1/like the project(s) in project url
|
||||||
|
section above first, and then thank the author(s) in Copyright section.
|
||||||
|
|
||||||
|
Here are some suggested ways:
|
||||||
|
|
||||||
|
- Email the authors a thank-you letter, and make friends with him/her/them.
|
||||||
|
- Report bugs or issues.
|
||||||
|
- Tell friends what a wonderful project this is.
|
||||||
|
- And, sure, you can just express thanks in your mind without telling the world.
|
||||||
|
|
||||||
|
Contributors of this project by forking have the option to add his/her name and
|
||||||
|
forked project url at copyright and project url sections, but shall not delete
|
||||||
|
or modify anything else in these two sections.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
# Judger
|
||||||
|
|
||||||
|
[](https://travis-ci.org/QingdaoU/Judger)
|
||||||
|
|
||||||
|
Judger for OnlineJudge
|
||||||
|
|
||||||
|
[Document](https://opensource.qduoj.com#/judger/api)
|
||||||
|
|
||||||
|
[JudgeServer](https://github.com/QingdaoU/JudgeServer)
|
||||||
|
|
||||||
|
[OnlineJudge](https://github.com/QingdaoU/OnlineJudge)
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Yanghe Sun
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
# Makefile for Judger's lua binding
|
||||||
|
|
||||||
|
CC = gcc
|
||||||
|
LDFLAGS ?= -pthread -lseccomp -fPIC -shared
|
||||||
|
LUAVER ?= 5.3
|
||||||
|
LUAVERPURE ?= $(subst .,,${LUAVER})
|
||||||
|
CFLAGS ?= -Wall -Werror -O3 -std=c99 -fPIC -I/usr/include/lua${LUAVER} -DLUA${LUAVERPURE}
|
||||||
|
|
||||||
|
JUDGER_OBJECTS = child.o killer.o logger.o runner.o rules/c_cpp.o rules/general.o
|
||||||
|
OBJECTS = $(addprefix ../../src/,${JUDGER_OBJECTS}) judger.o
|
||||||
|
|
||||||
|
.PHONY: all test install clean
|
||||||
|
|
||||||
|
all: judger.so
|
||||||
|
|
||||||
|
judger.so: ${OBJECTS}
|
||||||
|
${CC} ${OBJECTS} ${LDFLAGS} -o judger.so
|
||||||
|
|
||||||
|
install: all
|
||||||
|
@mkdir -p /usr/local/lib/lua/${LUAVER}/
|
||||||
|
@cp -f judger.so /usr/local/lib/lua/${LUAVER}/
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@rm -f ${OBJECTS} judger.so
|
||||||
|
|
||||||
|
%.o: %c
|
||||||
|
${CC} ${CFLAGS} -o $@ -c $<
|
||||||
|
|
||||||
@@ -0,0 +1,220 @@
|
|||||||
|
/*
|
||||||
|
Lua Binding:
|
||||||
|
The MIT License (MIT)
|
||||||
|
Copyright (c) <2016> <avaicode>
|
||||||
|
|
||||||
|
Judger:
|
||||||
|
The Star And Thank Author License (SATA)
|
||||||
|
Copyright (c) <Qingdao University Online Judge Dev Team> <[email protected]>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Usage:
|
||||||
|
In Lua: require "judger"
|
||||||
|
In C: luaL_requiref(L, "judger", luaopen_judger, 1)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <lua.h>
|
||||||
|
#include <lauxlib.h>
|
||||||
|
#include "../../src/runner.h"
|
||||||
|
|
||||||
|
typedef struct ConfItem {
|
||||||
|
const char* name;
|
||||||
|
int value;
|
||||||
|
} ConfItem;
|
||||||
|
static ConfItem conf_item[] = {
|
||||||
|
{"_VERSION", VERSION},
|
||||||
|
{"_UNLIMITED", UNLIMITED},
|
||||||
|
{"_RESULT_WRONG_ANSWER", WRONG_ANSWER},
|
||||||
|
{"_RESULT_SUCCESS", SUCCESS},
|
||||||
|
{"_RESULT_CPU_TIME_LIMIT_EXCEEDED", CPU_TIME_LIMIT_EXCEEDED},
|
||||||
|
{"_RESULT_REAL_TIME_LIMIT_EXCEEDED", REAL_TIME_LIMIT_EXCEEDED},
|
||||||
|
{"_RESULT_MEMORY_LIMIT_EXCEEDED", MEMORY_LIMIT_EXCEEDED},
|
||||||
|
{"_RESULT_RUNTIME_ERROR", RUNTIME_ERROR},
|
||||||
|
{"_RESULT_SYSTEM_ERROR", SYSTEM_ERROR},
|
||||||
|
{"_ERROR_INVALID_CONFIG", INVALID_CONFIG},
|
||||||
|
{"_ERROR_FORK_FAILED", FORK_FAILED},
|
||||||
|
|
||||||
|
{"_ERROR_PTHREAD_FAILED", PTHREAD_FAILED},
|
||||||
|
{"_ERROR_WAIT_FAILED", WAIT_FAILED},
|
||||||
|
{"_ERROR_ROOT_REQUIRED", ROOT_REQUIRED},
|
||||||
|
{"_ERROR_LOAD_SECCOMP_FAILED", LOAD_SECCOMP_FAILED},
|
||||||
|
{"_ERROR_SETRLIMIT_FAILED", SETRLIMIT_FAILED},
|
||||||
|
{"_ERROR_DUP2_FAILED", DUP2_FAILED},
|
||||||
|
{"_ERROR_SETUID_FAILED", SETUID_FAILED},
|
||||||
|
{"_ERROR_EXECVE_FAILED", EXECVE_FAILED},
|
||||||
|
{"_ERROR_SPJ_ERROR", SPJ_ERROR}
|
||||||
|
};
|
||||||
|
static const int conf_item_size = sizeof(conf_item) / sizeof(ConfItem);
|
||||||
|
|
||||||
|
typedef struct ArgMask {
|
||||||
|
const char* name;
|
||||||
|
int t; //Should be lua_type
|
||||||
|
int nil_accept;
|
||||||
|
} ArgMask;
|
||||||
|
static ArgMask arg_mask[] = {
|
||||||
|
{"max_cpu_time", LUA_TNUMBER, 0},
|
||||||
|
{"max_real_time", LUA_TNUMBER, 0},
|
||||||
|
{"max_memory", LUA_TNUMBER, 0},
|
||||||
|
{"max_process_number", LUA_TNUMBER, 0},
|
||||||
|
{"max_output_size", LUA_TNUMBER, 0},
|
||||||
|
{"exe_path", LUA_TSTRING, 0},
|
||||||
|
{"input_path", LUA_TSTRING, 0},
|
||||||
|
{"output_path", LUA_TSTRING, 0},
|
||||||
|
{"error_path", LUA_TSTRING, 0},
|
||||||
|
{"log_path", LUA_TSTRING, 0},
|
||||||
|
{"seccomp_rule_name", LUA_TSTRING, 1},
|
||||||
|
{"args", LUA_TTABLE, 0},
|
||||||
|
{"env", LUA_TTABLE, 0},
|
||||||
|
{"uid", LUA_TNUMBER, 0},
|
||||||
|
{"gid", LUA_TNUMBER, 0}
|
||||||
|
};
|
||||||
|
static const int arg_mask_size = sizeof(arg_mask) / sizeof(ArgMask);
|
||||||
|
|
||||||
|
static int global_judge(lua_State* L);
|
||||||
|
|
||||||
|
static const struct luaL_Reg judger[] = {
|
||||||
|
{"run", global_judge},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
static char* _strdup(const char* s) {
|
||||||
|
char* t = NULL;
|
||||||
|
if (s && (t = (char*)malloc(strlen(s) + 1)))
|
||||||
|
strcpy(t, s);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void destory_config(struct config* config) {
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
free(config->exe_path);
|
||||||
|
free(config->input_path);
|
||||||
|
free(config->output_path);
|
||||||
|
free(config->error_path);
|
||||||
|
free(config->log_path);
|
||||||
|
free(config->seccomp_rule_name);
|
||||||
|
|
||||||
|
for (; i < 256; ++i) {
|
||||||
|
free(config->args[i]);
|
||||||
|
free(config->env[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int lua_checkstringtable(lua_State* L, char** dst, int max_size, int* dst_size) {
|
||||||
|
int table_index = lua_gettop(L);
|
||||||
|
int table_size = 0;
|
||||||
|
|
||||||
|
lua_pushnil(L);
|
||||||
|
while (0 != lua_next(L, table_index)) {
|
||||||
|
if (lua_type(L, -1) != LUA_TSTRING)
|
||||||
|
return 0;
|
||||||
|
if (lua_type(L, -2) != LUA_TNUMBER)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (table_size == max_size)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
dst[table_size] = _strdup(lua_tostring(L, -1));
|
||||||
|
table_size++;
|
||||||
|
|
||||||
|
lua_pop(L, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dst_size != NULL)
|
||||||
|
*dst_size = table_size;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int global_judge(lua_State* L) {
|
||||||
|
int i = 0, table_size = 0;
|
||||||
|
struct config config;
|
||||||
|
struct result result;
|
||||||
|
void* arg_refs[arg_mask_size];
|
||||||
|
|
||||||
|
arg_refs[0] = &config.max_cpu_time;
|
||||||
|
arg_refs[1] = &config.max_real_time;
|
||||||
|
arg_refs[2] = &config.max_memory;
|
||||||
|
arg_refs[3] = &config.max_process_number;
|
||||||
|
arg_refs[4] = &config.max_output_size;
|
||||||
|
arg_refs[5] = &config.exe_path;
|
||||||
|
arg_refs[6] = &config.input_path;
|
||||||
|
arg_refs[7] = &config.output_path;
|
||||||
|
arg_refs[8] = &config.error_path;
|
||||||
|
arg_refs[9] = &config.log_path;
|
||||||
|
arg_refs[10] = &config.seccomp_rule_name;
|
||||||
|
arg_refs[11] = &config.args;
|
||||||
|
arg_refs[12] = &config.env;
|
||||||
|
arg_refs[13] = &config.uid;
|
||||||
|
arg_refs[14] = &config.gid;
|
||||||
|
memset(&config, 0, sizeof(config));
|
||||||
|
|
||||||
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
|
for (; i < arg_mask_size; ++i) {
|
||||||
|
lua_pushstring(L, arg_mask[i].name);
|
||||||
|
lua_gettable(L, -2);
|
||||||
|
if (lua_type(L, -1) != arg_mask[i].t && !(arg_mask[i].nil_accept && lua_type(L, -1) == LUA_TNIL))
|
||||||
|
return destory_config(&config), luaL_error(L, "bad argument %s", arg_mask[i].name);
|
||||||
|
|
||||||
|
|
||||||
|
switch (lua_type(L, -1)) {
|
||||||
|
case LUA_TNUMBER:
|
||||||
|
*(int*)arg_refs[i] = lua_tointeger(L, -1);
|
||||||
|
break;
|
||||||
|
case LUA_TSTRING:
|
||||||
|
*(char**)arg_refs[i] = _strdup(lua_tostring(L, -1));
|
||||||
|
break;
|
||||||
|
case LUA_TTABLE:
|
||||||
|
if (lua_checkstringtable(L, (char**)arg_refs[i], 256 - 1, &table_size) == 0) {
|
||||||
|
destory_config(&config);
|
||||||
|
return luaL_error(L, "bad argument %s (item must be a string)", arg_mask[i].name);
|
||||||
|
}
|
||||||
|
*((char**)arg_refs[i] + table_size) = NULL;
|
||||||
|
break;
|
||||||
|
case LUA_TNIL:
|
||||||
|
*(char**)arg_refs[i] = NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pop(L, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
run(&config, &result);
|
||||||
|
|
||||||
|
lua_newtable(L);
|
||||||
|
lua_pushstring(L, "cpu_time"); lua_pushnumber(L, result.cpu_time); lua_rawset(L, -3);
|
||||||
|
lua_pushstring(L, "real_time"); lua_pushnumber(L, result.real_time); lua_rawset(L, -3);
|
||||||
|
lua_pushstring(L, "memory"); lua_pushnumber(L, result.memory); lua_rawset(L, -3);
|
||||||
|
lua_pushstring(L, "signal"); lua_pushnumber(L, result.signal); lua_rawset(L, -3);
|
||||||
|
lua_pushstring(L, "exit_code"); lua_pushnumber(L, result.exit_code); lua_rawset(L, -3);
|
||||||
|
lua_pushstring(L, "error"); lua_pushnumber(L, result.error); lua_rawset(L, -3);
|
||||||
|
lua_pushstring(L, "result"); lua_pushnumber(L, result.result); lua_rawset(L, -3);
|
||||||
|
assert(lua_gettop(L) == 2);
|
||||||
|
|
||||||
|
destory_config(&config);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int luaopen_judger(lua_State* L) {
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
#ifdef LUA51
|
||||||
|
luaL_register(L, "judger", judger);
|
||||||
|
#else
|
||||||
|
luaL_newlib(L, judger);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
for (; i < conf_item_size; ++i) {
|
||||||
|
lua_pushstring(L, conf_item[i].name);
|
||||||
|
lua_pushnumber(L, conf_item[i].value);
|
||||||
|
lua_rawset(L, -3);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"target_name": "judger",
|
||||||
|
'cflags_cc!': [ '-std=c++11','-O2' ],
|
||||||
|
"sources": [ "judger.cc"],
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,234 @@
|
|||||||
|
#include "node.h"
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
#include <string>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
|
namespace demo {
|
||||||
|
|
||||||
|
using v8::FunctionCallbackInfo;
|
||||||
|
using v8::Isolate;
|
||||||
|
using v8::JSON;
|
||||||
|
using v8::Local;
|
||||||
|
using v8::Int32;
|
||||||
|
using v8::Array;
|
||||||
|
using v8::Integer;
|
||||||
|
using v8::Uint32;
|
||||||
|
using v8::Number;
|
||||||
|
using v8::Object;
|
||||||
|
using v8::String;
|
||||||
|
using v8::Value;
|
||||||
|
using v8::MaybeLocal;
|
||||||
|
using v8::Exception;
|
||||||
|
using v8::Context;
|
||||||
|
|
||||||
|
/* 转成数字 */
|
||||||
|
bool ToNumber(Isolate * isolate,Local<Value> &args,char * key,std::string &str){
|
||||||
|
Local<Context> context= isolate->GetCurrentContext();
|
||||||
|
Local<Value> val = args->ToObject()->Get(context,String::NewFromUtf8(isolate,key)).ToLocalChecked();
|
||||||
|
if(val->IsNullOrUndefined())
|
||||||
|
return true;
|
||||||
|
else if( val->IsNumber()){
|
||||||
|
Local<Integer> num = val->ToInteger(context).ToLocalChecked();
|
||||||
|
str+=" --";
|
||||||
|
str+=std::string(key);
|
||||||
|
str+="=";
|
||||||
|
str+=std::to_string(num->Value());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,"typeof argument must be Object!")));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char * _ToCharPTR(Local<Value> &val,Local<Context> &context,char * str_content){
|
||||||
|
String::Utf8Value str_val( val->ToString(context).ToLocalChecked());
|
||||||
|
strcpy(str_content,*str_val);
|
||||||
|
return str_content;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 转成字符串 */
|
||||||
|
bool ToCStr(Isolate * isolate,Local<Value> &args,char * key,std::string &str){
|
||||||
|
|
||||||
|
char str_content[255]={0};
|
||||||
|
Local<Context> context= isolate->GetCurrentContext();
|
||||||
|
Local<Value> val;
|
||||||
|
if(args->IsObject())
|
||||||
|
val = args->ToObject()->Get(context,String::NewFromUtf8(isolate,key)).ToLocalChecked();
|
||||||
|
else
|
||||||
|
val = args;
|
||||||
|
if(val->IsNullOrUndefined())
|
||||||
|
return true;
|
||||||
|
else if(val->IsString()){
|
||||||
|
_ToCharPTR(val,context,str_content);
|
||||||
|
str+=" --";
|
||||||
|
str+=std::string(key);
|
||||||
|
str+="=";
|
||||||
|
str+=std::string(str_content);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
char ret[100];
|
||||||
|
sprintf(ret,"typeof %s must be String!",key);
|
||||||
|
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,ret)));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 参数:
|
||||||
|
* {
|
||||||
|
* max_cpu_time
|
||||||
|
* max_real_time
|
||||||
|
* max_memory
|
||||||
|
* memory_limit_check_only ??
|
||||||
|
* max_stack
|
||||||
|
* max_process
|
||||||
|
* max_output_size
|
||||||
|
* exe_path
|
||||||
|
* input_path
|
||||||
|
* output_path
|
||||||
|
* error_path
|
||||||
|
* args:[]
|
||||||
|
* env:[]
|
||||||
|
* }
|
||||||
|
* 返回值
|
||||||
|
* {
|
||||||
|
* cpu_time
|
||||||
|
* real_time
|
||||||
|
* memory
|
||||||
|
* signal
|
||||||
|
* exit_code
|
||||||
|
* result
|
||||||
|
* error:
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
|
||||||
|
void Method(const FunctionCallbackInfo<Value>& args)
|
||||||
|
{
|
||||||
|
Isolate* isolate = args.GetIsolate();
|
||||||
|
Local<Context> context= isolate->GetCurrentContext();
|
||||||
|
|
||||||
|
std::string _args = "/usr/lib/judger/libjudger.so";
|
||||||
|
|
||||||
|
if( !args[0]->IsObject()){
|
||||||
|
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,"typeof argument must be Object!")));
|
||||||
|
}
|
||||||
|
|
||||||
|
Local<Value> argument = args[0];
|
||||||
|
|
||||||
|
std::string int_vars[] = { "max_cpu_time","max_real_time","max_memory","memory_limit_check_only","max_stack","max_process_number","max_output_size","uid","gid"};
|
||||||
|
std::string str_vars[] = { "input_path","output_path","error_path","exe_path","log_path","seccomp_rule_name"};
|
||||||
|
|
||||||
|
for( auto var :int_vars){
|
||||||
|
if( ! ToNumber(isolate,argument,(char *)var.c_str(),_args))
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for( auto var : str_vars){
|
||||||
|
if( !ToCStr(isolate,argument,(char *)var.c_str(),_args))
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* args */
|
||||||
|
Local<Value> margs= argument->ToObject()->Get(context,String::NewFromUtf8(isolate,"args")).ToLocalChecked();
|
||||||
|
if( margs->IsNullOrUndefined()){
|
||||||
|
;
|
||||||
|
}
|
||||||
|
else if( margs->IsArray()){
|
||||||
|
Local<Array> args = margs.As<Array>();
|
||||||
|
int len = args->Length();
|
||||||
|
int i;
|
||||||
|
for(i=0;i<len;i++){
|
||||||
|
Local<Value> in = args->Get(i);
|
||||||
|
if( !ToCStr(isolate,in,(char *)"args",_args))
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { //not array
|
||||||
|
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,"args must be a list")));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Local<Value> menv= argument->ToObject()->Get(context,String::NewFromUtf8(isolate,"env")).ToLocalChecked();
|
||||||
|
if( menv->IsNullOrUndefined()){
|
||||||
|
;
|
||||||
|
}
|
||||||
|
else if( margs->IsArray()){
|
||||||
|
Local<Array> env = menv.As<Array>();
|
||||||
|
int len = env->Length();
|
||||||
|
int i;
|
||||||
|
for(i=0;i<len;i++){
|
||||||
|
Local<Value> in = env->Get(i);
|
||||||
|
if(!ToCStr(isolate,in,(char *)"env",_args))
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { //not array
|
||||||
|
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,"env must be a list")));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
char buf[255];
|
||||||
|
FILE *_result_f;
|
||||||
|
std::string result;
|
||||||
|
if(( _result_f = popen(_args.c_str(),"r"))==NULL)
|
||||||
|
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,"run /usr/lib/judger/libjudger.so failed!")));
|
||||||
|
while(fgets(buf,sizeof(buf),_result_f)){
|
||||||
|
result += std::string(buf);
|
||||||
|
}
|
||||||
|
pclose(_result_f);
|
||||||
|
|
||||||
|
const char *pr = result.c_str();
|
||||||
|
int len = strlen(pr);
|
||||||
|
for(len--;len>=0;len--){
|
||||||
|
if( pr[len] == '{')
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(len)
|
||||||
|
printf("%*.*s",len,len,pr); //输出 程序的输出
|
||||||
|
|
||||||
|
MaybeLocal<Value> mres = JSON::Parse(isolate,String::NewFromUtf8(isolate,pr+len));
|
||||||
|
args.GetReturnValue().Set(mres.ToLocalChecked());
|
||||||
|
}
|
||||||
|
|
||||||
|
void init(Local<Object> exports)
|
||||||
|
{
|
||||||
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||||
|
NODE_SET_METHOD(exports, "run", Method);
|
||||||
|
exports->Set( String::NewFromUtf8(isolate,"UNLIMITED"),Integer::New(isolate, -1));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"VERSION"),Integer::New(isolate, 0x020101));
|
||||||
|
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"RESULT_SUCCESS"),Integer::New(isolate, 0));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"RESULT_WRONG_ANSWER"),Integer::New(isolate, -1));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"RESULT_CPU_TIME_LIMIT_EXCEEDED"),Integer::New(isolate, 1));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"RESULT_REAL_TIME_LIMIT_EXCEEDED"),Integer::New(isolate, 2));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"RESULT_MEMORY_LIMIT_EXCEEDED"),Integer::New(isolate, 3));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"RESULT_RUNTIME_ERROR"),Integer::New(isolate, 4));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"RESULT_SYSTEM_ERROR"),Integer::New(isolate, 5));
|
||||||
|
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"ERROR_INVALID_CONFIG"),Integer::New(isolate, -1));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"ERROR_FORK_FAILED"),Integer::New(isolate, -2));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"ERROR_PTHREAD_FAILED"),Integer::New(isolate, -3));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"ERROR_WAIT_FAILED"),Integer::New(isolate, -4));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"ERROR_ROOT_REQUIRED"),Integer::New(isolate, -5));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"ERROR_LOAD_SECCOMP_FAILED"),Integer::New(isolate, -6));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"ERROR_SETRLIMIT_FAILED"),Integer::New(isolate, -7));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"ERROR_DUP2_FAILED"),Integer::New(isolate, -8));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"ERROR_SETUID_FAILED"),Integer::New(isolate, -9));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"ERROR_EXECVE_FAILED"),Integer::New(isolate, -10));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate,"ERROR_SPJ_ERROR"),Integer::New(isolate, -11));
|
||||||
|
}
|
||||||
|
|
||||||
|
NODE_MODULE(addon, init)
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"name": "judger",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "judger for online system",
|
||||||
|
"main": "./build/Release/judger.node",
|
||||||
|
"scripts": {
|
||||||
|
"build": "node-gyp rebuild",
|
||||||
|
"test": "sudo ./node_modules/.bin/mocha -t 5000 ../../tests/Nodejs_and_core"
|
||||||
|
},
|
||||||
|
"author": "virusdefender,rainboy",
|
||||||
|
"license": "GPL",
|
||||||
|
"devDependencies": {
|
||||||
|
"mocha": "^6.1.4",
|
||||||
|
"node-gyp": "^4.0.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
## 安装
|
||||||
|
|
||||||
|
先安装`judger`,然后
|
||||||
|
|
||||||
|
```
|
||||||
|
npm install
|
||||||
|
npm run build
|
||||||
|
npm run test
|
||||||
|
```
|
||||||
|
|
||||||
|
## 已知bug
|
||||||
|
|
||||||
|
#### bug_1
|
||||||
|
|
||||||
|
`freopen("data.out","w",stdout)`,不能产生"data.out",这个应该是judger代码的锅,:cry:
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
dist/
|
||||||
|
build/
|
||||||
|
*.egg-info/
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
import json
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
UNLIMITED = -1
|
||||||
|
VERSION = 0x020101
|
||||||
|
|
||||||
|
RESULT_SUCCESS = 0
|
||||||
|
RESULT_WRONG_ANSWER = -1
|
||||||
|
RESULT_CPU_TIME_LIMIT_EXCEEDED = 1
|
||||||
|
RESULT_REAL_TIME_LIMIT_EXCEEDED = 2
|
||||||
|
RESULT_MEMORY_LIMIT_EXCEEDED = 3
|
||||||
|
RESULT_RUNTIME_ERROR = 4
|
||||||
|
RESULT_SYSTEM_ERROR = 5
|
||||||
|
|
||||||
|
ERROR_INVALID_CONFIG = -1
|
||||||
|
ERROR_FORK_FAILED = -2
|
||||||
|
ERROR_PTHREAD_FAILED = -3
|
||||||
|
ERROR_WAIT_FAILED = -4
|
||||||
|
ERROR_ROOT_REQUIRED = -5
|
||||||
|
ERROR_LOAD_SECCOMP_FAILED = -6
|
||||||
|
ERROR_SETRLIMIT_FAILED = -7
|
||||||
|
ERROR_DUP2_FAILED = -8
|
||||||
|
ERROR_SETUID_FAILED = -9
|
||||||
|
ERROR_EXECVE_FAILED = -10
|
||||||
|
ERROR_SPJ_ERROR = -11
|
||||||
|
|
||||||
|
|
||||||
|
def run(max_cpu_time,
|
||||||
|
max_real_time,
|
||||||
|
max_memory,
|
||||||
|
max_stack,
|
||||||
|
max_output_size,
|
||||||
|
max_process_number,
|
||||||
|
exe_path,
|
||||||
|
input_path,
|
||||||
|
output_path,
|
||||||
|
error_path,
|
||||||
|
args,
|
||||||
|
env,
|
||||||
|
log_path,
|
||||||
|
seccomp_rule_name,
|
||||||
|
uid,
|
||||||
|
gid,
|
||||||
|
memory_limit_check_only=0):
|
||||||
|
str_list_vars = ["args", "env"]
|
||||||
|
int_vars = ["max_cpu_time", "max_real_time",
|
||||||
|
"max_memory", "max_stack", "max_output_size",
|
||||||
|
"max_process_number", "uid", "gid", "memory_limit_check_only"]
|
||||||
|
str_vars = ["exe_path", "input_path", "output_path", "error_path", "log_path"]
|
||||||
|
|
||||||
|
proc_args = ["/usr/lib/judger/libjudger.so"]
|
||||||
|
|
||||||
|
for var in str_list_vars:
|
||||||
|
value = vars()[var]
|
||||||
|
if not isinstance(value, list):
|
||||||
|
raise ValueError("{} must be a list".format(var))
|
||||||
|
for item in value:
|
||||||
|
if not isinstance(item, str):
|
||||||
|
raise ValueError("{} item must be a string".format(var))
|
||||||
|
proc_args.append("--{}={}".format(var, item))
|
||||||
|
|
||||||
|
for var in int_vars:
|
||||||
|
value = vars()[var]
|
||||||
|
if not isinstance(value, int):
|
||||||
|
raise ValueError("{} must be a int".format(var))
|
||||||
|
if value != UNLIMITED:
|
||||||
|
proc_args.append("--{}={}".format(var, value))
|
||||||
|
|
||||||
|
for var in str_vars:
|
||||||
|
value = vars()[var]
|
||||||
|
if not isinstance(value, str):
|
||||||
|
raise ValueError("{} must be a string".format(var))
|
||||||
|
proc_args.append("--{}={}".format(var, value))
|
||||||
|
|
||||||
|
if not isinstance(seccomp_rule_name, str) and seccomp_rule_name is not None:
|
||||||
|
raise ValueError("seccomp_rule_name must be a string or None")
|
||||||
|
if seccomp_rule_name:
|
||||||
|
proc_args.append("--seccomp_rule={}".format(seccomp_rule_name))
|
||||||
|
|
||||||
|
proc = subprocess.Popen(proc_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
|
out, err = proc.communicate()
|
||||||
|
if err:
|
||||||
|
raise ValueError("Error occurred while calling judger: {}".format(err))
|
||||||
|
return json.loads(out.decode("utf-8"))
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
[build-system]
|
||||||
|
requires = ["setuptools"]
|
||||||
|
build-backend = "setuptools.build_meta"
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
[metadata]
|
||||||
|
name = judger
|
||||||
|
version = 2.2.0
|
||||||
|
description = python wrapper of libjudger
|
||||||
|
|
||||||
|
[options]
|
||||||
|
packages = find:
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import _judger
|
||||||
|
import os
|
||||||
|
|
||||||
|
if os.system("gcc main.c -o main"):
|
||||||
|
print("compile error")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
ret = _judger.run(max_cpu_time=1000,
|
||||||
|
max_real_time=2000,
|
||||||
|
max_memory=128 * 1024 * 1024,
|
||||||
|
max_process_number=200,
|
||||||
|
max_output_size=10000,
|
||||||
|
max_stack=32 * 1024 * 1024,
|
||||||
|
# five args above can be _judger.UNLIMITED
|
||||||
|
exe_path="main",
|
||||||
|
input_path="1.in",
|
||||||
|
output_path="1.out",
|
||||||
|
error_path="1.out",
|
||||||
|
args=[],
|
||||||
|
# can be empty list
|
||||||
|
env=[],
|
||||||
|
log_path="judger.log",
|
||||||
|
# can be None
|
||||||
|
seccomp_rule_name="c_cpp",
|
||||||
|
uid=0,
|
||||||
|
gid=0)
|
||||||
|
print(ret)
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
char input[1000];
|
||||||
|
scanf("%s", input);
|
||||||
|
printf("Hello %s\n", input);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
+6021
File diff suppressed because it is too large.
Load diff
+273
@@ -0,0 +1,273 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* argtable3: Declares the main interfaces of the library
|
||||||
|
*
|
||||||
|
* This file is part of the argtable3 library.
|
||||||
|
*
|
||||||
|
* Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
|
||||||
|
* <[email protected]>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of STEWART HEITMANN nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
|
||||||
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#ifndef ARGTABLE3
|
||||||
|
#define ARGTABLE3
|
||||||
|
|
||||||
|
#include <stdio.h> /* FILE */
|
||||||
|
#include <time.h> /* struct tm */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define ARG_REX_ICASE 1
|
||||||
|
#define ARG_DSTR_SIZE 200
|
||||||
|
#define ARG_CMD_NAME_LEN 100
|
||||||
|
#define ARG_CMD_DESCRIPTION_LEN 256
|
||||||
|
|
||||||
|
#ifndef ARG_REPLACE_GETOPT
|
||||||
|
#define ARG_REPLACE_GETOPT 1 /* use the embedded getopt as the system getopt(3) */
|
||||||
|
#endif /* ARG_REPLACE_GETOPT */
|
||||||
|
|
||||||
|
/* bit masks for arg_hdr.flag */
|
||||||
|
enum { ARG_TERMINATOR = 0x1, ARG_HASVALUE = 0x2, ARG_HASOPTVALUE = 0x4 };
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
#if defined(argtable3_EXPORTS)
|
||||||
|
#define ARG_EXTERN __declspec(dllexport)
|
||||||
|
#elif defined(argtable3_IMPORTS)
|
||||||
|
#define ARG_EXTERN __declspec(dllimport)
|
||||||
|
#else
|
||||||
|
#define ARG_EXTERN
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
#define ARG_EXTERN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct _internal_arg_dstr* arg_dstr_t;
|
||||||
|
typedef void* arg_cmd_itr_t;
|
||||||
|
|
||||||
|
typedef void(arg_resetfn)(void* parent);
|
||||||
|
typedef int(arg_scanfn)(void* parent, const char* argval);
|
||||||
|
typedef int(arg_checkfn)(void* parent);
|
||||||
|
typedef void(arg_errorfn)(void* parent, arg_dstr_t ds, int error, const char* argval, const char* progname);
|
||||||
|
typedef void(arg_dstr_freefn)(char* buf);
|
||||||
|
typedef int(arg_cmdfn)(int argc, char* argv[], arg_dstr_t res);
|
||||||
|
typedef int(arg_comparefn)(const void* k1, const void* k2);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The arg_hdr struct defines properties that are common to all arg_xxx structs.
|
||||||
|
* The argtable library requires each arg_xxx struct to have an arg_hdr
|
||||||
|
* struct as its first data member.
|
||||||
|
* The argtable library functions then use this data to identify the
|
||||||
|
* properties of the command line option, such as its option tags,
|
||||||
|
* datatype string, and glossary strings, and so on.
|
||||||
|
* Moreover, the arg_hdr struct contains pointers to custom functions that
|
||||||
|
* are provided by each arg_xxx struct which perform the tasks of parsing
|
||||||
|
* that particular arg_xxx arguments, performing post-parse checks, and
|
||||||
|
* reporting errors.
|
||||||
|
* These functions are private to the individual arg_xxx source code
|
||||||
|
* and are the pointer to them are initiliased by that arg_xxx struct's
|
||||||
|
* constructor function. The user could alter them after construction
|
||||||
|
* if desired, but the original intention is for them to be set by the
|
||||||
|
* constructor and left unaltered.
|
||||||
|
*/
|
||||||
|
typedef struct arg_hdr {
|
||||||
|
char flag; /* Modifier flags: ARG_TERMINATOR, ARG_HASVALUE. */
|
||||||
|
const char* shortopts; /* String defining the short options */
|
||||||
|
const char* longopts; /* String defiing the long options */
|
||||||
|
const char* datatype; /* Description of the argument data type */
|
||||||
|
const char* glossary; /* Description of the option as shown by arg_print_glossary function */
|
||||||
|
int mincount; /* Minimum number of occurences of this option accepted */
|
||||||
|
int maxcount; /* Maximum number of occurences if this option accepted */
|
||||||
|
void* parent; /* Pointer to parent arg_xxx struct */
|
||||||
|
arg_resetfn* resetfn; /* Pointer to parent arg_xxx reset function */
|
||||||
|
arg_scanfn* scanfn; /* Pointer to parent arg_xxx scan function */
|
||||||
|
arg_checkfn* checkfn; /* Pointer to parent arg_xxx check function */
|
||||||
|
arg_errorfn* errorfn; /* Pointer to parent arg_xxx error function */
|
||||||
|
void* priv; /* Pointer to private header data for use by arg_xxx functions */
|
||||||
|
} arg_hdr_t;
|
||||||
|
|
||||||
|
typedef struct arg_rem {
|
||||||
|
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||||
|
} arg_rem_t;
|
||||||
|
|
||||||
|
typedef struct arg_lit {
|
||||||
|
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||||
|
int count; /* Number of matching command line args */
|
||||||
|
} arg_lit_t;
|
||||||
|
|
||||||
|
typedef struct arg_int {
|
||||||
|
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||||
|
int count; /* Number of matching command line args */
|
||||||
|
int* ival; /* Array of parsed argument values */
|
||||||
|
} arg_int_t;
|
||||||
|
|
||||||
|
typedef struct arg_dbl {
|
||||||
|
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||||
|
int count; /* Number of matching command line args */
|
||||||
|
double* dval; /* Array of parsed argument values */
|
||||||
|
} arg_dbl_t;
|
||||||
|
|
||||||
|
typedef struct arg_str {
|
||||||
|
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||||
|
int count; /* Number of matching command line args */
|
||||||
|
const char** sval; /* Array of parsed argument values */
|
||||||
|
} arg_str_t;
|
||||||
|
|
||||||
|
typedef struct arg_rex {
|
||||||
|
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||||
|
int count; /* Number of matching command line args */
|
||||||
|
const char** sval; /* Array of parsed argument values */
|
||||||
|
} arg_rex_t;
|
||||||
|
|
||||||
|
typedef struct arg_file {
|
||||||
|
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||||
|
int count; /* Number of matching command line args*/
|
||||||
|
const char** filename; /* Array of parsed filenames (eg: /home/foo.bar) */
|
||||||
|
const char** basename; /* Array of parsed basenames (eg: foo.bar) */
|
||||||
|
const char** extension; /* Array of parsed extensions (eg: .bar) */
|
||||||
|
} arg_file_t;
|
||||||
|
|
||||||
|
typedef struct arg_date {
|
||||||
|
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||||
|
const char* format; /* strptime format string used to parse the date */
|
||||||
|
int count; /* Number of matching command line args */
|
||||||
|
struct tm* tmval; /* Array of parsed time values */
|
||||||
|
} arg_date_t;
|
||||||
|
|
||||||
|
enum { ARG_ELIMIT = 1, ARG_EMALLOC, ARG_ENOMATCH, ARG_ELONGOPT, ARG_EMISSARG };
|
||||||
|
typedef struct arg_end {
|
||||||
|
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||||
|
int count; /* Number of errors encountered */
|
||||||
|
int* error; /* Array of error codes */
|
||||||
|
void** parent; /* Array of pointers to offending arg_xxx struct */
|
||||||
|
const char** argval; /* Array of pointers to offending argv[] string */
|
||||||
|
} arg_end_t;
|
||||||
|
|
||||||
|
typedef struct arg_cmd_info {
|
||||||
|
char name[ARG_CMD_NAME_LEN];
|
||||||
|
char description[ARG_CMD_DESCRIPTION_LEN];
|
||||||
|
arg_cmdfn* proc;
|
||||||
|
} arg_cmd_info_t;
|
||||||
|
|
||||||
|
/**** arg_xxx constructor functions *********************************/
|
||||||
|
|
||||||
|
ARG_EXTERN struct arg_rem* arg_rem(const char* datatype, const char* glossary);
|
||||||
|
|
||||||
|
ARG_EXTERN struct arg_lit* arg_lit0(const char* shortopts, const char* longopts, const char* glossary);
|
||||||
|
ARG_EXTERN struct arg_lit* arg_lit1(const char* shortopts, const char* longopts, const char* glossary);
|
||||||
|
ARG_EXTERN struct arg_lit* arg_litn(const char* shortopts, const char* longopts, int mincount, int maxcount, const char* glossary);
|
||||||
|
|
||||||
|
ARG_EXTERN struct arg_int* arg_int0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||||
|
ARG_EXTERN struct arg_int* arg_int1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||||
|
ARG_EXTERN struct arg_int* arg_intn(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary);
|
||||||
|
|
||||||
|
ARG_EXTERN struct arg_dbl* arg_dbl0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||||
|
ARG_EXTERN struct arg_dbl* arg_dbl1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||||
|
ARG_EXTERN struct arg_dbl* arg_dbln(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary);
|
||||||
|
|
||||||
|
ARG_EXTERN struct arg_str* arg_str0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||||
|
ARG_EXTERN struct arg_str* arg_str1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||||
|
ARG_EXTERN struct arg_str* arg_strn(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary);
|
||||||
|
|
||||||
|
ARG_EXTERN struct arg_rex* arg_rex0(const char* shortopts, const char* longopts, const char* pattern, const char* datatype, int flags, const char* glossary);
|
||||||
|
ARG_EXTERN struct arg_rex* arg_rex1(const char* shortopts, const char* longopts, const char* pattern, const char* datatype, int flags, const char* glossary);
|
||||||
|
ARG_EXTERN struct arg_rex* arg_rexn(const char* shortopts,
|
||||||
|
const char* longopts,
|
||||||
|
const char* pattern,
|
||||||
|
const char* datatype,
|
||||||
|
int mincount,
|
||||||
|
int maxcount,
|
||||||
|
int flags,
|
||||||
|
const char* glossary);
|
||||||
|
|
||||||
|
ARG_EXTERN struct arg_file* arg_file0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||||
|
ARG_EXTERN struct arg_file* arg_file1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||||
|
ARG_EXTERN struct arg_file* arg_filen(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary);
|
||||||
|
|
||||||
|
ARG_EXTERN struct arg_date* arg_date0(const char* shortopts, const char* longopts, const char* format, const char* datatype, const char* glossary);
|
||||||
|
ARG_EXTERN struct arg_date* arg_date1(const char* shortopts, const char* longopts, const char* format, const char* datatype, const char* glossary);
|
||||||
|
ARG_EXTERN struct arg_date* arg_daten(const char* shortopts, const char* longopts, const char* format, const char* datatype, int mincount, int maxcount, const char* glossary);
|
||||||
|
|
||||||
|
ARG_EXTERN struct arg_end* arg_end(int maxcount);
|
||||||
|
|
||||||
|
#define ARG_DSTR_STATIC ((arg_dstr_freefn*)0)
|
||||||
|
#define ARG_DSTR_VOLATILE ((arg_dstr_freefn*)1)
|
||||||
|
#define ARG_DSTR_DYNAMIC ((arg_dstr_freefn*)3)
|
||||||
|
|
||||||
|
/**** other functions *******************************************/
|
||||||
|
ARG_EXTERN int arg_nullcheck(void** argtable);
|
||||||
|
ARG_EXTERN int arg_parse(int argc, char** argv, void** argtable);
|
||||||
|
ARG_EXTERN void arg_print_option(FILE* fp, const char* shortopts, const char* longopts, const char* datatype, const char* suffix);
|
||||||
|
ARG_EXTERN void arg_print_syntax(FILE* fp, void** argtable, const char* suffix);
|
||||||
|
ARG_EXTERN void arg_print_syntaxv(FILE* fp, void** argtable, const char* suffix);
|
||||||
|
ARG_EXTERN void arg_print_glossary(FILE* fp, void** argtable, const char* format);
|
||||||
|
ARG_EXTERN void arg_print_glossary_gnu(FILE* fp, void** argtable);
|
||||||
|
ARG_EXTERN void arg_print_errors(FILE* fp, struct arg_end* end, const char* progname);
|
||||||
|
ARG_EXTERN void arg_print_option_ds(arg_dstr_t ds, const char* shortopts, const char* longopts, const char* datatype, const char* suffix);
|
||||||
|
ARG_EXTERN void arg_print_syntax_ds(arg_dstr_t ds, void** argtable, const char* suffix);
|
||||||
|
ARG_EXTERN void arg_print_syntaxv_ds(arg_dstr_t ds, void** argtable, const char* suffix);
|
||||||
|
ARG_EXTERN void arg_print_glossary_ds(arg_dstr_t ds, void** argtable, const char* format);
|
||||||
|
ARG_EXTERN void arg_print_glossary_gnu_ds(arg_dstr_t ds, void** argtable);
|
||||||
|
ARG_EXTERN void arg_print_errors_ds(arg_dstr_t ds, struct arg_end* end, const char* progname);
|
||||||
|
ARG_EXTERN void arg_freetable(void** argtable, size_t n);
|
||||||
|
|
||||||
|
ARG_EXTERN arg_dstr_t arg_dstr_create(void);
|
||||||
|
ARG_EXTERN void arg_dstr_destroy(arg_dstr_t ds);
|
||||||
|
ARG_EXTERN void arg_dstr_reset(arg_dstr_t ds);
|
||||||
|
ARG_EXTERN void arg_dstr_free(arg_dstr_t ds);
|
||||||
|
ARG_EXTERN void arg_dstr_set(arg_dstr_t ds, char* str, arg_dstr_freefn* free_proc);
|
||||||
|
ARG_EXTERN void arg_dstr_cat(arg_dstr_t ds, const char* str);
|
||||||
|
ARG_EXTERN void arg_dstr_catc(arg_dstr_t ds, char c);
|
||||||
|
ARG_EXTERN void arg_dstr_catf(arg_dstr_t ds, const char* fmt, ...);
|
||||||
|
ARG_EXTERN char* arg_dstr_cstr(arg_dstr_t ds);
|
||||||
|
|
||||||
|
ARG_EXTERN void arg_cmd_init(void);
|
||||||
|
ARG_EXTERN void arg_cmd_uninit(void);
|
||||||
|
ARG_EXTERN void arg_cmd_register(const char* name, arg_cmdfn* proc, const char* description);
|
||||||
|
ARG_EXTERN void arg_cmd_unregister(const char* name);
|
||||||
|
ARG_EXTERN int arg_cmd_dispatch(const char* name, int argc, char* argv[], arg_dstr_t res);
|
||||||
|
ARG_EXTERN unsigned int arg_cmd_count(void);
|
||||||
|
ARG_EXTERN arg_cmd_info_t* arg_cmd_info(const char* name);
|
||||||
|
ARG_EXTERN arg_cmd_itr_t arg_cmd_itr_create(void);
|
||||||
|
ARG_EXTERN void arg_cmd_itr_destroy(arg_cmd_itr_t itr);
|
||||||
|
ARG_EXTERN int arg_cmd_itr_advance(arg_cmd_itr_t itr);
|
||||||
|
ARG_EXTERN char* arg_cmd_itr_key(arg_cmd_itr_t itr);
|
||||||
|
ARG_EXTERN arg_cmd_info_t* arg_cmd_itr_value(arg_cmd_itr_t itr);
|
||||||
|
ARG_EXTERN int arg_cmd_itr_search(arg_cmd_itr_t itr, void* k);
|
||||||
|
ARG_EXTERN void arg_mgsort(void* data, int size, int esize, int i, int k, arg_comparefn* comparefn);
|
||||||
|
ARG_EXTERN void arg_make_get_help_msg(arg_dstr_t res);
|
||||||
|
ARG_EXTERN void arg_make_help_msg(arg_dstr_t ds, char* cmd_name, void** argtable);
|
||||||
|
ARG_EXTERN void arg_make_syntax_err_msg(arg_dstr_t ds, void** argtable, struct arg_end* end);
|
||||||
|
ARG_EXTERN int arg_make_syntax_err_help_msg(arg_dstr_t ds, char* name, int help, int nerrors, void** argtable, struct arg_end* end, int* exitcode);
|
||||||
|
ARG_EXTERN void arg_set_module_name(const char* name);
|
||||||
|
ARG_EXTERN void arg_set_module_version(int major, int minor, int patch, const char* tag);
|
||||||
|
|
||||||
|
/**** deprecated functions, for back-compatibility only ********/
|
||||||
|
ARG_EXTERN void arg_free(void** argtable);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
+175
@@ -0,0 +1,175 @@
|
|||||||
|
#define _DEFAULT_SOURCE
|
||||||
|
#define _POSIX_SOURCE
|
||||||
|
#define _GNU_SOURCE
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <grp.h>
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sched.h>
|
||||||
|
#include <sys/resource.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <sys/mount.h>
|
||||||
|
|
||||||
|
#include "runner.h"
|
||||||
|
#include "child.h"
|
||||||
|
#include "logger.h"
|
||||||
|
#include "rules/seccomp_rules.h"
|
||||||
|
|
||||||
|
#include "killer.h"
|
||||||
|
|
||||||
|
|
||||||
|
void close_file(FILE *fp) {
|
||||||
|
if (fp != NULL) {
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void child_process(FILE *log_fp, struct config *_config) {
|
||||||
|
FILE *input_file = NULL, *output_file = NULL, *error_file = NULL;
|
||||||
|
|
||||||
|
if (_config->max_stack != UNLIMITED) {
|
||||||
|
struct rlimit max_stack;
|
||||||
|
max_stack.rlim_cur = max_stack.rlim_max = (rlim_t) (_config->max_stack);
|
||||||
|
if (setrlimit(RLIMIT_STACK, &max_stack) != 0) {
|
||||||
|
CHILD_ERROR_EXIT(SETRLIMIT_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set memory limit
|
||||||
|
// if memory_limit_check_only == 0, we only check memory usage number, because setrlimit(maxrss) will cause some crash issues
|
||||||
|
if (_config->memory_limit_check_only == 0) {
|
||||||
|
if (_config->max_memory != UNLIMITED) {
|
||||||
|
struct rlimit max_memory;
|
||||||
|
max_memory.rlim_cur = max_memory.rlim_max = (rlim_t) (_config->max_memory) * 2;
|
||||||
|
if (setrlimit(RLIMIT_AS, &max_memory) != 0) {
|
||||||
|
CHILD_ERROR_EXIT(SETRLIMIT_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set cpu time limit (in seconds)
|
||||||
|
if (_config->max_cpu_time != UNLIMITED) {
|
||||||
|
struct rlimit max_cpu_time;
|
||||||
|
max_cpu_time.rlim_cur = max_cpu_time.rlim_max = (rlim_t) ((_config->max_cpu_time + 1000) / 1000);
|
||||||
|
if (setrlimit(RLIMIT_CPU, &max_cpu_time) != 0) {
|
||||||
|
CHILD_ERROR_EXIT(SETRLIMIT_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set max process number limit
|
||||||
|
if (_config->max_process_number != UNLIMITED) {
|
||||||
|
struct rlimit max_process_number;
|
||||||
|
max_process_number.rlim_cur = max_process_number.rlim_max = (rlim_t) _config->max_process_number;
|
||||||
|
if (setrlimit(RLIMIT_NPROC, &max_process_number) != 0) {
|
||||||
|
CHILD_ERROR_EXIT(SETRLIMIT_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set max output size limit
|
||||||
|
if (_config->max_output_size != UNLIMITED) {
|
||||||
|
struct rlimit max_output_size;
|
||||||
|
max_output_size.rlim_cur = max_output_size.rlim_max = (rlim_t ) _config->max_output_size;
|
||||||
|
if (setrlimit(RLIMIT_FSIZE, &max_output_size) != 0) {
|
||||||
|
CHILD_ERROR_EXIT(SETRLIMIT_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_config->input_path != NULL) {
|
||||||
|
input_file = fopen(_config->input_path, "r");
|
||||||
|
if (input_file == NULL) {
|
||||||
|
CHILD_ERROR_EXIT(DUP2_FAILED);
|
||||||
|
}
|
||||||
|
// redirect file -> stdin
|
||||||
|
// On success, these system calls return the new descriptor.
|
||||||
|
// On error, -1 is returned, and errno is set appropriately.
|
||||||
|
if (dup2(fileno(input_file), fileno(stdin)) == -1) {
|
||||||
|
// todo log
|
||||||
|
CHILD_ERROR_EXIT(DUP2_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_config->output_path != NULL) {
|
||||||
|
output_file = fopen(_config->output_path, "w");
|
||||||
|
if (output_file == NULL) {
|
||||||
|
CHILD_ERROR_EXIT(DUP2_FAILED);
|
||||||
|
}
|
||||||
|
// redirect stdout -> file
|
||||||
|
if (dup2(fileno(output_file), fileno(stdout)) == -1) {
|
||||||
|
CHILD_ERROR_EXIT(DUP2_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_config->error_path != NULL) {
|
||||||
|
// if outfile and error_file is the same path, we use the same file pointer
|
||||||
|
if (_config->output_path != NULL && strcmp(_config->output_path, _config->error_path) == 0) {
|
||||||
|
error_file = output_file;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
error_file = fopen(_config->error_path, "w");
|
||||||
|
if (error_file == NULL) {
|
||||||
|
// todo log
|
||||||
|
CHILD_ERROR_EXIT(DUP2_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// redirect stderr -> file
|
||||||
|
if (dup2(fileno(error_file), fileno(stderr)) == -1) {
|
||||||
|
// todo log
|
||||||
|
CHILD_ERROR_EXIT(DUP2_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set gid
|
||||||
|
gid_t group_list[] = {_config->gid};
|
||||||
|
if (_config->gid != -1 && (setgid(_config->gid) == -1 || setgroups(sizeof(group_list) / sizeof(gid_t), group_list) == -1)) {
|
||||||
|
CHILD_ERROR_EXIT(SETUID_FAILED);
|
||||||
|
}
|
||||||
|
|
||||||
|
// set uid
|
||||||
|
if (_config->uid != -1 && setuid(_config->uid) == -1) {
|
||||||
|
CHILD_ERROR_EXIT(SETUID_FAILED);
|
||||||
|
}
|
||||||
|
|
||||||
|
// load seccomp
|
||||||
|
if (_config->seccomp_rule_name != NULL) {
|
||||||
|
if (strcmp("c_cpp", _config->seccomp_rule_name) == 0) {
|
||||||
|
if (c_cpp_seccomp_rules(_config) != SUCCESS) {
|
||||||
|
CHILD_ERROR_EXIT(LOAD_SECCOMP_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (strcmp("c_cpp_file_io", _config->seccomp_rule_name) == 0) {
|
||||||
|
if (c_cpp_file_io_seccomp_rules(_config) != SUCCESS) {
|
||||||
|
CHILD_ERROR_EXIT(LOAD_SECCOMP_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (strcmp("general", _config->seccomp_rule_name) == 0) {
|
||||||
|
if (general_seccomp_rules(_config) != SUCCESS ) {
|
||||||
|
CHILD_ERROR_EXIT(LOAD_SECCOMP_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (strcmp("golang", _config->seccomp_rule_name) == 0) {
|
||||||
|
if (golang_seccomp_rules(_config) != SUCCESS ) {
|
||||||
|
CHILD_ERROR_EXIT(LOAD_SECCOMP_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (strcmp("node", _config->seccomp_rule_name) == 0) {
|
||||||
|
if (node_seccomp_rules(_config) != SUCCESS ) {
|
||||||
|
CHILD_ERROR_EXIT(LOAD_SECCOMP_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// other rules
|
||||||
|
else {
|
||||||
|
// rule does not exist
|
||||||
|
CHILD_ERROR_EXIT(LOAD_SECCOMP_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
execve(_config->exe_path, _config->args, _config->env);
|
||||||
|
CHILD_ERROR_EXIT(EXECVE_FAILED);
|
||||||
|
}
|
||||||
+24
@@ -0,0 +1,24 @@
|
|||||||
|
#ifndef JUDGER_CHILD_H
|
||||||
|
#define JUDGER_CHILD_H
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include "runner.h"
|
||||||
|
|
||||||
|
#define CHILD_ERROR_EXIT(error_code)\
|
||||||
|
{\
|
||||||
|
LOG_FATAL(log_fp, "Error: System errno: %s; Internal errno: "#error_code, strerror(errno)); \
|
||||||
|
close_file(input_file); \
|
||||||
|
if (output_file == error_file) { \
|
||||||
|
close_file(output_file); \
|
||||||
|
} else { \
|
||||||
|
close_file(output_file); \
|
||||||
|
close_file(error_file); \
|
||||||
|
} \
|
||||||
|
raise(SIGUSR1); \
|
||||||
|
exit(EXIT_FAILURE); \
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void child_process(FILE *log_fp, struct config *_config);
|
||||||
|
|
||||||
|
#endif //JUDGER_CHILD_H
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
#define _POSIX_SOURCE
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
#include "killer.h"
|
||||||
|
|
||||||
|
|
||||||
|
int kill_pid(pid_t pid) {
|
||||||
|
return kill(pid, SIGKILL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void *timeout_killer(void *timeout_killer_args) {
|
||||||
|
// this is a new thread, kill the process if timeout
|
||||||
|
pid_t pid = ((struct timeout_killer_args *)timeout_killer_args)->pid;
|
||||||
|
int timeout = ((struct timeout_killer_args *)timeout_killer_args)->timeout;
|
||||||
|
// On success, pthread_detach() returns 0; on error, it returns an error number.
|
||||||
|
if (pthread_detach(pthread_self()) != 0) {
|
||||||
|
kill_pid(pid);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
// usleep can't be used, for time args must < 1000ms
|
||||||
|
// this may sleep longer that expected, but we will have a check at the end
|
||||||
|
if (sleep((unsigned int)((timeout + 1000) / 1000)) != 0) {
|
||||||
|
kill_pid(pid);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (kill_pid(pid) != 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#ifndef JUDGER_KILLER_H
|
||||||
|
#define JUDGER_KILLER_H
|
||||||
|
|
||||||
|
struct timeout_killer_args {
|
||||||
|
int pid;
|
||||||
|
int timeout;
|
||||||
|
};
|
||||||
|
|
||||||
|
int kill_pid(pid_t pid);
|
||||||
|
|
||||||
|
void *timeout_killer(void *timeout_killer_args);
|
||||||
|
|
||||||
|
#endif //JUDGER_KILLER_H
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
#define _POSIX_SOURCE
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/file.h>
|
||||||
|
|
||||||
|
#include "logger.h"
|
||||||
|
|
||||||
|
#define log_buffer_size 8192
|
||||||
|
|
||||||
|
|
||||||
|
FILE *log_open(const char *filename) {
|
||||||
|
FILE *log_fp = fopen(filename, "a");
|
||||||
|
if (log_fp == NULL) {
|
||||||
|
fprintf(stderr, "can not open log file %s", filename);
|
||||||
|
}
|
||||||
|
return log_fp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void log_close(FILE *log_fp) {
|
||||||
|
if (log_fp != NULL) {
|
||||||
|
fclose(log_fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void log_write(int level, const char *source_filename, const int line, const FILE *log_fp, const char *fmt, ...) {
|
||||||
|
char LOG_LEVEL_NOTE[][10] = {"FATAL", "WARNING", "INFO", "DEBUG"};
|
||||||
|
if (log_fp == NULL) {
|
||||||
|
fprintf(stderr, "can not open log file");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
static char buffer[log_buffer_size];
|
||||||
|
static char log_buffer[log_buffer_size];
|
||||||
|
static char datetime[100];
|
||||||
|
static char line_str[20];
|
||||||
|
static time_t now;
|
||||||
|
now = time(NULL);
|
||||||
|
|
||||||
|
strftime(datetime, 99, "%Y-%m-%d %H:%M:%S", localtime(&now));
|
||||||
|
snprintf(line_str, 19, "%d", line);
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
vsnprintf(log_buffer, log_buffer_size, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
int count = snprintf(buffer, log_buffer_size,
|
||||||
|
"%s [%s] [%s:%s]%s\n",
|
||||||
|
LOG_LEVEL_NOTE[level], datetime, source_filename, line_str, log_buffer);
|
||||||
|
// fprintf(stderr, "%s", buffer);
|
||||||
|
int log_fd = fileno((FILE *) log_fp);
|
||||||
|
if (flock(log_fd, LOCK_EX) == 0) {
|
||||||
|
if (write(log_fd, buffer, (size_t) count) < 0) {
|
||||||
|
fprintf(stderr, "write error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
flock(log_fd, LOCK_UN);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fprintf(stderr, "flock error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#ifndef JUDGER_LOGGER_H
|
||||||
|
#define JUDGER_LOGGER_H
|
||||||
|
|
||||||
|
#define LOG_LEVEL_FATAL 0
|
||||||
|
#define LOG_LEVEL_WARNING 1
|
||||||
|
#define LOG_LEVEL_INFO 2
|
||||||
|
#define LOG_LEVEL_DEBUG 3
|
||||||
|
|
||||||
|
|
||||||
|
FILE *log_open(const char *);
|
||||||
|
|
||||||
|
void log_close(FILE *);
|
||||||
|
|
||||||
|
void log_write(int level, const char *source_filename, const int line_number, const FILE *log_fp, const char *, ...);
|
||||||
|
|
||||||
|
#ifdef JUDGER_DEBUG
|
||||||
|
#define LOG_DEBUG(log_fp, x...) log_write(LOG_LEVEL_DEBUG, __FILE__, __LINE__, log_fp, ##x)
|
||||||
|
#else
|
||||||
|
#define LOG_DEBUG(log_fp, x...)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define LOG_INFO(log_fp, x...) log_write(LOG_LEVEL_INFO, __FILE__, __LINE__, log_fp, ##x)
|
||||||
|
#define LOG_WARNING(log_fp, x...) log_write(LOG_LEVEL_WARNING, __FILE__, __LINE__, log_fp, ##x)
|
||||||
|
#define LOG_FATAL(log_fp, x...) log_write(LOG_LEVEL_FATAL, __FILE__, __LINE__, log_fp, ##x)
|
||||||
|
|
||||||
|
#endif //JUDGER_LOGGER_H
|
||||||
+192
@@ -0,0 +1,192 @@
|
|||||||
|
#include "argtable3.h"
|
||||||
|
#include "runner.h"
|
||||||
|
|
||||||
|
#define INT_PLACE_HOLDER "<n>"
|
||||||
|
#define STR_PLACE_HOLDER "<str>"
|
||||||
|
|
||||||
|
struct arg_lit *verb, *help, *version;
|
||||||
|
struct arg_int *max_cpu_time, *max_real_time, *max_memory, *max_stack, *memory_limit_check_only,
|
||||||
|
*max_process_number, *max_output_size, *uid, *gid;
|
||||||
|
struct arg_str *exe_path, *input_path, *output_path, *error_path, *args, *env, *log_path, *seccomp_rule_name;
|
||||||
|
struct arg_end *end;
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
void *arg_table[] = {
|
||||||
|
help = arg_litn(NULL, "help", 0, 1, "Display This Help And Exit"),
|
||||||
|
version = arg_litn(NULL, "version", 0, 1, "Display Version Info And Exit"),
|
||||||
|
max_cpu_time = arg_intn(NULL, "max_cpu_time", INT_PLACE_HOLDER, 0, 1, "Max CPU Time (ms)"),
|
||||||
|
max_real_time = arg_intn(NULL, "max_real_time", INT_PLACE_HOLDER, 0, 1, "Max Real Time (ms)"),
|
||||||
|
max_memory = arg_intn(NULL, "max_memory", INT_PLACE_HOLDER, 0, 1, "Max Memory (byte)"),
|
||||||
|
memory_limit_check_only = arg_intn(NULL, "memory_limit_check_only", INT_PLACE_HOLDER, 0, 1, "only check memory usage, do not setrlimit (default False)"),
|
||||||
|
max_stack = arg_intn(NULL, "max_stack", INT_PLACE_HOLDER, 0, 1, "Max Stack (byte, default 16M)"),
|
||||||
|
max_process_number = arg_intn(NULL, "max_process_number", INT_PLACE_HOLDER, 0, 1, "Max Process Number"),
|
||||||
|
max_output_size = arg_intn(NULL, "max_output_size", INT_PLACE_HOLDER, 0, 1, "Max Output Size (byte)"),
|
||||||
|
|
||||||
|
exe_path = arg_str1(NULL, "exe_path", STR_PLACE_HOLDER, "Exe Path"),
|
||||||
|
input_path = arg_strn(NULL, "input_path", STR_PLACE_HOLDER, 0, 1, "Input Path"),
|
||||||
|
output_path = arg_strn(NULL, "output_path", STR_PLACE_HOLDER, 0, 1, "Output Path"),
|
||||||
|
error_path = arg_strn(NULL, "error_path", STR_PLACE_HOLDER, 0, 1, "Error Path"),
|
||||||
|
|
||||||
|
args = arg_strn(NULL, "args", STR_PLACE_HOLDER, 0, 255, "Arg"),
|
||||||
|
env = arg_strn(NULL, "env", STR_PLACE_HOLDER, 0, 255, "Env"),
|
||||||
|
|
||||||
|
log_path = arg_strn(NULL, "log_path", STR_PLACE_HOLDER, 0, 1, "Log Path"),
|
||||||
|
seccomp_rule_name = arg_strn(NULL, "seccomp_rule_name", STR_PLACE_HOLDER, 0, 1, "Seccomp Rule Name"),
|
||||||
|
|
||||||
|
uid = arg_intn(NULL, "uid", INT_PLACE_HOLDER, 0, 1, "UID (default 65534)"),
|
||||||
|
gid = arg_intn(NULL, "gid", INT_PLACE_HOLDER, 0, 1, "GID (default 65534)"),
|
||||||
|
|
||||||
|
end = arg_end(10),
|
||||||
|
};
|
||||||
|
|
||||||
|
int exitcode = 0;
|
||||||
|
char name[] = "libjudger.so";
|
||||||
|
|
||||||
|
int nerrors = arg_parse(argc, argv, arg_table);
|
||||||
|
|
||||||
|
if (help->count > 0) {
|
||||||
|
printf("Usage: %s", name);
|
||||||
|
arg_print_syntax(stdout, arg_table, "\n\n");
|
||||||
|
arg_print_glossary(stdout, arg_table, " %-25s %s\n");
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (version->count > 0) {
|
||||||
|
printf("Version: %d.%d.%d\n", (VERSION >> 16) & 0xff, (VERSION >> 8) & 0xff, VERSION & 0xff);
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nerrors > 0) {
|
||||||
|
arg_print_errors(stdout, end, name);
|
||||||
|
printf("Try '%s --help' for more information.\n", name);
|
||||||
|
exitcode = 1;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct config _config;
|
||||||
|
struct result _result = {0, 0, 0, 0, 0, 0, 0};
|
||||||
|
|
||||||
|
if (max_cpu_time->count > 0) {
|
||||||
|
_config.max_cpu_time = *max_cpu_time->ival;
|
||||||
|
} else {
|
||||||
|
_config.max_cpu_time = UNLIMITED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (max_real_time->count > 0) {
|
||||||
|
_config.max_real_time = *max_real_time->ival;
|
||||||
|
} else {
|
||||||
|
_config.max_real_time = UNLIMITED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (max_memory->count > 0) {
|
||||||
|
_config.max_memory = (long) *max_memory->ival;
|
||||||
|
} else {
|
||||||
|
_config.max_memory = UNLIMITED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (memory_limit_check_only->count > 0) {
|
||||||
|
_config.memory_limit_check_only = *memory_limit_check_only->ival == 0 ? 0 : 1;
|
||||||
|
} else {
|
||||||
|
_config.memory_limit_check_only = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (max_stack->count > 0) {
|
||||||
|
_config.max_stack = (long) *max_stack->ival;
|
||||||
|
} else {
|
||||||
|
_config.max_stack = 16 * 1024 * 1024;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (max_process_number->count > 0) {
|
||||||
|
_config.max_process_number = *max_process_number->ival;
|
||||||
|
} else {
|
||||||
|
_config.max_process_number = UNLIMITED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (max_output_size->count > 0) {
|
||||||
|
_config.max_output_size = (long) *max_output_size->ival;
|
||||||
|
} else {
|
||||||
|
_config.max_output_size = UNLIMITED;
|
||||||
|
}
|
||||||
|
|
||||||
|
_config.exe_path = (char *)*exe_path->sval;
|
||||||
|
|
||||||
|
if (input_path->count > 0) {
|
||||||
|
_config.input_path = (char *)input_path->sval[0];
|
||||||
|
} else {
|
||||||
|
_config.input_path = "/dev/stdin";
|
||||||
|
}
|
||||||
|
if (output_path->count > 0) {
|
||||||
|
_config.output_path = (char *)output_path->sval[0];
|
||||||
|
} else {
|
||||||
|
_config.output_path = "/dev/stdout";
|
||||||
|
}
|
||||||
|
if (error_path->count > 0) {
|
||||||
|
_config.error_path = (char *)error_path->sval[0];
|
||||||
|
} else {
|
||||||
|
_config.error_path = "/dev/stderr";
|
||||||
|
}
|
||||||
|
|
||||||
|
_config.args[0] = _config.exe_path;
|
||||||
|
int i = 1;
|
||||||
|
if (args->count > 0) {
|
||||||
|
for (; i < args->count + 1; i++) {
|
||||||
|
_config.args[i] = (char *)args->sval[i - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_config.args[i] = NULL;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (env->count > 0) {
|
||||||
|
for (; i < env->count; i++) {
|
||||||
|
_config.env[i] = (char *)env->sval[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_config.env[i] = NULL;
|
||||||
|
|
||||||
|
if (log_path->count > 0) {
|
||||||
|
_config.log_path = (char *)log_path->sval[0];
|
||||||
|
} else {
|
||||||
|
_config.log_path = "judger.log";
|
||||||
|
}
|
||||||
|
if (seccomp_rule_name->count > 0) {
|
||||||
|
_config.seccomp_rule_name = (char *)seccomp_rule_name->sval[0];
|
||||||
|
} else {
|
||||||
|
_config.seccomp_rule_name = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uid->count > 0) {
|
||||||
|
_config.uid = (uid_t)*(uid->ival);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_config.uid = 65534;
|
||||||
|
}
|
||||||
|
if(gid->count > 0) {
|
||||||
|
_config.gid = (gid_t)*(gid->ival);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_config.gid = 65534;
|
||||||
|
}
|
||||||
|
|
||||||
|
run(&_config, &_result);
|
||||||
|
|
||||||
|
printf("{\n"
|
||||||
|
" \"cpu_time\": %d,\n"
|
||||||
|
" \"real_time\": %d,\n"
|
||||||
|
" \"memory\": %ld,\n"
|
||||||
|
" \"signal\": %d,\n"
|
||||||
|
" \"exit_code\": %d,\n"
|
||||||
|
" \"error\": %d,\n"
|
||||||
|
" \"result\": %d\n"
|
||||||
|
"}",
|
||||||
|
_result.cpu_time,
|
||||||
|
_result.real_time,
|
||||||
|
_result.memory,
|
||||||
|
_result.signal,
|
||||||
|
_result.exit_code,
|
||||||
|
_result.error,
|
||||||
|
_result.result);
|
||||||
|
|
||||||
|
exit:
|
||||||
|
arg_freetable(arg_table, sizeof(arg_table) / sizeof(arg_table[0]));
|
||||||
|
return exitcode;
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <seccomp.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#include "../runner.h"
|
||||||
|
|
||||||
|
|
||||||
|
int _c_cpp_seccomp_rules(struct config *_config, bool allow_write_file) {
|
||||||
|
int syscalls_whitelist[] = {
|
||||||
|
SCMP_SYS(access),
|
||||||
|
SCMP_SYS(arch_prctl),
|
||||||
|
SCMP_SYS(brk),
|
||||||
|
SCMP_SYS(clock_gettime),
|
||||||
|
SCMP_SYS(close),
|
||||||
|
SCMP_SYS(exit_group),
|
||||||
|
SCMP_SYS(faccessat),
|
||||||
|
SCMP_SYS(fstat),
|
||||||
|
SCMP_SYS(futex),
|
||||||
|
SCMP_SYS(getrandom),
|
||||||
|
SCMP_SYS(lseek),
|
||||||
|
SCMP_SYS(mmap),
|
||||||
|
SCMP_SYS(mprotect),
|
||||||
|
SCMP_SYS(munmap),
|
||||||
|
SCMP_SYS(newfstatat),
|
||||||
|
SCMP_SYS(pread64),
|
||||||
|
SCMP_SYS(prlimit64),
|
||||||
|
SCMP_SYS(read),
|
||||||
|
SCMP_SYS(readlink),
|
||||||
|
SCMP_SYS(readv),
|
||||||
|
SCMP_SYS(rseq),
|
||||||
|
SCMP_SYS(set_robust_list),
|
||||||
|
SCMP_SYS(set_tid_address),
|
||||||
|
SCMP_SYS(write),
|
||||||
|
SCMP_SYS(writev)
|
||||||
|
};
|
||||||
|
|
||||||
|
int syscalls_whitelist_length = sizeof(syscalls_whitelist) / sizeof(int);
|
||||||
|
scmp_filter_ctx ctx = NULL;
|
||||||
|
|
||||||
|
// load seccomp rules
|
||||||
|
ctx = seccomp_init(SCMP_ACT_KILL);
|
||||||
|
if (!ctx) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < syscalls_whitelist_length; i++) {
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, syscalls_whitelist[i], 0) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// extra rule for execve
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(execve), 1, SCMP_A0(SCMP_CMP_EQ, (scmp_datum_t)(_config->exe_path))) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allow_write_file) {
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 0) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(openat), 0) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(dup), 0) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(dup2), 0) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(dup3), 0) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// do not allow "w" and "rw"
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1, SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY | O_RDWR, 0)) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(openat), 1, SCMP_CMP(2, SCMP_CMP_MASKED_EQ, O_WRONLY | O_RDWR, 0)) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (seccomp_load(ctx) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
seccomp_release(ctx);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int c_cpp_seccomp_rules(struct config *_config, bool allow_write_file) {
|
||||||
|
return _c_cpp_seccomp_rules(_config, false);
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
#include "seccomp_rules.h"
|
||||||
|
|
||||||
|
|
||||||
|
int c_cpp_file_io_seccomp_rules(struct config *_config) {
|
||||||
|
return _c_cpp_seccomp_rules(_config, true);
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <seccomp.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "../runner.h"
|
||||||
|
|
||||||
|
|
||||||
|
int general_seccomp_rules(struct config *_config) {
|
||||||
|
int syscalls_blacklist[] = {SCMP_SYS(clone),
|
||||||
|
SCMP_SYS(fork), SCMP_SYS(vfork),
|
||||||
|
SCMP_SYS(kill),
|
||||||
|
#ifdef __NR_execveat
|
||||||
|
SCMP_SYS(execveat)
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
int syscalls_blacklist_length = sizeof(syscalls_blacklist) / sizeof(int);
|
||||||
|
scmp_filter_ctx ctx = NULL;
|
||||||
|
// load seccomp rules
|
||||||
|
ctx = seccomp_init(SCMP_ACT_ALLOW);
|
||||||
|
if (!ctx) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < syscalls_blacklist_length; i++) {
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, syscalls_blacklist[i], 0) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// use SCMP_ACT_KILL for socket, python will be killed immediately
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(socket), 0) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
// add extra rule for execve
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(execve), 1, SCMP_A0(SCMP_CMP_NE, (scmp_datum_t)(_config->exe_path))) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
// do not allow "w" and "rw" using open
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(open), 1, SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY, O_WRONLY)) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(open), 1, SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_RDWR, O_RDWR)) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
// do not allow "w" and "rw" using openat
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(openat), 1, SCMP_CMP(2, SCMP_CMP_MASKED_EQ, O_WRONLY, O_WRONLY)) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(openat), 1, SCMP_CMP(2, SCMP_CMP_MASKED_EQ, O_RDWR, O_RDWR)) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (seccomp_load(ctx) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
seccomp_release(ctx);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <seccomp.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "../runner.h"
|
||||||
|
|
||||||
|
|
||||||
|
int golang_seccomp_rules(struct config *_config) {
|
||||||
|
int syscalls_blacklist[] = {SCMP_SYS(socket),
|
||||||
|
SCMP_SYS(fork), SCMP_SYS(vfork),
|
||||||
|
SCMP_SYS(kill),
|
||||||
|
#ifdef __NR_execveat
|
||||||
|
SCMP_SYS(execveat)
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
int syscalls_blacklist_length = sizeof(syscalls_blacklist) / sizeof(int);
|
||||||
|
scmp_filter_ctx ctx = NULL;
|
||||||
|
// load seccomp rules
|
||||||
|
ctx = seccomp_init(SCMP_ACT_ALLOW);
|
||||||
|
if (!ctx) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < syscalls_blacklist_length; i++) {
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, syscalls_blacklist[i], 0) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// do not allow "w" and "rw" using open
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(open), 1, SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY, O_WRONLY)) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(open), 1, SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_RDWR, O_RDWR)) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
// do not allow "w" and "rw" using openat
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(openat), 1, SCMP_CMP(2, SCMP_CMP_MASKED_EQ, O_WRONLY, O_WRONLY)) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(openat), 1, SCMP_CMP(2, SCMP_CMP_MASKED_EQ, O_RDWR, O_RDWR)) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (seccomp_load(ctx) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
seccomp_release(ctx);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <seccomp.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "../runner.h"
|
||||||
|
|
||||||
|
|
||||||
|
int node_seccomp_rules(struct config *_config) {
|
||||||
|
int syscalls_blacklist[] = {SCMP_SYS(socket),
|
||||||
|
SCMP_SYS(fork), SCMP_SYS(vfork),
|
||||||
|
SCMP_SYS(kill),
|
||||||
|
#ifdef __NR_execveat
|
||||||
|
SCMP_SYS(execveat)
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
int syscalls_blacklist_length = sizeof(syscalls_blacklist) / sizeof(int);
|
||||||
|
scmp_filter_ctx ctx = NULL;
|
||||||
|
// load seccomp rules
|
||||||
|
ctx = seccomp_init(SCMP_ACT_ALLOW);
|
||||||
|
if (!ctx) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < syscalls_blacklist_length; i++) {
|
||||||
|
if (seccomp_rule_add(ctx, SCMP_ACT_KILL, syscalls_blacklist[i], 0) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (seccomp_load(ctx) != 0) {
|
||||||
|
return LOAD_SECCOMP_FAILED;
|
||||||
|
}
|
||||||
|
seccomp_release(ctx);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#ifndef JUDGER_SECCOMP_RULES_H
|
||||||
|
#define JUDGER_SECCOMP_RULES_H
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../runner.h"
|
||||||
|
|
||||||
|
int _c_cpp_seccomp_rules(struct config *_config, bool allow_write_file);
|
||||||
|
int c_cpp_seccomp_rules(struct config *_config);
|
||||||
|
int general_seccomp_rules(struct config *_config);
|
||||||
|
int c_cpp_file_io_seccomp_rules(struct config *_config);
|
||||||
|
int golang_seccomp_rules(struct config *_config);
|
||||||
|
int node_seccomp_rules(struct config *_config);
|
||||||
|
|
||||||
|
#endif //JUDGER_SECCOMP_RULES_H
|
||||||
+143
@@ -0,0 +1,143 @@
|
|||||||
|
#define _GNU_SOURCE
|
||||||
|
#define _POSIX_SOURCE
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sched.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <sys/resource.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include "runner.h"
|
||||||
|
#include "killer.h"
|
||||||
|
#include "child.h"
|
||||||
|
#include "logger.h"
|
||||||
|
|
||||||
|
void init_result(struct result *_result) {
|
||||||
|
_result->result = _result->error = SUCCESS;
|
||||||
|
_result->cpu_time = _result->real_time = _result->signal = _result->exit_code = 0;
|
||||||
|
_result->memory = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void run(struct config *_config, struct result *_result) {
|
||||||
|
// init log fp
|
||||||
|
FILE *log_fp = log_open(_config->log_path);
|
||||||
|
|
||||||
|
// init result
|
||||||
|
init_result(_result);
|
||||||
|
|
||||||
|
// check whether current user is root
|
||||||
|
uid_t uid = getuid();
|
||||||
|
if (uid != 0) {
|
||||||
|
ERROR_EXIT(ROOT_REQUIRED);
|
||||||
|
}
|
||||||
|
|
||||||
|
// check args
|
||||||
|
if ((_config->max_cpu_time < 1 && _config->max_cpu_time != UNLIMITED) ||
|
||||||
|
(_config->max_real_time < 1 && _config->max_real_time != UNLIMITED) ||
|
||||||
|
(_config->max_stack < 1) ||
|
||||||
|
(_config->max_memory < 1 && _config->max_memory != UNLIMITED) ||
|
||||||
|
(_config->max_process_number < 1 && _config->max_process_number != UNLIMITED) ||
|
||||||
|
(_config->max_output_size < 1 && _config->max_output_size != UNLIMITED)) {
|
||||||
|
ERROR_EXIT(INVALID_CONFIG);
|
||||||
|
}
|
||||||
|
|
||||||
|
// record current time
|
||||||
|
struct timeval start, end;
|
||||||
|
gettimeofday(&start, NULL);
|
||||||
|
|
||||||
|
pid_t child_pid = fork();
|
||||||
|
|
||||||
|
// pid < 0 shows clone failed
|
||||||
|
if (child_pid < 0) {
|
||||||
|
ERROR_EXIT(FORK_FAILED);
|
||||||
|
}
|
||||||
|
else if (child_pid == 0) {
|
||||||
|
child_process(log_fp, _config);
|
||||||
|
}
|
||||||
|
else if (child_pid > 0){
|
||||||
|
// create new thread to monitor process running time
|
||||||
|
pthread_t tid = 0;
|
||||||
|
if (_config->max_real_time != UNLIMITED) {
|
||||||
|
struct timeout_killer_args killer_args;
|
||||||
|
|
||||||
|
killer_args.timeout = _config->max_real_time;
|
||||||
|
killer_args.pid = child_pid;
|
||||||
|
if (pthread_create(&tid, NULL, timeout_killer, (void *) (&killer_args)) != 0) {
|
||||||
|
kill_pid(child_pid);
|
||||||
|
ERROR_EXIT(PTHREAD_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int status;
|
||||||
|
struct rusage resource_usage;
|
||||||
|
|
||||||
|
// wait for child process to terminate
|
||||||
|
// on success, returns the process ID of the child whose state has changed;
|
||||||
|
// On error, -1 is returned.
|
||||||
|
if (wait4(child_pid, &status, WSTOPPED, &resource_usage) == -1) {
|
||||||
|
kill_pid(child_pid);
|
||||||
|
ERROR_EXIT(WAIT_FAILED);
|
||||||
|
}
|
||||||
|
// get end time
|
||||||
|
gettimeofday(&end, NULL);
|
||||||
|
_result->real_time = (int) (end.tv_sec * 1000 + end.tv_usec / 1000 - start.tv_sec * 1000 - start.tv_usec / 1000);
|
||||||
|
|
||||||
|
// process exited, we may need to cancel timeout killer thread
|
||||||
|
if (_config->max_real_time != UNLIMITED) {
|
||||||
|
if (pthread_cancel(tid) != 0) {
|
||||||
|
// todo logging
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (WIFSIGNALED(status) != 0) {
|
||||||
|
_result->signal = WTERMSIG(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(_result->signal == SIGUSR1) {
|
||||||
|
_result->result = SYSTEM_ERROR;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_result->exit_code = WEXITSTATUS(status);
|
||||||
|
_result->cpu_time = (int) (resource_usage.ru_utime.tv_sec * 1000 +
|
||||||
|
resource_usage.ru_utime.tv_usec / 1000);
|
||||||
|
_result->memory = resource_usage.ru_maxrss * 1024;
|
||||||
|
|
||||||
|
if (_result->exit_code != 0) {
|
||||||
|
_result->result = RUNTIME_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_result->signal == SIGSEGV) {
|
||||||
|
if (_config->max_memory != UNLIMITED && _result->memory > _config->max_memory) {
|
||||||
|
_result->result = MEMORY_LIMIT_EXCEEDED;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_result->result = RUNTIME_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (_result->signal != 0) {
|
||||||
|
_result->result = RUNTIME_ERROR;
|
||||||
|
}
|
||||||
|
if (_config->max_memory != UNLIMITED && _result->memory > _config->max_memory) {
|
||||||
|
_result->result = MEMORY_LIMIT_EXCEEDED;
|
||||||
|
}
|
||||||
|
if (_config->max_real_time != UNLIMITED && _result->real_time > _config->max_real_time) {
|
||||||
|
_result->result = REAL_TIME_LIMIT_EXCEEDED;
|
||||||
|
}
|
||||||
|
if (_config->max_cpu_time != UNLIMITED && _result->cpu_time > _config->max_cpu_time) {
|
||||||
|
_result->result = CPU_TIME_LIMIT_EXCEEDED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log_close(log_fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
#ifndef JUDGER_RUNNER_H
|
||||||
|
#define JUDGER_RUNNER_H
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// (ver >> 16) & 0xff, (ver >> 8) & 0xff, ver & 0xff -> real version
|
||||||
|
#define VERSION 0x020101
|
||||||
|
|
||||||
|
#define UNLIMITED -1
|
||||||
|
|
||||||
|
#define LOG_ERROR(error_code) LOG_FATAL(log_fp, "Error: "#error_code);
|
||||||
|
|
||||||
|
#define ERROR_EXIT(error_code)\
|
||||||
|
{\
|
||||||
|
LOG_ERROR(error_code); \
|
||||||
|
_result->error = error_code; \
|
||||||
|
log_close(log_fp); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ARGS_MAX_NUMBER 256
|
||||||
|
#define ENV_MAX_NUMBER 256
|
||||||
|
|
||||||
|
|
||||||
|
enum {
|
||||||
|
SUCCESS = 0,
|
||||||
|
INVALID_CONFIG = -1,
|
||||||
|
FORK_FAILED = -2,
|
||||||
|
PTHREAD_FAILED = -3,
|
||||||
|
WAIT_FAILED = -4,
|
||||||
|
ROOT_REQUIRED = -5,
|
||||||
|
LOAD_SECCOMP_FAILED = -6,
|
||||||
|
SETRLIMIT_FAILED = -7,
|
||||||
|
DUP2_FAILED = -8,
|
||||||
|
SETUID_FAILED = -9,
|
||||||
|
EXECVE_FAILED = -10,
|
||||||
|
SPJ_ERROR = -11
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct config {
|
||||||
|
int max_cpu_time;
|
||||||
|
int max_real_time;
|
||||||
|
long max_memory;
|
||||||
|
long max_stack;
|
||||||
|
int max_process_number;
|
||||||
|
long max_output_size;
|
||||||
|
int memory_limit_check_only;
|
||||||
|
char *exe_path;
|
||||||
|
char *input_path;
|
||||||
|
char *output_path;
|
||||||
|
char *error_path;
|
||||||
|
char *args[ARGS_MAX_NUMBER];
|
||||||
|
char *env[ENV_MAX_NUMBER];
|
||||||
|
char *log_path;
|
||||||
|
char *seccomp_rule_name;
|
||||||
|
uid_t uid;
|
||||||
|
gid_t gid;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
enum {
|
||||||
|
WRONG_ANSWER = -1,
|
||||||
|
CPU_TIME_LIMIT_EXCEEDED = 1,
|
||||||
|
REAL_TIME_LIMIT_EXCEEDED = 2,
|
||||||
|
MEMORY_LIMIT_EXCEEDED = 3,
|
||||||
|
RUNTIME_ERROR = 4,
|
||||||
|
SYSTEM_ERROR = 5
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct result {
|
||||||
|
int cpu_time;
|
||||||
|
int real_time;
|
||||||
|
long memory;
|
||||||
|
int signal;
|
||||||
|
int exit_code;
|
||||||
|
int error;
|
||||||
|
int result;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void run(struct config *, struct result *);
|
||||||
|
#endif //JUDGER_RUNNER_H
|
||||||
@@ -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