Files

137 lines
4.3 KiB
Go

package judge
import (
"context"
"crypto/sha256"
"errors"
"fmt"
"log/slog"
"slices"
"time"
"lincodex/runner/internal/checker"
"lincodex/runner/internal/sandbox"
)
// Limits of one custom checker call. They belong to the task family, not to
// the candidate's limits, and they never count toward the candidate's time.
const (
checkerCPU = 5 * time.Second
checkerWall = 10 * time.Second
checkerMemory = 256 << 20
checkerOutput = 64 << 10
)
// Exit statuses of a custom checker program.
const (
checkerAccepts = 0
checkerRejects = 1
)
// Files a custom checker receives, as arguments, under sandbox.CheckPath.
var checkFileNames = []string{"input", "expected", "actual"}
var errCheckerFailed = errors.New("custom checker failed")
// checkerSet builds the checkers of one run. A custom checker program is
// compiled once per run and reused by every group that names it.
type checkerSet struct {
judge *Judge
custom map[[sha256.Size]byte]*customChecker
}
func newCheckerSet(judge *Judge) *checkerSet {
return &checkerSet{judge: judge, custom: make(map[[sha256.Size]byte]*customChecker)}
}
func (s *checkerSet) get(ctx context.Context, spec checker.Spec) (checker.Checker, error) {
if spec.Kind != checker.Custom {
return checker.New(spec)
}
if spec.Program == nil {
return nil, errors.New("judge.checkerSet.get - custom checker without a program")
}
key := sha256.Sum256([]byte(spec.Program.Language + "\x00" + spec.Program.Source))
if existing, ok := s.custom[key]; ok {
return existing, nil
}
built, err := s.build(ctx, *spec.Program)
if err != nil {
return nil, err
}
s.custom[key] = built
return built, nil
}
// build compiles a checker program in its own box. Its diagnostics are not
// returned: the checker is task family content, so a failure is a failure
// of the runner, not of the candidate.
func (s *checkerSet) build(ctx context.Context, program checker.Program) (*customChecker, error) {
language, ok := s.judge.languages[program.Language]
if !ok {
return nil, fmt.Errorf("judge.checkerSet.build - unsupported checker language %q", program.Language)
}
box, err := s.judge.executor.NewBox(language.SourceFile, []byte(program.Source))
if err != nil {
return nil, fmt.Errorf("judge.checkerSet.build - box: %w", err)
}
compile, ok := s.judge.compile(ctx, language, box)
if !ok {
return nil, errors.Join(fmt.Errorf("judge.checkerSet.build - compile: %s", compile.Outcome), box.Remove())
}
return &customChecker{executor: s.judge.executor, language: language, box: box}, nil
}
func (s *checkerSet) close(ctx context.Context) {
for _, built := range s.custom {
if err := built.box.Remove(); err != nil {
s.judge.logger.ErrorContext(ctx, "cannot remove the checker directory", slog.Any("error", err))
}
}
}
// customChecker runs a family checker program in the sandbox with the same
// isolation as a candidate program. It sees the input, the expected output,
// and the actual output as read-only files and answers with its exit
// status; its own output is discarded and never returned.
type customChecker struct {
executor Executor
language Language
box *sandbox.Box
}
func (c *customChecker) Check(ctx context.Context, input checker.Input) (accepted bool, err error) {
mount, remove, err := c.box.AddCheckFiles(map[string][]byte{
checkFileNames[0]: input.TestInput,
checkFileNames[1]: input.Expected,
checkFileNames[2]: input.Actual,
})
if err != nil {
return false, err
}
defer func() {
err = errors.Join(err, remove())
}()
argv := slices.Clone(c.language.Run)
for _, name := range checkFileNames {
argv = append(argv, sandbox.CheckPath+"/"+name)
}
run, err := c.executor.Run(ctx, sandbox.Spec{
Argv: argv,
Mounts: append(c.box.Mounts(false), mount),
Env: c.language.RunEnv,
}, sandbox.Limits{CPUTime: checkerCPU, WallTime: checkerWall, MemoryBytes: checkerMemory, OutputBytes: checkerOutput})
if err != nil {
return false, fmt.Errorf("judge.customChecker.Check - run: %w", err)
}
if run.Termination == sandbox.TerminationNone && run.Signal == 0 {
switch run.ExitCode {
case checkerAccepts:
return true, nil
case checkerRejects:
return false, nil
}
}
return false, fmt.Errorf("judge.customChecker.Check - exit %d, signal %d, limit %q: %w", run.ExitCode, run.Signal, run.Termination, errCheckerFailed)
}