feat(runner): compile Go and check answers per test group

This commit is contained in:
shira committed 2026-10-11 08:59:29 +03:00
1 parent e95c681165
commit afee564bf4
61 files changed
+2592 -143

No files matched your search

+72 -9
View File
@@ -1,22 +1,36 @@
package judge
import (
"path/filepath"
"time"
"lincodex/runner/internal/sandbox"
)
// Language describes how the sandbox checks and runs a source file.
// 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 or syntax check command.
Compile []string
// CompileLimits bound the compile step; they do not depend on the run.
// 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
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
@@ -38,7 +52,7 @@ const (
pythonCompileCPU = 5 * time.Second
pythonCompileWall = 10 * time.Second
pythonCompileMemory = 256 << 20
pythonCompileOutput = 64 << 10
compileOutput = 64 << 10
)
// Python3 returns the Python 3 language for the interpreter at path. The
@@ -50,13 +64,62 @@ func Python3(interpreter string) Language {
return Language{
ID: "python3",
SourceFile: "main.py",
Compile: []string{interpreter, "-I", "-B", "-c", pythonSyntaxCheck, source},
Compile: []Step{{Argv: []string{interpreter, "-I", "-B", "-c", pythonSyntaxCheck, source}}},
CompileLimits: sandbox.Limits{
CPUTime: pythonCompileCPU,
WallTime: pythonCompileWall,
MemoryBytes: pythonCompileMemory,
OutputBytes: pythonCompileOutput,
OutputBytes: compileOutput,
},
Run: []string{interpreter, "-I", "-B", source},
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},
}
}