feat(runner): compile Go and check answers per test group
This commit is contained in:
1 parent
e95c681165
commit
afee564bf4
61 files changed
+2592
-143
No files matched your search
@@ -3,6 +3,7 @@ package sandbox
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -20,47 +21,143 @@ const (
|
||||
boxPrefix = "run-"
|
||||
)
|
||||
|
||||
// Box is the host directory with the files of one run. It is mounted
|
||||
// read-only into every jail of the run and removed with Remove.
|
||||
// Box is the host directory of one program: its source, mounted read-only
|
||||
// at BoxPath, and its build output, mounted at OutPath, writable only for
|
||||
// compile steps. Remove deletes it.
|
||||
type Box struct {
|
||||
dir string
|
||||
root string
|
||||
}
|
||||
|
||||
// Owner is the user that may write a box's build output. A negative ID
|
||||
// leaves the owner unchanged, which unit tests use.
|
||||
type Owner struct {
|
||||
UID int
|
||||
GID int
|
||||
}
|
||||
|
||||
const (
|
||||
boxSourceDir = "src"
|
||||
boxOutputDir = "out"
|
||||
checkPrefix = "check-"
|
||||
)
|
||||
|
||||
// Dir returns the host path of the box.
|
||||
func (b *Box) Dir() string {
|
||||
return b.dir
|
||||
return b.root
|
||||
}
|
||||
|
||||
// CreateBox creates a box under root with one file.
|
||||
func CreateBox(root, fileName string, content []byte) (*Box, error) {
|
||||
if fileName == "" || fileName != filepath.Base(fileName) || strings.HasPrefix(fileName, ".") {
|
||||
// CreateBox creates a box under root with one source file.
|
||||
func CreateBox(root, fileName string, content []byte, owner Owner) (*Box, error) {
|
||||
if !validFileName(fileName) {
|
||||
return nil, fmt.Errorf("sandbox.CreateBox - invalid file name %q", fileName)
|
||||
}
|
||||
dir, err := os.MkdirTemp(root, boxPrefix)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sandbox.CreateBox - create: %w", err)
|
||||
}
|
||||
box := &Box{dir: dir}
|
||||
if err := os.WriteFile(filepath.Join(dir, fileName), content, boxFileMode); err != nil {
|
||||
return nil, errors.Join(fmt.Errorf("sandbox.CreateBox - write: %w", err), box.Remove())
|
||||
}
|
||||
if err := os.Chmod(dir, boxDirMode); err != nil {
|
||||
return nil, errors.Join(fmt.Errorf("sandbox.CreateBox - chmod: %w", err), box.Remove())
|
||||
box := &Box{root: dir}
|
||||
if err := box.populate(fileName, content, owner); err != nil {
|
||||
return nil, errors.Join(err, box.Remove())
|
||||
}
|
||||
return box, nil
|
||||
}
|
||||
|
||||
// Remove deletes the box and its files.
|
||||
func (b *Box) Remove() error {
|
||||
if err := os.Chmod(b.dir, ownerOnlyMode); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return fmt.Errorf("sandbox.Box.Remove - chmod: %w", err)
|
||||
func (b *Box) populate(fileName string, content []byte, owner Owner) error {
|
||||
source := filepath.Join(b.root, boxSourceDir)
|
||||
if err := writeReadOnlyDir(source, map[string][]byte{fileName: content}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.RemoveAll(b.dir); err != nil {
|
||||
return fmt.Errorf("sandbox.Box.Remove - remove: %w", err)
|
||||
output := filepath.Join(b.root, boxOutputDir)
|
||||
if err := os.Mkdir(output, ownerOnlyMode); err != nil {
|
||||
return fmt.Errorf("sandbox.Box.populate - create output: %w", err)
|
||||
}
|
||||
if err := os.Chown(output, owner.UID, owner.GID); err != nil {
|
||||
return fmt.Errorf("sandbox.Box.populate - chown output: %w", err)
|
||||
}
|
||||
if err := os.Chmod(b.root, workRootMode); err != nil {
|
||||
return fmt.Errorf("sandbox.Box.populate - chmod: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Mounts returns the source and output mounts of the box.
|
||||
func (b *Box) Mounts(outputWritable bool) []Mount {
|
||||
return []Mount{
|
||||
{Source: filepath.Join(b.root, boxSourceDir), Target: BoxPath},
|
||||
{Source: filepath.Join(b.root, boxOutputDir), Target: OutPath, Writable: outputWritable},
|
||||
}
|
||||
}
|
||||
|
||||
// AddCheckFiles writes read-only files for one custom checker call and
|
||||
// returns their mount at CheckPath and a function that deletes them.
|
||||
func (b *Box) AddCheckFiles(files map[string][]byte) (Mount, func() error, error) {
|
||||
for name := range files {
|
||||
if !validFileName(name) {
|
||||
return Mount{}, nil, fmt.Errorf("sandbox.Box.AddCheckFiles - invalid file name %q", name)
|
||||
}
|
||||
}
|
||||
dir, err := os.MkdirTemp(b.root, checkPrefix)
|
||||
if err != nil {
|
||||
return Mount{}, nil, fmt.Errorf("sandbox.Box.AddCheckFiles - create: %w", err)
|
||||
}
|
||||
remove := func() error { return removeTree(dir) }
|
||||
if err := fillReadOnlyDir(dir, files); err != nil {
|
||||
return Mount{}, nil, errors.Join(err, remove())
|
||||
}
|
||||
return Mount{Source: dir, Target: CheckPath}, remove, nil
|
||||
}
|
||||
|
||||
// Remove deletes the box and its files.
|
||||
func (b *Box) Remove() error {
|
||||
return removeTree(b.root)
|
||||
}
|
||||
|
||||
// writeReadOnlyDir creates dir with read-only files that the jailed user
|
||||
// can read but not change.
|
||||
func writeReadOnlyDir(dir string, files map[string][]byte) error {
|
||||
if err := os.Mkdir(dir, ownerOnlyMode); err != nil {
|
||||
return fmt.Errorf("sandbox.writeReadOnlyDir - create: %w", err)
|
||||
}
|
||||
return fillReadOnlyDir(dir, files)
|
||||
}
|
||||
|
||||
// fillReadOnlyDir writes files into an existing directory and makes both
|
||||
// read-only.
|
||||
func fillReadOnlyDir(dir string, files map[string][]byte) error {
|
||||
for name, content := range files {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), content, boxFileMode); err != nil {
|
||||
return fmt.Errorf("sandbox.fillReadOnlyDir - write: %w", err)
|
||||
}
|
||||
}
|
||||
if err := os.Chmod(dir, boxDirMode); err != nil {
|
||||
return fmt.Errorf("sandbox.fillReadOnlyDir - chmod: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// removeTree makes read-only directories writable again and removes path.
|
||||
// The sandbox user owns build output and may have left symlinks there, so
|
||||
// the walk goes through an os.Root, which never follows a link out of path.
|
||||
func removeTree(path string) error {
|
||||
if root, err := os.OpenRoot(path); err == nil {
|
||||
_ = fs.WalkDir(root.FS(), ".", func(name string, entry fs.DirEntry, walkErr error) error {
|
||||
if walkErr == nil && entry.IsDir() {
|
||||
_ = root.Chmod(name, ownerOnlyMode) // RemoveAll reports what remains.
|
||||
}
|
||||
return nil
|
||||
})
|
||||
_ = root.Close() // Only the walk used it.
|
||||
}
|
||||
if err := os.RemoveAll(path); err != nil {
|
||||
return fmt.Errorf("sandbox.removeTree - remove: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validFileName(name string) bool {
|
||||
return name != "" && name == filepath.Base(name) && !strings.HasPrefix(name, ".")
|
||||
}
|
||||
|
||||
// emptyWorkRoot removes every entry of root, for example boxes of a process
|
||||
// that crashed, and creates root when it is missing.
|
||||
func emptyWorkRoot(root string) error {
|
||||
@@ -75,8 +172,7 @@ func emptyWorkRoot(root string) error {
|
||||
return fmt.Errorf("sandbox.emptyWorkRoot - list: %w", err)
|
||||
}
|
||||
for _, entry := range entries {
|
||||
box := &Box{dir: filepath.Join(root, entry.Name())}
|
||||
if err := box.Remove(); err != nil {
|
||||
if err := removeTree(filepath.Join(root, entry.Name())); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user