Skip to content

Conversation

graalvmbot
Copy link
Collaborator

Before this change the function System#setSecurityManager was always halting program execution with a VM error. This was inconvenient as the VM error prints an uncomprehensible error message and prevents further continuation of the program. For cases where the program is expected to throw an exception when System#setSecurityManager is called, execution on Native Image was not possible.

Now, System#setSecurityManager throws an java.lang.UnsupportedOperationException by default. If the property java.security.manager is set to allow the program will exit with code 99 by printing a user-readable stack trace.

After this change:

$ java HelloWorld
Exception in thread "main" java.lang.UnsupportedOperationException: The Security Manager is deprecated and will be removed in a future release
	at java.base/java.lang.System.setSecurityManager(System.java:429)
	at HelloWorld.main(HelloWorld.java:4)
$ ./helloworld
Exception in thread "main" java.lang.UnsupportedOperationException: The Security Manager is deprecated and will be removed in a future release
	at java.base@21.0.1/java.lang.System.setSecurityManager(System.java:423)
	at HelloWorld.main(HelloWorld.java:4)
	at java.base@21.0.1/java.lang.invoke.LambdaForm$DMH/sa346b79c.invokeStaticInit(LambdaForm$DMH)
$ ./helloworld -Djava.security.manager=allow
Fatal error: Property '-Djava.security.manager' is set, but SecurityManager is not supported by Native Image. Please unset this property.
Exiting the program to prevent misinterpretation of the set SecurityManager at:
	at java.base@21.0.1/java.lang.System.setSecurityManager(System.java:432)
	at HelloWorld.main(HelloWorld.java:5)
	at java.base@21.0.1/java.lang.invoke.LambdaForm$DMH/sa346b79c.invokeStaticInit(LambdaForm$DMH)
$ echo $status
99

Before this PR:

$ ./helloworld
Fatal error: Installing a SecurityManager is not yet supported

Printing instructions (ip=0x0000558e2b586fae):
  0x0000558e2b586eae: 0x28 0x00 0x48 0x89 0x4c 0x24 0x08 0x48 0x89 0x44 0x24 0x10 0xe8 0x01 0x0f 0xff
  0x0000558e2b586ebe: 0xff 0x90 0x41 0xc7 0x87 0xd4 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0xf0 0x83 0x04
  0x0000558e2b586ece: 0x24 0x00 0x49 0xc7 0x47 0x08 0x01 0x00 0x00 0x00 0x41 0xc7 0x87 0xd0 0x00 0x00
  0x0000558e2b586ede: 0x00 0xfe 0xfe 0xfe 0x7e 0x48 0x8b 0x7c 0x24 0x10 0x48 0x8b 0x74 0x24 0x08 0x49
  0x0000558e2b586eee: 0x8b 0xd6 0xe8 0x7b 0xe5 0xf5 0xff 0x90 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc
  0x0000558e2b586efe: 0xcc 0xcc 0x48 0x83 0xec 0x18 0x48 0x8b 0x44 0x24 0x18 0x49 0x8d 0x8e 0xf0 0xec
  0x0000558e2b586f0e: 0x28 0x00 0x48 0x89 0x4c 0x24 0x08 0x48 0x89 0x44 0x24 0x10 0xe8 0xa1 0x0e 0xff
  0x0000558e2b586f1e: 0xff 0x90 0x41 0xc7 0x87 0xd4 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0xf0 0x83 0x04
  0x0000558e2b586f2e: 0x24 0x00 0x49 0xc7 0x47 0x08 0x01 0x00 0x00 0x00 0x41 0xc7 0x87 0xd0 0x00 0x00
  0x0000558e2b586f3e: 0x00 0xfe 0xfe 0xfe 0x7e 0x48 0x8b 0x7c 0x24 0x10 0x48 0x8b 0x74 0x24 0x08 0x49
  0x0000558e2b586f4e: 0x8b 0xd6 0xe8 0x1b 0xe5 0xf5 0xff 0x90 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc
  0x0000558e2b586f5e: 0xcc 0xcc 0x48 0x83 0xec 0x18 0x48 0x8b 0x44 0x24 0x18 0x48 0x89 0x7c 0x24 0x08
  0x0000558e2b586f6e: 0x48 0x89 0x44 0x24 0x10 0xe8 0x48 0x0e 0xff 0xff 0x90 0x41 0xc7 0x87 0xd4 0x00
  0x0000558e2b586f7e: 0x00 0x00 0x01 0x00 0x00 0x00 0xf0 0x83 0x04 0x24 0x00 0x49 0xc7 0x47 0x08 0x01
  0x0000558e2b586f8e: 0x00 0x00 0x00 0x41 0xc7 0x87 0xd0 0x00 0x00 0x00 0xfe 0xfe 0xfe 0x7e 0x48 0x8b
  0x0000558e2b586f9e: 0x7c 0x24 0x10 0x48 0x8b 0x74 0x24 0x08 0x49 0x8b 0xd6 0xe8 0xc2 0xe4 0xf5 0xff
