I'm trying to use BridJ in a way that I'm not sure whether it is supported:
I have a 64 bit application. I get a byte array containing the memory of a 32 bit application through a debugger interface.
I'd like to use BridJ to parse the content of this byte array as C datatypes. I do this by using Pointer.pointerToBytes().as(my_struct.class).get()
. This technique works in a 32 bit application. But in a 64 bit application it does not, because BridJ uses the word size of the host application (64 bit) and I want to use the word size of the byte array (32 bit).
Can that be done? Is it possible to somehow switch the word size of BridJ manually, so that BridJ uses 32 bit words, even if it is run in a 64 bit application?
Longer example of what I'm trying to do
byte[] objData = debugger.readMemory(remoteStructAddr, BridJ.sizeOf(c_struct.class));
// pointerToBytes only works as expected when objData has the same
// word size as the host system
c_struct s = Pointer.pointerToBytes(objData).as(c_struct.class).get();
int structMem = s.member();
// offsetOfField only works as expected when objData has the same
// word size as the host system
byte[] namePtr = debugger.readMemory(removePtr + StructObject.offsetOfField(new c_struct(), "name"), 4);
String name = debugger.readString(namePtr);
Some More Details
- I use JNAerator to generate the BridJ classes
- I've tried to use the JNAerator
-arch
flag, but it doesn't seem to do what I want. - I don't use a native library together with BridJ, I just use BridJ to read byte arrays that I get with the debugger API.
I solved this problem by writing my own code for reading byte arrays with C structs into Java objects. This code is written for 32 bit input, whatever word size the host application is using.
Example:
The
Bridj32
class contains the implementation of this. It takes as input classes that are annotated with BridJ annotations (Field
,Struct
,Ptr
etc) and byte arrays with data. In gives as output parsed Java objects that contain the data of from the input array.The most tricky part of
Bridj32
is that it implements the C struct packing algorithm.Code of
Bridj32
: