├── README.md ├── compare ├── bootstrap.min.css ├── compare.sh ├── diff2html.py ├── readme.md ├── styles.css └── template.html ├── main.sh ├── runcode.sh ├── sandbox ├── Makefile ├── README.md ├── malloc.c ├── sandbox.c └── sandbox.sh ├── shield ├── blacklist_c.h ├── blacklist_cpp.h ├── java.policy ├── java_exceptions_list ├── readme.md ├── shield.sh ├── shield_c.c ├── shield_cpp.cpp ├── shield_python2.py └── shield_python3.py ├── tests ├── 1-java-hello │ ├── HelloWorld.java │ └── expected.txt ├── 10055-hashmat │ ├── Main.class │ ├── Main.java │ ├── expected.txt │ └── input.txt ├── 10194-football │ ├── Football.cpp │ ├── Sandbox.so │ ├── expected.txt │ ├── input.txt │ └── sandbox.so ├── 10420-list-of-conquests │ ├── Conquests.cpp │ ├── expected.txt │ └── input.txt ├── 10474-where-is-the-marble │ ├── Marble.cpp │ ├── expected.txt │ └── input.txt ├── 10785-the-mad-numerologist │ ├── Numerologist.cpp │ ├── expected.txt │ └── input.txt ├── 10879-code-refactoring │ ├── Code.c │ ├── expected.txt │ └── input.txt ├── 113-power-of-cryptography │ ├── Power.cpp │ ├── expected.txt │ └── input.txt ├── 123-searching-quickly │ ├── Searching.cpp │ ├── expected.txt │ └── input.txt ├── 2-java-arithmetic │ ├── Arithmetic.class │ ├── Arithmetic.java │ └── output.txt ├── 340-master-mind-hints │ ├── Master.cpp │ ├── expected.txt │ └── input.txt ├── 400-unix-ls │ ├── Unix.cpp │ ├── expected.txt │ └── input.txt └── 755-487-3279 │ ├── 755.cpp │ ├── expected.txt │ └── input.txt └── web ├── controllers └── Language.php └── views └── language └── newLanguage.php /README.md: -------------------------------------------------------------------------------- 1 | ## core-judge 2 | 3 | Core Judge is a set of modules open source to evaluate C, C++, Python and Java scripts. 4 | 5 | These scripts were design to any web interface. Python in Judge is not sandboxed yet. Just a low level of security is provided for python. 6 | 7 | If you want to use Core Judge for python, USE IT AT YOUR OWN RISK or provide sandboxing yourself. 8 | 9 | ### Features 10 | 11 | * Support for `C`, `C++`, `Python2`, `Python3` and `Java`. 12 | * Shield in searching of malicious lines in the source code to evaluate. 13 | * Sandboxing to C/C++ use [EasySandbox](https://github.com/daveho/EasySandbox). 14 | * Sandboxing _(not yet for python)_ 15 | * Output Comparison using `diff` and `diff2html` for checking output correctness. 16 | * Output Comparison in `HTML5` using `bootstrap 3`. 17 | * Simple answer! 18 | * ... 19 | 20 | ### Requirements 21 | 22 | For running core judge, a Linux server with following requirements is needed: 23 | 24 | * Tools for compiling and running codes (`gcc`, `g++`, `javac`, `java`, `python2` and `python3` commands). 25 | 26 | ### Usage 27 | 28 | ```sh 29 | ./main.sh /FULL_PATH/PROBLEM FLAG MEMLIMIT TIMELIMIT SHIELD SANDBOX COMPARE DIFF2HTML JAVA_POLICY DISPLAY_JAVA_EXCEPTION_ON 30 | ``` 31 | Example: 32 | ```sh 33 | ./main.sh /home/user/desktop/core-judge/tests/123-searching-quickly/Searching.cpp --cpp 5120 30 1 0 1 1 1 1 34 | ``` 35 | * Flags: [ --c, --cpp, --py2, --py3, --java] 36 | * MemoryLimit in kbs. 5120 kb (5MB). 37 | * TimeLimit in seconds. 90 (1m:30s) 38 | * Shield 0 - 1 39 | * Sandbox 0 - 1 40 | * Compare 0 - 1 41 | * Diff2html 0 - 1 42 | * Java policy 0 - 1 43 | * Display java exception 0 - 1 44 | 45 | ## License 46 | 47 | The MIT License (MIT) 48 | 49 | Copyright (c) 2015 Hector Jose Flores Colmenarez 50 | 51 | Permission is hereby granted, free of charge, to any person obtaining a copy 52 | of this software and associated documentation files (the "Software"), to deal 53 | in the Software without restriction, including without limitation the rights 54 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 55 | copies of the Software, and to permit persons to whom the Software is 56 | furnished to do so, subject to the following conditions: 57 | 58 | The above copyright notice and this permission notice shall be included in 59 | all copies or substantial portions of the Software. 60 | 61 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 62 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 63 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 64 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 65 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 66 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 67 | THE SOFTWARE. -------------------------------------------------------------------------------- /compare/compare.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ***************************************** 4 | # * FILE: compare.sh * 5 | # * AUTOR: Hector Jose Flores Colmenarez * 6 | # * EMAIL: hecto932@gmail.com * 7 | # ***************************************** 8 | 9 | # FUNCTIONALITY 10 | # ============== 11 | 12 | # THE COMPARE MUST BE RUN THIS WAY: 13 | # ./compare.sh file1 file2 diff2html 14 | # ./compare.sh file1 file2 15 | 16 | # FLAGS: 17 | # ===== 18 | # --diff2html WITH THIS FLAG WILL HAVE HTML OUTPUT 19 | 20 | # STATUS CODE 21 | # =========== 22 | # 0 - Files = 23 | # 1 - Files != 24 | # 2 - Some File doesn't exist 25 | # 3 - Error 26 | 27 | # GLOBALS 28 | # ======= 29 | DIFF_RESULT="-1" 30 | SCRIPT=$(readlink -f "$0") 31 | SCRIPTPATH=$(dirname "$SCRIPT") 32 | 33 | # GETTINGS ARGUMENTS 34 | # ================== 35 | 36 | # 1.- FILENAME FILE1 37 | FILE1=${1} 38 | FILE1NAME=${FILE1##*/} 39 | FILE1NAME="${FILE1NAME%.*}" 40 | 41 | # 2.- FILENAME FILE2 42 | FILE2=${2} 43 | FILE2NAME=${FILE2##*/} 44 | FILE2NAME="${FILE2NAME%.*}" 45 | 46 | # 3.- FLAG 47 | FLAG=${3} 48 | 49 | # 4.- FILENAME DIFF2HTML 50 | FILENAME="diffrestult.html" 51 | 52 | # 5.- DIRECTORY OF FILE 1 53 | DIRECTORY=$(dirname "$FILE1") 54 | 55 | # 6.- DIRECTORY NAME 56 | DIRECTORY_NAME="diff_"$FILE1NAME"_"$FILE2NAME.html 57 | 58 | # 7.- EXIT_CODE 59 | EXIT_CODE=3 60 | 61 | ################# FUNCTIONS ################# 62 | function rmAssets 63 | { 64 | rm $DIRECTORY/bootstrap.min.css >/dev/null 2>/dev/null 65 | rm $DIRECTORY/styles.css >/dev/null 2>/dev/null 66 | } 67 | 68 | if [[ ! -f "$FILE1" || ! -f "$FILE2" ]]; then 69 | echo "$FILE1NAME or $FILE2NAME doesn't exist." 70 | EXIT_CODE=2 71 | else 72 | 73 | # JUST COMPARE USING DIFF 74 | if [[ -z "$FLAG" ]]; then 75 | diff $FILE1 $FILE2 > /dev/null 2>&1 76 | EXIT_CODE=$? 77 | else 78 | rmAssets 79 | cp $SCRIPTPATH/bootstrap.min.css $DIRECTORY >/dev/null 2>/dev/null 80 | cp $SCRIPTPATH/styles.css $DIRECTORY >/dev/null 2>/dev/null 81 | python $SCRIPTPATH/diff2html.py $FILE1 $FILE2 > $DIRECTORY/$FILENAME 2>/dev/null 82 | diff $FILE1 $FILE2 > /dev/null 2>&1 83 | EXIT_CODE=$? 84 | fi 85 | fi 86 | 87 | echo $EXIT_CODE 88 | exit $EXIT_CODE -------------------------------------------------------------------------------- /compare/diff2html.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # ***************************************** 4 | # * FILE: diff2html.py * 5 | # * AUTOR: Hector Jose Flores Colmenarez * 6 | # * EMAIL: hecto932@gmail.com * 7 | # ***************************************** 8 | 9 | #LIBRARIES 10 | import sys 11 | import os 12 | import string 13 | import re 14 | import time 15 | import stat 16 | 17 | def str2html(s) : 18 | s1 = string.replace(string.rstrip(s), "&", "&") 19 | if ( len(s1) == 0 ) : return ( s1 ) ; 20 | s1 = string.replace(s1, "<", "<") 21 | s1 = string.replace(s1, ">", ">") 22 | i = 0 23 | s2 = "" 24 | while ( s1[i] == " " ) : 25 | s2 += " " 26 | i += 1 27 | s2 += s1[i:] 28 | return ( s2 ) 29 | 30 | # MAIN 31 | if ( __name__ == "__main__" ) : 32 | 33 | # PROCESS COMAND-LINE OPTIONS 34 | cmd_line = string.join(sys.argv) 35 | 36 | for ind_opt in range(len(sys.argv)) : 37 | if ( sys.argv[ind_opt] == "--help" ) : 38 | print_usage() 39 | sys.exit(0) 40 | 41 | external_css = "" 42 | ind_css = -1 43 | ind_chg = -1 44 | only_changes = 0 45 | argv = tuple(sys.argv) 46 | for ind_opt in range(len(argv)) : 47 | if ( argv[ind_opt] == "--style-sheet" ) : 48 | ind_css = in_opt 49 | external_css = argv[ind_css+1] 50 | if ( argv[ind_opt] == "--only-changes" ) : 51 | ind_chg = ind_opt 52 | only_changes = 1 53 | 54 | argv = list(argv) 55 | if ( ind_css >= 0 ) : 56 | del argv[ind_css:ind_css+2] 57 | if ( ind_chg >= 0 ) : 58 | del argv[ind_chg:ind_chg+1] 59 | 60 | # VERIFY IF BOTH FILES EXISTS 61 | file1 = argv[-2] 62 | file2 = argv[-1] 63 | if not os.access(file1, os.F_OK) : 64 | print "File %s does not exist or is not readable, aborting." % file1 65 | sys.exit(1) 66 | if not os.access(file2, os.F_OK) : 67 | print "File %s does not exist or is not readable, aborting." % file2 68 | sys.exit(1) 69 | 70 | # INVOKES "DIFF" 71 | diff_stdout = os.popen("diff %s" % string.join(argv[1:]), "r") 72 | diff_output = diff_stdout.readlines() 73 | diff_stdout.close() 74 | # REPORTED DIFFERENCES 75 | changed = {} 76 | deleted = {} 77 | added = {} 78 | # MAGIC REGULAR EXPRESSION 79 | diff_re = re.compile( 80 | r"^(?P\d+)(,(?P\d+))?"+ \ 81 | "(?P[acd])"+ \ 82 | "(?P\d+)(,(?P\d+))?") 83 | for diff_line in diff_output: 84 | diffs = diff_re.match(string.strip(diff_line)) 85 | # If the line doesn't match, it's useless for us 86 | if not ( diffs == None ) : 87 | f1_start = int(diffs.group("f1_start")) 88 | if ( diffs.group("f1_end") == None ) : 89 | f1_end = f1_start 90 | else : 91 | f1_end = int(diffs.group("f1_end")) 92 | f2_start = int(diffs.group("f2_start")) 93 | if ( diffs.group("f2_end") == None ) : 94 | f2_end = f2_start 95 | else : 96 | f2_end = int(diffs.group("f2_end")) 97 | f1_nb = (f1_end - f1_start) + 1 98 | f2_nb = (f2_end - f2_start) + 1 99 | if ( diffs.group("diff") == "c" ) : 100 | if ( f2_nb < f1_nb ) : 101 | for lf1 in range(f1_start, f1_start+f2_nb) : 102 | changed[lf1] = 0 103 | for lf1 in range(f1_start+f2_nb, f1_end+1) : 104 | deleted[lf1] = 0 105 | elif ( f1_nb < f2_nb ) : 106 | for lf1 in range(f1_start, f1_end+1) : 107 | changed[lf1] = 0 108 | for lf2 in range(f2_start+f1_nb, f2_end+1) : 109 | added[lf2] = 0 110 | else : 111 | for lf1 in range(f1_start, f1_end+1) : 112 | changed[lf1] = 0 113 | elif ( diffs.group("diff") == "a" ) : 114 | for lf2 in range(f2_start, f2_end+1): 115 | added[lf2] = 0 116 | else : 117 | for lf1 in range(f1_start, f1_end+1) : 118 | deleted[lf1] = 0 119 | 120 | f1 = open(file1, "r") 121 | f1_lines = f1.readlines() 122 | f1.close() 123 | f2 = open(file2, "r") 124 | f2_lines = f2.readlines() 125 | f2.close() 126 | 127 | f1_stat = os.stat(file1) 128 | f2_stat = os.stat(file2) 129 | 130 | if ( len(changed) == 0 ) : 131 | changed_lnks = "None" 132 | else : 133 | changed_lnks = "" 134 | keys = changed.keys() 135 | keys.sort() 136 | for key in keys : 137 | changed_lnks += "%d, " % (key, key) 138 | changed_lnks = changed_lnks[:-2] 139 | 140 | if ( len(added) == 0 ) : 141 | added_lnks = "None" 142 | else : 143 | added_lnks = "" 144 | keys = added.keys() 145 | keys.sort() 146 | for key in keys : 147 | added_lnks += "%d, " % (key, key) 148 | added_lnks = added_lnks[:-2] 149 | 150 | if ( len(deleted) == 0 ) : 151 | deleted_lnks = "None" 152 | else : 153 | deleted_lnks = "" 154 | keys = deleted.keys() 155 | keys.sort() 156 | for key in keys : 157 | deleted_lnks += "%d, " % (key, key) 158 | deleted_lnks = deleted_lnks[:-2] 159 | 160 | print """ 161 | 162 | 163 | 164 | 165 | 166 | 167 | Differences between %s and %s 168 | 169 | 170 | """ % (file1, file2) 171 | print """ 172 | 173 | 174 |
175 |
176 |

diff2html

177 |
178 |
179 | 180 | 181 | 197 | 198 | 199 |
182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 |
Modified lines: %s
Added line: %s
Removed line: %s
196 |
200 |
201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 217 | 218 | 219 | 225 | 226 | """ % (changed_lnks, added_lnks, deleted_lnks, 227 | file1, file2, 228 | len(f1_lines), f1_stat[stat.ST_SIZE], 229 | time.asctime(time.gmtime(f1_stat[stat.ST_MTIME])), 230 | len(f2_lines), f2_stat[stat.ST_SIZE], 231 | time.asctime(time.gmtime(f2_stat[stat.ST_MTIME]))) 232 | 233 | nl1 = nl2 = 0 234 | while not ( (nl1 >= len(f1_lines)) and (nl2 >= len(f2_lines)) ) : 235 | if ( added.has_key(nl2+1) ) : 236 | f2_lines[nl2] 237 | # This is an added line 238 | print """ 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | """ % (nl2+1, nl2+1, str2html(f2_lines[nl2])) 247 | nl2 += 1 248 | elif ( deleted.has_key(nl1+1) ) : 249 | print """ 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | """ % (nl1+1, nl1+1, str2html(f1_lines[nl1])) 258 | nl1 += 1 259 | elif ( changed.has_key(nl1+1) ) : 260 | print """ 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | """ % (nl1+1, nl1+1, str2html(f1_lines[nl1]), 269 | nl2+1, str2html(f2_lines[nl2])) 270 | nl1 += 1 271 | nl2 += 1 272 | else : 273 | if ( not only_changes ) : 274 | print """ 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | """ % (nl1+1, str2html(f1_lines[nl1]), 283 | nl2+1, str2html(f2_lines[nl2])) 284 | nl1 += 1 285 | nl2 += 1 286 | 287 | print """ 288 |
 %s  %s
  212 | %d lines
