Bug 23682 - gnu.java.nio.SelectorImpl.select(long) throws ArrayIndexOutOfBoundsException
Summary: gnu.java.nio.SelectorImpl.select(long) throws ArrayIndexOutOfBoundsException
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libgcj (show other bugs)
Version: 4.0.1
: P2 normal
Target Milestone: 4.3.0
Assignee: Tom Tromey
URL:
Keywords:
: 26506 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-09-01 17:58 UTC by Ilya Konstantinov
Modified: 2007-03-30 22:53 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-07-28 22:06:20


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ilya Konstantinov 2005-09-01 17:58:45 UTC
gnu.java.nio.SelectorImpl.select(long) throws ArrayIndexOutOfBoundsException.
According to the Sun docs, it shouldn't throw it:
http://java.sun.com/j2se/1.4.2/docs/api/java/nio/channels/Selector.html#select(long)
Comment 1 Andrew Pinski 2005-09-01 18:53:02 UTC
ArrayIndexOutOfBoundsException is thrown for no reason as far as I can see from the source?
Do you have an example where it is thrown?
Comment 2 Ilya Konstantinov 2005-09-01 19:11:21 UTC
Here's what Azureus (a free Java Bittorrent client) reported me:
[9:07:22] DEBUG::Thu Sep 01 09:07:22 GMT+03:00
2005::com.aelitis.azureus.core.networkmanager.VirtualChannelSelector::select(long)::-1:
[9:07:22]   Caught exception on selector.select() op: 1
[9:07:22]    
ReadController::readSelectorLoop()::-1,ReadController::access$0(com.aelitis.azureus.core.networkmanager.impl.ReadController)::-1,ReadController$1::runSupport()::-1,AEThread::run()::-1,::GC_start_routine::-1,::__clone::-1
[9:07:22] java.lang.ArrayIndexOutOfBoundsException: 1
   at gnu.java.nio.SelectorImpl.select(long) (/usr/lib64/libgcj.so.6.0.0)
   at
com.aelitis.azureus.core.networkmanager.impl.VirtualChannelSelectorImpl.select(long)
(Unknown Source)
   at
com.aelitis.azureus.core.networkmanager.VirtualChannelSelector.select(long)
(Unknown Source)
   at
com.aelitis.azureus.core.networkmanager.impl.ReadController.readSelectorLoop()
(Unknown Source)
   at
com.aelitis.azureus.core.networkmanager.impl.ReadController.access$0(com.aelitis.azureus.core.networkmanager.impl.ReadController)
(Unknown Source)
   at com.aelitis.azureus.core.networkmanager.impl.ReadController$1.runSupport()
(Unknown Source)
   at org.gudy.azureus2.core3.util.AEThread.run() (Unknown Source)
   at .GC_start_routine (/usr/lib64/libgcj.so.6.0.0)
   at .__clone (/lib64/libc-2.3.5.so)
Comment 3 Anthony Green 2006-04-03 13:02:12 UTC
Azureus users on FC5 see this as well (gcj 4.1.0).  Here's a slightly 
different stack trace.

[12:31:21.648] {stderr} DEBUG::Mon Apr 03 12:31:21 GMT
2006::com.aelitis.azureus.core.networkmanager.VirtualChannelSelector::select::-1:
[12:31:21.649] {stderr}   Caught exception on selector.select() op: 3
[12:31:21.650] {stderr}    
ReadController::readSelectorLoop::-1,ReadController::access$0::-1,ReadController$1::runSupport::-1,AEThread::run::-1
[12:31:21.718] {stderr} java.lang.ArrayIndexOutOfBoundsException: 3
[12:31:21.719] {stderr}    at gnu.java.nio.SelectorImpl.getFDsAsArray (libgcj.so.7)
[12:31:21.720] {stderr}    at gnu.java.nio.SelectorImpl.select (libgcj.so.7)
[12:31:21.720] {stderr}    at
com.aelitis.azureus.core.networkmanager.impl.VirtualChannelSelectorImpl.select
(Azureus2.jar.so)
[12:31:21.721] {stderr}    at
com.aelitis.azureus.core.networkmanager.VirtualChannelSelector.select
(Azureus2.jar.so)
[12:31:21.721] {stderr}    at
com.aelitis.azureus.core.networkmanager.impl.ReadController.readSelectorLoop
(Azureus2.jar.so)
[12:31:21.722] {stderr}    at
com.aelitis.azureus.core.networkmanager.impl.ReadController.access$0
(Azureus2.jar.so)
[12:31:21.722] {stderr}    at
com.aelitis.azureus.core.networkmanager.impl.ReadController$1.runSupport
(Azureus2.jar.so)
[12:31:21.723] {stderr}    at org.gudy.azureus2.core3.util.AEThread.run
(Azureus2.jar.so)
Comment 4 Tom Tromey 2006-04-05 21:38:39 UTC
One possible problem I see here is that the 'interestOps' of
a SelectionKeyImpl can change while getFDsAsArray is running.
There are a few possible fixes here, the simplest would be to
avoid having two loops in getFDsAsArray.

