## Summary PoC-A proved that dynamically loaded ELF plugins are workable on ESP32-S3: plugins ship as signed MPB packages, are parsed by a strict single-type parser shared between host and target, and execute from PSRAM+IRAM within a measured admission budget. A 1000-cycle load/unload soak passed 18/18 trend gates with stable heaps, the capacity ladder settled at 8 coexisting plugin instances with a 3356B per-instance budget, and the fail-closed corpus executed zero attacker-controlled bytes across 14 rejection classes. Scheduler semantics (strand FIFO ordering, completion-cancel CAS, quiesce-as-noop ABI) were validated on both host (TSan suite) and target. **Gate-A verdict: PASS** — ADR-0002 moves to **Accepted** with the capacity value 8 and global-sum admission rule as PoC-derived proposals. ## Changes by area Grouped from `git log main..HEAD --oneline` (18 commits): - **Vendored loader + patches**: `4bb53f2` vendor elf_loader 1.3.3 + harness scaffold; `2f67b4f` plugin_iram loader patch with budget consistency (p1-p5 patch series in `PATCHES.md`). - **MPB toolchain**: `4793cc2` deterministic MPB manifest builder with test signing; `408df44` strict single-type MPB parser shared host/target; `796f9d2` host MPB corpus + policy CI lane. - **Plugin pipeline + probes**: `21d2842` plugin pipeline and first PSRAM execution on target; `cab9664` relocation/import allowlist matrix with probes; `c5d6c6c` C++ feature rejection matrix. - **Harness commands**: `e0e7159` generation coexistence with measured admission budget; `2a37556` thousand-cycle lifecycle soak with heap trend gate; `093a72b` on-target scheduler semantics validation (+ `eea3876` clang-format-18 sweep); `a302151` capacity ladder with signed budget; `f34dc72` fail-closed corpus with execution sentinel. - **Host prototypes**: `6198bca` strand FIFO prototype with TSan suite; `1c3ad59` completion-cancel CAS and cleanup semantics. - **Evidence + ADR/docs**: `887d0f7` Gate-A manifest and report; `1c29e8a` ADR-0002 outcome + frozen PoC values. ## QA evidence - `poc/evidence/poc-a-20260901/manifest.json` — 67-artifact hash index, every sha256 recomputed at assembly. - `poc/evidence/poc-a-20260901/GATE-A-REPORT.md` — full nine-item development-plan §4 mapping; TEST-003 10/10 clauses. - 14 verbatim per-task transcripts (`task-1` … `task-15-*.log`) + fresh build artifacts (`build/frame_poc_a.map`, size components). - Frozen in-repo references: `ALLOWLIST.md`, `REJECT-MATRIX.md`, `ADMISSION-BUDGET.md`, `soak_samples_1000.csv`, fc corpus (33 committed `.mpb` containers). - F1-F4 final-wave verdicts: **APPROVE**. - Key numbers: soak **18/18** trend gates over 1000 cycles; capacity **8/8** instances, **3356B** per-instance; fc corpus **14 rejection classes, zero attacker bytes executed**; **4 upstream loader defects found and patched** (p1-p5). ## Requirement IDs (GOV-006) ADR-0002; TEST-002, TEST-003; PLUG-001, PLUG-004, PLUG-008, PLUG-009; MEM-001, MEM-003, MEM-007, MEM-008; LIFE-001, LIFE-002, LIFE-005, LIFE-007; SEC-002, SEC-003, SEC-004, SEC-005; GH-003; GOV-004, GOV-005. ## Risks / known limitations From `GATE-A-REPORT.md` §(f) plus the trade-off table: 1. **Capacity descent path 7-5 unexercised** — rung 8 passed first attempt; rungs 7/6/5 and retry-once logic never ran on hardware (untested against real OOM). 2. **No Wi-Fi in the harness (MEM-001 caveat)** — all PSRAM/IRAM budget numbers are radio-off harness-class; product admission budgets must be re-measured with the network stack active. 3. **On-target scheduler vectors are a subset of the host suite** (256-node pool / 2e4 storm posts vs host 31000 / 1e5); consistency declaration covers only the vectors actually run. 4. **Quiesce is a no-op by ABI semantics** — plugin ABI 1.2 has no quiesce slot; NULL entry is defined as trivial success; real quiesce coordination is later-gate work. 5. **MEMPROT=n trade-off** — `CONFIG_ESP_SYSTEM_MEMPROT=n` is required for the IDF exec heap (`MALLOC_CAP_EXEC`) that `.plugin_iram` needs; software-only PMP config, no eFuse burned (documented in `PATCHES.md` p5); production must re-decide security vs plugin IRAM. 6. **Owner sign-off PENDING** — Gate-A PASS is the PoC team's verdict; formal acceptance of ADR-0002 awaits the owner. ## Not in scope - Product components (GOV-004) — this PR is PoC evidence only; no product code paths change. - **Merge is the owner's call** — do not merge without owner sign-off on ADR-0002. --------- Co-authored-by: ania-lain <[email protected]>
360 lines
13 KiB
C++
360 lines
13 KiB
C++
#include <atomic>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "frame_poc/frame_strand.hh"
|
|
|
|
using frame::poc::io_core;
|
|
using frame::poc::strand;
|
|
|
|
namespace {
|
|
|
|
constexpr auto kPollDelay = std::chrono::microseconds{50};
|
|
|
|
bool wait_until(const std::function<bool()>& predicate, std::chrono::milliseconds timeout) {
|
|
const auto deadline = std::chrono::steady_clock::now() + timeout;
|
|
while (!predicate()) {
|
|
if (std::chrono::steady_clock::now() > deadline) {
|
|
return false;
|
|
}
|
|
std::this_thread::sleep_for(kPollDelay);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
struct ticket_cell {
|
|
std::vector<uint64_t> observed; // written only by the worker, read after destroy gate
|
|
};
|
|
|
|
struct ticket_payload {
|
|
ticket_cell* cell;
|
|
uint64_t ticket;
|
|
};
|
|
|
|
struct concurrency_guard {
|
|
std::atomic<bool> in_flight[2] = {false, false};
|
|
std::atomic<bool> fault{false};
|
|
std::atomic<bool> first{true};
|
|
std::atomic<bool> id_mismatch{false};
|
|
std::thread::id worker{}; // touched only by the single worker thread
|
|
};
|
|
|
|
struct slot_payload {
|
|
concurrency_guard* guard;
|
|
int slot;
|
|
};
|
|
|
|
struct flag_payload {
|
|
std::atomic<bool>* invoked;
|
|
std::atomic<bool>* destroyed;
|
|
};
|
|
|
|
struct slow_payload {
|
|
std::chrono::milliseconds nap;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
// T11 (1): one strand, 3 producer threads x 10k posts each. Per-producer
|
|
// ticket order must be strictly increasing in the handler (FIFO + 4.1.2
|
|
// linearization at the strand mutex).
|
|
TEST(FrameStrand, StrictFifoOrderAcrossProducers) {
|
|
io_core core(1);
|
|
strand s(core, 31000);
|
|
constexpr int kProducers = 3;
|
|
constexpr uint64_t kPerProducer = 10000;
|
|
std::vector<ticket_cell> cells(kProducers);
|
|
std::vector<ticket_payload> payloads;
|
|
payloads.reserve(static_cast<size_t>(kProducers) * kPerProducer);
|
|
for (int producer = 0; producer < kProducers; ++producer) {
|
|
for (uint64_t ticket = 0; ticket < kPerProducer; ++ticket) {
|
|
payloads.push_back(ticket_payload{&cells[static_cast<size_t>(producer)], ticket});
|
|
}
|
|
}
|
|
core.start();
|
|
|
|
std::vector<std::thread> producers;
|
|
producers.reserve(kProducers);
|
|
for (int producer = 0; producer < kProducers; ++producer) {
|
|
producers.emplace_back([&, producer] {
|
|
for (uint64_t ticket = 0; ticket < kPerProducer; ++ticket) {
|
|
ticket_payload& payload =
|
|
payloads[static_cast<size_t>(producer) * kPerProducer + ticket];
|
|
frame_post_operation_t op{};
|
|
op.user = &payload;
|
|
op.invoke = [](void* user) {
|
|
auto* item = static_cast<ticket_payload*>(user);
|
|
item->cell->observed.push_back(item->ticket);
|
|
};
|
|
op.destroy = [](void*) {};
|
|
ASSERT_EQ(s.post(op), FRAME_OK);
|
|
}
|
|
});
|
|
}
|
|
for (auto& producer : producers) {
|
|
producer.join();
|
|
}
|
|
|
|
const uint64_t total = static_cast<uint64_t>(kProducers) * kPerProducer;
|
|
ASSERT_TRUE(wait_until([&] { return s.destroyed_count() == total; }, std::chrono::seconds{30}));
|
|
for (int producer = 0; producer < kProducers; ++producer) {
|
|
const auto& observed = cells[static_cast<size_t>(producer)].observed;
|
|
ASSERT_EQ(observed.size(), kPerProducer);
|
|
for (uint64_t index = 0; index < kPerProducer; ++index) {
|
|
ASSERT_EQ(observed[index], index)
|
|
<< "producer " << producer << " ticket order broken at " << index;
|
|
}
|
|
}
|
|
EXPECT_EQ(s.invoked_count(), total);
|
|
core.stop();
|
|
}
|
|
|
|
// T11 (2): same-strand handlers never overlap (in-flight CAS guard) and all
|
|
// handlers run on one worker thread (LIFE-001 serialization).
|
|
TEST(FrameStrand, SameStrandHandlersNeverConcurrent) {
|
|
io_core core(2);
|
|
strand strands[2]{strand(core, 512), strand(core, 512)};
|
|
concurrency_guard guard;
|
|
slot_payload payloads[2]{{&guard, 0}, {&guard, 1}};
|
|
core.start();
|
|
|
|
auto launch = [&](int slot, int count) {
|
|
return std::thread([&, this_slot = slot, this_count = count] {
|
|
strand& target = strands[this_slot];
|
|
for (int index = 0; index < this_count; ++index) {
|
|
frame_post_operation_t op{};
|
|
op.user = &payloads[this_slot];
|
|
op.invoke = [](void* user) {
|
|
auto* item = static_cast<slot_payload*>(user);
|
|
auto* shared = item->guard;
|
|
const int slot = item->slot;
|
|
bool expected = false;
|
|
if (!shared->in_flight[slot].compare_exchange_strong(expected, true)) {
|
|
shared->fault.store(true);
|
|
}
|
|
const std::thread::id current = std::this_thread::get_id();
|
|
if (shared->first.exchange(false)) {
|
|
shared->worker = current;
|
|
} else if (current != shared->worker) {
|
|
shared->id_mismatch.store(true);
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::microseconds{20});
|
|
shared->in_flight[slot].store(false);
|
|
};
|
|
op.destroy = [](void*) {};
|
|
while (target.post(op) == FRAME_ERR_QUEUE_FULL) {
|
|
std::this_thread::yield();
|
|
}
|
|
}
|
|
});
|
|
};
|
|
std::vector<std::thread> threads;
|
|
for (int index = 0; index < 2; ++index) {
|
|
threads.push_back(launch(0, 400));
|
|
threads.push_back(launch(1, 400));
|
|
}
|
|
for (auto& thread : threads) {
|
|
thread.join();
|
|
}
|
|
|
|
const uint64_t total = 1600;
|
|
ASSERT_TRUE(wait_until(
|
|
[&] { return strands[0].destroyed_count() + strands[1].destroyed_count() == total; },
|
|
std::chrono::seconds{30}));
|
|
EXPECT_FALSE(guard.fault.load());
|
|
EXPECT_FALSE(guard.id_mismatch.load());
|
|
core.stop();
|
|
}
|
|
|
|
// T11 (3): the ready-ring duplicate detector stays clear under multi-producer
|
|
// stress against a single strand (max-once invariant, 4.1.3).
|
|
TEST(FrameStrand, ReadyQueueNeverDuplicatesStrand) {
|
|
io_core core(1);
|
|
strand s(core, 256);
|
|
constexpr int kProducers = 8;
|
|
constexpr int kPerProducer = 5000;
|
|
core.start();
|
|
|
|
auto producer = [&] {
|
|
for (int index = 0; index < kPerProducer; ++index) {
|
|
frame_post_operation_t op{};
|
|
op.user = nullptr;
|
|
op.invoke = [](void*) {};
|
|
op.destroy = [](void*) {};
|
|
while (s.post(op) == FRAME_ERR_QUEUE_FULL) {
|
|
std::this_thread::yield();
|
|
}
|
|
}
|
|
};
|
|
std::vector<std::thread> threads;
|
|
threads.reserve(kProducers);
|
|
for (int index = 0; index < kProducers; ++index) {
|
|
threads.emplace_back(producer);
|
|
}
|
|
for (auto& thread : threads) {
|
|
thread.join();
|
|
}
|
|
const uint64_t total = static_cast<uint64_t>(kProducers) * kPerProducer;
|
|
ASSERT_TRUE(wait_until([&] { return s.destroyed_count() == total; }, std::chrono::seconds{30}));
|
|
EXPECT_FALSE(core.duplicate_detected());
|
|
EXPECT_EQ(s.invoked_count(), total);
|
|
core.stop();
|
|
}
|
|
|
|
// T11 (4): producer storm vs drain with a tiny credit window (capacity 64):
|
|
// after 1e5 accepted posts every operation must be invoked and destroyed —
|
|
// a lost wakeup would strand FIFO nodes forever (4.1.3 anti-lost-wakeup).
|
|
TEST(FrameStrand, NoLostWakeupUnderProducerStorm) {
|
|
io_core core(1);
|
|
strand s(core, 64);
|
|
constexpr int kProducers = 4;
|
|
constexpr uint64_t kTotal = 100000;
|
|
std::atomic<uint64_t> accepted{0};
|
|
core.start();
|
|
|
|
auto producer = [&] {
|
|
for (;;) {
|
|
if (accepted.load() >= kTotal) {
|
|
return;
|
|
}
|
|
frame_post_operation_t op{};
|
|
op.user = nullptr;
|
|
op.invoke = [](void*) {};
|
|
op.destroy = [](void*) {};
|
|
while (s.post(op) == FRAME_ERR_QUEUE_FULL) {
|
|
std::this_thread::yield();
|
|
}
|
|
accepted.fetch_add(1);
|
|
}
|
|
};
|
|
std::vector<std::thread> threads;
|
|
threads.reserve(kProducers);
|
|
for (int index = 0; index < kProducers; ++index) {
|
|
threads.emplace_back(producer);
|
|
}
|
|
for (auto& thread : threads) {
|
|
thread.join();
|
|
}
|
|
// The claim check races, so the exact accepted total can exceed kTotal by
|
|
// up to kProducers-1; every accepted post must still be invoked+destroyed.
|
|
const uint64_t total = accepted.load(std::memory_order_acquire);
|
|
ASSERT_GE(total, kTotal);
|
|
ASSERT_TRUE(wait_until([&] { return s.invoked_count() == total; }, std::chrono::seconds{60}));
|
|
ASSERT_TRUE(wait_until([&] { return s.destroyed_count() == total; }, std::chrono::seconds{60}));
|
|
s.begin_quiescing();
|
|
ASSERT_TRUE(wait_until([&] { return s.stopped(); }, std::chrono::seconds{10}));
|
|
EXPECT_EQ(s.stale_generation_dropped(), 0u);
|
|
core.stop();
|
|
}
|
|
|
|
// T11 (5): credit exhaustion returns FRAME_ERR_QUEUE_FULL and the caller
|
|
// keeps ownership (destroy NOT called on reject, 4.1.2).
|
|
TEST(FrameStrand, QueueFullRejectKeepsCallerOwnership) {
|
|
io_core core(1);
|
|
strand s(core, 8);
|
|
std::vector<std::atomic<bool>> invoked_flags(8);
|
|
std::atomic<bool> reject_destroyed{false};
|
|
for (auto& flag : invoked_flags) {
|
|
flag.store(false);
|
|
}
|
|
|
|
for (auto& flag : invoked_flags) {
|
|
frame_post_operation_t op{};
|
|
op.user = &flag;
|
|
op.invoke = [](void*) {};
|
|
op.destroy = [](void* user) { static_cast<std::atomic<bool>*>(user)->store(true); };
|
|
ASSERT_EQ(s.post(op), FRAME_OK);
|
|
}
|
|
frame_post_operation_t overflow{};
|
|
overflow.user = &reject_destroyed;
|
|
overflow.invoke = [](void*) {};
|
|
overflow.destroy = [](void* user) { static_cast<std::atomic<bool>*>(user)->store(true); };
|
|
ASSERT_EQ(s.post(overflow), FRAME_ERR_QUEUE_FULL);
|
|
ASSERT_FALSE(reject_destroyed.load());
|
|
|
|
core.start();
|
|
ASSERT_TRUE(wait_until([&] { return s.destroyed_count() == 8; }, std::chrono::seconds{10}));
|
|
EXPECT_EQ(s.invoked_count(), 8u);
|
|
EXPECT_FALSE(reject_destroyed.load());
|
|
for (const auto& flag : invoked_flags) {
|
|
EXPECT_TRUE(flag.load());
|
|
}
|
|
core.stop();
|
|
}
|
|
|
|
// T11 (6): a handler slower than the budget bumps handler_overruns
|
|
// (LIFE-002 metric only — no preemption, no isolation).
|
|
TEST(FrameStrand, OverrunCounterIncrementsOnSlowHandler) {
|
|
io_core core(1);
|
|
strand s(core, 8);
|
|
core.start();
|
|
|
|
frame_post_operation_t fast{};
|
|
fast.user = nullptr;
|
|
fast.invoke = [](void*) {};
|
|
fast.destroy = [](void*) {};
|
|
ASSERT_EQ(s.post(fast), FRAME_OK);
|
|
ASSERT_TRUE(wait_until([&] { return s.destroyed_count() == 1; }, std::chrono::seconds{10}));
|
|
EXPECT_EQ(s.handler_overruns(), 0u);
|
|
|
|
slow_payload slow{std::chrono::milliseconds{3}};
|
|
for (int index = 0; index < 3; ++index) {
|
|
frame_post_operation_t op{};
|
|
op.user = &slow;
|
|
op.invoke = [](void* user) {
|
|
std::this_thread::sleep_for(static_cast<slow_payload*>(user)->nap);
|
|
};
|
|
op.destroy = [](void*) {};
|
|
ASSERT_EQ(s.post(op), FRAME_OK);
|
|
}
|
|
ASSERT_TRUE(wait_until([&] { return s.destroyed_count() == 4; }, std::chrono::seconds{10}));
|
|
EXPECT_EQ(s.handler_overruns(), 3u);
|
|
core.stop();
|
|
}
|
|
|
|
// T11 (7): posts are rejected with FRAME_ERR_PLUGIN_STOPPING once the strand
|
|
// is closing, and queued un-run posts are cleaned up (invoke skipped,
|
|
// destroy exactly once, LIFE-007).
|
|
TEST(FrameStrand, ClosingRejectsPostsAndCleansUp) {
|
|
io_core core(1);
|
|
strand s(core, 4);
|
|
std::vector<std::atomic<bool>> invoked_flags(2);
|
|
std::vector<std::atomic<bool>> destroy_flags(2);
|
|
for (size_t index = 0; index < 2; ++index) {
|
|
invoked_flags[index].store(false);
|
|
destroy_flags[index].store(false);
|
|
}
|
|
std::vector<flag_payload> payloads;
|
|
for (size_t index = 0; index < 2; ++index) {
|
|
payloads.push_back(flag_payload{&invoked_flags[index], &destroy_flags[index]});
|
|
}
|
|
for (auto& payload : payloads) {
|
|
frame_post_operation_t op{};
|
|
op.user = &payload;
|
|
op.invoke = [](void* user) { static_cast<flag_payload*>(user)->invoked->store(true); };
|
|
op.destroy = [](void* user) { static_cast<flag_payload*>(user)->destroyed->store(true); };
|
|
ASSERT_EQ(s.post(op), FRAME_OK);
|
|
}
|
|
s.begin_quiescing();
|
|
frame_post_operation_t late{};
|
|
late.user = nullptr;
|
|
late.invoke = [](void*) {};
|
|
late.destroy = [](void*) {};
|
|
ASSERT_EQ(s.post(late), FRAME_ERR_PLUGIN_STOPPING);
|
|
|
|
core.start();
|
|
ASSERT_TRUE(wait_until([&] { return s.destroyed_count() == 2; }, std::chrono::seconds{10}));
|
|
ASSERT_TRUE(wait_until([&] { return s.stopped(); }, std::chrono::seconds{10}));
|
|
EXPECT_EQ(s.invoked_count(), 0u);
|
|
for (size_t index = 0; index < 2; ++index) {
|
|
EXPECT_FALSE(invoked_flags[index].load());
|
|
EXPECT_TRUE(destroy_flags[index].load());
|
|
}
|
|
core.stop();
|
|
}
|