Skip to content

Commit

Permalink
8300275: SegmentScope.isAccessibleBy returning incorrect values
Browse files Browse the repository at this point in the history
Reviewed-by: alanb, jvernee
  • Loading branch information
mcimadamore committed Jan 18, 2023
1 parent c1b4212 commit b9275a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, 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
Expand Down Expand Up @@ -183,7 +183,7 @@ public static boolean sameOwnerThread(SegmentScope scope1, SegmentScope scope2)
@Override
public final boolean isAccessibleBy(Thread thread) {
Objects.requireNonNull(thread);
return owner == thread;
return owner == null || owner == thread;
}

/**
Expand Down
27 changes: 22 additions & 5 deletions test/jdk/java/foreign/TestSegments.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, 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
Expand Down Expand Up @@ -191,6 +191,25 @@ public void testAccessModesOfFactories(Supplier<MemorySegment> segmentSupplier)
assertFalse(segment.isReadOnly());
}

@DataProvider(name = "scopes")
public Object[][] scopes() {
return new Object[][] {
{ SegmentScope.auto(), false },
{ SegmentScope.global(), false },
{ Arena.openConfined().scope(), true },
{ Arena.openShared().scope(), false }
};
}

@Test(dataProvider = "scopes")
public void testIsAccessibleBy(SegmentScope scope, boolean isConfined) {
assertTrue(scope.isAccessibleBy(Thread.currentThread()));
assertTrue(scope.isAccessibleBy(new Thread()) != isConfined);
MemorySegment segment = MemorySegment.ofAddress(0, 0, scope);
assertTrue(segment.scope().isAccessibleBy(Thread.currentThread()));
assertTrue(segment.scope().isAccessibleBy(new Thread()) != isConfined);
}

@DataProvider(name = "segmentFactories")
public Object[][] segmentFactories() {
List<Supplier<MemorySegment>> l = List.of(
Expand Down Expand Up @@ -264,11 +283,9 @@ public void testFillThread(Supplier<MemorySegment> segmentSupplier) throws Excep
thread.start();
thread.join();

if (segment.scope().isAccessibleBy(Thread.currentThread())) {
if (!segment.scope().isAccessibleBy(Thread.currentThread())) {
RuntimeException e = exception.get();
if (!(e instanceof IllegalStateException)) {
throw e;
}
throw e;
} else {
assertNull(exception.get());
}
Expand Down

0 comments on commit b9275a8

Please sign in to comment.