This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: faulthandler._stack_overflow doesn't work on x86-linux with KPTI enabled
Type: behavior Stage: resolved
Components: Extension Modules Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: miss-islington, pablogsal, vstinner, xry111
Priority: normal Keywords: patch

Created on 2019-05-08 17:23 by xry111, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 13205 merged xry111, 2019-05-08 17:57
PR 13605 merged miss-islington, 2019-05-27 23:50
Messages (6)
msg341915 - (view) Author: Xi Ruoyao (xry111) * Date: 2019-05-08 17:23
In faulthandler.c STACK_OVERFLOW_MAX_SIZE is defined to 100 * 1024 * 1024 (100MB).  But recently KPTI has been applied to mitigate Meltdown (CVE-2017-5754) so the userspace stack pointer may be very close to 0xffffffff on Linux systems with 32-bit x86.  For example, on my laptop it's sometimes 0xffffcc20.   So the expression

sp + STACK_OVERFLOW_MAX_SIZE

overflows and becomes a very small number.  That causes faulthandler._stack_overflow() to bail out after only one recursive call:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
RuntimeError: unable to raise a stack overflow (allocated 4124 bytes on the stack, 1 recursive calls)
msg341922 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2019-05-08 18:16
Are uintptr_t overflow/underflow semantics a defined behavior?
msg341923 - (view) Author: Xi Ruoyao (xry111) * Date: 2019-05-08 18:19
> Are uintptr_t overflow/underflow semantics a defined behavior?

Yes.  Unlike signed overflow, unsigned overflow is defined to be 2's complement.  The problem is this overflow results a wrong stack pointer limit and breaks _stack_overflow.
msg342209 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-05-11 17:13
New changeset 6236c9823ef3e8e2229b0598d3d8189adf5e00f2 by Victor Stinner (Xi Ruoyao) in branch 'master':
bpo-36856: Handle possible overflow in faulthandler_stack_overflow (GH-13205)
https://github.com/python/cpython/commit/6236c9823ef3e8e2229b0598d3d8189adf5e00f2
msg343719 - (view) Author: miss-islington (miss-islington) Date: 2019-05-28 00:14
New changeset 1062cf71faa14b90185cf159877083910df10f27 by Miss Islington (bot) in branch '3.7':
bpo-36856: Handle possible overflow in faulthandler_stack_overflow (GH-13205)
https://github.com/python/cpython/commit/1062cf71faa14b90185cf159877083910df10f27
msg343721 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-05-28 00:20
Thanks Xi Ruoyao, I applied your fix to 3.7 and master branches.
History
Date User Action Args
2022-04-11 14:59:14adminsetgithub: 81037
2019-05-28 00:20:17vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg343721

stage: patch review -> resolved
2019-05-28 00:14:24miss-islingtonsetnosy: + miss-islington
messages: + msg343719
2019-05-27 23:50:13miss-islingtonsetpull_requests: + pull_request13511
2019-05-11 17:13:26vstinnersetmessages: + msg342209
2019-05-08 18:19:13xry111setmessages: + msg341923
2019-05-08 18:16:16pablogsalsetnosy: + pablogsal
messages: + msg341922
2019-05-08 17:57:57xry111setkeywords: + patch
stage: patch review
pull_requests: + pull_request13116
2019-05-08 17:24:26SilentGhostsetnosy: + vstinner
2019-05-08 17:23:39xry111create