stack manipulation APIs

Lukas Stadler lukas.stadler at jku.at
Wed Mar 26 16:17:12 PDT 2008


*Hi!*

I am currently working on APIs for the multi-language VM that would 
allow Java code to access and manipulate its own stacks. The overall 
scope is pretty general, ranging from call-with-current-continuation 
(call/cc) for languages like scheme and coroutine implementations to 
dynamic recompilation for compilers (like what the JIT does, but 
for ?-to-bytecode compilers). In the end it should be possible to 
change, replace, remove, etc. stack frames or even synthesize a new 
stack from scratch. *But*: It seems almost impossible to make this 
secure - some useful higher-level APIs will be needed. How these could 
be implemented under the covers - that's what I'm thinking about right now.

I'm starting with the most simple use case for now - continuations. Not 
really "stack manipulation", just saving/restoring stacks.

Some questions/remarks that crossed my mind: (most of this is "... am I 
correct assuming that:")

    *  From what I've understood a call/cc can be invoked even after the
      method in which it was created has returned.

    This could lead to all sorts of harmful behavior, like exiting a
    monitor twice, etc. Should this be possible, or will only a
    restricted case be implemented? There would have to be a big red
    sign (and possibly some kind of verifier) with all the things that
    aren't allowed in such code. 
    I recently looked at the apache commons Javaflow library - they
    implement storing the current stack state using only bytecode
    instrumentation and a small and unintrusive runtime framework. (I
    can write a short summary of how they're doing this if anyone is
    interested.) I think that, as their implementation is inherently
    safe, we could partly adopt its behaviour. 

    * I think that there are two variants to consider: one-shot
      continuations that really only qualify as nonlocal returns and
      full-fledged continuations that are invoked many times from
      everywhere.

I'm just now starting to explore the OpenJDK code, so any 
remarks/pointers are very welcome - especially where to look for 
examples on how to deal with stack frames (I thought about the 
deoptimization code...)

Some more use cases for the stack-manipulation that came to my mind:

    * sophisticated error logs (stack traces with local variables etc.)
    * checkpointing in servers
    * transferring threads between servers / nomadic lightweight threads
      (Second Life's agent execution model)
    * script interpreters/compilers that can switch between interpreted
      and compiled mode, like the JVM


Thanks,
 Lukas



More information about the mlvm-dev mailing list