Skip to content

Commit b6d63f1

Browse files
miss-islingtonYhg1smhsmith
authored
[3.15] gh-159320: Enable stack size detection on Android (GH-159321) (#159409)
gh-159320: Enable stack size detection on Android (GH-159321) Extend the use of pthread_getattr_np in stack size detection from any glibc linux to any glibc *or* android linux. Update tests for android's new recursion limit. (cherry picked from commit 8ff41c2) Co-authored-by: T. Wouters <thomas@python.org> Co-authored-by: Malcolm Smith <smith@chaquo.com>
1 parent 33a05de commit b6d63f1

4 files changed

Lines changed: 14 additions & 14 deletions

File tree

‎Lib/test/test_ast/test_ast.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,13 +1056,11 @@ def next(self):
10561056
enum._test_simple_enum(_Precedence, _ast_unparse._Precedence)
10571057

10581058
@support.cpython_only
1059-
@support.run_with_limited_c_stack(
1060-
100_000 if sys.platform == "android" else 500_000)
1059+
@support.run_with_limited_c_stack(500_000)
10611060
@skip_wasi_stack_overflow()
10621061
@skip_emscripten_stack_overflow()
10631062
def test_ast_recursion_limit(self):
1064-
# Android test devices have less memory.
1065-
crash_depth = 100_000 if sys.platform == "android" else 500_000
1063+
crash_depth = 500_000
10661064
success_depth = 200
10671065
if _testinternalcapi is not None:
10681066
remaining = _testinternalcapi.get_c_recursion_remaining()

‎Lib/test/test_compile.py‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -724,14 +724,12 @@ def test_yet_more_evil_still_undecodable(self):
724724

725725
@support.cpython_only
726726
@unittest.skipIf(support.is_wasi, "exhausts limited stack on WASI")
727-
@support.run_with_limited_c_stack(
728-
100_000 if sys.platform == "android" else 500_000)
727+
@support.run_with_limited_c_stack(500_000)
729728
@support.skip_emscripten_stack_overflow()
730729
def test_compiler_recursion_limit(self):
731730
# Compiler frames are small
732731
limit = 100
733-
# Android test devices have less memory.
734-
crash_depth = limit * (1000 if sys.platform == "android" else 5000)
732+
crash_depth = limit * 5000
735733
success_depth = limit
736734

737735
def check_limit(prefix, repeated, mode="single"):
@@ -1043,8 +1041,7 @@ def test_path_like_objects(self):
10431041
# complex statements.
10441042
@support.requires_resource('cpu')
10451043
def test_stack_overflow(self):
1046-
# Android test devices have less memory.
1047-
size = 100_000 if sys.platform == "android" else 200_000
1044+
size = 200_000
10481045
compile("if a: b\n" * size, "<dummy>", "exec")
10491046

10501047
# Multiple users rely on the fact that CPython does not generate
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add dynamic detection of stack sizes on Android, allowing safer deep
2+
recursion.

‎Python/ceval.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,14 @@ hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp)
154154
*top = (uintptr_t)stack_addr;
155155
*base = ((uintptr_t)stack_addr) - stack_size;
156156
#else
157-
/// XXX musl supports HAVE_PTHRED_GETATTR_NP, but the resulting stack size
158-
/// (on alpine at least) is much smaller than expected and imposes undue limits
159-
/// compared to the old stack size estimation. (We assume musl is not glibc.)
157+
// musl supports HAVE_PTHREAD_GETATTR_NP, but the resulting stack size
158+
// (on alpine at least) is much smaller than expected and imposes undue
159+
// limits compared to the old stack size estimation. musl doesn't give
160+
// us a way to detect it, so we have to assume any non-glibc,
161+
// non-Android Linux is musl and mustn't rely on pthread_getattr_np.
160162
# if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX) && \
161-
!defined(__NetBSD__) && (defined(__GLIBC__) || !defined(__linux__))
163+
!defined(__NetBSD__) && \
164+
(defined(__GLIBC__) || defined(__ANDROID__) || !defined(__linux__))
162165
size_t stack_size, guard_size;
163166
void *stack_addr;
164167
pthread_attr_t attr;

0 commit comments

Comments
 (0)