You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by Pranav Kuber <pr...@gmail.com> on 2008/11/18 00:21:55 UTC
Popping from operand stack
Hi,
I'm new to using BCEL and have a query.
I'm trying to instrument all IF instructions. So I just need to find out a
way where I could access the operand stack constants (iload_1 and iload_2).
For example, -
if I have some code in this format
if(a!=b)
do Something...
else
do something...
The corresponding bytecode generated would be -
0: iload_1
1: iload_2
2: if_icmpeq 16
5: getstatic #2; //Field
java/lang/System.out:Ljava/io/PrintStream;
8: ldc #3; //String TRUE
............ as so on.
I need to pop iload_1 and iload_2 from the stack and store them somewhere.
Is there any way I could do that?
Thanks for the help.
Regards,
Pranav
Re: Popping from operand stack
Posted by Pranav Kuber <pr...@gmail.com>.
Thanks for the help. It certainly helped me in a way.
One more query -
For the same example in the previous post,
I get
0: iload_1[27](1)
1: iload_2[28](1)
as the instruction handles for the iloads.
So from the list of instruction handles of the bytecode, I get these
instruction handles as arguments in my method.
Is there any way where I could access the operand values using instruction
handles?
Thanks,
Pranav
On Mon, Nov 17, 2008 at 10:50 PM, Dave Brosius <db...@apache.org> wrote:
> Well, in general it's not going to be easy to do what you want in the
> general case, but perhaps you only care about simple cases, where
> there is only one comparison in an if, or some other restriction.
>
> In theory, the IF_CMPEQ can simply be replaced with a call to
> ALOAD x
> INVOKEVIRTUAL
>
> where x is the object that you are calling your method on,
> since in this case, the two parameters are on the top of the stack.
> in the same way that parameters would be pushed.
>
> ----- Original Message ----- From: "Pranav Kuber" <pr...@gmail.com>
> To: "BCEL Users List" <bc...@jakarta.apache.org>
> Sent: Monday, November 17, 2008 7:09 PM
> Subject: Re: Popping from operand stack
>
>
>
> Thanks for the help.
>>
>> Actually, I want to completely replace the if instruction with my code. So
>> at runtime, instead of calling the if instruction if_icmpeq, my code shall
>> get executed. So I just need the values from the stack to make a method
>> call
>> passing these two values as arguments, thus, bypassing the if instruction.
>>
>> -Pranav
>>
>> On Mon, Nov 17, 2008 at 6:56 PM, Arrin Daley <ar...@anu.edu.au>
>> wrote:
>>
>> Hi Pranav
>>>
>>> Perhaps a better way might be to insert DUP (or some of the other DUP
>>> variants) after the iload? instructions so that you copy the contents of
>>> the stack then you are free to do what you like with these as long as you
>>> return the stack to the state it was in before your instrumentation.
>>>
>>> eg
>>> BYTECODES STACK
>>>
>>> 0: iload_1 i1
>>> 1: iload_2 i1,i2
>>> DUP2 i1,12,i1,i2
>>> ....Some code to instrument the values of i1 and i2...
>>> i1,i2
>>> 2: if_icmpeq 16
>>> 5: getstatic #2; //Field
>>> java/lang/System.out:Ljava/io/PrintStream;
>>> 8: ldc #3; //String TRUE
>>> ............ as so on.
>>>
>>>
>>> Note in this case copying the values was easy because they were single
>>> word
>>> values doubles require more care and you may have to use other DUP
>>> bytecodes
>>> and perhaps something a bit more complicated, shouldn't be too bad
>>> though.
>>>
>>> Notice the stack returns to i1,i2 if you don't return the stack to the
>>> same
>>> state you'll have problems.
>>>
>>> Remember to call MethodGen.setMaxStack() when you're finished because
>>> you've possibly changed the maximum stack height.
>>>
>>> Hope this helps
>>>
>>> Bye Arrin
>>>
>>>
>>> Pranav Kuber wrote:
>>>
>>> Hi,
>>>>
>>>> I'm new to using BCEL and have a query.
>>>>
>>>> I'm trying to instrument all IF instructions. So I just need to find out
>>>> a
>>>> way where I could access the operand stack constants (iload_1 and
>>>> iload_2).
>>>>
>>>> For example, -
>>>> if I have some code in this format
>>>>
>>>> if(a!=b)
>>>> do Something...
>>>> else
>>>> do something...
>>>>
>>>> The corresponding bytecode generated would be -
>>>>
>>>> 0: iload_1
>>>> 1: iload_2
>>>> 2: if_icmpeq 16
>>>> 5: getstatic #2; //Field
>>>> java/lang/System.out:Ljava/io/PrintStream;
>>>> 8: ldc #3; //String TRUE
>>>> ............ as so on.
>>>>
>>>> I need to pop iload_1 and iload_2 from the stack and store them
>>>> somewhere.
>>>>
>>>> Is there any way I could do that?
>>>>
>>>> Thanks for the help.
>>>>
>>>> Regards,
>>>> Pranav
>>>>
>>>>
>>>>
>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail: bcel-user-help@jakarta.apache.org
>>>
>>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bcel-user-help@jakarta.apache.org
>
>
--
Pranav Kuber
Graduate Student in Computer Science
Georgia Institute of Technology
http://www.cc.gatech.edu/~pkuber/
Re: Popping from operand stack
Posted by Dave Brosius <db...@apache.org>.
Well, in general it's not going to be easy to do what you want in the
general case, but perhaps you only care about simple cases, where
there is only one comparison in an if, or some other restriction.
In theory, the IF_CMPEQ can simply be replaced with a call to
ALOAD x
INVOKEVIRTUAL
where x is the object that you are calling your method on,
since in this case, the two parameters are on the top of the stack.
in the same way that parameters would be pushed.
----- Original Message -----
From: "Pranav Kuber" <pr...@gmail.com>
To: "BCEL Users List" <bc...@jakarta.apache.org>
Sent: Monday, November 17, 2008 7:09 PM
Subject: Re: Popping from operand stack
> Thanks for the help.
>
> Actually, I want to completely replace the if instruction with my code. So
> at runtime, instead of calling the if instruction if_icmpeq, my code shall
> get executed. So I just need the values from the stack to make a method
> call
> passing these two values as arguments, thus, bypassing the if instruction.
>
> -Pranav
>
> On Mon, Nov 17, 2008 at 6:56 PM, Arrin Daley <ar...@anu.edu.au>
> wrote:
>
>> Hi Pranav
>>
>> Perhaps a better way might be to insert DUP (or some of the other DUP
>> variants) after the iload? instructions so that you copy the contents of
>> the stack then you are free to do what you like with these as long as you
>> return the stack to the state it was in before your instrumentation.
>>
>> eg
>> BYTECODES STACK
>>
>> 0: iload_1 i1
>> 1: iload_2 i1,i2
>> DUP2 i1,12,i1,i2
>> ....Some code to instrument the values of i1 and i2...
>> i1,i2
>> 2: if_icmpeq 16
>> 5: getstatic #2; //Field
>> java/lang/System.out:Ljava/io/PrintStream;
>> 8: ldc #3; //String TRUE
>> ............ as so on.
>>
>>
>> Note in this case copying the values was easy because they were single
>> word
>> values doubles require more care and you may have to use other DUP
>> bytecodes
>> and perhaps something a bit more complicated, shouldn't be too bad
>> though.
>>
>> Notice the stack returns to i1,i2 if you don't return the stack to the
>> same
>> state you'll have problems.
>>
>> Remember to call MethodGen.setMaxStack() when you're finished because
>> you've possibly changed the maximum stack height.
>>
>> Hope this helps
>>
>> Bye Arrin
>>
>>
>> Pranav Kuber wrote:
>>
>>> Hi,
>>>
>>> I'm new to using BCEL and have a query.
>>>
>>> I'm trying to instrument all IF instructions. So I just need to find out
>>> a
>>> way where I could access the operand stack constants (iload_1 and
>>> iload_2).
>>>
>>> For example, -
>>> if I have some code in this format
>>>
>>> if(a!=b)
>>> do Something...
>>> else
>>> do something...
>>>
>>> The corresponding bytecode generated would be -
>>>
>>> 0: iload_1
>>> 1: iload_2
>>> 2: if_icmpeq 16
>>> 5: getstatic #2; //Field
>>> java/lang/System.out:Ljava/io/PrintStream;
>>> 8: ldc #3; //String TRUE
>>> ............ as so on.
>>>
>>> I need to pop iload_1 and iload_2 from the stack and store them
>>> somewhere.
>>>
>>> Is there any way I could do that?
>>>
>>> Thanks for the help.
>>>
>>> Regards,
>>> Pranav
>>>
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: bcel-user-help@jakarta.apache.org
>>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org
Re: Popping from operand stack
Posted by Pranav Kuber <pr...@gmail.com>.
Thanks for the help.
Actually, I want to completely replace the if instruction with my code. So
at runtime, instead of calling the if instruction if_icmpeq, my code shall
get executed. So I just need the values from the stack to make a method call
passing these two values as arguments, thus, bypassing the if instruction.
-Pranav
On Mon, Nov 17, 2008 at 6:56 PM, Arrin Daley <ar...@anu.edu.au> wrote:
> Hi Pranav
>
> Perhaps a better way might be to insert DUP (or some of the other DUP
> variants) after the iload? instructions so that you copy the contents of
> the stack then you are free to do what you like with these as long as you
> return the stack to the state it was in before your instrumentation.
>
> eg
> BYTECODES STACK
>
> 0: iload_1 i1
> 1: iload_2 i1,i2
> DUP2 i1,12,i1,i2
> ....Some code to instrument the values of i1 and i2...
> i1,i2
> 2: if_icmpeq 16
> 5: getstatic #2; //Field
> java/lang/System.out:Ljava/io/PrintStream;
> 8: ldc #3; //String TRUE
> ............ as so on.
>
>
> Note in this case copying the values was easy because they were single word
> values doubles require more care and you may have to use other DUP bytecodes
> and perhaps something a bit more complicated, shouldn't be too bad though.
>
> Notice the stack returns to i1,i2 if you don't return the stack to the same
> state you'll have problems.
>
> Remember to call MethodGen.setMaxStack() when you're finished because
> you've possibly changed the maximum stack height.
>
> Hope this helps
>
> Bye Arrin
>
>
> Pranav Kuber wrote:
>
>> Hi,
>>
>> I'm new to using BCEL and have a query.
>>
>> I'm trying to instrument all IF instructions. So I just need to find out a
>> way where I could access the operand stack constants (iload_1 and
>> iload_2).
>>
>> For example, -
>> if I have some code in this format
>>
>> if(a!=b)
>> do Something...
>> else
>> do something...
>>
>> The corresponding bytecode generated would be -
>>
>> 0: iload_1
>> 1: iload_2
>> 2: if_icmpeq 16
>> 5: getstatic #2; //Field
>> java/lang/System.out:Ljava/io/PrintStream;
>> 8: ldc #3; //String TRUE
>> ............ as so on.
>>
>> I need to pop iload_1 and iload_2 from the stack and store them somewhere.
>>
>> Is there any way I could do that?
>>
>> Thanks for the help.
>>
>> Regards,
>> Pranav
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bcel-user-help@jakarta.apache.org
>
Re: Popping from operand stack
Posted by Arrin Daley <ar...@anu.edu.au>.
Hi Pranav
Perhaps a better way might be to insert DUP (or some of the other DUP
variants) after the iload? instructions so that you copy the contents
of the stack then you are free to do what you like with these as long as
you return the stack to the state it was in before your instrumentation.
eg
BYTECODES STACK
0: iload_1 i1
1: iload_2 i1,i2
DUP2 i1,12,i1,i2
....Some code to instrument the values of i1 and i2...
i1,i2
2: if_icmpeq 16
5: getstatic #2; //Field
java/lang/System.out:Ljava/io/PrintStream;
8: ldc #3; //String TRUE
............ as so on.
Note in this case copying the values was easy because they were single
word values doubles require more care and you may have to use other DUP
bytecodes and perhaps something a bit more complicated, shouldn't be too
bad though.
Notice the stack returns to i1,i2 if you don't return the stack to the
same state you'll have problems.
Remember to call MethodGen.setMaxStack() when you're finished because
you've possibly changed the maximum stack height.
Hope this helps
Bye Arrin
Pranav Kuber wrote:
> Hi,
>
> I'm new to using BCEL and have a query.
>
> I'm trying to instrument all IF instructions. So I just need to find out a
> way where I could access the operand stack constants (iload_1 and iload_2).
>
> For example, -
> if I have some code in this format
>
> if(a!=b)
> do Something...
> else
> do something...
>
> The corresponding bytecode generated would be -
>
> 0: iload_1
> 1: iload_2
> 2: if_icmpeq 16
> 5: getstatic #2; //Field
> java/lang/System.out:Ljava/io/PrintStream;
> 8: ldc #3; //String TRUE
> ............ as so on.
>
> I need to pop iload_1 and iload_2 from the stack and store them somewhere.
>
> Is there any way I could do that?
>
> Thanks for the help.
>
> Regards,
> Pranav
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org
Re: Popping from operand stack
Posted by Paul Keeble <cs...@yahoo.co.uk>.
Running the command iload_1 will put on the stack that integer, iload_2 will do the same for the other integer. However where you choose to save them affects massively what you do next! You might want to explain where you want to store them and how.
Writing out the code yourself in java as it would look like after you have changed it and then looking at the byte code will likely give you the answer you need in terms of what the byte codes are to execute your intended storage plan. Amaziningly with BCEL you could even back engineer the creation of that bytecode from BCEL.
Can't be more specific about the approach without some details and I think once you try creating the java code and looking at the bytecode you will likely work out what it is you need to do. Once you have that then its simply a question of how to do that in BCEL!
Paul
----- Original Message ----
From: Pranav Kuber <pr...@gmail.com>
To: bcel-user@jakarta.apache.org
Sent: Monday, 17 November, 2008 23:21:55
Subject: Popping from operand stack
Hi,
I'm new to using BCEL and have a query.
I'm trying to instrument all IF instructions. So I just need to find out a
way where I could access the operand stack constants (iload_1 and iload_2).
For example, -
if I have some code in this format
if(a!=b)
do Something...
else
do something...
The corresponding bytecode generated would be -
0: iload_1
1: iload_2
2: if_icmpeq 16
5: getstatic #2; //Field
java/lang/System.out:Ljava/io/PrintStream;
8: ldc #3; //String TRUE
............ as so on.
I need to pop iload_1 and iload_2 from the stack and store them somewhere.
Is there any way I could do that?
Thanks for the help.
Regards,
Pranav
---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org