Skip to content

Commit 0ecb6f0

Browse files
authored
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
1 parent 2201770 commit 0ecb6f0

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

cores/esp8266/StackThunk.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ void stack_thunk_add_ref()
7373
}
7474
stack_thunk_top = stack_thunk_ptr + _stackSize - 1;
7575
stack_thunk_save = NULL;
76+
stack_thunk_yield_save = NULL;
7677
stack_thunk_repaint();
7778
}
7879
}
@@ -90,6 +91,7 @@ void stack_thunk_del_ref()
9091
stack_thunk_ptr = NULL;
9192
stack_thunk_top = NULL;
9293
stack_thunk_save = NULL;
94+
stack_thunk_yield_save = NULL;
9395
}
9496
}
9597

@@ -175,15 +177,18 @@ asm(
175177
"movi a2, stack_thunk_yield_save\n\t"
176178
"s32i.n a1, a2, 0\n\t"
177179
"movi a2, stack_thunk_save\n\t"
180+
/* But, only when inside of bssl stack (saved a1 != 0) */
181+
"l32i.n a3, a2, 0\n\t"
182+
"beqz a3, stack_thunk_yield_do_yield\n\t"
178183
"l32i.n a1, a2, 0\n\t"
179184
/* optimistic_yield(10000) without extra l32r */
185+
"stack_thunk_yield_do_yield:\n\t"
180186
"movi a2, 0x10\n\t"
181187
"addmi a2, a2, 0x2700\n\t"
182188
"call0 optimistic_yield\n\t"
183189
/* Swap bearssl <-> cont stacks, again */
184190
"movi a2, stack_thunk_yield_save\n\t"
185191
"l32i.n a1, a2, 0\n\t"
186-
"\n"
187192
/* Restore caller */
188193
"l32i.n a0, a1, 12\n\t"
189194
"addi a1, a1, 16\n\t"

cores/esp8266/StackThunk.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#ifndef _STACKTHUNK_H
2828
#define _STACKTHUNK_H
2929

30+
#include <stdint.h>
31+
3032
#ifdef __cplusplus
3133
extern "C" {
3234
#endif
@@ -80,9 +82,11 @@ thunk_"#fcnToThunk":\n\
8082
call0 stack_thunk_fatal_smashing\n\
8183
.L1"#fcnToThunk":\n\
8284
movi a15, stack_thunk_save /* Restore A1(SP) */\n\
83-
l32i.n a1, a15, 0\n\
84-
l32i.n a15, a1, 8 /* Restore the saved registers */\n\
85-
l32i.n a0, a1, 12\n\
85+
l32i.n a1, a15, 0/* Restore A1(SP) */\n\
86+
movi a0, 0 /* Purge temporary storage */\n\
87+
s32i.n a0, a15, 0\n\
88+
l32i.n a15, a1, 8/* Restore A15 */\n\
89+
l32i.n a0, a1, 12/* Restore A0 */\n\
8690
addi a1, a1, 16 /* Free up stack and return to caller */\n\
8791
ret\n\
8892
.size thunk_"#fcnToThunk", . - thunk_"#fcnToThunk"\n");

0 commit comments

Comments
 (0)