From 0f381137cb9338453a7d77a7ebdfaa9b34b5028b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eirik=20Bj=C3=B8rsn=C3=B8s?= Date: Wed, 2 Oct 2024 01:27:03 +0000 Subject: [PATCH] 8341243: Use ArraySupport.SOFT_MAX_ARRAY_LENGTH for max array size in java.base Reviewed-by: jpai, smarks --- src/java.base/share/classes/java/io/InputStream.java | 11 +++++------ src/java.base/share/classes/java/util/ArrayDeque.java | 8 +++----- src/java.base/share/classes/java/util/BitSet.java | 4 ++-- src/java.base/share/classes/java/util/Hashtable.java | 10 ++++------ .../java/util/concurrent/ConcurrentHashMap.java | 3 ++- .../share/classes/java/util/regex/Pattern.java | 4 ++-- .../share/classes/java/util/stream/Nodes.java | 6 ++++-- .../internal/foreign/AbstractMemorySegmentImpl.java | 2 +- .../sun/security/util/SignatureFileVerifier.java | 7 ++++--- test/jdk/java/lang/StringBuffer/HugeCapacity.java | 7 +++++-- test/jdk/java/lang/StringBuilder/HugeCapacity.java | 7 +++++-- .../nio/charset/CharsetDecoder/XcodeOverflow.java | 8 +++++--- .../java/util/Base64/TestEncodingDecodingLength.java | 4 +++- .../jdk/java/util/concurrent/tck/ArrayDeque8Test.java | 3 ++- test/jdk/java/util/concurrent/tck/JSR166TestCase.java | 8 ++++---- 15 files changed, 51 insertions(+), 41 deletions(-) diff --git a/src/java.base/share/classes/java/io/InputStream.java b/src/java.base/share/classes/java/io/InputStream.java index 736b6ebd904..a87870bfce0 100644 --- a/src/java.base/share/classes/java/io/InputStream.java +++ b/src/java.base/share/classes/java/io/InputStream.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,6 +25,8 @@ package java.io; +import jdk.internal.util.ArraysSupport; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -305,12 +307,9 @@ public abstract class InputStream implements Closeable { } /** - * The maximum size of array to allocate. - * Some VMs reserve some header words in an array. - * Attempts to allocate larger arrays may result in - * OutOfMemoryError: Requested array size exceeds VM limit + * The maximum size of array to allocate */ - private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8; + private static final int MAX_BUFFER_SIZE = ArraysSupport.SOFT_MAX_ARRAY_LENGTH; /** * Reads all remaining bytes from the input stream. This method blocks until diff --git a/src/java.base/share/classes/java/util/ArrayDeque.java b/src/java.base/share/classes/java/util/ArrayDeque.java index 9997110b658..60bb3ed4a45 100644 --- a/src/java.base/share/classes/java/util/ArrayDeque.java +++ b/src/java.base/share/classes/java/util/ArrayDeque.java @@ -38,6 +38,7 @@ import java.io.Serializable; import java.util.function.Consumer; import java.util.function.Predicate; import jdk.internal.access.SharedSecrets; +import jdk.internal.util.ArraysSupport; /** * Resizable-array implementation of the {@link Deque} interface. Array @@ -124,12 +125,9 @@ public class ArrayDeque extends AbstractCollection transient int tail; /** - * The maximum size of array to allocate. - * Some VMs reserve some header words in an array. - * Attempts to allocate larger arrays may result in - * OutOfMemoryError: Requested array size exceeds VM limit + * The maximum size of array to allocate */ - private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; + private static final int MAX_ARRAY_SIZE = ArraysSupport.SOFT_MAX_ARRAY_LENGTH; /** * Increases the capacity of this deque by at least the given amount. diff --git a/src/java.base/share/classes/java/util/BitSet.java b/src/java.base/share/classes/java/util/BitSet.java index fea9693d7ed..de4910b20c6 100644 --- a/src/java.base/share/classes/java/util/BitSet.java +++ b/src/java.base/share/classes/java/util/BitSet.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1995, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -1184,7 +1184,7 @@ public class BitSet implements Cloneable, java.io.Serializable { public String toString() { checkInvariants(); - final int MAX_INITIAL_CAPACITY = Integer.MAX_VALUE - 8; + final int MAX_INITIAL_CAPACITY = ArraysSupport.SOFT_MAX_ARRAY_LENGTH; int numBits = (wordsInUse > 128) ? cardinality() : wordsInUse * BITS_PER_WORD; // Avoid overflow in the case of a humongous numBits diff --git a/src/java.base/share/classes/java/util/Hashtable.java b/src/java.base/share/classes/java/util/Hashtable.java index 1fd56d5a258..ddff8f7a0ab 100644 --- a/src/java.base/share/classes/java/util/Hashtable.java +++ b/src/java.base/share/classes/java/util/Hashtable.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,6 +30,7 @@ import java.util.function.BiConsumer; import java.util.function.Function; import java.util.function.BiFunction; import jdk.internal.access.SharedSecrets; +import jdk.internal.util.ArraysSupport; /** * This class implements a hash table, which maps keys to values. Any @@ -390,12 +391,9 @@ public class Hashtable } /** - * The maximum size of array to allocate. - * Some VMs reserve some header words in an array. - * Attempts to allocate larger arrays may result in - * OutOfMemoryError: Requested array size exceeds VM limit + * The maximum size of array to allocate */ - private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; + private static final int MAX_ARRAY_SIZE = ArraysSupport.SOFT_MAX_ARRAY_LENGTH; /** * Increases the capacity of and internally reorganizes this diff --git a/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java b/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java index 4df4f73695f..5a23fdb05a6 100644 --- a/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java +++ b/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java @@ -69,6 +69,7 @@ import java.util.function.ToLongBiFunction; import java.util.function.ToLongFunction; import java.util.stream.Stream; import jdk.internal.misc.Unsafe; +import jdk.internal.util.ArraysSupport; /** * A hash table supporting full concurrency of retrievals and @@ -517,7 +518,7 @@ public class ConcurrentHashMap extends AbstractMap * The largest possible (non-power of two) array size. * Needed by toArray and related methods. */ - static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; + static final int MAX_ARRAY_SIZE = ArraysSupport.SOFT_MAX_ARRAY_LENGTH; /** * The default concurrency level for this table. Unused but diff --git a/src/java.base/share/classes/java/util/regex/Pattern.java b/src/java.base/share/classes/java/util/regex/Pattern.java index 0e87bebdcbf..654adb5376f 100644 --- a/src/java.base/share/classes/java/util/regex/Pattern.java +++ b/src/java.base/share/classes/java/util/regex/Pattern.java @@ -1502,8 +1502,8 @@ public final class Pattern return "\\Q" + s + "\\E"; int lenHint = s.length(); - lenHint = (lenHint < Integer.MAX_VALUE - 8 - lenHint) ? - (lenHint << 1) : (Integer.MAX_VALUE - 8); + lenHint = (lenHint < ArraysSupport.SOFT_MAX_ARRAY_LENGTH - lenHint) ? + (lenHint << 1) : ArraysSupport.SOFT_MAX_ARRAY_LENGTH; StringBuilder sb = new StringBuilder(lenHint); sb.append("\\Q"); diff --git a/src/java.base/share/classes/java/util/stream/Nodes.java b/src/java.base/share/classes/java/util/stream/Nodes.java index 7c06823b925..77d70091717 100644 --- a/src/java.base/share/classes/java/util/stream/Nodes.java +++ b/src/java.base/share/classes/java/util/stream/Nodes.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -24,6 +24,8 @@ */ package java.util.stream; +import jdk.internal.util.ArraysSupport; + import java.util.ArrayDeque; import java.util.Arrays; import java.util.Collection; @@ -57,7 +59,7 @@ final class Nodes { /** * The maximum size of an array that can be allocated. */ - static final long MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; + static final long MAX_ARRAY_SIZE = ArraysSupport.SOFT_MAX_ARRAY_LENGTH; // IllegalArgumentException messages static final String BAD_SIZE = "Stream size exceeds max array size"; diff --git a/src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java b/src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java index 64994af5cb7..c3b6295853f 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java +++ b/src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java @@ -379,7 +379,7 @@ public abstract sealed class AbstractMemorySegmentImpl throw new IllegalStateException(String.format("Segment size is not a multiple of %d. Size: %d", elemSize, length)); } long arraySize = length / elemSize; - if (arraySize > (Integer.MAX_VALUE - 8)) { //conservative check + if (arraySize > ArraysSupport.SOFT_MAX_ARRAY_LENGTH) { //conservative check throw new IllegalStateException(String.format("Segment is too large to wrap as %s. Size: %d", typeName, length)); } return (int)arraySize; diff --git a/src/java.base/share/classes/sun/security/util/SignatureFileVerifier.java b/src/java.base/share/classes/sun/security/util/SignatureFileVerifier.java index 1e204816bd6..1576388b653 100644 --- a/src/java.base/share/classes/sun/security/util/SignatureFileVerifier.java +++ b/src/java.base/share/classes/sun/security/util/SignatureFileVerifier.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -36,6 +36,7 @@ import java.util.*; import java.util.jar.Attributes; import java.util.jar.Manifest; +import jdk.internal.util.ArraysSupport; import sun.security.action.GetIntegerAction; import sun.security.jca.Providers; import sun.security.pkcs.PKCS7; @@ -87,8 +88,8 @@ public class SignatureFileVerifier { // the maximum allowed size in bytes for the signature-related files public static final int MAX_SIG_FILE_SIZE = initializeMaxSigFileSize(); - // The maximum size of array to allocate. Some VMs reserve some header words in an array. - private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; + // The maximum size of array to allocate + private static final int MAX_ARRAY_SIZE = ArraysSupport.SOFT_MAX_ARRAY_LENGTH; /** * Create the named SignatureFileVerifier. diff --git a/test/jdk/java/lang/StringBuffer/HugeCapacity.java b/test/jdk/java/lang/StringBuffer/HugeCapacity.java index e3c98496c50..d1ff51dddb2 100644 --- a/test/jdk/java/lang/StringBuffer/HugeCapacity.java +++ b/test/jdk/java/lang/StringBuffer/HugeCapacity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -21,11 +21,14 @@ * questions. */ +import jdk.internal.util.ArraysSupport; + /** * @test * @bug 8218227 * @summary StringBuilder/StringBuffer constructor throws confusing * NegativeArraySizeException + * @modules java.base/jdk.internal.util * @requires (sun.arch.data.model == "64" & os.maxMemory >= 8G) * @run main/othervm -Xms6G -Xmx6G HugeCapacity */ @@ -43,7 +46,7 @@ public class HugeCapacity { private static void testHugeInitialString() { try { - String str = "Z".repeat(Integer.MAX_VALUE - 8); + String str = "Z".repeat(ArraysSupport.SOFT_MAX_ARRAY_LENGTH); StringBuffer sb = new StringBuffer(str); } catch (OutOfMemoryError ignore) { } catch (Throwable unexpected) { diff --git a/test/jdk/java/lang/StringBuilder/HugeCapacity.java b/test/jdk/java/lang/StringBuilder/HugeCapacity.java index a584ce1f07a..a81f1614fe5 100644 --- a/test/jdk/java/lang/StringBuilder/HugeCapacity.java +++ b/test/jdk/java/lang/StringBuilder/HugeCapacity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -21,11 +21,14 @@ * questions. */ +import jdk.internal.util.ArraysSupport; + /** * @test * @bug 8149330 8218227 * @summary Capacity should not get close to Integer.MAX_VALUE unless * necessary + * @modules java.base/jdk.internal.util * @requires (sun.arch.data.model == "64" & os.maxMemory >= 8G) * @run main/othervm -Xms6G -Xmx6G -XX:+CompactStrings HugeCapacity true * @run main/othervm -Xms6G -Xmx6G -XX:-CompactStrings HugeCapacity false @@ -75,7 +78,7 @@ public class HugeCapacity { private static void testHugeInitialString() { try { - String str = "Z".repeat(Integer.MAX_VALUE - 8); + String str = "Z".repeat(ArraysSupport.SOFT_MAX_ARRAY_LENGTH); StringBuilder sb = new StringBuilder(str); } catch (OutOfMemoryError ignore) { } catch (Throwable unexpected) { diff --git a/test/jdk/java/nio/charset/CharsetDecoder/XcodeOverflow.java b/test/jdk/java/nio/charset/CharsetDecoder/XcodeOverflow.java index 7570da3945c..197b61c0ac5 100644 --- a/test/jdk/java/nio/charset/CharsetDecoder/XcodeOverflow.java +++ b/test/jdk/java/nio/charset/CharsetDecoder/XcodeOverflow.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -27,6 +27,7 @@ * @summary Make sure IAE is not thrown on `int` overflow, turning negative * size. The test should either not throw any Throwable, or an OOME * with real Java heap space error (not "exceeds VM limit"). + * @modules java.base/jdk.internal.util * @requires sun.arch.data.model == "64" * @run junit/othervm XcodeOverflow */ @@ -37,6 +38,7 @@ import java.nio.charset.CharacterCodingException; import java.nio.charset.StandardCharsets; import java.util.stream.Stream; +import jdk.internal.util.ArraysSupport; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.Arguments; @@ -44,8 +46,8 @@ import org.junit.jupiter.params.provider.Arguments; public class XcodeOverflow { private static Stream sizes() { return Stream.of( - // SOFT_MAX_ARRAY_LENGTH: copied from ArraysSupport. No overflow; no OOME. - Arguments.of(Integer.MAX_VALUE - 8), + // No overflow; no OOME. + Arguments.of(ArraysSupport.SOFT_MAX_ARRAY_LENGTH), // overflow case: OOME w/ "Java heap space" is thrown on decoding Arguments.of(Integer.MAX_VALUE - 1000000) diff --git a/test/jdk/java/util/Base64/TestEncodingDecodingLength.java b/test/jdk/java/util/Base64/TestEncodingDecodingLength.java index ce6d0db4adc..452f11ebea9 100644 --- a/test/jdk/java/util/Base64/TestEncodingDecodingLength.java +++ b/test/jdk/java/util/Base64/TestEncodingDecodingLength.java @@ -21,6 +21,7 @@ * questions. */ +import jdk.internal.util.ArraysSupport; import org.junit.jupiter.api.Test; import java.lang.reflect.InvocationTargetException; @@ -37,6 +38,7 @@ import static org.junit.jupiter.api.Assertions.fail; * Base64.Decoder.decode behavior with large, (Integer.MAX_VALUE) sized * input array/buffer. Tests the private methods "encodedOutLength" and * "decodedOutLength". + * @modules java.base/jdk.internal.util * @run junit/othervm --add-opens java.base/java.util=ALL-UNNAMED TestEncodingDecodingLength */ @@ -45,7 +47,7 @@ import static org.junit.jupiter.api.Assertions.fail; public class TestEncodingDecodingLength { // A value large enough to test the desired memory conditions in encode and decode - private static final int LARGE_MEM_SIZE = Integer.MAX_VALUE - 8; + private static final int LARGE_MEM_SIZE = ArraysSupport.SOFT_MAX_ARRAY_LENGTH; private static final Base64.Decoder DECODER = Base64.getDecoder(); private static final Base64.Encoder ENCODER = Base64.getEncoder(); diff --git a/test/jdk/java/util/concurrent/tck/ArrayDeque8Test.java b/test/jdk/java/util/concurrent/tck/ArrayDeque8Test.java index eb606b0c781..fb21ebfed36 100644 --- a/test/jdk/java/util/concurrent/tck/ArrayDeque8Test.java +++ b/test/jdk/java/util/concurrent/tck/ArrayDeque8Test.java @@ -36,6 +36,7 @@ import java.util.ArrayDeque; import java.util.Collections; import java.util.Spliterator; +import jdk.internal.util.ArraysSupport; import junit.framework.Test; public class ArrayDeque8Test extends JSR166TestCase { @@ -86,7 +87,7 @@ public class ArrayDeque8Test extends JSR166TestCase { return; final Item e = fortytwo; - final int maxArraySize = Integer.MAX_VALUE - 8; + final int maxArraySize = ArraysSupport.SOFT_MAX_ARRAY_LENGTH; assertThrows(OutOfMemoryError.class, () -> new ArrayDeque(Integer.MAX_VALUE)); diff --git a/test/jdk/java/util/concurrent/tck/JSR166TestCase.java b/test/jdk/java/util/concurrent/tck/JSR166TestCase.java index 451864bcf5d..19897fa0785 100644 --- a/test/jdk/java/util/concurrent/tck/JSR166TestCase.java +++ b/test/jdk/java/util/concurrent/tck/JSR166TestCase.java @@ -38,7 +38,7 @@ * @test id=default * @summary Conformance testing variant of JSR-166 tck tests. * @build * - * @modules java.management + * @modules java.management java.base/jdk.internal.util * @run junit/othervm/timeout=1000 JSR166TestCase */ @@ -47,7 +47,7 @@ * @summary Conformance testing variant of JSR-166 tck tests * with java security manager set to allow. * @build * - * @modules java.management + * @modules java.management java.base/jdk.internal.util * @run junit/othervm/timeout=1000 -Djava.security.manager=allow JSR166TestCase */ @@ -56,7 +56,7 @@ * @summary Test implementation details variant of JSR-166 * tck tests with ForkJoinPool common parallelism. * @build * - * @modules java.management + * @modules java.management java.base/jdk.internal.util * @run junit/othervm/timeout=1000 * --add-opens java.base/java.util.concurrent=ALL-UNNAMED * --add-opens java.base/java.lang=ALL-UNNAMED @@ -78,7 +78,7 @@ * JSR-166 tck tests apart from ForkJoinPool common * parallelism. * @build * - * @modules java.management + * @modules java.management java.base/jdk.internal.util * @run junit/othervm/timeout=1000 * --add-opens java.base/java.util.concurrent=ALL-UNNAMED * --add-opens java.base/java.lang=ALL-UNNAMED