From 6ccd0c1c55e5f16edcc0149e85a5ab9a0dd37ccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Tue, 21 Nov 2017 17:47:23 +0100 Subject: [PATCH] [python] Fixed the Python stack length computation. On the device, the stack is stored in reverse order, but it might not be the case on other platforms, which changes stack length computation. Change-Id: I0218224a77465b9672f137771c00f734fdfaea64 --- python/port/port.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/python/port/port.cpp b/python/port/port.cpp index 2e65242db..f3ecc4ec4 100644 --- a/python/port/port.cpp +++ b/python/port/port.cpp @@ -133,7 +133,17 @@ void gc_collect(void) { setjmp(regs); void **regs_ptr = (void**)(void*)®s; - gc_collect_root(regs_ptr, ((uintptr_t)python_stack_top - (uintptr_t)®s) / sizeof(uintptr_t)); + + /* On the device, the stack is stored in reverse order, but it might not be + * the case on a computer. We thus have to take the absolute value of the + * addresses difference. */ + size_t stackLength; + if ((uintptr_t)python_stack_top > (uintptr_t)(®s)) { + stackLength = (((uintptr_t)python_stack_top - (uintptr_t)(®s)) / sizeof(uintptr_t)); + } else { + stackLength = (((uintptr_t)(®s) - (uintptr_t)python_stack_top) / sizeof(uintptr_t)); + } + gc_collect_root(regs_ptr, stackLength); gc_collect_end(); }