Skip to content

gh-131591: Implement PEP 768 #131937

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

Merged
merged 75 commits into from
Apr 3, 2025
Merged

gh-131591: Implement PEP 768 #131937

merged 75 commits into from
Apr 3, 2025

Conversation

pablogsal
Copy link
Member

@pablogsal pablogsal commented Mar 31, 2025

pablogsal and others added 30 commits March 22, 2025 20:05
The PyRuntime section can be found in either the main executable (python.exe) or the Python DLL (pythonXY.dll, where X and Y represent the major and minor version numbers). Scan the modules of the process to locate the PyRuntime address in one of these modules.
Ensure that the caller and target processes have matching architectures
before proceeding. Attaching to a 64-bit process from a 32-bit process
will fail when using CreateToolhelp32Snapshot, and attempting to attach
to a 32-bit process from a 64-bit process may cause issues during PE
file parsing. To avoid potential errors abort the operation if the
architectures differ.
This reverts commit 746ecfc.

That commit isn't correct. It conflates the return from `IsWow64Process`
(whether the process is running under 32-bit emulation on a 64-bit
process) with whether the process is 64-bit. A 32-bit process on 32-bit
Windows would have `IsWow64Process` set our `isWow64` flag to `FALSE`,
and we'd then incorrectly return `TRUE` from `is_process64Bit`.
…_support

External debugger Windows support
This is useful because debuggers will write to this variable remotely,
and it's helpful for it to have a well known size rather than one that
could vary per platform.
Require the flags for turning this on to be set to exactly 1 to avoid
accidentally triggering remote debugging in the case of heap corruption.
Make a heap copy of the script path before using it to avoid the buffer
being overwritten while we're still using it by another debugger.
This avoids the need to reopen it by (wide character) path.
Previously it was leaked if `PyObject_AsFileDescriptor` failed.
I think this was a copy paste error.
The remote process must be a compatible version.

Explain our compatibility rules.
We can clean this up by freeing strings as soon as we're done with them,
and remove some duplicate code.
This was documented as working, but in fact we assumed that the path
would always be a unicode string. Factor the version that only accepts a
unicode string into a helper function, and have the caller call
`os.fsdecode` on the input path, which accepts either a byte string or a
unicode string and always returns a unicode string.
Previously we were returning the number of bytes, which we didn't need
and would never be different than the argument that was passed into the
function.
@zooba
Copy link
Member

zooba commented Apr 1, 2025

Working on my review now - have a few minor changes so far, so don't merge without me

@pablogsal
Copy link
Member Author

Addressed the docs review round from @zooba. @godlygeek is working on your second round

@pablogsal
Copy link
Member Author

Had to fix also some merge conflicts :(

@pablogsal pablogsal requested a review from zooba April 2, 2025 00:35
@pablogsal
Copy link
Member Author

@zooba I think we have addressed everything.

@pablogsal
Copy link
Member Author

Solved more merge conflicts

Copy link
Member

@zooba zooba left a comment

Choose a reason for hiding this comment

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

Looks fine to me. Left some comments, but up to you whether to address them now or later.

env = os.environ.copy()
env['PYTHON_DISABLE_REMOTE_DEBUG'] = '1'

_, out, err = assert_python_failure('-c', f'import os, sys; sys.remote_exec(os.getpid(), "{script}")', **env)
Copy link
Member

Choose a reason for hiding this comment

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

Random thought - should we special-case (or disallow) calling remote_exec on ourselves? It seems like it could be useful, but there's probably overheads involved that could be skipped. Alternatively, just saying "it's not for this" and blocking it also seems fine to me, though it'd complicate the tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nah, it actually requires no work whatsoever on our part, which is very cool! And we use it all the time for testing and developing since having to attach debuggers to two or even 3 processes is much much harder.

I am happy to keep it unless you feel strongly

PyExc_RuntimeError,
"Memory reading is not supported on this platform");
return -1;
Py_UNREACHABLE();
Copy link
Member

Choose a reason for hiding this comment

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

