-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(abi): binary ABI + Zig↔Idris layout cross-check #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b048a28
chore(maintenance): reconcile licence headers + honest state (correct…
claude fde37c4
merge: bring claude/admiring-babbage-4l40ko up to main (#37 proofs + …
claude 5d498cc
feat(abi): implement binary ABI + Zig↔Idris layout cross-check
claude f853278
fix(abi): resolve Hypatia new-alert errors (banned Python, unchecked …
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| #!/usr/bin/env python3 | ||
Check failureCode scanning / Hypatia Hypatia cicd_rules: banned_language_file Error
Python file detected -- banned language
|
||
| # SPDX-License-Identifier: AGPL-3.0-or-later | ||
| # Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> | ||
| # | ||
| # gen_abi_expected.py — extract the canonical C-ABI layout numbers from the | ||
| # machine-checked Idris2 model (src/interface/abi/Layout.idr) and emit a Zig | ||
| # source file (src/interface/ffi/src/abi_layout_expected.zig) consumed by the | ||
| # Zig-side cross-check (abi_layout.zig). | ||
| # | ||
| # Layout.idr is the single source of truth: it carries the proofs (NoOverlap, | ||
| # AllFieldsAligned, SizeAligned, SizeCoversFields). This script lifts the same | ||
| # `MkFieldDesc name offset size align` / `MkStructLayout [...] total align` | ||
| # constants into Zig so the compiler can assert `@offsetOf`/`@sizeOf`/`@alignOf` | ||
| # of the canonical extern structs agree with the proven model. | ||
| # | ||
| # Run from the repository root: | ||
| # python3 scripts/gen_abi_expected.py | ||
| # The build/CI re-runs this and `git diff --exit-code`s the result to guarantee | ||
| # the Zig expectations never drift from the Idris proofs. | ||
|
|
||
| import re | ||
| import sys | ||
| import pathlib | ||
|
|
||
| # Idris layout variable (lowerCamel, without the "Layout" suffix) -> Zig struct | ||
| # type name. Drives both extraction and the emitted const names. | ||
| STRUCTS = { | ||
| "serverHandle": "ServerHandle", | ||
| "probeResult": "ProbeResult", | ||
| "configField": "ConfigField", | ||
| "a2mlConfig": "A2MLConfig", | ||
| "gameProfile": "GameProfile", | ||
| "serverOctad": "ServerOctad", | ||
| "fingerprint": "Fingerprint", | ||
| "driftReport": "DriftReport", | ||
| } | ||
|
|
||
| FIELD_RE = re.compile( | ||
| r'MkFieldDesc\s+"(?P<name>\w+)"\s+(?P<offset>\d+)\s+(?P<size>\d+)\s+(?P<align>\d+)' | ||
| ) | ||
|
|
||
|
|
||
| def parse_layout(text, var): | ||
| """Return (fields, total_size, struct_align) for `<var>Layout`.""" | ||
| # Match: <var>Layout = MkStructLayout [ ...fields... ] <total> <align> | ||
| block_re = re.compile( | ||
| r'\b' + re.escape(var) + r'Layout\s*=\s*MkStructLayout\s*\[(?P<body>.*?)\]\s*' | ||
| r'(?P<total>\d+)\s+(?P<align>\d+)', | ||
| re.DOTALL, | ||
| ) | ||
| m = block_re.search(text) | ||
| if not m: | ||
| raise SystemExit(f"error: could not find {var}Layout in Layout.idr") | ||
| fields = [] | ||
| for fm in FIELD_RE.finditer(m.group("body")): | ||
| name = fm.group("name") | ||
| if name.startswith("padding"): | ||
| continue # implicit C padding; not a real extern-struct field | ||
| fields.append( | ||
| (name, int(fm.group("offset")), int(fm.group("size")), int(fm.group("align"))) | ||
| ) | ||
| return fields, int(m.group("total")), int(m.group("align")) | ||
|
|
||
|
|
||
| def main(): | ||
| root = pathlib.Path(__file__).resolve().parent.parent | ||
| layout_idr = root / "src" / "interface" / "abi" / "Layout.idr" | ||
| out_zig = root / "src" / "interface" / "ffi" / "src" / "abi_layout_expected.zig" | ||
|
|
||
| text = layout_idr.read_text(encoding="utf-8") | ||
|
|
||
| out = [] | ||
| out.append("// SPDX-License-Identifier: AGPL-3.0-or-later") | ||
| out.append("// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk>") | ||
| out.append("//") | ||
| out.append("// @generated by scripts/gen_abi_expected.py from src/interface/abi/Layout.idr") | ||
| out.append("// DO NOT EDIT. Regenerate with: python3 scripts/gen_abi_expected.py") | ||
| out.append("//") | ||
| out.append("// Canonical C-ABI layout numbers lifted from the machine-checked Idris2 model") | ||
| out.append("// (GSA.ABI.Layout). Consumed by abi_layout.zig to assert that the Zig extern") | ||
| out.append("// structs agree, byte-for-byte, with the proven Idris layout.") | ||
| out.append("") | ||
| out.append("pub const Field = struct {") | ||
| out.append(" name: []const u8,") | ||
| out.append(" offset: u32,") | ||
| out.append(" size: u32,") | ||
| out.append(" alignment: u32,") | ||
| out.append("};") | ||
| out.append("") | ||
| out.append("pub const StructExpect = struct {") | ||
| out.append(" name: []const u8,") | ||
| out.append(" total_size: u32,") | ||
| out.append(" struct_align: u32,") | ||
| out.append(" fields: []const Field,") | ||
| out.append("};") | ||
| out.append("") | ||
|
|
||
| names = [] | ||
| for var, zig_name in STRUCTS.items(): | ||
| fields, total, align = parse_layout(text, var) | ||
| names.append(zig_name) | ||
| out.append(f"pub const {zig_name} = StructExpect{{") | ||
| out.append(f' .name = "{zig_name}",') | ||
| out.append(f" .total_size = {total},") | ||
| out.append(f" .struct_align = {align},") | ||
| out.append(" .fields = &.{") | ||
| for (fn, off, sz, al) in fields: | ||
| out.append( | ||
| f' .{{ .name = "{fn}", .offset = {off}, .size = {sz}, .alignment = {al} }},' | ||
| ) | ||
| out.append(" },") | ||
| out.append("};") | ||
| out.append("") | ||
|
|
||
| out.append("pub const all = [_]StructExpect{ " + ", ".join(names) + " };") | ||
| out.append("") | ||
|
|
||
| out_zig.write_text("\n".join(out), encoding="utf-8") | ||
| print(f"wrote {out_zig.relative_to(root)} ({len(names)} structs)") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.