Skip to content

MSVC emits warnings in non-debug builds #131288

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

Closed
chris-eibl opened this issue Mar 15, 2025 · 0 comments
Closed

MSVC emits warnings in non-debug builds #131288

chris-eibl opened this issue Mar 15, 2025 · 0 comments
Labels
build The build process and cross-build interpreter-core (Objects, Python, Grammar, and Parser dirs) OS-windows type-bug An unexpected behavior, bug, or error

Comments

@chris-eibl
Copy link
Member

chris-eibl commented Mar 15, 2025

Since #130398, the below warning is emitted several times in case of MSVC release (or PGO) builds:

Include\internal\pycore_ceval.h(209): warning C4172: returning address of local variable or temporary : here

So the comment in the code

static inline uintptr_t
_Py_get_machine_stack_pointer(void) {
#if _Py__has_builtin(__builtin_frame_address)
return (uintptr_t)__builtin_frame_address(0);
#else
char here;
/* Avoid compiler warning about returning stack address */
return return_pointer_as_int(&here);
#endif
}

only works for debug builds. This is because in case of optimizing, MSVC is inlining return_pointer_as_int

#if !_Py__has_builtin(__builtin_frame_address)
static uintptr_t return_pointer_as_int(char* p) {
return (uintptr_t)p;
}
#endif

and then is "smart" enough to realize and warn about it.

See here https://godbolt.org/z/ooK5Poxo3 (btw gcc or clang won't do that https://godbolt.org/z/459f6hj9G).

__declspec(noinline) would help, but IMHO is an unneeded performance penalty.

I suggest to use #pragma warning(disable:4172) to silence the warning.
Since this is a code generation warning (4700-4999), we have to guard the whole
_Py_get_machine_stack_pointer and cannot just place it around line 209.

@colesbury had a much better idea: use _AddressOfReturnAddress which even gets rid of UB: see https://godbolt.org/z/7c9Gq9jzM and https://github.com/llvm/llvm-project/blob/llvmorg-10.0.0-rc1/clang/lib/Basic/Stack.cpp#L24.

Linked PRs

@picnixz picnixz added OS-windows interpreter-core (Objects, Python, Grammar, and Parser dirs) build The build process and cross-build type-bug An unexpected behavior, bug, or error labels Mar 15, 2025
colesbury pushed a commit that referenced this issue Apr 4, 2025
…h-131289)

Use `_AddressOfReturnAddress` in `_Py_get_machine_stack_pointer` to silence MSVC warning in pycore_ceval.h for release builds.
seehwan pushed a commit to seehwan/cpython that referenced this issue Apr 16, 2025
…al.h (pythongh-131289)

Use `_AddressOfReturnAddress` in `_Py_get_machine_stack_pointer` to silence MSVC warning in pycore_ceval.h for release builds.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build The build process and cross-build interpreter-core (Objects, Python, Grammar, and Parser dirs) OS-windows type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants