How to run untrusted Groovy code in a sandbox with memory and time limits?

I am creating something that will take arbitrary Groovy code and execute it (IRC bot). I know how to access files in a sandbox, threads, etc. Using SecurityManagers, but I want the script to be killed if it uses 128 MB of RAM or 3 seconds of runtime (so no one runs it while (true){})., Here is my code:

private static Object eval(String code) {
    GroovyShell sh = new GroovyShell()
    return sh.parse(code).run()
}

I really do not want to create a new JVM, because I would like to have exceptions thrown to the main program and the actual objects passed instead of strings (for reuse if I ever intend to make other interface modules).

+4
source share
1

TL;DR

script .

, . script Loader, Extensions ClassLoader , , Bootstrap . groovy script .

. script interrupt , 3- . , InterruptedException, . , .

- , . , - . - jvm.

, . , - , , . , .

, Java-, , Exception Serializable, , SocketChannel http, , . .

+1

All Articles