Skip to content

Commit 8ff41c2

Browse files
Yhg1smhsmith
andauthored
gh-159320: Enable stack size detection on Android (#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. Co-authored-by: Malcolm Smith <smith@chaquo.com>
1 parent efc6211 commit 8ff41c2

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

11091109
@support.cpython_only
1110-
@support.run_with_limited_c_stack(
1111-
100_000 if sys.platform == "android" else 500_000)
1110+
@support.run_with_limited_c_stack(500_000)
11121111
@skip_wasi_stack_overflow()
11131112
@skip_emscripten_stack_overflow()
11141113
def test_ast_recursion_limit(self):
1115-
# Android test devices have less memory.
1116-
crash_depth = 100_000 if sys.platform == "android" else 500_000
1114+
crash_depth = 500_000
11171115
success_depth = 200
11181116
if _testinternalcapi is not None:
11191117
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
@@ -773,14 +773,12 @@ def test_yet_more_evil_still_undecodable(self):
773773

774774
@support.cpython_only
775775
@unittest.skipIf(support.is_wasi, "exhausts limited stack on WASI")
776-
@support.run_with_limited_c_stack(
777-
100_000 if sys.platform == "android" else 500_000)
776+
@support.run_with_limited_c_stack(500_000)
778777
@support.skip_emscripten_stack_overflow()
779778
def test_compiler_recursion_limit(self):
780779
# Compiler frames are small
781780
limit = 100
782-
# Android test devices have less memory.
783-
crash_depth = limit * (1000 if sys.platform == "android" else 5000)
781+
crash_depth = limit * 5000
784782
success_depth = limit
785783

786784
def check_limit(prefix, repeated, mode="single"):
@@ -1092,8 +1090,7 @@ def test_path_like_objects(self):
10921090
# complex statements.
10931091
@support.requires_resource('cpu')
10941092
def test_stack_overflow(self):
1095-
# Android test devices have less memory.
1096-
size = 100_000 if sys.platform == "android" else 200_000
1093+
size = 200_000
10971094
compile("if a: b\n" * size, "<dummy>", "exec")
10981095

10991096
# 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
@@ -142,11 +142,14 @@ hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp)
142142
*top = (uintptr_t)stack_addr;
143143
*base = ((uintptr_t)stack_addr) - stack_size;
144144
#else
145-
/// XXX musl supports HAVE_PTHRED_GETATTR_NP, but the resulting stack size
146-
/// (on alpine at least) is much smaller than expected and imposes undue limits
147-
/// compared to the old stack size estimation. (We assume musl is not glibc.)
145+
// musl supports HAVE_PTHREAD_GETATTR_NP, but the resulting stack size
146+
// (on alpine at least) is much smaller than expected and imposes undue
147+
// limits compared to the old stack size estimation. musl doesn't give
148+
// us a way to detect it, so we have to assume any non-glibc,
149+
// non-Android Linux is musl and mustn't rely on pthread_getattr_np.
148150
# if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX) && \
149-
!defined(__NetBSD__) && (defined(__GLIBC__) || !defined(__linux__))
151+
!defined(__NetBSD__) && \
152+
(defined(__GLIBC__) || defined(__ANDROID__) || !defined(__linux__))
150153
size_t stack_size, guard_size;
151154
void *stack_addr;
152155
pthread_attr_t attr;

0 commit comments

Comments
 (0)