undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
162
test/jdk/java/lang/ScopedValue/ManyBindings.java
Normal file
162
test/jdk/java/lang/ScopedValue/ManyBindings.java
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
/*
|
||||
* Copyright (c) 2022, 2025, 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Stress test ScopedValue with many bindings and rebindings
|
||||
* @library /test/lib
|
||||
* @key randomness
|
||||
* @run junit ManyBindings
|
||||
*/
|
||||
|
||||
import java.lang.ScopedValue.Carrier;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
|
||||
import jdk.test.lib.RandomFactory;
|
||||
import jdk.test.lib.thread.VThreadRunner;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ManyBindings {
|
||||
private static final Random RND = RandomFactory.getRandom();
|
||||
|
||||
// number of scoped values to create
|
||||
private static final int SCOPED_VALUE_COUNT = 16;
|
||||
|
||||
// recursive depth to test
|
||||
private static final int MAX_DEPTH = 24;
|
||||
|
||||
/**
|
||||
* Stress test bindings on platform thread.
|
||||
*/
|
||||
@Test
|
||||
void testPlatformThread() {
|
||||
test();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stress test bindings on virtual thread.
|
||||
*/
|
||||
@Test
|
||||
void testVirtualThread() throws Exception {
|
||||
VThreadRunner.run(() -> test());
|
||||
}
|
||||
|
||||
/**
|
||||
* Scoped value and its expected value (or null if not bound).
|
||||
*/
|
||||
record KeyAndValue<T>(ScopedValue<T> key, T value) {
|
||||
KeyAndValue() {
|
||||
this(ScopedValue.newInstance(), null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stress test bindings on current thread.
|
||||
*/
|
||||
private void test() {
|
||||
KeyAndValue<Integer>[] array = new KeyAndValue[SCOPED_VALUE_COUNT];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
array[i] = new KeyAndValue<>();
|
||||
}
|
||||
test(array, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the scoped values in the array have the expected value, then
|
||||
* recursively call this method with some of the scoped values bound to a
|
||||
* new value.
|
||||
*
|
||||
* @param array the scoped values and their expected value
|
||||
* @param depth current recurive depth
|
||||
*/
|
||||
private void test(KeyAndValue<Integer>[] array, int depth) {
|
||||
if (depth > MAX_DEPTH)
|
||||
return;
|
||||
|
||||
// check that the scoped values have the expected values
|
||||
check(array);
|
||||
|
||||
// try to pollute the cache
|
||||
lotsOfReads(array);
|
||||
|
||||
// create a Carrier to bind/rebind some of the scoped values
|
||||
int len = array.length;
|
||||
Carrier carrier = null;
|
||||
|
||||
KeyAndValue<Integer>[] newArray = Arrays.copyOf(array, len);
|
||||
int n = Math.max(1, RND.nextInt(len / 2));
|
||||
while (n > 0) {
|
||||
int index = RND.nextInt(len);
|
||||
ScopedValue<Integer> key = array[index].key;
|
||||
int newValue = RND.nextInt();
|
||||
if (carrier == null) {
|
||||
carrier = ScopedValue.where(key, newValue);
|
||||
} else {
|
||||
carrier = carrier.where(key, newValue);
|
||||
}
|
||||
newArray[index] = new KeyAndValue<>(key, newValue);
|
||||
n--;
|
||||
}
|
||||
|
||||
// invoke recursively
|
||||
carrier.run(() -> {
|
||||
test(newArray, depth+1);
|
||||
});
|
||||
|
||||
// check that the scoped values have the original values
|
||||
check(array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the given scoped values have the expected value.
|
||||
*/
|
||||
private void check(KeyAndValue<Integer>[] array) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
ScopedValue<Integer> key = array[i].key;
|
||||
Integer value = array[i].value;
|
||||
if (value == null) {
|
||||
assertFalse(key.isBound());
|
||||
} else {
|
||||
assertEquals(value, key.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Do lots of reads of the scoped values, to pollute the SV cache.
|
||||
*/
|
||||
private void lotsOfReads(KeyAndValue<Integer>[] array) {
|
||||
for (int k = 0; k < 1000; k++) {
|
||||
int index = RND.nextInt(array.length);
|
||||
Integer value = array[index].value;
|
||||
if (value != null) {
|
||||
ScopedValue<Integer> key = array[index].key;
|
||||
assertEquals(value, key.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
453
test/jdk/java/lang/ScopedValue/ScopedValueAPI.java
Normal file
453
test/jdk/java/lang/ScopedValue/ScopedValueAPI.java
Normal file
|
|
@ -0,0 +1,453 @@
|
|||
/*
|
||||
* Copyright (c) 2021, 2025, 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Test ScopedValue API
|
||||
* @run junit ScopedValueAPI
|
||||
*/
|
||||
|
||||
import java.lang.ScopedValue.CallableOp;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ScopedValueAPI {
|
||||
|
||||
private static Stream<ThreadFactory> factories() {
|
||||
return Stream.of(Thread.ofPlatform().factory(), Thread.ofVirtual().factory());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that where invokes the Runnable's run method.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("factories")
|
||||
void testRunWhere(ThreadFactory factory) throws Exception {
|
||||
test(factory, () -> {
|
||||
class Box { static boolean executed; }
|
||||
ScopedValue<String> name = ScopedValue.newInstance();
|
||||
ScopedValue.where(name, "duke").run(() -> { Box.executed = true; });
|
||||
assertTrue(Box.executed);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test where when the run method throws an exception.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("factories")
|
||||
void testRunWhereThrows(ThreadFactory factory) throws Exception {
|
||||
test(factory, () -> {
|
||||
class FooException extends RuntimeException { }
|
||||
ScopedValue<String> name = ScopedValue.newInstance();
|
||||
Runnable op = () -> { throw new FooException(); };
|
||||
assertThrows(FooException.class, () -> ScopedValue.where(name, "duke").run(op));
|
||||
assertFalse(name.isBound());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that callWhere invokes the CallableOp's call method.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("factories")
|
||||
void testCallWhere(ThreadFactory factory) throws Exception {
|
||||
test(factory, () -> {
|
||||
ScopedValue<String> name = ScopedValue.newInstance();
|
||||
String result = ScopedValue.where(name, "duke").call(name::get);
|
||||
assertEquals("duke", result);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test callWhere when the call method throws an exception.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("factories")
|
||||
void testCallWhereThrows(ThreadFactory factory) throws Exception {
|
||||
test(factory, () -> {
|
||||
class FooException extends RuntimeException { }
|
||||
ScopedValue<String> name = ScopedValue.newInstance();
|
||||
CallableOp<Void, RuntimeException> op = () -> { throw new FooException(); };
|
||||
assertThrows(FooException.class, () -> ScopedValue.where(name, "duke").call(op));
|
||||
assertFalse(name.isBound());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get method.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("factories")
|
||||
void testGet(ThreadFactory factory) throws Exception {
|
||||
test(factory, () -> {
|
||||
ScopedValue<String> name1 = ScopedValue.newInstance();
|
||||
ScopedValue<String> name2 = ScopedValue.newInstance();
|
||||
assertThrows(NoSuchElementException.class, name1::get);
|
||||
assertThrows(NoSuchElementException.class, name2::get);
|
||||
|
||||
// where
|
||||
ScopedValue.where(name1, "duke").run(() -> {
|
||||
assertEquals("duke", name1.get());
|
||||
assertThrows(NoSuchElementException.class, name2::get);
|
||||
|
||||
});
|
||||
assertThrows(NoSuchElementException.class, name1::get);
|
||||
assertThrows(NoSuchElementException.class, name2::get);
|
||||
|
||||
// callWhere
|
||||
ScopedValue.where(name1, "duke").call(() -> {
|
||||
assertEquals("duke", name1.get());
|
||||
assertThrows(NoSuchElementException.class, name2::get);
|
||||
return null;
|
||||
});
|
||||
assertThrows(NoSuchElementException.class, name1::get);
|
||||
assertThrows(NoSuchElementException.class, name2::get);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test isBound method.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("factories")
|
||||
void testIsBound(ThreadFactory factory) throws Exception {
|
||||
test(factory, () -> {
|
||||
ScopedValue<String> name1 = ScopedValue.newInstance();
|
||||
ScopedValue<String> name2 = ScopedValue.newInstance();
|
||||
assertFalse(name1.isBound());
|
||||
assertFalse(name2.isBound());
|
||||
|
||||
// where
|
||||
ScopedValue.where(name1, "duke").run(() -> {
|
||||
assertTrue(name1.isBound());
|
||||
assertFalse(name2.isBound());
|
||||
});
|
||||
assertFalse(name1.isBound());
|
||||
assertFalse(name2.isBound());
|
||||
|
||||
// callWhere
|
||||
ScopedValue.where(name1, "duke").call(() -> {
|
||||
assertTrue(name1.isBound());
|
||||
assertFalse(name2.isBound());
|
||||
return null;
|
||||
});
|
||||
assertFalse(name1.isBound());
|
||||
assertFalse(name2.isBound());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test orElse method.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("factories")
|
||||
void testOrElse(ThreadFactory factory) throws Exception {
|
||||
test(factory, () -> {
|
||||
ScopedValue<String> name = ScopedValue.newInstance();
|
||||
assertEquals("default", name.orElse("default"));
|
||||
|
||||
// where
|
||||
ScopedValue.where(name, "duke").run(() -> {
|
||||
assertEquals("duke", name.orElse("default"));
|
||||
});
|
||||
|
||||
// callWhere
|
||||
ScopedValue.where(name, "duke").call(() -> {
|
||||
assertEquals("duke", name.orElse("default"));
|
||||
return null;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test orElseThrow method.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("factories")
|
||||
void testOrElseThrow(ThreadFactory factory) throws Exception {
|
||||
test(factory, () -> {
|
||||
class FooException extends RuntimeException { }
|
||||
ScopedValue<String> name = ScopedValue.newInstance();
|
||||
assertThrows(FooException.class, () -> name.orElseThrow(FooException::new));
|
||||
|
||||
// where
|
||||
ScopedValue.where(name, "duke").run(() -> {
|
||||
assertEquals("duke", name.orElseThrow(FooException::new));
|
||||
});
|
||||
|
||||
// callWhere
|
||||
ScopedValue.where(name, "duke").call(() -> {
|
||||
assertEquals("duke", name.orElseThrow(FooException::new));
|
||||
return null;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test two bindings.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("factories")
|
||||
void testTwoBindings(ThreadFactory factory) throws Exception {
|
||||
test(factory, () -> {
|
||||
ScopedValue<String> name = ScopedValue.newInstance();
|
||||
ScopedValue<Integer> age = ScopedValue.newInstance();
|
||||
|
||||
// Carrier.run
|
||||
ScopedValue.where(name, "duke").where(age, 100).run(() -> {
|
||||
assertTrue(name.isBound());
|
||||
assertTrue(age.isBound());
|
||||
assertEquals("duke", name.get());
|
||||
assertEquals(100, (int) age.get());
|
||||
});
|
||||
assertFalse(name.isBound());
|
||||
assertFalse(age.isBound());
|
||||
|
||||
// Carrier.call
|
||||
ScopedValue.where(name, "duke").where(age, 100).call(() -> {
|
||||
assertTrue(name.isBound());
|
||||
assertTrue(age.isBound());
|
||||
assertEquals("duke", name.get());
|
||||
assertEquals(100, (int) age.get());
|
||||
return null;
|
||||
});
|
||||
assertFalse(name.isBound());
|
||||
assertFalse(age.isBound());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test rebinding.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("factories")
|
||||
void testRebinding(ThreadFactory factory) throws Exception {
|
||||
test(factory, () -> {
|
||||
ScopedValue<String> name = ScopedValue.newInstance();
|
||||
|
||||
// where
|
||||
ScopedValue.where(name, "duke").run(() -> {
|
||||
assertTrue(name.isBound());
|
||||
assertEquals("duke", name.get());
|
||||
|
||||
ScopedValue.where(name, "duchess").run(() -> {
|
||||
assertTrue(name.isBound());
|
||||
assertEquals("duchess", name.get());
|
||||
});
|
||||
|
||||
assertTrue(name.isBound());
|
||||
assertEquals("duke", name.get());
|
||||
});
|
||||
assertFalse(name.isBound());
|
||||
|
||||
// callWhere
|
||||
ScopedValue.where(name, "duke").call(() -> {
|
||||
assertTrue(name.isBound());
|
||||
assertEquals("duke", name.get());
|
||||
|
||||
ScopedValue.where(name, "duchess").call(() -> {
|
||||
assertTrue(name.isBound());
|
||||
assertEquals("duchess", name.get());
|
||||
return null;
|
||||
});
|
||||
|
||||
assertTrue(name.isBound());
|
||||
assertEquals("duke", name.get());
|
||||
return null;
|
||||
});
|
||||
assertFalse(name.isBound());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test rebinding from null vaue to another value.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("factories")
|
||||
void testRebindingFromNull(ThreadFactory factory) throws Exception {
|
||||
test(factory, () -> {
|
||||
ScopedValue<String> name = ScopedValue.newInstance();
|
||||
|
||||
// where
|
||||
ScopedValue.where(name, null).run(() -> {
|
||||
assertTrue(name.isBound());
|
||||
assertNull(name.get());
|
||||
|
||||
ScopedValue.where(name, "duchess").run(() -> {
|
||||
assertTrue(name.isBound());
|
||||
assertTrue("duchess".equals(name.get()));
|
||||
});
|
||||
|
||||
assertTrue(name.isBound());
|
||||
assertNull(name.get());
|
||||
});
|
||||
assertFalse(name.isBound());
|
||||
|
||||
// callWhere
|
||||
ScopedValue.where(name, null).call(() -> {
|
||||
assertTrue(name.isBound());
|
||||
assertNull(name.get());
|
||||
|
||||
ScopedValue.where(name, "duchess").call(() -> {
|
||||
assertTrue(name.isBound());
|
||||
assertTrue("duchess".equals(name.get()));
|
||||
return null;
|
||||
});
|
||||
|
||||
assertTrue(name.isBound());
|
||||
assertNull(name.get());
|
||||
return null;
|
||||
});
|
||||
assertFalse(name.isBound());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test rebinding to null value.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("factories")
|
||||
void testRebindingToNull(ThreadFactory factory) throws Exception {
|
||||
test(factory, () -> {
|
||||
ScopedValue<String> name = ScopedValue.newInstance();
|
||||
|
||||
// where
|
||||
ScopedValue.where(name, "duke").run(() -> {
|
||||
assertTrue(name.isBound());
|
||||
assertEquals("duke", name.get());
|
||||
|
||||
ScopedValue.where(name, null).run(() -> {
|
||||
assertTrue(name.isBound());
|
||||
assertNull(name.get());
|
||||
});
|
||||
|
||||
assertTrue(name.isBound());
|
||||
assertEquals("duke", name.get());
|
||||
});
|
||||
assertFalse(name.isBound());
|
||||
|
||||
// callWhere
|
||||
ScopedValue.where(name, "duke").call(() -> {
|
||||
assertTrue(name.isBound());
|
||||
assertEquals("duke", name.get());
|
||||
|
||||
ScopedValue.where(name, null).call(() -> {
|
||||
assertTrue(name.isBound());
|
||||
assertNull(name.get());
|
||||
return null;
|
||||
});
|
||||
|
||||
assertTrue(name.isBound());
|
||||
assertEquals("duke", name.get());
|
||||
return null;
|
||||
});
|
||||
assertFalse(name.isBound());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Carrier.get.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("factories")
|
||||
void testCarrierGet(ThreadFactory factory) throws Exception {
|
||||
test(factory, () -> {
|
||||
ScopedValue<String> name = ScopedValue.newInstance();
|
||||
ScopedValue<Integer> age = ScopedValue.newInstance();
|
||||
|
||||
// one scoped value
|
||||
var carrier1 = ScopedValue.where(name, "duke");
|
||||
assertEquals("duke", carrier1.get(name));
|
||||
assertThrows(NoSuchElementException.class, () -> carrier1.get(age));
|
||||
|
||||
// two scoped values
|
||||
var carrier2 = carrier1.where(age, 20);
|
||||
assertEquals("duke", carrier2.get(name));
|
||||
assertEquals(20, (int) carrier2.get(age));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test NullPointerException.
|
||||
*/
|
||||
@Test
|
||||
void testNullPointerException() {
|
||||
ScopedValue<String> name = ScopedValue.newInstance();
|
||||
|
||||
assertThrows(NullPointerException.class, () -> ScopedValue.where(null, "duke"));
|
||||
|
||||
assertThrows(NullPointerException.class, () -> ScopedValue.where(null, "duke").run(() -> { }));
|
||||
assertThrows(NullPointerException.class, () -> ScopedValue.where(name, "duke").run(null));
|
||||
|
||||
assertThrows(NullPointerException.class, () -> ScopedValue.where(null, "duke").call(() -> ""));
|
||||
assertThrows(NullPointerException.class, () -> ScopedValue.where(name, "duke").call(null));
|
||||
|
||||
assertThrows(NullPointerException.class, () -> name.orElse(null));
|
||||
assertThrows(NullPointerException.class, () -> name.orElseThrow(null));
|
||||
|
||||
var carrier = ScopedValue.where(name, "duke");
|
||||
assertThrows(NullPointerException.class, () -> carrier.where(null, "duke"));
|
||||
assertThrows(NullPointerException.class, () -> carrier.get((ScopedValue<?>)null));
|
||||
assertThrows(NullPointerException.class, () -> carrier.run(null));
|
||||
assertThrows(NullPointerException.class, () -> carrier.call(null));
|
||||
assertThrows(NullPointerException.class, () -> carrier.run(() -> name.orElse(null)));
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface ThrowingRunnable {
|
||||
void run() throws Exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the given task in a thread created with the given thread factory.
|
||||
* @throws Exception if the task throws an exception
|
||||
*/
|
||||
private static void test(ThreadFactory factory, ThrowingRunnable task) throws Exception {
|
||||
try (var executor = Executors.newThreadPerTaskExecutor(factory)) {
|
||||
var future = executor.submit(() -> {
|
||||
task.run();
|
||||
return null;
|
||||
});
|
||||
try {
|
||||
future.get();
|
||||
} catch (ExecutionException ee) {
|
||||
Throwable cause = ee.getCause();
|
||||
if (cause instanceof Exception e)
|
||||
throw e;
|
||||
if (cause instanceof Error e)
|
||||
throw e;
|
||||
throw new RuntimeException(cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
262
test/jdk/java/lang/ScopedValue/StressStackOverflow.java
Normal file
262
test/jdk/java/lang/ScopedValue/StressStackOverflow.java
Normal file
|
|
@ -0,0 +1,262 @@
|
|||
/*
|
||||
* Copyright (c) 2021, 2022 Red Hat, Inc. 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test id=default
|
||||
* @summary Stress ScopedValue stack overflow recovery path
|
||||
* @enablePreview
|
||||
* @run main/othervm/timeout=300 StressStackOverflow
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test id=no-TieredCompilation
|
||||
* @enablePreview
|
||||
* @run main/othervm/timeout=300 -XX:-TieredCompilation StressStackOverflow
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test id=TieredStopAtLevel1
|
||||
* @enablePreview
|
||||
* @run main/othervm/timeout=300 -XX:TieredStopAtLevel=1 StressStackOverflow
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test id=no-vmcontinuations
|
||||
* @requires vm.continuations
|
||||
* @enablePreview
|
||||
* @run main/othervm/timeout=300 -XX:+UnlockExperimentalVMOptions -XX:-VMContinuations StressStackOverflow
|
||||
*/
|
||||
|
||||
import java.lang.ScopedValue.CallableOp;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.StructureViolationException;
|
||||
import java.util.concurrent.StructuredTaskScope;
|
||||
import java.util.concurrent.StructuredTaskScope.Joiner;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class StressStackOverflow {
|
||||
public static final ScopedValue<Integer> el = ScopedValue.newInstance();
|
||||
|
||||
public static final ScopedValue<Integer> inheritedValue = ScopedValue.newInstance();
|
||||
|
||||
static final TestFailureException testFailureException = new TestFailureException("Unexpected value for ScopedValue");
|
||||
int ITERS = 1_000_000;
|
||||
|
||||
static class TestFailureException extends RuntimeException {
|
||||
TestFailureException(String s) { super(s); }
|
||||
}
|
||||
|
||||
static final long DURATION_IN_NANOS = Duration.ofMinutes(1).toNanos();
|
||||
|
||||
// Test the ScopedValue recovery mechanism for stack overflows. We implement both CallableOp
|
||||
// and Runnable interfaces. Which one gets tested depends on the constructor argument.
|
||||
class DeepRecursion implements CallableOp<Object, RuntimeException>, Supplier<Object>, Runnable {
|
||||
|
||||
enum Behaviour {
|
||||
CALL, RUN;
|
||||
private static final Behaviour[] values = values();
|
||||
public static Behaviour choose(ThreadLocalRandom tlr) {
|
||||
return values[tlr.nextInt(3)];
|
||||
}
|
||||
}
|
||||
|
||||
final Behaviour behaviour;
|
||||
|
||||
public DeepRecursion(Behaviour behaviour) {
|
||||
this.behaviour = behaviour;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
final var last = el.get();
|
||||
while (ITERS-- > 0) {
|
||||
if (System.nanoTime() - startTime > DURATION_IN_NANOS) {
|
||||
return;
|
||||
}
|
||||
|
||||
var nextRandomFloat = ThreadLocalRandom.current().nextFloat();
|
||||
try {
|
||||
switch (behaviour) {
|
||||
case CALL -> ScopedValue.where(el, el.get() + 1).call(() -> fibonacci_pad(20, this));
|
||||
case RUN -> ScopedValue.where(el, el.get() + 1).run(() -> fibonacci_pad(20, this));
|
||||
}
|
||||
if (!last.equals(el.get())) {
|
||||
throw testFailureException;
|
||||
}
|
||||
} catch (StackOverflowError e) {
|
||||
if (nextRandomFloat <= 0.1) {
|
||||
ScopedValue.where(el, el.get() + 1).run(this);
|
||||
}
|
||||
} catch (TestFailureException e) {
|
||||
throw e;
|
||||
} catch (Throwable throwable) {
|
||||
// StackOverflowErrors cause many different failures. These include
|
||||
// StructureViolationExceptions and InvocationTargetExceptions. This test
|
||||
// checks that, no matter what the failure mode, scoped values are handled
|
||||
// correctly.
|
||||
} finally {
|
||||
if (!last.equals(el.get())) {
|
||||
throw testFailureException;
|
||||
}
|
||||
}
|
||||
|
||||
Thread.yield();
|
||||
}
|
||||
}
|
||||
|
||||
public Object get() {
|
||||
run();
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object call() {
|
||||
return get();
|
||||
}
|
||||
}
|
||||
|
||||
static final Runnable nop = () -> {};
|
||||
|
||||
// Consume some stack.
|
||||
//
|
||||
// The double recursion used here prevents an optimizing JIT from
|
||||
// inlining all the recursive calls, which would make it
|
||||
// ineffective.
|
||||
private long fibonacci_pad1(int n, Runnable op) {
|
||||
if (n <= 1) {
|
||||
op.run();
|
||||
return n;
|
||||
}
|
||||
return fibonacci_pad1(n - 1, op) + fibonacci_pad1(n - 2, nop);
|
||||
}
|
||||
|
||||
private static final Integer I_42 = 42;
|
||||
|
||||
long fibonacci_pad(int n, Runnable op) {
|
||||
final var last = el.get();
|
||||
try {
|
||||
return fibonacci_pad1(ThreadLocalRandom.current().nextInt(n), op);
|
||||
} catch (StackOverflowError err) {
|
||||
if (!inheritedValue.get().equals(I_42)) {
|
||||
throw testFailureException;
|
||||
}
|
||||
if (!last.equals(el.get())) {
|
||||
throw testFailureException;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// Run op in a new thread. Platform or virtual threads are chosen at random.
|
||||
void runInNewThread(Runnable op) {
|
||||
var threadFactory
|
||||
= (ThreadLocalRandom.current().nextBoolean() ? Thread.ofPlatform() : Thread.ofVirtual()).factory();
|
||||
try (var scope = StructuredTaskScope.open(Joiner.awaitAll(), cf -> cf.withThreadFactory(threadFactory))) {
|
||||
var handle = scope.fork(() -> {
|
||||
op.run();
|
||||
return null;
|
||||
});
|
||||
scope.join();
|
||||
handle.get();
|
||||
} catch (TestFailureException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
ScopedValue.where(inheritedValue, 42).where(el, 0).run(() -> {
|
||||
try (var scope = StructuredTaskScope.open(Joiner.awaitAll())) {
|
||||
try {
|
||||
if (ThreadLocalRandom.current().nextBoolean()) {
|
||||
// Repeatedly test Scoped Values set by ScopedValue::call(), get(), and run()
|
||||
final var deepRecursion
|
||||
= new DeepRecursion(DeepRecursion.Behaviour.choose(ThreadLocalRandom.current()));
|
||||
deepRecursion.run();
|
||||
} else {
|
||||
// Recursively run ourself until we get a stack overflow
|
||||
// Catch the overflow and make sure the recovery path works
|
||||
// for values inherited from a StructuredTaskScope.
|
||||
Runnable op = new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
fibonacci_pad(20, this);
|
||||
} catch (StackOverflowError e) {
|
||||
} catch (TestFailureException e) {
|
||||
throw e;
|
||||
} catch (Throwable throwable) {
|
||||
// StackOverflowErrors cause many different failures. These include
|
||||
// StructureViolationExceptions and InvocationTargetExceptions. This test
|
||||
// checks that, no matter what the failure mode, scoped values are handled
|
||||
// correctly.
|
||||
} finally {
|
||||
if (!inheritedValue.get().equals(I_42)) {
|
||||
throw testFailureException;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
runInNewThread(op);
|
||||
}
|
||||
scope.join();
|
||||
} catch (StructureViolationException structureViolationException) {
|
||||
// Can happen if a stack overflow prevented a StackableScope from
|
||||
// being removed. We can continue.
|
||||
} catch (TestFailureException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (TestFailureException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
// Can happen if a stack overflow prevented a StackableScope from
|
||||
// being removed. We can continue.
|
||||
}
|
||||
}
|
||||
|
||||
static long startTime = System.nanoTime();
|
||||
|
||||
public static void main(String[] args) {
|
||||
var torture = new StressStackOverflow();
|
||||
while (torture.ITERS > 0
|
||||
&& System.nanoTime() - startTime <= DURATION_IN_NANOS) {
|
||||
try {
|
||||
torture.run();
|
||||
if (inheritedValue.isBound()) {
|
||||
throw new TestFailureException("Should not be bound here");
|
||||
}
|
||||
} catch (TestFailureException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
// ScopedValueContainer and StructuredTaskScope can
|
||||
// throw many exceptions on stack overflow. Ignore
|
||||
// them all.
|
||||
}
|
||||
}
|
||||
System.out.println("OK");
|
||||
}
|
||||
}
|
||||
59
test/jdk/java/lang/ScopedValue/UnboundValueAfterOOME.java
Normal file
59
test/jdk/java/lang/ScopedValue/UnboundValueAfterOOME.java
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Red Hat, Inc. 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8319120
|
||||
* @run main/othervm -Xmx10m UnboundValueAfterOOME
|
||||
*/
|
||||
public class UnboundValueAfterOOME {
|
||||
|
||||
static final Thread doRun = new Thread() {
|
||||
public void run() {
|
||||
try {
|
||||
try {
|
||||
// Provoke the VM to throw an OutOfMemoryError
|
||||
java.util.Arrays.fill(new int[Integer.MAX_VALUE][], new int[Integer.MAX_VALUE]);
|
||||
} catch (OutOfMemoryError e) {
|
||||
// Try to get() an unbound ScopedValue
|
||||
ScopedValue.newInstance().get();
|
||||
}
|
||||
} catch (NoSuchElementException e) {
|
||||
System.out.println("OK");
|
||||
return;
|
||||
}
|
||||
throw new RuntimeException("Expected NoSuchElementException");
|
||||
}
|
||||
};
|
||||
|
||||
public static void main(String [] args) throws Exception {
|
||||
doRun.run(); // Run on this Thread
|
||||
var job = new Thread(doRun);
|
||||
job.start(); // Run on a new Thread
|
||||
job.join();
|
||||
doRun.start(); // Run on the Thread doRun
|
||||
doRun.join();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue