Skip to content

Commit bd243ab

Browse files
committed
Keep bssl stack pointer from confusing yield
implement stack_thunk_yield() which is called within bssl stack overflow checks for cont would always fail otherwise
1 parent 269df4b commit bd243ab

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

cores/esp8266/StackThunk.cpp

+42-1
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,26 @@
2727
#include <stdint.h>
2828
#include <stdlib.h>
2929
#include <stdio.h>
30-
#include "pgmspace.h"
30+
3131
#include "debug.h"
3232
#include "StackThunk.h"
33+
34+
#include <pgmspace.h>
3335
#include <ets_sys.h>
36+
3437
#include <umm_malloc/umm_malloc.h>
3538
#include <umm_malloc/umm_heap_select.h>
3639

3740
extern "C" {
3841

42+
extern void optimistic_yield(uint32_t);
43+
3944
uint32_t *stack_thunk_ptr = NULL;
4045
uint32_t *stack_thunk_top = NULL;
46+
4147
uint32_t *stack_thunk_save = NULL; /* Saved A1 while in BearSSL */
48+
uint32_t *stack_thunk_yield_save = NULL; /* Saved A1 when yielding from within BearSSL */
49+
4250
uint32_t stack_thunk_refcnt = 0;
4351

4452
/* Largest stack usage seen in the wild at 6120 */
@@ -150,4 +158,37 @@ void stack_thunk_fatal_smashing()
150158
__stack_chk_fail();
151159
}
152160

161+
/* Called within bearssl code instead of optimistic_yield(...) */
162+
void stack_thunk_yield();
163+
asm(
164+
".section .text.stack_thunk_yield,\"ax\",@progbits\n\t"
165+
".literal_position\n\t"
166+
".align 4\n\t"
167+
".global stack_thunk_yield\n\t"
168+
".type stack_thunk_yield, @function\n\t"
169+
"\n"
170+
"stack_thunk_yield:\n\t"
171+
/* Keep the original caller */
172+
"addi a1, a1, -16\n\t"
173+
"s32i.n a0, a1, 12\n\t"
174+
/* Swap bearssl <-> cont stacks */
175+
"movi a2, stack_thunk_yield_save\n\t"
176+
"s32i.n a1, a2, 0\n\t"
177+
"movi a2, stack_thunk_save\n\t"
178+
"l32i.n a1, a2, 0\n\t"
179+
/* optimistic_yield(10000) without extra l32r */
180+
"movi a2, 0x10\n\t"
181+
"addmi a2, a2, 0x2700\n\t"
182+
"call0 optimistic_yield\n\t"
183+
/* Swap bearssl <-> cont stacks, again */
184+
"movi a2, stack_thunk_yield_save\n\t"
185+
"l32i.n a1, a2, 0\n\t"
186+
"\n"
187+
/* Restore caller */
188+
"l32i.n a0, a1, 12\n\t"
189+
"addi a1, a1, 16\n\t"
190+
"ret.n\n\t"
191+
".size stack_thunk_yield, .-stack_thunk_yield\n\t"
192+
);
193+
153194
}

cores/esp8266/StackThunk.h

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
extern "C" {
3232
#endif
3333

34+
extern void stack_thunk_yield(void);
35+
3436
extern void stack_thunk_add_ref();
3537
extern void stack_thunk_del_ref();
3638
extern void stack_thunk_repaint();
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// Do not edit -- Automatically generated by tools/sdk/ssl/bearssl/Makefile
2-
#define BEARSSL_GIT b024386
2+
#define BEARSSL_GIT 5166f2b

tools/sdk/lib/libbearssl.a

10.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)