From 4a5eae23a29d452596ee0c8d0961f661ae635214 Mon Sep 17 00:00:00 2001
From: Carlo Wood <carlo@alinoe.com>
Date: Sun, 4 Sep 2016 23:04:04 +0200
Subject: [PATCH] Increase stack space on posix 64bit architectures. (#5447)

When running a simulation with, for example,
make posix jmavsim
px4 crashed almost 100% reproducable near start up.

This turned out to be a stack overflow. On gitter
it was suggested that the main reason for this could
be stack sizes, as currently used, assume a 32bit pointer
size and that doubling the stack size for everything
but NuttX would be the Right Thing.

This is the solution that I came up with (it makes
my core dumps disappear).
---
 src/platforms/posix/px4_layer/px4_posix_tasks.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/platforms/posix/px4_layer/px4_posix_tasks.cpp b/src/platforms/posix/px4_layer/px4_posix_tasks.cpp
index 908a444802..6dd249951f 100644
--- a/src/platforms/posix/px4_layer/px4_posix_tasks.cpp
+++ b/src/platforms/posix/px4_layer/px4_posix_tasks.cpp
@@ -185,6 +185,10 @@ px4_task_t px4_task_spawn_cmd(const char *name, int scheduler, int priority, int
 		stack_size = PTHREAD_STACK_MIN;
 	}
 
+	// The stack size is intended for 32-bit architectures; therefore
+	// we often run out of stack space when pointers are larger than 4 bytes.
+	// Double the stack size on posix when we're on a 64-bit architecture.
+	stack_size *= __SIZEOF_POINTER__ >> 2;
 	rv = pthread_attr_setstacksize(&attr, stack_size);
 
 	if (rv != 0) {
-- 
GitLab