added a version of piston which uses lxc instead of docker, added tests for lxc

This commit is contained in:
Brian Seymour 2018-10-22 16:38:52 -05:00
parent 748f438718
commit b26d1b5b45
30 changed files with 233 additions and 0 deletions

76
lxc/execute Executable file
View File

@ -0,0 +1,76 @@
#!/usr/bin/env bash
dir="$( cd "$( dirname "$0" )" && pwd )"
if [ -z "$1" ]; then
echo "invalid args"
exit
fi
if [ -z "$2" ]; then
echo "invalid args"
exit
fi
lang=$1
epoch=$(date +%s%3N)
basepath="/var/lib/lxc/piston/rootfs"
filepath="/tmp/$epoch/code.code"
file=$(basename $2)
argpath="/tmp/$epoch/args.args"
arg=$(basename $argpath)
# write arg file
mkdir -p $basepath/tmp/$epoch
chmod 777 $basepath/tmp/$epoch
cat $file > $basepath$filepath
echo "${@:3}" > $basepath$argpath
bin=
case "$lang" in
"python2")
bin=python2
;;
"python" | "python3")
bin=python3
;;
"ruby")
bin=ruby
;;
"javascript" | "js" | "node")
bin=node
;;
"c")
bin=c
;;
"cpp" | "c++")
bin=cpp
;;
"go")
bin=go
;;
"c#" | "csharp" | "cs")
bin=csharp
;;
"r")
bin=r
;;
"php")
bin=php
;;
"nasm" | "asm")
bin=nasm
;;
"java")
bin=java
;;
*)
echo "invalid language"
exit
esac
lxc-attach -n piston -- \
/bin/su ubuntu \
-c "bash /home/ubuntu/$bin $epoch 2>&1 | head -c 65536"
#rm -f $basepath$filepath
#rm -f $basepath$argpath

3
lxc/executors/c Executable file
View File

@ -0,0 +1,3 @@
cd /tmp/$1
timeout -s KILL 10 gcc -o binary -x c code.code
timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' ./binary"

3
lxc/executors/cpp Executable file
View File

@ -0,0 +1,3 @@
cd /tmp/$1
timeout -s KILL 10 g++ -o binary -x c++ code.code
timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' ./binary"

3
lxc/executors/csharp Executable file
View File

@ -0,0 +1,3 @@
cd /tmp/$1
timeout -s KILL 10 mcs $(echo code.code | sed 's/\///') -out:binary
timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' mono binary"

6
lxc/executors/go Executable file
View File

@ -0,0 +1,6 @@
cd /tmp/$1
cp code.code interim.go
file="interim.go"
GOROOT=/usr/lib/go timeout -s KILL 10 go build $file
file=${file%%.*}
timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' ./$file"

6
lxc/executors/java Executable file
View File

@ -0,0 +1,6 @@
cd /tmp/$1
cp code.code interim.java
name=$(cat interim.java | grep -Eo 'public\s+class\s+([A-Za-z0-9]+)' | sed -n 's/ */ /gp' | cut -d' ' -f3)
mv interim.java $name.java
timeout -s KILL 10 javac $name.java
timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' java $name"

4
lxc/executors/nasm Executable file
View File

@ -0,0 +1,4 @@
cd /tmp/$1
timeout -s KILL 10 nasm -f elf64 -o binary.o code.code
timeout -s KILL 10 ld binary.o -o binary
timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' ./binary"

2
lxc/executors/node Executable file
View File

@ -0,0 +1,2 @@
cd /tmp/$1
timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' nodejs code.code"

2
lxc/executors/php Executable file
View File

@ -0,0 +1,2 @@
cd /tmp/$1
timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' php code.code"

2
lxc/executors/python2 Executable file
View File

@ -0,0 +1,2 @@
cd /tmp/$1
timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' python2 code.code"

2
lxc/executors/python3 Executable file
View File

@ -0,0 +1,2 @@
cd /tmp/$1
timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' python3.6 code.code"

2
lxc/executors/r Executable file
View File

@ -0,0 +1,2 @@
cd /tmp/$1
timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' Rscript code.code"

2
lxc/executors/ruby Executable file
View File

@ -0,0 +1,2 @@
cd /tmp/$1
timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' ruby code.code"

