Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory.getBytes in Python script #858

Closed
Youyou404 opened this issue Jul 30, 2019 · 4 comments
Closed

Memory.getBytes in Python script #858

Youyou404 opened this issue Jul 30, 2019 · 4 comments
Assignees
Labels
Type: Question Further information is requested

Comments

@Youyou404
Copy link

Hello everyone,

I think I have a bug or an understanding issue here. Before filling a bug report I just want to confirm the issue. So a simplified version of the "problem" is the following :

Environment :
Computer : Linux 5.1.2-arch1-1-ARCH #1 SMP PREEMPT x86_64 GNU/Linux
Java version : 11.0.3
Ghidra version : 9.0.2

What I want to achieve :
Displaying bytes of an analyzed program in my console

What I am currently doing :
I wrote a simple Python script which get the first address of the program.
After that, I am displaying the first byte by calling getByte : everything is fine.
But then, I want to grab 4 bytes with getBytes : the problem is here.
As an argument, I am giving getBytes the address and a byte array.
But when I am displaying this byte array, only four '0' are printed.
The return value of getBytes is '4' so this is indicating that '4' bytes have been taken.

What is the expected result:
I want my 4 bytes array to contains the actual 4 bytes of my program starting at the given address.

What I tried :
I tried rewriting the same script but in Java. And even if I have a formatting issue with a byte showing a wrong value (0xffffffa8 instead of 0xa8), I achieve to get my 4 bytes with the getBytes method.

I give you the two used scripts in an attached tar.gz file if you want to have a precise example of what I am trying to achieve.

Feel free to ask me for clarification if you need.

Have a good day.
testGithub.tar.gz

@Youyou404 Youyou404 added the Type: Question Further information is requested label Jul 30, 2019
@ryanmkurtz
Copy link
Collaborator

This is an issue with the differences between how Python and Java implement arrays. Our Python environment is Jython, which allows you to easily call into our Java API. When the Java API calls for an array, you have to give it a jarray for things to work like you want it to.

@ryanmkurtz ryanmkurtz self-assigned this Jul 30, 2019
@Youyou404
Copy link
Author

Ok I see the difference here : it is not Python, it is Jython.
So at the beginning my bytes array was the following :
select_4bytes = bytearray ( b'\x00\x00\x00\x00' )
Now with the following declaration :
select_4bytes = array.array ( 'b', '\x00\x00\x00\x00' )
Everything is fine.
Thank you for this fast and precise answer.
I'm closing the thread.

@HermanTam
Copy link

HermanTam commented Dec 16, 2023

However, when trying to import jarray, it returns "import jarray
ModuleNotFoundError: No module named 'jarray'". I cannot find the jarray package to install.

import jarray
bytes = jarray.array(bytes_read, "b")

Could someone please let me know what I am doing wrong?

bytes_read = bytearray(bb.size)
I also try initializing bytes_read = array.array("b", bytes_read) and pass the bytes_read array to getBytes method. It doesn't populate the byte array. bytes_read remains the same before and after calling the method.
I don't think array.array() uses the jarray as its first argument is typecode where b is for signed characters instead of byte. Its second argument is the sequence. It is the other way around for jarray where b corresponds to byte for its second argument.

@ryanmkurtz
Copy link
Collaborator

@HermanTam This issue for for Jython. I believe you are using Ghidrathon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants