init:
This commit is contained in:
405
runner/runner.go
Normal file
405
runner/runner.go
Normal file
@@ -0,0 +1,405 @@
|
||||
package runner
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Mond1c/judge/dsl"
|
||||
)
|
||||
|
||||
// MaxOutputBytes caps stdout/stderr captured from the solution process.
|
||||
// Prevents runaway student programs from OOM-ing the judge host.
|
||||
const MaxOutputBytes = 16 * 1024 * 1024 // 16 MiB
|
||||
|
||||
type Config struct {
|
||||
WorkDir string
|
||||
BinaryName string
|
||||
Wrapper string // CLI override, wins over DSL wrapper
|
||||
}
|
||||
|
||||
type Runner struct {
|
||||
cfg Config
|
||||
file *dsl.File
|
||||
binary string
|
||||
}
|
||||
|
||||
func New(f *dsl.File, cfg Config) *Runner {
|
||||
name := cfg.BinaryName
|
||||
if name == "" {
|
||||
name = f.Binary
|
||||
}
|
||||
if name == "" {
|
||||
name = "solution"
|
||||
}
|
||||
absWork, err := filepath.Abs(cfg.WorkDir)
|
||||
if err != nil {
|
||||
absWork = cfg.WorkDir
|
||||
}
|
||||
cfg.WorkDir = absWork
|
||||
return &Runner{file: f, cfg: cfg, binary: resolveBinary(absWork, name)}
|
||||
}
|
||||
|
||||
// resolveBinary picks <work>/name, falling back to <work>/name.exe on Windows.
|
||||
func resolveBinary(workDir, name string) string {
|
||||
primary := filepath.Join(workDir, name)
|
||||
if runtime.GOOS == "windows" && !strings.HasSuffix(strings.ToLower(name), ".exe") {
|
||||
if _, err := os.Stat(primary); err != nil {
|
||||
withExe := primary + ".exe"
|
||||
if _, err2 := os.Stat(withExe); err2 == nil {
|
||||
return withExe
|
||||
}
|
||||
}
|
||||
}
|
||||
return primary
|
||||
}
|
||||
|
||||
func (r *Runner) Run() *SuiteResult {
|
||||
result := &SuiteResult{}
|
||||
|
||||
buildLog, err := r.build()
|
||||
result.BuildLog = buildLog
|
||||
if err != nil {
|
||||
for _, g := range r.file.Groups {
|
||||
gr := &GroupResult{
|
||||
Name: g.Name,
|
||||
Weight: g.Weight,
|
||||
Score: 0,
|
||||
}
|
||||
|
||||
total := len(g.Tests)
|
||||
if g.Pattern != nil {
|
||||
total = -1
|
||||
}
|
||||
gr.Total = total
|
||||
for _, t := range g.Tests {
|
||||
gr.Tests = append(gr.Tests, &TestResult{
|
||||
Name: t.Name,
|
||||
Status: StatusBuildError,
|
||||
})
|
||||
}
|
||||
result.Groups = append(result.Groups, gr)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// After build, re-resolve binary (the exe may have been produced just now).
|
||||
r.binary = resolveBinary(r.cfg.WorkDir, filepath.Base(r.binary))
|
||||
|
||||
for _, g := range r.file.Groups {
|
||||
gr := r.runGroup(g)
|
||||
result.Groups = append(result.Groups, gr)
|
||||
result.TotalScore += gr.Score
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// buildCommand picks the most specific build command for this OS.
|
||||
func (r *Runner) buildCommand() string {
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
if r.file.BuildWindows != "" {
|
||||
return r.file.BuildWindows
|
||||
}
|
||||
case "linux":
|
||||
if r.file.BuildLinux != "" {
|
||||
return r.file.BuildLinux
|
||||
}
|
||||
case "darwin":
|
||||
if r.file.BuildDarwin != "" {
|
||||
return r.file.BuildDarwin
|
||||
}
|
||||
}
|
||||
if r.file.Build != "" {
|
||||
return r.file.Build
|
||||
}
|
||||
return "go build -o solution ."
|
||||
}
|
||||
|
||||
func (r *Runner) build() (string, error) {
|
||||
buildCmd := r.buildCommand()
|
||||
|
||||
ctx := context.Background()
|
||||
if r.file.Timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, r.file.Timeout)
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
cmd := shellCommand(ctx, buildCmd)
|
||||
cmd.Dir = r.cfg.WorkDir
|
||||
cmd.Env = os.Environ()
|
||||
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &out
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
return out.String(), fmt.Errorf("build failed: %w\n%s", err, out.String())
|
||||
}
|
||||
return out.String(), nil
|
||||
}
|
||||
|
||||
// shellCommand runs a command string through the platform's shell so env vars
|
||||
// like $CC expand naturally from the CI matrix.
|
||||
func shellCommand(ctx context.Context, cmdline string) *exec.Cmd {
|
||||
if runtime.GOOS == "windows" {
|
||||
return exec.CommandContext(ctx, "cmd", "/C", cmdline)
|
||||
}
|
||||
return exec.CommandContext(ctx, "sh", "-c", cmdline)
|
||||
}
|
||||
|
||||
func (r *Runner) runGroup(g *dsl.Group) *GroupResult {
|
||||
gr := &GroupResult{
|
||||
Name: g.Name,
|
||||
Weight: g.Weight,
|
||||
}
|
||||
|
||||
tests := g.Tests
|
||||
|
||||
if g.Pattern != nil {
|
||||
expanded, err := expandPattern(g.Pattern, &zeroChecker{})
|
||||
if err != nil {
|
||||
gr.Tests = append(gr.Tests, &TestResult{
|
||||
Name: "pattern_expand",
|
||||
Status: StatusFail,
|
||||
Failures: []string{fmt.Sprintf("pattern expand error: %v", err)},
|
||||
})
|
||||
gr.Total = 1
|
||||
gr.Score = 0
|
||||
return gr
|
||||
}
|
||||
for _, t := range expanded {
|
||||
if t.Timeout == 0 {
|
||||
t.Timeout = g.Timeout
|
||||
}
|
||||
for k, v := range g.Env {
|
||||
if _, ok := t.Env[k]; !ok {
|
||||
t.Env[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
tests = append(tests, expanded...)
|
||||
}
|
||||
|
||||
gr.Total = len(tests)
|
||||
|
||||
for _, t := range tests {
|
||||
for k, v := range g.Env {
|
||||
if _, ok := t.Env[k]; !ok {
|
||||
t.Env[k] = v
|
||||
}
|
||||
}
|
||||
if t.Timeout == 0 {
|
||||
t.Timeout = g.Timeout
|
||||
}
|
||||
if t.Wrapper == "" {
|
||||
t.Wrapper = g.Wrapper
|
||||
}
|
||||
|
||||
tr := r.runTest(t)
|
||||
gr.Tests = append(gr.Tests, tr)
|
||||
if tr.Status == StatusPass {
|
||||
gr.Passed++
|
||||
}
|
||||
}
|
||||
|
||||
switch g.Scoring {
|
||||
case dsl.ScoringAllOrNone:
|
||||
if gr.Passed == gr.Total {
|
||||
gr.Score = g.Weight
|
||||
} else {
|
||||
gr.Score = 0
|
||||
}
|
||||
default:
|
||||
if gr.Total > 0 {
|
||||
gr.Score = g.Weight * float64(gr.Passed) / float64(gr.Total)
|
||||
}
|
||||
}
|
||||
|
||||
return gr
|
||||
}
|
||||
|
||||
func (r *Runner) runTest(t *dsl.Test) *TestResult {
|
||||
tr := &TestResult{Name: t.Name, Status: StatusPass}
|
||||
|
||||
tmpDir, err := os.MkdirTemp("", "judge-test-*")
|
||||
if err != nil {
|
||||
tr.Status = StatusRuntimeError
|
||||
tr.Failures = append(tr.Failures, fmt.Sprintf("failed to create temp dir: %v", err))
|
||||
return tr
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
for name, content := range t.InFiles {
|
||||
path := filepath.Join(tmpDir, name)
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
||||
tr.fail("mkdir for file %q: %v", name, err)
|
||||
return tr
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
||||
tr.fail("write input file %q: %v", name, err)
|
||||
return tr
|
||||
}
|
||||
}
|
||||
|
||||
timeout := t.Timeout
|
||||
if timeout == 0 {
|
||||
timeout = 10 * time.Second
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
// Wrapper precedence: CLI flag > test > group (already copied into test).
|
||||
wrapper := r.cfg.Wrapper
|
||||
if wrapper == "" {
|
||||
wrapper = t.Wrapper
|
||||
}
|
||||
|
||||
cmd := buildExecCmd(ctx, wrapper, r.binary, t.Args)
|
||||
cmd.Dir = tmpDir
|
||||
|
||||
cmd.Env = os.Environ()
|
||||
// Force C locale so numeric formatting (decimal separator, etc.) is stable
|
||||
// across runners. Student code can still override via test env.
|
||||
cmd.Env = append(cmd.Env, "LC_ALL=C", "LANG=C")
|
||||
for k, v := range t.Env {
|
||||
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
|
||||
}
|
||||
|
||||
if t.Stdin != nil {
|
||||
cmd.Stdin = strings.NewReader(*t.Stdin)
|
||||
}
|
||||
|
||||
stdout := &cappedBuffer{limit: MaxOutputBytes}
|
||||
stderr := &cappedBuffer{limit: MaxOutputBytes}
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
|
||||
start := time.Now()
|
||||
runErr := cmd.Run()
|
||||
tr.Elapsed = time.Since(start)
|
||||
|
||||
tr.ActualStdout = normalizeOutput(stdout.String(), r.file)
|
||||
tr.ActualStderr = normalizeOutput(stderr.String(), r.file)
|
||||
|
||||
if stdout.truncated || stderr.truncated {
|
||||
tr.fail("output truncated at %d bytes (possible runaway output)", MaxOutputBytes)
|
||||
}
|
||||
|
||||
if ctx.Err() == context.DeadlineExceeded {
|
||||
tr.Status = StatusTLE
|
||||
tr.Failures = append(tr.Failures, fmt.Sprintf("time limit exceeded (%v)", timeout))
|
||||
return tr
|
||||
}
|
||||
|
||||
actualCode := 0
|
||||
if runErr != nil {
|
||||
if exitErr, ok := runErr.(*exec.ExitError); ok {
|
||||
actualCode = exitErr.ExitCode()
|
||||
} else {
|
||||
tr.Status = StatusRuntimeError
|
||||
tr.fail("runtime error: %v", runErr)
|
||||
return tr
|
||||
}
|
||||
}
|
||||
tr.ActualCode = actualCode
|
||||
|
||||
if t.ExitCode != nil && actualCode != *t.ExitCode {
|
||||
tr.fail("exit code: expected %d, got %d", *t.ExitCode, actualCode)
|
||||
}
|
||||
|
||||
for _, f := range applyMatcher("stdout", t.Stdout, tr.ActualStdout) {
|
||||
tr.fail("%s", f)
|
||||
}
|
||||
for _, f := range applyMatcher("stderr", t.Stderr, tr.ActualStderr) {
|
||||
tr.fail("%s", f)
|
||||
}
|
||||
|
||||
for name, expected := range t.OutFiles {
|
||||
path := filepath.Join(tmpDir, name)
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
tr.fail("output file %q not found: %v", name, err)
|
||||
continue
|
||||
}
|
||||
actual := normalizeOutput(string(content), r.file)
|
||||
for _, f := range applyMatcher(fmt.Sprintf("file(%s)", name), dsl.ExactMatcher{Value: expected}, actual) {
|
||||
tr.fail("%s", f)
|
||||
}
|
||||
}
|
||||
|
||||
if len(tr.Failures) > 0 {
|
||||
tr.Status = StatusFail
|
||||
}
|
||||
|
||||
return tr
|
||||
}
|
||||
|
||||
// buildExecCmd creates an exec.Cmd for the solution, optionally prefixed with
|
||||
// a wrapper (gdb, valgrind, qemu, ...). Wrapper is a shell string so users can
|
||||
// pass flags like "valgrind --error-exitcode=99 --leak-check=full".
|
||||
func buildExecCmd(ctx context.Context, wrapper, binary string, args []string) *exec.Cmd {
|
||||
if wrapper == "" {
|
||||
return exec.CommandContext(ctx, binary, args...)
|
||||
}
|
||||
wrapperFields := strings.Fields(wrapper)
|
||||
full := append(wrapperFields, binary)
|
||||
full = append(full, args...)
|
||||
return exec.CommandContext(ctx, full[0], full[1:]...)
|
||||
}
|
||||
|
||||
// normalizeOutput applies the file-level CRLF / trailing-WS rules.
|
||||
func normalizeOutput(s string, f *dsl.File) string {
|
||||
if f.NormalizeCRLF {
|
||||
s = strings.ReplaceAll(s, "\r\n", "\n")
|
||||
s = strings.ReplaceAll(s, "\r", "\n")
|
||||
}
|
||||
if f.TrimTrailingWS {
|
||||
lines := strings.Split(s, "\n")
|
||||
for i := range lines {
|
||||
lines[i] = strings.TrimRight(lines[i], " \t\r")
|
||||
}
|
||||
s = strings.Join(lines, "\n")
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// cappedBuffer stops writing once limit bytes are captured, but keeps draining
|
||||
// the source so the child process doesn't block on a full pipe.
|
||||
type cappedBuffer struct {
|
||||
buf bytes.Buffer
|
||||
limit int
|
||||
truncated bool
|
||||
}
|
||||
|
||||
func (c *cappedBuffer) Write(p []byte) (int, error) {
|
||||
room := c.limit - c.buf.Len()
|
||||
if room <= 0 {
|
||||
c.truncated = true
|
||||
return len(p), nil // pretend we wrote, to keep the pipe flowing
|
||||
}
|
||||
if len(p) > room {
|
||||
c.buf.Write(p[:room])
|
||||
c.truncated = true
|
||||
return len(p), nil
|
||||
}
|
||||
return c.buf.Write(p)
|
||||
}
|
||||
|
||||
func (c *cappedBuffer) String() string { return c.buf.String() }
|
||||
|
||||
var _ io.Writer = (*cappedBuffer)(nil)
|
||||
|
||||
type zeroChecker struct{}
|
||||
|
||||
func (z *zeroChecker) IsZero() bool { return true }
|
||||
Reference in New Issue
Block a user