Skip to content

Commit 40876dc

Browse files
authored
Fixes failing https connections to HelloServerBearSSL when using the (#8206)
MMU option with 48K IRAM shared. This happended after changes that increased IRAM code size that caused free IRAM for Heap to fall below ~16K, then "new" would OOM out in WiFiClientSecureBearSSL. Added private function to try IRAM first then switch to DRAM on fail to WiFiClientSecureBearSSL for iobuff allocations.
1 parent 95c6fbb commit 40876dc

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

Diff for: libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp

+29-29
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,17 @@ bool WiFiClientSecureCtx::_installClientX509Validator() {
10831083
return true;
10841084
}
10851085

1086+
std::shared_ptr<unsigned char> WiFiClientSecureCtx::_alloc_iobuf(size_t sz)
1087+
{ // Allocate buffer with preference to IRAM
1088+
HeapSelectIram primary;
1089+
auto sptr = std::shared_ptr<unsigned char>(new (std::nothrow) unsigned char[sz], std::default_delete<unsigned char[]>());
1090+
if (!sptr) {
1091+
HeapSelectDram alternate;
1092+
sptr = std::shared_ptr<unsigned char>(new (std::nothrow) unsigned char[sz], std::default_delete<unsigned char[]>());
1093+
}
1094+
return sptr;
1095+
}
1096+
10861097
// Called by connect() to do the actual SSL setup and handshake.
10871098
// Returns if the SSL handshake succeeded.
10881099
bool WiFiClientSecureCtx::_connectSSL(const char* hostName) {
@@ -1099,17 +1110,12 @@ bool WiFiClientSecureCtx::_connectSSL(const char* hostName) {
10991110

11001111
_sc = std::make_shared<br_ssl_client_context>();
11011112
_eng = &_sc->eng; // Allocation/deallocation taken care of by the _sc shared_ptr
1102-
//C This was borrowed from @earlephilhower PoC, to exemplify the use of IRAM.
1103-
//C Is this something we want to keep in the final release?
1104-
{ // ESP.setIramHeap(); would be an alternative to using a class to set a scope for IRAM usage.
1105-
HeapSelectIram ephemeral;
1106-
_iobuf_in = std::shared_ptr<unsigned char>(new (std::nothrow) unsigned char[_iobuf_in_size], std::default_delete<unsigned char[]>());
1107-
_iobuf_out = std::shared_ptr<unsigned char>(new (std::nothrow) unsigned char[_iobuf_out_size], std::default_delete<unsigned char[]>());
1108-
DBG_MMU_PRINTF("\n_iobuf_in: %p\n", _iobuf_in.get());
1109-
DBG_MMU_PRINTF( "_iobuf_out: %p\n", _iobuf_out.get());
1110-
DBG_MMU_PRINTF( "_iobuf_in_size: %u\n", _iobuf_in_size);
1111-
DBG_MMU_PRINTF( "_iobuf_out_size: %u\n", _iobuf_out_size);
1112-
} // ESP.resetHeap();
1113+
_iobuf_in = _alloc_iobuf(_iobuf_in_size);
1114+
_iobuf_out = _alloc_iobuf(_iobuf_out_size);
1115+
DBG_MMU_PRINTF("\n_iobuf_in: %p\n", _iobuf_in.get());
1116+
DBG_MMU_PRINTF( "_iobuf_out: %p\n", _iobuf_out.get());
1117+
DBG_MMU_PRINTF( "_iobuf_in_size: %u\n", _iobuf_in_size);
1118+
DBG_MMU_PRINTF( "_iobuf_out_size: %u\n", _iobuf_out_size);
11131119

11141120
if (!_sc || !_iobuf_in || !_iobuf_out) {
11151121
_freeSSL(); // Frees _sc, _iobuf*
@@ -1225,15 +1231,12 @@ bool WiFiClientSecureCtx::_connectSSLServerRSA(const X509List *chain,
12251231
_oom_err = false;
12261232
_sc_svr = std::make_shared<br_ssl_server_context>();
12271233
_eng = &_sc_svr->eng; // Allocation/deallocation taken care of by the _sc shared_ptr
1228-
{ // ESP.setIramHeap();
1229-
HeapSelectIram ephemeral;
1230-
_iobuf_in = std::shared_ptr<unsigned char>(new (std::nothrow) unsigned char[_iobuf_in_size], std::default_delete<unsigned char[]>());
1231-
_iobuf_out = std::shared_ptr<unsigned char>(new (std::nothrow) unsigned char[_iobuf_out_size], std::default_delete<unsigned char[]>());
1232-
DBG_MMU_PRINTF("\n_iobuf_in: %p\n", _iobuf_in.get());
1233-
DBG_MMU_PRINTF( "_iobuf_out: %p\n", _iobuf_out.get());
1234-
DBG_MMU_PRINTF( "_iobuf_in_size: %u\n", _iobuf_in_size);
1235-
DBG_MMU_PRINTF( "_iobuf_out_size: %u\n", _iobuf_out_size);
1236-
} // ESP.resetHeap();
1234+
_iobuf_in = _alloc_iobuf(_iobuf_in_size);
1235+
_iobuf_out = _alloc_iobuf(_iobuf_out_size);
1236+
DBG_MMU_PRINTF("\n_iobuf_in: %p\n", _iobuf_in.get());
1237+
DBG_MMU_PRINTF( "_iobuf_out: %p\n", _iobuf_out.get());
1238+
DBG_MMU_PRINTF( "_iobuf_in_size: %u\n", _iobuf_in_size);
1239+
DBG_MMU_PRINTF( "_iobuf_out_size: %u\n", _iobuf_out_size);
12371240

12381241
if (!_sc_svr || !_iobuf_in || !_iobuf_out) {
12391242
_freeSSL();
@@ -1272,15 +1275,12 @@ bool WiFiClientSecureCtx::_connectSSLServerEC(const X509List *chain,
12721275
_oom_err = false;
12731276
_sc_svr = std::make_shared<br_ssl_server_context>();
12741277
_eng = &_sc_svr->eng; // Allocation/deallocation taken care of by the _sc shared_ptr
1275-
{ // ESP.setIramHeap();
1276-
HeapSelectIram ephemeral;
1277-
_iobuf_in = std::shared_ptr<unsigned char>(new (std::nothrow) unsigned char[_iobuf_in_size], std::default_delete<unsigned char[]>());
1278-
_iobuf_out = std::shared_ptr<unsigned char>(new (std::nothrow) unsigned char[_iobuf_out_size], std::default_delete<unsigned char[]>());
1279-
DBG_MMU_PRINTF("\n_iobuf_in: %p\n", _iobuf_in.get());
1280-
DBG_MMU_PRINTF( "_iobuf_out: %p\n", _iobuf_out.get());
1281-
DBG_MMU_PRINTF( "_iobuf_in_size: %u\n", _iobuf_in_size);
1282-
DBG_MMU_PRINTF( "_iobuf_out_size: %u\n", _iobuf_out_size);
1283-
} // ESP.resetHeap();
1278+
_iobuf_in = _alloc_iobuf(_iobuf_in_size);
1279+
_iobuf_out = _alloc_iobuf(_iobuf_out_size);
1280+
DBG_MMU_PRINTF("\n_iobuf_in: %p\n", _iobuf_in.get());
1281+
DBG_MMU_PRINTF( "_iobuf_out: %p\n", _iobuf_out.get());
1282+
DBG_MMU_PRINTF( "_iobuf_in_size: %u\n", _iobuf_in_size);
1283+
DBG_MMU_PRINTF( "_iobuf_out_size: %u\n", _iobuf_out_size);
12841284

12851285
if (!_sc_svr || !_iobuf_in || !_iobuf_out) {
12861286
_freeSSL();

Diff for: libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ class WiFiClientSecureCtx : public WiFiClient {
189189
size_t _recvapp_len;
190190

191191
bool _clientConnected(); // Is the underlying socket alive?
192+
std::shared_ptr<unsigned char> _alloc_iobuf(size_t sz);
192193
void _freeSSL();
193194
int _run_until(unsigned target, bool blocking = true);
194195
size_t _write(const uint8_t *buf, size_t size, bool pmem);
@@ -309,8 +310,8 @@ class WiFiClientSecure : public WiFiClient {
309310

310311
// Limit the TLS versions BearSSL will connect with. Default is
311312
// BR_TLS10...BR_TLS12. Allowed values are: BR_TLS10, BR_TLS11, BR_TLS12
312-
bool setSSLVersion(uint32_t min = BR_TLS10, uint32_t max = BR_TLS12) { return _ctx->setSSLVersion(min, max); };
313-
313+
bool setSSLVersion(uint32_t min = BR_TLS10, uint32_t max = BR_TLS12) { return _ctx->setSSLVersion(min, max); };
314+
314315
// Check for Maximum Fragment Length support for given len before connection (possibly insecure)
315316
static bool probeMaxFragmentLength(IPAddress ip, uint16_t port, uint16_t len);
316317
static bool probeMaxFragmentLength(const char *hostname, uint16_t port, uint16_t len);

0 commit comments

Comments
 (0)