26
lxc/notes.txt Normal file
View File

@ -0,0 +1,26 @@
# install
yum install lxc lxc-template debootstrap
# create container
lxc-create -t download -n piston
select ubuntu, bionic, amd64
# start container
lxc-start -n piston -d
# shell in and install stuff
lxc-attach -n piston
export PATH=/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
sed -i 's/http:\/\/archive.ubuntu.com\/ubuntu/http:\/\/mirror.math.princeton.edu\/pub\/ubuntu/' /etc/apt/sources.list
apt-get update
apt-get -y install tzdata nano dpkg-dev build-essential python python3 ruby nodejs golang php7.2 r-base mono-complete nasm openjdk-8-jdk
# apply limits
echo 'ubuntu soft nproc 128' >> /etc/security/limits.conf
echo 'ubuntu hard nproc 128' >> /etc/security/limits.conf
echo 'ubuntu soft nofile 1024' >> /etc/security/limits.conf
echo 'ubuntu hard nofile 1024' >> /etc/security/limits.conf
echo 'runner soft nproc 16' >> /etc/security/limits.conf
echo 'runner hard nproc 16' >> /etc/security/limits.conf
echo 'runner soft nofile 512' >> /etc/security/limits.conf
echo 'runner hard nofile 512' >> /etc/security/limits.conf

3
lxc/shell Executable file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
lxc-attach -n piston

6
lxc/start Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env bash
rm -f /var/lib/lxc/piston/rootfs/home/ubuntu/*
cp -f executors/* /var/lib/lxc/piston/rootfs/home/ubuntu
lxc-start -n piston -d

3
lxc/stop Executable file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
lxc-stop -n piston -k

5
tests/test.c Normal file
View File

@ -0,0 +1,5 @@
#include <stdio.h>
void main(void) {
printf("good\n");
}

6
tests/test.cpp Normal file
View File

@ -0,0 +1,6 @@
#include <iostream>
int main(void) {
printf("good\n");
return 1;
}

9
tests/test.cs Normal file
View File

@ -0,0 +1,9 @@
using System;
namespace HelloWorld {
class Hello {
static void Main() {
Console.WriteLine("good");
}
}
}

7
tests/test.go Normal file
View File

@ -0,0 +1,7 @@
package main
import "fmt"
func main() {
fmt.Println("good")
}

5
tests/test.java Normal file
View File

@ -0,0 +1,5 @@
public class HelloWorld {
public static void main(String[] args) {
System.out.println("good");
}
}

1
tests/test.js Normal file
View File

@ -0,0 +1 @@
console.log('good')

16
tests/test.nasm Normal file
View File

@ -0,0 +1,16 @@
SECTION .DATA
good: db 'good',10
txtlen: equ $-good
SECTION .TEXT
GLOBAL _start
_start:
mov eax,4
mov ebx,1
mov ecx,good
mov edx,txtlen
int 80h
mov eax,1
mov ebx,0
int 80h

3
tests/test.php Normal file
View File

@ -0,0 +1,3 @@
<?php
echo 'good' . "\n";

1
tests/test.r Normal file
View File

@ -0,0 +1 @@
print('good')

1
tests/test.rb Normal file
View File

@ -0,0 +1 @@
puts 'good'

1
tests/test2.py Normal file
View File

@ -0,0 +1 @@
print 'good'

1
tests/test3.py Normal file
View File

@ -0,0 +1 @@
print('good')

26
tests/test_all_lxc Executable file
View File

@ -0,0 +1,26 @@
#!/usr/bin/env bash
echo 'testing c'
../lxc/execute c test.c
echo 'testing cpp'
../lxc/execute cpp test.cpp
echo 'testing cs'
../lxc/execute cs test.cs
echo 'testing go'
../lxc/execute go test.go
echo 'testing java'
../lxc/execute java test.java
echo 'testing asm'
../lxc/execute asm test.nasm
echo 'testing js'
../lxc/execute js test.js
echo 'testing php'
../lxc/execute php test.php
echo 'testing python2'
../lxc/execute python2 test2.py
echo 'testing python3'
../lxc/execute python3 test3.py
echo 'testing r'
../lxc/execute r test.r
echo 'testing ruby'
../lxc/execute ruby test.rb