简体   繁体   English

使用ASM访问Java Stack中的第二个顶级对象

[英]Visit the second top Object in Java Stack using ASM

I am using ASM to monitor field accesses (putfield and getfield). 我正在使用ASM监视字段访问(putfield和getfield)。 (For putfield,) My problem is that when the top of stack is a basic value (X) and the second top is an object reference (Y), how can I insert some Java instructions to invoke my method with the second object reference (Y) as its one argument and, after returning from my method, the two (X and Y) will not be lost? (对于putfield,)我的问题是,当堆栈的顶部是一个基本值(X),第二个顶部是一个对象引用(Y)时,我该如何插入一些Java指令来调用带有第二个对象引用的方法( Y)作为它的一个参数,从我的方法返回后,两个(X和Y)不会丢失吗?

In summary, how can I access the second top object reference without affecting the top value of the stack (after my access) in Java at Java bytecode level? 总之,在Java字节码级别上,如何在Java中访问第二个顶级对象引用而不影响堆栈的顶级值(访问后)?

I want to use dup, but it can only handle the top value of the stack. 我想使用dup,但它只能处理堆栈的最高值。 So, it works for the getfield since there isn't a value and I can directly dup it. 因此,它适用于getfield,因为没有值,我可以直接将其复制。

Is there a better way to do that? 有更好的方法吗?

Thanks. 谢谢。

Case 1... The basic value is not Long or Double - 情况1 ...基本值不是Long或Double-

Lets say that the top of the stack looks something like this... 可以说栈顶看起来像这样...

Y,X (the rightmost element being the top of the stack.) Y,X(最右边的元素是堆栈的顶部。)

The following sequence of instructions should do the trick... 以下说明序列应该可以解决问题...

DUP2 DUP2

POP POP

The DUP2 will duplicate the top two instructions. DUP2将复制前两个指令。 Thus resulting in Y,X,Y,X. 从而得到Y,X,Y,X。 The POP will pop the X (the basic value) off. POP将弹出X(基本值)。 And you will be left with Y,X,Y. 然后您将剩下Y,X,Y。 And then you can invoke your function. 然后,您可以调用函数。

Case 2... The basic value is a Long or Double - 情况2 ...基本值是Long或Double-

The the top of the stack look like this... Y,X1,X2. 堆栈的顶部看起来像这样... Y,X1,X2。 For this you can use the following sequence of instructions... 为此,您可以使用以下说明序列...

DUP2_X1 // this will result in X1,X2,Y,X1,X2 DUP2_X1 //这将导致X1,X2,Y,X1,X2

POP2 // this will result in X1,X2,Y POP2 //这将导致X1,X2,Y

DUP_X2 // this will result in Y,X1,X2,Y DUP_X2 //这将导致Y,X1,X2,Y

Thus again, you have Y on top of the stack. 同样,您在堆栈顶部有Y。 And everything below it is just as it was before. 下方的所有内容均与以前相同。

In both these cases, what you finally get is the Object Ref (Y) on top of the stack, allowing you to use it for any operation of your choice, eg a method call. 在这两种情况下,最终得到的都是堆栈顶部的对象引用(Y),使您可以将其用于您选择的任何操作,例如方法调用。 Once that operation is complete, the state of the stack is exactly as it was before you carried out the manipulations. 该操作完成后,堆栈的状态将与执行操作之前的状态完全相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM