Skip to content

Commit

Permalink
embed: Improve stack top estimation.
Browse files Browse the repository at this point in the history
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 <yamamoto@midokura.com>
  • Loading branch information
yamt authored and dpgeorge committed Feb 14, 2024
1 parent be8d660 commit d2a3cd7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
9 changes: 8 additions & 1 deletion examples/embedding/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ static char heap[8 * 1024];

int main() {
// Initialise MicroPython.
mp_embed_init(&heap[0], sizeof(heap));
//
// Note: &stack_top below should be good enough for many cases.
// However, depending on environment, there might be more appropriate
// ways to get the stack top value.
// eg. pthread_get_stackaddr_np, pthread_getattr_np,
// __builtin_frame_address/__builtin_stack_address, etc.
int stack_top;
mp_embed_init(&heap[0], sizeof(heap), &stack_top);

// Run the example scripts (they will be compiled first).
mp_embed_exec_str(example_1);
Expand Down
4 changes: 2 additions & 2 deletions ports/embed/port/embed_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
#include "port/micropython_embed.h"

// Initialise the runtime.
void mp_embed_init(void *gc_heap, size_t gc_heap_size) {
mp_stack_ctrl_init();
void mp_embed_init(void *gc_heap, size_t gc_heap_size, void *stack_top) {
mp_stack_set_top(stack_top);
gc_init(gc_heap, (uint8_t *)gc_heap + gc_heap_size);
mp_init();
}
Expand Down
2 changes: 1 addition & 1 deletion ports/embed/port/micropython_embed.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <stddef.h>
#include <stdint.h>

void mp_embed_init(void *gc_heap, size_t gc_heap_size);
void mp_embed_init(void *gc_heap, size_t gc_heap_size, void *stack_top);
void mp_embed_deinit(void);

// Only available if MICROPY_ENABLE_COMPILER is enabled.
Expand Down

0 comments on commit d2a3cd7

Please sign in to comment.