stb 头字段偏移常量表:消除 vm/compiler 两侧裸魔法数
- vm/Image.cpp:匿名空间 constexpr 偏移常量(kOffMagic..kOffModelId + 段内行偏移), from/model_id/model_matches/func_row/const_entry 全部替换 - compiler/Stb.h:同值 inline constexpr 表(Codegen 写侧 + Stb 读侧共用) - compiler/Codegen.cpp assemble_image:put_* 头字段/行内偏移替换 - compiler/Stb.cpp StbView:读侧偏移替换 - 两侧 static_assert:头大小 = 72 + 型号标识、字段偏移单调、行内布局自洽
This commit is contained in:
+19
-19
@@ -243,31 +243,31 @@ StbView StbView::from(const uint8_t* buf, size_t len) {
|
||||
v.err_ = "image too short";
|
||||
return v;
|
||||
}
|
||||
if (get_le32(buf + 0) != kMagic) {
|
||||
if (get_le32(buf + kOffMagic) != kMagic) {
|
||||
v.err_ = "bad magic";
|
||||
return v;
|
||||
}
|
||||
if (get_le32(buf + 4) != kVersion) {
|
||||
if (get_le32(buf + kOffVersion) != kVersion) {
|
||||
v.err_ = "bad version";
|
||||
return v;
|
||||
}
|
||||
v.cycle_limit_ = get_le32(buf + 8);
|
||||
v.dt_ms_ = get_le32(buf + 12);
|
||||
v.project_hash_ = get_le64(buf + 16);
|
||||
v.entry_fn_id_ = get_le32(buf + 24);
|
||||
v.n_globals_ = get_le32(buf + 28);
|
||||
v.n_consts_ = get_le32(buf + 44);
|
||||
v.n_funcs_ = get_le32(buf + 48);
|
||||
v.offset_code_ = get_le32(buf + 60);
|
||||
v.offset_fb_ = get_le32(buf + 64);
|
||||
v.offset_data_ = get_le32(buf + 68);
|
||||
v.cycle_limit_ = get_le32(buf + kOffCycleLimit);
|
||||
v.dt_ms_ = get_le32(buf + kOffDtMs);
|
||||
v.project_hash_ = get_le64(buf + kOffProjectHash);
|
||||
v.entry_fn_id_ = get_le32(buf + kOffEntryFnId);
|
||||
v.n_globals_ = get_le32(buf + kOffNGlobals);
|
||||
v.n_consts_ = get_le32(buf + kOffNConsts);
|
||||
v.n_funcs_ = get_le32(buf + kOffNFuncs);
|
||||
v.offset_code_ = get_le32(buf + kOffCode);
|
||||
v.offset_fb_ = get_le32(buf + kOffFb);
|
||||
v.offset_data_ = get_le32(buf + kOffData);
|
||||
|
||||
// 段校验(12.13:文件尾 SHA-256[32] 在数据段之后)
|
||||
if (len < static_cast<size_t>(v.offset_data_) + kSha256Size) {
|
||||
v.err_ = "missing sha256 tail";
|
||||
return v;
|
||||
}
|
||||
const uint64_t offs[5] = {get_le32(buf + 52), get_le32(buf + 56), v.offset_code_,
|
||||
const uint64_t offs[5] = {get_le32(buf + kOffConst), get_le32(buf + kOffFuncs), v.offset_code_,
|
||||
v.offset_fb_, v.offset_data_};
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
if (offs[i] < kHeaderSize || offs[i] > len - kSha256Size) {
|
||||
@@ -311,7 +311,7 @@ ConstEntry StbView::const_entry(size_t i) const {
|
||||
if (ok_ && i < n_consts_) {
|
||||
const uint8_t* p = buf_ + offs_of_const() + i * kConstEntrySize;
|
||||
e.tag = get_le32(p);
|
||||
e.value = get_le64(p + 4);
|
||||
e.value = get_le64(p + kConstValueOff);
|
||||
}
|
||||
return e;
|
||||
}
|
||||
@@ -327,9 +327,9 @@ StbView::FuncRow StbView::func_row(size_t i) const {
|
||||
FuncRow r;
|
||||
if (ok_ && i < n_funcs_) {
|
||||
const uint8_t* p = buf_ + get_le32(buf_ + 56) + i * kFuncRowSize;
|
||||
r.nregs = get_le32(p + 0);
|
||||
r.code_offset = get_le32(p + 4);
|
||||
r.code_len = get_le32(p + 8);
|
||||
r.nregs = get_le32(p + kFuncNregsOff);
|
||||
r.code_offset = get_le32(p + kFuncCodeOff);
|
||||
r.code_len = get_le32(p + kFuncLenOff);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
@@ -359,7 +359,7 @@ std::string StbView::model_id() const {
|
||||
if (!ok_) {
|
||||
return "";
|
||||
}
|
||||
std::string s(reinterpret_cast<const char*>(buf_ + 72), kModelIdSize);
|
||||
std::string s(reinterpret_cast<const char*>(buf_ + kOffModelId), kModelIdSize);
|
||||
const size_t z = s.find('\0');
|
||||
if (z != std::string::npos) {
|
||||
s.resize(z);
|
||||
@@ -371,7 +371,7 @@ std::string StbView::model_id() const {
|
||||
bool StbView::model_matches(const std::string& name, uint32_t version) const {
|
||||
char want[kModelIdSize];
|
||||
fill_model_id(name, version, want);
|
||||
return std::memcmp(buf_ + 72, want, kModelIdSize) == 0;
|
||||
return std::memcmp(buf_ + kOffModelId, want, kModelIdSize) == 0;
|
||||
}
|
||||
|
||||
/// 文件尾 32 字节 SHA-256 校验(对文件尾之前全部内容重算;未 ok() 时返回 false)。
|
||||
|
||||
Reference in New Issue
Block a user