阶段 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
+6 -6
View File
@@ -187,7 +187,7 @@ static bool test_case09() {
const isa::ImageView v = isa::ImageView::from(img);
CHECK(v.ok());
CHECK(v.header().n_globals == 1);
CHECK(v.data_len() == 8); // 8 字节定宽槽
CHECK(v.data_len() == 8 + 32); // 8 字节定宽槽 + SHA-256 尾
CHECK(v.data_bytes()[0] == 5); // G1 初值 5(小端低位)
CHECK(v.data_bytes()[1] == 0);
@@ -261,7 +261,7 @@ static bool test_layout() {
const isa::ImageView v = isa::ImageView::from(img);
CHECK(v.ok());
CHECK(v.header().n_globals == 3);
CHECK(v.data_len() == 24); // 3 槽 × 8 字节定宽
CHECK(v.data_len() == 24 + 32); // 3 槽 × 8 字节定宽 + SHA-256 尾
CHECK(v.data_bytes()[0] == 0); // B 无初值
CHECK(v.data_bytes()[8] == 0x2C && v.data_bytes()[9] == 0x01); // I = 300(槽 1
CHECK(v.data_bytes()[16] == 10); // T = 10ms(槽 2
@@ -565,7 +565,7 @@ static bool test_case15() {
CHECK(v.ok());
CHECK(v.header().n_funcs == 2); // FB 占位 + MAIN
CHECK(v.header().entry_fn_id == 1);
CHECK(v.data_len() == 24); // 实例 3 槽 × 8 字节定宽
CHECK(v.data_len() == 24 + 32); // 实例 3 槽 × 8 字节定宽 + SHA-256 尾
const isa::FuncRow fm = v.func_row(1);
CHECK(fm.nregs == 12);
@@ -603,7 +603,7 @@ static bool test_case17() {
const isa::ImageView v = isa::ImageView::from(img);
CHECK(v.ok());
CHECK(v.data_len() == 32); // in@0 pt@8 q@16 et@24
CHECK(v.data_len() == 32 + 32); // in@0 pt@8 q@16 et@24 + SHA-256 尾
const isa::FuncRow r = v.func_row(0);
CHECK(r.nregs == 11);
const uint32_t* ins = reinterpret_cast<const uint32_t*>(v.code_bytes());
@@ -632,7 +632,7 @@ static bool test_case18() {
const isa::ImageView v = isa::ImageView::from(img);
CHECK(v.ok());
CHECK(v.data_len() == 72); // tf 4 槽 + c 5 槽 = 9 槽 × 8
CHECK(v.data_len() == 72 + 32); // tf 4 槽 + c 5 槽 = 9 槽 × 8 + SHA-256 尾
const isa::FuncRow r = v.func_row(0);
CHECK(r.nregs == 14);
const uint32_t* ins = reinterpret_cast<const uint32_t*>(v.code_bytes());
@@ -675,7 +675,7 @@ static bool test_line1() {
CHECK(v.header().n_globals == 4);
CHECK(v.header().n_funcs == 2);
CHECK(v.header().entry_fn_id == 1);
CHECK(v.data_len() == 56); // 7 槽 × 8 字节定宽
CHECK(v.data_len() == 56 + 32); // 7 槽 × 8 字节定宽 + SHA-256 尾
const isa::FuncRow fm = v.func_row(1);
CHECK(fm.nregs == 13);
+35
View File
@@ -6,10 +6,12 @@
*/
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <string>
#include "compiler/MachineConfig.h"
#include "compiler/Stb.h"
#include "compiler/TypeInfo.h"
#ifndef REPO_ROOT
@@ -284,11 +286,44 @@ static bool test_type_meta() {
return true;
}
// ---- 5. SHA-256 已知向量 + 型号标识 ----
static bool test_sha256() {
// 标准测试向量
{
uint8_t out[compiler::kSha256Size];
compiler::sha256(nullptr, 0, out);
const uint8_t want[32] = {0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55};
CHECK(std::memcmp(out, want, 32) == 0);
}
{
const uint8_t abc[] = {'a', 'b', 'c'};
uint8_t out[compiler::kSha256Size];
compiler::sha256(abc, 3, out);
const uint8_t want[32] = {0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad};
CHECK(std::memcmp(out, want, 32) == 0);
}
// 型号标识:STATOR + 1 → "STATOR1" 补 '\0'
char mid[compiler::kModelIdSize];
compiler::fill_model_id("STATOR", 1, mid);
CHECK(std::strncmp(mid, "STATOR1", 7) == 0);
CHECK(mid[7] == '\0');
CHECK(mid[31] == '\0');
return true;
}
int main() {
if (!test_positive()) return 1;
if (!test_negative()) return 1;
if (!test_positive_min()) return 1;
if (!test_type_meta()) return 1;
if (!test_sha256()) return 1;
std::printf("machine_test: %d checks passed\n", g_checks);
return 0;
}