|
44 | 44 | import java.util.stream.Stream; |
45 | 45 |
|
46 | 46 | import org.graalvm.nativeimage.ImageSingletons; |
| 47 | +import org.graalvm.nativeimage.LogHandler; |
47 | 48 | import org.graalvm.nativeimage.Platform; |
48 | 49 | import org.graalvm.nativeimage.Platforms; |
49 | 50 | import org.graalvm.nativeimage.hosted.FieldValueTransformer; |
|
66 | 67 | import com.oracle.svm.core.hub.DynamicHub; |
67 | 68 | import com.oracle.svm.core.jdk.JavaLangSubstitutions.ClassValueSupport; |
68 | 69 | import com.oracle.svm.core.monitor.MonitorSupport; |
| 70 | +import com.oracle.svm.core.option.HostedOptionKey; |
69 | 71 | import com.oracle.svm.core.snippets.SubstrateForeignCallTarget; |
70 | 72 | import com.oracle.svm.core.thread.JavaThreads; |
71 | 73 | import com.oracle.svm.core.thread.VMOperation; |
72 | 74 | import com.oracle.svm.core.util.VMError; |
73 | 75 | import com.oracle.svm.util.ReflectionUtil; |
74 | 76 |
|
| 77 | +import jdk.graal.compiler.options.Option; |
75 | 78 | import jdk.graal.compiler.replacements.nodes.BinaryMathIntrinsicNode; |
76 | 79 | import jdk.graal.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation; |
77 | 80 | import jdk.graal.compiler.replacements.nodes.UnaryMathIntrinsicNode; |
@@ -342,21 +345,6 @@ final class Target_java_lang_System { |
342 | 345 | @Alias private static PrintStream err; |
343 | 346 | @Alias private static InputStream in; |
344 | 347 |
|
345 | | - /** |
346 | | - * This substitution can be removed when the warning for setting `-Djava.security.manager=allow` |
347 | | - * in the image build is converted into an error. |
348 | | - */ |
349 | | - @Substitute |
350 | | - private static boolean allowSecurityManager() { |
351 | | - if (SystemPropertiesSupport.singleton().isSecurityManagerAllowed()) { |
352 | | - /* Fail fatally in case someone tried to set the SM at build time */ |
353 | | - System.err.println("Error: Property '-Djava.security.manager' was set at build time. Security manager is not supported by native image. Please unset this property. " + |
354 | | - "Exiting the program to prevent misinterpretation of the set SecurityManager."); |
355 | | - System.exit(-1); |
356 | | - } |
357 | | - return false; |
358 | | - } |
359 | | - |
360 | 348 | @Substitute |
361 | 349 | private static void setIn(InputStream is) { |
362 | 350 | in = is; |
@@ -417,6 +405,41 @@ private static String getProperty(String key, String def) { |
417 | 405 |
|
418 | 406 | @Alias |
419 | 407 | private static native void checkKey(String key); |
| 408 | + |
| 409 | + /** |
| 410 | + * Force System.Never in case it was set at build time via the `-Djava.security.manager=allow` |
| 411 | + * passed to the image builder. |
| 412 | + */ |
| 413 | + @Alias @RecomputeFieldValue(kind = Kind.FromAlias, isFinal = true) // |
| 414 | + private static int allowSecurityManager = 1; |
| 415 | + |
| 416 | + @Substitute |
| 417 | + @TargetElement(onlyWith = JavaLangSubstitutions.UseSecurityManagerPropertyAtRuntime.class) |
| 418 | + private static void setSecurityManager(SecurityManager s) { |
| 419 | + /* We read properties interpreted at isolate creation as that is what happens on the JVM */ |
| 420 | + String smp = SystemPropertiesSupport.singleton().getSavedProperties().get("java.security.manager"); |
| 421 | + if (smp != null && !smp.equals("disallow")) { |
| 422 | + /* |
| 423 | + * The strict failure is needed as the security precaution: In case a user does not read |
| 424 | + * our documentation, uses this deprecated API marked for removal, and passes |
| 425 | + * "-Djava.security.manager=allow" at runtime, and accidentally catches the |
| 426 | + * UnsupportedOperationException, we don't want to compromise their security. |
| 427 | + */ |
| 428 | + System.err.println(""" |
| 429 | + Fatal error: Property '-Djava.security.manager' is set, but SecurityManager is not supported by Native Image. Please unset this property. |
| 430 | + Exiting the program to prevent misinterpretation of the set SecurityManager at:"""); |
| 431 | + |
| 432 | + for (var traceElement : new UnsupportedOperationException().getStackTrace()) { |
| 433 | + System.err.println("\tat " + traceElement); |
| 434 | + } |
| 435 | + |
| 436 | + /* bypasses possible filters on System.exit */ |
| 437 | + ImageSingletons.lookup(LogHandler.class).fatalError(); |
| 438 | + } |
| 439 | + |
| 440 | + throw new UnsupportedOperationException( |
| 441 | + "The Security Manager is deprecated and will be removed in a future release"); |
| 442 | + } |
420 | 443 | } |
421 | 444 |
|
422 | 445 | final class NotAArch64 implements BooleanSupplier { |
@@ -622,6 +645,9 @@ public static Enumeration<URL> findResources(String name) { |
622 | 645 | // Checkstyle: resume |
623 | 646 | } |
624 | 647 |
|
| 648 | +/** |
| 649 | + * This substitution should not be needed: GR-49983. |
| 650 | + */ |
625 | 651 | @TargetClass(value = jdk.internal.logger.LoggerFinderLoader.class) |
626 | 652 | final class Target_jdk_internal_logger_LoggerFinderLoader { |
627 | 653 | // Checkstyle: stop |
@@ -660,6 +686,18 @@ public Object transform(Object receiver, Object originalValue) { |
660 | 686 | /** Dummy class to have a class with the file's name. */ |
661 | 687 | public final class JavaLangSubstitutions { |
662 | 688 |
|
| 689 | + public static class UseSecurityManagerPropertyAtRuntime implements BooleanSupplier { |
| 690 | + public static class Options { |
| 691 | + @Option(help = "Used only for testing as exiting the program shadows other working tests, please do not use in production.")// |
| 692 | + public static final HostedOptionKey<Boolean> TestingSecurityViolationUseSecurityManagerPropertyAtRuntime = new HostedOptionKey<>(true); |
| 693 | + } |
| 694 | + |
| 695 | + @Override |
| 696 | + public boolean getAsBoolean() { |
| 697 | + return Options.TestingSecurityViolationUseSecurityManagerPropertyAtRuntime.getValue(); |
| 698 | + } |
| 699 | + } |
| 700 | + |
663 | 701 | public static final class StringUtil { |
664 | 702 | /** |
665 | 703 | * Returns a character from a string at {@code index} position based on the encoding format. |
|
0 commit comments