阶段 B 步骤 6:.stb 写侧新格式(头 104 + 型号标识 + SHA-256 文件尾)。

- Stb:kHeaderSize 104、kSha256Size 32;SHA-256 实现(FIPS 180-4,含标准测试向量验证);
  fill_model_id(name+version 拼 32 字节补 '\0');StbView 解析新格式 +
  model_id()/model_matches()/sha_ok()(数据段不含尾、缺尾报错)
- Codegen:assemble_image 写型号标识(偏移 72)+ 文件尾 SHA-256
- main.cpp:--disasm 显示 model/sha;写后自检(ok+sha+型号匹配,失败报错)
- 测试:machine_test +SHA-256 已知向量(空串/abc)与型号标识断言(53 断言);
  codegen_test data_len 断言 +32(SHA 尾);ctest 12/12
- 验证:line1 288 字节 model=STATOR1 sha=ok;篡改一字节 sha=BAD
This commit is contained in:
2026-08-21 22:20:35 +08:00
parent e187ed7b03
commit a308ae713d
7 changed files with 245 additions and 15 deletions
+15 -2
View File
@@ -47,8 +47,10 @@ namespace {
}
std::printf("image: %s (%zu bytes, %u functions, %u globals, entry fn %u)\n",
path, bytes.size(), v.n_funcs(), v.n_globals(), v.entry_fn_id());
std::printf(" dt_ms=%u cycle_limit=%u hash=0x%016llx\n", v.dt_ms(),
v.cycle_limit(), static_cast<unsigned long long>(v.project_hash()));
std::printf(" dt_ms=%u cycle_limit=%u hash=0x%016llx model=%s sha=%s\n",
v.dt_ms(), v.cycle_limit(),
static_cast<unsigned long long>(v.project_hash()),
v.model_id().c_str(), v.sha_ok() ? "ok" : "BAD");
if (v.n_consts()) {
std::printf("constants:\n");
@@ -191,6 +193,17 @@ int main(int argc, char** argv) {
return 1;
}
// 写后自检:型号标识与 SHA-256
const compiler::StbView self = compiler::StbView::from(image);
if (!self.ok() || !self.sha_ok()) {
std::fprintf(stderr, "error: self-check failed: %s\n", self.error().c_str());
return 1;
}
if (!self.model_matches(cfg.model_name(), cfg.version())) {
std::fprintf(stderr, "error: self-check model mismatch\n");
return 1;
}
// sidecarI/O 绑定 var → 槽号 → channel/bit(不进映像,执行器采样用)
std::vector<compiler::IoBinding> bindings;
for (const compiler::IoBinding& b : proj.io) {