Skip to content

Commit 2ed8c19

Browse files
rewrite runtime-compile
1 parent 2eec449 commit 2ed8c19

File tree

9 files changed

+74
-116
lines changed

9 files changed

+74
-116
lines changed

backend/cmd/compile_user_code.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ import (
1313
"github.com/pkg/errors"
1414
)
1515

16-
const CompilationMemoryLimit = 50 * 1024 * 1024
16+
// Memory Limit of a G++ Process
17+
const CompilationMemoryLimit = 100 * 1024 * 1024
18+
19+
// it is required that the docker image is built before the program runs
1720
const imageName = "markhuang1212/code-grader/runtime-compile"
1821

1922
var InternalError = errors.New("internal error")
2023
var CompilationFailure = errors.New("compilation error")
2124

25+
// The function compiles user's code inside a docker container, and returns the
26+
// executable on success
2227
func CompileUserCode(ctx context.Context, gr types.GradeRequest) ([]byte, error) {
2328

2429
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
@@ -33,52 +38,59 @@ func CompileUserCode(ctx context.Context, gr types.GradeRequest) ([]byte, error)
3338
},
3439
AttachStdin: true,
3540
AttachStdout: true,
41+
AttachStderr: true,
42+
OpenStdin: true,
3643
}, &container.HostConfig{
3744
NetworkMode: "none",
38-
Resources: container.Resources{
39-
Memory: CompilationMemoryLimit,
45+
Resources: container.Resources{
46+
// Memory: CompilationMemoryLimit,
4047
},
4148
}, nil, nil, "")
4249

4350
if err != nil {
44-
return nil, errors.Wrap(err, "cannot create container")
51+
return nil, errors.Wrap(InternalError, "cannot create container")
4552
}
4653

47-
cli.ContainerStart(ctx, resp.ID, dockertypes.ContainerStartOptions{})
48-
4954
hjresp, err := cli.ContainerAttach(ctx, resp.ID, dockertypes.ContainerAttachOptions{
55+
Stream: true,
5056
Stdin: true,
5157
Stdout: true,
5258
Stderr: true,
5359
})
5460

5561
if err != nil {
56-
return nil, errors.Wrap(err, "cannot attach container")
62+
return nil, errors.Wrap(InternalError, "cannot attach container")
63+
}
64+
65+
statucCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNextExit)
66+
67+
err = cli.ContainerStart(ctx, resp.ID, dockertypes.ContainerStartOptions{})
68+
if err != nil {
69+
return nil, errors.Wrap(InternalError, "cannot start container")
5770
}
5871

5972
outR, outW := io.Pipe()
6073
errR, errW := io.Pipe()
61-
62-
stdcopy.StdCopy(outW, errW, hjresp.Reader)
6374
hjresp.Conn.Write([]byte(gr.UserCode))
75+
hjresp.Conn.Close()
76+
stdcopy.StdCopy(outW, errW, hjresp.Conn)
6477

65-
statucCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNextExit)
6678
select {
6779
case status := <-statucCh:
6880
if status.StatusCode == 0 {
6981
out, err := io.ReadAll(outR)
7082
if err != nil {
71-
return nil, errors.Wrap(err, "cannot read stdout")
83+
return nil, errors.Wrap(InternalError, "cannot read stdout")
7284
}
7385
return out, err
7486
} else {
7587
out, err := io.ReadAll(errR)
7688
if err != nil {
77-
return nil, errors.Wrap(err, "cannot read stderr")
89+
return nil, errors.Wrap(InternalError, "cannot read stderr")
7890
}
79-
return out, errors.New("compilation failed")
91+
return out, CompilationFailure
8092
}
81-
case err := <-errCh:
82-
return nil, err
93+
case <-errCh:
94+
return nil, errors.Wrap(InternalError, "error waiting container")
8395
}
8496
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cmd_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/markhuang1212/code-grader/backend/cmd"
9+
"github.com/markhuang1212/code-grader/types"
10+
)
11+
12+
func TestCompileUserCode(t *testing.T) {
13+
ctx := context.Background()
14+
gr := types.GradeRequest{
15+
TestCaseName: "example-1",
16+
UserCode: "",
17+
}
18+
19+
_, err := cmd.CompileUserCode(ctx, gr)
20+
fmt.Println(err)
21+
}

install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ sudo apt-get -y upgrade
99
sudo apt-get install docker.io
1010
sudo usermod -aG docker
1111

12-
docker build
12+
docker-compose build
1313
docker-compose up

runtime-compile/Dockerfile

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ FROM ubuntu:21.04
22

33
RUN apt-get update
44
RUN apt-get -y upgrade
5-
RUN apt-get -y install golang ca-certificates build-essential
6-
7-
RUN update-ca-certificates
5+
RUN apt-get -y install build-essential
86

97
WORKDIR /code-grader
108
COPY . .
119

1210
WORKDIR /code-grader/runtime-compile
13-
RUN go build
1411

1512
USER daemon
16-
ENTRYPOINT [ "./runtime-compile" ]
13+
ENTRYPOINT [ "./run.sh" ]

runtime-compile/main.go

Lines changed: 0 additions & 82 deletions
This file was deleted.

runtime-compile/run.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
4+
tee > code.txt
5+
6+
cat ${TEST_CASE_DIR}/prepend.txt > main.cpp
7+
cat code.txt >> main.cpp
8+
cat ${TEST_CASE_DIR}/append.txt >> main.cpp
9+
10+
${CXX} ${CXXFLAGS} -o /tmp/a.out main.cpp
11+
12+
if [[ $? -ne 0 ]]
13+
then
14+
exit 1
15+
fi
16+
17+
cat /tmp/a.out

testcases/example-1/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Writing Test Cases
22

3-
Please refer to this example test case.
3+
Please refer to this example for writing test case.

testcases/example-1/testcase.json

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
{
2-
"TestCaseName": "ExampleTestCase1",
3-
"PreprocessOptions": {
4-
"AppendCodePath": "./append.txt",
5-
"PrependCodePath": "./prepend.txt"
6-
},
7-
"CompilerOptions": {
8-
"Flags": [
9-
"-std=c++11"
10-
]
11-
},
122
"RuntimeOptions": {
13-
"StdinPath": "./input.txt",
14-
"StdoutPath": "./output.txt",
153
"MemoryLimit": 512,
164
"RuntimeLimit": 10
175
}

update.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
git pull
4+
docker-compose build
5+
docker-compose restart

0 commit comments

Comments
 (0)