Skip to content

aot cache file integrity - #403

Open
baibaidashixiong wants to merge 4 commits into
lat-opensource:masterfrom
baibaidashixiong:pr/aot-cache-file-integrity
Open

aot cache file integrity#403
baibaidashixiong wants to merge 4 commits into
lat-opensource:masterfrom
baibaidashixiong:pr/aot-cache-file-integrity

Conversation

@baibaidashixiong

Copy link
Copy Markdown
Contributor

Summary / 变更说明

English

Improve the correctness and resource handling of LATX AOT cache readers.

  • Replace the previous strstr()-based footer check with a shared exact-byte
    validator. The old implementation passed a fixed-width, non-NUL-terminated
    buffer to strstr(), which could read beyond the footer and make cache
    validation depend on unrelated memory.
  • Reject truncated AOT cache files before accessing aot_header.
  • Consolidate cleanup paths in get_tb_num() so mapped memory and file streams
    are released consistently when a cache is rejected.
  • Handle open(), fdopen(), fseek(), ftell(), and mmap() failures
    without leaking resources.
  • Check mmap() failures against MAP_FAILED.
  • Avoid closing a descriptor again after ownership has been transferred to and
    released by fclose().
  • Add regression coverage for exact footer matching, truncated cache headers,
    invalid footers, successful reader cleanup, and fdopen() failure handling.

These changes prevent out-of-bounds reads, descriptor leaks, double-close
behavior, and unsafe handling of malformed or incomplete AOT cache files.

中文

改进 LATX AOT 缓存读取流程的校验正确性和资源管理。

  • 使用共享的精确字节比较函数替换原有的 strstr() 尾部校验。原实现会将
    固定长度且未以 NUL 结尾的缓冲区传给 strstr(),可能越过缓存尾部读取,
    导致校验结果受到无关内存内容影响。
  • 在访问 aot_header 前检查缓存文件长度,拒绝头部不完整的缓存文件。
  • 统一 get_tb_num() 的退出和清理路径,确保缓存被拒绝时正确释放映射和
    文件流。
  • 正确处理 open()fdopen()fseek()ftell()mmap() 的失败
    情况,避免资源泄漏。
  • 使用 MAP_FAILED 判断 mmap() 是否失败。
  • 避免文件描述符交由 FILE 管理并经 fclose() 关闭后再次执行 close()
  • 添加回归测试,覆盖精确尾部匹配、缓存头截断、无效尾部、成功加载后的
    reader 清理以及 fdopen() 失败等场景。

这些修改可避免越界读取、文件描述符泄漏、重复关闭,以及对损坏或未完整
写入的 AOT 缓存文件进行不安全处理。

Validation / 验证

  • Confirmed that the new and extended unit tests cover:
    • exact AOT footer matching;
    • equal-length footer mismatches;
    • footers longer than the file;
    • invalid-footer and truncated-header cache rejection;
    • successful reader cleanup without closing a reused descriptor twice;
    • closing the original descriptor exactly once when fdopen() fails.
  • 已确认新增及扩展的单元测试覆盖:
    • AOT 文件尾的精确匹配;
    • 等长但内容不同的文件尾;
    • 长于文件本身的待匹配文件尾;
    • 无效文件尾及缓存头截断时的拒绝和清理流程;
    • 成功读取后不会重复关闭已被复用的文件描述符;
    • fdopen() 失败时原始文件描述符仅关闭一次。

Checklist / 检查项

  • I have read CONTRIBUTING.md. / 我已阅读 CONTRIBUTING.md
  • Every commit contains a DCO sign-off (git commit -s). /
    每个提交都包含 DCO 签署(git commit -s)。
  • I have included relevant build or test results, or explained why they
    are not applicable. /
    我已提供相关构建或测试结果,或说明了不适用的原因。

AOT readers copied a fixed-width footer into a non-NUL-terminated buffer and passed it to strstr(). That can read beyond the footer and make cache acceptance depend on unrelated memory.

Centralize footer validation in the AOT file helper and compare the exact stored byte span before either counting or loading an AOT cache. Close a rejected counting reader so the new shared validation preserves the previous stream ownership.

Signed-off-by: zqz <OrzZzznn@gmail.com>
Exercise the shared AOT footer validator with an exact footer, an equal-length byte mismatch, and a requested footer longer than the file. The coverage stays in the existing file-context test target and does not require a guest runtime or a system cache.

Signed-off-by: zqz <OrzZzznn@gmail.com>
Validate cache file size before reading an AOT header and route every get_tb_num() reader exit through one mapping and stream cleanup path.

Also handle open and fdopen failures in both readers, use MAP_FAILED for mmap failure checks, and avoid closing the descriptor again after fclose() in aot_load().

Signed-off-by: zqz <OrzZzznn@gmail.com>
Exercise invalid-footer and truncated-header cache paths repeatedly, then verify successful loading closes its stream without closing the descriptor twice. Also inject an fdopen() failure to verify that aot_load() closes the original descriptor exactly once.

Signed-off-by: zqz <OrzZzznn@gmail.com>
@baibaidashixiong
baibaidashixiong force-pushed the pr/aot-cache-file-integrity branch from 1f7018b to 429a95d Compare August 16, 2026 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant