Rewriting bytecode

Nagy Mostafa nagy.mostafa at
Thu Sep 3 13:24:28 PDT 2009

On Thu, Sep 3, 2009 at 2:53 PM, Keith McGuigan <Keith.McGuigan at>wrote:

> Nagy Mostafa wrote:
>> Hi Everyone, I am trying to re-write every method's bytecode right before
>> it executes to expand the opcode field to 4 bytes instead of 1. What is the
>> best way to do that ? From looking at the code, I am confused about the
>> methodOop layout and not sure how to do the resizing. Any code or
>> documentation pointers will be appreciated.
> Have you considered java.lang.Instrument to do this, instead of doing it in
> the VM?
> If you must do it from with the VM, perhaps start with reading
> src/share/vm/prims/jvmtiRedefineClasses.hpp and go from there.
> Changing the bytecode stream is tricky business, especially changing the
> length of it as you propose.  You'll have to update all jump updates and
> catch clause bounds (and line number tables, etc, if you're into the whole
> debugging thing);  and they'll of course be corner cases where the expanded
> code size (or jump offset) will overflow the storage that it's currently in
> and you'll have to rewrite some of the instructions to compensate.  It's all
> doable, I'm sure... but tricky.
> What are you trying to accomplish?
> --
> - Keith

Thank you, Keith.
I am trying to implement direct-threaded dispatching in Zero interpreter.
Currently Zero support either switch-case or indirect threaded. I need to
expand every opcode field to be able to accommodate its handler address. So
you see, I have to modify the interpreter/vm to dispatch differently, which
is easy, I assume. The tricky/hard part like you said is modifying the

I saw some code in "relocator.cpp" for bytecode re-writing and was wondering
if that should do the trick. I looked at "Relocator::insert_space_at()"
which I think expands the size of an instruction and adjusts the method
layout accordingly. I suppose I can use that and expand the first
instruction enough to have enough space to rewrite my code. I still need of
course to fix the exception table manually. Does that sound like a good idea
? Do I still need to adjust the linenumber table if i am not doing any
debugging ?

- nagy
-------------- next part --------------
An HTML attachment was scrubbed...

More information about the hotspot-dev mailing list