Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the stack top and re-enables the stack checker. See commit messages.