From ffda6c37311898fff3cf7541e7769103e2ec9bd0 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Mon, 2 Sep 2024 19:57:10 +0300 Subject: [PATCH] Keep bssl stack pointer from confusing yield only rememember a1, yield handles the rest of the registers see #9170, should no longer crash when using client keys --- cores/esp8266/StackThunk.cpp | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/cores/esp8266/StackThunk.cpp b/cores/esp8266/StackThunk.cpp index d9cb0316fe..c248283c87 100644 --- a/cores/esp8266/StackThunk.cpp +++ b/cores/esp8266/StackThunk.cpp @@ -43,7 +43,10 @@ extern void optimistic_yield(uint32_t); uint32_t *stack_thunk_ptr = NULL; uint32_t *stack_thunk_top = NULL; + uint32_t *stack_thunk_save = NULL; /* Saved A1 while in BearSSL */ +uint32_t *stack_thunk_yield_save = NULL; /* Saved A1 when yielding from within BearSSL */ + uint32_t stack_thunk_refcnt = 0; /* Largest stack usage seen in the wild at 6120 */ @@ -155,9 +158,32 @@ void stack_thunk_fatal_smashing() __stack_chk_fail(); } -void stack_thunk_yield() -{ - optimistic_yield(10000); -} +void stack_thunk_yield(); +asm( + ".section .text.stack_thunk_yield,\"ax\",@progbits\n\t" + ".literal_position\n\t" + ".align 4\n\t" + ".global stack_thunk_yield\n\t" + ".type stack_thunk_yield, @function\n\t" + "\n" +"stack_thunk_yield:\n\t" + "addi a1, a1, -16\n\t" + "s32i.n a0, a1, 12\n\t" + "call0 can_yield\n\t" + "beqz.n a2, .Lstack_thunk_yield_pass\n\t" + "movi a2, stack_thunk_yield_save\n\t" + "s32i.n a1, a2, 0\n\t" + "movi a2, stack_thunk_save\n\t" + "l32i.n a1, a2, 0\n\t" + "call0 yield\n\t" + "movi a2, stack_thunk_yield_save\n\t" + "l32i.n a1, a2, 0\n\t" + "\n" +".Lstack_thunk_yield_pass:\n\t" + "l32i.n a0, a1, 12\n\t" + "addi a1, a1, 16\n\t" + "ret.n\n\t" + ".size stack_thunk_yield, .-stack_thunk_yield\n\t" +); }