If I send a patch can you easily test it?
Comment 5 Tom Tromey 2006-07-28 22:06:19 UTC
I have a patch I'm testing.

This code in classpath looks quite bad :(
I fixed this problem by adding synchronization.
However, really very little synchronization should be needed.
Instead we could be doing something like building a data structure
for poll() ahead of time and tracking changes to the selection
keys more directly; I think this would yield better performance.
Right now we do multiple allocations for every select operation.

Also I suspect that using select(2) instead of poll(2) is bad
for applications making large numbers of connections.  select
has built-in limits on the the number of file descriptors it can use.

Finally. our code to select without waiting actually will wait
for up to 1ms.  This is fixable as well, it looks like the original
author just did it the "simple" way for convenience of writing a
single native method.
Comment 6 Tom Tromey 2006-07-31 21:58:29 UTC
*** Bug 26506 has been marked as a duplicate of this bug. ***
Comment 7 cvs-commit@developer.classpath.org 2006-07-31 22:09:16 UTC
Subject: Bug 23682

CVSROOT:	/cvsroot/classpath
Module name:	classpath
Changes by:	Tom Tromey <tromey>	06/07/31 22:08:00

Modified files:
	gnu/java/nio   : SelectionKeyImpl.java SelectorImpl.java 
	java/nio/channels: SelectionKey.java 
	.              : ChangeLog 
	java/nio/channels/spi: AbstractSelectionKey.java 

Log message:
		PR libgcj/23682:
		* java/nio/channels/SelectionKey.java (attach): Now synchronized.
		(attachment): Likewise.
		* java/nio/channels/spi/AbstractSelectionKey.java (cancel): Now
		synchronized.
		(isValid): Likewise.
		* gnu/java/nio/SelectionKeyImpl.java (impl): Now final
		(ch): Likewise.
		(interestOps): Synchronize.
		(readyOps): Likewise.
		* gnu/java/nio/SelectorImpl.java (register): Synchronize around
		interestOps call.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/classpath/gnu/java/nio/SelectionKeyImpl.java?cvsroot=classpath&r1=1.9&r2=1.10
http://cvs.savannah.gnu.org/viewcvs/classpath/gnu/java/nio/SelectorImpl.java?cvsroot=classpath&r1=1.22&r2=1.23
http://cvs.savannah.gnu.org/viewcvs/classpath/java/nio/channels/SelectionKey.java?cvsroot=classpath&r1=1.8&r2=1.9
http://cvs.savannah.gnu.org/viewcvs/classpath/ChangeLog?cvsroot=classpath&r1=1.8279&r2=1.8280
http://cvs.savannah.gnu.org/viewcvs/classpath/java/nio/channels/spi/AbstractSelectionKey.java?cvsroot=classpath&r1=1.9&r2=1.10



Comment 8 cvs-commit@developer.classpath.org 2006-08-07 22:23:45 UTC
Subject: Bug 23682

CVSROOT:	/cvsroot/classpath
Module name:	classpath
Branch:		classpath-0_92-branch
Changes by:	Mark Wielaard <mark>	06/08/07 22:22:52

Modified files:
	.              : ChangeLog 
	gnu/java/nio   : SelectionKeyImpl.java SelectorImpl.java 
	java/nio/channels: SelectionKey.java 
	java/nio/channels/spi: AbstractSelectionKey.java 

Log message:
	2006-08-07  Tom Tromey  <tromey@redhat.com>
	
	       PR libgcj/23682:
	       * java/nio/channels/SelectionKey.java (attach): Now synchronized.
	       (attachment): Likewise.
	       * java/nio/channels/spi/AbstractSelectionKey.java (cancel): Now
	       synchronized.
	       (isValid): Likewise.
	       * gnu/java/nio/SelectionKeyImpl.java (impl): Now final
	       (ch): Likewise.
	       (interestOps): Synchronize.
	       (readyOps): Likewise.
	       * gnu/java/nio/SelectorImpl.java (register): Synchronize around
	       interestOps call.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/classpath/ChangeLog?cvsroot=classpath&only_with_tag=classpath-0_92-branch&r1=1.8251.2.28&r2=1.8251.2.29
http://cvs.savannah.gnu.org/viewcvs/classpath/gnu/java/nio/SelectionKeyImpl.java?cvsroot=classpath&only_with_tag=classpath-0_92-branch&r1=1.9&r2=1.9.10.1
http://cvs.savannah.gnu.org/viewcvs/classpath/gnu/java/nio/SelectorImpl.java?cvsroot=classpath&only_with_tag=classpath-0_92-branch&r1=1.22&r2=1.22.2.1
http://cvs.savannah.gnu.org/viewcvs/classpath/java/nio/channels/SelectionKey.java?cvsroot=classpath&only_with_tag=classpath-0_92-branch&r1=1.8&r2=1.8.10.1
http://cvs.savannah.gnu.org/viewcvs/classpath/java/nio/channels/spi/AbstractSelectionKey.java?cvsroot=classpath&only_with_tag=classpath-0_92-branch&r1=1.9&r2=1.9.10.1



Comment 9 cvs-commit@developer.classpath.org 2006-08-07 22:24:19 UTC
Subject: Bug 23682

CVSROOT:	/cvsroot/classpath
Module name:	classpath
Branch:		generics-branch
Changes by:	Mark Wielaard <mark>	06/08/07 22:23:02

Modified files:
	.              : ChangeLog 
	gnu/java/nio   : SelectionKeyImpl.java SelectorImpl.java 
	java/nio/channels: SelectionKey.java 
	java/nio/channels/spi: AbstractSelectionKey.java 

Log message:
	2006-08-07  Tom Tromey  <tromey@redhat.com>
	
	       PR libgcj/23682:
	       * java/nio/channels/SelectionKey.java (attach): Now synchronized.
	       (attachment): Likewise.
	       * java/nio/channels/spi/AbstractSelectionKey.java (cancel): Now
	       synchronized.
	       (isValid): Likewise.
	       * gnu/java/nio/SelectionKeyImpl.java (impl): Now final
	       (ch): Likewise.
	       (interestOps): Synchronize.
	       (readyOps): Likewise.
	       * gnu/java/nio/SelectorImpl.java (register): Synchronize around
	       interestOps call.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/classpath/ChangeLog?cvsroot=classpath&only_with_tag=generics-branch&r1=1.2386.2.321&r2=1.2386.2.322
http://cvs.savannah.gnu.org/viewcvs/classpath/gnu/java/nio/SelectionKeyImpl.java?cvsroot=classpath&only_with_tag=generics-branch&r1=1.8.2.1&r2=1.8.2.2
http://cvs.savannah.gnu.org/viewcvs/classpath/gnu/java/nio/SelectorImpl.java?cvsroot=classpath&only_with_tag=generics-branch&r1=1.16.2.6&r2=1.16.2.7
http://cvs.savannah.gnu.org/viewcvs/classpath/java/nio/channels/SelectionKey.java?cvsroot=classpath&only_with_tag=generics-branch&r1=1.7.2.1&r2=1.7.2.2
http://cvs.savannah.gnu.org/viewcvs/classpath/java/nio/channels/spi/AbstractSelectionKey.java?cvsroot=classpath&only_with_tag=generics-branch&r1=1.8.2.1&r2=1.8.2.2



Comment 10 Tom Tromey 2007-03-30 22:53:22 UTC
The fix went in a while back.

Really someone ought to port classpath's newer poll/epoll-based
nio selector to CNI.  That would be more efficient than what
we have now.