Reduce shared RAM usage without lowering firmware capacities

Specialize repeater ACL session state, compact recent and flood records, stream Merkle proofs, and release cold OTA buffers safely. Preserve saved formats, stack sizes, table capacities, and OTA source limits. Account for lazy buffers when active and add compatibility, ARM alignment, allocation failure, and queued-response tests.

Validated six representative board/role builds, 1736 native test cases, sanitizer fixtures, and strict documentation. Document the preexisting Wio-E5 Mini flash overflow separately.
This commit is contained in:
mikecarper committed 2026-10-08 14:41:16 -07:00
1 parent 76ddc06e56
commit 4b3e419eb0
30 files changed
+1783 -150

No files matched your search

@@ -585,4 +585,5 @@ int main() {
std::printf("PASS: %s\n", test.name);
}
std::puts("27 ClientACL SPIFFS checks passed");
return 0;
}
+359
View File
@@ -0,0 +1,359 @@
#include <helpers/ota/OtaManager.h>
#include <helpers/ota/Multihash.h>
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace mesh::ota;
// Only production malloc/free calls are wrapped. C++ fixture containers use
// operator new, so the ledger measures the manager's actual cold allocations.
static std::array<void*, 32> live{};
static std::array<size_t, 32> sizes{};
static size_t allocated = 0, peak = 0;
static bool fail_next = false;
extern "C" void* __real_malloc(size_t);
extern "C" void __real_free(void*);
extern "C" void* __wrap_malloc(size_t bytes) {
if (fail_next) { fail_next = false; return nullptr; }
void* result = __real_malloc(bytes);
if (result) {
auto empty = std::find(live.begin(), live.end(), nullptr);
assert(empty != live.end());
const size_t slot = empty - live.begin();
live[slot] = result; sizes[slot] = bytes;
allocated += bytes; peak = std::max(peak, allocated);
}
return result;
}
extern "C" void __wrap_free(void* pointer) {
if (pointer) {
auto found = std::find(live.begin(), live.end(), pointer);
if (found != live.end()) {
const size_t slot = found - live.begin();
allocated -= sizes[slot]; live[slot] = nullptr;
}
}
__real_free(pointer);
}
static std::vector<uint8_t> reference_proof(std::vector<uint8_t> level, uint32_t index) {
std::vector<uint8_t> proof;
while (level.size() > 4) {
const uint32_t count = level.size() / 4;
if (!((count & 1u) && index == count - 1)) {
const uint32_t sibling = index ^ 1u;
proof.insert(proof.end(), level.begin() + sibling * 4, level.begin() + sibling * 4 + 4);
}
std::vector<uint8_t> next(((count + 1) / 2) * 4);
for (uint32_t i = 0; i < count; i += 2) {
if (i + 1 < count) merkle_combine(next.data() + (i / 2) * 4,
level.data() + i * 4, level.data() + (i + 1) * 4);
else memcpy(next.data() + (i / 2) * 4, level.data() + i * 4, 4);
}
level = std::move(next); index >>= 1;
}
return proof;
}
static void proofs() {
std::vector<uint32_t> counts;
for (uint32_t n = 1; n <= 65; ++n) counts.push_back(n);
for (uint32_t n : {127,128,129,255,256,257,511,512,513,1023,1024,1025,2047,2048,4095,4096})
counts.push_back(n);
for (uint32_t count : counts) {
std::vector<uint8_t> leaves(count * 4);
for (uint32_t i = 0; i < count; ++i) {
uint8_t value[4]; wr_u32le(value, i * 8191u + count);
merkle_leaf(leaves.data() + i * 4, value, 4);
}
const auto original = leaves;
uint8_t root[4]; merkle_root(root, leaves.data(), count);
const uint32_t step = count <= 65 ? 1 : std::max(1u, count / 7);
for (uint32_t i = 0; i < count; i += step) {
uint8_t output[128], tiny_scratch = 0x5A;
const uint8_t length = merkle_gen_proof(leaves.data(), count, i, nullptr, output);
const auto expected = reference_proof(leaves, i);
assert(expected.size() == length * 4u);
assert(expected.empty() || !memcmp(output, expected.data(), expected.size()));
assert(merkle_verify_from_leaf(leaves.data() + i * 4, i, output, length, root, count));
assert(merkle_gen_proof(leaves.data(), count, i, &tiny_scratch, output) == length);
assert(tiny_scratch == 0x5A && leaves == original);
}
}
uint8_t output[128]{};
assert(!merkle_gen_proof(nullptr, 1, 0, nullptr, output));
assert(!merkle_gen_proof(output, 1, 1, nullptr, output));
assert(!merkle_gen_proof(output, 1, 0, nullptr, nullptr));
}
class Source : public MotaSource {
public:
std::vector<uint8_t> mota;
MotaManifest manifest;
bool fail_read = false;
Source(uint32_t blocks, uint8_t value) {
const uint32_t total = 8 + MOTA_MFL + blocks * 4 + blocks * OTA_MAX_BLOCK + 5;
mota.resize(total);
memcpy(mota.data(), MOTA_MAGIC, 4); wr_u32le(mota.data() + 4, total);
uint8_t* mf = mota.data() + 8;
mf[0] = MOTA_FORMAT_VER; mf[1] = MFLAG_FULL; mf[2] = HASH_ALGO_SHA256;
wr_u32le(mf + 3, 123); wr_u32le(mf + 7, 1);
wr_u32le(mf + 11, blocks * OTA_MAX_BLOCK); wr_u32le(mf + 15, blocks * OTA_MAX_BLOCK);
mf[19] = 11; mf[56] = CODEC_FULL;
uint8_t* leaves = mf + MOTA_MFL;
uint8_t* payload = leaves + blocks * 4;
for (uint32_t i = 0; i < blocks; ++i) {
memset(payload + i * OTA_MAX_BLOCK, uint8_t(value + i), OTA_MAX_BLOCK);
merkle_leaf(leaves + i * 4, payload + i * OTA_MAX_BLOCK, OTA_MAX_BLOCK);
}
merkle_root(mf + 20, leaves, blocks);
memcpy(mota.data() + total - 5, MOTA_TRAILER, 5);
assert(mota_parse(mota.data(), mota.size(), manifest));
}
uint8_t count() override { return 1; }
bool describe(uint8_t index, MotaDesc& d) override {
if (index) return false;
memcpy(d.mid, manifest.merkle_root, 4); d.target_id = 123; d.fw_version = 1;
d.codec_id = CODEC_FULL; d.flags = MFLAG_FULL; d.block_size_log2 = 11;
d.total_size = mota.size(); d.leaves_off = 8 + MOTA_MFL;
d.block_count = manifest.block_count;
d.payload_off = manifest.payload - mota.data(); d.payload_size = manifest.payload_size;
return true;
}
bool read(uint8_t index, uint32_t off, uint8_t* out, uint32_t len) override {
if (index || (uint64_t)off + len > mota.size()) return false;
if (fail_read) { if (len) out[0] = 0xEE; return false; }
memcpy(out, mota.data() + off, len); return true;
}
};
struct Sent {
bool accept_data = true, accept_proof = true, accept_manifest = true;
std::vector<std::vector<uint8_t>> packets;
};
static bool send(void* opaque, const uint8_t* data, uint16_t len, bool) {
auto& sent = *static_cast<Sent*>(opaque);
if ((data[0] == OTA_DATA && !sent.accept_data) ||
(data[0] == OTA_PROOF && !sent.accept_proof) ||
(data[0] == OTA_MANIFEST && !sent.accept_manifest)) return false;
sent.packets.emplace_back(data, data + len); return true;
}
static bool encode(void*, const uint8_t* src, uint16_t, uint8_t* dst,
uint16_t cap, uint16_t* len) {
assert(cap == OTA_MAX_BLOCK); memset(dst, src[0], 8); *len = 8; return true;
}
static bool encode_long(void*, const uint8_t* src, uint16_t, uint8_t* dst,
uint16_t cap, uint16_t* len) {
assert(cap == OTA_MAX_BLOCK); memset(dst, src[0], 400); *len = 400; return true;
}
static bool decode(void*, const uint8_t* src, uint16_t len, uint8_t* dst,
uint16_t cap, uint16_t* produced) {
if (len != 8) return false;
memset(dst, src[0], cap); *produced = cap; return true;
}
static bool request(OtaManager& manager, const Source& source, uint16_t block = 0, bool compressed = true) {
ReqMsg req{}; memcpy(req.manifest_id, source.manifest.merkle_root, 4);
req.block_idx = block; req.want_mask = compressed ? ota_req_make_v2(0xFFF, true, true) : 0xFFFF;
uint8_t wire[MAX_PACKET_PAYLOAD];
return manager.on_message(wire, encode_req(wire, sizeof(wire), req));
}
static bool manifest_request(OtaManager& manager, const Source& source) {
GetManifestMsg req{}; memcpy(req.manifest_id, source.manifest.merkle_root, 4); req.want_mask = 0xFFFF;
uint8_t wire[MAX_PACKET_PAYLOAD];
return manager.on_message(wire, encode_get_manifest(wire, sizeof(wire), req));
}
static void drain(OtaManager& manager, uint32_t start = 100000) {
for (unsigned turn = 0; turn < 100 && (manager.pendingServeJobs() || manager.pendingManifestJobs()); ++turn) {
manager.set_clock(start + turn * 10000u); manager.serviceEgress();
}
assert(!manager.pendingServeJobs() && !manager.pendingManifestJobs());
}
static void verify(const Sent& sent, const Source& source, bool compressed) {
std::vector<uint8_t> bytes;
bool proof_seen = false;
for (const auto& wire : sent.packets) {
DataMsg data{}; ProofMsg proof{};
if (decode_data(wire.data(), wire.size(), data) && !memcmp(data.manifest_id, source.manifest.merkle_root, 4)) {
if (compressed) {
uint8_t fragment; uint16_t encoded; bool deflated;
assert(ota_data_v2_unpack_extended(data.frag_off, OTA_MAX_BLOCK, fragment, encoded, deflated));
assert(deflated && fragment == 0 && encoded == 8 && data.data_len == 12);
bytes.assign(OTA_MAX_BLOCK, data.data[4]);
} else bytes.insert(bytes.end(), data.data, data.data + data.data_len);
} else if (decode_proof(wire.data(), wire.size(), proof) &&
!memcmp(proof.manifest_id, source.manifest.merkle_root, 4)) {
assert(merkle_verify(source.manifest.payload, OTA_MAX_BLOCK, 0,
proof.proof, proof.n_proof, source.manifest.merkle_root, source.manifest.block_count));
proof_seen = true;
}
}
assert(proof_seen && bytes.size() == OTA_MAX_BLOCK);
assert(!memcmp(bytes.data(), source.manifest.payload, OTA_MAX_BLOCK));
}
static void lifetimes() {
Source source(9, 0x42);
Sent sent; OtaManager manager;
manager.begin(123, send, &sent); manager.set_transport_deflate_encoder(encode);
assert(manager.add_source(&source) && allocated == 0);
assert(request(manager, source) && allocated == 36);
sent.accept_data = false;
manager.serviceEgress(); assert(allocated == 36 + OTA_MAX_BLOCK);
manager.set_clock(OTA_SOURCE_BUFFER_IDLE_MS + 1000); manager.loop();
assert(allocated == 36 + OTA_MAX_BLOCK && manager.pendingServeJobs());
sent.accept_data = true; sent.accept_proof = false;
manager.serviceEgress(); manager.set_clock(100000); manager.serviceEgress();
manager.loop(); assert(allocated == 36 + OTA_MAX_BLOCK && manager.pendingServeJobs());
sent.accept_proof = true; drain(manager); verify(sent, source, true);
manager.set_clock(1000000); manager.loop();
assert(allocated == 0 && manager.servedCount() == 1); // descriptors survive; reload on demand
sent.packets.clear(); assert(request(manager, source)); drain(manager); verify(sent, source, true);
assert(manager.remove_source(&source) && allocated == 0 && manager.servedCount() == 0);
assert(manager.add_source(&source) && request(manager, source));
manager.clearPendingEgress(); assert(allocated == 0 && manager.servedCount() == 1);
assert(manifest_request(manager, source)); sent.accept_manifest = false;
manager.set_clock(2000000); manager.loop(); assert(allocated == 36);
manager.clear_sources(); assert(allocated == 0 && !manager.pendingManifestJobs());
}
static void faults_and_capacity() {
Source small(3, 0x31), large(OTA_PROOFGEN_SCRATCH / 4, 0x52);
Sent sent; OtaManager manager;
manager.begin(123, send, &sent); manager.set_transport_deflate_encoder(encode);
assert(manager.add_source(&small));
fail_next = true; assert(!request(manager, small) && allocated == 0 && !manager.pendingServeJobs());
assert(request(manager, small) && allocated == 12);
fail_next = true; manager.serviceEgress();
assert(allocated == 12); // encoder-output OOM selects raw v2 rather than dropping the transfer
drain(manager);
bool raw_seen = false;
for (const auto& wire : sent.packets) {
DataMsg data{};
if (decode_data(wire.data(), wire.size(), data)) {
uint8_t f; uint16_t n; bool deflated;
assert(ota_data_v2_unpack_extended(data.frag_off, OTA_MAX_BLOCK, f, n, deflated));
assert(!deflated); raw_seen = true;
}
}
assert(raw_seen); manager.clearPendingEgress(); assert(allocated == 0);
assert(manager.add_source(&large));
peak = 0; assert(manifest_request(manager, large));
assert(allocated == OTA_PROOFGEN_SCRATCH && peak == OTA_PROOFGEN_SCRATCH);
manager.clearPendingEgress(); assert(allocated == 0);
ReqProofMsg req{}; memcpy(req.manifest_id, large.manifest.merkle_root, 4);
req.block_idx = large.manifest.block_count - 1;
uint8_t wire[MAX_PACKET_PAYLOAD];
sent.packets.clear(); fail_next = false;
assert(manager.on_message(wire, encode_req_proof(wire, sizeof(wire), req)));
fail_next = true; drain(manager); assert(fail_next); fail_next = false;
assert(allocated == OTA_PROOFGEN_SCRATCH); // maximum-capacity proof needs no output allocation
ProofMsg maximum{}; assert(sent.packets.size() == 1);
assert(decode_proof(sent.packets[0].data(), sent.packets[0].size(), maximum));
assert(merkle_verify_from_leaf(large.manifest.leaves + req.block_idx * 4, req.block_idx,
maximum.proof, maximum.n_proof, large.manifest.merkle_root,
large.manifest.block_count));
manager.clearPendingEgress(); assert(allocated == 0);
small.fail_read = true; assert(!manifest_request(manager, small));
manager.set_clock(3000000); manager.loop(); assert(allocated == 0);
small.fail_read = false; assert(request(manager, small)); drain(manager);
assert(manager.serve(small.mota.data(), small.mota.size()) && allocated == 0);
fail_next = true; assert(request(manager, small, 0, false)); drain(manager);
assert(fail_next && allocated == 0); fail_next = false; // raw proof serving allocates nothing
}
static void concurrent_receive() {
Source outbound(5, 0x40), incoming(3, 0x70);
Sent sent; OtaManager manager; OtaStoreRam<16384> store;
manager.begin(123, send, &sent); manager.set_fetch_store(&store);
manager.set_transport_deflate_encoder(encode); manager.set_transport_deflate_decoder(decode);
assert(manager.add_source(&outbound) && request(manager, outbound));
sent.accept_data = false; manager.serviceEgress();
assert(allocated == 20 + OTA_MAX_BLOCK);
assert(manager.pull(incoming.manifest.merkle_root, 123) == OtaManager::PULL_STARTED);
for (unsigned f = 0; f < 2; ++f) {
ManifestMsg mf{}; memcpy(mf.manifest_id, incoming.manifest.merkle_root, 4);
mf.frag_idx = f; mf.frag_total = 2; mf.bytes = incoming.manifest.manifest_start + f * OTA_MF_FRAG;
mf.len = f == 0 ? OTA_MF_FRAG : MOTA_MFL - OTA_MF_FRAG;
uint8_t wire[MAX_PACKET_PAYLOAD]; manager.on_message(wire, encode_manifest(wire, sizeof(wire), mf));
}
assert(manager.fetchState() == OtaManager::FETCHING);
for (uint16_t block = 0; block < 3; ++block) {
uint8_t representation[12]; memset(representation + 4, uint8_t(0x70 + block), 8);
mh4(representation, representation + 4, 8);
DataMsg data{}; memcpy(data.manifest_id, incoming.manifest.merkle_root, 4); data.block_idx = block;
assert(ota_data_v2_pack_extended(0, 8, data.frag_off)); data.data = representation; data.data_len = 12;
uint8_t wire[MAX_PACKET_PAYLOAD]; assert(manager.on_message(wire, encode_data(wire, sizeof(wire), data)));
uint8_t siblings[128]; ProofMsg proof{}; memcpy(proof.manifest_id, incoming.manifest.merkle_root, 4);
proof.block_idx = block; proof.n_proof = merkle_gen_proof(incoming.manifest.leaves, 3, block, nullptr, siblings);
proof.proof = siblings; assert(manager.on_message(wire, encode_proof(wire, sizeof(wire), proof)));
manager.set_clock(100000 + block * 10000); manager.loop();
assert(allocated == 20 + OTA_MAX_BLOCK && manager.pendingServeJobs());
}
assert(manager.fetchState() == OtaManager::COMPLETE);
MotaManifest staged; assert(mota_parse(store.data(), store.staged_size(), staged));
assert(!memcmp(staged.payload, incoming.manifest.payload, incoming.manifest.payload_size));
sent.accept_data = true; drain(manager, 200000); verify(sent, outbound, true);
manager.clear_sources(); assert(allocated == 0);
}
static void interleaved_manifest() {
Source data_source(9, 0x35), manifest_source(17, 0x75);
Sent sent; OtaManager manager;
manager.begin(123, send, &sent); manager.set_transport_deflate_encoder(encode_long);
assert(manager.add_source(&data_source) && manager.add_source(&manifest_source));
assert(request(manager, data_source));
manager.serviceEgress(); // first compressed fragment is admitted; later DATA stays pinned
assert(manager.pendingServeJobs() && allocated == 36 + OTA_MAX_BLOCK);
peak = allocated;
assert(manifest_request(manager, manifest_source));
assert(allocated == 68 + OTA_MAX_BLOCK && peak == allocated); // old leaves were freed before growth
sent.accept_data = false;
manager.set_clock(100000); manager.serviceEgress();
manager.set_clock(200000); manager.serviceEgress();
assert(!manager.pendingManifestJobs() && manager.pendingServeJobs());
manager.loop(); assert(allocated == 68 + OTA_MAX_BLOCK);
sent.accept_data = true; drain(manager, 300000);
std::vector<uint8_t> encoded;
uint8_t identity[4]{}; bool proof_seen = false;
for (const auto& wire : sent.packets) {
DataMsg data{}; ProofMsg proof{};
if (decode_data(wire.data(), wire.size(), data)) {
assert(!memcmp(data.manifest_id, data_source.manifest.merkle_root, 4));
uint8_t fragment; uint16_t length; bool deflated;
assert(ota_data_v2_unpack_extended(data.frag_off, OTA_MAX_BLOCK, fragment, length, deflated));
assert(deflated && length == 400 && fragment * OTA_FRAG_DATA_V2 == encoded.size());
if (encoded.empty()) memcpy(identity, data.data, 4);
else assert(!memcmp(identity, data.data, 4));
encoded.insert(encoded.end(), data.data + 4, data.data + data.data_len);
} else if (decode_proof(wire.data(), wire.size(), proof)) {
assert(merkle_verify(data_source.manifest.payload, OTA_MAX_BLOCK, 0,
proof.proof, proof.n_proof, data_source.manifest.merkle_root,
data_source.manifest.block_count));
proof_seen = true;
}
}
assert(proof_seen && encoded == std::vector<uint8_t>(400, 0x35));
uint8_t expected[4]; mh4(expected, encoded.data(), encoded.size()); assert(!memcmp(identity, expected, 4));
manager.clearPendingEgress(); assert(allocated == 0 && manager.servedCount() == 2);
sent.packets.clear(); assert(request(manager, data_source)); manager.serviceEgress();
fail_next = true; assert(!manifest_request(manager, manifest_source));
assert(allocated == OTA_MAX_BLOCK); // failed growth released old leaves, but pinned DATA survives
drain(manager, 900000);
encoded.clear();
for (const auto& packet : sent.packets) {
DataMsg data{};
if (decode_data(packet.data(), packet.size(), data))
encoded.insert(encoded.end(), data.data + 4, data.data + data.data_len);
}
assert(encoded == std::vector<uint8_t>(400, 0x35));
manager.clearPendingEgress(); assert(allocated == 0);
manager.set_clock(0xFFFFFF00u); assert(request(manager, data_source)); drain(manager, 0xFFFFFF00u);
manager.set_clock(0xFFFFFF00u + 100000u); manager.loop(); assert(allocated == 0);
}
int main() {
proofs(); assert(allocated == 0);
lifetimes(); assert(allocated == 0);
faults_and_capacity(); assert(allocated == 0);
concurrent_receive(); assert(allocated == 0);
interleaved_manifest(); assert(allocated == 0);
puts("PASS: streaming proofs and OTA cold-buffer lifetimes");
}
+163
View File
@@ -0,0 +1,163 @@
"""Role-safe ACL layout and unchanged production persistence, without PlatformIO."""
import configparser
import importlib.util
from pathlib import Path
import re
import shutil
import subprocess
import tempfile
from types import SimpleNamespace
import unittest
ROOT = Path(__file__).resolve().parents[1]
SPEC = importlib.util.spec_from_file_location("client_acl_role", ROOT / "scripts/client_acl_role.py")
POLICY = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(POLICY)
DEFINE = POLICY.DEFINITION
FIXTURE = ROOT / "test/fixtures/client_acl_spiffs"
class ClientAclRoleTests(unittest.TestCase):
def test_only_complete_repeater_role_qualifies(self):
for value in ("+<../examples/simple_repeater>", "+<../examples/simple_repeater/*.cpp>",
["+<*.cpp>", "+<..\\examples\\simple_repeater\\*.cpp>"]):
self.assertEqual(POLICY.role_policy(value), {DEFINE: 1})
for role in POLICY.OTHER_ROLES:
self.assertEqual(POLICY.role_policy("+<../examples/" + role + ">"), {})
self.assertEqual(POLICY.role_policy("+<../examples/simple_repeater> "
"+<../examples/" + role + ">"), {})
for value in ("", "+<*>", "+<../examples/simple_repeater/main.cpp>",
"+<../examples/simple_repeater> -<../examples/simple_repeater/MyMesh.cpp>"):
self.assertEqual(POLICY.role_policy(value), {})
def test_ordered_source_selection_and_explicit_overrides(self):
role = "+<../examples/simple_repeater>"
self.assertEqual(POLICY.role_policy(role + " -<../examples/simple_repeater>"), {})
self.assertEqual(POLICY.role_policy(role + " -<../examples/simple_repeater> " + role), {DEFINE: 1})
for flag in ("-D" + DEFINE + "=0", "-D" + DEFINE + "=1", "-U" + DEFINE):
self.assertEqual(POLICY.role_policy(role, flag), {})
self.assertEqual(POLICY.role_policy(role, cppdefines=[(DEFINE, 1)]), {})
self.assertEqual(POLICY.role_policy(role, "-DCOMPANION_RADIO_FULL=1"), {})
for source in ("", "+<../examples/simple_room_server>", "+<*>"):
with self.assertRaises(ValueError):
POLICY.role_policy(source, "-D" + DEFINE + "=1")
for value in ("true", "2", "-1"):
with self.assertRaises(ValueError):
POLICY.role_policy(role, "-D" + DEFINE + "=" + value)
def test_one_common_definition_not_header_local(self):
class Env(dict):
def AppendUnique(self, **options): self["appended"] = options
def GetProjectOption(self, *args): raise AssertionError("effective filter required")
env = Env(SRC_FILTER="+<../examples/simple_repeater>", BUILD_FLAGS="", CPPDEFINES=[])
POLICY.install(env)
self.assertEqual(env["appended"], {"CPPDEFINES": [(DEFINE, 1)]})
env["SRC_FILTER"] = "+<../examples/simple_sensor>"
del env["appended"]
POLICY.install(env)
self.assertNotIn("appended", env)
def test_every_platform_base_installs_role_hook(self):
ini = (ROOT / "platformio.ini").read_text()
for name in ("esp32_base", "nrf52_base", "rp2040_base", "stm32_base"):
section = ini.split("[" + name + "]", 1)[1].split("\n[", 1)[0]
self.assertIn("pre:scripts/client_acl_role.py", section)
def test_real_board_roles_and_inherited_hooks(self):
config = configparser.ConfigParser(interpolation=None, strict=False,
inline_comment_prefixes=(";",))
config.read([str(ROOT / "platformio.ini")]
+ [str(path) for path in sorted((ROOT / "variants").glob("*/platformio.ini"))])
def option(section, key):
if config.has_option(section, key):
raw = config.get(section, key)
else:
parents = re.split(r"[,\n]", config.get(section, "extends", fallback=""))
raw = next((option(parent.strip(), key) for parent in parents
if parent.strip() and option(parent.strip(), key)), "")
return re.sub(r"\$\{([^}]+)\}", lambda match: option(*match[1].rsplit(".", 1)), raw)
platforms = set()
repeaters = 0
for section in config.sections():
if not section.startswith("env:") or section == "env:native": continue
filt = option(section, "build_src_filter")
definitions = POLICY.role_policy(filt, option(section, "build_flags"))
if definitions:
repeaters += 1
platforms.add(option(section, "platform"))
self.assertIn("pre:scripts/client_acl_role.py", option(section, "extra_scripts"), section)
self.assertGreater(repeaters, 100)
self.assertGreaterEqual(len(platforms), 4)
def test_compact_and_complete_acl_have_identical_persistence(self):
compiler = shutil.which("g++") or shutil.which("clang++")
self.assertIsNotNone(compiler)
source = r'''
#define main full_fixture_main
#include "test_client_acl_spiffs.cpp"
#undef main
#include <type_traits>
static_assert(std::is_trivially_copyable<ClientInfo>::value, "ACL remains copyable");
#if MESH_CLIENT_REPEATER_ONLY
static_assert(sizeof(((ClientInfo*)0)->extra) == 4, "only persisted sync field retained");
#else
static_assert(sizeof(((ClientInfo*)0)->extra.sensor.min_deltas) == 14, "sensor state retained");
static_assert(sizeof(((ClientInfo*)0)->extra.room.pending_ack) == 4, "room state retained");
#endif
int main() {
CHECK(full_fixture_main() == 0); // replay, corruption, OOM and rollback coverage
FakeFilesystem fs;
ClientACL acl;
acl.load(&fs, SELF);
for (int i = 0; i < MAX_CLIENTS; ++i) {
uint8_t key[PUB_KEY_SIZE] = {}; key[0] = uint8_t(i + 1);
auto* c = acl.putClient(mesh::Identity(key), PERM_ACL_ADMIN);
CHECK(c);
c->extra.room.sync_since = 0x12345678u + i;
c->last_timestamp = 100 + i;
c->out_path_is_persistable = true;
c->out_path_len = 1; c->out_path[0] = uint8_t(i + 3);
c->alt_path_len = 1; c->alt_path[0] = uint8_t(i + 5);
#if !MESH_CLIENT_REPEATER_ONLY
c->extra.room.pending_ack = 99; c->extra.room.push_failures = 3;
#endif
}
CHECK(acl.save(&fs));
ClientACL restored;
restored.load(&fs, SELF);
CHECK(restored.getNumClients() == MAX_CLIENTS);
for (int i = 0; i < MAX_CLIENTS; ++i) {
const auto* c = restored.getClientByIdx(i);
CHECK(c->extra.room.sync_since == 0x12345678u + i);
CHECK(c->out_path_len == 1 && c->out_path[0] == i + 3);
CHECK(c->alt_path_len == 1 && c->alt_path[0] == i + 5);
CHECK(c->isAdmin());
}
std::printf("SIZE:%zu\nCONTACTS:", sizeof(ClientInfo));
for (auto byte : fs.files["/s_contacts"]) std::printf("%02x", unsigned(byte));
std::puts("");
}
'''
results = []
with tempfile.TemporaryDirectory(prefix="compact-client-acl-") as directory:
work = Path(directory)
(work / "test.cpp").write_text(source)
for compact in (0, 1):
binary = work / ("acl-" + str(compact))
command = [compiler, "-std=c++17", "-Wall", "-Wextra", "-DESP32=1", "-DESP32_PLATFORM=1",
"-D" + DEFINE + "=" + str(compact), "-I" + str(FIXTURE / "mocks"),
"-I" + str(FIXTURE), "-I" + str(ROOT / "src"), str(work / "test.cpp"), "-o", str(binary)]
command[1:1] = ["-fsanitize=address,undefined", "-fno-sanitize-recover=all", "-fno-pie", "-no-pie"]
built = subprocess.run(command, text=True, capture_output=True, timeout=60)
self.assertEqual(built.returncode, 0, built.stdout + built.stderr)
run = subprocess.run([str(binary)], text=True, capture_output=True, timeout=30)
self.assertEqual(run.returncode, 0, run.stdout + run.stderr)
results.append(run.stdout)
full, compact = results
self.assertEqual(full.split("CONTACTS:")[1], compact.split("CONTACTS:")[1])
sizes = [int(re.search(r"SIZE:(\d+)", result)[1]) for result in results]
self.assertGreaterEqual(sizes[0] - sizes[1], 36)
if __name__ == "__main__": unittest.main()
@@ -99,6 +99,12 @@ struct SimpleMeshTables {
int8_t snr_x4;
};
std::vector<RecentRepeaterInfo> rows;
static void copyRecentRepeaterInfo(RecentRepeaterInfo& out,
const RecentRepeaterInfo& in) { out = in; }
static void copyRecentRepeaterPrefix(uint8_t* out,
const RecentRepeaterInfo& in) {
memcpy(out, in.prefix, sizeof(in.prefix));
}
int getRecentRepeaterCount() const { return static_cast<int>(rows.size()); }
const RecentRepeaterInfo* getNextRecentRepeaterBySortKey(
const RecentRepeaterInfo*, int previous, int& result) const {
+50 -10
View File
@@ -90,9 +90,11 @@ class FirmwareRamTest(unittest.TestCase):
self.assertEqual("ota_encoder_workspace" in components, enabled)
if enabled:
self.assertEqual(components["ota_encoder_workspace"], 1024 + 16)
self.assertEqual("ota_self_source_scratch" in components,
enabled and platform == "NRF52_PLATFORM")
if enabled and platform == "NRF52_PLATFORM":
self_source = (platform == "NRF52_PLATFORM" and "ENABLE_OTA" in defines
and "OTA_SEEDER_ONLY" not in defines and any(key in defines for key in (
"OTA_QSPI_STORE", "OTA_SD_STORE", "OTA_RAK_AUTO_STORE", "OTA_TOWER_AUTO_STORE")))
self.assertEqual("ota_self_source_scratch" in components, self_source)
if self_source:
self.assertEqual(components["ota_self_source_scratch"], 4096 + 32)
def test_device_encoder_hash_override_is_budgeted_and_bounded(self):
@@ -123,7 +125,7 @@ class FirmwareRamTest(unittest.TestCase):
"ENABLE_OTA": 1, "OTA_HEAP_CONTEXT": 1,
}, "GEPRC_Linkflow_900_repeater")
self.assertEqual(policy["components"]["client_table"], 32 * 320 + 16)
self.assertEqual(policy["components"]["flood_filter_table"], 63 * 200 + 16)
self.assertEqual(policy["components"]["flood_filter_table"], 63 * 192 + 16)
self.assertEqual(policy["components"]["ota_context"], 16384 + 16)
self.assertGreaterEqual(policy["required_contiguous_bytes"], 16384 + 16)
reduced = ram.requirements("STM32_PLATFORM", {
@@ -201,13 +203,13 @@ class FirmwareRamTest(unittest.TestCase):
}, "RAK_4631_repeater_unified_lora_ota")
components = combined["components"]
self.assertEqual(components["uart_bridge_and_driver"], 2560)
self.assertEqual(components["ethernet_or_external_ota_workspaces"], 5168)
self.assertEqual(components["ethernet_or_external_ota_workspaces"], 11344)
self.assertNotIn("ota_encoder_workspace", components)
self.assertNotIn("ota_self_source_scratch", components)
self.assertEqual(components["display_pixels_and_driver"], 2048)
self.assertEqual(combined["required_heap_bytes"] - legacy["required_heap_bytes"], 2560 - 2048)
self.assertEqual(components["loop_and_callback_stacks"], 8192 + 3072)
self.assertEqual(components["flood_filter_table"], 63 * 200 + 16)
self.assertEqual(components["flood_filter_table"], 63 * 192 + 16)
# An alternate port selects the same bridge instead of allocating two.
alternate = ram.requirements("NRF52_PLATFORM", {
**defines, "RAK4631_COMBINED_ETHERNET": 1,
@@ -221,7 +223,7 @@ class FirmwareRamTest(unittest.TestCase):
"RAK4631_COMBINED_ETHERNET": 1, "ENABLE_OTA": 1,
"MESHCORE_OTA_DEVICE_DEFLATE": 1,
}
for words, expected in ((1024, 5168), (1536, 6976), (2048, 9024)):
for words, expected in ((1024, 11344), (1536, 11344), (2048, 11344), (3072, 13120)):
with self.subTest(words=words):
policy = ram.requirements("NRF52_PLATFORM", {
**defines, "RAK4631_ETHERNET_TASK_STACK_WORDS": words,
@@ -231,21 +233,59 @@ class FirmwareRamTest(unittest.TestCase):
no_encoder = ram.requirements("NRF52_PLATFORM", {
**defines, "MESHCORE_OTA_DEVICE_DEFLATE": 0,
}, "RAK_4631_sensor")
self.assertEqual(no_encoder["components"]["ethernet_or_external_ota_workspaces"], 4928)
self.assertEqual(no_encoder["components"]["ethernet_or_external_ota_workspaces"], 10304)
for bits in (7, 8, 9, 10):
with self.subTest(bits=bits):
policy = ram.requirements("NRF52_PLATFORM", {
**defines, "MESHCORE_OTA_DEFLATE_HASH_BITS": bits,
}, "RAK_4631_sensor")
self.assertEqual(policy["components"]["ethernet_or_external_ota_workspaces"],
max(4928, (1 << bits) * 2 + 16 + 4096 + 32))
for capacity, expected in ((512, 5168), (1024, 5440), (2048, 6464)):
max(4928, (1 << bits) * 2 + 16 + 4096 + 32 + 6176))
for capacity, expected in ((512, 11344), (1024, 11344), (2048, 11344)):
with self.subTest(tx_capacity=capacity):
policy = ram.requirements("NRF52_PLATFORM", {
**defines, "ETHERNET_CLI_TX_BUFFER_BYTES": capacity,
}, "RAK_4631_sensor")
self.assertEqual(policy["components"]["ethernet_or_external_ota_workspaces"], expected)
def test_compact_tables_keep_capacity_and_reserve_active_ota_allocations(self):
for platform, leaf_limit in (("NRF52_PLATFORM", 4096), ("ESP32_PLATFORM", 16384),
("RP2040_PLATFORM", 4096), ("STM32_PLATFORM", 4096)):
with self.subTest(platform=platform):
flags = {"ENABLE_OTA": 1, "MESH_CLIENT_REPEATER_ONLY": 1,
"MAX_RECENT_REPEATERS": 512}
compact = ram.requirements(platform, flags, "board_repeater")["components"]
self.assertEqual(compact["client_table"], 32 * 284 + 16)
self.assertEqual(compact["ota_generic_source_buffers"], leaf_limit + 2048 + 32)
full = ram.requirements(platform, {**flags, "MESH_CLIENT_REPEATER_ONLY": 0},
"board_repeater")["components"]
self.assertEqual(full["client_table"] - compact["client_table"], 36 * 32)
if platform == "ESP32_PLATFORM":
self.assertEqual(compact["neighbor_history"], 512 * 9 + 16)
sd = ram.requirements("NRF52_PLATFORM", {"ENABLE_OTA": 1, "OTA_SD_STORE": 1},
"board_repeater")["components"]
self.assertEqual(sd["ota_generic_source_buffers"], 8192 + 2048 + 32)
custom = ram.requirements("NRF52_PLATFORM", {
"ENABLE_OTA": 1, "OTA_PROOFGEN_SCRATCH": 12000, "OTA_MAX_BLOCK": 1024,
}, "board_repeater")["components"]
self.assertEqual(custom["ota_generic_source_buffers"], 12000 + 1024 + 32)
for default, expected in ((256, 256), (2048, 2048)):
parts = ram.requirements("ESP32_PLATFORM", {
"MESH_RAM_DEFAULT_RECENT_REPEATERS": default,
}, "board_repeater")["components"]
self.assertEqual(parts["neighbor_history"], expected * 9 + 16)
disabled = ram.requirements("ESP32_PLATFORM", {"MESH_ENABLE_RECENT_REPEATERS": 0},
"board_repeater")["components"]
self.assertEqual(disabled["neighbor_history"], 0)
s3 = ram.requirements("ESP32_PLATFORM", {}, "board_repeater")
self.assertGreaterEqual(s3["required_contiguous_bytes"], 2048 * 9 + 16)
alias = ram.requirements("ESP32_PLATFORM", {"MESH_CLIENT_REPEATER_ONLY": 1},
"custom_companion_alias")
self.assertEqual(alias["components"]["client_table"], 32 * 284 + 16)
self.assertEqual(alias["components"]["flood_filter_table"], 63 * 192 + 16)
self.assertEqual(alias["components"]["neighbor_history"], 2048 * 9 + 16)
self.assertEqual(alias["components"]["radio_packet_pool"], 10240)
def test_combined_rak_oled_bound_does_not_change_other_display_budgets(self):
defines = {
"RAK_4631": 1, "ETHERNET_ENABLED": 1, "OTA_RAK_AUTO_STORE": 1,
+66
View File
@@ -76,7 +76,31 @@ public:
bool isFloodChannelDataRule(const FloodPacketFilterEntry&) const;
};
'''
ROOM_CLASS = r'''
class FloodRuleEngine {
public:
static constexpr uint8_t RULE_SLOTS = 31, NAME_LEN = 32, PATH_PREFIX_BYTES_MAX = 9;
@ENTRY@;
FILESYSTEM* _fs;
Entry storage[RULE_SLOTS]{};
Entry* _entries = storage;
explicit FloodRuleEngine(MemoryFS& fs): _fs(&fs) {}
bool save();
};
static const char RULE_FILE[] = "/flood_filter";
static const char RULE_TEMP_FILE[] = "/flood_filter.tmp";
static const char RULE_BACKUP_FILE[] = "/flood_filter.bak";
'''
SCENARIOS = r'''
template<class Entry> void canonicalRule(Entry& row) {
row.active=true;row.payload_type=PAYLOAD_TYPE_ADVERT;
row.min_hops=2;row.max_hops=63;row.suspend_on_temp_radio=true;
std::strcpy(row.scope_name,"#Town");row.match_blacklisted_path=true;
row.scope_uses_slow_timing=true;row.incoming_scope_kind=FloodFilterPolicy::RULE_IN_ALLOWED;
row.path_hash_size=3;row.rate_limit_enabled=true;row.rate_per_minute=1234;
row.priority=7;row.stop_on_match=true;row.retry_on_match=true;
row.rate_window_started=0xfedcba98;row.rate_window_count=17;row.rate_window_active=true;
}
std::vector<uint8_t> fixture(uint8_t slots, uint8_t highest) {
MemoryFS fs;MyMesh writer(fs,slots);
for (uint8_t index : {uint8_t(0),highest}) {
@@ -97,6 +121,41 @@ std::map<std::string,std::vector<uint8_t>> snapshot(const MemoryFS& fs) {
return result;
}
int main() {
// Canonical FPF7 bytes remain independent of the compact in-memory layout.
// Exercise every persisted packed flag plus the addressable retry flag.
{
MemoryFS fs;MyMesh writer(fs,63);auto& row=writer.storage[0];
canonicalRule(row);
std::vector<uint8_t> expected={'F','P','F','7',1,1,PAYLOAD_TYPE_ADVERT,2,63,1};
const auto zeroes=[&expected](size_t count) {expected.insert(expected.end(),count,0);};
expected.insert(expected.end(),{'#','T','o','w','n'});zeroes(27);
expected.insert(expected.end(),{1,1,1,3});zeroes(32);
expected.push_back(0x80);zeroes(32+32);
expected.insert(expected.end(),{3,0});zeroes(9);
expected.insert(expected.end(),{0,1,0xd2,0x04});zeroes(32);
expected.insert(expected.end(),{7,1,'F','P','S','1',0xff,0xff,0,0,0});
assert(writer.saveFloodPacketFilters());assert(fs.get(FLOOD_PACKET_FILTER_FILE)==expected);
MyMesh reader(fs,63);assert(reader.loadFloodPacketFilters());const auto& loaded=reader.storage[0];
assert(loaded.active && loaded.suspend_on_temp_radio && loaded.match_blacklisted_path);
assert(loaded.scope_uses_slow_timing && !loaded.drop_on_match && loaded.rate_limit_enabled);
assert(loaded.stop_on_match && loaded.retry_on_match && loaded.rate_per_minute==1234);
assert(!loaded.rate_window_active && loaded.rate_window_started==0 && loaded.rate_window_count==0);
// Execute the real room serializer too. It retains its 31 padded slots,
// and the same canonical row must load through the repeater path.
MemoryFS room_fs;FloodRuleEngine room(room_fs);canonicalRule(room.storage[0]);
auto room_expected=expected;room_expected[4]=31;
const size_t row_stride=expected.size()-5-9;
room_expected.insert(room_expected.end()-9,30*row_stride,0);
assert(room.save());assert(room_fs.get(FLOOD_PACKET_FILTER_FILE)==room_expected);
MyMesh from_room(room_fs,63);assert(from_room.loadFloodPacketFilters());
assert(from_room.storage[0].scope_uses_slow_timing && from_room.storage[0].stop_on_match);
assert(from_room.storage[0].retry_on_match && from_room.storage[0].rate_per_minute==1234);
// A drop rule exercises the remaining persisted packed flag without an
// incompatible scope/rate/retry action, while preserving the same format.
row={};row.active=true;row.payload_type=PAYLOAD_TYPE_ADVERT;row.max_hops=63;row.drop_on_match=true;
assert(writer.saveFloodPacketFilters());MyMesh drop(fs,63);assert(drop.loadFloodPacketFilters());
assert(drop.storage[0].active && drop.storage[0].drop_on_match && !drop.storage[0].retry_on_match);
}
const auto large=fixture(63,62),small=fixture(4,2);
// Older writers stored every slot, including inactive trailing entries.
// Build the exact production wire format and replace the final active row
@@ -198,6 +257,8 @@ class FloodPolicyCapacityTests(unittest.TestCase):
def test_saved_rules_survive_smaller_firmware_capacity(self):
source=(ROOT/'examples/simple_repeater/MyMesh.cpp').read_text()
header=(ROOT/'examples/simple_repeater/MyMesh.h').read_text()
room_source=(ROOT/'examples/simple_room_server/FloodRuleEngine.cpp').read_text()
room_header=(ROOT/'examples/simple_room_server/FloodRuleEngine.h').read_text()
fs=HARNESS[:HARNESS.index('@STORE@')]
fs=fs.replace('bool valid = false;', 'bool valid = false;\n size_t cursor=0;')
fs=fs.replace('size_t write(const uint8_t*, size_t);', '''size_t write(const uint8_t*, size_t);
@@ -216,6 +277,11 @@ class FloodPolicyCapacityTests(unittest.TestCase):
'bool MyMesh::isFloodChannelDataRule(', 'bool MyMesh::loadFloodPacketFilters(',
'bool MyMesh::saveFloodPacketFilters('):
parts.append(extract_braced(source,marker))
parts.append(ROOM_CLASS.replace('@ENTRY@',extract_braced(room_header,'struct Entry')))
for marker in ('static File openRead(', 'static File openWrite(',
'static uint32_t updateFileHash(', 'static bool verifyWrittenFile(',
'bool FloodRuleEngine::save('):
parts.append(extract_braced(room_source,marker))
parts.append(SCENARIOS)
with tempfile.TemporaryDirectory(prefix='flood-policy-capacity-') as temp:
p=Path(temp);cpp=p/'test.cpp';cpp.write_text('\n'.join(parts))
+238
View File
@@ -0,0 +1,238 @@
"""Exercise production flood-entry layouts and independent packed flag states."""
from pathlib import Path
import os
import subprocess
import tempfile
import unittest
from test_replay_reset_integration import extract_braced
ROOT = Path(__file__).resolve().parents[1]
PRELUDE = r'''
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <type_traits>
#include <helpers/FloodFilterPolicy.h>
#include <helpers/FloodRuleCLI.h>
#define FLOOD_PACKET_FILTER_SCOPE_NAME_LEN 32
#define FLOOD_GROUP_MODERATION_NAME_LEN 32
#define FLOOD_PACKET_FILTER_PATH_PREFIX_BYTES_MAX 9
static constexpr uint8_t NAME_LEN = 32, PATH_PREFIX_BYTES_MAX = 9;
'''
SCENARIOS = r'''
static_assert(sizeof(RepeaterEntry) == 192 && sizeof(RoomEntry) == 192,
"63 repeater rows / 31 room rows must retain the 192-byte bound");
static_assert(sizeof(LegacyEntry) == 40, "retain legacy STM32 rule layout");
static_assert(alignof(RepeaterEntry) >= alignof(uint32_t));
static_assert(alignof(RoomEntry) >= alignof(uint32_t));
#define CHECK_ALIGNED(T) \
static_assert(offsetof(T, rate_window_started) % alignof(uint32_t) == 0); \
static_assert(offsetof(T, rate_window_count) % alignof(uint16_t) == 0); \
static_assert(offsetof(T, rate_per_minute) % alignof(uint16_t) == 0); \
static_assert(std::is_trivially_copyable<T>::value)
CHECK_ALIGNED(RepeaterEntry);
CHECK_ALIGNED(RoomEntry);
template<class Entry> void setFlags(Entry& e, unsigned flags) {
e.active = flags & 1;
e.suspend_on_temp_radio = flags & 2;
e.match_blacklisted_path = flags & 4;
e.scope_uses_slow_timing = flags & 8;
e.drop_on_match = flags & 16;
e.rate_limit_enabled = flags & 32;
e.stop_on_match = flags & 64;
e.retry_on_match = flags & 128;
e.rate_window_active = flags & 256;
}
template<class Entry> unsigned flagsOf(const Entry& e) {
return unsigned(e.active) | unsigned(e.suspend_on_temp_radio) * 2
| unsigned(e.match_blacklisted_path) * 4
| unsigned(e.scope_uses_slow_timing) * 8 | unsigned(e.drop_on_match) * 16
| unsigned(e.rate_limit_enabled) * 32 | unsigned(e.stop_on_match) * 64
| unsigned(e.retry_on_match) * 128 | unsigned(e.rate_window_active) * 256;
}
template<class Entry> void exerciseFlags() {
// Exhaustive combinations detect adjacent flag corruption; copy/clear are
// the same operations used by production rule load, delete, and replace.
for (unsigned flags = 0; flags < 512; ++flags) {
Entry e{};
e.rate_window_started = 0xfedcba98;
e.rate_window_count = 0x7654;
e.rate_per_minute = 0x3210;
setFlags(e, flags);
assert(flagsOf(e) == flags);
assert(e.rate_window_started == 0xfedcba98);
assert(e.rate_window_count == 0x7654 && e.rate_per_minute == 0x3210);
Entry copied = e;
assert(flagsOf(copied) == flags);
std::memcpy(&copied, &e, sizeof(e));
assert(flagsOf(copied) == flags);
// These actual decoders require ordinary bool references. Packing must
// preserve this API and must not alter any unrelated flag.
assert(FloodFilterPolicy::decodeStoredRuleActive(5, copied.active,
copied.transport_modes));
assert(FloodFilterPolicy::decodeStoredRuleChannel(16, copied.channel_key_len,
copied.retry_on_match));
assert(flagsOf(copied) == ((flags | 1) & ~128U));
assert(copied.transport_modes == 4 && copied.channel_key_len == 16);
std::memset(&copied, 0, sizeof(copied));
assert(flagsOf(copied) == 0 && copied.rate_window_started == 0);
}
}
template<class Entry> void exerciseConfigurationEquality() {
Entry original{};
original.active = true;
original.rate_limit_enabled = true;
original.rate_per_minute = 5;
Entry live = original;
live.rate_window_started = 0xfedcba98;
live.rate_window_count = 3;
live.rate_window_active = true;
assert(FloodFilterPolicy::sameRuleConfiguration(original, live));
// Bytewise struct comparisons are forbidden: neither alignment padding nor
// live counters identify a rule. Poison trailing padding to enforce this.
for (size_t offset = offsetof(Entry, transport_modes) + 1; offset < sizeof(Entry); ++offset)
reinterpret_cast<unsigned char*>(&live)[offset] = 0xa5;
assert(FloodFilterPolicy::sameRuleConfiguration(original, live));
// Every configured field must still distinguish two rows. Arrays include
// their final byte so accidental prefix-only comparisons cannot pass.
for (unsigned field = 0; field < 26; ++field) {
Entry changed = original;
switch (field) {
case 0: changed.active = false; break;
case 1: changed.payload_type = 1; break;
case 2: changed.min_hops = 1; break;
case 3: changed.max_hops = 1; break;
case 4: changed.suspend_on_temp_radio = true; break;
case 5: changed.scope_name[sizeof(changed.scope_name)-1] = 1; break;
case 6: changed.match_blacklisted_path = true; break;
case 7: changed.scope_uses_slow_timing = true; break;
case 8: changed.incoming_scope_kind = 1; break;
case 9: changed.incoming_scope_name[sizeof(changed.incoming_scope_name)-1] = 1; break;
case 10: changed.channel_key_len = 1; break;
case 11: changed.channel_hash = 1; break;
case 12: changed.channel_secret[sizeof(changed.channel_secret)-1] = 1; break;
case 13: changed.channel_name[sizeof(changed.channel_name)-1] = 1; break;
case 14: changed.path_hash_size = 1; break;
case 15: changed.path_hops = 1; break;
case 16: changed.path[sizeof(changed.path)-1] = 1; break;
case 17: changed.target_region_name[sizeof(changed.target_region_name)-1] = 1; break;
case 18: changed.drop_on_match = true; break;
case 19: changed.rate_limit_enabled = false; break;
case 20: changed.rate_per_minute = 6; break;
case 21: changed.priority = 1; break;
case 22: changed.stop_on_match = true; break;
case 23: changed.retry_on_match = true; break;
case 24: changed.transport_modes = 2; break;
case 25: changed.scope_name[0] = '#'; break;
}
assert(!FloodFilterPolicy::sameRuleConfiguration(original, changed));
assert(!FloodFilterPolicy::sameRuleConfiguration(changed, original));
}
}
template<class Rules, class Entry> void exerciseProductionSlotSelection() {
Rules rules;
Entry candidate{};
candidate.active = true; candidate.payload_type = PAYLOAD_TYPE_ADVERT;
candidate.max_hops = 63; candidate.rate_limit_enabled = true;
candidate.rate_per_minute = 5;
assert(rules.select(candidate) == 0);
rules.storage[0] = candidate;
rules.storage[0].rate_window_started = 100;
rules.storage[0].rate_window_count = 3;
rules.storage[0].rate_window_active = true;
assert(rules.select(candidate) == 0); // Reuse despite a live rate window.
candidate.path_hash_size = 2;
assert(rules.select(candidate) == 1); // Distinct configuration needs a slot.
rules.storage[1] = candidate;
assert(rules.select(candidate) == 1);
assert(rules.select(candidate, 3) == 3); // Explicit index remains authoritative.
}
int main() {
exerciseFlags<RepeaterEntry>();
exerciseFlags<RoomEntry>();
exerciseConfigurationEquality<RepeaterEntry>();
exerciseConfigurationEquality<RoomEntry>();
exerciseProductionSlotSelection<RepeaterRules, RepeaterEntry>();
exerciseProductionSlotSelection<RoomRules, RoomEntry>();
}
'''
def production_layouts():
repeater = extract_braced(
(ROOT / 'examples/simple_repeater/MyMesh.h').read_text(),
'struct FloodPacketFilterEntry')
room = extract_braced(
(ROOT / 'examples/simple_room_server/FloodRuleEngine.h').read_text(),
'struct Entry')
return ('#define MESH_ENABLE_FLOOD_RULE_ENGINE 1\n'
+ repeater.replace('struct FloodPacketFilterEntry', 'struct RepeaterEntry')
+ ';\n' + room.replace('struct Entry', 'struct RoomEntry') + ';\n'
+ '#undef MESH_ENABLE_FLOOD_RULE_ENGINE\n'
+ '#define MESH_ENABLE_FLOOD_RULE_ENGINE 0\n'
+ repeater.replace('struct FloodPacketFilterEntry', 'struct LegacyEntry') + ';\n')
def production_slot_selection():
bodies = []
for role, marker, end, members in (
('simple_repeater', 'void MyMesh::setFloodPacketFilter(',
'\n if (slot < 0) {\n strcpy(reply, "Err - filter table full");',
'using FloodPacketFilterEntry = RepeaterEntry;\n'
'RepeaterEntry storage[63]{};\n'
'RepeaterEntry* flood_packet_filters = storage;\n'
'uint8_t flood_packet_filter_slots = 63, flood_channel_data_rule_slot = 0xff;'),
('simple_room_server', 'void FloodRuleEngine::set(',
'\n if (slot < 0) {\n copyString(reply, "Err - filter table full", 160);',
'using Entry = RoomEntry;\n'
'static constexpr uint8_t RULE_SLOTS = 31;\n'
'RoomEntry storage[RULE_SLOTS]{};\nEntry* _entries = storage;'),
):
file = 'MyMesh.cpp' if role == 'simple_repeater' else 'FloodRuleEngine.cpp'
method = extract_braced((ROOT / 'examples' / role / file).read_text(), marker)
# Compile both real setter slot-selection paths, not a test rewrite.
body = method[method.index(' int slot = requested_slot;'):method.index(end)]
name = 'RepeaterRules' if role == 'simple_repeater' else 'RoomRules'
entry = 'RepeaterEntry' if role == 'simple_repeater' else 'RoomEntry'
bodies.append('struct ' + name + ' {\n' + members
+ '\nint select(const ' + entry + '& candidate, int requested_slot=-1) {\n'
+ body + '\nreturn slot;\n}\n};\n')
return ''.join(bodies)
class FloodRuleLayoutTests(unittest.TestCase):
def test_packed_flags_keep_all_states_and_aligned_counters(self):
with tempfile.TemporaryDirectory(prefix='flood-rule-layout-') as temp:
path = Path(temp)
source = path / 'test.cpp'
source.write_text(PRELUDE + production_layouts() + production_slot_selection() + SCENARIOS)
exe = path / 'test'
subprocess.run(['g++', '-std=c++17', '-O1', '-g',
'-fsanitize=address,undefined', '-fno-omit-frame-pointer',
'-no-pie', '-I' + str(ROOT / 'src'), str(source),
'-o', str(exe)], check=True)
subprocess.run([str(exe)], check=True,
env={**os.environ, 'ASAN_OPTIONS': 'detect_leaks=1'})
def test_arm_layouts_keep_the_same_bound_and_natural_alignment(self):
compilers = sorted((Path.home() / '.platformio/packages').glob(
'toolchain-gccarmnoneeabi*/bin/arm-none-eabi-g++'))
if not compilers:
self.skipTest('ARM compiler is not installed')
# Syntax/type checks invoke the compiler directly, never PlatformIO.
for compiler in compilers:
with self.subTest(compiler=compiler.parent.parent.name):
subprocess.run([str(compiler), '-std=c++17', '-mcpu=cortex-m4',
'-mthumb', '-fsyntax-only', '-x', 'c++',
'-I' + str(ROOT / 'src'), '-'], check=True,
input=PRELUDE + production_layouts() + production_slot_selection() + SCENARIOS,
text=True)
if __name__ == '__main__':
unittest.main()
@@ -1,9 +1,88 @@
#include <gtest/gtest.h>
#include <cstring>
#include <new>
#include <type_traits>
#define MAX_RECENT_REPEATERS 8
#include "helpers/SimpleMeshTables.h"
using namespace mesh;
TEST(SimpleMeshTables, CompactRecentRowsPreserveFullMillisAtEveryByteAlignment) {
using Info = SimpleMeshTables::RecentRepeaterInfo;
static_assert(sizeof(Info) == 9 && alignof(Info) == 1, "compact row layout");
static_assert(std::is_trivially_copyable<Info>::value, "memset/cursor copy support");
constexpr uint32_t values[] = {0, 1, 0x0000ffff, 0x12345678, 0x80000000,
0xfffffffe, 0xffffffff};
for (size_t offset = 0; offset < alignof(uint32_t); ++offset) {
alignas(uint32_t) unsigned char bytes[sizeof(Info) + alignof(uint32_t)] = {};
Info* info = new (bytes + offset) Info{};
info->prefix[0] = 0x86;
info->prefix[1] = 0x0c;
info->prefix[2] = 0xca;
info->prefix_len = 3;
info->snr_x4 = -128;
for (uint32_t value : values) {
info->last_heard_millis = value;
EXPECT_EQ(value, uint32_t(info->last_heard_millis));
Info cursor;
SimpleMeshTables::copyRecentRepeaterInfo(cursor, *info);
EXPECT_EQ(value, uint32_t(cursor.last_heard_millis));
EXPECT_EQ(0x86, info->prefix[0]);
EXPECT_EQ(0x0c, info->prefix[1]);
EXPECT_EQ(0xca, info->prefix[2]);
EXPECT_EQ(3, info->prefix_len);
EXPECT_EQ(-128, info->snr_x4);
}
SimpleMeshTables::clearRecentRepeaterInfo(*info);
EXPECT_EQ(0u, uint32_t(info->last_heard_millis));
}
}
TEST(SimpleMeshTables, CompactRecentPrefixCopiesStayWithinThreeBytes) {
using Info = SimpleMeshTables::RecentRepeaterInfo;
alignas(uint32_t) unsigned char bytes[sizeof(Info) + alignof(uint32_t)] = {};
for (size_t offset = 0; offset < alignof(uint32_t); ++offset) {
Info* info = new (bytes + offset) Info{};
info->prefix[0] = 0x86;
info->prefix[1] = 0x0c;
info->prefix[2] = 0xca;
info->prefix_len = 3;
unsigned char out[] = {0xa5, 0xa5, 0xa5, 0xa5, 0xa5};
SimpleMeshTables::copyRecentRepeaterPrefix(out + 1, *info);
EXPECT_EQ(0xa5, out[0]);
EXPECT_EQ(0x86, out[1]);
EXPECT_EQ(0x0c, out[2]);
EXPECT_EQ(0xca, out[3]);
EXPECT_EQ(0xa5, out[4]);
EXPECT_EQ(0, routeHashPrefixBytesCompare(info->prefix, out + 1, 3));
out[3] = 0xc9;
EXPECT_GT(routeHashPrefixBytesCompare(info->prefix, out + 1, 3), 0);
EXPECT_LT(routeHashPrefixBytesCompare(out + 1, info->prefix, 3), 0);
}
}
TEST(SimpleMeshTables, CompactRecentExpiryPreservesUnsignedMillisRollover) {
SimpleMeshTables::RecentRepeaterInfo storage[3];
SimpleMeshTables table(storage, 3);
const uint8_t older[] = {0x10, 0x20, 0x30};
const uint8_t boundary[] = {0x11, 0x21, 0x31};
const uint8_t fresh[] = {0x12, 0x22, 0x32};
ASSERT_TRUE(table.setRecentRepeater(older, 3, 12));
ASSERT_TRUE(table.setRecentRepeater(boundary, 3, 8));
ASSERT_TRUE(table.setRecentRepeater(fresh, 3, 4));
storage[0].last_heard_millis = 0xffffffe0;
storage[1].last_heard_millis = 0xfffffff0;
storage[2].last_heard_millis = 8;
EXPECT_EQ(1, table.expireRecentRepeaters(16, 32));
EXPECT_EQ(nullptr, table.findRecentRepeaterByHash(older, 3));
ASSERT_NE(nullptr, table.findRecentRepeaterByHash(boundary, 3));
ASSERT_NE(nullptr, table.findRecentRepeaterByHash(fresh, 3));
EXPECT_EQ(0xfffffff0u, uint32_t(table.findRecentRepeaterByHash(boundary, 3)->last_heard_millis));
EXPECT_EQ(8u, uint32_t(table.findRecentRepeaterByHash(fresh, 3)->last_heard_millis));
EXPECT_EQ(2, table.getRecentRepeaterCount());
EXPECT_EQ(0u, uint32_t(storage[2].last_heard_millis));
}
// Build a packet that calculatePacketHash() distinguishes by payload content.
// header selects ROUTE_TYPE_FLOOD so isRouteDirect() returns false.
static Packet makeFloodPacket(uint8_t seed) {
@@ -362,7 +441,7 @@ TEST(SimpleMeshTables, CooperativeRecentCursorMatchesUnchangedRankOrder) {
rank == 0 ? nullptr : &cursor, cursor_index, found_index);
ASSERT_NE(nullptr, found);
EXPECT_EQ(t.getRecentRepeaterBySortedIdx(rank), found);
cursor = *found;
SimpleMeshTables::copyRecentRepeaterInfo(cursor, *found);
cursor_index = found_index;
}
int found_index = 123;
@@ -382,7 +461,8 @@ TEST(SimpleMeshTables, CooperativeRecentCursorSurvivesLiveStorageCompaction) {
int cursor_index = -1;
const auto* first_row = t.getNextRecentRepeaterBySortKey(nullptr, -1, cursor_index);
ASSERT_NE(nullptr, first_row);
const auto cursor = *first_row;
SimpleMeshTables::RecentRepeaterInfo cursor;
SimpleMeshTables::copyRecentRepeaterInfo(cursor, *first_row);
storage[0].last_heard_millis = 0;
storage[1].last_heard_millis = 100;
storage[2].last_heard_millis = 100;
@@ -411,7 +491,7 @@ TEST(SimpleMeshTables, CooperativeRecentCursorTraverses2048Rows) {
ASSERT_NE(nullptr, found);
EXPECT_EQ(static_cast<uint8_t>(i >> 8), found->prefix[1]);
EXPECT_EQ(static_cast<uint8_t>(i), found->prefix[2]);
cursor = *found;
SimpleMeshTables::copyRecentRepeaterInfo(cursor, *found);
cursor_index = found_index;
}
EXPECT_EQ(nullptr, t.getNextRecentRepeaterBySortKey(&cursor, cursor_index, cursor_index));
+43
View File
@@ -0,0 +1,43 @@
#!/usr/bin/env python3
"""Verify streaming proofs and real OTA cold-buffer lifetimes without PlatformIO."""
from pathlib import Path
import os
import shutil
import subprocess
import tempfile
import unittest
from test_serial_wifi_sessions import SANITIZER_FLAGS
ROOT = Path(__file__).resolve().parents[1]
FIXTURE = ROOT / "test/fixtures/ota_cold_buffers/test.cpp"
class OtaColdBufferTests(unittest.TestCase):
def test_proofs_allocation_failures_and_transfer_lifetimes(self):
compiler = os.environ.get("CXX") or shutil.which("g++") or shutil.which("clang++")
if not compiler:
self.skipTest("a host C++17 compiler is required")
for platform in ((), ("-DNRF52_PLATFORM=1",),
("-DNRF52_PLATFORM=1", "-DOTA_SD_STORE=1"),
("-DESP32_PLATFORM=1",)):
with self.subTest(platform=platform), tempfile.TemporaryDirectory(prefix="ota-cold-") as directory:
binary = Path(directory) / "cold-buffers"
sources = ("OtaManager.cpp", "OtaProtocol.cpp", "MotaContainer.cpp", "MerkleTree.cpp")
built = subprocess.run([
compiler, "-std=c++17", "-O1", "-Wall", "-Wextra", "-Werror",
"-Wno-unused-parameter", "-Wno-sign-compare", *SANITIZER_FLAGS, *platform,
"-I", str(ROOT / "src"), "-I", str(ROOT / "test/mocks"),
str(FIXTURE), *[str(ROOT / "src/helpers/ota" / name) for name in sources],
str(ROOT / "src/Utils.cpp"), "-Wl,--wrap=malloc", "-Wl,--wrap=free",
"-o", str(binary),
], capture_output=True, text=True, timeout=60)
self.assertEqual(built.returncode, 0, built.stdout + built.stderr)
checked = subprocess.run([str(binary)], capture_output=True, text=True, timeout=60)
self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr)
self.assertIn("PASS: streaming proofs and OTA cold-buffer lifetimes", checked.stdout)
if __name__ == "__main__":
unittest.main()
+231
View File
@@ -0,0 +1,231 @@
#!/usr/bin/env python3
"""Exercise real recent-table clock behavior and alignment-safe ARM access."""
from pathlib import Path
import os
import re
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
ARDUINO_CASES = r'''
#include <cassert>
#include <cstdint>
#include <cstring>
#include <initializer_list>
#include <new>
#include <type_traits>
#include "helpers/SimpleMeshTables.h"
using Info = SimpleMeshTables::RecentRepeaterInfo;
static_assert(sizeof(Info) == 9 && alignof(Info) == 1, "compact ARM-compatible rows");
static_assert(std::is_trivially_copyable<Info>::value, "clear/compaction/cursor API");
int main() {
// A full table must evict the oldest learned time, including a millis wrap.
Info rows[3];
SimpleMeshTables table(rows, 3);
const uint8_t a[] = {0x10, 0x20, 0x30}, b[] = {0x11, 0x21, 0x31};
const uint8_t c[] = {0x12, 0x22, 0x32}, d[] = {0x13, 0x23, 0x33};
g_mock_millis = 0xffffff00;
assert(table.setRecentRepeater(a, 3, -128));
g_mock_millis = 0xffffff80;
assert(table.setRecentRepeater(b, 3, 127));
g_mock_millis = 0x10;
assert(table.setRecentRepeater(c, 3, 4));
g_mock_millis = 0x20;
assert(table.setRecentRepeater(d, 3, 8));
assert(!table.findRecentRepeaterByHash(a, 3));
assert(table.findRecentRepeaterByHash(b, 3));
assert(table.findRecentRepeaterByHash(c, 3));
assert(table.findRecentRepeaterByHash(d, 3));
assert(table.getRecentRepeaterCount() == 3);
assert(uint32_t(table.findRecentRepeaterByHash(b, 3)->last_heard_millis) == 0xffffff80);
assert(uint32_t(table.findRecentRepeaterByHash(d, 3)->last_heard_millis) == 0x20);
// An update must refresh the full timestamp and keep the SNR blending rule.
g_mock_millis = 0xfedcba98;
assert(table.setRecentRepeater(b, 3, -128));
Info saved;
SimpleMeshTables::copyRecentRepeaterInfo(saved, *table.findRecentRepeaterByHash(b, 3));
assert(uint32_t(saved.last_heard_millis) == 0xfedcba98);
assert(saved.snr_x4 == 64); // ceil((127*3 - 128)/4)
assert(table.expireRecentRepeaters(0xfedcbaa0, 8) == 2);
assert(table.getRecentRepeaterCount() == 1);
assert(uint32_t(rows[0].last_heard_millis) == 0xfedcba98);
assert(uint32_t(rows[1].last_heard_millis) == 0);
assert(uint32_t(rows[2].last_heard_millis) == 0);
assert(uint32_t(saved.last_heard_millis) == 0xfedcba98);
table.clearRecentRepeaters();
assert(table.getRecentRepeaterCount() == 0);
assert(uint32_t(rows[0].last_heard_millis) == 0);
// Every board capacity retains every unique row with no alignment padding.
for (const size_t capacity : {size_t(64), size_t(256), size_t(512), size_t(2048)}) {
Info* storage = new Info[capacity];
SimpleMeshTables full(storage, capacity);
for (size_t i = 0; i < capacity; ++i) {
const uint8_t prefix[] = {0x80, uint8_t(i >> 8), uint8_t(i)};
g_mock_millis = 0xff000001u + uint32_t(i);
assert(full.setRecentRepeater(prefix, 3, int8_t(i % 128)));
}
assert(full.getRecentRepeaterCount() == int(capacity));
for (size_t i = 0; i < capacity; ++i) {
const uint8_t prefix[] = {0x80, uint8_t(i >> 8), uint8_t(i)};
const Info* found = full.findRecentRepeaterByHash(prefix, 3);
assert(found && uint32_t(found->last_heard_millis) == 0xff000001u + uint32_t(i));
assert(found->snr_x4 == int8_t(i % 128));
}
delete[] storage;
}
// Roles which do not supply a history keep the zero-capacity behavior.
SimpleMeshTables no_storage;
assert(!no_storage.setRecentRepeater(a, 3, 12));
assert(!no_storage.findRecentRepeaterByHash(a, 3));
assert(no_storage.expireRecentRepeaters(0xffffffff, 1) == 0);
assert(no_storage.getRecentRepeaterCount() == 0);
}
'''
def run_checked(case, args, **kwargs):
result = subprocess.run(args, capture_output=True, text=True, timeout=60, **kwargs)
case.assertEqual(result.returncode, 0, result.stdout + result.stderr)
return result
class RecentRepeaterStorageTests(unittest.TestCase):
def test_real_table_arduino_clock_and_all_board_capacities(self):
with tempfile.TemporaryDirectory() as directory:
cpp = Path(directory) / "recent.cpp"
cpp.write_text(ARDUINO_CASES)
for sanitizer in (False, True):
with self.subTest(sanitizer=sanitizer):
binary = Path(directory) / ("recent-san" if sanitizer else "recent")
flags = (["-fsanitize=address,undefined", "-fno-sanitize-recover=all",
"-fno-pie", "-no-pie"] if sanitizer else [])
run_checked(self, [
"g++", "-std=c++17", "-O2", "-Wall", "-Wextra", "-Werror",
"-Wno-unused-parameter", "-Wno-sign-compare", "-Wno-reorder",
"-DARDUINO=1", *flags, "-I", str(ROOT / "src"),
"-I", str(ROOT / "test/mocks"), str(cpp),
str(ROOT / "src/Packet.cpp"), "-o", str(binary)])
run_checked(self, [str(binary)])
def test_actual_row_arm_copy_clear_prefix_and_timestamp_accesses_are_bytes(self):
compiler = shutil.which("arm-none-eabi-g++")
package_dir = Path(os.environ.get("PLATFORMIO_CORE_DIR",
str(Path.home() / ".platformio"))) / "packages"
compilers = set(package_dir.glob("toolchain-gccarmnoneeabi*/bin/arm-none-eabi-g++"))
if compiler:
compilers.add(Path(compiler))
if not compilers:
if os.environ.get("MESHCORE_REQUIRE_ARM_COMPILER") == "1":
self.fail("ARM compiler required for compact recent-row alignment checks")
self.skipTest("ARM code generation additionally checked by firmware qualification")
header = (ROOT / "src/helpers/SimpleMeshTables.h").read_text()
start = header.index(" struct RecentRepeaterMillis {")
end = header.index("\nprivate:", start)
# Extract the actual definition: do not duplicate the production wrapper.
source = "#include <stdint.h>\n#include <stddef.h>\n#include <type_traits>\n#define MAX_ROUTE_HASH_BYTES 3\n"
prefix_start = header.index("inline int routeHashPrefixBytesCompare(")
prefix_end = header.index("\ninline bool routeHashPrefixesOverlap(", prefix_start)
source += header[prefix_start:prefix_end] + "\n"
source += "struct RecentRows {\n" + header[start:end] + "\n};\n"
source += r'''
extern "C" __attribute__((noinline)) uint32_t read_recent(
const RecentRows::RecentRepeaterInfo* rows, unsigned i) {
return rows[i].last_heard_millis;
}
extern "C" __attribute__((noinline)) void write_recent(
RecentRows::RecentRepeaterInfo* rows, unsigned i, uint32_t value) {
rows[i].last_heard_millis = value;
}
extern "C" __attribute__((noinline, flatten)) void copy_recent(
RecentRows::RecentRepeaterInfo* out, const RecentRows::RecentRepeaterInfo* in) {
RecentRows::copyRecentRepeaterInfo(*out, *in);
}
extern "C" __attribute__((noinline, flatten)) void clear_recent(
RecentRows::RecentRepeaterInfo* out) {
RecentRows::clearRecentRepeaterInfo(*out);
}
extern "C" __attribute__((noinline, flatten)) void clear_history(
RecentRows::RecentRepeaterInfo* out) {
for (unsigned i = 0; i < 512; ++i) RecentRows::clearRecentRepeaterInfo(out[i]);
}
extern "C" __attribute__((noinline, flatten)) void copy_prefix(
uint8_t* out, const RecentRows::RecentRepeaterInfo* in) {
RecentRows::copyRecentRepeaterPrefix(out, *in);
}
extern "C" __attribute__((noinline, flatten)) int compare_prefix(
const RecentRows::RecentRepeaterInfo* a, const RecentRows::RecentRepeaterInfo* b) {
return routeHashPrefixBytesCompare(a->prefix, b->prefix, 3);
}
extern "C" __attribute__((noinline, flatten)) uint8_t read_prefix_length(
const RecentRows::RecentRepeaterInfo* rows, unsigned i) {
return rows[i].prefix_len;
}
extern "C" __attribute__((noinline, flatten)) int8_t read_snr(
const RecentRows::RecentRepeaterInfo* rows, unsigned i) {
return rows[i].snr_x4;
}
RecentRows::RecentRepeaterInfo history[512];
static_assert(sizeof(history) == 4608, "all 512 rows stay available");
'''
# Prefix writes must also use the actual production implementation.
write_start = header.index(" volatile uint8_t* out_prefix = slot.prefix;")
write_end = header.index(" slot.prefix_len = prefix_len;", write_start)
source += 'extern "C" __attribute__((noinline, flatten)) void write_prefix(\n'
source += " RecentRows::RecentRepeaterInfo& slot, const uint8_t* prefix, uint8_t prefix_len) {\n"
source += header[write_start:write_end] + "}\n"
with tempfile.TemporaryDirectory() as directory:
cpp = Path(directory) / "arm-recent.cpp"
cpp.write_text(source)
for compiler_path in sorted(compilers):
for cpu in ("cortex-m0", "cortex-m4"):
for optimization in ("-Os", "-O2"):
with self.subTest(compiler=str(compiler_path), cpu=cpu, optimization=optimization):
asm = Path(directory) / "arm-recent.s"
run_checked(self, [str(compiler_path), "-std=c++11", "-mcpu=" + cpu,
"-mthumb", optimization, "-Wall", "-Wextra", "-Werror",
"-S", str(cpp), "-o", str(asm)])
generated = asm.read_text()
for function, operation in (("read_recent", "ldrb"), ("write_recent", "strb")):
body = generated.split(function + ":", 1)[1].split(".size", 1)[0]
self.assertEqual(len(re.findall(r"\b" + operation + r"(?:\.w)?\s", body)), 4,
body)
self.assertNotRegex(body, r"\b(?:ldr|str|ldrh|strh|ldrd|strd|ldm\w*|stm\w*)(?:\.w)?\s")
for function, operation in (("copy_recent", "ldrb"), ("clear_recent", "strb"),
("clear_history", "strb"), ("copy_prefix", "ldrb"),
("compare_prefix", "ldrb"), ("write_prefix", "strb"),
("read_prefix_length", "ldrb"), ("read_snr", "ldr(?:s)?b")):
body = generated.split(function + ":", 1)[1].split(".size", 1)[0]
# Literal-pool LDR and stack PUSH/POP are naturally
# aligned; loads/stores through row pointers must be bytes.
self.assertRegex(body, r"\b" + operation + r"(?:\.w)?\s", body)
self.assertNotRegex(body, r"\b(?:ldr|str|ldrh|strh|ldrd|strd)(?:\.w)?\s[^\n]*\[")
self.assertNotRegex(body, r"\b(?:ldm\w*|stm\w*)\s")
self.assertNotRegex(body, r"\b(?:memcpy|memset|memcmp)\b")
def test_actual_compaction_and_console_callers_use_byte_helpers(self):
header = (ROOT / "src/helpers/SimpleMeshTables.h").read_text()
source = (ROOT / "examples/simple_repeater/MyMesh.cpp").read_text()
self.assertIn("copyRecentRepeaterInfo(info, _recent_repeaters[_recent_repeater_count])", header)
self.assertIn("clearRecentRepeaterInfo(_recent_repeaters[_recent_repeater_count])", header)
self.assertNotIn("memset(_recent_repeaters", header)
self.assertNotIn("memcpy(slot.prefix", header)
self.assertNotIn("memcmp(info->prefix", header)
self.assertIn("copyRecentRepeaterInfo(serial_recent_cursor, *info)", source)
self.assertNotIn("serial_recent_cursor = *info", source)
self.assertNotIn("toHex(prefix, info->prefix", source)
self.assertEqual(source.count("copyRecentRepeaterPrefix(recorded_prefix, *info)"), 4)
self.assertEqual(source.count("alignas(uint32_t) uint8_t recorded_prefix[MAX_ROUTE_HASH_BYTES]"), 4)
if __name__ == "__main__":
unittest.main()
@@ -426,6 +426,37 @@ TEST_F(TransportFilter, ShorthandAddReusesLongRuleAndDeletePersists) {
EXPECT_FALSE(rules.handleCommand(text, reply));
}
TEST_F(TransportFilter, UnnumberedRulesKeepDistinctConfigurations) {
command("del fr all");
command("set fr any pb=1 d");
ASSERT_EQ(0, strncmp(reply, "OK", 2)) << reply;
command("set fr any pb=2 d");
ASSERT_EQ(0, strncmp(reply, "OK", 2)) << reply;
command("get fr.1"); EXPECT_STREQ("set fr.1 any pb=1 d", reply);
command("get fr.2"); EXPECT_STREQ("set fr.2 any pb=2 d", reply);
rules.begin(&fs, nullptr);
command("get fr.1"); EXPECT_STREQ("set fr.1 any pb=1 d", reply);
command("get fr.2"); EXPECT_STREQ("set fr.2 any pb=2 d", reply);
}
TEST_F(TransportFilter, UnnumberedDuplicateReusesRuleWithLiveRateWindow) {
command("del fr all");
command("set fr any pb=2 q=1");
ASSERT_EQ(0, strncmp(reply, "OK", 2)) << reply;
auto p = packet();p.setPathHashSizeAndCount(2, 0);
const auto mask = rules.evaluate(&p, false, true, nullptr, RULE_MODE_RADIO);
ASSERT_NE(0U, mask);
EXPECT_FALSE(rules.shouldBlock(&p, mask, 100));
rules.commitRates(&p, mask, 100);
EXPECT_TRUE(rules.shouldBlock(&p, mask, 100));
const auto saved = fs.files["/flood_filter"];
command("set fr any pb=2 q=1");
ASSERT_EQ(0, strncmp(reply, "OK", 2)) << reply;
EXPECT_EQ(saved, fs.files["/flood_filter"]);
command("get fr.1"); EXPECT_STREQ("set fr.1 any pb=2 q=1", reply);
command("get fr.2"); EXPECT_EQ(0, strncmp(reply, "Err", 3));
}
TEST_F(TransportFilter, OversizedCompactReplyNeverReturnsPartialSetter) {
// A valid short input can fit in 192 command bytes but exceed a 160-byte reply.
const std::string channel = "#" + std::string(30, 'b'), name(30, 'a');