You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Py_UNREACHABLE() no longer behaves as a function call #82386
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2019-10-01.10:28:14.960>created_at=<Date2019-09-17.22:12:35.773>labels= ['interpreter-core', 'build', '3.9']
title='Py_UNREACHABLE() no longer behaves as a function call'updated_at=<Date2019-10-01.10:28:14.959>user='https://github.com/vstinner'
I believe that the problem is caused by the change in Py_UNREACHABLE() (3ab6147).
Before the mentioned commit Py_UNREACHABLE() was an expression, now it's a block. Py_UNREACHABLE() macro is public (see https://docs.python.org/3/c-api/intro.html#c.Py_UNREACHABLE), so this change can cause similar problems outside of CPython (i.e. that change was breaking).
sir-sigurd commented on Sep 18, 2019
sir-sigurdmannequin
Mannequin
More actions
Also quote from Py_UNREACHABLE() doc:
Use this in places where you might be tempted to put an assert(0) or abort() call.
6b51998 does exactly that, it replaces assert(0) with Py_UNREACHABLE().
Whoops :(. Fixing this is beyond my C skill. I'd rather not lose the easter egg, but if it can't be fixed properly then 3ab6147 should just be reverted.
The choice to use a macro for this when it was created and the way the macro was defined means it is an expression forever unless we want to bother forcing code cleanups for uses of it.
I have poked at this a bit and have an implementation that defines a static inline void _Py_Unreachable() in pymacro.h, see zware@d07b425
This works on at least macOS and Windows, and we also have prior art for defining static inline functions in headers in #54288. If we agree that that's an acceptable solution, I'll open a PR for that change.
animalize commented on Sep 19, 2019
animalizemannequin
Mannequin
More actions
If use static inline function, and Py_UNREACHABLE() inside an if-else branch that should return a value, compiler may emit warning: https://godbolt.org/z/YtcNSf
MSVC v19.14:
warning C4715: 'test': not all control paths return a value
clang 8.0.0:
warning: control may reach end of non-void function [-Wreturn-type]
Other compilers (gcc, icc) don't emit this warning.
I like the idea of implementing Py_UNREACHABLE() as a function. Would it make sense to even declare it as a regular function rather than a static inline function? What is the benefit of inlining here? Inlining can make the code larger, rather than a function call at the machine code level is shorter.
I wrote PR 16280: "Convert Py_UNREACHABLE() macro to a function". This change fix this issue but also enhance Py_UNREACHABLE() which now dumps the Python traceback where the bug occurs. See my example: #16280 (comment)
changed the title [-]Python no longer compiles without small integer singletons[/-][+]Py_UNREACHABLE() no longer behaves as a function call[/+]on Sep 19, 2019
I prefer to keep it a macro. The compiler does not know that it is never executed, so it can generate a suboptimal code.
While it is a macro, it can be made a no-op, or even with compiler-specific instructions like __builtin_unreachable. This can help the compiler to generate more optimal code. For example, the popular idiom:
While it is a macro, it can be made a no-op, or even with compiler-specific instructions like __builtin_unreachable.
My PR 16280 uses _Py_NO_RETURN which uses __attribute__((noreturn)) with GCC and clang.
I'm not sure how __builtin_unreachable could be used with Py_UNREACHABLE() macro.
While it is a macro, it can be made a no-op
I understood that Py_UNREACHABLE() is used on purpose to prevent undefined behavior. For example, if a function accepts an enum, but is called with a value which is not part of the enum: what should happen? Should Python crash? Usually, we try to be nice and return an error. But sometimes, you cannot report an error and so Py_UNREACHABLE() is a good solution.
I prefer to keep it a macro. The compiler does not know that it is never executed, so it can generate a suboptimal code.
I don't see how PR 16280 could have an effect on that. I don't see how the compiler can guess that the code is never executed with the current macro.
--
Using a function allows to put a breakpoint on it.
In fact, I can easily modify PR 16280 to keep the macro, since I only call Py_FatalError() with a string. The function body is simple.
sir-sigurd commented on Sep 19, 2019
sir-sigurdmannequin
Mannequin
More actions
FWIW I proposed to add Py_ASSUME() macro that uses __builtin_unreachable() in bpo-38147.
_Py_NO_RETURN is a promise that the code past the function is unreachable, not that the function call is unreachable.
I'm not sure how __builtin_unreachable could be used with Py_UNREACHABLE() macro.
In the release mode Py_UNREACHABLE() can be replaced with __builtin_unreachable().
For example, if a function accepts an enum, but is called with a value which is not part of the enum: what should happen? Should Python crash?
If the accessibility of the code depends on the call from Python code, we should raise normal exception. If it is not possible to pass it out of the function, we should use PyErr_WriteUnraisable().
If the accessibility of the code depends on the call from C code,
we can use PyErr_BadInternalCall() or raise a SystemError directly.
If we are in the situation where recovering from errors is not possible (for example if the memory manager is broken) we use Py_FatalError().
If we are absolutely sure that the code never can be executed (unless memory was corrupted or something like), we use Py_UNREACHABLE(). It will silence compiler complains, fail loudly in debug mode if our assumption is wrong (this is our programming bug), and allow the compiler to optimize out the code if substituted by __builtin_unreachable() in the release mode.
I don't see how the compiler can guess that the code is never executed with the current macro.
But we can make the macro to expand to __builtin_unreachable in the release mode. Or just to no-op if there is other way to silence the compiler warning.
Using a function allows to put a breakpoint on it.
You can put a brackpoint on Py_FatalError().
In fact, I can easily modify PR 16280 to keep the macro, since I only call Py_FatalError() with a string.
It would be nice. We can consider using __builtin_unreachable() in different issue. I am also going to use Py_UNREACHABLE() more widely to write more uniform, but still optimal code. It could replace
Activity
vstinner commentedon Sep 17, 2019
With the following change, Python no longer compiles. I'm sure that it compiled successfully 2 weeks ago.
Main GCC error:
Objects/longobject.c: In function '_PyLong_Copy':
./Include/pymacro.h:119:26: error: expected expression before 'do'
119 | #define Py_UNREACHABLE() do { \
| ^~
Objects/longobject.c:83:30: note: in expansion of macro 'Py_UNREACHABLE'
83 | #define get_small_int(ival) (Py_UNREACHABLE(), NULL)
| ^~~~~~~~~~~~~~
It seems to be a regression introduced by:
commit 6b51998
Author: animalize <[email protected]>
Date: Fri Sep 6 14:00:56 2019 +0800
animalize commentedon Sep 18, 2019
This commit changed Py_UNREACHABLE() five days ago:
3ab6147
If remove this change, it can be compiled successfully.
animalize commentedon Sep 18, 2019
We can change Py_UNREACHABLE() to assert(0) in longobject.c
Or remove the article in Py_UNREACHABLE()
vstinner commentedon Sep 18, 2019
See https://bugs.python.org/issue37812#msg352670 and https://bugs.python.org/issue38015
sir-sigurd commentedon Sep 18, 2019
I believe that the problem is caused by the change in Py_UNREACHABLE() (3ab6147).
Before the mentioned commit Py_UNREACHABLE() was an expression, now it's a block. Py_UNREACHABLE() macro is public (see https://docs.python.org/3/c-api/intro.html#c.Py_UNREACHABLE), so this change can cause similar problems outside of CPython (i.e. that change was breaking).
sir-sigurd commentedon Sep 18, 2019
Also quote from Py_UNREACHABLE() doc:
6b51998 does exactly that, it replaces assert(0) with Py_UNREACHABLE().
vstinner commentedon Sep 18, 2019
Right, it was changed by:
commit 3ab6147
Author: Zachary Ware <[email protected]>
Date: Thu Sep 12 13:35:48 2019 +0100
I add Zachary Ware in the nosy list.
zware commentedon Sep 18, 2019
Whoops :(. Fixing this is beyond my C skill. I'd rather not lose the easter egg, but if it can't be fixed properly then 3ab6147 should just be reverted.
gpshead commentedon Sep 18, 2019
While I don't like how that get_small_int macro is defined...
and I don't like that Py_UNREACHABLE() was usable as an expression in the past...
it is probably best to just revert 3ab6147.
The choice to use a macro for this when it was created and the way the macro was defined means it is an expression forever unless we want to bother forcing code cleanups for uses of it.
zware commentedon Sep 18, 2019
I have poked at this a bit and have an implementation that defines a
static inline void _Py_Unreachable()in pymacro.h, see zware@d07b425This works on at least macOS and Windows, and we also have prior art for defining static inline functions in headers in #54288. If we agree that that's an acceptable solution, I'll open a PR for that change.
animalize commentedon Sep 19, 2019
If use static inline function, and Py_UNREACHABLE() inside an if-else branch that should return a value, compiler may emit warning:
https://godbolt.org/z/YtcNSf
Other compilers (gcc, icc) don't emit this warning.
This situation in real code:
https://github.com/python/cpython/blob/v3.8.0b4/Include/object.h#L600
https://github.com/python/cpython/blob/v3.8.0b4/Objects/longobject.c#L3088
animalize commentedon Sep 19, 2019
PR 16270 use Py_UNREACHABLE() in a single line.
It solves this particular issue.
ammaraskar commentedon Sep 19, 2019
For the control path warning, that can be fixed by just hinting to the compiler that the function doesn't return like so:
(https://godbolt.org/z/AG9tx_)
vstinner commentedon Sep 19, 2019
I like the idea of implementing Py_UNREACHABLE() as a function. Would it make sense to even declare it as a regular function rather than a static inline function? What is the benefit of inlining here? Inlining can make the code larger, rather than a function call at the machine code level is shorter.
vstinner commentedon Sep 19, 2019
I wrote PR 16280: "Convert Py_UNREACHABLE() macro to a function". This change fix this issue but also enhance Py_UNREACHABLE() which now dumps the Python traceback where the bug occurs. See my example:
#16280 (comment)
[-]Python no longer compiles without small integer singletons[/-][+]Py_UNREACHABLE() no longer behaves as a function call[/+]vstinner commentedon Sep 19, 2019
I propose to restrict this issue to Py_UNREACHABLE() macro/function.
Please use bpo-37812 to discuss longobject.c.
serhiy-storchaka commentedon Sep 19, 2019
I prefer to keep it a macro. The compiler does not know that it is never executed, so it can generate a suboptimal code.
While it is a macro, it can be made a no-op, or even with compiler-specific instructions like __builtin_unreachable. This can help the compiler to generate more optimal code. For example, the popular idiom:
could be compiled to the code equivalent to:
vstinner commentedon Sep 19, 2019
My PR 16280 uses _Py_NO_RETURN which uses __attribute__((noreturn)) with GCC and clang.
I'm not sure how __builtin_unreachable could be used with Py_UNREACHABLE() macro.
I understood that Py_UNREACHABLE() is used on purpose to prevent undefined behavior. For example, if a function accepts an enum, but is called with a value which is not part of the enum: what should happen? Should Python crash? Usually, we try to be nice and return an error. But sometimes, you cannot report an error and so Py_UNREACHABLE() is a good solution.
I don't see how PR 16280 could have an effect on that. I don't see how the compiler can guess that the code is never executed with the current macro.
--
Using a function allows to put a breakpoint on it.
In fact, I can easily modify PR 16280 to keep the macro, since I only call Py_FatalError() with a string. The function body is simple.
sir-sigurd commentedon Sep 19, 2019
FWIW I proposed to add Py_ASSUME() macro that uses __builtin_unreachable() in bpo-38147.
serhiy-storchaka commentedon Sep 19, 2019
_Py_NO_RETURN is a promise that the code past the function is unreachable, not that the function call is unreachable.
In the release mode Py_UNREACHABLE() can be replaced with __builtin_unreachable().
If the accessibility of the code depends on the call from Python code, we should raise normal exception. If it is not possible to pass it out of the function, we should use PyErr_WriteUnraisable().
If the accessibility of the code depends on the call from C code,
we can use PyErr_BadInternalCall() or raise a SystemError directly.
If we are in the situation where recovering from errors is not possible (for example if the memory manager is broken) we use Py_FatalError().
If we are absolutely sure that the code never can be executed (unless memory was corrupted or something like), we use Py_UNREACHABLE(). It will silence compiler complains, fail loudly in debug mode if our assumption is wrong (this is our programming bug), and allow the compiler to optimize out the code if substituted by __builtin_unreachable() in the release mode.
But we can make the macro to expand to __builtin_unreachable in the release mode. Or just to no-op if there is other way to silence the compiler warning.
You can put a brackpoint on Py_FatalError().
It would be nice. We can consider using __builtin_unreachable() in different issue. I am also going to use Py_UNREACHABLE() more widely to write more uniform, but still optimal code. It could replace
with
vstinner commentedon Sep 20, 2019
New changeset b154258 by Victor Stinner in branch 'master':
bpo-38205: Py_UNREACHABLE() calls Py_FatalError() (GH-16290)
b154258
vstinner commentedon Sep 20, 2019
New changeset 245d439 by Victor Stinner in branch '3.8':
bpo-38205: Py_UNREACHABLE() calls Py_FatalError() (GH-16290) (GH-16306)
245d439
vstinner commentedon Sep 23, 2019
Serhiy created bpo-38249: "Optimize out Py_UNREACHABLE in the release mode".
vstinner commentedon Oct 1, 2019
The initial issue has been fixed in 3.8 and master. I close the issue.