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]>
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
#ifndef FRAME_POC_FRAME_STRAND_HH
|
||||
#define FRAME_POC_FRAME_STRAND_HH
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "frame_poc/frame_abi.h"
|
||||
|
||||
namespace frame::poc {
|
||||
|
||||
// Scheduling protocol mapping (project.md §4.1.1-4.1.3):
|
||||
//
|
||||
// post(operation)
|
||||
// -> operation enters target strand FIFO (linearization = strand mutex)
|
||||
// -> strand IDLE -> SCHEDULED (CAS under the same lock)
|
||||
// -> strand pointer enters the io_core ready ring (max-once invariant:
|
||||
// a strand is in the ready ring only between a successful
|
||||
// IDLE->SCHEDULED conversion and the worker's run_one, therefore a
|
||||
// strand can appear in the ring at most once)
|
||||
//
|
||||
// io_core.run_loop()
|
||||
// -> pop a ready strand
|
||||
// -> run_one(): pop ONE node; FIFO still non-empty -> requeue at ring
|
||||
// tail; FIFO empty -> IDLE (or STOPPED when closing)
|
||||
// -> execute the popped node OUTSIDE the strand lock
|
||||
//
|
||||
// Anti-lost-wakeup protocol (§4.1.3): the FIFO empty-check and the IDLE
|
||||
// store happen under the same strand mutex as enqueue + IDLE->SCHEDULED
|
||||
// CAS. A post that acquires the lock before run_one's check sees a
|
||||
// non-empty FIFO and keeps the strand scheduled; a post that acquires it
|
||||
// after sees IDLE and schedules the strand itself. Either way a non-empty
|
||||
// FIFO is always matched by a pending or upcoming ready-ring entry.
|
||||
//
|
||||
// FreeRTOS port note (T13): the strand mutex maps to a critical section
|
||||
// (taskENTER_CRITICAL/taskEXIT_CRITICAL) around the identical FIFO +
|
||||
// state-transition sequence; the ready ring maps to a bounded queue of
|
||||
// strand handles; run_loop maps to the per-core worker task. The protocol
|
||||
// itself is unchanged.
|
||||
enum class strand_state : uint8_t { idle, scheduled, running, closing, stopped };
|
||||
|
||||
enum class op_kind : uint8_t { post, cleanup, completion };
|
||||
|
||||
// Intrusive FIFO node. Pool nodes (kind post) come from the strand's
|
||||
// preallocated array; completion nodes (kind completion) are embedded in
|
||||
// caller-owned storage and never touch the pool.
|
||||
struct strand_op_node {
|
||||
strand_op_node* next = nullptr;
|
||||
op_kind kind = op_kind::post;
|
||||
bool pooled = false;
|
||||
uint64_t seq = 0;
|
||||
uint64_t generation = 0;
|
||||
frame_post_operation_t post{};
|
||||
frame_completion_operation_t completion{};
|
||||
frame_err_t completion_err = FRAME_OK;
|
||||
};
|
||||
|
||||
class strand;
|
||||
|
||||
// One worker thread (host stand-in for one fixed-core FreeRTOS worker, §4.1.1).
|
||||
// Preallocated fixed-capacity ring of strand pointers; capacity equals the
|
||||
// maximum number of strands, so a legal SCHEDULED transition never blocks.
|
||||
class io_core {
|
||||
public:
|
||||
explicit io_core(std::size_t max_strands);
|
||||
~io_core();
|
||||
|
||||
io_core(const io_core&) = delete;
|
||||
io_core& operator=(const io_core&) = delete;
|
||||
|
||||
// Spawns the worker thread. push_ready may be called before start();
|
||||
// queued strands are drained once started.
|
||||
void start();
|
||||
// Idempotent. Wakes the worker, drains nothing, joins the thread.
|
||||
void stop();
|
||||
|
||||
bool duplicate_detected() const noexcept {
|
||||
return duplicate_fault_.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
private:
|
||||
friend class strand;
|
||||
|
||||
void push_ready(strand& target);
|
||||
strand* pop_ready();
|
||||
void run_loop();
|
||||
|
||||
const std::size_t capacity_;
|
||||
std::vector<strand*> ring_;
|
||||
std::size_t head_ = 0;
|
||||
std::size_t count_ = 0;
|
||||
std::mutex mutex_;
|
||||
std::condition_variable not_empty_;
|
||||
std::condition_variable not_full_;
|
||||
bool stopping_ = false;
|
||||
std::thread worker_;
|
||||
bool started_ = false;
|
||||
// Fault flag set under mutex_ when a strand is pushed while already
|
||||
// enqueued (protocol violation detector; tests assert it stays clear).
|
||||
std::atomic<bool> duplicate_fault_{false};
|
||||
};
|
||||
|
||||
// Logical serial executor (§4.1.2): one FIFO, preallocated pool nodes,
|
||||
// bounded outstanding-operations credit, exactly-once invoke/destroy.
|
||||
class strand {
|
||||
public:
|
||||
strand(io_core& core, std::size_t op_capacity,
|
||||
std::chrono::microseconds handler_budget = std::chrono::milliseconds{1});
|
||||
~strand();
|
||||
|
||||
strand(const strand&) = delete;
|
||||
strand& operator=(const strand&) = delete;
|
||||
|
||||
// Async post only (LIFE-002: no dispatch/defer). On FRAME_OK the strand
|
||||
// owns `operation` and will call invoke then destroy exactly once. On
|
||||
// FRAME_ERR_QUEUE_FULL the credit is exhausted and the caller keeps
|
||||
// ownership (destroy is NOT called). On FRAME_ERR_PLUGIN_STOPPING the
|
||||
// strand is closing/stopped and the caller keeps ownership.
|
||||
frame_err_t post(frame_post_operation_t operation);
|
||||
|
||||
// §4.1.6 step 5 / LIFE-007: close the post entry, convert every queued
|
||||
// un-run POST in place (kind -> cleanup, FIFO position preserved).
|
||||
// When the FIFO drains in closing the state becomes stopped (poll via
|
||||
// stopped()). Idempotent.
|
||||
void begin_quiescing();
|
||||
|
||||
// Internal completion-delivery path (§4.1.4): bypasses credit and pool
|
||||
// (caller-owned reserved node), allowed during closing (delivers cancel
|
||||
// results), cannot fail. `reserved.completion` / `reserved.completion_err`
|
||||
// must already be filled; the node is executed as kind completion:
|
||||
// complete(user, err) then destroy(user), exactly once each.
|
||||
void post_completion(strand_op_node& reserved);
|
||||
|
||||
// Executes one popped node; called by io_core's worker.
|
||||
void run_one();
|
||||
|
||||
bool stopped() const noexcept {
|
||||
return state_.load(std::memory_order_acquire) == strand_state::stopped;
|
||||
}
|
||||
strand_state state() const noexcept { return state_.load(std::memory_order_acquire); }
|
||||
|
||||
uint64_t generation() const noexcept { return generation_.load(std::memory_order_acquire); }
|
||||
uint64_t advance_generation() noexcept {
|
||||
return generation_.fetch_add(1, std::memory_order_acq_rel);
|
||||
}
|
||||
|
||||
// Instrumentation (LIFE-002 handler budget, LIFE-007 stale drop).
|
||||
uint64_t invoked_count() const noexcept { return invoked_.load(std::memory_order_acquire); }
|
||||
uint64_t destroyed_count() const noexcept { return destroyed_.load(std::memory_order_acquire); }
|
||||
uint64_t handler_overruns() const noexcept { return overruns_.load(std::memory_order_acquire); }
|
||||
uint64_t stale_generation_dropped() const noexcept {
|
||||
return stale_dropped_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
private:
|
||||
friend class io_core;
|
||||
|
||||
strand_op_node* pop_fifo_locked();
|
||||
void push_fifo_locked(strand_op_node* node);
|
||||
strand_op_node* pop_freelist_locked();
|
||||
void push_freelist_locked(strand_op_node* node);
|
||||
void execute_node(strand_op_node* node) noexcept;
|
||||
|
||||
io_core& core_;
|
||||
const std::size_t capacity_;
|
||||
const std::chrono::microseconds handler_budget_;
|
||||
|
||||
std::mutex mutex_;
|
||||
strand_op_node* fifo_head_ = nullptr;
|
||||
strand_op_node* fifo_tail_ = nullptr;
|
||||
strand_op_node* free_head_ = nullptr;
|
||||
std::vector<strand_op_node> pool_;
|
||||
std::size_t outstanding_ = 0;
|
||||
uint64_t ticket_ = 0;
|
||||
bool ready_pending_ = false; // in ready ring (or push in flight); guarded by mutex_
|
||||
bool closing_ = false; // quiescing flag; guarded by mutex_
|
||||
|
||||
std::atomic<strand_state> state_{strand_state::idle};
|
||||
std::atomic<uint64_t> generation_{0};
|
||||
std::atomic<uint64_t> invoked_{0};
|
||||
std::atomic<uint64_t> destroyed_{0};
|
||||
std::atomic<uint64_t> overruns_{0};
|
||||
std::atomic<uint64_t> stale_dropped_{0};
|
||||
};
|
||||
|
||||
} // namespace frame::poc
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user