阶段 C 步骤 8:vm 读侧型号匹配 + SHA-256 校验。

- vm/Image.cpp:执行器侧 SHA-256 实现(与 compiler 各一份)、fill_model_id、
  model_id()/model_matches()/sha_ok();data_len 不含 SHA 尾
- Machine::create:sha256 不匹配 → 'image sha256 mismatch';型号不匹配 →
  'image model mismatch'(内建 kModelName=STATOR/kModelVersion=1),均拒绝
- vm_test:手拼映像填型号(修越界读 bug)+ compiler::sha256 填尾(95 断言);
  新增篡改拒绝(sha256 mismatch)与型号不匹配(重算 SHA 后 model mismatch)用例
- 验证:line1 正常执行 Q=1;篡改一字节 → 执行器 'image sha256 mismatch' 拒绝;ctest 12/12
This commit is contained in:
2026-08-21 22:29:33 +08:00
parent 5fccbadaa8
commit b13585711a
4 changed files with 246 additions and 4 deletions
+10 -1
View File
@@ -17,6 +17,10 @@
namespace vm {
// 执行器内建支持型号(与 machine.toml [meta] 对齐;.stb 型号不匹配直接拒绝)
static const char* const kModelName = "STATOR";
static const uint32_t kModelVersion = 1;
// 映像头(字段与 Doc/isa/指令与映像.md 一致)
struct ImageHeader {
uint32_t cycle_limit = 0;
@@ -62,7 +66,12 @@ namespace vm {
const uint8_t* code_bytes() const; // 字节码段起点
const uint8_t* data_bytes() const; // 数据段起点
size_t data_len() const; // 字节数
size_t data_len() const; // 字节数(不含 SHA 尾)
// 12.13:型号标识与 SHA-256 校验
std::string model_id() const;
bool model_matches(const std::string& name, uint32_t version) const;
bool sha_ok() const; // 文件尾 32 字节 SHA-256 校验
public:
// 默认构造为无效态(ok() == false