开发者

Read memory from own process in Java

开发者 https://www.devze.com 2023-02-21 00:53 出处:网络
I\'m current attending a course where we have to write an AI to play battleships, and we managed to put out a great working one, but our teacher is a smartass and I\'d like to make a cheating AI, that

I'm current attending a course where we have to write an AI to play battleships, and we managed to put out a great working one, but our teacher is a smartass and I'd like to make a cheating AI, that reads the memory and looks where the opponent AI has开发者_如何学JAVA placed the ships.

The UI is running in a separate thread, where it runs an observer pattern on the logic in the main thread. The positions of the ships is stored in a binary two-dimensional array where true represents a point on a ship (not which, just any ship).

Now the question is: Is it possible to read the memory of the two-dimensional array of enemyBoard somehow, when it is running in the same process and in the same thread?


If it's in the same process and your classes have any kind of link to the driver (and thus indirectly to the other array), you could obtain it using only the reflection API.


One way of doing this would be to call out to a piece of native C/C++ code that uses the JNI interface to copy the array contents from the heap. JNI offers a number of methods for reading/copying and manipulating objects on the heap. The official documentation is a good place to start.


You can have direct access to the memory only if you dive to the native code. So, I suppose, the only way to do that is a JNI call.


In Java you don't have raw access to the memory in the same way you do in C/C++. So you could try and use JNI to get at the raw memory.

Another option may be to use reflection. If your code has a reference to whatever object has the opponents positions then you can access it's fields, even the private ones.

0

精彩评论

暂无评论...
验证码 换一张
取 消