mirror of
https://github.com/JetBrains/JetBrainsRuntime.git
synced 2025-12-06 01:19:28 +01:00
JBR-9637 revert test/jdk changes made in 8370344, 8371315, 8371474
This commit is contained in:
@@ -1,124 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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
|
||||
* @bug 8370344
|
||||
* @requires os.family != "windows"
|
||||
* @requires vm.flavor != "zero"
|
||||
* @requires vm.hasJFR
|
||||
* @summary Test closing a shared scope during faulting access
|
||||
*
|
||||
* @library /test/lib
|
||||
* @build jdk.test.whitebox.WhiteBox
|
||||
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
|
||||
* @run main jdk.test.lib.FileInstaller sharedCloseJfr.jfc sharedCloseJfr.jfc
|
||||
* @run main/othervm
|
||||
* -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
|
||||
* -XX:CompileCommand=exclude,*TestSharedCloseJFR.main
|
||||
* -XX:StartFlightRecording:filename=recording.jfr,dumponexit=true,settings=sharedCloseJfr.jfc
|
||||
* TestSharedCloseJFR
|
||||
*/
|
||||
|
||||
import jdk.test.whitebox.WhiteBox;
|
||||
|
||||
import java.io.RandomAccessFile;
|
||||
import java.lang.foreign.Arena;
|
||||
import java.lang.foreign.MemorySegment;
|
||||
import java.lang.foreign.ValueLayout;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
// We are interested in the following scenario:
|
||||
// When accessing a memory-mapped file that is truncated
|
||||
// a segmentation fault will occur (see also test/hotspot/jtreg/runtime/Unsafe/InternalErrorTest.java)
|
||||
//
|
||||
// This segmentation fault will be caught in the VM's signal handler
|
||||
// and get turned into an InternalError by a VM handshake operation.
|
||||
// This handshake operation calls back into Java to the constructor
|
||||
// of InternalError. This constructor calls super constructors until
|
||||
// it ends up in the constructor of Throwable, where JFR starts logging
|
||||
// the Throwable being created. This logging code adds a bunch
|
||||
// of extra Java frames to the stack.
|
||||
//
|
||||
// All of this occurs during the original memory access, i.e.
|
||||
// while we are inside a @Scoped method call (jdk.internal.misc.ScopedMemoryAccess).
|
||||
// If at this point a shared arena is closed in another thread,
|
||||
// the shared scope closure handshake (src/hotspot/share/prims/scopedMemoryAccess.cpp)
|
||||
// will see all the extra frames added by JFR and the InternalError constructor,
|
||||
// while walking the stack of the thread doing the faulting access.
|
||||
//
|
||||
// This test is here to make sure that the shared scope closure handshake can
|
||||
// deal with that situation.
|
||||
public class TestSharedCloseJFR {
|
||||
|
||||
private static final int PAGE_SIZE = WhiteBox.getWhiteBox().getVMPageSize();
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
String fileName = "tmp.txt";
|
||||
Path path = Path.of(fileName);
|
||||
AtomicBoolean stop = new AtomicBoolean();
|
||||
|
||||
Files.write(path, "1".repeat(PAGE_SIZE + 1000).getBytes());
|
||||
try (RandomAccessFile file = new RandomAccessFile(fileName, "rw")) {
|
||||
FileChannel fileChannel = file.getChannel();
|
||||
MemorySegment segment =
|
||||
fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, fileChannel.size(), Arena.ofAuto());
|
||||
// truncate file
|
||||
// this will make the access fault
|
||||
Files.write(path, "2".getBytes());
|
||||
|
||||
// start worker thread
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
Thread.ofPlatform().start(() -> {
|
||||
latch.countDown();
|
||||
while (!stop.get()) {
|
||||
Arena.ofShared().close(); // hammer VM with handshakes
|
||||
}
|
||||
});
|
||||
|
||||
// wait util the worker thread has started
|
||||
latch.await();
|
||||
|
||||
// access (should fault)
|
||||
// try it a few times until we get a handshake during JFR reporting
|
||||
for (int i = 0; i < 50_000; i++) {
|
||||
try {
|
||||
segment.get(ValueLayout.JAVA_INT, PAGE_SIZE);
|
||||
throw new RuntimeException("InternalError was expected");
|
||||
} catch (InternalError e) {
|
||||
// InternalError as expected
|
||||
if (!e.getMessage().contains("a fault occurred in an unsafe memory access")) {
|
||||
throw new RuntimeException("Unexpected exception", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
// stop worker
|
||||
stop.set(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration label="Custom" version="2.0">
|
||||
<event name="jdk.JavaErrorThrow">
|
||||
<setting name="enabled" control="enable-errors">true</setting>
|
||||
<setting name="stackTrace">true</setting>
|
||||
</event>
|
||||
</configuration>
|
||||
@@ -1,131 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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
|
||||
* @bug 8370344
|
||||
* @library /test/lib
|
||||
* @run junit/native TestSharedCloseJvmti
|
||||
*/
|
||||
|
||||
import jdk.test.lib.Utils;
|
||||
import jdk.test.lib.process.OutputAnalyzer;
|
||||
import jdk.test.lib.process.ProcessTools;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.foreign.Arena;
|
||||
import java.lang.foreign.MemorySegment;
|
||||
import java.lang.foreign.ValueLayout;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
public class TestSharedCloseJvmti {
|
||||
|
||||
private static final String JVMTI_AGENT_LIB = Path.of(Utils.TEST_NATIVE_PATH, System.mapLibraryName("SharedCloseAgent"))
|
||||
.toAbsolutePath().toString();
|
||||
|
||||
@Test
|
||||
void eventDuringScopedAccess() throws Throwable {
|
||||
List<String> command = new ArrayList<>(List.of(
|
||||
"-agentpath:" + JVMTI_AGENT_LIB,
|
||||
"-Xcheck:jni",
|
||||
EventDuringScopedAccessRunner.class.getName()
|
||||
));
|
||||
|
||||
try {
|
||||
ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(command);
|
||||
Process process = ProcessTools.startProcess("fork", pb, null, null, 1L, TimeUnit.MINUTES);
|
||||
OutputAnalyzer output = new OutputAnalyzer(process);
|
||||
output.shouldHaveExitValue(0);
|
||||
output.stderrShouldContain("Exception in thread \"Trigger\" jdk.internal.misc.ScopedMemoryAccess$ScopedAccessError: Invalid memory access");
|
||||
} catch (TimeoutException e) {
|
||||
throw new RuntimeException("Timeout while waiting for forked process");
|
||||
}
|
||||
}
|
||||
|
||||
public static class EventDuringScopedAccessRunner {
|
||||
static final int ADDED_FRAMES = 10;
|
||||
|
||||
static final CountDownLatch MAIN_LATCH = new CountDownLatch(1);
|
||||
static final CountDownLatch TARGET_LATCH = new CountDownLatch(1);
|
||||
|
||||
static volatile int SINK;
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
try (Arena arena = Arena.ofShared()) {
|
||||
MemorySegment segment = arena.allocate(4);
|
||||
// run in separate thread so that waiting on
|
||||
// latch doesn't block main thread
|
||||
Thread.ofPlatform().name("Trigger").start(() -> {
|
||||
SINK = segment.get(ValueLayout.JAVA_INT, 0);
|
||||
});
|
||||
// wait until trigger thread is in JVMTI event callback
|
||||
MAIN_LATCH.await();
|
||||
}
|
||||
// Notify trigger thread that arena was closed
|
||||
TARGET_LATCH.countDown();
|
||||
}
|
||||
|
||||
static boolean reentrant = false;
|
||||
|
||||
// called by jvmti agent
|
||||
// we get here after checking arena liveness
|
||||
private static void target() {
|
||||
String callerName = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).walk(frames ->
|
||||
frames.skip(2).findFirst().orElseThrow().getClassName());
|
||||
if (!callerName.equals("jdk.internal.misc.ScopedMemoryAccess")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (reentrant) {
|
||||
// put some frames on the stack, so stack walk does not see @Scoped method
|
||||
addFrames(0);
|
||||
} else {
|
||||
reentrant = true;
|
||||
try (Arena arena = Arena.ofConfined()) {
|
||||
SINK = arena.allocate(4).get(ValueLayout.JAVA_INT, 0);
|
||||
}
|
||||
reentrant = false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void addFrames(int depth) {
|
||||
if (depth >= ADDED_FRAMES) {
|
||||
// notify main thread to close the arena
|
||||
MAIN_LATCH.countDown();
|
||||
try {
|
||||
// wait here until main thread has closed arena
|
||||
TARGET_LATCH.await();
|
||||
} catch (InterruptedException ex) {
|
||||
throw new RuntimeException("Unexpected interruption");
|
||||
}
|
||||
return;
|
||||
}
|
||||
addFrames(depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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.
|
||||
*/
|
||||
|
||||
#include <jvmti.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static jclass MAIN_CLS;
|
||||
static jmethodID TARGET_ID;
|
||||
|
||||
static const char* TARGET_CLASS_NAME = "TestSharedCloseJvmti$EventDuringScopedAccessRunner";
|
||||
static const char* TARGET_METHOD_NAME = "target";
|
||||
static const char* TARGET_METHOD_SIG = "()V";
|
||||
|
||||
static const char* INTERCEPT_CLASS_NAME = "Ljdk/internal/foreign/MemorySessionImpl;";
|
||||
static const char* INTERCEPT_METHOD_NAME = "checkValidStateRaw";
|
||||
|
||||
void start(jvmtiEnv*, JNIEnv* jni_env, jthread) {
|
||||
|
||||
jclass cls = jni_env->FindClass(TARGET_CLASS_NAME);
|
||||
if (cls == nullptr) {
|
||||
jni_env->ExceptionDescribe();
|
||||
return;
|
||||
}
|
||||
|
||||
MAIN_CLS = (jclass) jni_env->NewGlobalRef(cls);
|
||||
|
||||
TARGET_ID = jni_env->GetStaticMethodID(cls, TARGET_METHOD_NAME, TARGET_METHOD_SIG);
|
||||
if (TARGET_ID == nullptr) {
|
||||
jni_env->ExceptionDescribe();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void method_exit(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, jmethodID method,
|
||||
jboolean was_popped_by_exception, jvalue return_value) {
|
||||
char* method_name = nullptr;
|
||||
jvmtiError err = jvmti_env->GetMethodName(method, &method_name, nullptr, nullptr);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp(method_name, INTERCEPT_METHOD_NAME) != 0) {
|
||||
jvmti_env->Deallocate((unsigned char*) method_name);
|
||||
return;
|
||||
}
|
||||
|
||||
jclass cls;
|
||||
err = jvmti_env->GetMethodDeclaringClass(method, &cls);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
jvmti_env->Deallocate((unsigned char*) method_name);
|
||||
return;
|
||||
}
|
||||
|
||||
char* class_sig = nullptr;
|
||||
err = jvmti_env->GetClassSignature(cls, &class_sig, nullptr);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
jvmti_env->Deallocate((unsigned char*) method_name);
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp(class_sig, INTERCEPT_CLASS_NAME) != 0) {
|
||||
jvmti_env->Deallocate((unsigned char*) method_name);
|
||||
jvmti_env->Deallocate((unsigned char*) class_sig);
|
||||
return;
|
||||
}
|
||||
|
||||
jni_env->CallStaticVoidMethod(MAIN_CLS, TARGET_ID);
|
||||
if (jni_env->ExceptionOccurred()) {
|
||||
jni_env->ExceptionDescribe();
|
||||
}
|
||||
|
||||
jvmti_env->Deallocate((unsigned char*) method_name);
|
||||
jvmti_env->Deallocate((unsigned char*) class_sig);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Agent_OnLoad(JavaVM *vm, char *options, void *reserved) {
|
||||
jvmtiEnv* env;
|
||||
jint jni_err = vm->GetEnv((void**) &env, JVMTI_VERSION);
|
||||
if (jni_err != JNI_OK) {
|
||||
return jni_err;
|
||||
}
|
||||
|
||||
jvmtiCapabilities capabilities{};
|
||||
capabilities.can_generate_method_exit_events = 1;
|
||||
|
||||
jvmtiError err = env->AddCapabilities(&capabilities);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
|
||||
jvmtiEventCallbacks callbacks;
|
||||
callbacks.VMInit = start;
|
||||
callbacks.MethodExit = method_exit;
|
||||
|
||||
err = env->SetEventCallbacks(&callbacks, (jint) sizeof(callbacks));
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_METHOD_EXIT, nullptr);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, nullptr);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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.
|
||||
*/
|
||||
package org.openjdk.bench.java.lang.foreign;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Fork;
|
||||
import org.openjdk.jmh.annotations.Measurement;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Param;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.annotations.TearDown;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
|
||||
import java.lang.foreign.Arena;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@Warmup(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS)
|
||||
@Measurement(iterations = 10, time = 10, timeUnit = TimeUnit.SECONDS)
|
||||
@State(Scope.Benchmark)
|
||||
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
||||
@Fork(value = 1, jvmArgs = { "--enable-native-access=ALL-UNNAMED", "-Djava.library.path=micro/native" })
|
||||
public class SharedCloseStackWalk {
|
||||
|
||||
@Param({"1", "10", "100"})
|
||||
int numOtherThread;
|
||||
|
||||
@Param({"10", "100", "1000"})
|
||||
int extraFrames;
|
||||
|
||||
@Param({"false", "true"})
|
||||
boolean virtualThreads;
|
||||
|
||||
private CountDownLatch stop;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
stop = new CountDownLatch(1);
|
||||
for (int i = 0; i < numOtherThread; i++) {
|
||||
(virtualThreads
|
||||
? Thread.ofVirtual()
|
||||
: Thread.ofPlatform()).start(() -> recurse(0));
|
||||
}
|
||||
}
|
||||
|
||||
@TearDown
|
||||
public void teardown() {
|
||||
stop.countDown();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void sharedOpenClose() {
|
||||
Arena.ofShared().close();
|
||||
}
|
||||
|
||||
private void recurse(int depth) {
|
||||
if (depth == extraFrames) {
|
||||
try {
|
||||
stop.await();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException("Don't interrupt me!", e);
|
||||
}
|
||||
} else {
|
||||
recurse(depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user