Ratankumar10/code-execution-engine
0
1#!/bin/bash2# ╔══════════════════════════════════════════════════════════════════╗3# ║ Sandbox Runner — BATCH MODE ║4# ║ Compiles ONCE, runs ALL test cases in one container ║5# ║ Usage: run.sh <language> <time_limit> <num_test_cases> ║6# ║ Supports: c, cpp, python ║7# ╚══════════════════════════════════════════════════════════════════╝8 9LANG="$1"10TIME_LIMIT="${2:-5}"11NUM_CASES="${3:-1}"12COMPILE_TIMEOUT=1013 14WORKDIR="/sandbox"15CODE_DIR="$WORKDIR/code"16TC_DIR="$WORKDIR/testcases"17RES_DIR="$WORKDIR/results"18 19# ─── Ensure results directory exists ───20mkdir -p "$RES_DIR"21 22# ─── Helper: write meta for a specific test case ───23write_meta() {24 local idx="$1" verdict="$2" time_ms="$3" memory="$4" exit_code="$5"25 echo "verdict=$verdict" > "$RES_DIR/$idx.meta"26 echo "time=$time_ms" >> "$RES_DIR/$idx.meta"27 echo "memory=$memory" >> "$RES_DIR/$idx.meta"28 echo "exitCode=$exit_code" >> "$RES_DIR/$idx.meta"29}30 31# ─── Helper: write CE for all remaining test cases ───32write_ce_all() {33 local stderr_msg="$1"34 for i in $(seq 1 "$NUM_CASES"); do35 write_meta "$i" "CE" "0" "0" "1"36 echo "" > "$RES_DIR/$i.out"37 echo "$stderr_msg" > "$RES_DIR/$i.err"38 done39}40 41# ═══════════════════════════════════════════════42# PHASE 1: COMPILATION (once for all test cases)43# ═══════════════════════════════════════════════44 45compile_result=046COMPILE_STDERR=""47 48case "$LANG" in49 c)50 COMPILE_STDERR=$(timeout $COMPILE_TIMEOUT gcc -O2 -Wall -o /tmp/solution "$CODE_DIR/solution.c" -lm 2>&1)51 compile_result=$?52 EXEC_CMD="/tmp/solution"53 ;;54 cpp)55 COMPILE_STDERR=$(timeout $COMPILE_TIMEOUT g++ -O2 -std=c++17 -Wall -o /tmp/solution "$CODE_DIR/solution.cpp" -lm 2>&1)56 compile_result=$?57 EXEC_CMD="/tmp/solution"58 ;;59 python)60 COMPILE_STDERR=$(timeout $COMPILE_TIMEOUT python3 -m py_compile "$CODE_DIR/solution.py" 2>&1)61 compile_result=$?62 EXEC_CMD="python3 $CODE_DIR/solution.py"63 ;;64 *)65 for i in $(seq 1 "$NUM_CASES"); do66 write_meta "$i" "IE" "0" "0" "1"67 echo "" > "$RES_DIR/$i.out"68 echo "Unsupported language: $LANG" > "$RES_DIR/$i.err"69 done70 exit 071 ;;72esac73 74if [ $compile_result -ne 0 ]; then75 write_ce_all "$COMPILE_STDERR"76 exit 077fi78 79# ═══════════════════════════════════════════════80# PHASE 2: RUN EACH TEST CASE81# ═══════════════════════════════════════════════82 83for i in $(seq 1 "$NUM_CASES"); do84 INPUT_FILE="$TC_DIR/$i.in"85 OUTPUT_FILE="$RES_DIR/$i.out"86 STDERR_FILE="$RES_DIR/$i.err"87 88 # Initialize output files89 echo -n "" > "$OUTPUT_FILE"90 echo -n "" > "$STDERR_FILE"91 92 # Default input if file missing93 if [ ! -f "$INPUT_FILE" ]; then94 INPUT_FILE="/dev/null"95 fi96 97 # Record start time98 START_MS=$(date +%s%3N)99 100 # Run with timeout101 timeout "$TIME_LIMIT" sh -c "$EXEC_CMD < \"$INPUT_FILE\" > \"$OUTPUT_FILE\" 2>\"$STDERR_FILE\""102 RUN_EXIT=$?103 104 # Record end time105 END_MS=$(date +%s%3N)106 EXEC_TIME_MS=$((END_MS - START_MS))107 108 # ─── Memory measurement ───109 PEAK_MEMORY=0110 if [ -f /sys/fs/cgroup/memory.peak ]; then111 PEAK_BYTES=$(cat /sys/fs/cgroup/memory.peak 2>/dev/null || echo 0)112 PEAK_MEMORY=$((PEAK_BYTES / 1024))113 elif [ -f /sys/fs/cgroup/memory/memory.max_usage_in_bytes ]; then114 PEAK_BYTES=$(cat /sys/fs/cgroup/memory/memory.max_usage_in_bytes 2>/dev/null || echo 0)115 PEAK_MEMORY=$((PEAK_BYTES / 1024))116 elif [ -f /sys/fs/cgroup/memory.current ]; then117 PEAK_BYTES=$(cat /sys/fs/cgroup/memory.current 2>/dev/null || echo 0)118 PEAK_MEMORY=$((PEAK_BYTES / 1024))119 fi120 121 # ─── Determine verdict ───122 if [ $RUN_EXIT -eq 124 ]; then123 VERDICT="TLE"124 elif [ $RUN_EXIT -eq 137 ]; then125 VERDICT="MLE"126 elif [ $RUN_EXIT -eq 139 ]; then127 echo "Segmentation fault (SIGSEGV)" >> "$STDERR_FILE"128 VERDICT="RE"129 elif [ $RUN_EXIT -eq 136 ]; then130 echo "Floating point exception (SIGFPE)" >> "$STDERR_FILE"131 VERDICT="RE"132 elif [ $RUN_EXIT -eq 134 ]; then133 echo "Aborted (SIGABRT)" >> "$STDERR_FILE"134 VERDICT="RE"135 elif [ $RUN_EXIT -ne 0 ]; then136 VERDICT="RE"137 else138 VERDICT="OK"139 fi140 141 # Write metadata for this test case142 write_meta "$i" "$VERDICT" "$EXEC_TIME_MS" "$PEAK_MEMORY" "$RUN_EXIT"143done144 145exit 0146 