Re: ArrayIndexOutOfBoundsException in generic.PUTFIELD

Razvanica <[email protected]> Tue, 26 May 2009 06:24:55 -0700 (PDT)
Newsgroups gmane.comp.jakarta.bcel.user
Message-ID <[email protected]>
Hello Arrin, 

Thank you for your response. I found the method that fails and it's true
that the bytecode resulting after compiling with the "-source 1.4" flag and
the one that result when I compile with the "-source 1.5" are very
different. Unfortunately for me, I don't have the skills required to judge
what exactly triggers the exception I was talking about (I understand I try
to pop from an empty stack but I haven't yet figured what exactly should be
on that stack). I will post here the 2 bytecodes, hoping you or someone else
would be able to detect the problem. 

The bytecode that works (generated with "-source 1.4") is:
public void _jistPostInit();
  Code:
   0:	aload_0
   1:	getstatic	#24; //Field
jist/runtime/JistAPI.THIS:Ljist/runtime/JistAPI$Entity;
   4:	getstatic	#25; //Field
class$jist$swans$app$AppInterface$UdpApp:Ljava/lang/Class;
   7:	ifnonnull	22
   10:	ldc	#26; //String jist.swans.app.AppInterface$UdpApp
   12:	invokestatic	#27; //Method
class$:(Ljava/lang/String;)Ljava/lang/Class;
   15:	dup
   16:	putstatic	#25; //Field
class$jist$swans$app$AppInterface$UdpApp:Ljava/lang/Class;
   19:	goto	25
   22:	getstatic	#25; //Field
class$jist$swans$app$AppInterface$UdpApp:Ljava/lang/Class;
   25:	invokestatic	#28; //Method
jist/runtime/JistAPI.proxy:(Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;
   28:	checkcast	#29; //class jist/swans/app/AppInterface$UdpApp
   31:	invokeinterface	#30,  1; //InterfaceMethod
jist/swans/app/AppInterface$UdpApp.getUdpEntity:()Ljist/swans/trans/TransInterface$TransUdpInterface;
   36:	putfield	#31; //Field udpEntity:Ljist/swans/trans/TransInterface;
   39:	aload_0
   40:	new	#6; //class java/net/InetSocketAddress
   43:	dup
   44:	aload_0
   45:	getfield	#17; //Field laddr:Ljava/net/InetAddress;
   48:	aload_0
   49:	getfield	#15; //Field lport:I
   52:	invokespecial	#13; //Method
java/net/InetSocketAddress."<init>":(Ljava/net/InetAddress;I)V
   55:	invokevirtual	#32; //Method bind:(Ljava/net/SocketAddress;)V
   58:	return

The one that fails, generated with "-source 1.5" is:
public void _jistPostInit();
  Code:
   0:	aload_0
   1:	getstatic	#19; //Field
jist/runtime/JistAPI.THIS:Ljist/runtime/JistAPI$Entity;
   4:	ldc_w	#20; //class jist/swans/app/AppInterface$UdpApp
   7:	invokestatic	#21; //Method
jist/runtime/JistAPI.proxy:(Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;
   10:	checkcast	#20; //class jist/swans/app/AppInterface$UdpApp
   13:	invokeinterface	#22,  1; //InterfaceMethod
jist/swans/app/AppInterface$UdpApp.getUdpEntity:()Ljist/swans/trans/TransInterface$TransUdpInterface;
   18:	putfield	#23; //Field udpEntity:Ljist/swans/trans/TransInterface;
   21:	aload_0
   22:	new	#1; //class java/net/InetSocketAddress
   25:	dup
   26:	aload_0
   27:	getfield	#12; //Field laddr:Ljava/net/InetAddress;
   30:	aload_0
   31:	getfield	#10; //Field lport:I
   34:	invokespecial	#8; //Method
java/net/InetSocketAddress."<init>":(Ljava/net/InetAddress;I)V
   37:	invokevirtual	#24; //Method bind:(Ljava/net/SocketAddress;)V
   40:	return

The exception appears when the putfield operation is rewritten.

Thank you once again for your answer,

Razvan. 




Arrin Daley wrote:
> 
> Hi
> 
> My experience is that the error you are getting is it because some 
> method code has an invalid stack, in this case you are removing too many 
> items from the stack, the verifier is picking this up and reporting the 
> error. I'm not sure but perhaps some code doesn't call 
> MethodGen.setMaxStack() after transforming, although I doubt it, because 
> if the code were otherwise correct it should have complained of a stack 
> overflow.
> 
> The problem has to be with the method it is verifying at the time so if 
> you can print out which method the verifier is working on and it's 
> bytecodes you could go through it and find the problem. The bytecode 
> pre-transformation by BCEL should be correct so the problem lies with 
> the transformations your code performs.
> 
> I'm don't know why this should be a problem with 1.5 and not 1.4.
> 
> Hope it helps
> 
> Bye Arrin
> 
> Razvanica wrote:
>> Hello all,
>>
>> Here's my problem. I am trying to make some network simulations using a
>> tool
>> called JiST/SWANS. This simulator has been written for Java 1.4 and, for
>> my
>> simulations, I would like to use some 1.5 features, like genericity. 
>>
>> So what I am trying to do is to compile the original code with the
>> "source
>> -1.5" tag instead of the "source -1.4" tag. The problem is that the
>> simulator uses bcel to rewrite the bytecode so that JVM could be used for
>> simulations. The bcel library in the release doesn't work with the
>> "source
>> -1.5" tag so I replace it with bcel-5.2. I made 2 minor modifications to
>> the
>> original code :
>> - I have added an implementation of getClassPath in a class that
>> implements
>> org.apache.bcel.util.Repository. This class just returns null, just like
>> in
>> org.apache.bcel.util.ClassLoaderRepository
>> - In a class using org.apache.bcel.Repository.lookupClass() I am now
>> throwing java.lang.ClassNotFoundException 
>>
>> These 2 modifications shouldn't change anything as far as I understand
>> the
>> application. And when I compile the new code with the "-source 1.4" tag,
>> it
>> doesn,t change anything. Everything compiles and works just fine.
>> However,
>> when I compile the new code with the "-source 1.5" tag the compilation
>> goes
>> smoothly, but, at runtime, the rewriter throws an
>> ArrayIndexOutOfBoundsException.
>>
>>
>> This happens every time org.apache.bcel.generic.PUTFIELD.accept is
>> called:
>> java.lang.ArrayIndexOutOfBoundsException: -1 
>>    at java.util.ArrayList.remove(ArrayList.java:390)
>>    at
>> org.apache.bcel.verifier.structurals.OperandStack.pop(OperandStack.java:135)
>>    at
>> org.apache.bcel.verifier.structurals.ExecutionVisitor.visitPUTFIELD(ExecutionVisitor.java:1048)
>>    at org.apache.bcel.generic.PUTFIELD.accept(PUTFIELD.java:78)
>>
>> Does anybody have any idea what happens and if there's something I could
>> do
>> to fix this?
>>
>> Thank you.
>>   
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/ArrayIndexOutOfBoundsException-in-generic.PUTFIELD-tp23710707p23722920.html
Sent from the BCEL - User mailing list archive at Nabble.com.