开发者

Bytecode and Objects

开发者 https://www.devze.com 2022-12-29 07:04 出处:网络
I am working on a bytecode instrumentation project. Currently when handling objects, the verifier throws an error most of the time. So I would like to get things clear concerning rules with objects (I

I am working on a bytecode instrumentation project. Currently when handling objects, the verifier throws an error most of the time. So I would like to get things clear concerning rules with objects (I read the JVMS but couldn't find the answer I was looking for):

I am instrumenting the NEW instruction:

original bytecode

NEW <MyClass>
DUP
INVOKESPECIAL <MyClass.<init>>

after instrumentation

NEW <MyClass>
DUP
INVOKESTATIC <Profiler.handleNEW>
DUP
INVOKESPECIAL <MyClass.<init>>

Note that I added a call to Profiler.handleNEW() which takes as argument an object reference (the newly created object).

The piece of code above throws a VerificationError. While if I don't add the INVOKESTATIC (leaving only the DUP), it doesn't. So what is the rule that I'm violating? I can duplicate an uni开发者_运维技巧nitialized reference but I can't pass it as parameter? I would appreciate any help. Thank you


The JVM verifier treats an object whose constructor has yet to be called as if it had a special compile-time type called "uninitialized".

So what's happening from the verifier's point of view is that you are passing the wrong type of object as the first parameter to Profiler.handleNEW(), because "uninitialized" is not considered a subclass of Object (so to speak).

The relevant part of the JVM spec regarding how "uninitialized" is defined is here.


Until the java.lang.Object constructor is called (technically it is probably normally exited), the type of the reference is "uninitialised". So you can't do much with the reference before calling a constructor on it. This is also true in the constructors of the object in question.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号