field_ios

In-the-field app for Radroots on iOS
git clone https://radroots.dev/git/field_ios.git
Log | Files | Refs | README | LICENSE

ffi_artifacts.py (9151B)


      1 """Exact staged native artifact inventory and source-tuple verification."""
      2 
      3 from __future__ import annotations
      4 
      5 import hashlib
      6 import plistlib
      7 import re
      8 from pathlib import Path
      9 from typing import Any
     10 
     11 import ffi_provenance as provenance
     12 import ffi_source as source
     13 import ffi_notices as notices
     14 import package_contract as contract
     15 
     16 TARGETS = ("aarch64-apple-ios", "aarch64-apple-ios-sim", "aarch64-apple-darwin")
     17 MAX_ARTIFACT_BYTES = 256 * 1024 * 1024
     18 FRAMEWORK = "TeraFFI.xcframework"
     19 MODULE = "TeraKitBindings"
     20 MANIFEST = "provenance.json"
     21 
     22 
     23 def expected_paths() -> set[str]:
     24     paths = {
     25         "generated/TeraKitBindings.swift",
     26         "generated/TeraFFI.h",
     27         "generated/TeraFFI.modulemap",
     28         "headers/TeraFFI.h",
     29         "headers/module.modulemap",
     30         f"{FRAMEWORK}/Info.plist",
     31         "api/TeraKitBindings.symbols.json",
     32         "abi_symbols.json",
     33         notices.ARTIFACT_PATH,
     34     }
     35     for target in TARGETS:
     36         extension = "dylib" if target == TARGETS[-1] else "a"
     37         paths.add(f"native/{target}/libtera_ffi.{extension}")
     38         paths.add(f"source/{target}.json")
     39     for platform in ("ios-arm64", "ios-arm64-simulator"):
     40         for relative in (
     41             "libtera_ffi.a",
     42             "Headers/TeraFFI.h",
     43             "Headers/module.modulemap",
     44         ):
     45             paths.add(f"{FRAMEWORK}/{platform}/{relative}")
     46     return paths
     47 
     48 
     49 def regular_path(root: Path, relative: str) -> Path:
     50     path = Path(relative)
     51     if path.is_absolute() or str(path) != relative or ".." in path.parts:
     52         raise source.ProvenanceError("native artifact path is invalid")
     53     current = root
     54     for part in path.parts:
     55         current = current / part
     56         if current.is_symlink():
     57             raise source.ProvenanceError("native artifact path contains a symlink")
     58     if not current.is_file():
     59         raise source.ProvenanceError("native artifact file is missing")
     60     return current
     61 
     62 
     63 def file_record(root: Path, relative: str) -> dict[str, Any]:
     64     path = regular_path(root, relative)
     65     size = path.stat().st_size
     66     if size <= 0 or size > MAX_ARTIFACT_BYTES:
     67         raise source.ProvenanceError("native artifact exceeds its byte bound")
     68     digest = hashlib.sha256()
     69     with path.open("rb") as handle:
     70         while data := handle.read(1024 * 1024):
     71             digest.update(data)
     72     if path.stat().st_size != size:
     73         raise source.ProvenanceError("native artifact changed during verification")
     74     return {"path": relative, "bytes": size, "sha256": digest.hexdigest()}
     75 
     76 
     77 def inventory(root: Path) -> list[dict[str, Any]]:
     78     paths = set()
     79     for path in root.rglob("*"):
     80         if path.is_symlink():
     81             raise source.ProvenanceError("native artifact inventory contains a symlink")
     82         if path.is_file() and path.relative_to(root).as_posix() != MANIFEST:
     83             paths.add(path.relative_to(root).as_posix())
     84     if paths != expected_paths():
     85         raise source.ProvenanceError("native artifact inventory differs")
     86     return [file_record(root, relative) for relative in sorted(paths)]
     87 
     88 
     89 def validate_module_files(root: Path) -> None:
     90     generated = root / "generated"
     91     header = contract._read_regular(generated / "TeraFFI.h")
     92     modulemap = contract._read_regular(generated / "TeraFFI.modulemap")
     93     if b"module TeraFFI {" not in modulemap or b'header "TeraFFI.h"' not in modulemap:
     94         raise source.ProvenanceError("generated native module identity differs")
     95     swift = contract._read_regular(generated / "TeraKitBindings.swift")
     96     if b"import TeraFFI" not in swift:
     97         raise source.ProvenanceError("generated Swift module does not import its FFI")
     98     for prefix in (
     99         "headers",
    100         f"{FRAMEWORK}/ios-arm64/Headers",
    101         f"{FRAMEWORK}/ios-arm64-simulator/Headers",
    102     ):
    103         if contract._read_regular(root / prefix / "TeraFFI.h") != header:
    104             raise source.ProvenanceError(
    105                 "packaged FFI header differs from generated header"
    106             )
    107         if contract._read_regular(root / prefix / "module.modulemap") != modulemap:
    108             raise source.ProvenanceError(
    109                 "packaged FFI module map differs from generated module map"
    110             )
    111 
    112 
    113 def validate_framework(root: Path) -> None:
    114     info = plistlib.loads(contract._read_regular(root / FRAMEWORK / "Info.plist"))
    115     libraries = info.get("AvailableLibraries", [])
    116     expected = {
    117         "ios-arm64": None,
    118         "ios-arm64-simulator": "simulator",
    119     }
    120     if len(libraries) != 2 or {
    121         item.get("LibraryIdentifier") for item in libraries
    122     } != set(expected):
    123         raise source.ProvenanceError("XCFramework platform inventory differs")
    124     for item in libraries:
    125         validate_platform(item, expected[item["LibraryIdentifier"]])
    126     for target, platform in zip(TARGETS[:2], expected, strict=True):
    127         first = file_record(root, f"native/{target}/libtera_ffi.a")
    128         packaged = file_record(root, f"{FRAMEWORK}/{platform}/libtera_ffi.a")
    129         if (first["bytes"], first["sha256"]) != (packaged["bytes"], packaged["sha256"]):
    130             raise source.ProvenanceError(
    131                 "XCFramework library differs from its built target"
    132             )
    133 
    134 
    135 def validate_platform(item: dict[str, Any], variant: str | None) -> None:
    136     if (
    137         item.get("SupportedArchitectures") != ["arm64"]
    138         or item.get("SupportedPlatform") != "ios"
    139         or item.get("SupportedPlatformVariant") != variant
    140         or item.get("LibraryPath") != "libtera_ffi.a"
    141         or item.get("HeadersPath") != "Headers"
    142     ):
    143         raise source.ProvenanceError("XCFramework library contract differs")
    144 
    145 
    146 def validate_abi(root: Path) -> None:
    147     symbols = contract._read_json(root / "abi_symbols.json")
    148     if set(symbols) != set(TARGETS):
    149         raise source.ProvenanceError("native ABI target inventory differs")
    150     host = symbols[TARGETS[-1]]
    151     if not isinstance(host, list) or not host or host != sorted(set(host)):
    152         raise source.ProvenanceError("native ABI symbols are invalid")
    153     if any(symbols[target] != host for target in TARGETS):
    154         raise source.ProvenanceError("native target ABI symbols differ")
    155     header = contract._read_regular(root / "generated/TeraFFI.h").decode()
    156     declared = set(
    157         re.findall(r"\b((?:ffi|uniffi)_tera_ffi_[A-Za-z0-9_]+)\s*\(", header)
    158     )
    159     if not declared or not declared.issubset(host):
    160         raise source.ProvenanceError(
    161             "generated header declares an unavailable native symbol"
    162         )
    163 
    164 
    165 def manifest(root: Path, records: dict[str, dict[str, Any]]) -> dict[str, Any]:
    166     if set(records) != set(TARGETS):
    167         raise source.ProvenanceError("producer source target inventory differs")
    168     source_tree = records[TARGETS[0]]["source"]["tree"]
    169     redistribution = records[TARGETS[0]]["redistribution"]
    170     notices.verify_material(root / notices.ARTIFACT_PATH, redistribution)
    171     for target, record in records.items():
    172         if (
    173             record["source"]["tree"] != source_tree
    174             or record["build"]["target"] != target
    175         ):
    176             raise source.ProvenanceError("producer source tuples disagree")
    177         notices.validate_binding(record["redistribution"])
    178         if record["redistribution"] != redistribution:
    179             raise source.ProvenanceError("producer redistribution tuples disagree")
    180         for key in ("repository", "revision"):
    181             contract._exact(
    182                 redistribution[key],
    183                 record["foundation"][key],
    184                 "redistribution producer identity",
    185             )
    186         provenance.verify_record(
    187             contract._read_regular(root / "source" / f"{target}.json"), record
    188         )
    189     validate_module_files(root)
    190     validate_framework(root)
    191     validate_abi(root)
    192     validate_api(root)
    193     files = inventory(root)
    194     notices.verify_inventory(files, redistribution, notices.ARTIFACT_PATH)
    195     return {
    196         "schema": "radroots.artifact-manifest.v3",
    197         "product": "tera",
    198         "target": "ios",
    199         "language": "swift",
    200         "external_names": ["TeraFFI", "TeraKitBindings"],
    201         "source": {
    202             "repository": records[TARGETS[0]]["repository"],
    203             "tree": source_tree,
    204         },
    205         "source_records": {target: f"source/{target}.json" for target in TARGETS},
    206         "redistribution": redistribution,
    207         "files": files,
    208         "disposition": "local_candidate_not_installed",
    209     }
    210 
    211 
    212 def validate_api(root: Path) -> None:
    213     value = contract._read_json(root / "api/TeraKitBindings.symbols.json")
    214     if (
    215         value.get("schema") != "radroots.swift-api-snapshot.v1"
    216         or value.get("module", {}).get("name") != MODULE
    217         or not isinstance(value.get("symbols"), list)
    218         or not value["symbols"]
    219     ):
    220         raise source.ProvenanceError("generated Swift API snapshot identity differs")
    221 
    222 
    223 def check(root: Path, records: dict[str, dict[str, Any]]) -> dict[str, Any]:
    224     expected = manifest(root, records)
    225     if contract._read_regular(root / MANIFEST) != provenance.encoded(expected):
    226         raise source.ProvenanceError("native artifact provenance is stale")
    227     return expected