title: Looking for a safe Sandbox API for JVM plugins
body: <p>When you have an application server, and you want to run third-party plugins in it, you can use a restrictive Security Manager to stop them doing things like System.exit(), but that's only half the story. Those untrusted plugins can still get in an infinite loop, or eat up all the free heap before you have time to blink. Thread.stop() has been deprecated, so you can't just kill an amok Thread, and since the heap is shared, not only will the plugin get an OutOfMemoryError when it uses up all the heap, but so will all other threads running.</p>
  
  <p>Is there some Open Source application/API/framework out there that can manipulate the bytecode of plugin classes to make the threads killable and/or track the allocation so that the thread can be killed if it allocates too much? Even if the code is not readily "packaged" to be "used separately". You can make a Thread killable by inserting code that can produce an Exception at will, triggered by another "manager" thread, and make sure that Exception is not caught by the plugin. And you can add some kind of counters that count the number of calls and loops and amount of allocation, and have the "manager" thread kill a plugin that breaks the configured limits.</p>
  
  <p>I think all those things could be done with ASM, but I'm hoping they have been done before. I could let the plugins run in their own JVM, but that would involve massive constant marshaling/unmarshaling of the data, and in case the plugin JVM dies/crashes, I still don't know which of the potentiall dozens (100s?) of plugins was the problem, and I can't possibly run one JVM per plugin.</p>
  
  <p>I have found some related questions, but none solve the issue with infinite loops and eating the heap:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/4011260/sandbox-jvm-to-secure-server-from-untrusted-sources">Sandbox JVM to secure server from untrusted sources</a></li>
  <li><a href="https://stackoverflow.com/questions/1715036/how-do-i-create-a-java-sandbox">How do i create a java sandbox?</a></li>
  <li><a href="https://stackoverflow.com/questions/4775084/securitymanager-for-a-cloud-service-sandbox">SecurityManager for a cloud service “sandbox”</a></li>
  </ul>