interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tools / aienos_lock_source.sh

167 lines · 9024 bytes Raw Blame History
1#!/bin/bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# aienos_lock_source.sh -- resolve and prove the aienos source pinned by aienos.lock.
18#
19# The trusted capability root is the native C library at the FULL commit in aienos.lock
20# (spec C3). A directory name, an abbreviated SHA or an environment override is not proof
21# of that identity. This tool proves it from git objects:
22# * the lock must be one full 40-hex commit id;
23# * a repository counts only if it holds that commit object; replace refs and grafts
24# are disabled (GIT_NO_REPLACE_OBJECTS) and every object read is re-hashed, so the
25# content returned is the content the commit id names;
26# * a directory counts only if every file under the asked subpaths has the raw bytes
27# (no filters, no attributes) and mode of the locked commit and no other file (build
28# outputs included) sits beside them.
29#
30# Usage:
31# aienos_lock_source.sh identity one JSON object: lock, repo, tree, status
32# aienos_lock_source.sh show <path> file content at the locked commit
33# aienos_lock_source.sh blob <path> blob id of <path> at the locked commit
34# aienos_lock_source.sh verify-dir <dir> <subpath>... prove <dir> holds the locked subpaths
35# aienos_lock_source.sh materialize <dir> <subpath>... extract missing subpaths into <dir>
36# (only if empty of them), then verify-dir
37# Exit status: 0 proven; 1 verified mismatch or verified absence (the locked commit was read
38# and the path is missing, or the directory differs); 2 unavailable (no repository holds
39# the locked commit, or the lock is malformed). 2 never means "absent".
40#
41# Environment:
42# AIENOS_LOCK_FILE lock file (default <omega>/aienos.lock)
43# AIENOS_LOCK lock value instead of the file (tests only; must still be full 40-hex)
44# AIENOS_LOCK_REPO repository to read objects from (default <omega>/../aienos-argus-cap);
45# a place to look, not proof: the commit object is the proof
46# AIENOS_R7_DIR if it lies inside a git repository, that repository is also tried
47set -u
48export GIT_NO_REPLACE_OBJECTS=1 GIT_GRAFT_FILE=/nonexistent/aienos-lock-source-grafts
49HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
50
51die2() { echo "aienos_lock_source: UNAVAILABLE: $*" >&2; exit 2; }
52die1() { echo "aienos_lock_source: MISMATCH: $*" >&2; exit 1; }
53
54LOCK=${AIENOS_LOCK:-$(head -n 1 "${AIENOS_LOCK_FILE:-$HERE/aienos.lock}" 2>/dev/null)}
55[[ "$LOCK" =~ ^[0-9a-f]{40}$ ]] || die2 "aienos.lock is not one full 40-hex commit id: '${LOCK}'"
56
57# rehash <repo> <type> <id>: the object's content hashes to its id.
58rehash() {
59 local got
60 got=$(git -C "$1" cat-file "$2" "$3" 2>/dev/null | git hash-object -t "$2" --stdin 2>/dev/null) || return 1
61 [ "$got" = "$3" ]
62}
63
64REPO=""
65for cand in "${AIENOS_LOCK_REPO:-$HERE/../aienos-argus-cap}" \
66 "$( [ -n "${AIENOS_R7_DIR:-}" ] && [ -d "$AIENOS_R7_DIR" ] && git -C "$AIENOS_R7_DIR" rev-parse --show-toplevel 2>/dev/null)"; do
67 [ -n "$cand" ] && [ -d "$cand" ] || continue
68 [ "$(git -C "$cand" rev-parse --verify -q "$LOCK^{commit}" 2>/dev/null)" = "$LOCK" ] || continue
69 rehash "$cand" commit "$LOCK" || continue
70 REPO=$(cd "$cand" && pwd); break
71done
72TREE=""
73if [ -n "$REPO" ]; then
74 TREE=$(git -C "$REPO" rev-parse --verify -q "$LOCK^{tree}")
75 rehash "$REPO" tree "$TREE" || die2 "tree object $TREE of $LOCK fails its hash in $REPO"
76fi
77need_repo() { [ -n "$REPO" ] || die2 "no repository holds commit $LOCK (AIENOS_LOCK_REPO=${AIENOS_LOCK_REPO:-unset})"; }
78
79# check_trees <subpath>: every tree object on the way to and under <subpath> re-hashes.
80check_trees() {
81 local mode type id path
82 while read -r mode type id path; do
83 [ "$type" = tree ] || continue
84 rehash "$REPO" tree "$id" || die2 "tree $id ($path) fails its hash"
85 done < <(git -C "$REPO" ls-tree -r -t "$LOCK" -- "$1")
86}
87
88cmd=${1:-}; shift || true
89case "$cmd" in
90identity)
91 if [ -n "$REPO" ]; then
92 printf '{"aienos_lock": "%s", "source": "git-object", "repo": "%s", "commit_tree": "%s", "status": "available"}\n' "$LOCK" "$REPO" "$TREE"
93 else
94 printf '{"aienos_lock": "%s", "source": "none", "repo": "", "commit_tree": "", "status": "unavailable"}\n' "$LOCK"
95 exit 2
96 fi ;;
97blob|show)
98 [ $# = 1 ] || die2 "usage: $cmd <path>"
99 need_repo
100 check_trees "$1"
101 id=$(git -C "$REPO" rev-parse --verify -q "$LOCK:$1") || die1 "$1 is absent at $LOCK"
102 [ "$(git -C "$REPO" cat-file -t "$id")" = blob ] || die1 "$1 is not a file at $LOCK"
103 rehash "$REPO" blob "$id" || die2 "blob $id ($1) fails its hash"
104 if [ "$cmd" = blob ]; then echo "$id"; else git -C "$REPO" cat-file blob "$id"; fi ;;
105verify-dir|materialize)
106 [ $# -ge 2 ] || die2 "usage: $cmd <dir> <subpath>..."
107 dir=$1; shift
108 need_repo
109 for s in "$@"; do
110 git -C "$REPO" rev-parse --verify -q "$LOCK:$s" >/dev/null || die1 "$s is absent at $LOCK"
111 check_trees "$s"
112 done
113 if [ "$cmd" = materialize ]; then
114 missing=()
115 for s in "$@"; do [ -e "$dir/$s" ] || missing+=("$s"); done
116 if [ ${#missing[@]} -gt 0 ]; then
117 mkdir -p "$dir" || die2 "cannot create $dir"
118 git -C "$REPO" archive "$LOCK" "${missing[@]}" | tar -x -C "$dir" || die2 "extraction into $dir failed"
119 fi
120 fi
121 [ -d "$dir" ] || die1 "$dir does not exist"
122 dir=$(cd "$dir" && pwd)
123 # Compare raw bytes, outside git's checkout machinery: no index, no ignore rules,
124 # no .gitattributes (a planted "* text eol=lf" would hide CRLF edits), no config
125 # filters. Each file is hashed with --no-filters and must equal the locked blob;
126 # its type and executable bit must equal the locked mode; and the subpaths must
127 # hold no other file (build outputs included: the authority is built out of tree).
128 tmpd=$(mktemp -d "${TMPDIR:-/tmp}/aienos-lock-vd.XXXXXX") || die2 "mktemp failed"
129 trap 'rm -rf "$tmpd"' EXIT
130 git -C "$REPO" ls-tree -r -z --full-tree "$LOCK" -- "$@" > "$tmpd/tree" || die2 "ls-tree $LOCK failed"
131 : > "$tmpd/want"; : > "$tmpd/regular"; : > "$tmpd/regular.ids"; bad=""
132 while IFS= read -r -d '' ent; do
133 meta=${ent%%$'\t'*}; path=${ent#*$'\t'}
134 read -r mode type id <<< "$meta"
135 case "$path" in *$'\n'*) die2 "path with a newline at $LOCK: refusing" ;; esac
136 printf '%s\n' "$path" >> "$tmpd/want"
137 f=$dir/$path
138 case "$mode" in
139 120000)
140 if [ -L "$f" ] && [ "$(readlink "$f" | tr -d '\n' | git -C "$REPO" hash-object --no-filters --stdin)" = "$id" ]; then :
141 else bad+="$path(symlink) "; fi ;;
142 100644|100755)
143 if [ -L "$f" ] || [ ! -f "$f" ]; then bad+="$path(not a file) "; continue; fi
144 if [ "$mode" = 100755 ] && [ ! -x "$f" ]; then bad+="$path(mode) "; continue; fi
145 if [ "$mode" = 100644 ] && [ -x "$f" ]; then bad+="$path(mode) "; continue; fi
146 printf '%s\n' "$f" >> "$tmpd/regular"; printf '%s\n' "$id" >> "$tmpd/regular.ids" ;;
147 *) die2 "$path has unsupported mode $mode ($type) at $LOCK" ;;
148 esac
149 done < "$tmpd/tree"
150 [ -s "$tmpd/want" ] || die1 "no files under $* at $LOCK"
151 if [ -s "$tmpd/regular" ]; then
152 git -C "$REPO" hash-object --no-filters --stdin-paths < "$tmpd/regular" > "$tmpd/regular.got" 2>/dev/null \
153 || die1 "cannot hash the files under $dir"
154 bad+=$(paste -d ' ' "$tmpd/regular.ids" "$tmpd/regular.got" "$tmpd/regular" \
155 | awk -v d="$dir/" '$1 != $2 { p = substr($0, length($1) + length($2) + 3); sub("^" d, "", p); printf "%s(content) ", p }')
156 fi
157 [ -z "$bad" ] || die1 "$dir differs from $LOCK: $(echo "$bad" | cut -c1-300)"
158 # Every non-directory entry under the subpaths must be a locked path.
159 (cd "$dir" && find "$@" ! -type d -print0) > "$tmpd/have0" 2>/dev/null || die1 "cannot list every entry under $dir (unreadable directory?)"
160 tr '\0' '\n' < "$tmpd/have0" | sort > "$tmpd/have"
161 sort -o "$tmpd/want" "$tmpd/want"
162 extra=$(comm -13 "$tmpd/want" "$tmpd/have")
163 [ -z "$extra" ] || die1 "$dir has files not in $LOCK (build outputs included; build out of tree): $(echo "$extra" | head -5 | tr '\n' ' ')"
164 echo "aienos_lock_source: $dir matches $LOCK for $*" >&2 ;;
165*)
166 die2 "unknown command '$cmd' (identity | show | blob | verify-dir | materialize)" ;;
167esac