Skip to content

Add optional full-file SHA-256 verification - #258

Merged
leehack merged 2 commits into
mainfrom
feat/file-sha256-wrapper
Nov 30, 2026
Merged

leehack merged 2 commits into
mainfrom
feat/file-sha256-wrapper

Conversation

@leehack

@leehack leehack commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

Add a versioned C API for verifying a downloaded model before backend initialization. The wrapper checks the opened file’s initial length, reads in 256 KiB chunks and probes EOF. The caller’s digest buffer is written only after a successful read and close.

Refs leehack/llamadart#2433.

Changelog

  • Add optional native full-file SHA-256 hashing with cancellation and ARM SHA-2 acceleration.

ABI and scope

uint32_t llama_dart_file_sha256_api_version(void);
enum llama_dart_file_sha256_status llama_dart_file_sha256(
    const char *path, uint8_t out_digest[32], const int8_t *cancel_flag);

Look up the probe and require version 1. Supply a stable regular file at a UTF-8 path and a null or live atomic cancellation byte. Status values are 0 success, 1 invalid argument, 2 open failure, 3 I/O or file-type error, 4 premature EOF, 5 cancelled, 6 file grew and 7 allocation failure. A successful hash is over bytes read from the opened handle. Callers must prevent concurrent replacement or rewriting when publishing that file.

The portable compressor and padding come from the pinned llama.cpp public-domain SHA-256 implementation. Android and Linux arm64 check HWCAP_SHA2 before entering the accelerated routine. Apple arm64 uses the same routine. There is no new runtime dependency or native release version change.

Validation

At 314022cbbeecc2bb37281e17d32ea632ee388071, the Release suite passed 117/117 on macOS arm64, the scalar contract passed under Rosetta, and both the macOS and Android arm64 libraries passed all 122 export checks. Independent mutations removing regular-file validation, cancellation after EOF, or the close-result check each failed the contract. Compiling the test with NDEBUG produced the expected compile error. The Windows CI contract checks directory status 2.

Initial implementation measurements at 975dd57:

  • macOS arm64 Release, CPU configuration: 117/117 CTests. Tests cover known answers, 256 KiB read boundaries, a sparse file, concurrent callers, cancellation and injected I/O errors both after a prefix and on the final EOF check.
  • Portable compressor: the contract also passed in an x86_64 executable under Rosetta.
  • Android arm64-v8a OpenCL, NDK 28.2.13676358: cross-compiled the wrapper and verified its 122 required dynamic exports. The bundle’s seven CPU variants and 16 KiB ELF load alignments passed artifact validation. macOS Mach-O passed the same export check.
  • Python tooling: 519 tests ran successfully with one skip.
  • On Apple M4 Max, the Release benchmark verified the same 1 GiB byte pattern as the earlier Dart/JDK comparison in 0.4750, 0.4755 and 0.4755 s (2,260.42, 2,258.19 and 2,258.02 MB/s). The median was 11.2× the earlier Dart AOT median of 201.06 MB/s. Every run matched the reference digest. Android device throughput remains unmeasured.

@leehack

leehack commented Nov 30, 2026

Copy link
Copy Markdown
Owner Author

Independent deep audit (uninvolved session) at 975dd57 and delta audit at 314022c: clean, no P1/P2/P3. 1,346 random files match shasum on the ARM SHA-2 and portable x86_64 paths and under ASan/UBSan/TSan; a 5 GiB sparse file past 4 GiB matches; every failure leaves the caller's digest buffer untouched; 2,270 MB/s median on 1 GiB (M4 Max); the delta added FIFO, /dev/null, directory, cancel-at-EOF and close-failure tests and an NDEBUG guard, and removing each guard now fails a test. All 29 checks pass at the head.

@leehack
leehack marked this pull request as ready for review November 30, 2026 14:34
@leehack
leehack merged commit 1e02a03 into main Nov 30, 2026
33 of 34 checks passed
leehack added a commit to leehack/llamadart that referenced this pull request Nov 30, 2026
## Summary

