diff options
11 files changed, 210 insertions, 245 deletions
diff --git a/dex/src/main/java/com/android/dex/Dex.java b/dex/src/main/java/com/android/dex/Dex.java index d0434d620a..59e5ffb761 100644 --- a/dex/src/main/java/com/android/dex/Dex.java +++ b/dex/src/main/java/com/android/dex/Dex.java @@ -129,6 +129,8 @@ public final class Dex { * both {@code .dex} and {@code .odex} input. Calling this constructor * transfers ownership of {@code bytes} to the returned Dex: it is an error * to access the buffer after calling this method. + * + * NOTE: This method is called by the runtime. */ public static Dex create(ByteBuffer data) throws IOException { data.order(ByteOrder.LITTLE_ENDIAN); @@ -351,56 +353,6 @@ public final class Dex { return data.getInt(position); // nameIndex } - public int findStringIndex(String s) { - return Collections.binarySearch(strings, s); - } - - public int findTypeIndex(String descriptor) { - return Collections.binarySearch(typeNames, descriptor); - } - - public int findFieldIndex(FieldId fieldId) { - return Collections.binarySearch(fieldIds, fieldId); - } - - public int findMethodIndex(MethodId methodId) { - return Collections.binarySearch(methodIds, methodId); - } - - public int findClassDefIndexFromTypeIndex(int typeIndex) { - checkBounds(typeIndex, tableOfContents.typeIds.size); - if (!tableOfContents.classDefs.exists()) { - return -1; - } - for (int i = 0; i < tableOfContents.classDefs.size; i++) { - if (typeIndexFromClassDefIndex(i) == typeIndex) { - return i; - } - } - return -1; - } - - /** - * Look up a field id type index from a field index. Cheaper than: - * {@code fieldIds().get(fieldDexIndex).getTypeIndex();} - */ - public int typeIndexFromFieldIndex(int fieldIndex) { - checkBounds(fieldIndex, tableOfContents.fieldIds.size); - int position = tableOfContents.fieldIds.off + (SizeOf.MEMBER_ID_ITEM * fieldIndex); - position += SizeOf.USHORT; // declaringClassIndex - return data.getShort(position) & 0xFFFF; // typeIndex - } - - /** - * Look up a method id declaring class index from a method index. Cheaper than: - * {@code methodIds().get(methodIndex).getDeclaringClassIndex();} - */ - public int declaringClassIndexFromMethodIndex(int methodIndex) { - checkBounds(methodIndex, tableOfContents.methodIds.size); - int position = tableOfContents.methodIds.off + (SizeOf.MEMBER_ID_ITEM * methodIndex); - return data.getShort(position) & 0xFFFF; // declaringClassIndex - } - /** * Look up a method id name index from a method index. Cheaper than: * {@code methodIds().get(methodIndex).getNameIndex();} @@ -470,29 +422,6 @@ public final class Dex { } /** - * Look up a type index index from a class def index. - */ - public int typeIndexFromClassDefIndex(int classDefIndex) { - checkBounds(classDefIndex, tableOfContents.classDefs.size); - int position = tableOfContents.classDefs.off + (SizeOf.CLASS_DEF_ITEM * classDefIndex); - return data.getInt(position); - } - - /** - * Look up an annotation directory offset from a class def index. - */ - public int annotationDirectoryOffsetFromClassDefIndex(int classDefIndex) { - checkBounds(classDefIndex, tableOfContents.classDefs.size); - int position = tableOfContents.classDefs.off + (SizeOf.CLASS_DEF_ITEM * classDefIndex); - position += SizeOf.UINT; // type - position += SizeOf.UINT; // accessFlags - position += SizeOf.UINT; // superType - position += SizeOf.UINT; // interfacesOffset - position += SizeOf.UINT; // sourceFileIndex - return data.getInt(position); - } - - /** * Look up interface types indices from a return type index from a method index. Cheaper than: * {@code ...getClassDef(classDefIndex).getInterfaces();} */ @@ -883,13 +812,6 @@ public final class Dex { } /** - * Returns the number of bytes remaining in this section. - */ - public int remaining() { - return data.remaining(); - } - - /** * Returns the number of bytes used by this section. */ public int used() { diff --git a/dex/src/main/java/com/android/dex/Leb128.java b/dex/src/main/java/com/android/dex/Leb128.java index 1a82e383e9..e4ca5002df 100644 --- a/dex/src/main/java/com/android/dex/Leb128.java +++ b/dex/src/main/java/com/android/dex/Leb128.java @@ -49,33 +49,6 @@ public final class Leb128 { } /** - * Gets the number of bytes in the signed LEB128 encoding of the - * given value. - * - * @param value the value in question - * @return its write size, in bytes - */ - public static int signedLeb128Size(int value) { - // TODO: This could be much cleverer. - - int remaining = value >> 7; - int count = 0; - boolean hasMore = true; - int end = ((value & Integer.MIN_VALUE) == 0) ? 0 : -1; - - while (hasMore) { - hasMore = (remaining != end) - || ((remaining & 1) != ((value >> 6) & 1)); - - value = remaining; - remaining >>= 7; - count++; - } - - return count; - } - - /** * Reads an signed integer from {@code in}. */ public static int readSignedLeb128(ByteInput in) { diff --git a/libart/src/main/java/dalvik/system/VMRuntime.java b/libart/src/main/java/dalvik/system/VMRuntime.java index 60c1e5568f..6a673f2a4b 100644 --- a/libart/src/main/java/dalvik/system/VMRuntime.java +++ b/libart/src/main/java/dalvik/system/VMRuntime.java @@ -365,10 +365,11 @@ public final class VMRuntime { public native void preloadDexCaches(); /** - * Register application info + * Register application info. + * @param profileFile the path of the file where the profile information should be stored. + * @param codePaths the code paths that should be profiled. */ - public static native void registerAppInfo(String packageName, String appDir, - String[] codePaths, String foreignDexProfileDir); + public static native void registerAppInfo(String profileFile, String[] codePaths); /** * Returns the runtime instruction set corresponding to a given ABI. Multiple diff --git a/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java b/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java index da20791e3f..440d2cd2f5 100644 --- a/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java +++ b/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java @@ -40,7 +40,7 @@ import java.util.concurrent.CopyOnWriteArrayList; */ public class ConcurrentCloseTest extends junit.framework.TestCase { private static final InetSocketAddress UNREACHABLE_ADDRESS - = new InetSocketAddress("192.0.2.0", 80); // RFC 6666 + = new InetSocketAddress("192.0.2.0", 80); // RFC 5737 public void test_accept() throws Exception { ServerSocket ss = new ServerSocket(0); diff --git a/luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java b/luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java index 99723619cb..d809eab9ad 100644 --- a/luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java +++ b/luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java @@ -25,6 +25,7 @@ import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.net.InterfaceAddress; +import java.net.MulticastSocket; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Collections; @@ -232,4 +233,10 @@ public class NetworkInterfaceTest extends TestCase { private static boolean isEthernet(String ifName) throws Exception { return ethernetNamePattern.matcher(ifName).matches(); } + + public void testGetInterfaceAddressesDoesNotThrowNPE() throws Exception { + try (MulticastSocket mcastSock = new MulticastSocket()) { + mcastSock.getNetworkInterface().getInterfaceAddresses(); + } + } } diff --git a/luni/src/test/java/libcore/java/net/OldAndroidDatagramTest.java b/luni/src/test/java/libcore/java/net/OldAndroidDatagramTest.java index 108fd35939..65ca12aa79 100644 --- a/luni/src/test/java/libcore/java/net/OldAndroidDatagramTest.java +++ b/luni/src/test/java/libcore/java/net/OldAndroidDatagramTest.java @@ -164,30 +164,6 @@ public class OldAndroidDatagramTest extends TestCase { } } - // Regression test for issue 1018003: DatagramSocket ignored a set timeout. - public void testDatagramSocketSetSOTimeout() throws Exception { - DatagramSocket sock = null; - int timeout = 5000; - long start = System.currentTimeMillis(); - try { - sock = new DatagramSocket(); - DatagramPacket pack = new DatagramPacket(new byte[100], 100); - sock.setSoTimeout(timeout); - sock.receive(pack); - } catch (SocketTimeoutException e) { - // expected - long delay = System.currentTimeMillis() - start; - if (Math.abs(delay - timeout) > 1000) { - fail("timeout was not accurate. expected: " + timeout - + " actual: " + delay + " miliseconds."); - } - } finally { - if (sock != null) { - sock.close(); - } - } - } - public void test_54072_DatagramSocket() throws Exception { DatagramSocket s = new DatagramSocket(null); assertTrue(s.getLocalAddress().isAnyLocalAddress()); diff --git a/luni/src/test/java/libcore/java/net/OldServerSocketTest.java b/luni/src/test/java/libcore/java/net/OldServerSocketTest.java index 85881444bc..8c9c3ea72c 100644 --- a/luni/src/test/java/libcore/java/net/OldServerSocketTest.java +++ b/luni/src/test/java/libcore/java/net/OldServerSocketTest.java @@ -212,15 +212,6 @@ public class OldServerSocketTest extends OldSocketTestCase { } public void test_accept() throws IOException { - ServerSocket newSocket = new ServerSocket(0); - newSocket.setSoTimeout(500); - try { - Socket accepted = newSocket.accept(); - fail("SocketTimeoutException was not thrown: " + accepted); - } catch(SocketTimeoutException expected) { - } - newSocket.close(); - ServerSocketChannel ssc = ServerSocketChannel.open(); ServerSocket ss = ssc.socket(); diff --git a/luni/src/test/java/libcore/java/net/OldSocketTest.java b/luni/src/test/java/libcore/java/net/OldSocketTest.java index d627f2f33d..8a577fb0c4 100644 --- a/luni/src/test/java/libcore/java/net/OldSocketTest.java +++ b/luni/src/test/java/libcore/java/net/OldSocketTest.java @@ -43,7 +43,7 @@ import tests.support.Support_Configuration; public class OldSocketTest extends OldSocketTestCase { private static final InetSocketAddress UNREACHABLE_ADDRESS - = new InetSocketAddress("192.0.2.0", 0); // RFC 6666 + = new InetSocketAddress("192.0.2.0", 0); // RFC 5737 ServerSocket ss; diff --git a/luni/src/test/java/libcore/java/net/SocketTimeoutTest.java b/luni/src/test/java/libcore/java/net/SocketTimeoutTest.java new file mode 100644 index 0000000000..78ee3ab8de --- /dev/null +++ b/luni/src/test/java/libcore/java/net/SocketTimeoutTest.java @@ -0,0 +1,182 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package libcore.java.net; + +import org.junit.Test; + +import java.io.Closeable; +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketTimeoutException; +import java.nio.channels.ServerSocketChannel; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import libcore.util.EmptyArray; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * Tests socket timeout behavior for various different socket types. + */ +public class SocketTimeoutTest { + + private static final int TIMEOUT_MILLIS = 500; + + private static final InetSocketAddress UNREACHABLE_ADDRESS + = new InetSocketAddress("192.0.2.0", 0); // RFC 5737 + + @FunctionalInterface + private interface SocketOperation<T> { + void operate(T s) throws IOException; + } + + @FunctionalInterface + private interface SocketConstructor<T> { + T get() throws IOException; + } + + private static <T extends Closeable> void checkOperationTimesOut(SocketConstructor<T> construct, + SocketOperation<T> op) throws Exception { + try (T socket = construct.get()) { + long startingTime = System.currentTimeMillis(); + try { + op.operate(socket); + fail(); + } catch (SocketTimeoutException timeoutException) { + long timeElapsed = System.currentTimeMillis() - startingTime; + assertTrue( + Math.abs(((float) timeElapsed / TIMEOUT_MILLIS) - 1) + < 0.2f); // Allow some error. + } + } + } + + @Test + public void testSocketConnectTimeout() throws Exception { + // #connect(SocketAddress endpoint, int timeout) + checkOperationTimesOut(() -> new Socket(), s -> s.connect(UNREACHABLE_ADDRESS, + TIMEOUT_MILLIS)); + + // Setting SO_TIMEOUT should not affect connect timeout. + checkOperationTimesOut(() -> new Socket(), + s -> { + s.setSoTimeout(TIMEOUT_MILLIS / 2); + s.connect(UNREACHABLE_ADDRESS, TIMEOUT_MILLIS); + }); + } + + @Test + public void testSocketReadTimeout() throws Exception { + // #read() + try (ServerSocket ss = new ServerSocket(0)) { + // The server socket will accept the connection without explicitly calling accept() due + // to TCP backlog. + + checkOperationTimesOut(() -> new Socket(), s -> { + s.connect(ss.getLocalSocketAddress()); + s.setSoTimeout(TIMEOUT_MILLIS); + s.getInputStream().read(); + }); + } + } + + @Test + public void testSocketWriteNeverTimeouts() throws Exception { + // #write() should block if the buffers are full, and does not drop packets or throw + // SocketTimeoutException. + try (Socket sock = new Socket(); + ServerSocket serverSocket = new ServerSocket(0)) { + // Setting this option should not affect behaviour, as specified by the spec. + sock.setSoTimeout(TIMEOUT_MILLIS); + + // Set SO_SNDBUF and SO_RCVBUF to minimum value allowed by kernel. + sock.setSendBufferSize(1); + serverSocket.setReceiveBufferSize(1); + int actualSize = sock.getSendBufferSize() + serverSocket.getReceiveBufferSize(); + + sock.connect(serverSocket.getLocalSocketAddress()); + + CountDownLatch threadStarted = new CountDownLatch(1); + CountDownLatch writeCompleted = new CountDownLatch(1); + Thread thread = new Thread(() -> { + threadStarted.countDown(); + try { + // Should block + sock.getOutputStream().write(new byte[actualSize + 1]); + writeCompleted.countDown(); + } catch (IOException ignored) { + } finally { + writeCompleted.countDown(); + } + }); + + thread.start(); + + // Wait for the thread to start. + assertTrue(threadStarted.await(500, TimeUnit.MILLISECONDS)); + + // Wait for TIMEOUT_MILLIS + slop. If write does not complete by then, we assume it has + // blocked. + boolean blocked = + !writeCompleted.await(TIMEOUT_MILLIS * 2, TimeUnit.MILLISECONDS); + assertTrue(blocked); + + // Make sure the writing thread completes after the socket is closed. + sock.close(); + assertTrue(writeCompleted.await(5000, TimeUnit.MILLISECONDS)); + } + } + + @Test + public void testServerSocketAcceptTimeout() throws Exception { + // #accept() + checkOperationTimesOut(() -> new ServerSocket(0), + s -> { + s.setSoTimeout(TIMEOUT_MILLIS); + s.accept(); + }); + } + + @Test + public void testServerSocketChannelAcceptTimeout() throws Exception { + // #accept() + checkOperationTimesOut(() -> ServerSocketChannel.open(), + s -> { + s.bind(null, 0); + s.socket().setSoTimeout(TIMEOUT_MILLIS); + s.socket().accept(); + }); + } + + @Test + public void testDatagramSocketReceive() throws Exception { + checkOperationTimesOut(() -> new DatagramSocket(), s -> { + s.setSoTimeout(TIMEOUT_MILLIS); + s.receive(new DatagramPacket(EmptyArray.BYTE, 0)); + }); + } + + // TODO(yikong), http://b/35867657: + // Add tests for SocksSocketImpl once a mock Socks server is implemented. +} diff --git a/ojluni/src/main/java/java/lang/Class.java b/ojluni/src/main/java/java/lang/Class.java index 018507a587..3e7fb6099e 100644 --- a/ojluni/src/main/java/java/lang/Class.java +++ b/ojluni/src/main/java/java/lang/Class.java @@ -2677,91 +2677,7 @@ public final class Class<T> implements java.io.Serializable, } return resolvedType; } - /** - * The annotation directory offset of this class in its own Dex, or 0 if it - * is unknown. - * - * TODO: 0 is a sentinel that means 'no annotations directory'; this should be -1 if unknown - * - * @hide - */ - public int getDexAnnotationDirectoryOffset() { - Dex dex = getDex(); - if (dex == null) { - return 0; - } - int classDefIndex = getDexClassDefIndex(); - if (classDefIndex < 0) { - return 0; - } - return dex.annotationDirectoryOffsetFromClassDefIndex(classDefIndex); - } - /** - * The type index of this class in its own Dex, or -1 if it is unknown. If a class is referenced - * by multiple Dex files, it will have a different type index in each. Dex files support 65534 - * type indices, with 65535 representing no index. - * - * @hide - */ - public int getDexTypeIndex() { - int typeIndex = dexTypeIndex; - if (typeIndex != 65535) { - return typeIndex; - } - synchronized (this) { - typeIndex = dexTypeIndex; - if (typeIndex == 65535) { - if (dexClassDefIndex >= 0) { - typeIndex = getDex().typeIndexFromClassDefIndex(dexClassDefIndex); - } else { - typeIndex = getDex().findTypeIndex(InternalNames.getInternalName(this)); - if (typeIndex < 0) { - typeIndex = -1; - } - } - dexTypeIndex = typeIndex; - } - } - return typeIndex; - } - private boolean canAccess(Class<?> c) { - if(Modifier.isPublic(c.accessFlags)) { - return true; - } - return inSamePackage(c); - } - - private boolean canAccessMember(Class<?> memberClass, int memberModifiers) { - if (memberClass == this || Modifier.isPublic(memberModifiers)) { - return true; - } - if (Modifier.isPrivate(memberModifiers)) { - return false; - } - if (Modifier.isProtected(memberModifiers)) { - for (Class<?> parent = this.superClass; parent != null; parent = parent.superClass) { - if (parent == memberClass) { - return true; - } - } - } - return inSamePackage(memberClass); - } - private boolean inSamePackage(Class<?> c) { - if (classLoader != c.classLoader) { - return false; - } - String packageName1 = getPackageName$(); - String packageName2 = c.getPackageName$(); - if (packageName1 == null) { - return packageName2 == null; - } else if (packageName2 == null) { - return false; - } else { - return packageName1.equals(packageName2); - } - } /** * @hide */ @@ -2780,14 +2696,6 @@ public final class Class<T> implements java.io.Serializable, @FastNative private native Method getDeclaredMethodInternal(String name, Class<?>[] args); - /** - * The class def of this class in its own Dex, or -1 if there is no class def. - * - * @hide - */ - public int getDexClassDefIndex() { - return (dexClassDefIndex == 65535) ? -1 : dexClassDefIndex; - } private static class Caches { /** * Cache to avoid frequent recalculation of generic interfaces, which is generally uncommon. diff --git a/ojluni/src/main/java/java/net/NetworkInterface.java b/ojluni/src/main/java/java/net/NetworkInterface.java index c157eca8fe..2547673c3d 100644 --- a/ojluni/src/main/java/java/net/NetworkInterface.java +++ b/ojluni/src/main/java/java/net/NetworkInterface.java @@ -175,15 +175,20 @@ public final class NetworkInterface { */ public java.util.List<InterfaceAddress> getInterfaceAddresses() { java.util.List<InterfaceAddress> lst = new java.util.ArrayList<InterfaceAddress>(1); - SecurityManager sec = System.getSecurityManager(); - for (int j=0; j<bindings.length; j++) { - try { - if (sec != null) { - sec.checkConnect(bindings[j].getAddress().getHostAddress(), -1); - } - lst.add(bindings[j]); - } catch (SecurityException e) { } + // BEGIN Android-changed: Cherry-picked upstream OpenJDK9 change rev 59a110a38cea + // http://b/30628919 + if (bindings != null) { + SecurityManager sec = System.getSecurityManager(); + for (int j=0; j<bindings.length; j++) { + try { + if (sec != null) { + sec.checkConnect(bindings[j].getAddress().getHostAddress(), -1); + } + lst.add(bindings[j]); + } catch (SecurityException e) { } + } } + // END Android-changed: Cherry-picked upstream OpenJDK9 change rev 59a110a38cea return lst; } |