1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-07 16:23:38 +03:00

Stack Thunk - check saved a1 before using and zero after using (#9252)

post #9224, allow to call 'stack_thunk_yield()' outside of bssl context

Reset 'stack_thunk_save' before returning from 'thunk_...'ed function
Skip invalid a1 load and yield with the current value
This commit is contained in:
Max Prokhorov 2025-05-29 04:00:31 +03:00 committed by GitHub
parent 2201770a20
commit 0ecb6f06fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 4 deletions

View File

@ -73,6 +73,7 @@ void stack_thunk_add_ref()
} }
stack_thunk_top = stack_thunk_ptr + _stackSize - 1; stack_thunk_top = stack_thunk_ptr + _stackSize - 1;
stack_thunk_save = NULL; stack_thunk_save = NULL;
stack_thunk_yield_save = NULL;
stack_thunk_repaint(); stack_thunk_repaint();
} }
} }
@ -90,6 +91,7 @@ void stack_thunk_del_ref()
stack_thunk_ptr = NULL; stack_thunk_ptr = NULL;
stack_thunk_top = NULL; stack_thunk_top = NULL;
stack_thunk_save = NULL; stack_thunk_save = NULL;
stack_thunk_yield_save = NULL;
} }
} }
@ -175,15 +177,18 @@ asm(
"movi a2, stack_thunk_yield_save\n\t" "movi a2, stack_thunk_yield_save\n\t"
"s32i.n a1, a2, 0\n\t" "s32i.n a1, a2, 0\n\t"
"movi a2, stack_thunk_save\n\t" "movi a2, stack_thunk_save\n\t"
/* But, only when inside of bssl stack (saved a1 != 0) */
"l32i.n a3, a2, 0\n\t"
"beqz a3, stack_thunk_yield_do_yield\n\t"
"l32i.n a1, a2, 0\n\t" "l32i.n a1, a2, 0\n\t"
/* optimistic_yield(10000) without extra l32r */ /* optimistic_yield(10000) without extra l32r */
"stack_thunk_yield_do_yield:\n\t"
"movi a2, 0x10\n\t" "movi a2, 0x10\n\t"
"addmi a2, a2, 0x2700\n\t" "addmi a2, a2, 0x2700\n\t"
"call0 optimistic_yield\n\t" "call0 optimistic_yield\n\t"
/* Swap bearssl <-> cont stacks, again */ /* Swap bearssl <-> cont stacks, again */
"movi a2, stack_thunk_yield_save\n\t" "movi a2, stack_thunk_yield_save\n\t"
"l32i.n a1, a2, 0\n\t" "l32i.n a1, a2, 0\n\t"
"\n"
/* Restore caller */ /* Restore caller */
"l32i.n a0, a1, 12\n\t" "l32i.n a0, a1, 12\n\t"
"addi a1, a1, 16\n\t" "addi a1, a1, 16\n\t"

View File

@ -27,6 +27,8 @@
#ifndef _STACKTHUNK_H #ifndef _STACKTHUNK_H
#define _STACKTHUNK_H #define _STACKTHUNK_H
#include <stdint.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -80,9 +82,11 @@ thunk_"#fcnToThunk":\n\
call0 stack_thunk_fatal_smashing\n\ call0 stack_thunk_fatal_smashing\n\
.L1"#fcnToThunk":\n\ .L1"#fcnToThunk":\n\
movi a15, stack_thunk_save /* Restore A1(SP) */\n\ movi a15, stack_thunk_save /* Restore A1(SP) */\n\
l32i.n a1, a15, 0\n\ l32i.n a1, a15, 0/* Restore A1(SP) */\n\
l32i.n a15, a1, 8 /* Restore the saved registers */\n\ movi a0, 0 /* Purge temporary storage */\n\
l32i.n a0, a1, 12\n\ s32i.n a0, a15, 0\n\
l32i.n a15, a1, 8/* Restore A15 */\n\
l32i.n a0, a1, 12/* Restore A0 */\n\
addi a1, a1, 16 /* Free up stack and return to caller */\n\ addi a1, a1, 16 /* Free up stack and return to caller */\n\
ret\n\ ret\n\
.size thunk_"#fcnToThunk", . - thunk_"#fcnToThunk"\n"); .size thunk_"#fcnToThunk", . - thunk_"#fcnToThunk"\n");