> 0x0000558e2b586fae: 0x90 0xcc 0x48 0x83 0xec 0x18 0x48 0x8b 0x44 0x24 0x18 0x49 0x8d 0x8e 0x08 0xed
  0x0000558e2b586fbe: 0x28 0x00 0x48 0x89 0x4c 0x24 0x08 0x48 0x89 0x44 0x24 0x10 0xe8 0xf1 0x0d 0xff
  0x0000558e2b586fce: 0xff 0x90 0x41 0xc7 0x87 0xd4 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0xf0 0x83 0x04
  0x0000558e2b586fde: 0x24 0x00 0x49 0xc7 0x47 0x08 0x01 0x00 0x00 0x00 0x41 0xc7 0x87 0xd0 0x00 0x00
  0x0000558e2b586fee: 0x00 0xfe 0xfe 0xfe 0x7e 0x48 0x8b 0x7c 0x24 0x10 0x48 0x8b 0x74 0x24 0x08 0x49
  0x0000558e2b586ffe: 0x8b 0xd6 0xe8 0x6b 0xe4 0xf5 0xff 0x90 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc
  0x0000558e2b58700e: 0xcc 0xcc 0x48 0x8d 0x5c 0x24 0xe8 0x49 0x3b 0x5f 0x08 0x0f 0x86 0x91 0xf5 0xf3
  0x0000558e2b58701e: 0xff 0x48 0x8b 0xe3 0x49 0x3b 0xf6 0x0f 0x84 0x17 0x00 0x00 0x00 0x8b 0x06 0xc1
  0x0000558e2b58702e: 0xe8 0x05 0x41 0x0f 0xb7 0x44 0xc6 0x0c 0x8d 0x40 0xc0 0x83 0xf8 0x06 0x0f 0x82
  0x0000558e2b58703e: 0x82 0x00 0x00 0x00 0x48 0x89 0x7c 0x24 0x10 0x49 0x3b 0xf6 0x0f 0x84 0x1a 0x00
  0x0000558e2b58704e: 0x00 0x00 0x8b 0x3e 0xc1 0xef 0x05 0x41 0x0f 0xb7 0x7c 0xfe 0x0a 0x8d 0xbf 0x9f
  0x0000558e2b58705e: 0xfe 0xff 0xff 0x83 0xff 0x07 0x0f 0x82 0x6e 0x00 0x00 0x00 0x66 0x0f 0x1f 0x44
  0x0000558e2b58706e: 0x00 0x00 0x49 0x3b 0xf6 0x0f 0x84 0x6c 0x00 0x00 0x00 0x8b 0x3e 0xc1 0xef 0x05
  0x0000558e2b58707e: 0x66 0x41 0x83 0x7c 0xfe 0x0c 0x0f 0x0f 0x85 0x5a 0x00 0x00 0x00 0x8b 0x3e 0xc1
  0x0000558e2b58708e: 0xef 0x05 0x49 0x8b 0x84 0xfe 0xb0 0x00 0x00 0x00 0x48 0x8b 0xfe 0xff 0xd0 0x90
  0x0000558e2b58709e: 0x48 0x8b 0x7c 0x24 0x10 0x48 0x8b 0xf0 0xe8 0x55 0x00 0x00 0x00 0x90 0x48 0x8b

Top of stack (sp=0x00007ffec8e14ad0):
  0x00007ffec8e14ab0: 0x0000558e2b527649 0x00007ffec8e149b8 0x00007f9852e44060 0x0000558e2b586fae
