Skip to content

Commit a11d4cd

Browse files
miss-islingtonYhg1smhsmith
authored
[3.14] gh-159320: Enable stack size detection on Android (GH-159321) (#159410)
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 c0e1ba1 commit a11d4cd

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
@@ -1027,13 +1027,11 @@ def next(self):
10271027
enum._test_simple_enum(_Precedence, _ast_unparse._Precedence)
10281028

10291029
@support.cpython_only
1030-
@support.run_with_limited_c_stack(
1031-
100_000 if sys.platform == "android" else 500_000)
1030+
@support.run_with_limited_c_stack(500_000)
10321031
@skip_wasi_stack_overflow()
10331032
@skip_emscripten_stack_overflow()
10341033
def test_ast_recursion_limit(self):
1035-
# Android test devices have less memory.
1036-
crash_depth = 100_000 if sys.platform == "android" else 500_000
1034+
crash_depth = 500_000
10371035
success_depth = 200
10381036
if _testinternalcapi is not None:
10391037
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"):
@@ -1037,8 +1035,7 @@ def test_path_like_objects(self):
10371035
# complex statements.
10381036
@support.requires_resource('cpu')
10391037
def test_stack_overflow(self):
1040-
# Android test devices have less memory.
1041-
size = 100_000 if sys.platform == "android" else 200_000
1038+
size = 200_000
10421039
compile("if a: b\n" * size, "<dummy>", "exec")
10431040

10441041
# 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
@@ -457,11 +457,14 @@ hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp)
457457
*top = (uintptr_t)stack_addr;
458458
*base = ((uintptr_t)stack_addr) - stack_size;
459459
#else
460-
/// XXX musl supports HAVE_PTHRED_GETATTR_NP, but the resulting stack size
461-
/// (on alpine at least) is much smaller than expected and imposes undue limits
462-
/// compared to the old stack size estimation. (We assume musl is not glibc.)
460+
// musl supports HAVE_PTHREAD_GETATTR_NP, but the resulting stack size
461+
// (on alpine at least) is much smaller than expected and imposes undue
462+
// limits compared to the old stack size estimation. musl doesn't give
463+
// us a way to detect it, so we have to assume any non-glibc,
464+
// non-Android Linux is musl and mustn't rely on pthread_getattr_np.
463465
# if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX) && \
464-
!defined(__NetBSD__) && (defined(__GLIBC__) || !defined(__linux__))
466+
!defined(__NetBSD__) && \
467+
(defined(__GLIBC__) || defined(__ANDROID__) || !defined(__linux__))
465468
size_t stack_size, guard_size;
466469
void *stack_addr;
467470
pthread_attr_t attr;

0 commit comments

Comments
 (0)