Skip to content

Commit a25f023

Browse files
Refactor once more, update stack size, add stress
Make stack_thunks generic, remove bearssl include inside of cores/esp8266. Allocate the stack on a WiFiServerSecure object creation to avoid fragmentation since we will need to allocate the stack to do any connected work, anyway. A stress test is now included which checks the total BearSSL second stack usage for a variety of TLS handshake and certificate options from badssl.org.
1 parent e6bbf7f commit a25f023

File tree

10 files changed

+438
-221
lines changed

10 files changed

+438
-221
lines changed

cores/esp8266/BearSSLThunks.c

Lines changed: 0 additions & 152 deletions
This file was deleted.

cores/esp8266/BearSSLThunks.h

Lines changed: 0 additions & 59 deletions
This file was deleted.

cores/esp8266/StackThunk.c

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
StackThunk.c - Allow use second stack for BearSSL calls
3+
4+
BearSSL uses a significant amount of stack space, much larger than
5+
the default Arduino core stack. These routines handle swapping
6+
between a secondary, user-allocated stack on the heap and the real
7+
stack.
8+
9+
Copyright (c) 2017 Earle F. Philhower, III. All rights reserved.
10+
11+
This library is free software; you can redistribute it and/or
12+
modify it under the terms of the GNU Lesser General Public
13+
License as published by the Free Software Foundation; either
14+
version 2.1 of the License, or (at your option) any later version.
15+
16+
This library is distributed in the hope that it will be useful,
17+
but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19+
Lesser General Public License for more details.
20+
21+
You should have received a copy of the GNU Lesser General Public
22+
License along with this library; if not, write to the Free Software
23+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24+
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
25+
*/
26+
27+
#include <stdint.h>
28+
#include <stdlib.h>
29+
#include "StackThunk.h"
30+
31+
uint32_t *stack_thunk_ptr = NULL;
32+
uint32_t *stack_thunk_top = NULL;
33+
uint32_t *stack_thunk_save = NULL; /* Saved A1 while in BearSSL */
34+
uint32_t stack_thunk_refcnt = 0;
35+
36+
#define _stackSize (5600/4)
37+
#define _stackPaint 0xdeadbeef
38+
39+
/* Add a reference, and allocate the stack if necessary */
40+
void stack_thunk_add_ref()
41+
{
42+
stack_thunk_refcnt++;
43+
if (stack_thunk_refcnt == 1) {
44+
stack_thunk_ptr = (uint32_t *)malloc(_stackSize * sizeof(uint32_t));
45+
stack_thunk_top = stack_thunk_ptr + _stackSize - 1;
46+
stack_thunk_save = NULL;
47+
stack_thunk_repaint();
48+
}
49+
}
50+
51+
/* Drop a reference, and free stack if no more in use */
52+
void stack_thunk_del_ref()
53+
{
54+
if (stack_thunk_refcnt == 0) {
55+
/* Error! */
56+
return;
57+
}
58+
stack_thunk_refcnt--;
59+
if (!stack_thunk_refcnt) {
60+
free(stack_thunk_ptr);
61+
stack_thunk_ptr = NULL;
62+
stack_thunk_top = NULL;
63+
stack_thunk_save = NULL;
64+
}
65+
}
66+
67+
void stack_thunk_repaint()
68+
{
69+
for (int i=0; i < _stackSize; i++) {
70+
stack_thunk_ptr[i] = _stackPaint;
71+
}
72+
}
73+
74+
/* Simple accessor functions used by postmortem */
75+
uint32_t stack_thunk_get_refcnt() {
76+
return stack_thunk_refcnt;
77+
}
78+
79+
uint32_t stack_thunk_get_stack_top() {
80+
return (uint32_t)stack_thunk_top;
81+
}
82+
83+
uint32_t stack_thunk_get_stack_bot() {
84+
return (uint32_t)stack_thunk_ptr;
85+
}
86+
87+
uint32_t stack_thunk_get_cont_sp() {
88+
return (uint32_t)stack_thunk_save;
89+
}
90+
91+
/* Return the number of bytes ever used since the stack was created */
92+
uint32_t stack_thunk_get_max_usage()
93+
{
94+
uint32_t cnt = 0;
95+
96+
/* No stack == no usage by definition! */
97+
if (!stack_thunk_ptr) {
98+
return 0;
99+
}
100+
101+
for (cnt=0; (cnt < _stackSize) && (stack_thunk_ptr[cnt] == _stackPaint); cnt++) {
102+
/* Noop, all work done in for() */
103+
}
104+
return 4 * (_stackSize - cnt);
105+
}
106+
107+
/* Print the stack from the first used 16-byte chunk to the top, decodable by the exception decoder */
108+
void stack_thunk_dump_stack()
109+
{
110+
uint32_t *pos = stack_thunk_top;
111+
while (pos < stack_thunk_ptr) {
112+
if ((pos[0] != _stackPaint) || (pos[1] != _stackPaint) || (pos[2] != _stackPaint) || (pos[3] != _stackPaint))
113+
break;
114+
pos += 4;
115+
}
116+
ets_printf(">>>stack>>>\n");
117+
while (pos < stack_thunk_ptr) {
118+
ets_printf("%08x: %08x %08x %08x %08x\n", pos, pos[0], pos[1], pos[2], pos[3]);
119+
pos += 4;
120+
}
121+
ets_printf("<<<stack<<<\n");
122+
}

0 commit comments

Comments
 (0)