阶段 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
@@ -19,12 +19,14 @@
namespace compiler {
// ---- .stb 格式常量(契约)----
// ---- .stb 格式常量(契约12.13 修订:头 104 = 原 72 + 型号标识[32],文件尾 SHA-256[32]----
static const uint32_t kMagic = 0x43545353u; // "STSC" 小端
static const uint32_t kVersion = 1;
static const size_t kHeaderSize = 72;
static const size_t kHeaderSize = 104;
static const size_t kConstEntrySize = 12;
static const size_t kFuncRowSize = 12;
static const size_t kSha256Size = 32;
static const size_t kModelIdSize = 32;
static const uint64_t kFnvBasis = 0xcbf29ce484222325ull;
static const uint64_t kFnvPrime = 0x100000001b3ull;
@@ -38,6 +40,12 @@ namespace compiler {
uint64_t fnv1a64_update(uint64_t h, const uint8_t* data, size_t len);
uint64_t fnv1a64(const uint8_t* data, size_t len);
// SHA-256(文件完整性校验,非密码学用途之外的完整性)
void sha256(const uint8_t* data, size_t len, uint8_t out[kSha256Size]);
// 型号标识:name + version 拼 32 字节 ASCII(如 "STATOR1"),不足补 '\0',超长截断
void fill_model_id(const std::string& name, uint32_t version, char out[kModelIdSize]);
// 只读视图(--disasm 用;校验魔数/版本/段边界)
class StbView {
public:
@@ -71,6 +79,11 @@ namespace compiler {
const uint8_t* data_bytes() const;
size_t data_len() const;
// 12.13:型号标识与 SHA-256 校验
std::string model_id() const; // 头 72..103(补 '\0' 后字符串)
bool model_matches(const std::string& name, uint32_t version) const;
bool sha_ok() const; // 文件尾 32 字节 SHA-256 校验
private:
StbView() : buf_(0), len_(0) {}