126 lines
4.4 KiB
Go
126 lines
4.4 KiB
Go
package judge
|
|
|
|
import (
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"lincodex/runner/internal/sandbox"
|
|
)
|
|
|
|
// Step is one command of a compile phase.
|
|
type Step struct {
|
|
Argv []string
|
|
// WorkDir is the working directory in the jail; empty means the default.
|
|
WorkDir string
|
|
}
|
|
|
|
// Language describes how the sandbox builds and runs a source file. Every
|
|
// step runs in its own jail: compile steps may write sandbox.OutPath, test
|
|
// runs see it read-only.
|
|
type Language struct {
|
|
ID string
|
|
// SourceFile is the file name of the source inside sandbox.BoxPath.
|
|
SourceFile string
|
|
// Compile is the optional compile phase; the first failing step ends it.
|
|
Compile []Step
|
|
// CompileLimits bound every compile step; they do not depend on the run.
|
|
CompileLimits sandbox.Limits
|
|
CompileEnv []string
|
|
// Run is the command of every test; the test input is its stdin.
|
|
Run []string
|
|
RunEnv []string
|
|
// Requires lists host files the language needs; startup checks them.
|
|
Requires []string
|
|
}
|
|
|
|
// pythonSyntaxCheck compiles the source without running it and prints only
|
|
// the exception line, so that a syntax error is a compile_error with a short
|
|
// message. compile() never executes the program.
|
|
const pythonSyntaxCheck = `import sys, traceback
|
|
path = sys.argv[1]
|
|
try:
|
|
with open(path, "rb") as stream:
|
|
compile(stream.read(), "main.py", "exec", dont_inherit=True)
|
|
except (SyntaxError, ValueError) as error:
|
|
sys.stderr.write("".join(traceback.format_exception_only(type(error), error)))
|
|
sys.exit(1)
|
|
`
|
|
|
|
// Limits of the Python syntax check. A 64 KiB source compiles far below
|
|
// them; a pathological source fails with compile_error.
|
|
const (
|
|
pythonCompileCPU = 5 * time.Second
|
|
pythonCompileWall = 10 * time.Second
|
|
pythonCompileMemory = 256 << 20
|
|
compileOutput = 64 << 10
|
|
)
|
|
|
|
// Python3 returns the Python 3 language for the interpreter at path. The
|
|
// interpreter runs in isolated mode (-I: no environment variables, no user
|
|
// site directory, no script directory on sys.path) and never writes
|
|
// bytecode (-B).
|
|
func Python3(interpreter string) Language {
|
|
source := sandbox.BoxPath + "/main.py"
|
|
return Language{
|
|
ID: "python3",
|
|
SourceFile: "main.py",
|
|
Compile: []Step{{Argv: []string{interpreter, "-I", "-B", "-c", pythonSyntaxCheck, source}}},
|
|
CompileLimits: sandbox.Limits{
|
|
CPUTime: pythonCompileCPU,
|
|
WallTime: pythonCompileWall,
|
|
MemoryBytes: pythonCompileMemory,
|
|
OutputBytes: compileOutput,
|
|
},
|
|
Run: []string{interpreter, "-I", "-B", source},
|
|
Requires: []string{interpreter},
|
|
}
|
|
}
|
|
|
|
// Limits of the Go compile and link steps: the compiler and the linker of
|
|
// one file with the prebuilt standard library need well under a second and
|
|
// a few hundred MiB; the binary may be tens of MiB.
|
|
const (
|
|
goCompileCPU = 20 * time.Second
|
|
goCompileWall = 40 * time.Second
|
|
goCompileMemory = 768 << 20
|
|
goCompileFileSize = 64 << 20
|
|
)
|
|
|
|
// goEnv keeps the Go runtime on one processor, which matches the one-CPU
|
|
// cgroup quota and bounds the number of runtime threads.
|
|
var goEnv = []string{"GOMAXPROCS=1", "TMPDIR=" + sandbox.ScratchPath}
|
|
|
|
// Go returns the Go language for the tool directory built into the runner
|
|
// image: the compile and link tools and an importcfg that maps every
|
|
// standard library package to a prebuilt archive. The build therefore needs
|
|
// no Go build cache and no network, can import only the standard library,
|
|
// and has no cgo.
|
|
func Go(toolDir string) Language {
|
|
compiler := filepath.Join(toolDir, "compile")
|
|
linker := filepath.Join(toolDir, "link")
|
|
importcfg := filepath.Join(toolDir, "importcfg")
|
|
archive := sandbox.OutPath + "/main.a"
|
|
binary := sandbox.OutPath + "/main"
|
|
return Language{
|
|
ID: "go",
|
|
SourceFile: "main.go",
|
|
Compile: []Step{
|
|
// Compiling from the box with a relative path keeps host-free
|
|
// file names such as main.go:3:2 in diagnostics.
|
|
{Argv: []string{compiler, "-o", archive, "-p", "main", "-complete", "-pack", "-importcfg", importcfg, "main.go"}, WorkDir: sandbox.BoxPath},
|
|
{Argv: []string{linker, "-o", binary, "-importcfg", importcfg, "-buildmode=exe", "-s", "-w", archive}},
|
|
},
|
|
CompileLimits: sandbox.Limits{
|
|
CPUTime: goCompileCPU,
|
|
WallTime: goCompileWall,
|
|
MemoryBytes: goCompileMemory,
|
|
OutputBytes: compileOutput,
|
|
FileSizeBytes: goCompileFileSize,
|
|
},
|
|
CompileEnv: goEnv,
|
|
Run: []string{binary},
|
|
RunEnv: goEnv,
|
|
Requires: []string{compiler, linker, importcfg},
|
|
}
|
|
}
|