Does this do something if it is actually reached? I'd guess Py_FatalError is the only real option, since it doesn't know how to return an error code.

Copy link
Member Author

Choose a reason for hiding this comment

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

They are equivalent. This one crashes with the funny message from xkcd:

cpython/Include/pymacro.h

Lines 152 to 179 in 275056a

#if defined(RANDALL_WAS_HERE)
# define Py_UNREACHABLE() \
Py_FatalError( \
"If you're seeing this, the code is in what I thought was\n" \
"an unreachable state.\n\n" \
"I could give you advice for what to do, but honestly, why\n" \
"should you trust me? I clearly screwed this up. I'm writing\n" \
"a message that should never appear, yet I know it will\n" \
"probably appear someday.\n\n" \
"On a deep level, I know I'm not up to this task.\n" \
"I'm so sorry.\n" \
"https://xkcd.com/2200")
#elif defined(Py_DEBUG)
# define Py_UNREACHABLE() \
Py_FatalError( \
"We've reached an unreachable state. Anything is possible.\n" \
"The limits were in our heads all along. Follow your dreams.\n" \
"https://xkcd.com/2200")
#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
# define Py_UNREACHABLE() __builtin_unreachable()
#elif defined(__clang__) || defined(__INTEL_COMPILER)
# define Py_UNREACHABLE() __builtin_unreachable()
#elif defined(_MSC_VER)
# define Py_UNREACHABLE() __assume(0)
#else
# define Py_UNREACHABLE() \
Py_FatalError("Unreachable C code path reached")
#endif

Copy link
Member

Choose a reason for hiding this comment

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

I don't know about the others, but it's not runtime-safe for execution to actually reach __assume(0) - it implies genuine lack of reachability, and UB if it's reached (which probably crashes, but it's not guaranteed). Most of the time we won't get the funny messages.

It shouldn't matter in this case, because we have Windows code everywhere you've used it, but we should more clearly decide how we want to handle Py_UNREACHABLE.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is the case, we should only reach this I we really screwed up the guards

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we can blow up at compile time?

Co-authored-by: Steve Dower <steve.dower@microsoft.com>
@pablogsal pablogsal merged commit 943cc14 into python:main Apr 3, 2025
42 checks passed
@pablogsal pablogsal deleted the pep-768 branch April 3, 2025 15:20
@pablogsal
Copy link
Member Author

I'm addressed some of the last comments. We will be filing smaller PRs to improve the error messages and other smaller details raised during the review, but I think the best route is landing this PR as it's already 2k of changes that are attracting merge conflicts like crazy.

Thanks a lot everyone for the reviews and congrats to @ivonastojanovic and @godlygeek

@pablogsal
Copy link
Member Author

P.S. Congrats also means "more work is coming" 😉

@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot iOS ARM64 Simulator 3.x (tier-3) has failed when building commit 943cc14.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1380/builds/3111) and take a look at the build logs.
  4. Check if the failure is related to this commit (943cc14) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1380/builds/3111

Summary of the results of the build (if available):

Click to see traceback logs
remote: Enumerating objects: 50, done.        
remote: Counting objects:   2% (1/39)        
remote: Counting objects:   5% (2/39)        
remote: Counting objects:   7% (3/39)        
remote: Counting objects:  10% (4/39)        
remote: Counting objects:  12% (5/39)        
remote: Counting objects:  15% (6/39)        
remote: Counting objects:  17% (7/39)        
remote: Counting objects:  20% (8/39)        
remote: Counting objects:  23% (9/39)        
remote: Counting objects:  25% (10/39)        
remote: Counting objects:  28% (11/39)        
remote: Counting objects:  30% (12/39)        
remote: Counting objects:  33% (13/39)        
remote: Counting objects:  35% (14/39)        
remote: Counting objects:  38% (15/39)        
remote: Counting objects:  41% (16/39)        
remote: Counting objects:  43% (17/39)        
remote: Counting objects:  46% (18/39)        
remote: Counting objects:  48% (19/39)        
remote: Counting objects:  51% (20/39)        
remote: Counting objects:  53% (21/39)        
remote: Counting objects:  56% (22/39)        
remote: Counting objects:  58% (23/39)        
remote: Counting objects:  61% (24/39)        
remote: Counting objects:  64% (25/39)        
remote: Counting objects:  66% (26/39)        
remote: Counting objects:  69% (27/39)        
remote: Counting objects:  71% (28/39)        
remote: Counting objects:  74% (29/39)        
remote: Counting objects:  76% (30/39)        
remote: Counting objects:  79% (31/39)        
remote: Counting objects:  82% (32/39)        
remote: Counting objects:  84% (33/39)        
remote: Counting objects:  87% (34/39)        
remote: Counting objects:  89% (35/39)        
remote: Counting objects:  92% (36/39)        
remote: Counting objects:  94% (37/39)        
remote: Counting objects:  97% (38/39)        
remote: Counting objects: 100% (39/39)        
remote: Counting objects: 100% (39/39), done.        
remote: Compressing objects:   3% (1/27)        
remote: Compressing objects:   7% (2/27)        
remote: Compressing objects:  11% (3/27)        
remote: Compressing objects:  14% (4/27)        
remote: Compressing objects:  18% (5/27)        
remote: Compressing objects:  22% (6/27)        
remote: Compressing objects:  25% (7/27)        
remote: Compressing objects:  29% (8/27)        
remote: Compressing objects:  33% (9/27)        
remote: Compressing objects:  37% (10/27)        
remote: Compressing objects:  40% (11/27)        
remote: Compressing objects:  44% (12/27)        
remote: Compressing objects:  48% (13/27)        
remote: Compressing objects:  51% (14/27)        
remote: Compressing objects:  55% (15/27)        
remote: Compressing objects:  59% (16/27)        
remote: Compressing objects:  62% (17/27)        
remote: Compressing objects:  66% (18/27)        
remote: Compressing objects:  70% (19/27)        
remote: Compressing objects:  74% (20/27)        
remote: Compressing objects:  77% (21/27)        
remote: Compressing objects:  81% (22/27)        
remote: Compressing objects:  85% (23/27)        
remote: Compressing objects:  88% (24/27)        
remote: Compressing objects:  92% (25/27)        
remote: Compressing objects:  96% (26/27)        
remote: Compressing objects: 100% (27/27)        
remote: Compressing objects: 100% (27/27), done.        
remote: Total 50 (delta 12), reused 12 (delta 12), pack-reused 11 (from 2)        
From https://github.com/python/cpython
 * branch                    main       -> FETCH_HEAD
Note: switching to '943cc1431ebf4a265b79f69fa286787e77410fe9'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 943cc1431eb gh-131591: Implement PEP 768 (#131937)
Switched to and reset branch 'main'

configure: WARNING: no system libmpdecimal found; falling back to bundled libmpdecimal (deprecated and scheduled for removal in Python 3.15)
configure: WARNING: pkg-config is missing. Some dependencies may not be detected correctly.

configure: WARNING: no system libmpdecimal found; falling back to bundled libmpdecimal (deprecated and scheduled for removal in Python 3.15)
configure: WARNING: pkg-config is missing. Some dependencies may not be detected correctly.

../../Python/remote_debugging.c:30:14: fatal error: 'libproc.h' file not found
   30 | #    include <libproc.h>
      |              ^~~~~~~~~~~
1 error generated.
make: *** [Python/remote_debugging.o] Error 1

find: build: No such file or directory
find: build: No such file or directory
find: build: No such file or directory
find: build: No such file or directory
make: [clean-retain-profile] Error 1 (ignored)

@pablogsal
Copy link
Member Author

Ah, yes, the inevitable fall of the build bots. Will address this immediately

seehwan pushed a commit to seehwan/cpython that referenced this pull request Apr 16, 2025
Co-authored-by: Ivona Stojanovic <stojanovic.i@hotmail.com>
Co-authored-by: Matt Wozniski <godlygeek@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants