Skip to content

Commit

Permalink
8253063: ScopedAccessError is sometimes thrown spuriously
Browse files Browse the repository at this point in the history
Reviewed-by: eosterlund, psandoz
  • Loading branch information
mcimadamore committed Sep 14, 2020
1 parent 16f0b44 commit 1cc7a78
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/hotspot/share/c1/c1_Runtime1.cpp
Expand Up @@ -638,6 +638,7 @@ address Runtime1::exception_handler_for_pc(JavaThread* thread) {
{
// Enter VM mode by calling the helper
ResetNoHandleMark rnhm;
ExceptionHandlingMark ehm(thread);
continuation = exception_handler_for_pc_helper(thread, exception, pc, nm);
}
// Back in JAVA, use no oops DON'T safepoint
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/jvmci/jvmciRuntime.cpp
Expand Up @@ -375,6 +375,7 @@ address JVMCIRuntime::exception_handler_for_pc(JavaThread* thread) {
{
// Enter VM mode by calling the helper
ResetNoHandleMark rnhm;
ExceptionHandlingMark ehm(thread);
continuation = exception_handler_for_pc_helper(thread, exception, pc, cm);
}
// Back in JAVA, use no oops DON'T safepoint
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/opto/runtime.cpp
Expand Up @@ -1420,6 +1420,7 @@ address OptoRuntime::handle_exception_C(JavaThread* thread) {
// Enter the VM

ResetNoHandleMark rnhm;
ExceptionHandlingMark ehm(thread);
handler_address = handle_exception_C_helper(thread, nm);
}

Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/prims/scopedMemoryAccess.cpp
Expand Up @@ -99,7 +99,7 @@ class CloseScopedMemoryClosure : public HandshakeClosure {
if (var->type() == T_OBJECT) {
if (var->get_obj() == JNIHandles::resolve(_deopt)) {
assert(depth < max_critical_stack_depth, "can't have more than %d critical frames", max_critical_stack_depth);
if (!thread->has_pending_exception()) {
if (!jt->is_exception_handling()) {
jt->install_async_exception(JNIHandles::resolve(_exception));
}
return;
Expand Down
5 changes: 4 additions & 1 deletion src/hotspot/share/runtime/deoptimization.cpp
Expand Up @@ -160,7 +160,10 @@ JRT_BLOCK_ENTRY(Deoptimization::UnrollBlock*, Deoptimization::fetch_unroll_info(
}
thread->inc_in_deopt_handler();

return fetch_unroll_info_helper(thread, exec_mode);
UnrollBlock* result = fetch_unroll_info_helper(thread, exec_mode);
thread->check_and_handle_async_exceptions(true);

return result;
JRT_END

#if COMPILER2_OR_JVMCI
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/runtime/thread.cpp
Expand Up @@ -1712,6 +1712,7 @@ void JavaThread::initialize() {
set_exception_oop(oop());
_exception_pc = 0;
_exception_handler_pc = 0;
_is_exception_handling = false;
_is_method_handle_return = 0;
_jvmti_thread_state= NULL;
_should_post_on_exceptions_flag = JNI_FALSE;
Expand Down
4 changes: 4 additions & 0 deletions src/hotspot/share/runtime/thread.hpp
Expand Up @@ -1211,6 +1211,7 @@ class JavaThread: public Thread {
volatile address _exception_pc; // PC where exception happened
volatile address _exception_handler_pc; // PC for handler of exception
volatile int _is_method_handle_return; // true (== 1) if the current exception PC is a MethodHandle call site.
bool _is_exception_handling;

private:
// support for JNI critical regions
Expand Down Expand Up @@ -1589,6 +1590,9 @@ class JavaThread: public Thread {
void set_exception_handler_pc(address a) { _exception_handler_pc = a; }
void set_is_method_handle_return(bool value) { _is_method_handle_return = value ? 1 : 0; }

bool is_exception_handling() const { return _is_exception_handling; }
void set_is_exception_handling(bool val) { _is_exception_handling = val; }

void clear_exception_oop_and_pc() {
set_exception_oop(NULL);
set_exception_pc(NULL);
Expand Down
10 changes: 10 additions & 0 deletions src/hotspot/share/utilities/exceptions.cpp
Expand Up @@ -506,6 +506,16 @@ ExceptionMark::~ExceptionMark() {
}
}

// Implementation of ExceptionHandlingMark

ExceptionHandlingMark::ExceptionHandlingMark(JavaThread* jt) : _jt(jt) {
jt->set_is_exception_handling(true);
}

ExceptionHandlingMark::~ExceptionHandlingMark() {
_jt->set_is_exception_handling(false);
}

// ----------------------------------------------------------------------------------------

// caller frees value_string if necessary
Expand Down
10 changes: 10 additions & 0 deletions src/hotspot/share/utilities/exceptions.hpp
Expand Up @@ -52,6 +52,7 @@ class Thread;
class Handle;
class Symbol;
class JavaCallArguments;
class JavaThread;
class methodHandle;

// The ThreadShadow class is a helper class to access the _pending_exception
Expand Down Expand Up @@ -322,6 +323,15 @@ class ExceptionMark {
};


class ExceptionHandlingMark {
private:
JavaThread* _jt;

public:
ExceptionHandlingMark(JavaThread* jt);
~ExceptionHandlingMark();
};


// Use an EXCEPTION_MARK for 'local' exceptions. EXCEPTION_MARK makes sure that no
// pending exception exists upon entering its scope and tests that no pending exception
Expand Down
26 changes: 23 additions & 3 deletions test/jdk/java/foreign/TestHandshake.java
Expand Up @@ -28,6 +28,7 @@
* @run testng/othervm TestHandshake
* @run testng/othervm -Xint TestHandshake
* @run testng/othervm -XX:TieredStopAtLevel=1 TestHandshake
* @run testng/othervm -XX:-TieredCompilation TestHandshake
*/

import jdk.incubator.foreign.MemoryAccess;
Expand All @@ -49,9 +50,9 @@

public class TestHandshake {

static final int ITERATIONS = 5;
static final int ITERATIONS = 10;
static final int SEGMENT_SIZE = 1_000_000;
static final int MAX_DELAY_MILLIS = 2000;
static final int MAX_DELAY_MILLIS = 500;
static final int MAX_EXECUTOR_WAIT_SECONDS = 10;

@Test(dataProvider = "accessors")
Expand All @@ -60,7 +61,7 @@ public void testHandshake(Function<MemorySegment, Runnable> accessorFactory) thr
MemorySegment segment = MemorySegment.allocateNative(SEGMENT_SIZE).withOwnerThread(null);
System.err.println("ITERATION " + it);
ExecutorService accessExecutor = Executors.newCachedThreadPool();
for (int i = 0; i < ThreadLocalRandom.current().nextInt(Runtime.getRuntime().availableProcessors()); i++) {
for (int i = 0; i < Runtime.getRuntime().availableProcessors() ; i++) {
accessExecutor.execute(accessorFactory.apply(segment));
}
Thread.sleep(ThreadLocalRandom.current().nextInt(MAX_DELAY_MILLIS));
Expand Down Expand Up @@ -119,6 +120,24 @@ public void run() {
}
}

static class SegmentFillAccessor implements Runnable {

final MemorySegment segment;

SegmentFillAccessor(MemorySegment segment) {
this.segment = segment;
}

@Override
public void run() {
try {
segment.fill((byte)ThreadLocalRandom.current().nextInt(10));
} catch (IllegalStateException ex) {
// do nothing
}
}
}

static class SegmentMismatchAccessor implements Runnable {

final MemorySegment segment;
Expand Down Expand Up @@ -215,6 +234,7 @@ static Object[][] accessors() {
{ (Function<MemorySegment, Runnable>)SegmentAccessor::new },
{ (Function<MemorySegment, Runnable>)SegmentCopyAccessor::new },
{ (Function<MemorySegment, Runnable>)SegmentMismatchAccessor::new },
{ (Function<MemorySegment, Runnable>)SegmentFillAccessor::new },
{ (Function<MemorySegment, Runnable>)BufferAccessor::new },
{ (Function<MemorySegment, Runnable>)BufferHandleAccessor::new }
};
Expand Down

0 comments on commit 1cc7a78

Please sign in to comment.