diff --git a/apps/main.cpp b/apps/main.cpp index 51a95e2b9ab..0ea75e8885e 100644 --- a/apps/main.cpp +++ b/apps/main.cpp @@ -2,19 +2,6 @@ #include "global_preferences.h" #include -#if PLATFORM_DEVICE -// On device, stack start address is always known. TODO : Factorize address -static void * s_stackStart = reinterpret_cast(0x20000000 + 256*1024); -#else -// Stack start will be defined in ion_main. -static void * s_stackStart = nullptr; -#endif - -void * Ion::stackStart() { - assert(s_stackStart != nullptr); - return s_stackStart; -} - #define DUMMY_MAIN 0 #if DUMMY_MAIN @@ -80,7 +67,7 @@ void ion_main(int argc, const char * const argv[]) { * example, stack pointer could go backward after initialization and allocated * memory pointers could be overlooked during mark procedure. */ volatile int stackTop; - s_stackStart = (void *)(&stackTop); + Ion::setStackStart((void *)(&stackTop)); #endif AppsContainer::sharedAppsContainer()->run(); diff --git a/ion/Makefile b/ion/Makefile index 1c325ebc654..6efea82b872 100644 --- a/ion/Makefile +++ b/ion/Makefile @@ -31,6 +31,7 @@ ion_src += $(addprefix ion/src/shared/, \ events_keyboard.cpp \ events_modifier.cpp \ platform_info.cpp \ + stack_position.cpp \ storage.cpp \ unicode/utf8_decoder.cpp\ unicode/utf8_helper.cpp\ diff --git a/ion/include/ion.h b/ion/include/ion.h index 47c01061449..e01bc03c1fb 100644 --- a/ion/include/ion.h +++ b/ion/include/ion.h @@ -46,8 +46,9 @@ uint32_t random(); // Decompress data void decompress(const uint8_t * src, uint8_t * dst, int srcSize, int dstSize); -// Returns address to the first object that can be allocated on stack +// Sets and returns address to the first object that can be allocated on stack void * stackStart(); +void setStackStart(void *); // Tells whether the stack pointer is within acceptable bounds bool stackSafe(); diff --git a/ion/src/shared/stack_position.cpp b/ion/src/shared/stack_position.cpp new file mode 100644 index 00000000000..9cb17b2ee76 --- /dev/null +++ b/ion/src/shared/stack_position.cpp @@ -0,0 +1,22 @@ +#include + +namespace Ion { + +#if PLATFORM_DEVICE +// On device, stack start address is always known. TODO : Factorize address +static void * s_stackStart = reinterpret_cast(0x20000000 + 256*1024); +#else +// Stack start will be defined in ion_main. +static void * s_stackStart = nullptr; +#endif + +void * stackStart() { + assert(s_stackStart != nullptr); + return s_stackStart; +} + +void setStackStart(void * pointer) { + assert(pointer != nullptr); + s_stackStart = pointer; +} +} \ No newline at end of file diff --git a/quiz/src/runner.cpp b/quiz/src/runner.cpp index ef4b883c3d9..996a7ec9673 100644 --- a/quiz/src/runner.cpp +++ b/quiz/src/runner.cpp @@ -31,10 +31,16 @@ static inline void ion_main_inner() { void ion_main(int argc, const char * const argv[]) { - // Initialize the backlight Ion::Backlight::init(); - // Initialize Poincare::TreePool::sharedPool - Poincare::Init(); + Poincare::Init(); // Initialize Poincare::TreePool::sharedPool +#if !PLATFORM_DEVICE + /* s_stackStart must be defined as early as possible to ensure that there + * cannot be allocated memory pointers before. Otherwise, with MicroPython for + * example, stack pointer could go backward after initialization and allocated + * memory pointers could be overlooked during mark procedure. */ + volatile int stackTop; + Ion::setStackStart((void *)(&stackTop)); +#endif Poincare::ExceptionCheckpoint ecp; if (ExceptionRun(ecp)) {