@@ -5,59 +5,107 @@ set -e
55# Security: Set resource limits
66ulimit -t 30 # CPU time limit (30 seconds)
77ulimit -f 10240 # File size limit (10MB)
8- ulimit -v 134217728 # Virtual memory limit (128MB)
8+ ulimit -v 268435456 # Virtual memory limit (256MB) - increased for C++
99ulimit -n 50 # Max open files
1010ulimit -u 50 # Max user processes
1111
12- # Read JSON input from stdin
13- input=$( cat)
14-
15- # Parse JSON to extract code and input
16- code=$( echo " $input " | jq -r ' .code // ""' )
17- test_input=$( echo " $input " | jq -r ' .input // ""' )
18-
19- # Validate code
20- if [ -z " $code " ]; then
21- echo ' {"success": false, "output": "", "error": "No code provided"}'
22- exit 0
12+ # Handle direct execution mode (when called from Docker executor)
13+ if [ $# -eq 0 ]; then
14+ # Legacy JSON mode for backward compatibility
15+ input=$( cat)
16+ code=$( echo " $input " | jq -r ' .code // ""' )
17+ test_input=$( echo " $input " | jq -r ' .input // ""' )
18+
19+ if [ -z " $code " ]; then
20+ echo ' {"success": false, "output": "", "error": "No code provided"}'
21+ exit 0
22+ fi
23+
24+ echo " $code " > /app/solution.cpp
25+ echo " $test_input " > /app/input.txt
2326fi
2427
25- # Create temporary file
26- temp_file=" /tmp/solution_$( date +%s%N) .cpp"
27- executable=" /tmp/solution_$( date +%s%N) "
28+ # Check if solution file exists
29+ if [ ! -f " /app/solution.cpp" ]; then
30+ echo " Error: solution.cpp not found"
31+ exit 1
32+ fi
2833
29- # Write code to file
30- echo " $code " > " $temp_file "
34+ # Create unique identifiers to avoid conflicts
35+ timestamp=$( date +%s%N)
36+ executable=" /tmp/solution_${timestamp} "
3137
32- # Compile with security flags
38+ # Compile C++ code with optimized flags
39+ compile_start=$( date +%s%N)
3340compile_output=$( g++ -std=c++17 -Wall -Wextra -O2 \
3441 -fstack-protector-strong -D_FORTIFY_SOURCE=2 \
3542 -fno-exec-stack -fPIE -pie \
36- " $temp_file " -o " $executable " 2>&1 ) || {
43+ -static-libgcc -static-libstdc++ \
44+ /app/solution.cpp -o " $executable " 2>&1 ) || {
45+
46+ # Clean compilation error output
47+ clean_error=$( echo " $compile_output " | sed ' s|/app/solution.cpp|solution.cpp|g' | head -20)
3748
38- echo " {\" success\" : false, \" output\" : \"\" , \" error\" : \" Compilation Error: $compile_output \" }"
39- rm -f " $temp_file " " $executable "
40- exit 0
49+ if [ $# -eq 0 ]; then
50+ echo " {\" success\" : false, \" output\" : \"\" , \" error\" : \" Compilation Error: $clean_error \" }"
51+ else
52+ echo " Compilation Error: $clean_error "
53+ fi
54+ rm -f " $executable "
55+ exit 1
4156}
57+ compile_end=$( date +%s%N)
58+ compile_time=$(( (compile_end - compile_start) / 1000000 ))
59+
60+ # Set executable permissions
61+ chmod +x " $executable "
62+
63+ # Prepare input
64+ if [ -f " /app/input.txt" ]; then
65+ input_data=$( cat /app/input.txt)
66+ else
67+ input_data=" "
68+ fi
4269
4370# Execute with timeout and capture output
44- timeout 30s bash -c " echo '$test_input ' | $executable " > /tmp/output.txt 2>&1 || {
71+ exec_start=$( date +%s%N)
72+ timeout 30s bash -c " echo '$input_data ' | $executable " > /tmp/output.txt 2>&1 || {
4573 exit_code=$?
74+ exec_end=$( date +%s%N)
75+ exec_time=$(( (exec_end - exec_start) / 1000000 ))
76+
4677 if [ $exit_code -eq 124 ]; then
47- echo ' {"success": false, "output": "", "error": "Time Limit Exceeded"}'
78+ if [ $# -eq 0 ]; then
79+ echo ' {"success": false, "output": "", "error": "Time Limit Exceeded (30s)", "compilationTime": ' $compile_time ' , "executionTime": 30000}'
80+ else
81+ echo " Time Limit Exceeded (30s)"
82+ fi
4883 else
49- error_output=$( cat /tmp/output.txt 2> /dev/null || echo " Runtime Error" )
50- echo " {\" success\" : false, \" output\" : \"\" , \" error\" : \" Runtime Error: $error_output \" }"
84+ error_output=$( cat /tmp/output.txt 2> /dev/null | head -100 || echo " Runtime Error" )
85+ if [ $# -eq 0 ]; then
86+ echo " {\" success\" : false, \" output\" : \"\" , \" error\" : \" Runtime Error: $error_output \" , \" compilationTime\" : $compile_time , \" executionTime\" : $exec_time }"
87+ else
88+ echo " Runtime Error: $error_output "
89+ fi
5190 fi
52- rm -f " $temp_file " " $ executable" /tmp/output.txt
53- exit 0
91+ rm -f " $executable " /tmp/output.txt
92+ exit $exit_code
5493}
94+ exec_end=$( date +%s%N)
95+ exec_time=$(( (exec_end - exec_start) / 1000000 ))
5596
5697# Get output
5798output=$( cat /tmp/output.txt 2> /dev/null || echo " " )
5899
59100# Clean up
60- rm -f " $temp_file " " $ executable" /tmp/output.txt
101+ rm -f " $executable " /tmp/output.txt
61102
62103# Return success result
63- echo " {\" success\" : true, \" output\" : \" $output \" , \" error\" : \"\" }"
104+ if [ $# -eq 0 ]; then
105+ # JSON mode
106+ output_escaped=$( echo " $output " | sed ' s/"/\\"/g' | sed ' s/$/\\n/' | tr -d ' \n' | sed ' s/\\n$//' )
107+ echo " {\" success\" : true, \" output\" : \" $output_escaped \" , \" error\" : \"\" , \" compilationTime\" : $compile_time , \" executionTime\" : $exec_time }"
108+ else
109+ # Direct output mode
110+ echo " $output "
111+ fi
0 commit comments