tensorflow / tensorflow/java

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

Ouverte Adaptée aux débutants
#653 1 commentaire 4 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
Java
Étoiles
928
Forks
227
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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:

https://github.com/tensorflow/java/blob/master/tensorflow-ndarray/src/main/java/org/tensorflow/ndarray/impl/buffer/raw/UnsafeReference.java

Under --sun-misc-unsafe-memory-access=deny (the future default), the methods still exist — they just throw UnsupportedOperationException when invoked. So:

  1. UnsafeReference.isAvailable() returns true
  2. RawDataBufferFactory.canBeUsed() returns true, so callers take the raw-buffer path
  3. The first real operation (e.g. UnsafeMemoryHandle.fromArrayUnsafe.arrayIndexScale / arrayBaseOffset, or any getByte/putFloat/copyMemory) throws UnsupportedOperationException at 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. call unsafe.arrayBaseOffset(byte[].class) inside the existing try and also catch UnsupportedOperationException. Then deny mode cleanly disables raw buffers and the existing fallback path takes over.
  • Long term: migrate the raw buffer implementation to VarHandle and/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=allow silences the warning.
  • Deny-by-default JDKs: --sun-misc-unsafe-memory-access=warn keeps raw buffers functional (the allow value 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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par tensorflow-ndarray/src/main/java/org/tensorflow/ndarray/impl/buffer/raw/UnsafeReference.java et examinez sa vérification statique de disponibilité, puis suivez RawDataBufferFactory.canBeUsed() jusqu’aux points d’entrée de UnsafeMemoryHandle. Vérifiez le comportement en deny mode et assurez-vous que les buffers raw sont désactivés afin que le fallback heap/NIO existant soit sélectionné au lieu d’échouer à l’exécution.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
java
Domaine
backend
Type d'issue
Bug
Difficulté
2/5
Temps estimé
1-3 heures
Activité
Calme
Clarté
Plutôt claire
Accessibilité débutants
65/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.