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

UnicornEmulateCommand: "python" should be "python3" #677

Closed
4 tasks done
theguy147 opened this issue Jul 22, 2021 · 7 comments · Fixed by #678
Closed
4 tasks done

UnicornEmulateCommand: "python" should be "python3" #677

theguy147 opened this issue Jul 22, 2021 · 7 comments · Fixed by #678

Comments

@theguy147
Copy link
Collaborator

  • Did you use the latest version of GEF from dev branch?
  • Is your bug specific to GEF (not GDB)? - Try to reproduce it running gdb -nx
  • Did you read the documentation first?
  • Did you check issues (including
    the closed ones) - and the PR?

Step 1: Describe your environment

  • Operating System / Distribution: Ubuntu 20.04
  • Architecture: x86_64
  • GEF version (including the Python library version) run version in GEF:
GEF: (Standalone)
SHA1(/home/theguy/.gdbinit-gef.py): 1bcadc4a2cb35354163c45a19316392f6f982108
GDB: 10.2
GDB-Python: 3.9

Step 2: Describe your problem

in UnicornEmulateCommand() gef tries to find the python executable like this which("python") whereas it would be better to search with which("python3") like done in the if __name__ == "__main__" section.

Distros like Ubuntu 20.04 e.g. don't have a python executable if only python3 is installed and other Distros that do usually just symlink python to python3. So using python3 should work on every Distro.

Steps to reproduce

  1. Use Distro where only python3 is available (like ubuntu 20.04 without python2 installed))
  2. `gdb -q ${which ls}"
  3. emu 10

Observed Results

[!] Command 'unicorn-emulate' failed to execute properly, reason: Missing file `python`

Expected results

Something like:

[+] Starting emulation: 0x55555555a7d0  →  0x55555555a7f8
[...]
@hugsy
Copy link
Owner

hugsy commented Jul 22, 2021

This is a local configuration option to each Linux, not a gef issue.
It can be fixed by creating a simple symlink or using update-alternatives. Now that Python2 has been deprecated, Python in GEF means Python3.

@hugsy hugsy added not-a-bug and removed triage labels Jul 22, 2021
@theguy147
Copy link
Collaborator Author

theguy147 commented Jul 22, 2021

Exactly my point! So why is there an inconsistency in gef.py for this?

EDIT:
To clarify by "exactly my point" I meant that python in gef means python3. So why not use it specifically (especially because gef.py is already doing that at a different place)? And yes the issue can be made a Linux configuration issue but when using a specific major python version one can be exact and as seen with the change from py2 to py3 such lines of code can cause some trouble if and when py4 becomes available (I am aware that this is probably not happening for quite some years, but still...)

@theguy147
Copy link
Collaborator Author

theguy147 commented Jul 22, 2021

Another option would be to use the recommendation from PEP394: use sys.executable instead of which("python")

When reinvoking the interpreter from a Python script, querying sys.executable to avoid hardcoded assumptions regarding the interpreter location remains the preferred approach.

@hugsy
Copy link
Owner

hugsy commented Jul 22, 2021

Another option would be to use the recommendation from PEP394: use sys.executable instead of which("python")

When reinvoking the interpreter from a Python script, querying sys.executable to avoid hardcoded assumptions regarding the interpreter location remains the preferred approach.

That's a valid point, we may prefer turn into that approach actually.

@theguy147
Copy link
Collaborator Author

theguy147 commented Jul 22, 2021

Unfortunately I just noticed that gdb behaves very oddly in regards to the used python interpreter and breaks the promise made by sys.executable (as a hack gdb changes its python path at compile-time in these lines)

The result is that sys.executable points to the location the system-wide python interpreter was at the time of compilation. With package managers like apt providing pre-compiled versions this might differ from the local python interpreter.

Therefore the PEP394 recommendation is not always possible here.

I see two possible solutions to this issue:

  • use of a little bit hacky fix as well: set sys.executable = which("python3") once at the beginning and then always use sys.executable. The hacky part is the manipulation of that value while the advantage would be to only have one place in the code where this kind of hardcoded assumption is made.
  • use the PEP recommendation all the way through but add a check at the beginning to determine if the interpreter at sys.executable can be found and provide a clear error message if that is not the case so the user can "fix" his/her Linux configuration. This solution is probably cleaner and in complete compliance with the recommendations.

@hugsy
Copy link
Owner

hugsy commented Jul 22, 2021

Actually we already defined pythonbin initialized very early during the startup process (see https://github.com/hugsy/gef/blob/dev/gef.py#L10701) which does use python3. Can you update your PR to use that value then? That'll be good enough for now

@theguy147
Copy link
Collaborator Author

ok. I did so and also changed pythonbin to PYTHONBIN to clarify that it is a constant.

@hugsy hugsy linked a pull request Jul 23, 2021 that will close this issue
5 tasks
@hugsy hugsy added this to the Release 2021.10 milestone Jul 23, 2021
@hugsy hugsy added confirmed and removed not-a-bug labels Jul 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants