GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-98724: Fix Py_CLEAR() macro side effects (#99100) by vstinner · Pull Request #100070 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Dec 7, 2022 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

The Py_CLEAR(), Py_SETREF() and Py_XSETREF() macros now only evaluate their arguments once. If an argument has side effects, these side effects are no longer duplicated.

Use temporary variables to avoid duplicating side effects of macro arguments side effects. Use memcpy() for assignment to prevent a miscompilation with strict aliasing caused by type punning.

Add test_py_clear() and test_py_setref() unit tests to _testcapi.

  • Issue: Macro Py_CLEAR references argument two times. #98724

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot mentioned this pull request Dec 7, 2022
Macro Py_CLEAR references argument two times. #98724
Closed
@netlify

netlify Bot commented Dec 7, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

✅ Deploy Preview for python-cpython-preview ready!

Name Link
🔨 Latest commit cffd9d4
🔍 Latest deploy log https://app.netlify.com/sites/python-cpython-preview/deploys/639094347616260008a4e69e
😎 Deploy Preview https://deploy-preview-100070--python-cpython-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added the awaiting core review label Dec 7, 2022
@vstinner

vstinner commented Dec 7, 2022

Copy link
Copy Markdown
Member Author

cc @serge-sans-paille @erlend-aasland: This version should no longer miscompile Python: it avoids the type punning issue thanks to memcpy(). I wrote a comment in Py_CLEAR() to explain the rationale for memcpy().

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Dec 7, 2022

Copy link
Copy Markdown
Member Author

This PR no longer reintroduces the miscompilation bug: issue #99701. I tested manually.

I tested this PR with this local patch (otherwise the output is just too verbose!):

diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 8209132ebc..cf243c8ef9 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -1891,9 +1891,6 @@ Py_FinalizeEx(void)
     /* Destroy all modules */
     finalize_modules(tstate);
 
-    /* Print debug stats if any */
-    _PyEval_Fini();
-
     /* Flush sys.stdout and sys.stderr (again, in case more was printed) */
     if (flush_std_files() < 0) {
         status = -1;

Test command:

rm ~/pyprefix -rf; git clean -fdx
./configure --enable-pystats --prefix ~/pyprefix CC=gcc CFLAGS="-O3 -fstrict-aliasing"
make && make install
./python -m test test_capi test_itertools

The test succeeded with CC=gcc CFLAGS="-O3 -fstrict-aliasing" and CC=clang LD=clang CFLAGS="-O3 -fstrict-aliasing".

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Dec 7, 2022

Copy link
Copy Markdown
Member Author

I updated my PR to use __typeof__() if available to still emit compiler warnings in Py_SETREF(dst, src) if src has not the same type than dst. On Windows, the memcpy() implementation is used since __typeof__() is not available: memcpy() avoids the type punning issue, but it does not emit a compiler warning if src has the same type.

My previous attempt PR #99739 always used __typeof__(), but this function is not available on Windows with MSVC.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Dec 7, 2022

Copy link
Copy Markdown
Member Author

I ran my test for 4 cases and all succeeded:

  • GCC with __typeof__()
  • GCC with memcpy()
  • clang with __typeof__()
  • clang with memcpy()
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Dec 7, 2022

Copy link
Copy Markdown
Member Author

To also emit a warning in the memcpy() implementation, @serge-sans-paille suggested to me using sizeof(dst=src). It works because sizeof() does not evalute its argument at runtime, it only infers the argument type. MSVC emits a warning with sizeof(dst=src).

I'm not sure about this one. Maybe this enhancement can be made later, once the initial issue #98724 is fixed with my current PR implementation. So it's easy to revert if something goes wrong.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner vstinner added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Dec 7, 2022
@bedevere-bot

bedevere-bot commented Dec 7, 2022

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @vstinner for commit 86d8878 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Dec 7, 2022
@vstinner

vstinner commented Dec 7, 2022

Copy link
Copy Markdown
Member Author

buildbot/ARM64 Windows PR

test_ast.test_recursion_direct() crash with a stack overflow. It sounds unrelated to this change. The ARM64 Windows 3.x buildbot is sick for 2 days: it fails with "exception" (don't build+test Python).

test_parse_in_error (test.test_ast.ASTHelpers_Test.test_parse_in_error) ... ok
Windows fatal exception: stack overflow
Current thread 0x0000142c (most recent call first):
  File "C:\Workspace\buildarea\pull_request.linaro-win-arm64\build\Lib\test\test_ast.py", line 1252 in test_recursion_direct
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn sobolevn mentioned this pull request Dec 7, 2022
Netlify builds happen for PRs that do not change docs #100072
Closed
@vstinner
gh-98724: Fix Py_CLEAR() macro side effects (#99100)
cffd9d4
The Py_CLEAR(), Py_SETREF() and Py_XSETREF() macros now only evaluate
their arguments once. If an argument has side effects, these side
effects are no longer duplicated.

Use temporary variables to avoid duplicating side effects of macro
arguments side effects. If available, use _Py_TYPEOF() to avoid type
punning. Otherwise, use memcpy() for the assignment to prevent a
miscompilation with strict aliasing caused by type punning.

Add _Py_TYPEOF() macro: __typeof__() on GCC and clang.

Add test_py_clear() and test_py_setref() unit tests to _testcapi.
@vstinner

vstinner commented Dec 7, 2022

Copy link
Copy Markdown
Member Author

Apart of the sick "ARM64 Windows 3.x" buildbot, all buildbots were happy with this change.

I fixed a fix typos. I amended the commit message.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner merged commit b11a384 into python:main Dec 7, 2022
@vstinner
vstinner deleted the py_clear_memset branch December 7, 2022 14:22
@bedevere-bot bedevere-bot removed the awaiting core review label Dec 7, 2022
@vstinner

vstinner commented Dec 8, 2022

Copy link
Copy Markdown
Member Author

buildbot/ARM64 Windows PR: test_ast.test_recursion_direct() crash with a stack overflow

FYI I proposed #100104 to fix this bug (unrelated to this change).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@encukou encukou mentioned this pull request Feb 10, 2026
memcpy() usage without #include <string.h> in pwdmodule.c #121617
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@vstinner @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.