Replies: 1 comment
-
|
It looks like the only other semaphore usage in the zephyr port is the per-thread mutex, which is not on the stack: https://github.com/search?q=repo%3Amicropython%2Fmicropython+%2Fk_sem_%28take%7Cgive%7Cinit%29%2F&type=code You're right that the stack allocation of the semaphore is wrong; it should be a file-scope static instead. It looks like the intent is to hold the semaphore until the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
In the following code,
k_sem_initis called on memory allocated on stack:micropython/ports/zephyr/modsocket.c
Lines 436 to 442 in 255d74b
This will cause memory corruption if
CONFIG_OBJ_CORE_SEM=y. I can consistently reproduce it on olimex_stm32_e407 board:The issue is that with the config enabled, the address of the sem will be added to a global list
obj_type_sem, ref:https://github.com/zephyrproject-rtos/zephyr/blob/8e5e9922c5c428d457568e317256894efccce078/kernel/sem.c#L68-L73. We essentially caused a use-after-free once the function
getaddrinforeturns. This also reveals a potential larger problem where we need to treat each sem's lifecycle as infinite, as I don't see a zephyr interface to "destroy" a semaphore. Thus, rather than recreating a new sem each time we entersgetaddrinfofunction, we'll probably need a global one. Moreover, it seems the semaphore is really not needed in this function, and we can just do without -- dns_resolve_cb is called synchronously, right?micropython/ports/zephyr/modsocket.c
Lines 443 to 451 in 255d74b
Please let me know your thoughts. If my theory is correct, maybe we should also review other semaphore usage in port/zephyr? Thanks!
Bo
Beta Was this translation helpful? Give feedback.
All reactions