ScopedValue cache intrinsic can reuse a previous carrier's handle after virtual-thread suspension
Describe the issue
Graal's Thread.scopedValueCache() intrinsic reads JavaThread::_scopedValueCache through a location currently marked immutable. The resulting OopHandle address can be reused across a call that unmounts a virtual thread.
If the virtual thread resumes on another carrier, compiled code can dereference the previous carrier's handle and ScopedValue.get() can return a binding belonging to another worker.
Steps to reproduce the issue
- Apply the attached test-only patch for
ScopedValueCacheTest. - Build the compiler test image:
cd compiler mx build --dependencies GRAAL_TEST,GRAALJDK_CE_1602C36D2E_JAVA25 --build-logs=silent --no-daemon - Run the regression with Graal as the JIT:
mx unittest --verbose \ -XX:+UseJVMCICompiler \ -Djdk.virtualThreadScheduler.parallelism=2 \ ScopedValueCacheTest VirtualThreadCarrierThreadTest
On the affected compiler the virtual-thread test fails with values from another worker, for example:
expected:<-32> but was:<-60032>The IR checks also show that two scoped-value-cache accesses separated by a call are reduced to one load of the carrier-relative handle.
Describe GraalVM and your environment:
- GraalVM version or commit id: GraalVM 25.3.4.1 / compiler commit
7b025988a922a73286d1326e1eddc1ca39d3f569 - JDK major version: 25 (
25.0.4.1+1-LTS-jvmci-25.3-b22) - OS: Linux
- Architecture: AMD64
More details
JAVA_THREAD_SCOPED_VALUE_CACHE_LOCATION is currently created with immutable=true. The handle is stable for one carrier's JavaThread, but not for a virtual thread across unmount/remount.
Before the fix, generated code keeps the handle address across a call. Marking this location mutable makes the post-call access reload it from the current JavaThread; the regression then passes.
Source: oracle/graal