aboutsummaryrefslogtreecommitdiffhomepage
path: root/worker/exec.go
blob: 10bc99ac69099b669f9b39c2204ca5c35777a060 (plain)
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
package main

import (
	"bytes"
	"context"
	"crypto/md5"
	"fmt"
	"os"
	"os/exec"
	"strings"
	"time"
)

const (
	dataRootDir = "/app/data"
	// Stores *.swift files.
	dataSwiftRootDir = dataRootDir + "/swift"
	// Stores *.wasm files.
	dataWasmRootDir = dataRootDir + "/wasm"
	// Stores *.cwasm files (compiled wasm generated by "wasmtime compile").
	dataCwasmRootDir = dataRootDir + "/cwasm"

	wasmMaxMemorySize = 10 * 1024 * 1024 // 10 MiB
)

func prepareDirectories() error {
	if err := os.MkdirAll(dataSwiftRootDir, 0755); err != nil {
		return err
	}
	if err := os.MkdirAll(dataWasmRootDir, 0755); err != nil {
		return err
	}
	if err := os.MkdirAll(dataCwasmRootDir, 0755); err != nil {
		return err
	}
	return nil
}

func calcHash(code string) string {
	return fmt.Sprintf("%x", md5.Sum([]byte(code)))
}

func calcFilePath(hash, ext string) string {
	return fmt.Sprintf("%s/%s/%s.%s", dataRootDir, ext, hash, ext)
}

func execCommandWithTimeout(
	ctx context.Context,
	maxDuration time.Duration,
	makeCmd func(context.Context) *exec.Cmd,
) (string, string, error) {
	ctx, cancel := context.WithTimeout(ctx, maxDuration)
	defer cancel()

	cmd := makeCmd(ctx)

	var stdout bytes.Buffer
	var stderr bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr

	exitCh := make(chan error)
	go func() {
		exitCh <- cmd.Run()
	}()

	select {
	case <-ctx.Done():
		return stdout.String(), stderr.String(), ctx.Err()
	case err := <-exitCh:
		return stdout.String(), stderr.String(), err
	}
}

func convertCommandErrorToResultType(err error) string {
	if err != nil {
		if err == context.DeadlineExceeded {
			return resultTimeout
		} else {
			return resultFailure
		}
	} else {
		return resultSuccess
	}
}

func execSwiftCompile(
	ctx context.Context,
	code string,
	maxDuration time.Duration,
) swiftCompileResponseData {
	hash := calcHash(code)
	inPath := calcFilePath(hash, "swift")
	outPath := calcFilePath(hash, "wasm")

	if err := os.WriteFile(inPath, []byte(code), 0644); err != nil {
		return swiftCompileResponseData{
			Status: resultInternalError,
			Stdout: "",
			Stderr: err.Error(),
		}
	}

	stdout, stderr, err := execCommandWithTimeout(
		ctx,
		maxDuration,
		func(ctx context.Context) *exec.Cmd {
			return exec.CommandContext(
				ctx,
				"swiftc",
				"-target", "wasm32-unknown-wasi",
				"-o", outPath,
				inPath,
			)
		},
	)

	return swiftCompileResponseData{
		Status: convertCommandErrorToResultType(err),
		Stdout: stdout,
		Stderr: stderr,
	}
}

func execWasmCompile(
	ctx context.Context,
	code string,
	maxDuration time.Duration,
) wasmCompileResponseData {
	hash := calcHash(code)
	inPath := calcFilePath(hash, "wasm")
	outPath := calcFilePath(hash, "cwasm")

	stdout, stderr, err := execCommandWithTimeout(
		ctx,
		maxDuration,
		func(ctx context.Context) *exec.Cmd {
			return exec.CommandContext(
				ctx,
				"wasmtime", "compile",
				"-O", "opt-level=0",
				"-C", "cache=n",
				"-W", fmt.Sprintf("max-memory-size=%d", wasmMaxMemorySize),
				"-o", outPath,
				inPath,
			)
		},
	)

	return wasmCompileResponseData{
		Status: convertCommandErrorToResultType(err),
		Stdout: stdout,
		Stderr: stderr,
	}
}

func execTestRun(
	ctx context.Context,
	code string,
	stdin string,
	maxDuration time.Duration,
) testRunResponseData {
	hash := calcHash(code)
	inPath := calcFilePath(hash, "cwasm")

	stdout, stderr, err := execCommandWithTimeout(
		ctx,
		maxDuration,
		func(ctx context.Context) *exec.Cmd {
			cmd := exec.CommandContext(
				ctx,
				"wasmtime", "run",
				"--allow-precompiled",
				inPath,
			)
			cmd.Stdin = strings.NewReader(stdin)
			return cmd
		},
	)

	return testRunResponseData{
		Status: convertCommandErrorToResultType(err),
		Stdout: stdout,
		Stderr: stderr,
	}
}