Skip to content

Commit

Permalink
Keep bssl stack pointer from confusing yield
Browse files Browse the repository at this point in the history
implement stack_thunk_yield() which is called within bssl
stack overflow checks for cont would always fail otherwise
  • Loading branch information
mcspr committed Jan 24, 2025
1 parent 269df4b commit bd243ab
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
43 changes: 42 additions & 1 deletion cores/esp8266/StackThunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,26 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "pgmspace.h"

#include "debug.h"
#include "StackThunk.h"

#include <pgmspace.h>
#include <ets_sys.h>

#include <umm_malloc/umm_malloc.h>
#include <umm_malloc/umm_heap_select.h>

extern "C" {

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 */
Expand Down Expand Up @@ -150,4 +158,37 @@ void stack_thunk_fatal_smashing()
__stack_chk_fail();
}

/* Called within bearssl code instead of optimistic_yield(...) */
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"
/* Keep the original caller */
"addi a1, a1, -16\n\t"
"s32i.n a0, a1, 12\n\t"
/* Swap bearssl <-> cont stacks */
"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"
/* optimistic_yield(10000) without extra l32r */
"movi a2, 0x10\n\t"
"addmi a2, a2, 0x2700\n\t"
"call0 optimistic_yield\n\t"
/* Swap bearssl <-> cont stacks, again */
"movi a2, stack_thunk_yield_save\n\t"
"l32i.n a1, a2, 0\n\t"
"\n"
/* Restore caller */
"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"
);

}
2 changes: 2 additions & 0 deletions cores/esp8266/StackThunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
extern "C" {
#endif

extern void stack_thunk_yield(void);

extern void stack_thunk_add_ref();
extern void stack_thunk_del_ref();
extern void stack_thunk_repaint();
Expand Down
2 changes: 1 addition & 1 deletion tools/sdk/include/bearssl/bearssl_git.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Do not edit -- Automatically generated by tools/sdk/ssl/bearssl/Makefile
#define BEARSSL_GIT b024386
#define BEARSSL_GIT 5166f2b
Binary file modified tools/sdk/lib/libbearssl.a
Binary file not shown.

0 comments on commit bd243ab

Please sign in to comment.