213 | %d bytes
214 | Last modified : %s
215 |
216 |
   220 | %d lines
221 | %d bytes
222 | Last modified : %s
223 |
224 |
   %d%s
%d%s   
%d%s %d%s
%d%s %d%s
289 |
290 |

291 | Generated by diff2html on %s 292 |
293 | Command-line:

295 | 298 | 299 | 300 | """ % (time.asctime(time.gmtime(time.time())), cmd_line) -------------------------------------------------------------------------------- /compare/readme.md: -------------------------------------------------------------------------------- 1 | ## Compare 2 | 3 | Core judge uses diff to compare the outputs with the option to show the differences by an html file designed on Bootstrap 3 with some additional styles. 4 | 5 | Diff is a linux command used for compare two files. In this case, core judge use diff to compare the output of source code with output expected. 6 | 7 | ### Usage 8 | 9 | ```sh 10 | $ ./compare /full_path/file1 /full_path/file2 --diff2html(optional) 11 | ``` 12 | * diff2html generate an HTML file with the differences(optional). 13 | 14 | #### Status Codes 15 | * 0 - Files are equal. 16 | * 1 - Files are different. 17 | * 2 - Some file don't exist. 18 | * 3 - Unexpected error. 19 | 20 | -------------------------------------------------------------------------------- /compare/styles.css: -------------------------------------------------------------------------------- 1 | body 2 | { 3 | font-family: Ubuntu, "times new roman", times, roman, serif; 4 | font-size: 16px; 5 | } 6 | 7 | table { border-collapse: collapse; border-spacing: 0px; margin: 0px auto;} 8 | 9 | td.linenum 10 | { 11 | color: #909090; 12 | text-align: right; 13 | vertical-align: top; 14 | font-weight: bold; 15 | border-right: 1px solid black; 16 | border-left: 1px solid black; 17 | text-align: center; 18 | } 19 | 20 | .bg-color 21 | { 22 | background: #f3f3f4; 23 | } 24 | 25 | .header-green 26 | { 27 | background: #1ab394; 28 | border-radius: 7px 7px 0px 0px; 29 | } 30 | 31 | .title 32 | { 33 | font-size: 3em; 34 | color: white; 35 | text-align: center; 36 | } 37 | 38 | footer 39 | { 40 | text-align: center; 41 | } 42 | 43 | section 44 | { 45 | background: white; 46 | } 47 | 48 | .file1, .file2 49 | { 50 | text-align: center; 51 | color: gray; 52 | } 53 | 54 | .log-file 55 | { 56 | text-align: center; 57 | } 58 | 59 | .color-added 60 | { 61 | background: #DDDDFF; 62 | } 63 | 64 | .color-delete 65 | { 66 | background: #FFCCCC; 67 | 68 | } 69 | 70 | .color-modified 71 | { 72 | background: #BBFFBB; 73 | } 74 | 75 | .color-ok 76 | { 77 | background: #FFFFE1; 78 | } -------------------------------------------------------------------------------- /compare/template.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Template Diff2html 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 |
17 |

diff2html

18 |
19 |
20 |
21 |
22 |
23 |

file1

24 |
25 |

3 lines

26 |

7 bytes

27 |

Last modified Thu Jul 23 05:53:30 2015

28 |
29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 |
#
12
24
310
4
5
6
63 |
64 |
65 |

file2

66 |
67 |

6 lines

68 |

12 bytes

69 |

Last modified Thu Jul 23 05:59:56 2015

70 |
71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 |
#
12
24
3100
45
5
65
105 |
106 |
107 |

108 | Generated by diff2html on Thu Jul 23 07:22:01 2015 109 | Command-line: ./diff2html file1 file2 110 |

111 |
112 | 115 |
116 | 117 | -------------------------------------------------------------------------------- /main.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ***************************************** 4 | # * FILE: main.sh * 5 | # * AUTOR: Hector Jose Flores Colmenarez * 6 | # * EMAIL: hecto932@gmail.com * 7 | # ***************************************** 8 | 9 | # USAGE : ./main.sh /FULLPATH/PROBLEM --FLAG MEMORYLIMIT TIMELIMIT ON_SHIELD ON_SANDBOX ON_COMPARE ON_DIFF2HMTL JAVA_POLICY DISPLAY_JAVA_EXCEPTION_ON 10 | # ======= 11 | 12 | # FLAGS: 13 | # ===== 14 | # --c to language C 15 | # --cpp to C++ language 16 | # --py2 to Python language 17 | # --py3 to Python language 18 | # --java to Java language 19 | 20 | # MEMORYLIMIT (kb) 21 | # TIMELIMIT (seconds) 22 | # ON_SHIELD (0 or 1) 23 | # ON_SANDBOX (0 or 1) 24 | # ON_COMPARE (0 or 1) 25 | # JAVA_POLICY (0 or 1) 26 | # DISPLAY_JAVA_EXPTION_ON (0 or 1) 27 | 28 | # STATUS CODES 29 | # ============ 30 | # 0 - OK 31 | # 1 - Error 32 | # 2 - File not found. 33 | # 3 - Missing Arguments 34 | # 4 - Compare is disabled. 35 | 36 | # GET CURRENT TIME (IN MILLISECONDS) 37 | START=$(($(date +%s%N)/1000000)); 38 | 39 | # GLOBALS 40 | # ======= 41 | 42 | # MISC 43 | SCRIPT=$(readlink -f "$0") 44 | 45 | # DIRECTORY PATH 46 | SCRIPTPATH=$(dirname "$SCRIPT") 47 | 48 | # TIMELIMIT (1m:30s = 90s) 49 | # DEFAULT_TIMELIMIT=90 50 | 51 | # MEMORYLIMIT (5MB = 5120 Kb) 52 | # DEFAULT_MEMORYLIMIT=5120 53 | 54 | # GETTINGS ARGUMENTS 55 | # ================== 56 | 57 | # 1.- FULLPATH PROBLEM 58 | PROBLEMPATH=${1} 59 | 60 | # 2.- FLAG 61 | FLAG=${2} 62 | 63 | # 3.- MEMORYLIMIT(Kb) 64 | MEMORYLIMIT=${3} 65 | 66 | # 4.- TIMELIMIT(Seconds) 67 | TIMELIMIT=${4} 68 | 69 | # 5.- ENABLE/DISABLE SHIELD 70 | ON_SHIELD=${5} 71 | 72 | # 6.- ENABLE/DISABLE SANDBOX 73 | ON_SANDBOX=${6} 74 | 75 | # 7.- ENABLE/DISABLE DIFF 76 | ON_COMPARE=${7} 77 | 78 | # 8.- ENABLE/DISABLE DIFF2HTML 79 | ON_DIFF2HMTL=${8} 80 | 81 | # 9.- JAVA_POLICY ENABLE/DISABLE JAVA SECURITY MANAGER 82 | JAVA_POLICY=${9} 83 | if [[ $JAVA_POLICY == "1" ]]; then 84 | JAVA_POLICY="-Djava.security.manager -Djava.security.policy=java.policy" 85 | else 86 | JAVA_POLICY="" 87 | fi 88 | 89 | # 10.- DISPLAY_JAVA_EXCEPTION_ON 90 | DISPLAY_JAVA_EXCEPTION_ON=${10} 91 | 92 | # COMPILER OPTIONS FOR C/C++ 93 | # ========================== 94 | C_COMPILER="gcc" 95 | C_OPTIONS="-fno-asm -Dasm=error -lm -O2" 96 | C_WARNING_OPTION="-w" 97 | C_EXEFILE="1" 98 | C_SHIELD="shield_c.c" 99 | C_FLAG="--c" 100 | C_EXT="c" 101 | C_BLACKLIST="blacklist_c.h" 102 | 103 | CPP_COMPILER="g++" 104 | CPP_EXEFILE="1" 105 | CPP_SHIELD="shield_cpp.cpp" 106 | CPP_FLAG="--cpp" 107 | CPP_EXT="cpp" 108 | CPP_BLACKLIST="blacklist_cpp.h" 109 | # -w: Inhibit all warning messages 110 | # -Werror: Make all warnings into errors 111 | # -Wall ... 112 | # Read more: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html 113 | # Read more: http://www.eis.uva.es/~fergay/III/enlaces/gcc.html 114 | 115 | # COMPILER OPTIONS FOR PYTHON2 && PYTHON3 116 | # ======================================= 117 | PY2_COMPILER="python" 118 | PY3_COMPILER="python3" 119 | PY2_FLAG="--py2" 120 | PY3_FLAG="--py3" 121 | PY_EXT="py" 122 | PY2_SHIELD="shield_python2.py" 123 | PY3_SHIELD="shield_python3.py" 124 | PY_OPTIONS="-O -m py_compile" 125 | 126 | # COMPILER OPTIONS FOR JAVA 127 | # ========================= 128 | JAVA_FLAG="--java" 129 | JAVA_COMPILER="javac" 130 | JAVA_RUNNER="java" 131 | JAVA_EXT="java" 132 | JAVA_POLICY="-Djava.security.manager -Djava.security.policy=java.policy" 133 | JAVA_FILEPOLICY="java.policy" 134 | 135 | # MISC 136 | # ==== 137 | 138 | # FILE 139 | FILE=${PROBLEMPATH##*/} 140 | 141 | # EXTENSION 142 | EXT="${FILE#*.}" 143 | 144 | # PATH 145 | DIRECTORY=$(dirname "$PROBLEMPATH") 146 | 147 | # FILENAME_OUTPUT 148 | OUTPUT="output.txt" 149 | 150 | # CLASS NAME(ONLY FOR JAVA) 151 | CLASSNAME="${FILE%.*}" 152 | 153 | # INPUTNAME 154 | INPUTNAME="input.txt" 155 | 156 | ########### - FUNCTIONS - ########### 157 | 158 | LOG="$DIRECTORY/log"; echo "" >>$LOG 159 | 160 | function judge_log 161 | { 162 | if $LOG_ON; then 163 | echo "$@" >>$LOG 164 | fi 165 | } 166 | ########### - /FUNCTIONS - ########### 167 | 168 | judge_log "$(date)" 169 | judge_log "Language: $EXT" 170 | judge_log "Time Limit: $TIMELIMIT s" 171 | judge_log "Memory Limit: $MEMORYLIMIT kB" 172 | 173 | if [[ ! -f $PROBLEMPATH ]]; then 174 | judge_log "File not found!" 175 | exit 2 176 | else 177 | if [[ $# == 10 ]]; then 178 | 179 | 180 | 181 | #################### C #################### 182 | if [[ $FLAG == $C_FLAG ]]; then 183 | # IF SHIELD IS ENABLE 184 | if [[ $ON_SHIELD == "1" ]]; then 185 | SHIELD_CODE=$($SCRIPTPATH/shield/shield.sh $FLAG $PROBLEMPATH) >/dev/null 2>$DIRECTORY/cerr 186 | else 187 | $C_COMPILER $FULLPATH $C_OPTIONS $C_WARNING_OPTION -o $DIRECTORY/$CLASSNAME >/dev/null 2>$DIRECTORY/cerr 188 | SHIELD_CODE=$? 189 | fi 190 | 191 | judge_log "SHIELD = $SHIELD_CODE" 192 | 193 | if [[ $SHIELD_CODE == "0" ]]; then 194 | 195 | # IF SANDBOX IS ENABLE 196 | if [[ $ON_SANDBOX == "1" ]]; then 197 | 198 | # IF THE PROBLEM HAS INPUT 199 | if [[ -f $DIRECTORY/input.txt ]]; then 200 | $SCRIPTPATH/runcode.sh $FLAG $DIRECTORY $MEMORYLIMIT $TIMELIMIT "LD_PRELOAD=$SCRIPTPATH/sandbox/sandbox.so $DIRECTORY/$CLASSNAME" $DIRECTORY/input.txt 201 | else 202 | $SCRIPTPATH/runcode.sh $FLAG $DIRECTORY $MEMORYLIMIT $TIMELIMIT "LD_PRELOAD=$SCRIPTPATH/sandbox/sandbox.so $DIRECTORY/$CLASSNAME" 203 | fi 204 | SANDBOX_CODE=$? 205 | judge_log "SANDBOX = $SANDBOX_CODE" 206 | EXIT_CODE=$SANDBOX_CODE 207 | else # IF SANDBOX IS DISABLE 208 | 209 | # IF THE PROBLEM HAS INPUT 210 | if [[ -f $DIRECTORY/input.txt ]]; then 211 | $SCRIPTPATH/runcode.sh $FLAG $DIRECTORY $MEMORYLIMIT $TIMELIMIT "$DIRECTORY/$CLASSNAME" $DIRECTORY/input.txt >/dev/null 2>$DIRECTORY/cerr 212 | else 213 | $SCRIPTPATH/runcode.sh $FLAG $DIRECTORY $MEMORYLIMIT $TIMELIMIT "$DIRECTORY/$CLASSNAME" >/dev/null 2>$DIRECTORY/cerr 214 | fi 215 | RUN_CODE=$? 216 | judge_log "RUN = $RUN_CODE" 217 | EXIT_CODE=$RUN_CODE 218 | fi 219 | 220 | if [[ $EXIT_CODE == "0" ]]; then 221 | if [[ $ON_COMPARE == "1" ]]; then 222 | if [[ $ON_DIFF2HMTL == "1" ]]; then 223 | $SCRIPTPATH/compare/compare.sh $DIRECTORY/output.txt $DIRECTORY/expected.txt --diff2html >/dev/null 2>$DIRECTORY/cerr 224 | else 225 | $SCRIPTPATH/compare/compare.sh $DIRECTORY/output.txt $DIRECTORY/expected.txt >/dev/null 2>$DIRECTORY/cerr 226 | fi 227 | COMPARE_CODE=$? 228 | judge_log "COMPARE = $COMPARE_CODE" 229 | else 230 | COMPARE_CODE=4 231 | judge_log "COMPARE = Disable." 232 | fi 233 | fi 234 | fi 235 | fi 236 | 237 | ################### C++ ################### 238 | if [[ $FLAG == $CPP_FLAG ]]; then 239 | # IF SHIELD IS ENABLE 240 | if [[ $ON_SHIELD == "1" ]]; then 241 | SHIELD_CODE=$($SCRIPTPATH/shield/shield.sh $FLAG $PROBLEMPATH) >/dev/null 2>$DIRECTORY/cerr 242 | else 243 | $CPP_COMPILER $FULLPATH $CPP_OPTIONS $CPP_WARNING_OPTION -o $DIRECTORY/$CLASSNAME >/dev/null 2>$DIRECTORY/cerr 244 | SHIELD_CODE=$? 245 | fi 246 | 247 | judge_log "SHIELD = $SHIELD_CODE" 248 | 249 | if [[ $SHIELD_CODE == "0" ]]; then 250 | 251 | # IF SANDBOX IS ENABLE 252 | if [[ $ON_SANDBOX == "1" ]]; then 253 | 254 | # IF THE PROBLEM HAS INPUT 255 | if [[ -f $DIRECTORY/input.txt ]]; then 256 | cp $SCRIPTPATH/sandbox/sandbox.so $DIRECTORY/Sandbox.so 257 | $SCRIPTPATH/runcode.sh $FLAG $DIRECTORY $MEMORYLIMIT $TIMELIMIT "LD_PRELOAD=./Sandbox.so $DIRECTORY/$CLASSNAME" $DIRECTORY/input.txt >/dev/null 2>$DIRECTORY/cerr 258 | else 259 | cp $SCRIPTPATH/sandbox/sandbox.so $DIRECTORY/Sandbox.so 260 | $SCRIPTPATH/runcode.sh $FLAG $DIRECTORY $MEMORYLIMIT $TIMELIMIT "LD_PRELOAD=./Sandbox.so $DIRECTORY/$CLASSNAME" >/dev/null 2>$DIRECTORY/cerr 261 | fi 262 | SANDBOX_CODE=$? 263 | judge_log "SANDBOX = $SANDBOX_CODE" 264 | EXIT_CODE=$SANDBOX_CODE 265 | else # IF SANDBOX IS DISABLE 266 | 267 | # IF THE PROBLEM HAS INPUT 268 | if [[ -f $DIRECTORY/input.txt ]]; then 269 | $SCRIPTPATH/runcode.sh $FLAG $DIRECTORY $MEMORYLIMIT $TIMELIMIT "$DIRECTORY/$CLASSNAME" $DIRECTORY/input.txt >/dev/null 2>$DIRECTORY/cerr 270 | else 271 | $SCRIPTPATH/runcode.sh $FLAG $DIRECTORY $MEMORYLIMIT $TIMELIMIT "$DIRECTORY/$CLASSNAME" >/dev/null 2>$DIRECTORY/cerr 272 | fi 273 | RUN_CODE=$? 274 | judge_log "RUN = $RUN_CODE" 275 | EXIT_CODE=$RUN_CODE 276 | fi 277 | 278 | if [[ $EXIT_CODE == "0" ]]; then 279 | if [[ $ON_COMPARE == "1" ]]; then 280 | if [[ $ON_DIFF2HMTL == "1" ]]; then 281 | $SCRIPTPATH/compare/compare.sh $DIRECTORY/output.txt $DIRECTORY/expected.txt --diff2html >/dev/null 2>$DIRECTORY/cerr 282 | else 283 | $SCRIPTPATH/compare/compare.sh $DIRECTORY/output.txt $DIRECTORY/expected.txt >/dev/null 2>$DIRECTORY/cerr 284 | fi 285 | COMPARE_CODE=$? 286 | judge_log "COMPARE = $COMPARE_CODE" 287 | else 288 | COMPARE_CODE=4 289 | judge_log "COMPARE = Disable." 290 | fi 291 | fi 292 | fi 293 | fi 294 | 295 | ################### PYTHON2 ################### 296 | if [[ $FLAG == $PY2_FLAG ]]; then 297 | # IF SHIELD IS ENABLE 298 | if [[ $ON_SHIELD == "1" ]]; then 299 | SHIELD_CODE=$($SCRIPTPATH/shield/shield.sh $FLAG $PROBLEMPATH) >/dev/null 2>$DIRECTORY/cerr 300 | else 301 | $PY2_COMPILER $PY_OPTIONS $DIRECTORY/$FILE >/dev/null 2>$DIRECTORY/cerr 302 | SHIELD_CODE=$? 303 | fi 304 | 305 | judge_log "SHIELD = $SHIELD_CODE" 306 | 307 | if [[ $SHIELD_CODE == "0" ]]; then 308 | 309 | # IF THE PROBLEM HAS INPUT 310 | if [[ -f $DIRECTORY/input.txt ]]; then 311 | $SCRIPTPATH/runcode.sh $FLAG $DIRECTORY $MEMORYLIMIT $TIMELIMIT "python2 -O $PROBLEMPATH" $DIRECTORY/input.txt >/dev/null 2>$DIRECTORY/cerr 312 | else 313 | $SCRIPTPATH/runcode.sh $FLAG $DIRECTORY $MEMORYLIMIT $TIMELIMIT "python2 -O $PROBLEMPATH" >/dev/null 2>$DIRECTORY/cerr 314 | fi 315 | 316 | RUN_CODE=$? 317 | 318 | judge_log "RUN = $RUN_CODE" 319 | 320 | if [[ $RUN_CODE == "0" ]]; then 321 | if [[ $ON_COMPARE == "1" ]]; then 322 | if [[ $ON_DIFF2HMTL == "1" ]]; then 323 | $SCRIPTPATH/compare/compare.sh $DIRECTORY/output.txt $DIRECTORY/expected.txt --diff2html >/dev/null 2>$DIRECTORY/cerr 324 | else 325 | $SCRIPTPATH/compare/compare.sh $DIRECTORY/output.txt $DIRECTORY/expected.txt >/dev/null 2>$DIRECTORY/cerr 326 | fi 327 | COMPARE_CODE=$? 328 | judge_log "COMPARE = $COMPARE_CODE" 329 | else 330 | COMPARE_CODE=4 331 | judge_log "COMPARE = Disable." 332 | fi 333 | fi 334 | fi 335 | fi 336 | 337 | ################### PYTHON3 ################### 338 | if [[ $FLAG == $PY3_FLAG ]]; then 339 | 340 | # IF SHIELD IS ENABLE 341 | if [[ $ON_SHIELD == "1" ]]; then 342 | SHIELD_CODE=$($SCRIPTPATH/shield/shield.sh $FLAG $PROBLEMPATH) >/dev/null 2>$DIRECTORY/cerr 343 | else 344 | $PY3_COMPILER $PY_OPTIONS $DIRECTORY/$FILE >/dev/null 2>$DIRECTORY/cerr 345 | SHIELD_CODE=$? 346 | fi 347 | 348 | judge_log "SHIELD = $SHIELD_CODE" 349 | 350 | if [[ $SHIELD_CODE == "0" ]]; then 351 | 352 | # IF THE PROBLEM HAS INPUT 353 | if [[ -f $DIRECTORY/input.txt ]]; then 354 | $SCRIPTPATH/runcode.sh $FLAG $DIRECTORY $MEMORYLIMIT $TIMELIMIT "python3 -O $PROBLEMPATH" $DIRECTORY/input.txt >/dev/null 2>$DIRECTORY/cerr 355 | else 356 | $SCRIPTPATH/runcode.sh $FLAG $DIRECTORY $MEMORYLIMIT $TIMELIMIT "python3 -O $PROBLEMPATH" >/dev/null 2>$DIRECTORY/cerr 357 | fi 358 | 359 | RUN_CODE=$? 360 | judge_log "RUN = $RUN_CODE" 361 | if [[ $RUN_CODE == "0" ]]; then 362 | if [[ $ON_COMPARE == "1" ]]; then 363 | if [[ $ON_DIFF2HMTL == "1" ]]; then 364 | $SCRIPTPATH/compare/compare.sh $DIRECTORY/output.txt $DIRECTORY/expected.txt --diff2html >/dev/null 2>$DIRECTORY/cerr 365 | else 366 | $SCRIPTPATH/compare/compare.sh $DIRECTORY/output.txt $DIRECTORY/expected.txt >/dev/null 2>$DIRECTORY/cerr 367 | fi 368 | COMPARE_CODE=$? 369 | judge_log "COMPARE = $COMPARE_CODE" 370 | else 371 | COMPARE_CODE=4 372 | judge_log "COMPARE = Disable." 373 | fi 374 | fi 375 | fi 376 | fi 377 | ################### JAVA ################### 378 | if [[ $FLAG == $JAVA_FLAG ]]; then 379 | judge_log "JAVA_POLICY: \"$JAVA_POLICY\"" 380 | judge_log "DISPLAY_JAVA_EXCEPTION_ON: $DISPLAY_JAVA_EXCEPTION_ON" 381 | # IF SHIELD IS ENABLE 382 | if [[ $ON_SHIELD == "1" ]]; then 383 | SHIELD_CODE=$($SCRIPTPATH/shield/shield.sh $FLAG $PROBLEMPATH) >/dev/null 2>$DIRECTORY/cerr 384 | else 385 | $JAVA_COMPILER $PROBLEMPATH >/dev/null 2>$DIRECTORY/cerr 386 | SHIELD_CODE=$? 387 | fi 388 | 389 | judge_log "SHIELD = $SHIELD_CODE" 390 | 391 | if [[ $SHIELD_CODE == "0" ]]; then 392 | 393 | # IF THE PROBLEM HAS INPUT 394 | cd $DIRECTORY 395 | if [[ -f $DIRECTORY/input.txt ]]; then 396 | $SCRIPTPATH/runcode.sh $FLAG $DIRECTORY $MEMORYLIMIT $TIMELIMIT "java -mx${MEMORYLIMIT}k $JAVA_POLICY $CLASSNAME" $DIRECTORY/input.txt 397 | else 398 | $SCRIPTPATH/runcode.sh $FLAG $DIRECTORY $MEMORYLIMIT $TIMELIMIT "java -mx${MEMORYLIMIT}k $JAVA_POLICY $CLASSNAME" 399 | fi 400 | 401 | RUN_CODE=$? 402 | judge_log "RUN = $RUN_CODE" 403 | if [[ $RUN_CODE == "0" ]]; then 404 | if [[ $ON_COMPARE == 1 ]]; then 405 | if [[ $ON_DIFF2HMTL == 1 ]]; then 406 | $SCRIPTPATH/compare/compare.sh $DIRECTORY/output.txt $DIRECTORY/expected.txt --diff2html >/dev/null 2>$DIRECTORY/cerr 407 | else 408 | $SCRIPTPATH/compare/compare.sh $DIRECTORY/output.txt $DIRECTORY/expected.txt >/dev/null 2>$DIRECTORY/cerr 409 | fi 410 | COMPARE_CODE=$? 411 | judge_log "COMPARE = $COMPARE_CODE" 412 | else 413 | COMPARE_CODE=4 414 | judge_log "COMPARE = Disable." 415 | fi 416 | fi 417 | 418 | if grep -iq -m 1 "Too small initial heap" output.txt || grep -q -m 1 "java.lang.OutOfMemoryError" err; then 419 | judge_log "Memory Limit Exceeded" 420 | continue 421 | fi 422 | 423 | # SHOW EXCEPTION 424 | if grep -q -m 1 "Exception in" err; then 425 | javaexceptionname=`grep -m 1 "Exception in" err | grep -m 1 -oE 'java\.[a-zA-Z\.]*' | head -1 | head -c 80` 426 | javaexceptionplace=`grep -m 1 "$MAINFILENAME.java" err | head -1 | head -c 80` 427 | judge_log "Exception: $javaexceptionname\nMaybe at:$javaexceptionplace" 428 | 429 | if $DISPLAY_JAVA_EXCEPTION_ON && grep -q -m 1 "^$javaexceptionname\$" ../java_exceptions_list; then 430 | judge_log "Runtime Error $(javaexceptionname)" 431 | else 432 | judge_log "Runtime Error" 433 | fi 434 | continue 435 | fi 436 | fi 437 | fi 438 | 439 | 440 | ##################### - RESULTS - ##################### 441 | if [[ $RUN_CODE == "137" ]]; then 442 | judge_log "Killed." 443 | fi 444 | 445 | if [[ $RUN_CODE != "0" ]]; then 446 | judge_log "Runtime error." 447 | fi 448 | 449 | else 450 | judge_log "Missing arguments." 451 | exit 3 452 | fi 453 | fi 454 | 455 | END=$(($(date +%s%N)/1000000)); 456 | judge_log "Total Execution Time: $((END-START)) ms" 457 | 458 | exit $EXIT_CODE -------------------------------------------------------------------------------- /runcode.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ***************************************** 4 | # * FILE: runcode.sh * 5 | # * AUTOR: Hector Jose Flores Colmenarez * 6 | # * EMAIL: hecto932@gmail.com * 7 | # ***************************************** 8 | 9 | # USAGE: ./runcode.sh --FLAG DIRECTORY MEMORYLIMIT TIMELIMIT COMMAND INPUT(OPTIONAL) 10 | 11 | # STATUS CODES 12 | # ============ 13 | # timeout exit codes http://www.gnu.org/software/coreutils/manual/html_node/timeout-invocation.html#timeout-invocation 14 | 15 | # GETTINGS ARGUMENTS 16 | # ================== 17 | 18 | # 1.- FLAGS 19 | FLAG=${1} 20 | 21 | # 2.- PROBLEM DIRECTORY 22 | DIRECTORY=${2} 23 | 24 | # 2.- MEMORYLIMIT 25 | MEMORYLIMIT=${3} 26 | 27 | # 3.- TIMELIMITINT 28 | TIMELIMIT=${4} 29 | 30 | # 4.- COMMAND 31 | CMD=${5} 32 | 33 | # 5.- INPUT 34 | INPUT=${6} 35 | 36 | # 6.- EXIT_CODE 37 | EXIT_CODE=0 38 | 39 | # echo $FLAG 40 | # echo $DIRECTORY 41 | # echo $MEMORYLIMIT 42 | # echo $TIMELIMIT 43 | # echo $CMD 44 | # echo $INPUT 45 | 46 | # DETECTING EXISTENCE OF TIMEOUT 47 | TIMEOUT_EXISTS=true 48 | hash timeout 2>/dev/null || TIMEOUT_EXISTS=false 49 | 50 | if [ $FLAG == "--py2" ]; then 51 | mem=$(pid=$(python2 >/dev/null 2>/dev/null & echo $!) && ps -p $pid -o vsz=; kill $pid >/dev/null 2>/dev/null;) 52 | MEMORYLIMIT=$((MEMORYLIMIT+mem+5000)) 53 | elif [ $FLAG == "--py3" ]; then 54 | mem=$(pid=$(python3 >/dev/null 2>/dev/null & echo $!) && ps -p $pid -o vsz=; kill $pid >/dev/null 2>/dev/null;) 55 | MEMORYLIMIT=$((MEMORYLIMIT+mem+5000)) 56 | fi 57 | 58 | # IMPOSING MEMORY LIMIT WITH ulimit 59 | if [ "$FLAG" != "--java" ]; then 60 | ulimit -v $((MEMORYLIMIT+10000)) 61 | ulimit -m $((MEMORYLIMIT+10000)) 62 | ulimit -s $((MEMORYLIMIT+10000)) 63 | fi 64 | 65 | # IMPOSING TIME LIMIT WITH ULIMIT 66 | ulimit -t $TIMELIMIT 67 | 68 | if $TIMEOUT_EXISTS; then 69 | # RUN THE COMMAND WITH REAL TIME LIMIT OF TIMELIMIT*2 70 | if [[ -z "$INPUT" ]]; then 71 | timeout -s9 $((TIMELIMIT*2)) $CMD > $DIRECTORY/output.txt 2>$DIRECTORY/err 72 | else 73 | timeout -s9 $((TIMELIMIT*2)) $CMD <$INPUT > $DIRECTORY/output.txt 2>$DIRECTORY/err 74 | fi 75 | EXIT_CODE=$? 76 | else 77 | # RUN THE COMMAND 78 | if [[ -z "$INPUT" ]]; then 79 | $CMD >$DIRECTORY/output.txt 2>$DIRECTORY/err 80 | else 81 | $CMD <$INPUT > $DIRECTORY/output.txt 2>$DIRECTORY/err 82 | fi 83 | EXIT_CODE=$? 84 | fi 85 | 86 | echo $EXIT_CODE 87 | exit $EXIT_CODE 88 | -------------------------------------------------------------------------------- /sandbox/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CXX = g++ 3 | COMMON_FLAGS = -g -Wall -D_BSD_SOURCE 4 | CFLAGS = -std=c99 $(COMMON_FLAGS) #-DDEBUG_MALLOC 5 | CXXFLAGS = $(COMMON_FLAGS) 6 | SHLIB_CFLAGS = -fPIC $(CFLAGS) 7 | 8 | sandbox.so : sandbox.o malloc.o 9 | gcc -shared -o sandbox.so sandbox.o malloc.o -ldl 10 | 11 | sandbox.o : sandbox.c 12 | gcc -c $(SHLIB_CFLAGS) sandbox.c 13 | 14 | malloc.o : malloc.c 15 | gcc -c $(SHLIB_CFLAGS) malloc.c 16 | 17 | clean : 18 | rm -f *.o *.so 19 | -------------------------------------------------------------------------------- /sandbox/README.md: -------------------------------------------------------------------------------- 1 | ## Sandbox 2 | 3 | Core-jude can execute code in several languages, such as C, C ++, Python and Java, foreach one it uses a different technique.It should run codes in a restricted environment. 4 | 5 | You can improve security by enabling alongside Sandbox. 6 | 7 | ### C/C++ Sandboxing 8 | 9 | Core-judge use [EasySandbox](https://github.com/daveho/EasySandbox) for sandboxing C/C++ codes. EasySandbox limits the running code using **[seccomp](http://lwn.net/Articles/332974/)**, a sandboxing mechanism in Linux kernel. 10 | 11 | ### Python Sandboxing 12 | 13 | Don't have a solution yet. 14 | 15 | ### Java Sandboxing 16 | 17 | Java sandbox is enabled by default using Java Security Manager. 18 | 19 | ### Usage 20 | Run the `make` command to build the sandbox shared library. 21 | 22 | ```sh 23 | $ ./sandbox.sh /full_path/exefile /full_path/input.txt(Optional) 24 | ``` 25 | #### Status Codes 26 | * 0 - Sandboxing Success. 27 | * 1 - Sandboxing Failed. 28 | * 2 - File not found. 29 | 30 | 31 | -------------------------------------------------------------------------------- /sandbox/malloc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * EasySandbox: an extremely simple sandbox for untrusted C/C++ programs 3 | * Copyright (c) 2012,2013 David Hovemeyer 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 9 | * of the Software, and to permit persons to whom the Software is furnished to do 10 | * so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | /* Minimum amount of memory to allocate when we use sbrk to extend the heap */ 31 | #define MIN_ALLOC 65536 32 | 33 | /* Block flags */ 34 | #define ALLOCATED 1 35 | 36 | /* Header found at the beginning of each block */ 37 | union Header { 38 | struct { 39 | union Header *prev, *next; /* previous and next blocks */ 40 | size_t size; /* total size of block, including header */ 41 | int flags; /* block flags */ 42 | } h; 43 | long align; /* force alignment */ 44 | }; 45 | 46 | /* List of blocks, sorted by order of increasing addresses */ 47 | static union Header *s_head, *s_tail; 48 | 49 | #ifdef DEBUG_MALLOC 50 | static void dump_block_list(void) 51 | { 52 | union Header *block; 53 | 54 | printf("head=%p,tail=%p\n", s_head, s_tail); 55 | 56 | for (block = s_head; block != 0; block = block->h.next) { 57 | printf("%p:size=%lu,flags=%d,prev=%p,next=%p\n", 58 | block, (unsigned long) block->h.size, block->h.flags, 59 | block->h.prev, block->h.next); 60 | } 61 | } 62 | #endif 63 | 64 | /* 65 | * Round given size up to the nearest multiple. 66 | */ 67 | static inline size_t round_to_multiple(size_t n, size_t multiple) 68 | { 69 | if (n % multiple != 0) { 70 | n += multiple - (n % multiple); 71 | } 72 | return n; 73 | } 74 | 75 | /* 76 | * Predicate to test whether given block is allocated. 77 | */ 78 | static inline int is_allocated(union Header *block) 79 | { 80 | return (block->h.flags & ALLOCATED) != 0; 81 | } 82 | 83 | /* 84 | * Allocate a new block using sbrk and append it to the list of blocks. 85 | * Returns the pointer to the allocated block, or null if 86 | * sbrk couldn't allocate more memory 87 | */ 88 | static union Header *alloc_block(size_t block_size) 89 | { 90 | union Header *block; 91 | 92 | /* ensure minimum allocation size */ 93 | if (block_size < MIN_ALLOC) { 94 | block_size = MIN_ALLOC; 95 | } 96 | 97 | /* use sbrk to extend the heap */ 98 | block = sbrk((intptr_t) block_size); 99 | if (block == (void *)-1) { 100 | return 0; 101 | } 102 | 103 | /* Append block to list */ 104 | block->h.next = 0; 105 | if (s_head == 0) { 106 | /* first allocation */ 107 | s_head = s_tail = block; 108 | block->h.prev = 0; 109 | } else { 110 | /* append block at tail of list */ 111 | s_tail->h.next = block; 112 | block->h.prev = s_tail; 113 | s_tail = block; 114 | } 115 | block->h.size = block_size; 116 | block->h.flags = 0; 117 | 118 | return block; 119 | } 120 | 121 | /* 122 | * Split given block if its excess space beyond given required block size 123 | * is large enough to form a useful block. 124 | */ 125 | static void split_block_if_necessary(union Header *block, size_t required_block_size) 126 | { 127 | union Header *excess; 128 | size_t left_over; 129 | 130 | /* is there enough room to form a useful block (larger than just a header)? */ 131 | left_over = block->h.size - required_block_size; 132 | if (left_over <= sizeof(union Header)) { 133 | return; 134 | } 135 | 136 | /* adjust size of the original block */ 137 | block->h.size = required_block_size; 138 | 139 | /* compute address of block formed from excess memory in block */ 140 | excess = (union Header *) (((char *) block) + required_block_size); 141 | 142 | /* initialize the new block's header */ 143 | excess->h.size = left_over; 144 | excess->h.flags = 0; 145 | 146 | /* graft the new block into the list as current block's successor */ 147 | excess->h.next = block->h.next; 148 | excess->h.prev = block; 149 | if (block->h.next != 0) { 150 | block->h.next->h.prev = excess; 151 | } else { 152 | /* splitting the tail block, so excess is new tail */ 153 | s_tail = excess; 154 | } 155 | block->h.next = excess; 156 | } 157 | 158 | /* 159 | * Coalesce given block with its successor if necesary. 160 | */ 161 | static void coalesce_if_necessary(union Header *block) 162 | { 163 | union Header *succ; 164 | 165 | if (block == 0) { 166 | return; 167 | } 168 | succ = block->h.next; 169 | 170 | /* check whether successor exists and both block and successor are free */ 171 | if (is_allocated(block) || succ == 0 || is_allocated(succ)) { 172 | return; 173 | } 174 | 175 | /* absorb successor into block */ 176 | block->h.size += succ->h.size; 177 | 178 | /* splice successor out of the list */ 179 | if (succ->h.next != 0) { 180 | /* update successor's successor to have block as its predecessor */ 181 | succ->h.next->h.prev = block; 182 | } else { 183 | /* successor was the tail block, so block becomes tail */ 184 | s_tail = block; 185 | } 186 | block->h.next = block->h.next->h.next; 187 | } 188 | 189 | /* 190 | * Allocate a buffer of given size. 191 | */ 192 | void *malloc(size_t size) 193 | { 194 | size_t required_block_size; 195 | union Header *block; 196 | 197 | /* calculate the minimum block size needed for this allocation */ 198 | required_block_size = round_to_multiple(size + sizeof(union Header), sizeof(union Header)); 199 | 200 | /* search list for an unallocated block of sufficient size */ 201 | for (block = s_head; block != 0; block = block->h.next) { 202 | if (block->h.size >= required_block_size && !is_allocated(block)) { 203 | break; 204 | } 205 | } 206 | 207 | /* if no sufficiently-large block was found, allocate one and append it to list */ 208 | if (block == 0) { 209 | block = alloc_block(required_block_size); 210 | if (block == 0) { 211 | /* failed to allocate a new block */ 212 | errno = ENOMEM; 213 | return 0; 214 | } 215 | } 216 | 217 | /* if block size exceeds required block size by more than the size of one header, 218 | * then split it */ 219 | split_block_if_necessary(block, required_block_size); 220 | 221 | /* mark the block as allocated */ 222 | block->h.flags |= ALLOCATED; 223 | 224 | #ifdef DEBUG_MALLOC 225 | printf("After malloc (of block %p):\n", block); 226 | dump_block_list(); 227 | #endif 228 | 229 | return (void*) (block + 1); 230 | } 231 | 232 | /* 233 | * Free a buffer allocated with malloc. 234 | */ 235 | void free(void *p) 236 | { 237 | union Header *block; 238 | 239 | if (p == 0) { 240 | return; 241 | } 242 | 243 | /* find header */ 244 | block = ((union Header *)p) - 1; 245 | 246 | /* ensure that this is actually an allocated block */ 247 | if (!is_allocated(block)) { 248 | fprintf(stderr, "Invalid free at %p\n", p); 249 | return; 250 | } 251 | 252 | /* mark block as being free */ 253 | block->h.flags &= ~(ALLOCATED); 254 | 255 | /* Attempt to coalesce with predecessor and successor blocks */ 256 | coalesce_if_necessary(block); 257 | coalesce_if_necessary(block->h.prev); 258 | 259 | #ifdef DEBUG_MALLOC 260 | /* scan block list to ensure that there are no pairs of adjacent 261 | * free blocks */ 262 | { 263 | union Header *p; 264 | for (p = s_head; p != 0 && p->h.next != 0; p = p->h.next) { 265 | union Header *succ = p->h.next; 266 | if (!is_allocated(p) && !is_allocated(succ)) { 267 | fprintf(stderr, "Freeing block %p: adjacent unallocated blocks at %p, %p\n", 268 | block, p, succ); 269 | dump_block_list(); 270 | abort(); 271 | } 272 | } 273 | } 274 | #endif 275 | 276 | #ifdef DEBUG_MALLOC 277 | printf("After free (of block %p):\n", block); 278 | dump_block_list(); 279 | #endif 280 | } 281 | 282 | /* 283 | * Allocate a zeroed buffer. 284 | */ 285 | void *calloc(size_t nmemb, size_t size) 286 | { 287 | void *buf; 288 | 289 | buf = malloc(nmemb * size); 290 | if (buf != 0) { 291 | memset(buf, 0, nmemb * size); 292 | } 293 | return buf; 294 | } 295 | 296 | /* 297 | * Reallocate given buffer so that it has given size. 298 | */ 299 | void *realloc(void *ptr, size_t size) 300 | { 301 | union Header *block; 302 | size_t to_copy; 303 | void *buf; 304 | 305 | /* special case: if ptr is null, then allocate a new buffer 306 | * using malloc */ 307 | if (ptr == 0) { 308 | return malloc(size); 309 | } 310 | 311 | /* special case: if size is 0, then free the buffer */ 312 | if (size == 0) { 313 | free(ptr); 314 | return 0; 315 | } 316 | 317 | /* find buffer's block header */ 318 | block = ((union Header *)ptr) - 1; 319 | 320 | /* allocate a new buffer */ 321 | buf = malloc(size); 322 | if (buf == 0) { 323 | return 0; 324 | } 325 | 326 | /* copy data from old buffer to new buffer */ 327 | to_copy = block->h.size - sizeof(union Header); /* original buffer size */ 328 | if (to_copy > size) { 329 | /* original size was larger than new size */ 330 | to_copy = size; 331 | } 332 | memcpy(buf, ptr, to_copy); 333 | 334 | /* free the old buffer */ 335 | free(ptr); 336 | 337 | /* return the new buffer */ 338 | return buf; 339 | } 340 | -------------------------------------------------------------------------------- /sandbox/sandbox.c: -------------------------------------------------------------------------------- 1 | /* 2 | * EasySandbox: an extremely simple sandbox for untrusted C/C++ programs 3 | * Copyright (c) 2012,2013 David Hovemeyer 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 9 | * of the Software, and to permit persons to whom the Software is furnished to do 10 | * so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | 24 | /* 25 | * Resources: 26 | * 27 | * - http://justanothergeek.chdir.org//2010/03/seccomp-as-sandboxing-solution/ 28 | * 29 | * This is where the idea (and code) to use __libc_start_main as a hook 30 | * into the startup process came from. However, my implementation is 31 | * slightly different, in that I enable SECCOMP before any of the 32 | * constructor functions run. (Without this modification, constructor 33 | * functions would run with full privileges.) 34 | * 35 | * - http://www.win.tue.nl/~aeb/linux/lk/lk-14.html 36 | * 37 | * Very practical advice on using SECCOMP. 38 | */ 39 | 40 | // LINKS 41 | // ===== 42 | // http://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/baselib---libc-start-main-.html 43 | // http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html 44 | // http://stackoverflow.com/questions/7259830/why-and-when-to-use-static-structures-in-c-programming 45 | // http://justanothergeek.chdir.org//2010/02/how-system-calls-work-on-recent-linux/ 46 | // http://www.tutorialspoint.com/c_standard_library/c_function_atexit.htm 47 | // http://refspecs.linuxbase.org/LSB_2.1.0/LSB-generic/LSB-generic/baselib---cxa-atexit.html 48 | // http://stackoverflow.com/questions/12851184/dlopen-failed-cannot-open-shared-object-file-no-such-file-or-directory 49 | // http://justanothergeek.chdir.org//2010/03/seccomp-as-sandboxing-solution/ 50 | 51 | // LIBRARIES 52 | // ========= 53 | #include 54 | #include 55 | #include 56 | #include 57 | #include 58 | #include 59 | #include 60 | #include 61 | #include 62 | #include 63 | 64 | // DEFAULT HEAP SIZE IS 8MB(Bytes) 65 | #define DEFAULT_HEAP_SIZE 8388608 66 | 67 | #define DLOPEN_FAILED 120 68 | #define SECCOMP_FAILED 121 69 | #define EXIT_FAILED 122 // SHOULD NOT HAPPEN! 70 | #define MMAP_FAILED 123 71 | 72 | // WE IMPLEMENT OUR OWN ATEXIT AND __CXA__ATEXIT 73 | struct CxaAtexitHandler { 74 | union { 75 | void (*atexit_fn)(void); 76 | void (*cxa_atexit_fn)(void *); 77 | } f; 78 | void *arg; 79 | int type; // 0 FOR atexit FUNCTION, 1 FOR __cxa_atexit FUNCTION 80 | }; 81 | 82 | // TABLE OF atexit HANDLERS. 83 | #define MAX_ATEXIT_HANDLERS 1024 84 | static struct CxaAtexitHandler s_atexit_handlers[MAX_ATEXIT_HANDLERS]; 85 | static int s_atexit_handler_count; 86 | 87 | // SAVED POINTERS TO THE REAL INIT, MAIN, DESTRUCTOR AND RUNTIME LOADER DESTRUCTOR FUNCTIONS. 88 | static void (*real_init)(void); 89 | static int (*real_main)(int, char **, char **); 90 | static void (*real_fini)(void); 91 | static void (*real_rtld_fini)(void); 92 | 93 | // PROTOTYPES FOR OUR IDEMPOTENT WRAPPER DESTRUCTOR AND RUNTIME LOADER DESTRUCTOR FUNCTION. 94 | static void wrapper_fini(void); 95 | static void wrapper_rtld_fini(void); 96 | 97 | // KEEP TRACK OF WHETHER DESTRUCTOR FUNCTIONS HAVE BEEN RUN. 98 | static int s_ran_fini; 99 | static int s_ran_rtld_fini; 100 | 101 | /* 102 | * Preallocated region of memory with which to 103 | * implement a custom sbrk() routine. This is used by 104 | * the memory allocator in malloc.c to implement 105 | * malloc/free and friends. This approach allows us 106 | * to support malloc/free without any system calls. 107 | */ 108 | static char *s_heap; 109 | static size_t s_heapsize; 110 | static char *s_brk; 111 | 112 | /* 113 | * Custom implementation of sbrk() that allocates from a fixed-size 114 | * array of bytes. This avoids the need for malloc/free and 115 | * friends to make any system calls. 116 | */ 117 | void *sbrk(intptr_t incr) 118 | { 119 | intptr_t used, remaining; 120 | void *newbrk; 121 | 122 | if (s_brk == 0) { 123 | s_brk = s_heap; 124 | } 125 | 126 | used = s_brk - s_heap; 127 | remaining = s_heapsize - used; 128 | 129 | if (remaining < incr) { 130 | errno = ENOMEM; 131 | return (void*) -1; 132 | } 133 | newbrk = s_brk; 134 | s_brk += incr; 135 | return newbrk; 136 | } 137 | 138 | /* 139 | * Re-implementation of exit. 140 | * Flushes stdout and stderr, and exits using the exit system 141 | * call. glibc's exit function is unusable in SECCOMP mode because 142 | * it invokes the exit_group system call. 143 | */ 144 | void exit(int exit_code) 145 | { 146 | /* Invoke atexit handlers in reverse order. */ 147 | while (s_atexit_handler_count > 0) { 148 | struct CxaAtexitHandler *handler; 149 | s_atexit_handler_count--; 150 | handler = &s_atexit_handlers[s_atexit_handler_count]; 151 | switch (handler->type) { 152 | case 0: 153 | handler->f.atexit_fn(); 154 | break; 155 | case 1: 156 | handler->f.cxa_atexit_fn(handler->arg); 157 | break; 158 | } 159 | } 160 | 161 | /* This is probably a good time to call destructor functions */ 162 | wrapper_fini(); 163 | wrapper_rtld_fini(); 164 | 165 | /* Flush output streams */ 166 | fflush(stdout); 167 | fflush(stderr); 168 | 169 | /* The loop is because gcc doesn't know that syscall doesn't return 170 | * in this particular case */ 171 | while (1) { 172 | syscall(SYS_exit, exit_code); 173 | } 174 | } 175 | 176 | #define IMPL_ATEXIT(func_,field_,arg_,type_) \ 177 | struct CxaAtexitHandler *handler; \ 178 | if (s_atexit_handler_count >= MAX_ATEXIT_HANDLERS) { \ 179 | return -1; \ 180 | } \ 181 | handler = &s_atexit_handlers[s_atexit_handler_count]; \ 182 | handler->f.field_ = func_; \ 183 | handler->arg = arg_; \ 184 | handler->type = type_; \ 185 | s_atexit_handler_count++; \ 186 | return 0 187 | 188 | /* 189 | * Custom implementation of __cxa_atexit. 190 | * Note that the dso_handle is ignored, and we don't 191 | * attempt to hook into dynamic unloading. 192 | */ 193 | int __cxa_atexit(void (*func)(void *), void *arg, void *dso_handle) 194 | { 195 | IMPL_ATEXIT(func, cxa_atexit_fn, arg, 1); 196 | } 197 | 198 | /* 199 | * Custom implementation of atexit. 200 | */ 201 | int atexit(void (*func)(void)) 202 | { 203 | IMPL_ATEXIT(func, atexit_fn, 0, 0); 204 | } 205 | 206 | static void wrapper_init(void) 207 | { 208 | int stdin_flags; 209 | int c; 210 | 211 | /* The first call to print to a stream will cause glibc to 212 | * invoke the fstat system call, which will cause SECCOMP 213 | * to kill the process. There does not seem to be any way 214 | * of working around this problem except to print some output 215 | * on the stdout and strerr streams before entering SECCOMP mode. 216 | * Unfortunately, a printf call that generates no output doesn't 217 | * work, so some extraneous output seems unavoidable. Fortunately, 218 | * this is easy to filter out as a post-processing step. */ 219 | fprintf(stdout, "<>\n"); 220 | fflush(stdout); 221 | fprintf(stderr, "<>\n"); 222 | fflush(stderr); 223 | 224 | /* The first call to read from stdin will also result in a 225 | * call to fstat. Work around this by setting the stdin 226 | * file descriptor to nonblocking, then reading a single character 227 | * from stdin. */ 228 | stdin_flags = fcntl(0, F_GETFL, 0); 229 | fcntl(0, F_SETFL, stdin_flags | O_NONBLOCK); /* make stdin nonblocking */ 230 | c = fgetc(stdin); 231 | if (c != EOF) { 232 | /* We read a character, so put it back */ 233 | ungetc(c, stdin); 234 | } 235 | fcntl(0, F_SETFL, stdin_flags); /* restore original stdin flags */ 236 | 237 | #if 1 238 | /* Enter SECCOMP mode */ 239 | if (prctl(PR_SET_SECCOMP, 1, 0, 0) == -1) { 240 | _exit(SECCOMP_FAILED); 241 | } 242 | #endif 243 | 244 | /* Call the real init function */ 245 | real_init(); 246 | } 247 | 248 | static int wrapper_main(int argc, char **argv, char **envp) 249 | { 250 | /* Call the real main function. 251 | * Note that we call our reimplementation of the exit function, 252 | * because returning would cause glibc to invoke the exit_group 253 | * system call, which is not allowed in SECCOMP mode. */ 254 | int n; 255 | n = real_main(argc, argv, envp); 256 | exit(n); 257 | return EXIT_FAILED; 258 | } 259 | 260 | static void wrapper_fini(void) 261 | { 262 | if (!s_ran_fini) { 263 | /*printf("Running destructors...\n");*/ 264 | fflush(stdout); 265 | s_ran_fini = 1; 266 | real_fini(); 267 | } 268 | } 269 | 270 | static void wrapper_rtld_fini(void) 271 | { 272 | if (!s_ran_rtld_fini) { 273 | /*printf("Running runtime loader destructors...\n");*/ 274 | fflush(stdout); 275 | s_ran_rtld_fini = 1; 276 | real_rtld_fini(); 277 | } 278 | } 279 | 280 | int __libc_start_main( 281 | int (*main)(int, char **, char **), 282 | int argc, 283 | char ** ubp_av, 284 | void (*init)(void), 285 | void (*fini)(void), 286 | void (*rtld_fini)(void), 287 | void (* stack_end)) 288 | { 289 | void *libc_handle; 290 | const char *heapenv; 291 | 292 | int (*real_libc_start_main)( 293 | int (*main) (int, char **, char **), 294 | int argc, 295 | char ** ubp_av, 296 | void (*init)(void), 297 | void (*fini)(void), 298 | void (*rtld_fini)(void), 299 | void (* stack_end)); 300 | 301 | /* Save pointers to the real init, main, destructor, and runtime loader destructor functions */ 302 | real_init = init; 303 | real_main = main; 304 | real_fini = fini; 305 | real_rtld_fini = rtld_fini; 306 | 307 | /* Use mmap to allocate a region of memory to serve as the heap. 308 | * This must be done early since dlopen/dlsym will call malloc. */ 309 | heapenv = getenv("EASYSANDBOX_HEAPSIZE"); 310 | s_heapsize = (size_t) ((heapenv != 0) ? atol(heapenv) : DEFAULT_HEAP_SIZE); 311 | s_heap = mmap(0, s_heapsize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); 312 | if (s_heap == MAP_FAILED) { 313 | _exit(MMAP_FAILED); 314 | } 315 | 316 | /* explicitly open the glibc shared library */ 317 | libc_handle = dlopen("libc.so.6", RTLD_LOCAL | RTLD_LAZY); 318 | if (libc_handle == 0) { 319 | _exit(DLOPEN_FAILED); 320 | } 321 | 322 | /* get a pointer to the real __libc_start_main function */ 323 | *(void **) (&real_libc_start_main) = dlsym(libc_handle, "__libc_start_main"); 324 | 325 | /* Delegate to the real __libc_start_main, but provide our 326 | * wrapper init, main, destructor, and runtime loader destructor functions */ 327 | return real_libc_start_main(wrapper_main, argc, ubp_av, 328 | wrapper_init, wrapper_fini, wrapper_rtld_fini, stack_end); 329 | } 330 | -------------------------------------------------------------------------------- /sandbox/sandbox.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ***************************************** 4 | # * FILE: sandbox.sh * 5 | # * AUTOR: Hector Jose Flores Colmenarez * 6 | # * EMAIL: hecto932@gmail.com * 7 | # ***************************************** 8 | 9 | # FUNCTIONALITY 10 | # ============== 11 | # ./sandbox.sh /home/user/Desktop/exefile /home/user/Desktop/input.ext(Optional) 12 | 13 | # STATUS CODES 14 | # ============ 15 | # 0 - SANDBOX SUCCESS 16 | # 1 - SANDBOX FAILED 17 | # 2 - FILE NOT FOUND 18 | 19 | # GLOBALS 20 | # ======= 21 | 22 | # MISC 23 | SCRIPT=$(readlink -f "$0") 24 | 25 | # DIRECTORY PATH 26 | SCRIPTPATH=$(dirname "$SCRIPT") 27 | 28 | # GETTINGS ARGUMENTS 29 | # ================== 30 | 31 | # 1.- EXE FILE 32 | EXEFILE=${1} 33 | 34 | # 2.- INPUT 35 | INPUT=${2} 36 | 37 | # 3.- FILENAME_OUTPUT 38 | OUTPUT="output.txt" 39 | 40 | # 4.- DIRNAME 41 | DIRECTORY=$(dirname "$EXEFILE") 42 | 43 | # 5.- RESULT_CODE 44 | EXIT_CODE="2" 45 | 46 | # IF EXIST FILE PROBLEM 47 | if [[ ! -f "$EXEFILE" ]]; then 48 | echo "$EXEFILE doesn't exist." 49 | EXIT_CODE=2 50 | else 51 | if [[ -z "$INPUT" ]]; then 52 | # echo "DOES NOT EXPECT INPUT" 53 | EXIT_CODE=$(LD_PRELOAD=$SCRIPTPATH/sandbox.so $EXEFILE > $DIRECTORY/$OUTPUT 2> /dev/null) 54 | tail -n +2 $DIRECTORY/$OUTPUT >thetemp && mv thetemp $DIRECTORY/$OUTPUT 55 | else 56 | # echo "DOES EXPECT INPUT" 57 | EXIT_CODE=$(LD_PRELOAD=$SCRIPTPATH/sandbox.so $EXEFILE < $INPUT > $DIRECTORY/$OUTPUT 2> /dev/null) 58 | tail -n +2 $DIRECTORY/$OUTPUT >thetemp && mv thetemp $DIRECTORY/$OUTPUT 59 | fi 60 | fi 61 | 62 | echo $EXIT_CODE 63 | exit $EXIT_CODE -------------------------------------------------------------------------------- /shield/blacklist_c.h: -------------------------------------------------------------------------------- 1 | //REFERENCE : https://github.com/mjnaderi/Sharif-Judge/blob/7ca3101bb848447e8d8aabb14aae0d6ae9dfde31/tester/shield/defc.h 2 | 3 | /* 4 | SI DESEA AGREGAR UNA NUEVA DESCRIPCCION 5 | SOLO COLOQUELA DEBAJO DE LAS LINEAS "#define" 6 | */ 7 | 8 | #define system errorNo1 //"SYSTEM" NO ESTA PERMITIDA 9 | #define freopen errorNo2 //OPERACION DE ARCHIVOS NO PERMITIDA 10 | #define fopen errorNo3 //OPERACION DE ARCHIVOS NO PERMITIDA 11 | #define fprintf errorNo4 //OPERACION DE ARCHIVOS NO PERMITIDA 12 | #define fscanf errorNo5 //OPERACION DE ARCHIVOS NO PERMITIDA 13 | #define feof errorNo6 //OPERACION DE ARCHIVOS NO PERMITIDA 14 | #define fclose errorNo7 //OPERACION DE ARCHIVOS NO PERMITIDA 15 | #define ifstream errorNo8 //OPERACION DE ARCHIVOS NO PERMITIDA 16 | #define ofstream errorNo9 //OPERACION DE ARCHIVOS NO PERMITIDA 17 | #define fork errorNo10 //FORK NO ESTA PERMITIDA 18 | #define clone errorNo11 //CLONE NO ESTA PERMITIDA 19 | #define sleep errorNo12 //SLEEP NO ESTA PERMITIDA 20 | -------------------------------------------------------------------------------- /shield/blacklist_cpp.h: -------------------------------------------------------------------------------- 1 | /* 2 | ***************************************** 3 | SI DESEA AGREGAR UNA NUEVA DESCRIPCCION 4 | SOLO COLOQUELA DEBAJO DE LAS LINEAS "#define" 5 | ** HAY FUNCIONES EN C QUE EN C++ NO FUNCIONAN 6 | */ 7 | 8 | #define fork errorNo1 //Fork is not allowed 9 | #define clone errorNo2 //Clone is not allowed 10 | #define sleep errorNo3 //Sleep is not allowed 11 | -------------------------------------------------------------------------------- /shield/java.policy: -------------------------------------------------------------------------------- 1 | grant { 2 | }; -------------------------------------------------------------------------------- /shield/java_exceptions_list: -------------------------------------------------------------------------------- 1 | @file java_exceptions_list 2 | @author Mohammad Javad Naderi 3 | # List of Some Java Exceptions 4 | # When "Java Exceptions" is enabled for an assignment, we show the name of an exception if 5 | # this file contains that name. This is for security reasons. 6 | # REFERENCE: https://github.com/mjnaderi/Sharif-Judge/blob/7ca3101bb848447e8d8aabb14aae0d6ae9dfde31/tester/java_exceptions_list 7 | 8 | # JAVA EXCEPTIONS LIST 9 | # LIST OF SOME JAVA EXCEPTIONS 10 | 11 | java.lang.ArithmeticException 12 | java.lang.ArrayIndexOutOfBoundsException 13 | java.lang.ArrayStoreException 14 | java.lang.ClassCastException 15 | java.lang.ClassNotFoundException 16 | java.lang.CloneNotSupportedException 17 | java.lang.EnumConstantNotPresentException 18 | java.lang.Exception 19 | java.lang.IllegalAccessException 20 | java.lang.IllegalArgumentException 21 | java.lang.IllegalMonitorStateException 22 | java.lang.IllegalStateException 23 | java.lang.IllegalThreadStateException 24 | java.lang.IndexOutOfBoundsException 25 | java.lang.InstantiationException 26 | java.lang.InterruptedException 27 | java.lang.NegativeArraySizeException 28 | java.lang.NoSuchFieldException 29 | java.lang.NoSuchMethodException 30 | java.lang.NullPointerException 31 | java.lang.NumberFormatException 32 | java.lang.ReflectiveOperationException 33 | java.lang.RuntimeException 34 | java.lang.SecurityException 35 | java.lang.StringIndexOutOfBoundsException 36 | java.lang.TypeNotPresentException 37 | java.lang.UnsupportedOperationException 38 | 39 | java.lang.AbstractMethodError 40 | java.lang.AssertionError 41 | java.lang.BootstrapMethodError 42 | java.lang.ClassCircularityError 43 | java.lang.ClassFormatError 44 | java.lang.Error 45 | java.lang.ExceptionInInitializerError 46 | java.lang.IllegalAccessError 47 | java.lang.IncompatibleClassChangeError 48 | java.lang.InstantiationError 49 | java.lang.InternalError 50 | java.lang.LinkageError 51 | java.lang.NoClassDefFoundError 52 | java.lang.NoSuchFieldError 53 | java.lang.NoSuchMethodError 54 | java.lang.OutOfMemoryError 55 | java.lang.StackOverflowError 56 | java.lang.ThreadDeath 57 | java.lang.UnknownError 58 | java.lang.UnsatisfiedLinkError 59 | java.lang.UnsupportedClassVersionError 60 | java.lang.VerifyError 61 | java.lang.VirtualMachineError 62 | 63 | java.util.ConcurrentModificationException 64 | java.util.EmptyStackException 65 | java.util.MissingResourceException 66 | java.util.NoSuchElementException 67 | java.util.TooManyListenersException 68 | 69 | java.io.IOError 70 | java.io.EOFException 71 | java.io.FileNotFoundException 72 | java.io.InterruptedIOException 73 | java.io.InvalidClassException 74 | java.io.InvalidObjectException 75 | java.io.IOException 76 | java.io.NotActiveException 77 | java.io.NotSerializableException 78 | java.io.OptionalDataException 79 | java.io.StreamCorruptedException 80 | java.io.SyncFailedException 81 | java.io.UnsupportedEncodingException 82 | java.io.UTFDataFormatException 83 | java.io.WriteAbortedException 84 | 85 | java.security.AccessControlException 86 | -------------------------------------------------------------------------------- /shield/readme.md: -------------------------------------------------------------------------------- 1 | ## Shield 2 | 3 | Shield is an extremely simple mechanism to forbid running of potentially harmful codes. 4 | 5 | Shield is not a sandboxing solution. Shield provides only a partial protection against trivial attacks. 6 | 7 | ### Shield for C/C++ 8 | 9 | By enabling Shield for C/C++, just adds some `#define`s at the beginning of submitted C/C++ code before running. 10 | 11 | For example we can forbid using `goto` by adding this line at the beginning of submitted code: 12 | 13 | ```c 14 | #define goto YouCannotUseGoto 15 | ``` 16 | 17 | With this line at the beginning of files, all submitted codes which use `goto` will get a compilation error. 18 | 19 | If you enable Shield, any code that contains `#undef` will get a compilation error. 20 | 21 | #### Adding Rules for C/C++ 22 | 23 | List of `#define` rules is located in files `core-judge/shield/blacklist_c.h` (for C) and `core-judge/shield/blacklist_cpp.h` (for C++). You can add new `#define` rules in these files. 24 | 25 | The syntax used in these files is like this: 26 | 27 | ```c 28 | 29 | #define system errorNo1 //"system" is not allowed 30 | #define freopen errorNo2 //File operation is not allowed 31 | #define fopen errorNo3 //File operation is not allowed 32 | #define fprintf errorNo4 //File operation is not allowed 33 | #define fscanf errorNo5 //File operation is not allowed 34 | #define feof errorNo6 //File operation is not allowed 35 | #define fclose errorNo7 //File operation is not allowed 36 | #define ifstream errorNo8 //File operation is not allowed 37 | #define ofstream errorNo9 //File operation is not allowed 38 | #define fork errorNo10 //Fork is not allowed 39 | #define clone errorNo11 //Clone is not allowed 40 | #define sleep errorNo12 //Sleep is not allowed 41 | ``` 42 | 43 | There should be a newline at the end of files `blacklist_c.h` and `blacklist_cpp.h`. 44 | 45 | Note that lots of these rules are not usable in g++. For example we cannot use `#define fopen errorNo3` for C++. Because it results in compile error. 46 | 47 | ### Shield for Python 48 | 49 | By enabling Shield for Python, just adds some code at the beginning of submitted Python code before running to prevent using dangerous functions. 50 | 51 | There are ways to escape from Shield in python and use blacklisted functions! 52 | 53 | ```python 54 | # @file shield_python3.py 55 | 56 | import sys 57 | sys.modules['os']=None 58 | 59 | BLACKLIST = [ 60 | #'__import__', # deny importing modules 61 | 'eval', # eval is evil 62 | 'open', 63 | 'file', 64 | 'exec', 65 | 'execfile', 66 | 'compile', 67 | 'reload', 68 | #'input' 69 | ] 70 | for module in BLACKLIST: 71 | if module in __builtins__.__dict__: 72 | del __builtins__.__dict__[module] 73 | ``` 74 | 75 | 76 | ### Java List Exceptions 77 | 78 | By enabling Shield for Java, there a file with all exceptions. 79 | 80 | ```Java 81 | 82 | java.lang.ArithmeticException 83 | java.lang.ArrayIndexOutOfBoundsException 84 | java.lang.ArrayStoreException 85 | java.lang.ClassCastException 86 | java.lang.ClassNotFoundException 87 | java.lang.CloneNotSupportedException 88 | java.lang.EnumConstantNotPresentException 89 | java.lang.Exception 90 | java.lang.IllegalAccessException 91 | java.lang.IllegalArgumentException 92 | java.lang.IllegalMonitorStateException 93 | java.lang.IllegalStateException 94 | java.lang.IllegalThreadStateException 95 | java.lang.IndexOutOfBoundsException 96 | java.lang.InstantiationException 97 | java.lang.InterruptedException 98 | java.lang.NegativeArraySizeException 99 | java.lang.NoSuchFieldException 100 | java.lang.NoSuchMethodException 101 | java.lang.NullPointerException 102 | java.lang.NumberFormatException 103 | java.lang.ReflectiveOperationException 104 | java.lang.RuntimeException 105 | java.lang.SecurityException 106 | java.lang.StringIndexOutOfBoundsException 107 | java.lang.TypeNotPresentException 108 | java.lang.UnsupportedOperationException 109 | 110 | java.lang.AbstractMethodError 111 | java.lang.AssertionError 112 | java.lang.BootstrapMethodError 113 | java.lang.ClassCircularityError 114 | java.lang.ClassFormatError 115 | java.lang.Error 116 | java.lang.ExceptionInInitializerError 117 | java.lang.IllegalAccessError 118 | java.lang.IncompatibleClassChangeError 119 | java.lang.InstantiationError 120 | java.lang.InternalError 121 | java.lang.LinkageError 122 | java.lang.NoClassDefFoundError 123 | java.lang.NoSuchFieldError 124 | java.lang.NoSuchMethodError 125 | java.lang.OutOfMemoryError 126 | java.lang.StackOverflowError 127 | java.lang.ThreadDeath 128 | java.lang.UnknownError 129 | java.lang.UnsatisfiedLinkError 130 | java.lang.UnsupportedClassVersionError 131 | java.lang.VerifyError 132 | java.lang.VirtualMachineError 133 | 134 | java.util.ConcurrentModificationException 135 | java.util.EmptyStackException 136 | java.util.MissingResourceException 137 | java.util.NoSuchElementException 138 | java.util.TooManyListenersException 139 | 140 | java.io.IOError 141 | java.io.EOFException 142 | java.io.FileNotFoundException 143 | java.io.InterruptedIOException 144 | java.io.InvalidClassException 145 | java.io.InvalidObjectException 146 | java.io.IOException 147 | java.io.NotActiveException 148 | java.io.NotSerializableException 149 | java.io.OptionalDataException 150 | java.io.StreamCorruptedException 151 | java.io.SyncFailedException 152 | java.io.UnsupportedEncodingException 153 | java.io.UTFDataFormatException 154 | java.io.WriteAbortedException 155 | 156 | java.security.AccessControlException 157 | 158 | ``` -------------------------------------------------------------------------------- /shield/shield.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ***************************************** 4 | # * FILE: shield.sh * 5 | # * AUTOR: Hector Jose Flores Colmenarez * 6 | # * EMAIL: hecto932@gmail.com * 7 | # ***************************************** 8 | 9 | # FUNCTIONALITY 10 | # ============== 11 | 12 | # THE SHIELD MUST BE RUN THIS WAY: 13 | # ./shield.sh --c /absolutepath/example.c 14 | # ./shield.sh --cpp /absolutepath/example.cpp 15 | # ./shield.sh --py2 /absolutepath/example.py 16 | # ./shield.sh --py3 /absolutepath/example.py 17 | # ./shield.sh --java /absolutepath/example.java 18 | 19 | # FLAGS: 20 | # ===== 21 | # --c to language C 22 | # --cpp to C++ language 23 | # --py2 to Python language 24 | # --py3 to Python language 25 | # --java to Java language 26 | 27 | # STATUS CODES 28 | # ============ 29 | # 0 - SHIELD SUCCESS 30 | # 1 - SHIELD FAILED 31 | # 2 - FILE NOT FOUND 32 | # 3 - INCORRECT FLAG 33 | 34 | # GLOBALS 35 | # ======= 36 | 37 | SCRIPT=$(readlink -f "$0") 38 | SCRIPTPATH=$(dirname "$SCRIPT") 39 | SHIELD_PATH=$SCRIPTPATH 40 | 41 | # COMPILER OPTIONS FOR C/C++ 42 | # ========================== 43 | C_COMPILER="gcc" 44 | C_OPTIONS="-fno-asm -Dasm=error -lm -O2" 45 | C_WARNING_OPTION="-w" 46 | C_EXEFILE="1" 47 | C_SHIELD="shield_c.c" 48 | C_FLAG="--c" 49 | C_EXT="c" 50 | C_BLACKLIST="blacklist_c.h" 51 | 52 | CPP_COMPILER="g++" 53 | CPP_EXEFILE="1" 54 | CPP_SHIELD="shield_cpp.cpp" 55 | CPP_FLAG="--cpp" 56 | CPP_EXT="cpp" 57 | CPP_BLACKLIST="blacklist_cpp.h" 58 | # -w: Inhibit all warning messages 59 | # -Werror: Make all warnings into errors 60 | # -Wall ... 61 | # Read more: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html 62 | # Read more: http://www.eis.uva.es/~fergay/III/enlaces/gcc.html 63 | 64 | # COMPILER OPTIONS FOR PYTHON2 && PYTHON3 65 | # ======================================= 66 | PY2_COMPILER="python" 67 | PY3_COMPILER="python3" 68 | PY2_FLAG="--py2" 69 | PY3_FLAG="--py3" 70 | PY_EXT="py" 71 | PY2_SHIELD="shield_python2.py" 72 | PY3_SHIELD="shield_python3.py" 73 | PY_OPTIONS="-O -m py_compile" 74 | 75 | # COMPILER OPTIONS FOR JAVA 76 | # ========================= 77 | JAVA_FLAG="--java" 78 | JAVA_COMPILER="javac" 79 | JAVA_RUNNER="java" 80 | JAVA_EXT="java" 81 | JAVA_POLICY="-Djava.security.manager -Djava.security.policy=java.policy" 82 | JAVA_FILEPOLICY="java.policy" 83 | 84 | # GETTINGS ARGUMENTS 85 | # ================== 86 | 87 | # 1.- FLAG 88 | FLAG=${1} 89 | 90 | # 2.- PROBLEM DIRECTORY(FULL PATH) 91 | PROBLEMPATH=${2} 92 | 93 | # 3.- FILE 94 | FILE=${PROBLEMPATH##*/} 95 | 96 | # 4.- EXTENSION FILE 97 | EXT="${FILE#*.}" 98 | 99 | # 5.- CLASS NAME(ONLY FOR JAVA) 100 | CLASSNAME="${FILE%.*}" 101 | 102 | # 6.- DIRECTORY OF PROBLEM 103 | DIRECTORY=$(dirname "$PROBLEMPATH") 104 | 105 | # 7.- FILENAME(WITHOUT EXTENSION) 106 | FILENAME="${FILE%.*}" 107 | 108 | # 8.- ROUTE OF THE SOLUTION 109 | ROUTEOFSOLUTION="$DIRECTORY/shield_$FILENAME$(date +"%d%m%Y%H%M%S")" 110 | 111 | # 9.- LOG FILE 112 | LOG="$ROUTEOFSOLUTION/shield_log" 113 | 114 | # 10.- EXIT_CODE 115 | EXIT_CODE=3 116 | 117 | ################# FUNCTIONS ################# 118 | function write_log 119 | { 120 | echo -e "$@" >> $LOG 121 | } 122 | 123 | function rmShieldFilesC 124 | { 125 | rm $DIRECTORY/$CLASSNAME >/dev/null 2>/dev/null 126 | rm $DIRECTORY/output.out >/dev/null 2>/dev/null 127 | rm $DIRECTORY/*.h >/dev/null 2>/dev/null 128 | rm $DIRECTORY/code.c >/dev/null 2>/dev/null && rm $DIRECTORY/shield_c.c >/dev/null 2>/dev/null 129 | rm $DIRECTORY/cerr >/dev/null 2>/dev/null 130 | rm $DIRECTORY/shield_log >/dev/null 2>/dev/null 131 | rm $DIRECTORY/*.css >/dev/null 2>/dev/null 132 | rm $DIRECTORY/*.html >/dev/null 2>/dev/null 133 | } 134 | 135 | function rmShieldFilesCPP 136 | { 137 | rm $DIRECTORY/$CLASSNAME >/dev/null 2>/dev/null 138 | rm $DIRECTORY/output.out >/dev/null 2>/dev/null 139 | rm $DIRECTORY/*.h >/dev/null 2>/dev/null 140 | rm $DIRECTORY/code.c >/dev/null 2>/dev/null && rm $DIRECTORY/shield_cpp.cpp >/dev/null 2>/dev/null 141 | rm $DIRECTORY/cerr >/dev/null 2>/dev/null 142 | rm $DIRECTORY/shield_log >/dev/null 2>/dev/null 143 | rm $DIRECTORY/*.css >/dev/null 2>/dev/null 144 | rm $DIRECTORY/*.html >/dev/null 2>/dev/null 145 | } 146 | 147 | function rmShieldFilesPython2 148 | { 149 | rm $DIRECTORY/*.pyo 2>/dev/null 150 | rm $DIRECTORY/shield_log 2>/dev/null 151 | rm $DIRECTORY/cerr 2>/dev/null 152 | rm $DIRECTORY/shield_python2.py 2>/dev/null 153 | rm $DIRECTORY/*.css >/dev/null 2>/dev/null 154 | rm $DIRECTORY/*.html >/dev/null 2>/dev/null 155 | } 156 | 157 | function rmShieldFilesPython3 158 | { 159 | rm $DIRECTORY/*.pyo 2>/dev/null 160 | rm $DIRECTORY/shield_log 2>/dev/null 161 | rm $DIRECTORY/cerr 2>/dev/null 162 | rm $DIRECTORY/shield_python3.py 2>/dev/null 163 | rm $DIRECTORY/*.css >/dev/null 2>/dev/null 164 | rm $DIRECTORY/*.html >/dev/null 2>/dev/null 165 | } 166 | 167 | function rmShieldFilesJava 168 | { 169 | rm $DIRECTORY/cerr 2>/dev/null 170 | rm $DIRECTORY/err 2>/dev/null 171 | rm $DIRECTORY/java.policy 2>/dev/null 172 | rm $DIRECTORY/*.class 2>/dev/null 173 | rm $DIRECTORY/*.css 2>/dev/null 174 | rm $DIRECTORY/*.html 2>/dev/null 175 | } 176 | 177 | # IF EXIST FILE PROBLEM 178 | if [[ ! -f "$PROBLEMPATH" ]]; then 179 | echo "$FILE doesn't exist." 180 | EXIT_CODE=2 181 | echo 182 | fi 183 | 184 | ############################ C ############################ 185 | 186 | if [[ $FLAG == $C_FLAG && $EXT == $C_EXT ]]; then 187 | rmShieldFilesC 188 | cp $PROBLEMPATH $DIRECTORY/code.c 189 | # if code contains any 'undef', raise compile error: 190 | if tr -d ' \t\n\r\f' < $DIRECTORY/code.c | grep -q '#undef'; then 191 | echo 'code.c:#undef is not allowed' >cerr 192 | EXIT_CODE=1 193 | else 194 | cp $SHIELD_PATH/$C_SHIELD $DIRECTORY/$C_SHIELD 195 | cp $SHIELD_PATH/$C_BLACKLIST $DIRECTORY/$C_BLACKLIST 196 | echo '#define main thenewmainfunction' | cat - $DIRECTORY/code.c > thetemp && mv thetemp $DIRECTORY/code.c 197 | $C_COMPILER $DIRECTORY/$C_SHIELD $C_OPTIONS $C_WARNING_OPTION -o $DIRECTORY/$CLASSNAME >/dev/null 2>$DIRECTORY/cerr 198 | EXIT_CODE=$? 199 | 200 | fi 201 | fi 202 | 203 | ############################ C++ ########################### 204 | 205 | if [[ $EXT == $CPP_EXT && $FLAG == $CPP_FLAG ]]; then 206 | rmShieldFilesCPP 207 | cp $PROBLEMPATH $DIRECTORY/code.c 208 | # if code contains any 'undef', raise compile error: 209 | if tr -d ' \t\n\r\f' < $DIRECTORY/code.c | grep -q '#undef'; then 210 | echo 'code.c:#undef is not allowed' >cerr 211 | EXIT_CODE=1 212 | else 213 | cp $SHIELD_PATH/$CPP_SHIELD $DIRECTORY/$CPP_SHIELD 214 | cp $SHIELD_PATH/$CPP_BLACKLIST $DIRECTORY/$CPP_BLACKLIST 215 | echo '#define main thenewmainfunction' | cat - $DIRECTORY/code.c > thetemp && mv thetemp $DIRECTORY/code.c 216 | $CPP_COMPILER $DIRECTORY/$CPP_SHIELD $C_OPTIONS $C_WARNING_OPTION -o $DIRECTORY/$CLASSNAME >/dev/null 2>$DIRECTORY/cerr 217 | EXIT_CODE=$? 218 | fi 219 | fi 220 | 221 | ########################## PYTHON2 ########################## 222 | 223 | if [[ $FLAG == $PY2_FLAG && $PY_EXT == $EXT ]]; then 224 | rmShieldFilesPython2 225 | cp $SHIELD_PATH/$PY2_SHIELD $DIRECTORY/$PY2_SHIELD 226 | cat $SCRIPTPATH/$PY2_SHIELD | cat - $DIRECTORY/$FILE > thetemp && mv thetemp $DIRECTORY/$FILE 227 | $PY2_COMPILER $PY_OPTIONS $DIRECTORY/$FILE >/dev/null 2>$DIRECTORY/cerr 228 | EXIT_CODE=$? 229 | fi 230 | 231 | ########################## PYTHON3 ########################## 232 | 233 | if [[ $FLAG == $PY3_FLAG && $PY_EXT == $EXT ]]; then 234 | rmShieldFilesPython3 235 | cp $SHIELD_PATH/$PY3_SHIELD $DIRECTORY/$PY3_SHIELD 236 | cat $SCRIPTPATH/$PY3_SHIELD | cat - $DIRECTORY/$FILE > thetemp && mv thetemp $DIRECTORY/$FILE 237 | $PY3_COMPILER $PY_OPTIONS $DIRECTORY/$FILE >/dev/null 2>$DIRECTORY/cerr 238 | EXIT_CODE=$? 239 | fi 240 | 241 | ########################## JAVA ########################## 242 | 243 | if [[ $FLAG == $JAVA_FLAG && $EXT == $JAVA_EXT ]]; then 244 | rmShieldFilesJava 245 | cp $SHIELD_PATH/java.policy $DIRECTORY/java.policy 246 | $JAVA_COMPILER $DIRECTORY/$FILE >/dev/null 2>$DIRECTORY/cerr 247 | EXIT_CODE=$? 248 | fi 249 | 250 | echo $EXIT_CODE 251 | exit $EXIT_CODE -------------------------------------------------------------------------------- /shield/shield_c.c: -------------------------------------------------------------------------------- 1 | #include "blacklist_c.h" 2 | 3 | int thenewmainfunction(); 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | thenewmainfunction(); 8 | return 0; 9 | } 10 | 11 | #include "code.c" -------------------------------------------------------------------------------- /shield/shield_cpp.cpp: -------------------------------------------------------------------------------- 1 | //REFERENCE : https://github.com/mjnaderi/Sharif-Judge/blob/7ca3101bb848447e8d8aabb14aae0d6ae9dfde31/tester/shield/defcpp.h 2 | /* 3 | SI DESEA AGREGAR UNA NUEVA DESCRIPCCION 4 | SOLO COLOQUELA DEBAJO DE LAS LINEAS "#define" 5 | */ 6 | 7 | 8 | #include "blacklist_cpp.h" 9 | 10 | int thenewmainfunction(); 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | 15 | thenewmainfunction(); 16 | 17 | return 0; 18 | } 19 | 20 | #include "code.c" 21 | -------------------------------------------------------------------------------- /shield/shield_python2.py: -------------------------------------------------------------------------------- 1 | # REFERENCE: https://github.com/mjnaderi/Sharif-Judge/blob/7ca3101bb848447e8d8aabb14aae0d6ae9dfde31/tester/shield/shield_py2.py 2 | 3 | #LIBRERIAS 4 | import sys 5 | sys.modules['os'] = None #EXCLUYE OS 6 | 7 | #LISTA DE MODULOS A NO PERMITIR 8 | BLACKLIST = [ 9 | #'__import__', #SI ESTE MODULO SE DESCOMENTA NEGAMOS CUALQUIER TIPO DE IMPORTE DE MODULOS 10 | 'eval', #EVAL ES UN DEMONIO 11 | 'open', 12 | 'file', 13 | 'exec', 14 | 'execfile', 15 | 'compile', 16 | 'reload', 17 | 'input' #INPUT EN PYTHON2 USA EVAL 18 | ] 19 | 20 | #PARA CADA MODULO DE LA BLACKLIST 21 | for module in BLACKLIST: 22 | #VERIFICAMOS SI ESTA EN LA LISTA DE MODULOS 23 | if module in __builtins__.__dict__: 24 | #LA ELIMINAMOS 25 | del __builtins__.__dict__[module] 26 | 27 | -------------------------------------------------------------------------------- /shield/shield_python3.py: -------------------------------------------------------------------------------- 1 | #REFERENCES : https://github.com/mjnaderi/Sharif-Judge/blob/7ca3101bb848447e8d8aabb14aae0d6ae9dfde31/tester/shield/shield_py3.py 2 | #LIBRERIAS 3 | import sys 4 | sys.modules['os'] = None #EXCLUYE OS 5 | 6 | #LISTA DE MODULOS A NO PERMITIR 7 | BLACKLIST = [ 8 | #'__import__', #SI ESTE MODULO SE DESCOMENTA NEGAMOS CUALQUIER TIPO DE IMPORTE DE MODULOS 9 | 'eval', #EVAL ES UN DEMONIO 10 | 'open', 11 | 'file', 12 | 'exec', 13 | 'execfile', 14 | 'compile', 15 | 'reload', 16 | #'input' 17 | ] 18 | 19 | #PARA CADA MODULO DE LA BLACKLIST 20 | for module in BLACKLIST: 21 | #VERIFICAMOS SI ESTA EN LA LISTA DE MODULOS 22 | if module in __builtins__.__dict__: 23 | #LA ELIMINAMOS 24 | del __builtins__.__dict__[module] 25 | 26 | -------------------------------------------------------------------------------- /tests/1-java-hello/HelloWorld.java: -------------------------------------------------------------------------------- 1 | public class HelloWorld { 2 | 3 | public static void main(String[] args) { 4 | System.out.println("Hello, World"); 5 | } 6 | 7 | } 8 | -------------------------------------------------------------------------------- /tests/1-java-hello/expected.txt: -------------------------------------------------------------------------------- 1 | Hello, World 2 | -------------------------------------------------------------------------------- /tests/10055-hashmat/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hecto932/core-judge/ad80c3a707dafbc4a39dd7ea79da42988ed332c6/tests/10055-hashmat/Main.class -------------------------------------------------------------------------------- /tests/10055-hashmat/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.math.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner s = new Scanner(System.in); 8 | while(s.hasNext()) 9 | System.out.println(s.nextBigInteger() 10 | .subtract(s.nextBigInteger()).abs()); 11 | } 12 | } -------------------------------------------------------------------------------- /tests/10055-hashmat/expected.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 4 3 | 100 4 | -------------------------------------------------------------------------------- /tests/10055-hashmat/input.txt: -------------------------------------------------------------------------------- 1 | 10 12 2 | 10 14 3 | 100 200 -------------------------------------------------------------------------------- /tests/10194-football/Football.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | struct datos 17 | { 18 | int points; 19 | int games; 20 | int win; 21 | int tie; 22 | int lost; 23 | int df; 24 | int goalsf; 25 | int goalsc; 26 | 27 | }; 28 | 29 | string toMayus(string s) 30 | { 31 | unsigned int i=0; 32 | for(i=0; ipar1 , pairpar2) 39 | { 40 | if(par1.second.points!=par2.second.points) return(par1.second.points>par2.second.points); 41 | if(par1.second.win!=par2.second.win) return(par1.second.win>par2.second.win); 42 | if(par1.second.df!=par2.second.df) return(par1.second.df>par2.second.df); 43 | if(par1.second.goalsf!=par2.second.goalsf) return(par1.second.goalsf>par2.second.goalsf); 44 | if(par1.second.games!=par2.second.games) return(par1.second.games!=par2.second.games); 45 | if(toMayus(par1.first)!=toMayus(par2.first)) return toMayus(par1.first) f; 59 | getline(cin,copa); 60 | scanf("%d\n",&nequipos); 61 | while(nequipos--) 62 | { 63 | getline(cin,equipo); 64 | f[equipo].points=0; 65 | f[equipo].games=0; 66 | f[equipo].win=0; 67 | f[equipo].tie=0; 68 | f[equipo].lost=0; 69 | f[equipo].df=0; 70 | f[equipo].goalsf=0; 71 | f[equipo].goalsc=0; 72 | } 73 | scanf("%d\n",&matches); 74 | while(matches--) 75 | { 76 | scanf("%[^#]#%d@%d#%[^\n]\n",team1,&t1,&t2,team2); 77 | string equipo1(team1); 78 | string equipo2(team2); 79 | f[equipo1].games++; 80 | f[equipo2].games++; 81 | f[equipo1].goalsf+=t1; 82 | f[equipo2].goalsf+=t2; 83 | f[equipo1].goalsc+=t2; 84 | f[equipo2].goalsc+=t1; 85 | f[equipo1].df+=(t1-t2); 86 | f[equipo2].df+=(t2-t1); 87 | if(t1>t2) 88 | { 89 | f[equipo1].win++; 90 | f[equipo2].lost++; 91 | f[equipo1].points+=3; 92 | } 93 | else if(t1 res; 112 | res=*min_element(f.begin(),f.end(),compare); 113 | printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n",i+1,res.first.c_str(),res.second.points,res.second.games,res.second.win,res.second.tie,res.second.lost,res.second.df,res.second.goalsf,res.second.goalsc); 114 | f.erase(res.first); 115 | } 116 | if(testCase) printf("\n"); 117 | } 118 | 119 | 120 | return 0; 121 | } -------------------------------------------------------------------------------- /tests/10194-football/Sandbox.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hecto932/core-judge/ad80c3a707dafbc4a39dd7ea79da42988ed332c6/tests/10194-football/Sandbox.so -------------------------------------------------------------------------------- /tests/10194-football/expected.txt: -------------------------------------------------------------------------------- 1 | World Cup 1998 - Group A 2 | 1) Brazil 6p, 3g (2-0-1), 3gd (6-3) 3 | 2) Norway 5p, 3g (1-2-0), 1gd (5-4) 4 | 3) Morocco 4p, 3g (1-1-1), 0gd (5-5) 5 | 4) Scotland 1p, 3g (0-1-2), -4gd (2-6) 6 | 7 | Some strange tournament 8 | 1) Team D 4p, 2g (1-1-0), 1gd (2-1) 9 | 2) Team E 3p, 2g (1-0-1), 0gd (3-3) 10 | 3) Team A 3p, 3g (0-3-0), 0gd (3-3) 11 | 4) Team B 1p, 1g (0-1-0), 0gd (1-1) 12 | 5) Team C 1p, 2g (0-1-1), -1gd (3-4) 13 | -------------------------------------------------------------------------------- /tests/10194-football/input.txt: -------------------------------------------------------------------------------- 1 | 2 2 | World Cup 1998 - Group A 3 | 4 4 | Brazil 5 | Norway 6 | Morocco 7 | Scotland 8 | 6 9 | Brazil#2@1#Scotland 10 | Norway#2@2#Morocco 11 | Scotland#1@1#Norway 12 | Brazil#3@0#Morocco 13 | Morocco#3@0#Scotland 14 | Brazil#1@2#Norway 15 | Some strange tournament 16 | 5 17 | Team A 18 | Team B 19 | Team C 20 | Team D 21 | Team E 22 | 5 23 | Team A#1@1#Team B 24 | Team A#2@2#Team C 25 | Team A#0@0#Team D 26 | Team E#2@1#Team C 27 | Team E#1@2#Team D -------------------------------------------------------------------------------- /tests/10194-football/sandbox.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hecto932/core-judge/ad80c3a707dafbc4a39dd7ea79da42988ed332c6/tests/10194-football/sandbox.so -------------------------------------------------------------------------------- /tests/10420-list-of-conquests/Conquests.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | map paises; 9 | 10 | int main() 11 | { 12 | int n; 13 | char s[100]; 14 | string str; 15 | 16 | cin >> n; 17 | for(int i=0;i> str; 20 | cin.getline(s,100); 21 | ++paises[str]; 22 | } 23 | for(map::iterator it=paises.begin(); it!=paises.end();++it) 24 | cout << it->first << " " << it->second << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /tests/10420-list-of-conquests/expected.txt: -------------------------------------------------------------------------------- 1 | England 1 2 | Spain 2 3 | -------------------------------------------------------------------------------- /tests/10420-list-of-conquests/input.txt: -------------------------------------------------------------------------------- 1 | 3 2 | Spain Donna Elvira 3 | England Jane Doe 4 | Spain Donna Anna -------------------------------------------------------------------------------- /tests/10474-where-is-the-marble/Marble.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int n,q,x,caso=0; 8 | vector v; 9 | vector::iterator itr; 10 | 11 | int main() 12 | { 13 | while((cin >> n >> q)&&(n!=0 && q!=0)) 14 | { 15 | cout << "CASE# " << ++caso << ":" << endl; 16 | v.clear(); 17 | for(int i = n ; i > 0 ; --i) 18 | { 19 | cin >> x; 20 | v.push_back(x); 21 | } 22 | sort(v.begin(),v.end()); 23 | for(int i = q ; i > 0 ; --i) 24 | { 25 | cin >> x; 26 | bool ok=false; 27 | for(int j = 0; j < n && !ok; ++j) 28 | { 29 | if(v[j]==x) 30 | { 31 | cout << x << " found at " << j+1 << endl; 32 | ok=true; 33 | } 34 | else if(v[j]>x) 35 | break; 36 | 37 | } 38 | if(!ok) 39 | cout << x << " not found" << endl; 40 | } 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /tests/10474-where-is-the-marble/expected.txt: -------------------------------------------------------------------------------- 1 | CASE# 1: 2 | 5 found at 4 3 | CASE# 2: 4 | 2 not found 5 | 3 found at 3 6 | -------------------------------------------------------------------------------- /tests/10474-where-is-the-marble/input.txt: -------------------------------------------------------------------------------- 1 | 4 1 2 | 2 3 | 3 4 | 5 5 | 1 6 | 5 7 | 5 2 8 | 1 9 | 3 10 | 3 11 | 3 12 | 1 13 | 2 14 | 3 15 | 0 0 16 | -------------------------------------------------------------------------------- /tests/10785-the-mad-numerologist/Numerologist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | char const vocales[] = "AUEOI"; 7 | char const consonantes[] = "JSBKTCLDMVNWFXGPYHQZR"; 8 | 9 | int main() 10 | { 11 | int casos,n,imp,par; 12 | char ans1[10086]; 13 | char ans2[10086]; 14 | 15 | cin >> casos; 16 | for(int i=0;i> n; 20 | imp=par=0; 21 | for(int j=0;j 2 | #include 3 | 4 | int main() 5 | { 6 | int test,k,j,i,x; 7 | int v[2]; 8 | scanf("%d\n",&test); 9 | for(x=1;x<=test;++x) 10 | { 11 | scanf("%d\n",&k); 12 | j=0; 13 | for(i=2;j<2 && (j*j)<=k;++i) 14 | if(k%i==0) 15 | v[j++]=i; 16 | printf("Case #%d: %d = %d * %d = %d * %d\n",x,k,v[0],k/v[0],v[1],k/v[1]); 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /tests/10879-code-refactoring/expected.txt: -------------------------------------------------------------------------------- 1 | Case #1: 120 = 2 * 60 = 3 * 40 2 | Case #2: 210 = 2 * 105 = 3 * 70 3 | Case #3: 10000000 = 2 * 5000000 = 4 * 2500000 4 | -------------------------------------------------------------------------------- /tests/10879-code-refactoring/input.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 120 3 | 210 4 | 10000000 -------------------------------------------------------------------------------- /tests/113-power-of-cryptography/Power.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | double n,p; 10 | while(scanf("%lf%lf",&n,&p)==2) 11 | { 12 | printf("%.0lf\n",pow(p,1/n)); 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /tests/113-power-of-cryptography/expected.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 3 3 | 1234 4 | 1 5 | -------------------------------------------------------------------------------- /tests/113-power-of-cryptography/input.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 16 3 | 3 4 | 27 5 | 7 6 | 4357186184021382204544 7 | 200 8 | 1000000000 -------------------------------------------------------------------------------- /tests/123-searching-quickly/Searching.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | typedef list List; 12 | typedef string::iterator Its; 13 | typedef list::iterator itl; 14 | 15 | List palabra,titulo,key; 16 | 17 | int main() 18 | { 19 | string str; 20 | while(cin >> str && str!="::") 21 | palabra.push_back(str); 22 | palabra.sort(); 23 | while(getline(cin,str)) 24 | { 25 | for(Its it = str.begin();it!=str.end();++it) 26 | *it=tolower(*it); 27 | istringstream sin(str); 28 | string buf; 29 | while(sin >> buf) 30 | { 31 | if(binary_search(palabra.begin(),palabra.end(),buf)) 32 | continue; 33 | key.push_back(buf); 34 | } 35 | titulo.push_back(" " + str + " "); 36 | } 37 | key.sort(); 38 | key.unique(); 39 | for(itl it = key.begin();it!=key.end();++it) 40 | { 41 | string w = " " + (*it) + " "; 42 | for(itl it1 = titulo.begin(); it1 != titulo.end();++it1) 43 | { 44 | str = *it1; 45 | int pos = str.find(w) + 1, len = str.length()-1; 46 | while(pos>0) 47 | { 48 | str=*it1; 49 | while(pos < len && isalpha(str[pos])) 50 | { 51 | str[pos]=toupper(str[pos]); 52 | ++pos; 53 | } 54 | for(int i = 1 ; i < len;++i) 55 | putchar(str[i]); 56 | putchar(10); 57 | pos=str.find(w,pos)+1; 58 | } 59 | } 60 | } 61 | return 0; 62 | } -------------------------------------------------------------------------------- /tests/123-searching-quickly/expected.txt: -------------------------------------------------------------------------------- 1 | a portrait of the ARTIST as a young man 2 | the ASCENT of man 3 | a man is a man but BUBBLESORT is a dog 4 | DESCENT of man 5 | a man is a man but bubblesort is a DOG 6 | descent of MAN 7 | the ascent of MAN 8 | the old MAN and the sea 9 | a portrait of the artist as a young MAN 10 | a MAN is a man but bubblesort is a dog 11 | a man is a MAN but bubblesort is a dog 12 | the OLD man and the sea 13 | a PORTRAIT of the artist as a young man 14 | the old man and the SEA 15 | a portrait of the artist as a YOUNG man 16 | -------------------------------------------------------------------------------- /tests/123-searching-quickly/input.txt: -------------------------------------------------------------------------------- 1 | is 2 | the 3 | of 4 | and 5 | as 6 | a 7 | but 8 | :: 9 | Descent of Man 10 | The Ascent of Man 11 | The Old Man and The Sea 12 | A Portrait of The Artist As a Young Man 13 | A Man is a Man but Bubblesort IS A DOG -------------------------------------------------------------------------------- /tests/2-java-arithmetic/Arithmetic.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hecto932/core-judge/ad80c3a707dafbc4a39dd7ea79da42988ed332c6/tests/2-java-arithmetic/Arithmetic.class -------------------------------------------------------------------------------- /tests/2-java-arithmetic/Arithmetic.java: -------------------------------------------------------------------------------- 1 | import java.lang.ArithmeticException; 2 | 3 | public class Arithmetic { 4 | 5 | public static void main(String[] args) { 6 | System.out.println("Hello, World"); 7 | 8 | try { 9 | int i = 0 / 0; 10 | } catch (ArithmeticException e) { 11 | System.out.println("ArithmeticException occured!"); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /tests/2-java-arithmetic/output.txt: -------------------------------------------------------------------------------- 1 | Hello, World 2 | ArithmeticException occured! 3 | -------------------------------------------------------------------------------- /tests/340-master-mind-hints/Master.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | long long int leer(int *b,int n) 9 | { 10 | long long int cont=0; 11 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | string str[100+5]; 14 | 15 | int main() 16 | { 17 | int n; 18 | while(cin>>n) 19 | { 20 | int max=0; 21 | for(int i=0;i> str[i]; 24 | int l=str[i].length(); 25 | if(l>max) 26 | max=l; 27 | } 28 | sort(str,str+n); 29 | int columnas=62/(max+2); 30 | int just=max+2; 31 | int filas=ceil(n/(double)columnas); 32 | printf ("------------------------------------------------------------\n"); 33 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | int length; 14 | string str; 15 | string dir [100000 + 10]; 16 | 17 | char cambio(char c) 18 | { 19 | if(c=='A' || c=='B' || c=='C') return '2'; 20 | if(c=='D' || c=='E' || c=='F') return '3'; 21 | if(c=='G' || c=='H' || c=='I') return '4'; 22 | if(c=='J' || c=='K' || c=='L') return '5'; 23 | if(c=='M' || c=='N' || c=='O') return '6'; 24 | if(c=='P' || c=='R' || c=='S') return '7'; 25 | if(c=='T' || c=='U' || c=='V') return '8'; 26 | if(c=='W' || c=='X' || c=='Y') return '9'; 27 | return c; 28 | } 29 | 30 | void str_numero (char *a) 31 | { 32 | str.clear(); 33 | for(int i=0;a[i];++i) 34 | { 35 | if(isdigit(a[i])) 36 | str+=a[i]; 37 | else if(isalpha(a[i])) 38 | str+=cambio(a[i]); 39 | } 40 | str.insert(3,"-"); 41 | } 42 | 43 | int main() 44 | { 45 | int testcase; 46 | scanf("%d",&testcase); 47 | bool blanco=false; 48 | while(testcase--) 49 | { 50 | length=0; 51 | int directory; 52 | scanf("%d",&directory); 53 | getchar(); 54 | map d; 55 | while(directory--) 56 | { 57 | char numero[1000]; 58 | gets(numero); 59 | str_numero(numero); 60 | d[str]++; 61 | if(d[str]==2) dir[length++]=str; 62 | } 63 | sort(dir,dir+length); 64 | if(blanco) cout << endl; 65 | blanco=true; 66 | bool duplicates=false; 67 | for(int i=0;ilayout->set_title('Judge' . $this->layout->title_separator . 'New Language'); 16 | 17 | //ADD CSS AND JS HEADER 18 | $this->layout->add_includes('css', 'assets/css/plugins/dataTables/dataTables.bootstrap.css','header',$prepend_base_url = TRUE); 19 | $this->layout->add_includes('css', 'assets/css/plugins/dataTables/dataTables.responsive.css','header',$prepend_base_url = TRUE); 20 | $this->layout->add_includes('css', 'assets/css/plugins/dataTables/dataTables.tableTools.min.css','header',$prepend_base_url = TRUE); 21 | 22 | //ADD JS FOOTER 23 | $this->layout->add_includes('js', 'assets/js/plugins/dataTables/jquery.dataTables.js', 'footer',$prepend_base_url = TRUE); 24 | $this->layout->add_includes('js', 'assets/js/plugins/dataTables/dataTables.bootstrap.js', 'footer',$prepend_base_url = TRUE); 25 | $this->layout->add_includes('js', 'assets/js/plugins/dataTables/dataTables.responsive.js', 'footer',$prepend_base_url = TRUE); 26 | $this->layout->add_includes('js', 'assets/js/plugins/dataTables/dataTables.tableTools.min.js', 'footer',$prepend_base_url = TRUE); 27 | 28 | //CONF 29 | $this->layout->add_includes('js', 'assets/js/conf/admin/role.js', 'footer',$prepend_base_url = TRUE); 30 | 31 | if( isset($_POST['id']) && isset($_POST['role'])) 32 | { 33 | $data['id'] = $this->security->xss_clean(trim($this->input->post('id'))); 34 | $data['role'] = $this->security->xss_clean(trim($this->input->post('role'))); 35 | 36 | $data['idFlag'] = ( ( empty($data['id']) ) ? 1 : 0 ); 37 | $data['roleFlag'] = ( ( empty($data['role']) ) ? 1 : 0 ); 38 | 39 | if(! $data['idFlag'] || $data['roleFlag'] ) 40 | { 41 | echo $this->process->set_role($data); 42 | } 43 | 44 | 45 | } 46 | 47 | //GET PROBLEMS LIST 48 | $data['users'] = $this->process->get_users(); 49 | 50 | //GET ROLE LIST 51 | $data['role'] = $this->process->get_roles(); 52 | 53 | //LOAD VIEW 54 | $this->layout->view('language/newLanguage',$data); 55 | } 56 | 57 | //CARGAMOS LOS ADJUNTOS 58 | public function upload_shield($language_name) 59 | { 60 | //SI EXISTEN ADJUNTOS 61 | if(!empty($_FILES['shield'])) 62 | { 63 | foreach ($_FILES["shield"]["error"] as $key => $error) 64 | { 65 | if ($error == UPLOAD_ERR_OK) 66 | { 67 | $tmp_name = $_FILES["shield"]["tmp_name"][$key]; 68 | $name = $_FILES["shield"]["name"][$key]; 69 | $type = $_FILES["shield"]["type"][$key]; 70 | move_uploaded_file($tmp_name, "languages/$language_name/shield/$name"); 71 | if($type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') 72 | $type = 'DOCX'; 73 | elseif($type == 'application/pdf') 74 | $type = 'PDF'; 75 | elseif($type == 'application/zip') 76 | $type = 'ZIP'; 77 | elseif($type == 'application/vnd.ms-powerpoint') 78 | $type = 'PPT'; 79 | elseif($type == 'application/msword') 80 | $type = 'DOC'; 81 | } 82 | else 83 | { 84 | return $this->codeToMessage($error); 85 | } 86 | } 87 | } 88 | } 89 | 90 | //CARGAMOS LOS ADJUNTOS 91 | public function upload_sandbox($language_name) 92 | { 93 | //SI EXISTEN ADJUNTOS 94 | if(!empty($_FILES['shield'])) 95 | { 96 | foreach ($_FILES["sandbox"]["error"] as $key => $error) 97 | { 98 | if ($error == UPLOAD_ERR_OK) 99 | { 100 | $tmp_name = $_FILES["sandbox"]["tmp_name"][$key]; 101 | $name = $_FILES["sandbox"]["name"][$key]; 102 | $type = $_FILES["sandbox"]["type"][$key]; 103 | move_uploaded_file($tmp_name, "languages/$language_name/sandbox/$name"); 104 | if($type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') 105 | $type = 'DOCX'; 106 | elseif($type == 'application/pdf') 107 | $type = 'PDF'; 108 | elseif($type == 'application/zip') 109 | $type = 'ZIP'; 110 | elseif($type == 'application/vnd.ms-powerpoint') 111 | $type = 'PPT'; 112 | elseif($type == 'application/msword') 113 | $type = 'DOC'; 114 | } 115 | else 116 | { 117 | return $this->codeToMessage($error); 118 | } 119 | } 120 | } 121 | } 122 | 123 | public function upload_script($language_name) 124 | { 125 | //SI EXISTEN ADJUNTOS 126 | if(!empty($_FILES['script'])) 127 | { 128 | foreach ($_FILES["script"]["error"] as $key => $error) 129 | { 130 | if ($error == UPLOAD_ERR_OK) 131 | { 132 | $tmp_name = $_FILES["script"]["tmp_name"][$key]; 133 | $name = $_FILES["script"]["name"][$key]; 134 | $type = $_FILES["script"]["type"][$key]; 135 | move_uploaded_file($tmp_name, "languages/$language_name/$name"); 136 | if($type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') 137 | $type = 'DOCX'; 138 | elseif($type == 'application/pdf') 139 | $type = 'PDF'; 140 | elseif($type == 'application/zip') 141 | $type = 'ZIP'; 142 | elseif($type == 'application/vnd.ms-powerpoint') 143 | $type = 'PPT'; 144 | elseif($type == 'application/msword') 145 | $type = 'DOC'; 146 | } 147 | else 148 | { 149 | return $this->codeToMessage($error); 150 | } 151 | } 152 | } 153 | } 154 | 155 | public function newlanguage() 156 | { 157 | $this->form_validation->set_rules('name', 'Nombre', 'required'); 158 | $this->form_validation->set_rules('name','flag', 'required'); 159 | 160 | $this->form_validation->set_message('required', 'Este campo es requerido'); 161 | 162 | if($this->form_validation->run()) 163 | { 164 | // Estructura de la carpeta deseada 165 | $estructura = './languages/'.$this->input->post('name').'/'; 166 | 167 | // Para crear una estructura anidada se debe especificar 168 | // el parámetro $recursive en mkdir(). 169 | 170 | if(!mkdir($estructura, 0777, true)) 171 | { 172 | die('Fallo al crear las carpetas...'); 173 | }else 174 | { 175 | if(!empty($_FILES['shield'])) 176 | { 177 | $estructura = './languages/'.$this->input->post('name').'/shield'; 178 | mkdir($estructura, 0777, true); 179 | $this->upload_shield(); 180 | } 181 | 182 | if(!empty($_FILES['sandbox'])) 183 | { 184 | $estructura = './languages/'.$this->input->post('name').'/sandbox'; 185 | mkdir($estructura, 0777, true); 186 | $this->upload_sandbox(); 187 | } 188 | 189 | if(!empty($_FILES['script'])) 190 | { 191 | $estructura = './languages/'.$this->input->post('name').'/sandbox'; 192 | mkdir($estructura, 0777, true); 193 | $this->upload_script(); 194 | } 195 | } 196 | } 197 | else 198 | { 199 | //SET TITLE 200 | $this->layout->set_title('Judge' . $this->layout->title_separator . 'New Language'); 201 | 202 | //ADD CSS AND JS HEADER 203 | $this->layout->add_includes('css', 'assets/css/plugins/dataTables/dataTables.bootstrap.css','header',$prepend_base_url = TRUE); 204 | $this->layout->add_includes('css', 'assets/css/plugins/dataTables/dataTables.responsive.css','header',$prepend_base_url = TRUE); 205 | $this->layout->add_includes('css', 'assets/css/plugins/dataTables/dataTables.tableTools.min.css','header',$prepend_base_url = TRUE); 206 | 207 | //ADD JS FOOTER 208 | $this->layout->add_includes('js', 'assets/js/plugins/dataTables/jquery.dataTables.js', 'footer',$prepend_base_url = TRUE); 209 | $this->layout->add_includes('js', 'assets/js/plugins/dataTables/dataTables.bootstrap.js', 'footer',$prepend_base_url = TRUE); 210 | $this->layout->add_includes('js', 'assets/js/plugins/dataTables/dataTables.responsive.js', 'footer',$prepend_base_url = TRUE); 211 | $this->layout->add_includes('js', 'assets/js/plugins/dataTables/dataTables.tableTools.min.js', 'footer',$prepend_base_url = TRUE); 212 | 213 | //CONF 214 | $this->layout->add_includes('js', 'assets/js/conf/admin/role.js', 'footer',$prepend_base_url = TRUE); 215 | 216 | if( isset($_POST['id']) && isset($_POST['role'])) 217 | { 218 | $data['id'] = $this->security->xss_clean(trim($this->input->post('id'))); 219 | $data['role'] = $this->security->xss_clean(trim($this->input->post('role'))); 220 | 221 | $data['idFlag'] = ( ( empty($data['id']) ) ? 1 : 0 ); 222 | $data['roleFlag'] = ( ( empty($data['role']) ) ? 1 : 0 ); 223 | 224 | if(! $data['idFlag'] || $data['roleFlag'] ) 225 | { 226 | echo $this->process->set_role($data); 227 | } 228 | 229 | 230 | } 231 | 232 | //GET PROBLEMS LIST 233 | $data['users'] = $this->process->get_users(); 234 | 235 | //GET ROLE LIST 236 | $data['role'] = $this->process->get_roles(); 237 | 238 | //LOAD VIEW 239 | $this->layout->view('language/newLanguage',$data); 240 | } 241 | } 242 | 243 | } 244 | 245 | -------------------------------------------------------------------------------- /web/views/language/newLanguage.php: -------------------------------------------------------------------------------- 1 |
2 |
3 |

Nuevo lenguaje

4 | 15 |
16 |
17 | 18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
Lenguaje
26 |
27 |
28 |

29 | Nuevo Lenguaje 30 |

31 |
32 | 33 |

Información del lenguaje

34 |
35 |

Información

36 |
37 |
38 |
39 | 40 | 41 |
42 |
43 | 44 | 45 |
46 |
47 | 48 | 49 |
50 |
51 | 52 | 53 |
54 |
55 | 56 | 57 |
58 |
59 |
60 |
61 | 62 |
63 |
64 |
65 | 66 |
67 |
68 |
69 |
70 |
71 | 72 |
73 |
74 | --------------------------------------------------------------------------------