sun.misc.Unsafe memory-access deprecation (JEP 471): JDK 24+ warns, and future deny-by-default JDKs will break ndarray raw buffers at runtime instead of falling back
まだ誰も着手していません。
- 主要言語
- Java
- スター
- 928
- フォーク
- 227
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
Investigated and reported by AI, reviewed by me.
Summary
sun.misc.Unsafe memory-access methods are terminally deprecated (JEP 471, JDK 23) and JDK 24+ emits a runtime warning on first use (JEP 498). ndarray's raw buffer implementation triggers this warning today, and — more importantly — the way UnsafeReference detects Unsafe availability means that when a future JDK flips the default from warn to deny (planned for JDK 26 or later, see the OpenJDK integrity-by-default roadmap), raw buffers will fail at runtime instead of falling back to the non-Unsafe buffer path.
Current warning (JDK 24/25)
Seen with tensorflow-core-api / ndarray 1.2.0 on JDK 25:
WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::arrayBaseOffset has been called by org.tensorflow.ndarray.impl.buffer.raw.UnsafeMemoryHandle (file:.../tensorflow-ndarray-1.2.0.jar)
WARNING: Please consider reporting this to the maintainers of class org.tensorflow.ndarray.impl.buffer.raw.UnsafeMemoryHandle
WARNING: sun.misc.Unsafe::arrayBaseOffset will be removed in a future release
The latent problem: availability check won't detect deny mode
UnsafeReference's static initializer validates the Unsafe methods only by reflective existence check (Class.getDeclaredMethod(...)) — it never actually invokes one:
Under --sun-misc-unsafe-memory-access=deny (the future default), the methods still exist — they just throw UnsupportedOperationException when invoked. So:
UnsafeReference.isAvailable()returnstrueRawDataBufferFactory.canBeUsed()returnstrue, so callers take the raw-buffer path- The first real operation (e.g.
UnsafeMemoryHandle.fromArray→Unsafe.arrayIndexScale/arrayBaseOffset, or anygetByte/putFloat/copyMemory) throwsUnsupportedOperationExceptionat runtime
i.e. instead of degrading gracefully to the heap/nio buffer implementations, applications will start crashing on the first tensor buffer operation once they run on a deny-by-default JDK.
Suggested fixes
- Short term (small change): in
UnsafeReference's static initializer, probe by invocation rather than existence — e.g. callunsafe.arrayBaseOffset(byte[].class)inside the existingtryand also catchUnsupportedOperationException. Thendenymode cleanly disables raw buffers and the existing fallback path takes over. - Long term: migrate the raw buffer implementation to
VarHandleand/or the Foreign Function & Memory API (java.lang.foreign.MemorySegment), which are the JEP-sanctioned replacements. That also eliminates the startup warning entirely. Once the Unsafe methods are removed (final phase of JEP 471), no JVM flag will keep the current code working.
Workaround for users (for anyone landing here)
- JDK 24/25:
--sun-misc-unsafe-memory-access=allowsilences the warning. - Deny-by-default JDKs:
--sun-misc-unsafe-memory-access=warnkeeps raw buffers functional (theallowvalue is no longer accepted in that phase).
Environment
org.tensorflow:tensorflow-core-api:1.2.0/tensorflow-ndarray:1.2.0- JDK 25 (warning), any JDK ≥ 24 warns; behavior controlled by
--sun-misc-unsafe-memory-access
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
tensorflow-ndarray/src/main/java/org/tensorflow/ndarray/impl/buffer/raw/UnsafeReference.java から始めて、その静的な利用可能性チェックを調査し、次に RawDataBufferFactory.canBeUsed() を UnsafeMemoryHandle のエントリポイントまでたどります。deny mode での動作を検証し、raw バッファが無効化されて、実行時に失敗するのではなく既存の heap/NIO フォールバックが選択されることを確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- java
- 領域
- backend
- issue の種類
- バグ
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 65/100