From b81690a9de48dad02300de715cad5d40067be098 Mon Sep 17 00:00:00 2001 From: Brian Seymour Date: Wed, 13 Jan 2021 23:45:48 -0600 Subject: [PATCH] fix jelly, move all executor code to runner permission, introduce stdout/stderr separate output to the api --- api/main.go | 48 ++++++++++++++++++++++++++++++++++++---- lxc/execute | 4 ++-- lxc/executors/c | 2 +- lxc/executors/cpp | 7 +++--- lxc/executors/csharp | 7 +++--- lxc/executors/emacs | 4 ++-- lxc/executors/go | 2 +- lxc/executors/haskell | 7 ++++-- lxc/executors/java | 7 ++++-- lxc/executors/kotlin | 6 +++-- lxc/executors/nasm | 9 ++++---- lxc/executors/nasm64 | 9 ++++---- lxc/executors/rust | 6 +++-- lxc/executors/typescript | 13 ++++++----- lxc/start | 2 +- lxc/test_all_lxc | 4 +++- lxc/tests/test.jelly | 1 + var/install.txt | 4 +++- 18 files changed, 101 insertions(+), 41 deletions(-) create mode 100644 lxc/tests/test.jelly diff --git a/api/main.go b/api/main.go index d328363..1fb17e5 100644 --- a/api/main.go +++ b/api/main.go @@ -28,6 +28,8 @@ type Outbound struct { Language string `json:"language"` Version string `json:"version"` Output string `json:"output"` + Stdout string `json:"stdout"` + Stderr string `json:"stderr"` } type Language struct { @@ -124,6 +126,18 @@ func Versions(res http.ResponseWriter, req *http.Request) { res.Write(data) } +type StdWriter struct { + combined *string + separate *string +} + +func (writer *StdWriter) Write(data []byte) (int, error) { + *writer.combined += string(data) + *writer.separate += string(data) + + return len(data), nil +} + func launch(request Inbound, res http.ResponseWriter) { stamp := time.Now().UnixNano() @@ -136,9 +150,33 @@ func launch(request Inbound, res http.ResponseWriter) { cmd := exec.Command("../lxc/execute", request.Language, srcfile, strings.Join(request.Args, "\n")) // capture out/err - var stdout, stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr + var stdout, stderr, combined string + + cmd.Stdout = &StdWriter{ + combined: &combined, + separate: &stdout, + } + + cmd.Stderr = &StdWriter{ + combined: &combined, + separate: &stderr, + } + + stdout = strings.TrimSpace(stdout) + stderr = strings.TrimSpace(stderr) + combined = strings.TrimSpace(combined) + + if len(stdout) > 65536 { + stdout = stdout[:65536] + } + + if len(stderr) > 65536 { + stderr = stdout[:65536] + } + + if len(combined) > 65536 { + combined = combined[:65536] + } err := cmd.Run() @@ -179,7 +217,9 @@ func launch(request Inbound, res http.ResponseWriter) { Ran: err == nil, Language: request.Language, Version: "", - Output: strings.TrimSpace(stdout.String()), + Output: combined, + Stdout: stdout, + Stderr: stderr, } // retrieve the language version diff --git a/lxc/execute b/lxc/execute index efc375d..ba81715 100755 --- a/lxc/execute +++ b/lxc/execute @@ -135,9 +135,9 @@ case "$lang" in esac # runner -timeout -s KILL 10 \ +timeout -s KILL 20 \ lxc-attach --clear-env -n piston -- \ - /bin/bash -l -c "bash /exec/$bin $newinc $epoch 2>&1 | head -c 65536" + /bin/bash -l -c "bash /exec/$bin $newinc $epoch" # process janitor lxc-attach --clear-env -n piston -- \ diff --git a/lxc/executors/c b/lxc/executors/c index b993915..3130257 100755 --- a/lxc/executors/c +++ b/lxc/executors/c @@ -1,4 +1,4 @@ runuser runner$1 -c "\ cd /tmp/$2 ; \ - timeout -s KILL 10 gcc -std=c11 -o binary -x c code.code ; \ + gcc -std=c11 -o binary -x c code.code ; \ cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" diff --git a/lxc/executors/cpp b/lxc/executors/cpp index c4f6471..4b8c6cf 100755 --- a/lxc/executors/cpp +++ b/lxc/executors/cpp @@ -1,3 +1,4 @@ -cd /tmp/$2 -timeout -s KILL 10 g++ -std=c++17 -o binary -x c++ code.code -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" +runuser runner$1 -c "\ + cd /tmp/$2 ; \ + g++ -std=c++17 -o binary -x c++ code.code ; \ + cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" diff --git a/lxc/executors/csharp b/lxc/executors/csharp index 76b3338..2c742c7 100755 --- a/lxc/executors/csharp +++ b/lxc/executors/csharp @@ -1,3 +1,4 @@ -cd /tmp/$2 -timeout -s KILL 10 mcs $(echo code.code | sed 's/\///') -nowarn:0219 -out:binary -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 mono binary" +runuser runner$1 -c "\ + cd /tmp/$2 ; \ + mcs $(echo code.code | sed 's/\///') -nowarn:0219 -out:binary ; \ + cat args.args | xargs -d '\n' timeout -s KILL 3 mono binary" diff --git a/lxc/executors/emacs b/lxc/executors/emacs index 156723e..31f6186 100755 --- a/lxc/executors/emacs +++ b/lxc/executors/emacs @@ -1,7 +1,7 @@ cd /tmp/$2 if [[ -z $(grep '[^[:space:]]' args.args) ]]; then - runuser -l runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 emacs -Q --script code.code" + runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 emacs -Q --script code.code" else - runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 emacs -Q --script code.code" + runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 emacs -Q --script code.code" fi diff --git a/lxc/executors/go b/lxc/executors/go index e7903d1..063af21 100755 --- a/lxc/executors/go +++ b/lxc/executors/go @@ -1,6 +1,6 @@ cd /tmp/$2 cp code.code interim.go file="interim.go" -timeout -s KILL 10 go build $file +go build $file file=${file%%.*} runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./$file" diff --git a/lxc/executors/haskell b/lxc/executors/haskell index edde524..7593b60 100755 --- a/lxc/executors/haskell +++ b/lxc/executors/haskell @@ -1,4 +1,7 @@ cd /tmp/$2 mv code.code code.hs -timeout -s KILL 10 ghc -dynamic -o binary code.hs > /dev/null 2>&1 -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" + +runuser runner$1 -c "\ + cd /tmp/$2 ; \ + ghc -dynamic -o binary code.hs > /dev/null 2>&1 ; \ + cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" diff --git a/lxc/executors/java b/lxc/executors/java index 66261bb..0318a9f 100755 --- a/lxc/executors/java +++ b/lxc/executors/java @@ -2,5 +2,8 @@ cd /tmp/$2 cp code.code interim.java name=$(grep -Po "(?<=\n|\A)\s*(public\s+)?(class|interface)\s+\K([^\n\s{]+)" interim.java) mv interim.java $name.java -timeout -s KILL 10 javac $name.java -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 java $name" + +runuser runner$1 -c "\ + cd /tmp/$2 ; \ + javac $name.java ; \ + cat args.args | xargs -d '\n' timeout -s KILL 3 java $name" diff --git a/lxc/executors/kotlin b/lxc/executors/kotlin index dcd5eff..5d5070a 100755 --- a/lxc/executors/kotlin +++ b/lxc/executors/kotlin @@ -1,4 +1,6 @@ cd /tmp/$2 cp code.code code.kt -timeout -s KILL 10 kotlinc code.kt -include-runtime -d code.jar -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 java -jar code.jar" +runuser runner$1 -c "\ + cd /tmp/$2 ; \ + kotlinc code.kt -include-runtime -d code.jar ; \ + cat args.args | xargs -d '\n' timeout -s KILL 3 java -jar code.jar" diff --git a/lxc/executors/nasm b/lxc/executors/nasm index 60c4963..22026af 100755 --- a/lxc/executors/nasm +++ b/lxc/executors/nasm @@ -1,4 +1,5 @@ -cd /tmp/$2 -timeout -s KILL 10 nasm -f elf32 -o binary.o code.code -timeout -s KILL 10 ld -m elf_i386 binary.o -o binary -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" +runuser runner$1 -c "\ + cd /tmp/$2 ; \ + nasm -f elf32 -o binary.o code.code ; \ + ld -m elf_i386 binary.o -o binary ; \ + cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" diff --git a/lxc/executors/nasm64 b/lxc/executors/nasm64 index 8723995..a6dc197 100755 --- a/lxc/executors/nasm64 +++ b/lxc/executors/nasm64 @@ -1,4 +1,5 @@ -cd /tmp/$2 -timeout -s KILL 10 nasm -f elf64 -o binary.o code.code -timeout -s KILL 10 ld -m elf_x86_64 binary.o -o binary -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" +runuser runner$1 -c "\ + cd /tmp/$2 ; \ + nasm -f elf64 -o binary.o code.code ; \ + ld -m elf_x86_64 binary.o -o binary ; \ + cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" diff --git a/lxc/executors/rust b/lxc/executors/rust index aef4af3..c939259 100755 --- a/lxc/executors/rust +++ b/lxc/executors/rust @@ -1,3 +1,5 @@ cd /tmp/$2 -timeout -s KILL 10 rustc -o binary code.code -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" +rustc -o binary code.code +runuser runner$1 -c "\ + cd /tmp/$2 ; \ + cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" diff --git a/lxc/executors/typescript b/lxc/executors/typescript index 5ed50c5..863d8d0 100755 --- a/lxc/executors/typescript +++ b/lxc/executors/typescript @@ -1,6 +1,7 @@ -cd /tmp/$2 -mv code.code interim.ts -timeout -s KILL 10 tsc interim.ts -rm interim.ts -mv interim.js code.code -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 node code.code" +runuser runner$1 -c "\ + cd /tmp/$2 ; \ + mv code.code interim.ts ; \ + tsc interim.ts ; \ + rm -f interim.ts ; \ + mv interim.js code.code ; \ + cat args.args | xargs -d '\n' timeout -s KILL 3 node code.code" diff --git a/lxc/start b/lxc/start index 6c13c98..0c17ec8 100755 --- a/lxc/start +++ b/lxc/start @@ -6,4 +6,4 @@ cp -f executors/* /var/lib/lxc/piston/rootfs/exec chmod 700 /var/lib/lxc/piston/rootfs/exec/* chown -R root:root /var/lib/lxc/piston/rootfs/exec -lxc-start -n piston -d +#lxc-start -n piston -d diff --git a/lxc/test_all_lxc b/lxc/test_all_lxc index c7f0b28..873c840 100755 --- a/lxc/test_all_lxc +++ b/lxc/test_all_lxc @@ -9,7 +9,7 @@ echo 'testing cpp' ../execute cpp test.cpp echo 'testing cs' ../execute cs test.cs -echo 'testing deno ts' +echo 'testing deno' ../execute deno testdeno.ts echo 'testing elisp' ../execute elisp test.el @@ -21,6 +21,8 @@ echo 'testing haskell' ../execute haskell test.hs echo 'testing java' ../execute java test.java +echo 'testing jelly' +../execute jelly test.jelly echo 'testing jl' ../execute jl test.jl echo 'testing js' diff --git a/lxc/tests/test.jelly b/lxc/tests/test.jelly new file mode 100644 index 0000000..e688ff7 --- /dev/null +++ b/lxc/tests/test.jelly @@ -0,0 +1 @@ +“3ḅaė;œ» diff --git a/var/install.txt b/var/install.txt index 4f44e22..3dbeb44 100644 --- a/var/install.txt +++ b/var/install.txt @@ -157,7 +157,8 @@ cd /opt && mkdir jelly && cd jelly wget https://github.com/DennisMitchell/jellylanguage/archive/master.zip unzip master.zip cd jellylanguage-master -pip3.8 install . +python3.8 -m pip install . +sed -i 's/\/usr\/local\/bin\/python3.8/\/opt\/python3\/Python-3.8.2\/python3.8/' /usr/local/bin/jelly # install julia # final binary: /opt/julia/julia-1.5.0/bin/julia @@ -202,6 +203,7 @@ source /opt/.profile cd /opt && mkdir emacs && cd emacs wget https://mirrors.ocf.berkeley.edu/gnu/emacs/emacs-26.3.tar.xz tar -xf emacs-26.3.tar.xz +rm emacs-26.3.tar.xz cd emacs-26.3 ./configure --with-gnutls=no make