R
Russell Wallace
Is it possible to reliably sandbox a computation in Java?
To clarify, I'm not talking about the "in a web browser" meaning of
"sandbox"; I'm looking at doing a variant of evolutionary computation,
where subprograms are generated at random and tested against a fitness
function (the version I have in mind generates programs somewhat
nonrandomly, but it makes no difference in this context); the
subprograms in question would be in a scripting language run on an
interpreter written in Java, and don't have the ability to perform
I/O, execute arbitrary Java byte code or call arbitrary library
classes, so I'm only concerned about resource exhaustion. There are
three specific issues I'm concerned about:
1. A subprogram could go into an infinite loop or otherwise spend too
much CPU time.
Solution: use a separate thread, have a monitor thread kill the
subprogram after an X second timeout.
2. Infinite or excessively deep recursion could result in stack overflow.
I think that throws a reliably catchable exception, right?
3. Out of memory.
This is the one I'm worried about: it throws an exception, but suppose
there's other stuff going on in the main program, say in a user
interface thread, that will also need memory. Couldn't there be a
condition where something else fails in the time between exhausting
memory and catch/cleanup? In other words, you can't reliably deal with
an out of memory exception in the same process, right?
If so, is there a reliable way to kick off a second JVM instance to
run a sandboxed subprogram? ("Reliable" includes portable, e.g. can't
use native fork() because it has to run on Windows as well as Unix.)
Or is there another way to run a subprogram in a separate memory pool?
(It would be nice if there was also a way to know how much memory a
subprogram had used even in the event of successful completion, or
better yet, monitor how much it was using at any given time. I'm
guessing this is impossible because other things might also use
memory, and there's no way of knowing when garbage collection has
occurred, is this correct?)
Thanks,
To clarify, I'm not talking about the "in a web browser" meaning of
"sandbox"; I'm looking at doing a variant of evolutionary computation,
where subprograms are generated at random and tested against a fitness
function (the version I have in mind generates programs somewhat
nonrandomly, but it makes no difference in this context); the
subprograms in question would be in a scripting language run on an
interpreter written in Java, and don't have the ability to perform
I/O, execute arbitrary Java byte code or call arbitrary library
classes, so I'm only concerned about resource exhaustion. There are
three specific issues I'm concerned about:
1. A subprogram could go into an infinite loop or otherwise spend too
much CPU time.
Solution: use a separate thread, have a monitor thread kill the
subprogram after an X second timeout.
2. Infinite or excessively deep recursion could result in stack overflow.
I think that throws a reliably catchable exception, right?
3. Out of memory.
This is the one I'm worried about: it throws an exception, but suppose
there's other stuff going on in the main program, say in a user
interface thread, that will also need memory. Couldn't there be a
condition where something else fails in the time between exhausting
memory and catch/cleanup? In other words, you can't reliably deal with
an out of memory exception in the same process, right?
If so, is there a reliable way to kick off a second JVM instance to
run a sandboxed subprogram? ("Reliable" includes portable, e.g. can't
use native fork() because it has to run on Windows as well as Unix.)
Or is there another way to run a subprogram in a separate memory pool?
(It would be nice if there was also a way to know how much memory a
subprogram had used even in the event of successful completion, or
better yet, monitor how much it was using at any given time. I'm
guessing this is impossible because other things might also use
memory, and there's no way of knowing when garbage collection has
occurred, is this correct?)
Thanks,