Skip to content

Commit

Permalink
Disallow Random/SplittableRandom in the image heap
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit authored and vjovanov committed Dec 24, 2020
1 parent a3ecb98 commit 11b9817
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import java.lang.reflect.Field;
import java.nio.Buffer;
import java.nio.MappedByteBuffer;
import java.util.Random;
import java.util.SplittableRandom;
import java.util.concurrent.ThreadLocalRandom;

import com.oracle.svm.core.util.VMError;
import com.oracle.svm.util.ReflectionUtil;
Expand All @@ -55,6 +58,13 @@ public interface DisallowedObjectReporter {
}

public static void check(Object obj, DisallowedObjectReporter reporter) {
/* Random/SplittableRandom can not be in the image heap. */
if (((obj instanceof Random) && !(obj instanceof ThreadLocalRandom)) || obj instanceof SplittableRandom) {
throw reporter.raise("Detected an instance of Random/SplittableRandom class in the image heap. " +
"Instances created during image generation have cached seed values and don't behave as expected.",
obj, "Try avoiding to initialize the class that caused initialization of the object.");
}

/* Started Threads can not be in the image heap. */
if (obj instanceof Thread) {
final Thread asThread = (Thread) obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static AtomicLong getSeeder() {
return SINGLETON.getOrInitializeSeeder();
}

/** The setter is necessary if ThreadLocalRandom is initilized at run time. */
/** The setter is necessary if ThreadLocalRandom is initialized at run time. */
public static void setSeeder(AtomicLong value) {
SINGLETON.seeder = value;
}
Expand Down

0 comments on commit 11b9817

Please sign in to comment.