Skip to content

Commit d2a3cd7

Browse files
yamtdpgeorge
authored andcommitted
embed: Improve stack top estimation.
Obtaining the stack-top via a few function calls may yield a pointer which is too deep within the stack. So require the user to obtain it from a higher level (or via some other means). Fixes issue micropython#11781. Signed-off-by: YAMAMOTO Takashi <[email protected]>
1 parent be8d660 commit d2a3cd7

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

examples/embedding/main.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ static char heap[8 * 1024];
3131

3232
int main() {
3333
// Initialise MicroPython.
34-
mp_embed_init(&heap[0], sizeof(heap));
34+
//
35+
// Note: &stack_top below should be good enough for many cases.
36+
// However, depending on environment, there might be more appropriate
37+
// ways to get the stack top value.
38+
// eg. pthread_get_stackaddr_np, pthread_getattr_np,
39+
// __builtin_frame_address/__builtin_stack_address, etc.
40+
int stack_top;
41+
mp_embed_init(&heap[0], sizeof(heap), &stack_top);
3542

3643
// Run the example scripts (they will be compiled first).
3744
mp_embed_exec_str(example_1);

ports/embed/port/embed_util.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
#include "port/micropython_embed.h"
3535

3636
// Initialise the runtime.
37-
void mp_embed_init(void *gc_heap, size_t gc_heap_size) {
38-
mp_stack_ctrl_init();
37+
void mp_embed_init(void *gc_heap, size_t gc_heap_size, void *stack_top) {
38+
mp_stack_set_top(stack_top);
3939
gc_init(gc_heap, (uint8_t *)gc_heap + gc_heap_size);
4040
mp_init();
4141
}

ports/embed/port/micropython_embed.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <stddef.h>
3030
#include <stdint.h>
3131

32-
void mp_embed_init(void *gc_heap, size_t gc_heap_size);
32+
void mp_embed_init(void *gc_heap, size_t gc_heap_size, void *stack_top);
3333
void mp_embed_deinit(void);
3434

3535
// Only available if MICROPY_ENABLE_COMPILER is enabled.

0 commit comments

Comments
 (0)