Add OOM hardening across Transport, Identity, Destination, and Packet

- Transport::inbound(): pre-emptive cull before destination-table insert;
  try/catch around insert for explicit OOM diagnostics
- Transport::jobs(): split stale-entry culling into 5 independent
  per-phase try/catch blocks so a failure in one phase doesn't abort
  the others; reserve() before push_back loops in each phase
- cull_path_table()/cull_known_destinations(): std::min_element fallback
  when sort-index allocation fails, breaking the cull-OOM-still-full loop
- Identity::remember(): wrapped _known_destinations.insert in try/catch
  for explicit OOM diagnostic logging
- Destination::announce(): outer try/catch covers all OOM-prone ops
  (Bytes concat, sign, Packet ctor, send); inner _path_responses.insert
  catch preserved for degraded-mode behavior
- Packet::unpack(): dedicated std::bad_alloc catch returns false cleanly
- Type.h: MAX_RECEIPTS and MAX_QUEUED_ANNOUNCES now overridable at
  compile time via RNS_RECEIPTS_MAX / RNS_QUEUED_ANNOUNCES_MAX
- Transport.h: hashlist_maxsize and max_pr_tags exposed as runtime
  accessors for dynamic capacity tuning
This commit is contained in:
Chad Attermann committed 2026-02-27 12:53:49 -07:00
1 parent 20a7022241
commit f5a23a5a56
10 files changed
+381 -239

No files matched your search

+53 -25
View File
@@ -19,12 +19,15 @@ using namespace RNS::Type::Identity;
using namespace RNS::Cryptography;
using namespace RNS::Utilities;
#ifndef RNS_KNOWN_DESTINATIONS_MAX
#define RNS_KNOWN_DESTINATIONS_MAX 100
#endif
/*static*/ std::map<Bytes, Identity::IdentityEntry> Identity::_known_destinations;
/*static*/ bool Identity::_saving_known_destinations = false;
// CBA
// CBA ACCUMULATES
/*static*/ //uint16_t Identity::_known_destinations_maxsize = 100;
/*static*/ uint16_t Identity::_known_destinations_maxsize = 100;
/*static*/ uint16_t Identity::_known_destinations_maxsize = RNS_KNOWN_DESTINATIONS_MAX;
Identity::Identity(bool create_keys /*= true*/) : _object(new Object()) {
if (create_keys) {
@@ -196,7 +199,15 @@ Can be used to load previously created and saved identities into Reticulum.
else {
//p _known_destinations[destination_hash] = {OS::time(), packet_hash, public_key, app_data};
// CBA ACCUMULATES
_known_destinations.insert({destination_hash, {OS::time(), packet_hash, public_key, app_data}});
try {
_known_destinations.insert({destination_hash, {OS::time(), packet_hash, public_key, app_data}});
}
catch (const std::bad_alloc&) {
ERROR("remember: out of memory, identity not stored for " + destination_hash.toHex());
}
catch (const std::exception& e) {
ERROR(std::string("remember: exception storing identity: ") + e.what());
}
}
}
@@ -352,32 +363,49 @@ Recall last heard app_data for a destination hash.
}
/*static*/ void Identity::cull_known_destinations() {
TRACE("Transport::cull_path_table()");
TRACE("Identity::cull_known_destinations()");
if (_known_destinations.size() > _known_destinations_maxsize) {
// prune by age
uint16_t count = 0;
std::vector<std::pair<Bytes, IdentityEntry>> sorted_pairs;
// Copy key/value pairs from map into vector
std::for_each(_known_destinations.begin(), _known_destinations.end(), [&](const std::pair<const Bytes, IdentityEntry>& ref) {
sorted_pairs.push_back(ref);
});
// Sort vector using specified comparator
std::sort(sorted_pairs.begin(), sorted_pairs.end(), [](const std::pair<Bytes, IdentityEntry> &left, const std::pair<Bytes, IdentityEntry> &right) {
return left.second._timestamp < right.second._timestamp;
});
// Iterate vector of sorted values
for (auto& [destination_hash, identity_entry] : sorted_pairs) {
TRACE("Transport::cull_path_table: Removing destination " + destination_hash.toHex() + " from known destinations");
// Remove destination from known destinations
if (_known_destinations.erase(destination_hash) < 1) {
WARNING("Failed to remove destination " + destination_hash.toHex() + " from known destinations");
try {
// Build lightweight (timestamp, key) index to avoid copying full IdentityEntry
// objects — prevents OOM on heap-constrained devices when the table is full.
std::vector<std::pair<double, Bytes>> sorted_keys;
sorted_keys.reserve(_known_destinations.size());
for (const auto& [key, entry] : _known_destinations) {
sorted_keys.emplace_back(entry._timestamp, key);
}
++count;
if (_known_destinations.size() <= _known_destinations_maxsize) {
break;
// Sort ascending by timestamp (oldest first)
std::sort(sorted_keys.begin(), sorted_keys.end());
uint16_t count = 0;
for (const auto& [timestamp, destination_hash] : sorted_keys) {
TRACE("Identity::cull_known_destinations: Removing destination " + destination_hash.toHex() + " from known destinations");
if (_known_destinations.erase(destination_hash) < 1) {
WARNING("Failed to remove destination " + destination_hash.toHex() + " from known destinations");
}
++count;
if (_known_destinations.size() <= _known_destinations_maxsize) {
break;
}
}
DEBUG("Removed " + std::to_string(count) + " path(s) from known destinations");
}
catch (const std::bad_alloc& e) {
ERROR("cull_known_destinations: out of memory building sort index, falling back to single erase");
// Fallback: std::min_element does no heap allocation — erase one oldest entry
auto oldest = std::min_element(
_known_destinations.begin(), _known_destinations.end(),
[](const std::pair<const Bytes, IdentityEntry>& a,
const std::pair<const Bytes, IdentityEntry>& b) {
return a.second._timestamp < b.second._timestamp;
}
);
if (oldest != _known_destinations.end()) {
_known_destinations.erase(oldest);
}
}
DEBUG("Removed " + std::to_string(count) + " path(s) from known destinations");
catch (const std::exception& e) {
ERROR(std::string("cull_known_destinations: exception: ") + e.what());
}
}
}