Skip to content

Commit

Permalink
Prevent that an invalid direct memory size is cached in java.nio.Bits…
Browse files Browse the repository at this point in the history
…#MAX_MEMORY.
  • Loading branch information
christianhaeubl committed Oct 6, 2023
1 parent af74d9f commit a606940
Showing 1 changed file with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import java.util.concurrent.atomic.AtomicLong;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.InjectAccessors;
import com.oracle.svm.core.annotate.RecomputeFieldValue;
import com.oracle.svm.core.annotate.RecomputeFieldValue.Kind;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.heap.PhysicalMemory;

@TargetClass(className = "java.nio.Bits")
final class Target_java_nio_Bits {
Expand All @@ -40,9 +42,9 @@ final class Target_java_nio_Bits {
private static int PAGE_SIZE = -1;

@Alias @RecomputeFieldValue(kind = Kind.FromAlias) //
private static boolean MEMORY_LIMIT_SET = false;
@Alias @RecomputeFieldValue(kind = Kind.FromAlias) //
private static long MAX_MEMORY = -1;
private static boolean MEMORY_LIMIT_SET = true;
@Alias @InjectAccessors(MaxMemoryAccessor.class) //
private static long MAX_MEMORY;

@Alias @RecomputeFieldValue(kind = Kind.FromAlias) //
private static AtomicLong RESERVED_MEMORY = new AtomicLong();
Expand All @@ -53,3 +55,23 @@ final class Target_java_nio_Bits {

// Checkstyle: resume
}

/**
* {@code java.nio.Bits} caches the max. direct memory size in the field {@code MAX_MEMORY}. We
* disable this cache and always call {@link DirectMemoryAccessors#getDirectMemory()} instead, which
* uses our own cache. Otherwise, it could happen that {@code MAX_MEMORY} caches a temporary value
* that is used during early VM startup, before {@link PhysicalMemory} is fully initialized.
*/
final class MaxMemoryAccessor {
// Checkstyle: stop

static long getMAX_MEMORY() {
return DirectMemoryAccessors.getDirectMemory();
}

static void setMAX_MEMORY(long value) {
/* Nothing to do. */
}

// Checkstyle: resume
}

0 comments on commit a606940

Please sign in to comment.