Merge pull request #19 from kevrocks67/master

Change ASM module to default to 32 bit and add 64 bit executor
This commit is contained in:
Brian Seymour 2020-10-03 04:21:21 -05:00 committed by GitHub
commit 58f4088007
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 177 additions and 169 deletions

View File

@ -1,249 +1,252 @@
package main package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os/exec" "os/exec"
"regexp" "regexp"
"strings" "strings"
"time" "time"
) )
type Inbound struct { type Inbound struct {
Language string `json:"language"` Language string `json:"language"`
Source string `json:"source"` Source string `json:"source"`
Args []string `json:"args"` Args []string `json:"args"`
} }
type Problem struct { type Problem struct {
Code string `json:"code"` Code string `json:"code"`
Message string `json:"message"` Message string `json:"message"`
} }
type Outbound struct { type Outbound struct {
Ran bool `json:"ran"` Ran bool `json:"ran"`
Language string `json:"language"` Language string `json:"language"`
Version string `json:"version"` Version string `json:"version"`
Output string `json:"output"` Output string `json:"output"`
} }
type Language struct { type Language struct {
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"` Version string `json:"version,omitempty"`
} }
var instance int var instance int
var languages []Language var languages []Language
func main() { func main() {
port := "2000" port := "2000"
var err error var err error
languages, err = UpdateVersions() languages, err = UpdateVersions()
if err != nil { if err != nil {
fmt.Println("could not get version info and therefore couldn't start") fmt.Println("could not get version info and therefore couldn't start")
fmt.Println(err) fmt.Println(err)
return return
} }
fmt.Println("starting api on port", port) fmt.Println("starting api on port", port)
http.HandleFunc("/execute", Execute) http.HandleFunc("/execute", Execute)
http.HandleFunc("/versions", Versions) http.HandleFunc("/versions", Versions)
http.ListenAndServe(":"+port, nil) http.ListenAndServe(":"+port, nil)
} }
func Execute(res http.ResponseWriter, req *http.Request) { func Execute(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json") res.Header().Set("Content-Type", "application/json")
// get json // get json
inbound := Inbound{} inbound := Inbound{}
message := json.NewDecoder(req.Body) message := json.NewDecoder(req.Body)
message.Decode(&inbound) message.Decode(&inbound)
whitelist := []string{ whitelist := []string{
"awk", "awk",
"bash", "bash",
"brainfuck", "bf", "brainfuck", "bf",
"c", "c",
"cpp", "c++", "cpp", "c++",
"csharp", "cs", "c#", "csharp", "cs", "c#",
"elixir", "exs", "elixir", "exs",
"emacs", "elisp", "el", "emacs", "elisp", "el",
"go", "go",
"java", "java",
"julia", "jl", "julia", "jl",
"kotlin", "kotlin",
"nasm", "asm", "nasm", "asm",
"node", "javascript", "js", "nasm64", "asm64",
"perl", "pl", "node", "javascript", "js",
"php", "perl", "pl",
"python2", "php",
"python3", "python", "python2",
"ruby", "python3", "python",
"rust", "ruby",
"swift", "rust",
"typescript", "ts", "swift",
} "typescript", "ts",
}
// check if the supplied language is supported // check if the supplied language is supported
// now calls function and returns // now calls function and returns
for _, lang := range whitelist { for _, lang := range whitelist {
if lang == inbound.Language { if lang == inbound.Language {
launch(inbound, res) launch(inbound, res)
return return
} }
} }
// now only called when the language is not supported // now only called when the language is not supported
problem := Problem{ problem := Problem{
Code: "unsupported_language", Code: "unsupported_language",
Message: inbound.Language + " is not supported by Piston", Message: inbound.Language + " is not supported by Piston",
} }
pres, _ := json.Marshal(problem) pres, _ := json.Marshal(problem)
res.Write(pres) res.Write(pres)
} }
func Versions(res http.ResponseWriter, req *http.Request) { func Versions(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json") res.Header().Set("Content-Type", "application/json")
data, _ := json.Marshal(languages) data, _ := json.Marshal(languages)
res.Write(data) res.Write(data)
} }
func launch(request Inbound, res http.ResponseWriter) { func launch(request Inbound, res http.ResponseWriter) {
stamp := time.Now().UnixNano() stamp := time.Now().UnixNano()
// write the code to temp dir // write the code to temp dir
srcfile := fmt.Sprintf("/tmp/%d.code", stamp) srcfile := fmt.Sprintf("/tmp/%d.code", stamp)
ioutil.WriteFile(srcfile, []byte(request.Source), 0644) ioutil.WriteFile(srcfile, []byte(request.Source), 0644)
// set up the arguments to send to the execute command // set up the arguments to send to the execute command
cmd := exec.Command("../lxc/execute", request.Language, srcfile, strings.Join(request.Args, "\n")) cmd := exec.Command("../lxc/execute", request.Language, srcfile, strings.Join(request.Args, "\n"))
// capture out/err // capture out/err
var stdout, stderr bytes.Buffer var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout cmd.Stdout = &stdout
cmd.Stderr = &stderr cmd.Stderr = &stderr
err := cmd.Run() err := cmd.Run()
// get the executing version of the language // get the executing version of the language
execlang := request.Language execlang := request.Language
switch execlang { switch execlang {
case "bf": case "bf":
execlang = "brainfuck" execlang = "brainfuck"
case "c++": case "c++":
execlang = "cpp" execlang = "cpp"
case "cs", "c#": case "cs", "c#":
execlang = "csharp" execlang = "csharp"
case "el", "elisp": case "el", "elisp":
execlang = "emacs" execlang = "emacs"
case "exs": case "exs":
execlang = "elixir" execlang = "elixir"
case "asm": case "asm":
execlang = "nasm" execlang = "nasm"
case "js", "javascript": case "asm64":
execlang = "node" execlang = "nasm64"
case "jl": case "js", "javascript":
execlang = "julia" execlang = "node"
case "python": case "jl":
execlang = "python3" execlang = "julia"
case "ts": case "python":
execlang = "typescript" execlang = "python3"
} case "ts":
execlang = "typescript"
}
// prepare response // prepare response
outbound := Outbound{ outbound := Outbound{
Ran: err == nil, Ran: err == nil,
Language: request.Language, Language: request.Language,
Version: "", Version: "",
Output: strings.TrimSpace(stdout.String()), Output: strings.TrimSpace(stdout.String()),
} }
// retrieve the language version // retrieve the language version
for _, lang := range languages { for _, lang := range languages {
if lang.Name == execlang { if lang.Name == execlang {
outbound.Version = lang.Version outbound.Version = lang.Version
break break
} }
} }
response, _ := json.Marshal(outbound) response, _ := json.Marshal(outbound)
res.Write(response) res.Write(response)
} }
func UpdateVersions() ([]Language, error) { func UpdateVersions() ([]Language, error) {
langs, err := GetVersions() langs, err := GetVersions()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return langs, nil return langs, nil
} }
// get all the language and their current version // get all the language and their current version
func GetVersions() ([]Language, error) { func GetVersions() ([]Language, error) {
var languages []Language var languages []Language
res, err := ExecVersionScript() res, err := ExecVersionScript()
if err != nil { if err != nil {
return nil, err return nil, err
} }
info := strings.Split(res, "---") info := strings.Split(res, "---")
for _, v := range info { for _, v := range info {
if len(v) < 2 { if len(v) < 2 {
continue continue
} }
name, version := GetVersion(v) name, version := GetVersion(v)
languages = append(languages, Language{ languages = append(languages, Language{
Name: name, Name: name,
Version: version, Version: version,
}) })
} }
return languages, nil return languages, nil
} }
// run the script that retrieves all the language versions // run the script that retrieves all the language versions
func ExecVersionScript() (string, error) { func ExecVersionScript() (string, error) {
cmd := exec.Command("../lxc/versions") cmd := exec.Command("../lxc/versions")
var stdout bytes.Buffer var stdout bytes.Buffer
cmd.Stdout = &stdout cmd.Stdout = &stdout
cmd.Stderr = &stdout cmd.Stderr = &stdout
err := cmd.Run() err := cmd.Run()
return strings.ToLower(stdout.String()), err return strings.ToLower(stdout.String()), err
} }
// return the language and its version // return the language and its version
// most of the time it is easy to get the name and version // most of the time it is easy to get the name and version
// but for some languages helper functions are used // but for some languages helper functions are used
func GetVersion(s string) (string, string) { func GetVersion(s string) (string, string) {
lines := strings.Split(s, "\n") lines := strings.Split(s, "\n")
if lines[1] == "java" { if lines[1] == "java" {
return "java", regexp.MustCompile("([0-9]+)").FindString(lines[2]) return "java", regexp.MustCompile("([0-9]+)").FindString(lines[2])
} }
if lines[1] == "emacs" { if lines[1] == "emacs" {
return "emacs", regexp.MustCompile("([0-9]+\\.[0-9]+)").FindString(lines[2]) return "emacs", regexp.MustCompile("([0-9]+\\.[0-9]+)").FindString(lines[2])
} }
return lines[1], regexp.MustCompile("([0-9]+\\.[0-9]+\\.[0-9]+)").FindString(s) return lines[1], regexp.MustCompile("([0-9]+\\.[0-9]+\\.[0-9]+)").FindString(s)
} }

View File

@ -78,6 +78,9 @@ case "$lang" in
"nasm" | "asm") "nasm" | "asm")
bin=nasm bin=nasm
;; ;;
"nasm64" | "asm64")
bin=nasm64
;;
"node" | "js" | "javascript") "node" | "js" | "javascript")
bin=node bin=node
;; ;;

View File

@ -1,4 +1,4 @@
cd /tmp/$2 cd /tmp/$2
timeout -s KILL 10 nasm -f elf64 -o binary.o code.code timeout -s KILL 10 nasm -f elf32 -o binary.o code.code
timeout -s KILL 10 ld binary.o -o binary 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 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary"

View File

@ -23,8 +23,10 @@ echo 'testing js'
../execute js test.js ../execute js test.js
echo 'testing kotlin' echo 'testing kotlin'
../execute kotlin test.kt ../execute kotlin test.kt
echo 'testing asm' echo 'testing asm 32 bit'
../execute asm test.nasm ../execute asm test.nasm
echo 'testing asm 64 bit'
../execute asm64 test64.nasm
echo 'testing php' echo 'testing php'
../execute php test.php ../execute php test.php
echo 'testing perl' echo 'testing perl'