> 0x00007ffec8e14ad0: 0x00007f9852f0b0f0 0x00007f9852bfffe0 0x0000558e2b45d09c 0x0000558e2b45d09c
  0x00007ffec8e14af0: 0x0000000000000008 0x0000558e2c227468 0x00007f9852e44060 0x0000558e2b4664f1
  0x00007ffec8e14b10: 0x0000558e2c2270c0 0x0000558e2b48d33d 0x0000000001940000 0x0000000000000000
  0x00007ffec8e14b30: 0x0000000000000000 0x0000000000000000 0x0000558e2c227b30 0x00007ffec8e14c98
  0x00007ffec8e14b50: 0x0000000100000000 0x0000000000000001 0x00007fa052c8c000 0x0000558e2c225b48
  0x00007ffec8e14b70: 0x00007ffec8e14ca8 0x0000000000000000 0x00007ffec8e14c98 0x00007fa052a280d0
  0x00007ffec8e14b90: 0x00007ffec8e14c80 0x0000558e2b48d290 0x000000012b3ef040 0x00007ffec8e14c98
  0x00007ffec8e14bb0: 0x00007ffec8e14c98 0x4a8063a3d5cfe081 0x0000000000000000 0x00007ffec8e14ca8
  0x00007ffec8e14bd0: 0x0000558e2c225b48 0x00007fa052c8c000 0xb57df26142ede081 0xb5c0c6e6d4c5e081
  0x00007ffec8e14bf0: 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x00007ffec8e14c98
  0x00007ffec8e14c10: 0x00007ffec8e14c98 0xd1dc08493e3cc700 0x0000000000000000 0x00007fa052a28189
  0x00007ffec8e14c30: 0x0000558e2b48d290 0x0000558e2c225b48 0x00007fa052c8d2d0 0x0000000000000000
  0x00007ffec8e14c50: 0x0000000000000000 0x0000558e2b45c000 0x00007ffec8e14c90 0x0000000000000000
  0x00007ffec8e14c70: 0x0000000000000000 0x0000558e2b45c025 0x00007ffec8e14c88 0x00007fa052c53160
  0x00007ffec8e14c90: 0x0000000000000001 0x00007ffec8e16d93 0x0000000000000000 0x00007ffec8e16da0
  0x00007ffec8e14cb0: 0x00007ffec8e16dac 0x00007ffec8e16dd6 0x00007ffec8e16de8 0x00007ffec8e16e20

VM thread locals for the failing thread 0x0000558e2e192300:
  0: JNIThreadLocalEnvironment.jniFunctions = (bytes) 0x00007f9852cde008
  8: StackOverflowCheckImpl.stackBoundaryTL = (Word) 0x0000000000000001 (1)
  16: Safepoint.safepointRequested = (int) 0x7fffad96 (2147462550)
  20: StatusSupport.statusTL = (int) 0x00000001 (1)
  24: ThreadLocalAllocation.regularTLAB = (bytes) 
    0x0000558e2e192318: 0x00007f9853180000 0x00007f9853200000
    0x0000558e2e192328: 0x00007f98531a2738 0x0000000000000000
  56: JavaFrameAnchors.lastAnchor = (Word) 0x0000000000000000 (0)
  64: JavaThreads.currentVThreadId = (long) 0x0000000000000001 (1)
  72: PlatformThreads.currentThread = (Object) 0x00007f9852e32e88
    is an object of type java.lang.Thread
  80: SubstrateDiagnostics.threadOnlyAttachedForCrashHandler = (bytes) 0x0000000000000000
  88: ThreadLocalAllocation.allocatedBytes = (Word) 0x0000000000000000 (0)
  96: VMThreads.IsolateTL = (Word) 0x00007f9852980000 (140292197449728)
  104: VMThreads.OSThreadHandleTL = (Word) 0x00007fa052c16740 (140326559901504)
  112: VMThreads.OSThreadIdTL = (Word) 0x000000000004acb3 (306355)
  120: VMThreads.StackBase = (Word) 0x00007ffec8e15000 (140732268630016)
  128: VMThreads.StackEnd = (Word) 0x00007ffec861a000 (140732260261888)
  136: VMThreads.StartedByCurrentIsolate = (bytes) 0x0000000000000000
  144: VMThreads.nextTL = (Word) 0x0000000000000000 (0)
  152: VMThreads.unalignedIsolateThreadMemoryTL = (Word) 0x0000558e2e1922e0 (94069147116256)
  160: ExceptionUnwind.currentException = (Object) 0x0000000000000000
  164: IsolatedCompileClient.currentClient = (Object) 0x0000000000000000
  168: IsolatedCompileContext.currentContext = (Object) 0x0000000000000000
  172: JNIObjectHandles.handles = (Object) 0x00007f9853180928
    is an object of type com.oracle.svm.core.handles.ThreadLocalHandles
  176: JNIThreadLocalPendingException.pendingException = (Object) 0x0000000000000000
  180: JNIThreadLocalReferencedObjects.referencedObjectsListHead = (Object) 0x0000000000000000
  184: JNIThreadOwnedMonitors.ownedMonitors = (Object) 0x0000000000000000
  188: NoAllocationVerifier.openVerifiers = (Object) 0x0000000000000000
  192: PrivilegedStack.AccessControlContextStack = (Object) 0x00007f9853180ed0
    is an object of type java.util.ArrayDeque
  196: ThreadingSupportImpl.activeTimer = (Object) 0x0000000000000000
  200: ActionOnTransitionToJavaSupport.actionTL = (int) 0x00000000 (0)
  204: ImplicitExceptions.implicitExceptionsAreFatal = (int) 0x00000000 (0)
  208: StackOverflowCheckImpl.yellowZoneStateTL = (int) 0x7efefefe (2130640638)
  212: StatusSupport.safepointBehaviorTL = (int) 0x00000001 (1)
  216: ThreadingSupportImpl.currentPauseDepth = (int) 0x00000000 (0)
  
