简体   繁体   English

在java中使用Unsafe写入/读取内存

[英]Write/read memory using Unsafe in java

I know it is possible to write and read directly to memory by using Unsafe class within the jvm. 我知道可以通过在jvm中使用Unsafe类直接写入和读取内存。

Apart from this being really unsafe and somehow counterproductive, I was instead wondering who/what is taking charge of checking the boundaries of the allowed memory location. 除了这是非常不安全和某种方式适得其反,我反而想知道谁/什么负责检查允许的内存位置的边界。 And also if there's some mechanism that safeguard the already allocated memory to be overwritten. 如果有一些机制可以保护已经分配的内存被覆盖。 Related to this latest question, what possible damage can one does by overwriting what he shouldn't ? 与这个最新的问题相关,可以通过覆盖他不应该做的事情来造成什么损害? Well, this could be best reformed as: what type of memory is the one that can be written, or what kind of information are already stored in there ? 好吧,这可以最好改写为:可以写入的内存类型是什么类型,或者已经存储了哪种类型的信息?

Thanks 谢谢

The underlying operating system will ensure that you do not write outside of the memory allocated to your application (which it can do easily, due to Virtual Memory ). 底层操作系统将确保您不会在分配给应用程序的内存之外写入(由于虚拟内存 ,它可以轻松地执行)。 If you do, you will get a segmentation fault or similar error In addition to that, the JVM might do its own bound checking. 如果这样做,您将收到分段错误或类似错误除此之外,JVM可能会执行自己的绑定检查。 You'll definitely not be allowed to write in any sensible (ie other applications) memory, and I doubt the JVM will allow you to overwrite internal structures, but I might be wrong there. 你肯定不会被允许写入任何明智的(即其他应用程序)内存,我怀疑JVM会允许你覆盖内部结构,但我可能在那里错了。 So to summarize the answer to your question: The possible damage to overwriting where you shouldn't is in the best case your application malfunctioning and in the worst case the JVM crashing. 因此,总结一下您的问题的答案:在您应用程序出现故障并且在最坏的情况下JVM崩溃的最佳情况下,覆盖您可能不会受到的损坏。

In regards to safeguards, not using Unsafe would be a pretty good one... Other than that, you can of course write your own memory allocator inside the memory allocated by Unsafe and flag some areas so your application code does not write into areas it shouldn't. 关于安全措施,不使用Unsafe会是一个非常好的...除此之外,您当然可以在Unsafe分配的内存中编写自己的内存分配器并标记一些区域,以便您的应用程序代码不会写入区域不应该。

So the answer to your last question would be that if the JVM allows you to access memory outside of the allocated unsafe regions, then the information and type of memory you can access is any of your objects as well as any internal JVM structure. 因此,对于上一个问题的答案是,如果JVM允许您访问分配的不安全区域之外的内存,那么您可以访问的内存信息和类型是您的任何对象以及任何内部JVM结构。 Obviously changing those would wreak havoc, so it's likely that attempting to do so would result in a crash. 显然,改变这些会造成严重破坏,因此尝试这样做可能会导致崩溃。

By using the Unsafe class, you are taking charge of checking the boundaries of the allowed memory locations. 通过使用Unsafe类, 负责检查允许的内存位置的边界。

You shouldn't assume that there'll be some mechanism that will prevent you from corrupting parts of your process's memory that you're not supposed to touch. 你不应该假设有一些机制会阻止你破坏你不应该接触的进程内存部分。

The comment that accompanies Unsafe.getUnsafe() is pretty clear: Unsafe.getUnsafe()附带的注释非常清楚:

[the Unsafe instance] can be used to read and write data at arbitrary memory addresses. [ Unsafe实例]可用于在任意内存地址读取和写入数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM