ojudge

A self-hosted competitive programming site written in OCaml
git clone git://git.laack.co/ojudge.git
Log | Files | Refs | README

commit 85f0ba4460e7e7c22f8ff22083a3f881573f1c32
parent 7063c47ab9a8d983282d14a63431c927c026969d
Author: Andrew Laack <andrew@laack.co>
Date:   Tue, 17 Nov 2026 10:43:11 -0600

Working on executor, the program to run in the container

Diffstat:
M.gitignore | 4++++
MMakefile | 1+
Mojudge/bin/dune | 5+++++
Aojudge/bin/executor.ml | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 73 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,7 @@ cgi/ +*.cmi +*.cmx +*.out +*.o _build _build* diff --git a/Makefile b/Makefile @@ -3,6 +3,7 @@ ocaml: cd ojudge && dune build cp ./ojudge/_build/install/default/bin/home cgi/ cp ./ojudge/_build/install/default/bin/problem cgi/ + cp ./ojudge/_build/install/default/bin/executor cgi/ cp ./ojudge/_build/install/default/bin/submit cgi/ ensure-config: diff --git a/ojudge/bin/dune b/ojudge/bin/dune @@ -12,3 +12,8 @@ (public_name submit) (name submit) (libraries ojudge unix uuidm)) + +(executable + (public_name executor) + (name executor) + (libraries ojudge unix)) diff --git a/ojudge/bin/executor.ml b/ojudge/bin/executor.ml @@ -0,0 +1,63 @@ +(* This is the executor process that runs within the container, compiling and executing the user's code *) + +(* + +In this container we have the following directories bind mounted in read-only: + - /code + - user's code + - we detect the compilation strategy based on file extension + - if .ml -> ocaml + - /inputs + - inputs to evaluate against + +Since these mounts are read only we are expected to: + - Place artifacts in /tmp for compilation and such + - Output results to stdout + - This is done as follows: + - Print a status integer: + - 0 + - 1 - COMPILE ERROR + - 2 - TLE + - (there will likely be more in the future) + - Print a space + - Print the size of the test execution's output in bytes + - e.g. 20394 + - Print delimiter + - in our case "\n", everything after the first newline is stdout until reading in BYTES + - Print captured stdout +*) + + +let execCmd cmd = + let inp = Unix.open_process_in cmd in + let r = In_channel.input_all inp in + match Unix.close_process_in inp with + | Unix.WEXITED code -> (r, code) + | Unix.WSIGNALED s -> (r, 128 + s) + | Unix.WSTOPPED s -> (r, 128 + s) + +let copyOcamlToTemp () = + (* TODO: Handle failures *) + let _ = execCmd "cp /code/main.ml /tmp/" in () + +let runTest filename = + let output = execCmd ("cat /inputs/" ^ filename ^ "| /tmp/exe.out") in print_string (fst output) + +let runTests () = + let arr = Sys.readdir "/inputs" in + let rec acc idx = if idx = Array.length arr then () else + (runTest arr.(idx); (acc (idx+1))) + in acc 0 + +let compile () = + let () = copyOcamlToTemp () in + let output = execCmd "cd /tmp && ocamlopt nums.cmxa str.cmxa -unsafe main.ml -o exe.out 2>&1" in + if (snd output) <> 0 then (print_endline ("1 " ^ (fst output)); false) + else true + +let () = + let compPass = compile () in + if compPass then + runTests () + else + ()