-
Notifications
You must be signed in to change notification settings - Fork 11
/
run.go
274 lines (247 loc) · 7.88 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package worker
import (
"database/sql"
"log"
"math"
"strconv"
"strings"
"time"
"github.com/jmoiron/sqlx"
"github.com/natsukagami/kjudge/models"
"github.com/pkg/errors"
)
// The filename of the "compare" binary.
const CompareFilename = "compare"
// RunContext is the context needed to run a test.
type RunContext struct {
DB *sqlx.Tx
Sub *models.Submission
Problem *models.Problem
TestGroup *models.TestGroup
Test *models.Test
}
// TimeLimit returns the time limit of the context, in time.Duration.
func (r *RunContext) TimeLimit() time.Duration {
if r.TestGroup.TimeLimit.Valid {
return time.Duration(r.TestGroup.TimeLimit.Int64) * time.Millisecond
}
return time.Duration(r.Problem.TimeLimit) * time.Millisecond
}
// MemoryLimit returns the memory limit of the context, in Kilobytes.
func (r *RunContext) MemoryLimit() int {
if r.TestGroup.MemoryLimit.Valid {
return int(r.TestGroup.MemoryLimit.Int64)
}
return r.Problem.MemoryLimit
}
// RunnCommand returns the run command (command, args list) for the language.
func RunCommand(l models.Language) (string, []string, error) {
switch l {
case models.LanguageJava:
return "/usr/bin/java", []string{"-Donline_judge=true", "-Dkjudge=true", "-Smx512M", "-Xss64M", "-cp", "code", "Main"}, nil
case models.LanguagePy2:
return "/usr/bin/python2", []string{"-S", "code"}, nil
case models.LanguagePy3:
return "/usr/bin/python3", []string{"-S", "code"}, nil
case models.LanguageCpp, models.LanguageGo, models.LanguageRust, models.LanguagePas:
return "code", nil, nil
default:
return "", nil, errors.New("unknown language")
}
}
// CompiledSource returns the CompiledSource. Returns false when the submission hasn't been compiled.
// Returns nil if the submission failed to compile.
func (r *RunContext) CompiledSource() (bool, []byte) {
if r.Sub.CompilerOutput == nil {
return false, nil
}
return true, r.Sub.CompiledSource
}
// RunInput creates a SandboxInput for running the submission's source.
func (r *RunContext) RunInput(source []byte) (*SandboxInput, error) {
command, args, err := RunCommand(r.Sub.Language)
if err != nil {
return nil, err
}
return &SandboxInput{
Command: command,
Args: args,
Files: nil,
TimeLimit: r.TimeLimit(),
MemoryLimit: r.MemoryLimit(),
CompiledSource: source,
Input: r.Test.Input,
}, nil
}
// CompareInput creates a SandboxInput for running the comparator.
// Also returns whether we have diff-based or comparator-based input.
func (r *RunContext) CompareInput(submissionOutput []byte) (input *SandboxInput, useComparator bool, err error) {
file, err := models.GetFileWithName(r.DB, r.Problem.ID, "compare")
if errors.Is(err, sql.ErrNoRows) {
// Use a simple diff
return &SandboxInput{
Command: "/usr/bin/diff",
Args: []string{"-wqts", "output", "expected"},
Files: map[string][]byte{"output": submissionOutput, "expected": r.Test.Output},
TimeLimit: time.Second,
MemoryLimit: 262144, // 256MBs
}, false, nil
}
if err != nil {
return nil, false, err
}
// Use the given comparator.
return &SandboxInput{
Command: "code",
Args: []string{"input", "expected", "output"},
Files: map[string][]byte{"input": r.Test.Input, "expected": r.Test.Output, "output": submissionOutput},
TimeLimit: 20 * time.Second,
MemoryLimit: (2 << 20), // 1 GB
CompiledSource: file.Content,
}, true, nil
}
func RunSingleCommand(sandbox Sandbox, r *RunContext, source []byte) (output *SandboxOutput, err error) {
// First, use the sandbox to run the submission itself.
input, err := r.RunInput(source)
if err != nil {
return nil, err
}
output, err = sandbox.Run(input)
if err != nil {
return nil, errors.WithStack(err)
}
return output, nil
}
func RunMutipleCommands(sandbox Sandbox, r *RunContext, source []byte, stages []string) (output *SandboxOutput, err error) {
command, args, err := RunCommand(r.Sub.Language)
if err != nil {
return nil, err
}
input := r.Test.Input
for i, stage := range stages {
// somehow Go includes EOF when splitting a string file line by line
if stage == "" && i == len(stages)-1 {
continue
}
stageArgs := strings.Split(stage, " ")
sandboxInput := &SandboxInput{
Command: command,
Args: append(stageArgs, args...),
Files: nil,
TimeLimit: r.TimeLimit(),
MemoryLimit: r.MemoryLimit(),
CompiledSource: source,
Input: input,
}
output, err = sandbox.Run(sandboxInput)
if err != nil {
return nil, err
}
// stop if the current run fails
if !output.Success {
break
}
// Next input in the chain will be the standard output of the previous command run
input = output.Stdout
}
return output, nil
}
// Run runs a RunContext.
func Run(sandbox Sandbox, r *RunContext) error {
compiled, source := r.CompiledSource()
if !compiled {
// Add a compilation job and re-add ourselves.
log.Printf("[WORKER] Submission %v not compiled, creating Compile job.\n", r.Sub.ID)
return models.BatchInsertJobs(r.DB, models.NewJobCompile(r.Sub.ID), models.NewJobRun(r.Sub.ID, r.Test.ID))
}
if source == nil {
log.Printf("[WORKER] Not running a submission that failed to compile.\n")
return nil
}
log.Printf("[WORKER] Running submission %v on [test `%v`, group `%v`]\n", r.Sub.ID, r.Test.Name, r.TestGroup.Name)
var output *SandboxOutput
file, err := models.GetFileWithName(r.DB, r.Problem.ID, ".stages")
if errors.Is(err, sql.ErrNoRows) {
// Problem type is not Chained Type, run a single command
output, err = RunSingleCommand(sandbox, r, source)
if err != nil {
return err
}
} else if err != nil {
return err
} else {
// Problem Type is Chained Type, we need to run mutiple commands with arguments from .stages (file)
stages := strings.Split(string(file.Content), "\n")
output, err = RunMutipleCommands(sandbox, r, source, stages)
if err != nil {
return err
}
}
result := parseSandboxOutput(output, r)
if !output.Success {
result.Verdict = "Runtime Error"
if output.ErrorMessage != "" {
result.Verdict = output.ErrorMessage
}
// If running the source did not succeed, we stop here and be happy with the test result.
return result.Write(r.DB)
}
// Attempt to run the comparator
input, useComparator, err := r.CompareInput(output.Stdout)
if err != nil {
return err
}
output, err = sandbox.Run(input)
if err != nil {
return err
}
if err := parseComparatorOutput(output, result, useComparator); err != nil {
return err
}
log.Printf("[WORKER] Done running submission %v on [test `%v`, group `%v`]: %.1f (t = %v, m = %v)\n",
r.Sub.ID, r.Test.Name, r.TestGroup.Name, result.Score, result.RunningTime, result.MemoryUsed)
return result.Write(r.DB)
}
// Parse the comparator's output and reflect it into `result`.
func parseComparatorOutput(s *SandboxOutput, result *models.TestResult, useComparator bool) error {
if useComparator {
// Paste the comparator's output to result
result.Verdict = strings.TrimSpace(string(s.Stderr))
if result.Verdict == "" {
result.Verdict = "Compare returns no output."
}
score, err := strconv.ParseFloat(strings.TrimSpace(string(s.Stdout)), 64)
if err != nil {
return errors.WithStack(err)
}
if math.IsInf(score, 0) || math.IsNaN(score) || score < 0 || score > 1 {
return errors.Errorf("invalid comparator score %f", score)
}
result.Score = score
} else {
// Cute message from diff
result.Verdict = strings.TrimSpace(string(s.Stdout))
if result.Verdict == "" {
result.Verdict = "Diff failed"
}
if !s.Success {
result.Score = 0.0
}
}
return nil
}
// Parse the sandbox output into a TestResult.
func parseSandboxOutput(s *SandboxOutput, r *RunContext) *models.TestResult {
score := 1.0
if !s.Success {
score = 0.0
}
return &models.TestResult{
MemoryUsed: s.MemoryUsed,
RunningTime: int(s.RunningTime / time.Millisecond),
Score: score,
SubmissionID: r.Sub.ID,
TestID: r.Test.ID,
Verdict: s.ErrorMessage,
}
}