Java frame anchors for the failing thread 0x0000558e2e192300:
  No anchors
  
Stacktrace for the failing thread 0x0000558e2e192300 (A=AOT compiled, J=JIT compiled, D=deoptimized, i=inlined):
  i  SP 0x00007ffec8e14ad0 IP 0x0000558e2b586fae size=32    com.oracle.svm.core.jdk.VMErrorSubstitutions.shutdown(VMErrorSubstitutions.java:148)
  i  SP 0x00007ffec8e14ad0 IP 0x0000558e2b586fae size=32    com.oracle.svm.core.jdk.VMErrorSubstitutions.shouldNotReachHere(VMErrorSubstitutions.java:141)
  A  SP 0x00007ffec8e14ad0 IP 0x0000558e2b586fae size=32    com.oracle.svm.core.util.VMError.shouldNotReachHere(VMError.java:90)
  i  SP 0x00007ffec8e14af0 IP 0x0000558e2b45d09c size=32    java.lang.System.setSecurityManager(System.java:418)
  A  SP 0x00007ffec8e14af0 IP 0x0000558e2b45d09c size=32    HelloWorld.main(HelloWorld.java:4)
  i  SP 0x00007ffec8e14b10 IP 0x0000558e2b4664f1 size=16    java.lang.invoke.LambdaForm$DMH/sa346b79c.invokeStaticInit(LambdaForm$DMH)
  i  SP 0x00007ffec8e14b10 IP 0x0000558e2b4664f1 size=16    com.oracle.svm.core.JavaMainWrapper.invokeMain(JavaMainWrapper.java:180)
  A  SP 0x00007ffec8e14b10 IP 0x0000558e2b4664f1 size=16    com.oracle.svm.core.JavaMainWrapper.runCore0(JavaMainWrapper.java:233)
  i  SP 0x00007ffec8e14b20 IP 0x0000558e2b48d33d size=112   com.oracle.svm.core.JavaMainWrapper.runCore(JavaMainWrapper.java:200)
  i  SP 0x00007ffec8e14b20 IP 0x0000558e2b48d33d size=112   com.oracle.svm.core.JavaMainWrapper.doRun(JavaMainWrapper.java:289)
  i  SP 0x00007ffec8e14b20 IP 0x0000558e2b48d33d size=112   com.oracle.svm.core.JavaMainWrapper.run(JavaMainWrapper.java:275)
  A  SP 0x00007ffec8e14b20 IP 0x0000558e2b48d33d size=112   com.oracle.svm.core.code.IsolateEnterStub.JavaMainWrapper_run_5087f5482cc9a6abc971913ece43acb471d2631b(IsolateEnterStub.java:0)
  
