Skip to content

Fix cstack top#4

Merged
ltratt merged 2 commits intoykjit:masterfrom
vext01:fix-cstack-top
Mar 12, 2026
Merged

Fix cstack top#4
ltratt merged 2 commits intoykjit:masterfrom
vext01:fix-cstack-top

Conversation

@vext01
Copy link

@vext01 vext01 commented Mar 12, 2026

Fixes the stack top and re-enables the stack checker. See commit messages.

vext01 added 2 commits March 12, 2026 14:19
Previously we had it in `mp_state_ctx_t`, but that has this scary
comment:

```c
// This structure combines the above 3 structures.
// The order of the entries are important for root pointer scanning in the GC to work.
typedef struct _mp_state_ctx_t {
    mp_state_thread_t thread;
    mp_state_vm_t vm;
    mp_state_mem_t mem;
    #ifdef USE_YK
    YkMT *ykmt;
    #endif
} mp_state_ctx_t;
```

This makes me wonder what dark magic we may be violating by inserting
the YkMT there.

For peace of mind, move it into `mp_state_vm_t`, which is arguably a
better logical place anyway.
We broke the GC by giving it a bogus C stack top.

To gc in micropython you pass a pointer to the start of a memory buffer
and a number of aligned pointers that fit into that region. The GC then
scans the pointers.

For the C stack, it achieves this by.

 - stashing a pointer to the top of the C stack early on -- this is the
   buffer passed to the GC.

 - computing the number of pointers that fit on the C stack by computing
   the difference between the stack top and bottom dividing by the size
   of a pointer.

This is how the top of the stack is determined:

```c
void mp_cstack_init_with_sp_here(size_t stack_size) {
    #if __GNUC__ >= 13
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wdangling-pointer"
    #endif
    volatile int stack_dummy;
    mp_cstack_init_with_top((void *)&stack_dummy, stack_size);
    #if __GNUC__ >= 13
    #pragma GCC diagnostic pop
    #endif
}
```

Currently, this gets inlined into a parent function (`main`) with a
shadow stack and `stack_dummy` is allocated to our shadow stack.

This means:

 - the buffer passed to the GC is wrong. We scan the shadow stack
   instead of the C stack.

 - because the shadow stack is below the C stack, the difference between
   the "stack top" and bottom gives a -ve number, which is the casted
   into a massive +ve number which is divided by 8 to give a massive
   number of pointers to scan. The results in a buffer overflow.

The fix is to not allow this function to have a shadow stack or be
inlined into something which has a shadow stack.

This is also the reason that micropython's stack checker was borking. So
we can turn it back on.

Note that we still don't scan the yk shadow stack. This will be
implemented in a later PR.
@ltratt ltratt added this pull request to the merge queue Mar 12, 2026
Merged via the queue into ykjit:master with commit 900ae4a Mar 12, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants