as C and C++, provide a lot of freedom and flexibility in memory management while relying heavily on the programmer (後略) NSA recommends using a memory safe language when possible. Examples of memory safe language include Python®, Java®, C#, Go, Delphi/Object Pascal, Swift®, Ruby , Rust®, and Ada. (中略) (中略) 18
with a memory safe language, memory management is not entirely memory safe. Most memory safe languages recognize that software sometimes needs to perform an unsafe memory management function to accomplish certain tasks. メモリ安全な言語を使えばメモリ管理が安全になるとは限らない https://media.defense.gov/2022/Nov/10/2003112742/-1/-1/0/CSI_SOFTWARE_MEMORY_SAFETY.PDF 22 Javaでは、JNI/FFM/sun.misc.Unsafeなどが該当
int x = 0; public int value() { return x; } public void increment() { x += 2; } public void decrement() { x -= 2; } } Invariant: ‘x’ の値は常に偶数である カプセル化(encapsulation)により実現 Integrity Invariantではない 26
languages require anything memory unsafe to be explicitly annotated as such to make the programmer and any reviewers of the program aware that it is unsafe. https://media.defense.gov/2022/Nov/10/2003112742/-1/-1/0/CSI_SOFTWARE_MEMORY_SAFETY.PDF NSAガイドライン 危険機能を使う場合は使っていることがわかるようにする Java: Integrity by Default 35
: Integrity by Default https://openjdk.org/jeps/8305968 デフォルトで危険機能を使えない、 あるいは、使った場合に警告がでる 関連するJEP JEP 261: Module System JEP 260: Encapsulate Most Internal APIs JEP 396: Strongly Encapsulate JDK Internals by Default JEP 403: Strongly Encapsulate JDK Internals JEP 451: Prepare to Disallow the Dynamic Loading of Agents JEP 498: Warn upon Use of Memory-Access Methods in sun.misc.Unsafe JEP 471: Deprecate the Memory-Access Methods in sun.misc.Unsafe for Removal JEP 472: Prepare to Restrict the Use of JNI JEP 500: Prepare to Make Final Mean Final 36
FFMでの注意喚起 JNI FFM WARNING: A restricted method in java.lang.System has been called WARNING: java.lang.System::loadLibrary has been called by JNI in an unnamed module (file:/tmp/) WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module WARNING: Restricted methods will be blocked in a future release unless native access is enabled WARNING: A restricted method in java.lang.foreign.MemorySegment has been called WARNING: java.lang.foreign.MemorySegment::reinterpret has been called by FFM in an unnamed module (file:/tmp/) WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module WARNING: Restricted methods will be blocked in a future release unless native access is enabled 37
public class Foo { final int bar; Foo foo = ・・・ if (foo.bar == n) { ・・・ tree shaking public class Foo { private void unused(); 現在はJNI/Unsafe/deep reflectionの可能性により最適化不可 38