Files
frame/poc/host/tests/frame_completion_test.cc
T
Lainandlain e71842aa8a feat(poc-a): gate-a dynamic elf proof of concept (#13)
## 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]>
2026-09-07 20:02:18 +08:00

367 lines
14 KiB
C++

#include <atomic>
#include <chrono>
#include <cstdint>
#include <functional>
#include <thread>
#include <vector>
#include <gtest/gtest.h>
#include "frame_poc/frame_completion.hh"
#include "frame_poc/frame_strand.hh"
using frame::poc::completion_node;
using frame::poc::completion_state;
using frame::poc::io_core;
using frame::poc::strand;
namespace {
constexpr auto kPollDelay = std::chrono::microseconds{50};
constexpr frame_err_t kOriginalErr = FRAME_ERR_TIMEOUT; // -2, distinct from CANCELLED (-15)
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 completion_rec {
std::atomic<uint64_t> complete_delivered{0}; // err != CANCELLED
std::atomic<uint64_t> cancel_delivered{0}; // err == FRAME_ERR_CANCELLED
std::atomic<uint64_t> destroyed{0};
std::vector<frame_err_t> delivered_errs; // written only by the worker
std::vector<std::thread::id> destroy_threads; // written only by the worker
uint64_t deliveries() const {
return complete_delivered.load(std::memory_order_acquire) +
cancel_delivered.load(std::memory_order_acquire);
}
};
struct post_rec {
std::atomic<bool> invoked{false};
std::atomic<bool> destroyed{false};
std::thread::id destroy_thread{}; // written only by the worker
};
frame_completion_operation_t make_completion_op(completion_rec* rec) {
frame_completion_operation_t op{};
op.user = rec;
op.complete = [](void* user, frame_err_t err) {
auto* target = static_cast<completion_rec*>(user);
if (err == FRAME_ERR_CANCELLED) {
target->cancel_delivered.fetch_add(1, std::memory_order_seq_cst);
} else {
target->complete_delivered.fetch_add(1, std::memory_order_seq_cst);
}
target->delivered_errs.push_back(err);
};
op.destroy = [](void* user) {
auto* target = static_cast<completion_rec*>(user);
target->destroy_threads.push_back(std::this_thread::get_id());
target->destroyed.fetch_add(1, std::memory_order_seq_cst);
};
return op;
}
} // namespace
// T12 (1): complete-vs-cancel race, two persistent threads, atomic round
// gate, 1e5 rounds. Exactly one CAS winner per round; deliveries partition
// into complete/cancel; destroy exactly once per op.
TEST(FrameCompletion, CompleteVsCancelRaceExactlyOnce) {
io_core core(1);
strand s(core, 64);
constexpr uint64_t kRounds = 100000;
constexpr std::size_t kPool = 64;
std::vector<completion_node> nodes(kPool);
completion_rec rec;
std::atomic<uint64_t> gate{0};
std::atomic<uint64_t> done_complete{0};
std::atomic<uint64_t> done_cancel{0};
std::atomic<bool> shutdown{false};
std::atomic<uint64_t> reserve_failures{0};
core.start();
auto spinner = [&](auto act, std::atomic<uint64_t>& done) {
uint64_t next = 1;
while (!shutdown.load(std::memory_order_acquire)) {
while (gate.load(std::memory_order_acquire) < next) {
if (shutdown.load(std::memory_order_acquire)) {
return;
}
std::this_thread::yield();
}
act(next - 1, nodes[(next - 1) % kPool]);
done.store(next, std::memory_order_release);
++next;
}
};
std::thread complete_thread(
spinner,
[&](uint64_t, completion_node& node) {
if (!node.complete(kOriginalErr)) {
// losing the CAS is legal; only the winner delivers
}
},
std::ref(done_complete));
std::thread cancel_thread(
spinner, [&](uint64_t, completion_node& node) { (void)node.cancel(); },
std::ref(done_cancel));
for (uint64_t round = 0; round < kRounds; ++round) {
completion_node& node = nodes[round % kPool];
if (!node.reserve(s, s.generation(), make_completion_op(&rec))) {
reserve_failures.fetch_add(1);
break;
}
gate.store(round + 1, std::memory_order_release);
ASSERT_TRUE(wait_until(
[&] {
return done_complete.load(std::memory_order_acquire) >= round + 1 &&
done_cancel.load(std::memory_order_acquire) >= round + 1 &&
rec.deliveries() == round + 1 && rec.destroyed.load() == round + 1 &&
s.destroyed_count() == round + 1;
},
std::chrono::seconds{30}))
<< "stalled at round " << round;
node.recycle();
}
// Release spinners via the shutdown flag only: bumping the gate here
// would unlock one extra act() on a recycled (PENDING) node.
shutdown.store(true, std::memory_order_release);
complete_thread.join();
cancel_thread.join();
EXPECT_EQ(reserve_failures.load(), 0u);
EXPECT_EQ(rec.deliveries(), kRounds);
EXPECT_EQ(rec.complete_delivered.load() + rec.cancel_delivered.load(), kRounds);
EXPECT_EQ(rec.destroyed.load(), kRounds);
EXPECT_EQ(s.destroyed_count(), kRounds);
ASSERT_EQ(rec.delivered_errs.size(), kRounds);
for (const frame_err_t err : rec.delivered_errs) {
EXPECT_TRUE(err == kOriginalErr || err == FRAME_ERR_CANCELLED);
}
core.stop();
}
// T12 (2): COMPLETED wins the CAS first -> the original error is delivered;
// a later cancel is a no-op (late result releases nothing).
TEST(FrameCompletion, CancelAfterCompleteKeepsOriginalErr) {
io_core core(1);
strand s(core, 4);
completion_node node;
completion_rec rec;
core.start();
ASSERT_TRUE(node.reserve(s, s.generation(), make_completion_op(&rec)));
ASSERT_TRUE(node.complete(kOriginalErr));
ASSERT_FALSE(node.cancel());
ASSERT_FALSE(node.complete(FRAME_ERR_BUSY));
EXPECT_EQ(node.state(), completion_state::completed);
ASSERT_TRUE(wait_until([&] { return rec.destroyed.load() == 1; }, std::chrono::seconds{10}));
ASSERT_EQ(rec.delivered_errs.size(), 1u);
EXPECT_EQ(rec.delivered_errs[0], kOriginalErr);
EXPECT_EQ(rec.cancel_delivered.load(), 0u);
EXPECT_EQ(rec.complete_delivered.load(), 1u);
EXPECT_EQ(s.destroyed_count(), 1u);
core.stop();
}
// T12 (3): reservation failure means nothing was started: the service
// returns QUEUE_FULL synchronously and never touches the caller's op.
TEST(FrameCompletion, ReserveFailureStartsNothing) {
io_core core(1);
strand s(core, 4);
completion_node node;
completion_rec rec;
const frame_completion_operation_t op = make_completion_op(&rec);
ASSERT_TRUE(node.reserve(s, s.generation(), op));
ASSERT_FALSE(node.reserve(s, s.generation(), op)); // already used
// Service-side contract on the failed path: synchronous QUEUE_FULL.
const frame_err_t started =
node.reserve(s, s.generation(), op) ? FRAME_OK : FRAME_ERR_QUEUE_FULL;
EXPECT_EQ(started, FRAME_ERR_QUEUE_FULL);
EXPECT_EQ(rec.deliveries(), 0u);
EXPECT_EQ(rec.destroyed.load(), 0u);
EXPECT_EQ(s.invoked_count(), 0u);
EXPECT_EQ(s.destroyed_count(), 0u);
core.stop();
}
// T12 (4): QUIESCING converts queued un-run POSTs to CLEANUP in place; every
// destroy runs on the worker thread (LIFE-007), no handler is invoked.
TEST(FrameCompletion, QuiescingCleanupRunsOnWorkerThread) {
io_core core(1);
strand s(core, 16);
constexpr int kPosts = 16;
std::vector<post_rec> posts(kPosts);
for (auto& post : posts) {
frame_post_operation_t op{};
op.user = &post;
op.invoke = [](void* user) { static_cast<post_rec*>(user)->invoked.store(true); };
op.destroy = [](void* user) {
auto* post = static_cast<post_rec*>(user);
post->destroy_thread = std::this_thread::get_id();
post->destroyed.store(true);
};
ASSERT_EQ(s.post(op), FRAME_OK);
}
s.begin_quiescing();
core.start();
ASSERT_TRUE(
wait_until([&] { return s.destroyed_count() == kPosts; }, std::chrono::seconds{10}));
ASSERT_TRUE(wait_until([&] { return s.stopped(); }, std::chrono::seconds{10}));
EXPECT_EQ(s.invoked_count(), 0u);
EXPECT_EQ(s.stale_generation_dropped(), 0u);
const std::thread::id main_id = std::this_thread::get_id();
for (const auto& post : posts) {
EXPECT_FALSE(post.invoked.load());
EXPECT_TRUE(post.destroyed.load());
EXPECT_NE(post.destroy_thread, std::thread::id{});
EXPECT_NE(post.destroy_thread, main_id); // destroys ran on the worker
EXPECT_EQ(post.destroy_thread, posts.front().destroy_thread);
}
core.stop();
}
// T12 (5): stale generation callbacks are dropped (no complete/invoke) but
// still destroyed exactly once and counted.
TEST(FrameCompletion, StaleGenerationDroppedButDestroyed) {
io_core core(1);
strand s(core, 8);
std::vector<post_rec> posts(4);
for (auto& post : posts) {
frame_post_operation_t op{};
op.user = &post;
op.invoke = [](void* user) { static_cast<post_rec*>(user)->invoked.store(true); };
op.destroy = [](void* user) { static_cast<post_rec*>(user)->destroyed.store(true); };
ASSERT_EQ(s.post(op), FRAME_OK);
}
const uint64_t stale_generation = s.generation();
s.advance_generation();
core.start();
ASSERT_TRUE(wait_until([&] { return s.destroyed_count() == 4; }, std::chrono::seconds{10}));
EXPECT_EQ(s.invoked_count(), 0u);
EXPECT_EQ(s.stale_generation_dropped(), 4u);
for (const auto& post : posts) {
EXPECT_FALSE(post.invoked.load());
EXPECT_TRUE(post.destroyed.load());
}
completion_node node;
completion_rec rec;
ASSERT_TRUE(node.reserve(s, stale_generation, make_completion_op(&rec)));
ASSERT_TRUE(node.complete(kOriginalErr));
ASSERT_TRUE(wait_until([&] { return rec.destroyed.load() == 1; }, std::chrono::seconds{10}));
EXPECT_EQ(rec.deliveries(), 0u); // stale: complete() skipped
EXPECT_EQ(rec.destroyed.load(), 1u);
EXPECT_EQ(s.stale_generation_dropped(), 5u);
core.stop();
}
// T12 (6): three-way race complete/cancel/quiesce per round on a fresh
// strand (quiesce is terminal). Invariants: exactly one completion delivery
// (original err or CANCELLED), every accepted post destroyed exactly once
// (invoked or cleaned up), the strand reaches stopped.
TEST(FrameCompletion, ThreeWayRaceCompleteCancelQuiesce) {
io_core core(1);
constexpr uint64_t kRounds = 20000;
constexpr std::size_t kPool = 64;
constexpr int kPostsPerRound = 3;
std::vector<completion_node> nodes(kPool);
std::vector<post_rec> posts(kPostsPerRound);
completion_rec rec;
std::atomic<uint64_t> gate{0};
std::atomic<uint64_t> done[3] = {{0}, {0}, {0}};
std::atomic<bool> shutdown{false};
std::atomic<strand*> current{nullptr};
std::atomic<uint64_t> reserve_failures{0};
core.start();
auto spinner = [&](int slot, auto act) {
uint64_t next = 1;
while (!shutdown.load(std::memory_order_acquire)) {
while (gate.load(std::memory_order_acquire) < next) {
if (shutdown.load(std::memory_order_acquire)) {
return;
}
std::this_thread::yield();
}
act(next - 1, nodes[(next - 1) % kPool]);
done[slot].store(next, std::memory_order_release);
++next;
}
};
std::thread complete_thread(
spinner, 0, [&](uint64_t, completion_node& node) { (void)node.complete(kOriginalErr); });
std::thread cancel_thread(spinner, 1,
[&](uint64_t, completion_node& node) { (void)node.cancel(); });
std::thread quiesce_thread(spinner, 2, [&](uint64_t, completion_node&) {
current.load(std::memory_order_acquire)->begin_quiescing();
});
for (uint64_t round = 0; round < kRounds; ++round) {
strand round_strand(core, 8);
for (auto& post : posts) {
post.invoked.store(false);
post.destroyed.store(false);
}
completion_node& node = nodes[round % kPool];
if (!node.reserve(round_strand, round_strand.generation(), make_completion_op(&rec))) {
reserve_failures.fetch_add(1);
break;
}
for (auto& post : posts) {
frame_post_operation_t op{};
op.user = &post;
op.invoke = [](void* user) { static_cast<post_rec*>(user)->invoked.store(true); };
op.destroy = [](void* user) {
auto* item = static_cast<post_rec*>(user);
item->destroy_thread = std::this_thread::get_id();
item->destroyed.store(true);
};
ASSERT_EQ(round_strand.post(op), FRAME_OK) << "round " << round;
}
current.store(&round_strand, std::memory_order_release);
gate.store(round + 1, std::memory_order_release);
ASSERT_TRUE(wait_until(
[&] {
return done[0].load(std::memory_order_acquire) >= round + 1 &&
done[1].load(std::memory_order_acquire) >= round + 1 &&
done[2].load(std::memory_order_acquire) >= round + 1 &&
rec.deliveries() == round + 1 && rec.destroyed.load() == round + 1 &&
round_strand.destroyed_count() == kPostsPerRound + 1 &&
round_strand.stopped();
},
std::chrono::seconds{30}))
<< "stalled at round " << round;
for (const auto& post : posts) {
ASSERT_TRUE(post.destroyed.load()) << "round " << round;
}
node.recycle();
}
shutdown.store(true, std::memory_order_release);
complete_thread.join();
cancel_thread.join();
quiesce_thread.join();
EXPECT_EQ(reserve_failures.load(), 0u);
EXPECT_EQ(rec.deliveries(), kRounds);
EXPECT_EQ(rec.complete_delivered.load() + rec.cancel_delivered.load(), kRounds);
EXPECT_EQ(rec.destroyed.load(), kRounds);
core.stop();
}