6

I have a program loaded in the memory. Now I want to access the memory directly and change the OPCODE and DATA in the memory for that program. For this I need to write a Java program.

Can you please tell me if this is feasible? If yes, please let me know how to write such a program.

Thanks in advance!

kajarigd
  • 1,299
  • 3
  • 28
  • 46

2 Answers2

6

Java is not designed for this.
The main aim of Java is to let the JVM manage the memory for you. Thus, your programs are sandboxed.

However, there seems to be a backdoor in HotSpot JVM:

Java was initially designed as a safe, managed environment. Nevertheless, Java HotSpot VM contains a “backdoor” that provides a number of low-level operations to manipulate memory and threads directly. This backdoor – sun.misc.Unsafe – is widely used by JDK itself in the packages like java.nio or java.util.concurrent. It is hard to imagine a Java developer who uses this backdoor in any regular development because this API is extremely dangerous, non portable, and volatile. Nevertheless, Unsafe provides an easy way to look into HotSpot JVM internals and do some tricks. Sometimes it is simply funny, sometimes it can be used to study VM internals without C++ code debugging, sometimes it can be leveraged for profiling and development tools.

Source: http://highlyscalable.wordpress.com/2012/02/02/direct-memory-access-in-java/

The Unsafe class is, however, undocumented. You may want to have a look at this SO answer for more details: https://stackoverflow.com/questions/5574241/interesting-uses-of-sun-misc-unsafe

Unoffical Docs: http://mishadoff.github.io/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/
Absolute Beginners' Guide http://java-performance.info/string-packing-converting-characters-to-bytes/
http://javapapers.com/core-java/address-of-a-java-object/

P.S. I am aware that I must post some of the content of the link here but since the articles are really very detailed, I have skipped that part

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • 1
    Thanks for your reply. I have looked into the above articles on unSafe. What I understood is that, it can be used to modify variable values and not access and change at the OPCODE level. Please let me know if I am on the right track? Thanks. – kajarigd Oct 17 '13 at 05:40
  • @kajarigd It would be misleading to say that I know. I have never used `Unsafe` myself as I am still a novice. Hopefully, some experienced Stacker will be able to help you better =) – An SO User Oct 17 '13 at 05:41
  • Here is some more StackOver **load** : http://javapapers.com/core-java/address-of-a-java-object/ – An SO User Oct 17 '13 at 05:44
  • if you need more overload, let me know. I will be happy to do that =D – An SO User Oct 17 '13 at 05:47
  • 1
    When a program is loaded in the memory, we have it in the OPCODE, DATA format. I want to access and modify the OPCODE. Is that possible? – kajarigd Oct 17 '13 at 05:59
  • Dear customer, your question is valuable to us. Please stay on the line and one of our experts will attend to your call shortly. Please hold. =D – An SO User Oct 17 '13 at 06:01
0

You cannot directly reference memory in java, as their is no concept of pointers in java like c/c++

You must go through this referencing memory address

Hope it helps.

Community
  • 1
  • 1
Ashish
  • 735
  • 1
  • 6
  • 15
  • Thanks for your reply. I would like to know if with unSafe I can access any part of memory, or just the variable values? When a program is loaded in the memory, we have it in the OPCODE, DATA format. I want to access and modify the OPCODE. Is that possible? – kajarigd Oct 17 '13 at 05:58
  • I think your first question should be. can you modify any address location with java directly ? Sorry i have never tried it. probably you have to search it by yourself. share it if you get any solution. – Ashish Oct 17 '13 at 06:09