- Pin the default llama.cpp bundles and Apple SwiftPM XCFramework to
[llamadart-native
v1.1.0-4](https://github.com/leehack/llamadart-native/releases/tag/v1.1.0-4).
The only change from `v1.1.0-3` is the file SHA-256 export
(`llama_dart_file_sha256_api_version`, `llama_dart_file_sha256`) from
[llamadart-native#258](leehack/llamadart-native#258).
Same upstream llama.cpp
`v1.1.0@171527522610996f5f0acf12334e185156ca8f44`.
- Regenerate bindings on Linux (Dart 3.13.1 container). The diff is
additive: the two exports and `llama_dart_file_sha256_status`. The
Android download fast path from
[#2470](#2470) now resolves
against the pinned runtime.
- Bind the Android qualification tools and default AOT consumer
qualification to `v1.1.0-4`; keep the Web bridge pin aligned (`v0.1.96`,
same upstream commit).
- `release/0.23.x` is untouched. Companion versions and core floors are
left to next-minor release prep.

## Changelog

- [core] Update the default llama.cpp native runtime to `v1.1.0-4`,
which provides the native file SHA-256 export used for Android model
verification.
- [packages/llamadart_llama_cpp_flutter] Update the Apple SwiftPM
XCFramework to `v1.1.0-4`.

## Production-readiness scope
- **User-facing scope:** Native GGUF consumers resolve the new runtime;
Android model downloads can hash with the native wrapper (API version 1)
instead of the portable path.
- **Supported platforms/paths:** Existing llama.cpp native-assets
targets and the matching Flutter Apple SwiftPM companion.
- **Unsupported or intentionally unavailable paths:** A runtime without
the exports (earlier tags, custom overrides) keeps portable hashing. The
hook still rejects incompatible Apple companion pins with the existing
upgrade diagnostic.
- **Out of scope / follow-ups:** Package versions and dependency floors
are handled during next-minor release prep. `--release-prep`
verification reports the expected pending companion bump.

## Completeness checklist
- [x] Declared scope is fully implemented, or explicitly reduced above.
- [x] Unsupported platform/option combinations fail loudly with
actionable diagnostics or are clearly documented as unavailable.
- [x] Public API docs, README/website docs, examples, support matrices,
and the PR changelog section are updated where relevant.
- [x] Regression coverage covers the original issue plus key
negative/version-skew paths where relevant. The native symbol suite
checks declarations, exports, API version and hashing; `-3` is the
missing-export control.
- [x] Validation coverage registry: N/A, no public capability change.
- [x] Security/privacy review completed: pin metadata and generated code
only.
- [x] Follow-up work tracked: None beyond release prep.

## PR type guidance
Native artifact repin. The owner release adds two exports; behavior is
exercised by the native symbol suite and a host smoke.

## Test Plan
- [x] `dart run tool/prepare_workspace.dart`
- [x] `python3 tool/ci/check_format.py`, `dart analyze`, platform
boundaries, `git diff --check`
- [x] `dart test -p vm -j 1 --exclude-tags local-only` via the coverage
row
- [x] `dart test -p chrome --exclude-tags local-only`
- [x] `coverage-lib`
- [x] Native sync Python suites, native release digests, bridge tag with
`--verify-manifest`, release-doc verifier, docs build and links,
validation package tests

## Matrix Evidence

| Matrix row | Scope covered | Platform / model / backend | Result |
Evidence / notes |
| --- | --- | --- | --- | --- |
| `static-format-analyze` | Format, analysis, import boundaries |
Flutter 3.47.1 / Dart 3.13.1 | PASS | Root checks complete |
| `native-release-digests` | Pin tables vs published release |
`v1.1.0-4`, 12 bundles | PASS | Match `SHA256SUMS`; also checked against
the evidence record |
| `native-sync-python-suites` | Pin, header archive, binding sync |
Python 3.15 | PASS | 160 + 14 + 23 + 5 tests; 70 AOT qualification
tests; 3 Android prepare tests |
| `webgpu-bridge-tag-consistency` | Bridge tag vs native pin, immutable
manifest | Bridge `v0.1.96` | PASS | Verified with `--verify-manifest` |
| `release-doc-version-consistency` | Core and companion docs | Current
checkout | PASS | `--release-prep` reports the expected pending
companion bump, as for `v1.1.0-3` |
| `docs-site` | Site build and links | Jaspr | PASS | Build and link
validation passed |
| Android qualification pins | Archive and headers identity |
`packages/llamadart_validation` | PASS | 655 tests passed |
| Apple XCFramework | Checksum, slice exports | macOS, iOS, iOS
simulator | PASS | `swift package compute-checksum` equals the pin;
every slice exports both new symbols and `grammar_top_k` |
| Host smoke | Real `v1.1.0-4` macOS arm64 dylib through
`modelFileSha256` | macOS arm64 | PASS | API version 1, native status 0,
digests equal `shasum -a 256` for 0 B, 3 B, 1 MiB+17 B and 96 MiB+5 B.
The `v1.1.0-3` dylib has no exports and falls back. |
| `root-vm` / `coverage-lib` | Full VM suite and line coverage | macOS
arm64 | PASS | 16,751 passed, 136 skipped, 1 failed: the new symbol
test's own declaration check, fixed in `1d9be1b3d0` and passing alone.
Library line coverage 93.88% (38,224/40,717). |
| `root-chrome` | Browser-compatible suite | Chrome | PASS | 5,423 tests
passed, one skipped. |
| Apple SwiftPM app builds | `example/chat_app` release builds with the
companion | macOS arm64/x86_64, iOS arm64 (no codesign) | PASS |
Packaged `llama.framework` exports both new symbols in every slice
built. |
| `high-risk-exact-head-independent-qa` | Independent blocking review |
Exact head and base | PASS | auditor audit clean at 33def83
(llamadart-evidence/pr-2481/audit.md) |

## Review Notes
- Independent review status: accepted: independent audit of exact head
33def83 against main 377d97d found no blocking issues.
- CI status / head SHA: green at `33def83d73` (one macOS `worker_test`
quarantine timeout passed on rerun) (merged `main` `377d97d2fa` into
`1d9be1b3d0`; evidence above ran at `1d9be1b3d0`).

## High-risk regression review
- **Classification:** High-risk (`artifactConsumer`, `backendRuntime`,
`regressionPolicy`)
- **Implementation task:** Default native artifact repin and generated
FFI declarations.
- **Independent blocking QA task:** Completed by the independent audit
(clean).
- **Exact head / current base:** Head `33def83d73`, base `main`
`377d97d2fa`.
- **Production-branch deletion, bypass, or miswire proof:** Audit
confirmed; the symbol test fails without the generated declarations or
exports.
- **Affected-family real-model/artifact evidence:** Published `v1.1.0-4`
binaries and XCFramework ([release
run](https://github.com/leehack/llamadart-native/actions/runs/46456519420));
host SHA-256 smoke above. No model generation: only the hashing exports
changed.
- **Explicit unavailable-family or other N/A evidence:** No Android
device run; the Android path is covered by the existing injected-binding
tests and the real-library host smoke.
- **Known PR-caused P1 regressions:** 0 (implementation)
- **Unresolved review threads:** 0 at opening.
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