Skip to content

gh-137293: Ignore Exceptions when searching ELF File in Remote Debug #137309

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

uxioandrade
Copy link

@uxioandrade uxioandrade commented Aug 1, 2025

When calling search_elf_file_for_section from search_linux_map_for_section passing a file that either doesn't exist or can't be opened correctly for a variety of reasons, this will throw an exception regardless of whether that file contains PyRuntime. The call to search_linux_map_for_section can behave as expected if it does eventually open the file with PyRuntime. After discussion, this fix removes exceptions from search_linux_map_for_section

Tested manually with the following script:

import sys
import os
import time

os.chmod("/cpython/python.exe", 0x0000)
sys.remote_exec(77223, "./remote_code.py")

Before:

/cpython# ./python.exe trigger_script.py
OSError: Cannot open ELF file '/cpython/Modules/readline.cpython-315-aarch64-linux-gnu.so (deleted)' for section 'PyRuntime' search: No such file or directory

During handling of the above exception, another exception occurred:

RuntimeError: Failed to find the PyRuntime section in process 77223 on Linux platform

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/cpython/trigger_script.py", line 11, in <module>
    sys.remote_exec(77223, "./remote_code.py")
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: PyRuntime address lookup failed during debug offsets initialization

Now

/cpython# ./python.exe trigger_script.py
RuntimeError: Failed to find the PyRuntime section in process 77223 on Linux platform

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/cpython/trigger_script.py", line 11, in <module>
    sys.remote_exec(77223, "./remote_code.py")
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: PyRuntime address lookup failed during debug offsets initialization

@uxioandrade uxioandrade requested a review from pablogsal as a code owner August 1, 2025 16:17
@python-cla-bot
Copy link

python-cla-bot bot commented Aug 1, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Aug 1, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a news entry, and if possible, a test case. We can probably test this by running remote_exec on a file with some weird permissions?

@@ -732,6 +732,9 @@ search_linux_map_for_section(proc_handle_t *handle, const char* secname, const c
if (retval) {
break;
}
if (PyErr_Occurred()){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my understanding, this should always be true at this point. If search_elf_file_for_section returns 0, there's an exception set.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be overly defensive but could there be no exceptions in case section is null here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have argued that was its own issue, but per discussion it seems we should just remove exceptions from search_elf_file_for_section entirely. It's not called anywhere else.

@pablogsal
Copy link
Member

I don't think we should print the exception. For example there are some normal conditions where we will find the target but we will have some exceptions while reading unrelated files and other cases and we don't want to confuse the users showing that error.

What do you think @godlygeek ?

@godlygeek
Copy link
Contributor

I'm inclined to agree. Any problems encountered at this point aren't necessarily fatal, and there's no reason to ever report anything to the user if we go on to succeed.

@uxioandrade uxioandrade changed the title gh-137293: Print Exceptions in searching ELF File in Remote Debug gh-137293: Ignore Exceptions in searching ELF File in Remote Debug Aug 1, 2025
@uxioandrade uxioandrade changed the title gh-137293: Ignore Exceptions in searching ELF File in Remote Debug gh-137293: Ignore Exceptions when searching ELF File in Remote Debug Aug 1, 2025
@uxioandrade
Copy link
Author

Please add a news entry, and if possible, a test case. We can probably test this by running remote_exec on a file with some weird permissions?

I might be off here but the problem with creating a test case for this is that when running remote_exec with a dummy file, the first ELF file at the top of the /proc//maps file will always be the one containing PyRuntime so the other file with weird permissions is never reached

@pablogsal
Copy link
Member

Please add a news entry, and if possible, a test case. We can probably test this by running remote_exec on a file with some weird permissions?

I might be off here but the problem with creating a test case for this is that when running remote_exec with a dummy file, the first ELF file at the top of the /proc//maps file will always be the one containing PyRuntime so the other file with weird permissions is never reached

You can try with a build with --enable-shares but don't worry about a test case, it's going to be too complex. You can manually test it though by forcing a failure by hand and confirming it works

@ZeroIntensity
Copy link
Member

Ok, sure, no test is fine.

I don't think search_elf_file_for_section should be setting exceptions at all, right? If we're only going to clear it, it'd be better to omit them entirely.

@uxioandrade
Copy link
Author

Ok, sure, no test is fine.

I don't think search_elf_file_for_section should be setting exceptions at all, right? If we're only going to clear it, it'd be better to omit them entirely.

Yeah that makes a lot of sense, I just removed the exceptions from search_elf_file_for_section

…e-137293.4x3JbV.rst

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
@uxioandrade
Copy link
Author

Please add a news entry, and if possible, a test case. We can probably test this by running remote_exec on a file with some weird permissions?

I might be off here but the problem with creating a test case for this is that when running remote_exec with a dummy file, the first ELF file at the top of the /proc//maps file will always be the one containing PyRuntime so the other file with weird permissions is never reached

You can try with a build with --enable-shares but don't worry about a test case, it's going to be too complex. You can manually test it though by forcing a failure by hand and confirming it works

Sweet, that sounds good, I just added my manual test details in the description

@serhiy-storchaka
Copy link
Member

I do not think that silencing all exceptions indiscriminately is a good idea. This may remove useful information.

What if just add PyErr_Clear() before calling search_elf_file_for_section() (or at the beginning of it)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants