Files
frame/_doc/decisions/0002-elf-loader.md
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

14 KiB
Raw Permalink Blame History

ADR-0002: Dynamic ELF Loader Selection

Status: Accepted (2026-09-01)

Per development-plan §4("ADR 只有全部 required evidence 通过后才能 Accepted"):全部 required evidence 已按 GATE-A-REPORT 通过(GATE-A: PASS,nine §4 items + TEST-003 10/10)。Owner sign-off of the report's PENDING OWNER CONFIRMATION block completes the record(与 M1-008 owner-record pattern 同构,见 poc/evidence/m1-20260831/);在该 sign-off 落笔前 Gate-A 视为 unsigned。

Context

The architecture requires signed .mpb packages containing Xtensa ESP32-S3 ELF payloads whose verified bytes are loaded into PSRAM/IRAM. No loader or commit is selected. PoC-A must compare candidates before implementation details become product dependencies.

Candidate Evaluation

Candidate Repository Commit License ESP-IDF v6.0.2 Xtensa relocations Dynamic PSRAM text .plugin_iram Cache synchronization Result
espressif/elf_loader esp-iot-solution/components/elf_loader + registry espressif/elf_loader v1.3.3 (6526c5b1, 2026-07-28) Apache-2.0 ≥4.4.3; v6.x picolibc fixed in v1.3.2 (ff8c8663) — use ≥1.3.2 R_XTENSA_RELATIVE/RTLD/GLOB_DAT/JMP_SLOT only; others -EINVAL(硬编码 allowlist,与 project_so 链接参数耦合) ✅ S3 via I/D-bus mirror offset(SOC_IROM_LOW−SOC_DROM_LOW),heap_caps SPIRAM 8BIT ❌ 无 per-section IRAM(仅 internal EXEC fallback) full Cache_WriteBack_All + spi_flash_disable_interrupts_caches_and_other_cpu(无 esp_cache_msync;v1.3.3 因 unaligned faults 在 S3 上移除 ranged API) Primary — PoC-A PASS 2026-09-01(GATE-A-REPORT: nine items evidenced; 1000-cycle soak 18/18; capacity 8/8(PASS at rung 8, attempt 1); 14-class fail-closed zero-bytes-executed; 4 upstream defects found + fixed p1–p4 + .plugin_iram feature patch p5, per PATCHES.md @887d0f7 with diff + tree.sha256)
igrr/hotreload igrr/hotreload(registry igrr/hotreload) 0.10.0 (4acb68d, 2026-02-25) MIT idf ≥5.0;v6 untested RELATIVE/32/JMP_SLOT/PLT + 完整 SLOT0_OP operand patcher(CLAUDE.md 声称支持 ASM_EXPAND 但 switch 中缺失 — discrepancy) ✅ S3 PSRAM;esp_cache_msync DIR_C2M|UNALIGNED w/ fallback(最干净的参考实现) ❌ esp_cache_msync Not evaluated on target — reference/patterns role confirmed(esp_cache_msync usage、SLOT0_OP patcher;未集成)
Self-built minimal route n/a(IDF v6.0.2 natives:esp_mmu_map MMU_MEM_CAP_EXEC + esp_cache_msync + S3 bus-mirror) n/a project full control 自选 allowlist(在插件链接参数受约束的前提下,elf_loader 的 4-reloc 集是最小可行集;SLOT0_OP family 是难点) ✅ esp_mmu_map per-arena ✅ esp_cache_msync aligned esp_cache_msync Not needed (contingency) — Gate A PASS 使其无需启用(Decision Rule 仍不授权 self-built fallback)

Candidate Priority (2026-08-31):Primary = espressif/elf_loader ≥v1.3.3(pending PoC-A);Secondary = igrr/hotreload,仅作为 pattern/reference 来源(esp_cache_msync 用法、SLOT0_OP patcher),不直接集成;Contingency = self-built minimal route,仅在 Gate A 判定"无 candidate 通过"并回到架构评审后才可考虑(见 Decision Rule:本 ADR 不授权 automatic fallback 或新的 loader 实现)。

PoC-A outcome (2026-09-01):Primary 通过 Gate-A(见 matrix Result 与 PoC-A Frozen Outputs 节);Secondary 的 reference/patterns 角色经评审确认(msync 模式用于 p5 的 cache-sync 设计参照);Contingency 未启用。

espressif/elf_loader — Evaluation Evidence (2026-08-31)

核心 API 为 buffer 模型:esp_elf_relocate(elf, buf) 直接从已验证的 staging buffer 加载,其上的 dlopen 风格 layer(可选组件)建议保持关闭,以满足 immutable staging buffer 约束。未解析 import 默认 hard-fail -ENOSYS;v1.3.1 起可用 elf_set_symbol_resolver 提供 host-side 覆盖。PSRAM arena 为 per-instance(esp_elf_init/esp_elf_deinit 配对),支持多实例隔离。无 .init_array 处理——采用 entry-point 模型,构造函数语义需在 PoC-A 按 Required Evidence 单独裁决。已知 open issues(影响 PoC-A 观察):#776(2026-08-30,.so 调用崩溃)、#686(.got.loc)、#497(symbol tables 仅子集)、#560(C++ 支持)。

Citations:

igrr/hotreload — Evaluation Evidence (2026-08-31)

作者 Ivan Grokhotkov。定位是 dev-time 指针交换式 reload(single-generation),非生产级多代共存模型。提供 hotreload_load_from_buffer(对 PoC-A 的 immutable staging 有参考价值)。已知限制:unload 后旧 stub 悬挂(dangling stubs);未解析的 JMP_SLOT/PLT 仅告警(是否 hard-fail 未验证——与 Frame 的 fail-before-execute 要求可能冲突);固定地址 symbol table 导致固件更新后插件必须重建。价值在于 patterns:esp_cache_msync 的正确用法(本 matrix 中最干净的参考)与 SLOT0_OP operand patcher 实现。

Citations:

Self-built minimal route — Evaluation Evidence (2026-08-31)

ESP-IDF v6.0.2 树内没有 dynamic loader(espcoredump 仅含私有 elf.h struct 定义,无 relocate 能力);self-built 即在 IDF natives(esp_mmu_map + esp_cache_msync + S3 bus-mirror)之上重写 relocation/映射/同步层。R_XTENSA_REGISTER 并非真实存在的 dynamic relocation(GitHub 全域 0 命中;windowed-register 语义实际编码在 SLOT*_OP 系列中,是 self-built 路线的主要难点)。Canonical prior art 为 glibc xtensa dl-machine.h。

Citations:

Correction Log (2026-08)

  • 仓库 espressif/esp-elf-loader 不存在(GitHub 404);elf_loader 组件实际位于 espressif/esp-iot-solution 仓库 components/elf_loader 目录并以 registry espressif/elf_loader 发布。
  • "ARMAnt" 全网无结果,不是真实 candidate。
  • R_XTENSA_REGISTER 不是真实的 dynamic relocation(见 self-built evidence)。

Selection Rule Review (2026-08)

现有 Decision Rule(必须满足全部 required PoC-A item、仅加载 immutable verified staging buffer、无 automatic fallback)经评审后 UNCHANGED。新增一条 clarification:elf_loader 的窄 relocation allowlist 隐含插件必须经其 project_so pipeline(或等价的受约束链接参数)构建——该耦合作为 PoC-A probe item(TEST-003 relocation allowlist)验证,不构成规则变更。

Required Evidence

  • Exact repository URL, commit, license, and local patches
  • Accepted and rejected ELF class, machine, section, relocation, and import types
  • Program-header and relocation arithmetic with overflow checks
  • Function-pointer calls from dynamically allocated PSRAM
  • .plugin_iram copy, relocation, invocation, and release
  • Instruction/data cache synchronization APIs for the locked ESP-IDF tag
  • .init_array, static constructor/destructor, TLS, exception, RTTI, and unwind behavior
  • One thousand load/unload cycles with raw heap and largest-block samples

PoC-A Frozen Outputs (2026-09-01)

PLUG-009 / MEM-007 PoC-derived 值由 Gate-A 冻结(来源:GATE-A-REPORT section (d))。按 GOV-001 顺序,本 Accepted ADR 中的冻结值高于 v4.2 仍标 PoC-derived 的条目(后者保持原值待 sign-off,不在本次回填中改写)。Product admission 需在 PoC-B 以 Wi-Fi 激活态复测(harness 无线电关闭,MEM-001 caveat)。

Frozen output Value Basis / evidence
CONFIG_FRAME_MAX_MPB_BYTES(PoC policy) 512 KiB(524288 B)hard max;default arena 256 KiB,256–512 KiB requestable 双重强制:manifest_builder.py 拒绝打包更大声明(T2 pytest)+ 板上 admission 对签名有效的 600 KiB 敌意声明 pre-init 拒绝(neg_maxmem)
PSRAM cost per instance 3356 B(capacity class:staging 1984 B + loader arena 1352 B);baseline class 3388 B(manifest 名更长) ×3 次 identical ladder runs 实测;text-base stride 交叉验证
8-instance peak consumption 26848 B = 0.32 % of 8 MiB pool;largest_free_block 不变(8257536 B);unload 后 PSRAM 精确复原 capacity rung 8 attempt 1,×3 runs
Capacity value 8 ACTIVE per distinct plugin name(MEM-007 floor 5 从未触达;下降路径 7/6/5 未在硬件运行——honest limitation (f)1) signed budget line(ADMISSION-BUDGET.md)
Relocation + import allowlist dynamic reloc types {R_XTENSA_NONE=0, RTLD=2, GLOB_DAT=3, JMP_SLOT=4, RELATIVE=5};closed-world imports(declared + registered only,否则 -ENOSYS);program-header budget admission 全集冻结于 ALLOWLIST.md(正例 value-asserted、负例 byte-patched-and-rejected、link-flag contract)
C++ feature policy 全六项 REJECT before execute:static ctor/.init_array、dtor/fini_array、dynamic TLS、-fexceptions、RTTI、unwind tables(pipeline gate + loader section-scan 双层) REJECT-MATRIX.md(每特性拒绝层 + 证据引用)
Epoch floor virtual floor = 1(编译期常量,SEC-005 dev test policy);真实 eFuse floor 延后至 PoC-E neg_epoch rollback 类板上 pre-exec 拒绝(task-14)
MEMPROT trade-off CONFIG_ESP_SYSTEM_MEMPROT=n(.plugin_iram 所需的 IDF exec heap MALLOC_CAP_EXEC 的前提);software-only PMP config,未烧 eFuse;production 必须重新裁决 security vs plugin IRAM PATCHES.md p5 + sdkconfig.defaults 注释
Local patches p1–p5,each with diff + post-patch tree.sha256(最终 manifest sha256 3c7706cb…49a81,26 upstream files) PATCHES.md @887d0f7;assembly 时离线自洽复验

ADR amendment(承 GATE-A-REPORT (d) note):worst-case 8 ×(512 KiB arena + 512 KiB container)≈ 8 MiB ≱ pool —— product admission controller 必须对驻留声明的 global sum 设闸,而非仅 per-plugin hard max。

Decision Rule

The selected candidate must satisfy every required PoC-A item without loading bytes other than the immutable, verified staging buffer. If no candidate passes, Gate A fails and the architecture returns to review; this ADR does not authorize an automatic fallback or a new loader implementation.

Gate Impact

PoC-A 于 2026-09-01 通过 Gate-A(GATE-A-REPORT PENDING owner sign-off 仍开放);本 ADR Accepted 后,development-plan §5 的 "ADR-0002 Accepted" 前置条件在证据上已满足(以该 sign-off 落笔为准)。Product PluginLoader 实现本身仍受 GOV-004 约束:S1 architecture sign-off 前不得把 PoC 代码提升为产品组件。

Selected: espressif/elf_loader v1.3.3 vendored + patches p1–p5(poc/apps/poc_a/components/elf_loader/);hotreload confirmed as pattern source only;self-built contingency not needed.