Threads:
  0x00007f984c000b80 STATUS_IN_NATIVE (ALLOW_SAFEPOINT) "Reference Handler" - 0x00007f9852e32e10, daemon, stack(0x00007f9852181000,0x00007f9852980000)
  0x0000558e2e192300 STATUS_IN_JAVA (PREVENT_VM_FROM_REACHING_SAFEPOINT) "main" - 0x00007f9852e32e88, stack(0x00007ffec861a000,0x00007ffec8e15000)
  
No VMOperation in progress

The 30 most recent VM operation status changes:
  
VM mutexes:
  mutex "RealLog.backTracePrinterMutex" is unlocked.
  mutex "freeList" is unlocked.
  mutex "mainVMOperationControlWorkQueue" is unlocked.
  mutex "referencePendingList" is unlocked.
  mutex "thread" is unlocked.
  
Build time information:
  Version: 21.0.1+11, serial gc, compressed references
  Platform: linux/amd64
  Page size: 4096
  Container support: true
  CPU features used for AOT compiled code: CX8, CMOV, FXSR, MMX, SSE, SSE2, SSE3, SSSE3, SSE4_1, SSE4_2, POPCNT, LZCNT, AVX, AVX2, BMI1, BMI2, FMA, F16C
  
Runtime information:
  CPU cores (container): unknown
  CPU cores (OS): 20
  Memory: 63965M
  Page size: 4096
  VM uptime: 0.008s
  Current timestamp: 1699468098486
  AOT compiled code: 0x0000558e2b45d000 - 0x0000558e2bafb24f
  
OS information:
  Max threads: 510882
  Max memory mappings: 65530
  Max PID: 4194304
  
Command line: 

Heap settings and statistics:
  Supports isolates: true
  Heap base: 0x00007f9852980000
  Object reference size: 4
  Reserved object header bits: 0b11111
  Aligned chunk size: 524288
  Large array threshold: 131072
  Incremental collections: 0
  Complete collections: 0
  
Heap usage:
  Eden: 1.00M (0.00M in 0 aligned chunks, 0.00M in 0 unaligned chunks)
  Old: 0.00M (0.00M in 0 aligned chunks, 0.00M in 0 unaligned chunks)
  
Native image heap boundaries:
  ReadOnly Primitives: 0x00007f9852a00830 - 0x00007f9852bc6388
  ReadOnly References: 0x00007f9852bc6388 - 0x00007f9852cdd038
  ReadOnly Relocatables: 0x00007f9852cde000 - 0x00007f9852d4b460
  Writable Primitives: 0x00007f9852d4c000 - 0x00007f9852dfe5c8
  Writable References: 0x00007f9852dfe5c8 - 0x00007f9852f49950
  Writable Huge: 0x0000000000000000 - 0x0000000000000000
  ReadOnly Huge: 0x00007f9852f80038 - 0x00007f98531146f0
  
Heap chunks: E=eden, S=survivor, O=old, F=free; A=aligned chunk, U=unaligned chunk; T=to space
  
Fatal error: Installing a SecurityManager is not yet supported

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Nov 12, 2023
@graalvmbot graalvmbot force-pushed the vj/GR-49807-security-manager-folding branch 8 times, most recently from 2256ce9 to 5b55ebb Compare November 17, 2023 12:04
@graalvmbot graalvmbot force-pushed the vj/GR-49807-security-manager-folding branch 3 times, most recently from cd7d26f to d9f4079 Compare November 27, 2023 13:05
Before this change the function `System#setSecurityManager` was always halting program execution with a VM error. This was inconvenient as the VM error prints an uncomprehensible error message and prevents further continuation of the program. For cases where the program is expected to throw an exception when  `System#setSecurityManager` is called, execution on Native Image was not possible. Now, `System#setSecurityManager` throws an `java.lang.UnsupportedOperationException` by default. If the property `java.security.manager` is set to anything but `disallow` at program startup this function will throw a `java.lang.SecurityException` according to the Java spec.
@graalvmbot graalvmbot force-pushed the vj/GR-49807-security-manager-folding branch from d9f4079 to 0fbd3d3 Compare November 27, 2023 16:55
@graalvmbot graalvmbot closed this Nov 29, 2023
@graalvmbot graalvmbot merged commit de474a9 into master Nov 29, 2023
@graalvmbot graalvmbot deleted the vj/GR-49807-security-manager-folding branch November 29, 2023 03:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants