Skip to content

Commit

Permalink
Merge latest openjdk
Browse files Browse the repository at this point in the history
  • Loading branch information
j9build committed Nov 8, 2024
2 parents 5f05133 + 76fec3d commit 00a5720
Show file tree
Hide file tree
Showing 45 changed files with 1,520 additions and 557 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,8 @@ MemorySegment reinterpret(long newSize,
* @param offset offset in bytes (relative to this segment address) at which this
* access operation will occur
* @param charset the charset used to {@linkplain Charset#newDecoder() decode} the
* string bytes
* string bytes. The {@code charset} must be a
* {@linkplain StandardCharsets standard charset}
* @return a Java string constructed from the bytes read from the given starting
* address up to (but not including) the first {@code '\0'} terminator
* character (assuming one is found)
Expand Down Expand Up @@ -1375,7 +1376,9 @@ MemorySegment reinterpret(long newSize,
* access operation will occur, the final address of this write
* operation can be expressed as {@code address() + offset}
* @param str the Java string to be written into this segment
* @param charset the charset used to {@linkplain Charset#newEncoder() encode} the string bytes
* @param charset the charset used to {@linkplain Charset#newEncoder() encode} the
* string bytes. The {@code charset} must be a
* {@linkplain StandardCharsets standard charset}
* @throws IndexOutOfBoundsException if {@code offset < 0}
* @throws IndexOutOfBoundsException if {@code offset > byteSize() - (B + N)}, where:
* <ul>
Expand Down
64 changes: 40 additions & 24 deletions src/java.base/share/classes/javax/crypto/KDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -396,19 +396,21 @@ public static KDF getInstance(String algorithm,
InvalidAlgorithmParameterException {
Objects.requireNonNull(algorithm, "algorithm must not be null");
Objects.requireNonNull(provider, "provider must not be null");

Instance instance = GetInstance.getInstance("KDF", KDFSpi.class,
algorithm,
kdfParameters,
provider);
if (!JceSecurity.canUseProvider(instance.provider)) {
String msg = "JCE cannot authenticate the provider "
+ instance.provider.getName();
throw new NoSuchProviderException(msg);
try {
Instance instance = GetInstance.getInstance("KDF", KDFSpi.class,
algorithm,
kdfParameters,
provider);
if (!JceSecurity.canUseProvider(instance.provider)) {
String msg = "JCE cannot authenticate the provider "
+ instance.provider.getName();
throw new NoSuchProviderException(msg);
}
return new KDF(new Delegate((KDFSpi) instance.impl,
instance.provider), algorithm);
} catch (NoSuchAlgorithmException nsae) {
return handleException(nsae);
}
return new KDF(new Delegate((KDFSpi) instance.impl,
instance.provider), algorithm
);
}

/**
Expand Down Expand Up @@ -444,18 +446,31 @@ public static KDF getInstance(String algorithm,
InvalidAlgorithmParameterException {
Objects.requireNonNull(algorithm, "algorithm must not be null");
Objects.requireNonNull(provider, "provider must not be null");
Instance instance = GetInstance.getInstance("KDF", KDFSpi.class,
algorithm,
kdfParameters,
provider);
if (!JceSecurity.canUseProvider(instance.provider)) {
String msg = "JCE cannot authenticate the provider "
+ instance.provider.getName();
throw new SecurityException(msg);
try {
Instance instance = GetInstance.getInstance("KDF", KDFSpi.class,
algorithm,
kdfParameters,
provider);
if (!JceSecurity.canUseProvider(instance.provider)) {
String msg = "JCE cannot authenticate the provider "
+ instance.provider.getName();
throw new SecurityException(msg);
}
return new KDF(new Delegate((KDFSpi) instance.impl,
instance.provider), algorithm);
} catch (NoSuchAlgorithmException nsae) {
return handleException(nsae);
}
}

private static KDF handleException(NoSuchAlgorithmException e)
throws NoSuchAlgorithmException,
InvalidAlgorithmParameterException {
Throwable cause = e.getCause();
if (cause instanceof InvalidAlgorithmParameterException iape) {
throw iape;
}
return new KDF(new Delegate((KDFSpi) instance.impl,
instance.provider), algorithm
);
throw e;
}

/**
Expand Down Expand Up @@ -671,7 +686,8 @@ private static Delegate getNext(Iterator<Service> serviceIter,
if (hasOne) throw new InvalidAlgorithmParameterException(
"The KDFParameters supplied could not be used in combination "
+ "with the supplied algorithm for the selected Provider");
else throw new NoSuchAlgorithmException();
else throw new NoSuchAlgorithmException(
"No available provider supports the specified algorithm");
}

private static boolean checkSpiNonNull(Delegate d) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ final class ConfinedSession extends MemorySessionImpl {

private int asyncReleaseCount = 0;

static final VarHandle ASYNC_RELEASE_COUNT= MhUtil.findVarHandle(
MethodHandles.lookup(), "asyncReleaseCount", int.class);
static final VarHandle ASYNC_RELEASE_COUNT= MhUtil.findVarHandle(MethodHandles.lookup(), "asyncReleaseCount", int.class);

public ConfinedSession(Thread owner) {
super(owner, new ConfinedResourceList());
Expand All @@ -52,17 +51,17 @@ public ConfinedSession(Thread owner) {
@ForceInline
public void acquire0() {
checkValidState();
if (state == MAX_FORKS) {
if (acquireCount == MAX_FORKS) {
throw tooManyAcquires();
}
state++;
acquireCount++;
}

@Override
@ForceInline
public void release0() {
if (Thread.currentThread() == owner) {
state--;
acquireCount--;
} else {
// It is possible to end up here in two cases: this session was kept alive by some other confined session
// which is implicitly released (in which case the release call comes from the cleaner thread). Or,
Expand All @@ -75,11 +74,11 @@ public void release0() {
void justClose() {
checkValidState();
int asyncCount = (int)ASYNC_RELEASE_COUNT.getVolatile(this);
if ((state == 0 && asyncCount == 0)
|| ((state - asyncCount) == 0)) {
int acquire = acquireCount - asyncCount;
if (acquire == 0) {
state = CLOSED;
} else {
throw alreadyAcquired(state - asyncCount);
throw alreadyAcquired(acquire);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ non-sealed class GlobalSession extends MemorySessionImpl {

public GlobalSession() {
super(null, null);
this.state = NONCLOSEABLE;
}

@Override
Expand All @@ -50,11 +51,6 @@ public void release0() {
// do nothing
}

@Override
public boolean isCloseable() {
return false;
}

@Override
@ForceInline
public void acquire0() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class ImplicitSession extends SharedSession {

public ImplicitSession(Cleaner cleaner) {
super();
this.state = NONCLOSEABLE;
cleaner.register(this, resourceList);
}

Expand All @@ -55,11 +56,6 @@ public void acquire0() {
// do nothing
}

@Override
public boolean isCloseable() {
return false;
}

@Override
public void justClose() {
throw nonCloseable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.Stable;

/**
* This class manages the temporal bounds associated with a memory segment as well
Expand All @@ -55,11 +56,19 @@
public abstract sealed class MemorySessionImpl
implements Scope
permits ConfinedSession, GlobalSession, SharedSession {

/**
* The value of the {@code state} of a {@code MemorySessionImpl}. The only possible transition
* is OPEN -> CLOSED. As a result, the states CLOSED and NONCLOSEABLE are stable. This allows
* us to annotate {@code state} with {@link Stable} and elide liveness check on non-closeable
* constant scopes, such as {@code GLOBAL_SESSION}.
*/
static final int OPEN = 0;
static final int CLOSED = -1;
static final int NONCLOSEABLE = 1;

static final VarHandle STATE = MhUtil.findVarHandle(
MethodHandles.lookup(), "state", int.class);
static final VarHandle STATE = MhUtil.findVarHandle(MethodHandles.lookup(), "state", int.class);
static final VarHandle ACQUIRE_COUNT = MhUtil.findVarHandle(MethodHandles.lookup(), "acquireCount", int.class);

static final int MAX_FORKS = Integer.MAX_VALUE;

Expand All @@ -70,7 +79,11 @@ public abstract sealed class MemorySessionImpl

final ResourceList resourceList;
final Thread owner;
int state = OPEN;

@Stable
int state;

int acquireCount;

public Arena asArena() {
return new ArenaImpl(this);
Expand Down Expand Up @@ -214,8 +227,8 @@ protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}

public boolean isCloseable() {
return true;
public final boolean isCloseable() {
return state <= OPEN;
}

/**
Expand Down
39 changes: 26 additions & 13 deletions src/java.base/share/classes/jdk/internal/foreign/SharedSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ sealed class SharedSession extends MemorySessionImpl permits ImplicitSession {

private static final ScopedMemoryAccess SCOPED_MEMORY_ACCESS = ScopedMemoryAccess.getScopedMemoryAccess();

private static final int CLOSED_ACQUIRE_COUNT = -1;

SharedSession() {
super(null, new SharedResourceList());
}
Expand All @@ -53,40 +55,51 @@ sealed class SharedSession extends MemorySessionImpl permits ImplicitSession {
public void acquire0() {
int value;
do {
value = (int) STATE.getVolatile(this);
if (value < OPEN) {
value = (int) ACQUIRE_COUNT.getVolatile(this);
if (value < 0) {
//segment is not open!
throw alreadyClosed();
throw sharedSessionAlreadyClosed();
} else if (value == MAX_FORKS) {
//overflow
throw tooManyAcquires();
}
} while (!STATE.compareAndSet(this, value, value + 1));
} while (!ACQUIRE_COUNT.compareAndSet(this, value, value + 1));
}

@Override
@ForceInline
public void release0() {
int value;
do {
value = (int) STATE.getVolatile(this);
if (value <= OPEN) {
value = (int) ACQUIRE_COUNT.getVolatile(this);
if (value <= 0) {
//cannot get here - we can't close segment twice
throw alreadyClosed();
throw sharedSessionAlreadyClosed();
}
} while (!STATE.compareAndSet(this, value, value - 1));
} while (!ACQUIRE_COUNT.compareAndSet(this, value, value - 1));
}

void justClose() {
int prevState = (int) STATE.compareAndExchange(this, OPEN, CLOSED);
if (prevState < 0) {
throw alreadyClosed();
} else if (prevState != OPEN) {
throw alreadyAcquired(prevState);
int acquireCount = (int) ACQUIRE_COUNT.compareAndExchange(this, 0, CLOSED_ACQUIRE_COUNT);
if (acquireCount < 0) {
throw sharedSessionAlreadyClosed();
} else if (acquireCount > 0) {
throw alreadyAcquired(acquireCount);
}

STATE.setVolatile(this, CLOSED);
SCOPED_MEMORY_ACCESS.closeScope(this, ALREADY_CLOSED);
}

private IllegalStateException sharedSessionAlreadyClosed() {
// To avoid the situation where a scope fails to be acquired or closed but still reports as
// alive afterward, we wait for the state to change before throwing the exception
while ((int) STATE.getVolatile(this) == OPEN) {
Thread.onSpinWait();
}
return alreadyClosed();
}

/**
* A shared resource list; this implementation has to handle add vs. add races, as well as add vs. cleanup races.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 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
Expand Down Expand Up @@ -139,11 +139,13 @@ class UnixConstants {
#endif

// flags used with openat/unlinkat/etc.
#if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_REMOVEDIR)
#if defined(AT_FDCWD) && defined(AT_SYMLINK_NOFOLLOW) && defined(AT_REMOVEDIR)
static final int PREFIX_AT_FDCWD = AT_FDCWD;
static final int PREFIX_AT_SYMLINK_NOFOLLOW = AT_SYMLINK_NOFOLLOW;
static final int PREFIX_AT_REMOVEDIR = AT_REMOVEDIR;
#else
// not supported (dummy values will not be used at runtime).
static final int PREFIX_AT_FDCWD = 00;
static final int PREFIX_AT_SYMLINK_NOFOLLOW = 00;
static final int PREFIX_AT_REMOVEDIR = 00;
#endif
Expand Down
Loading

0 comments on commit 00a5720

Please sign in to comment.