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
omega-mv / om / sha256 / om2-evidence / audit / audit.py
| 1 | #!/usr/bin/env python3 |
| 2 | # L838 audit (oracle tooling, outside the delivered closure): every caller of sha256_init/update/final/hash |
| 3 | # in files tracked by git and built from this repo (src tools tests research om; evidence/ holds non-built |
| 4 | # snapshots and is listed separately). |
| 5 | import re, subprocess, sys, collections, json |
| 6 | ROOT = sys.argv[1] |
| 7 | files = subprocess.run(['git', '-C', ROOT, 'ls-files', '--', '*.c', '*.h', '*.cc', '*.cpp', '*.inc'], |
| 8 | capture_output=True, text=True).stdout.split() |
| 9 | CALL = re.compile(r'\bsha256_(init|update|final|hash)\s*\(') |
| 10 | |
| 11 | def strip_comments(s): |
| 12 | # keep offsets: replace comment and string bodies by spaces (newlines kept) |
| 13 | out = list(s); i = 0; n = len(s) |
| 14 | while i < n: |
| 15 | if s.startswith('/*', i): |
| 16 | j = s.find('*/', i + 2); j = n if j < 0 else j + 2 |
| 17 | for k in range(i, j): |
| 18 | if out[k] != '\n': out[k] = ' ' |
| 19 | i = j |
| 20 | elif s.startswith('//', i): |
| 21 | j = s.find('\n', i); j = n if j < 0 else j |
| 22 | for k in range(i, j): out[k] = ' ' |
| 23 | i = j |
| 24 | elif s[i] == '"' or s[i] == "'": |
| 25 | q = s[i]; j = i + 1 |
| 26 | while j < n and s[j] != q: |
| 27 | j += 2 if s[j] == '\\' else 1 |
| 28 | for k in range(i + 1, min(j, n)): |
| 29 | if out[k] != '\n': out[k] = ' ' |
| 30 | i = j + 1 |
| 31 | else: |
| 32 | i += 1 |
| 33 | return ''.join(out) |
| 34 | |
| 35 | def args_at(s, p): # s[p] == '(' |
| 36 | depth = 0; cur = []; args = []; i = p |
| 37 | while i < len(s): |
| 38 | c = s[i] |
| 39 | if c in '([{': |
| 40 | depth += 1 |
| 41 | if depth > 1: cur.append(c) |
| 42 | elif c in ')]}': |
| 43 | depth -= 1 |
| 44 | if depth == 0: |
| 45 | args.append(''.join(cur).strip()); return args, i |
| 46 | cur.append(c) |
| 47 | elif c == ',' and depth == 1: |
| 48 | args.append(''.join(cur).strip()); cur = [] |
| 49 | else: |
| 50 | cur.append(c) |
| 51 | i += 1 |
| 52 | return args, i |
| 53 | |
| 54 | def norm(a): |
| 55 | a = re.sub(r'\s+', ' ', a) |
| 56 | a = re.sub(r'\((const )?(uint8_t|unsigned char|char|void|uint32_t|uint64_t) ?\*\)', '', a) |
| 57 | return a.strip() |
| 58 | |
| 59 | def base(a): |
| 60 | a = norm(a).lstrip('&*( ') |
| 61 | m = re.match(r'[A-Za-z_][A-Za-z_0-9]*(\s*(->|\.)\s*[A-Za-z_][A-Za-z_0-9]*)*', a) |
| 62 | return re.sub(r'\s', '', m.group(0)) if m else a |
| 63 | |
| 64 | calls = [] |
| 65 | for f in files: |
| 66 | if not re.match(r'(src|tools|tests|research|om|evidence)/', f) and '/' in f: |
| 67 | continue |
| 68 | try: |
| 69 | raw = open(f'{ROOT}/{f}', encoding='utf-8', errors='replace').read() |
| 70 | except (IsADirectoryError, FileNotFoundError): |
| 71 | continue |
| 72 | s = strip_comments(raw) |
| 73 | for m in CALL.finditer(s): |
| 74 | # skip declarations/definitions: preceded by a type word on the same line |
| 75 | line_start = s.rfind('\n', 0, m.start()) + 1 |
| 76 | prefix = s[line_start:m.start()] |
| 77 | p = m.end() - 1 |
| 78 | args, end = args_at(s, p) |
| 79 | after = s[end + 1:end + 40].lstrip() |
| 80 | is_def = bool(re.match(r'^\s*((static|extern|inline)\s+)*void\s*$', prefix)) |
| 81 | if re.match(r'\s*#\s*define', prefix): is_def = True |
| 82 | line = s.count('\n', 0, m.start()) + 1 |
| 83 | calls.append(dict(file=f, line=line, fn=m.group(1), args=args, defn=is_def, off=m.start())) |
| 84 | |
| 85 | json.dump(calls, open('/tmp/zl/sha-audit/calls.json', 'w'), indent=0) |
| 86 | built = [c for c in calls if not c['defn'] and not c['file'].startswith('evidence/')] |
| 87 | snap = [c for c in calls if not c['defn'] and c['file'].startswith('evidence/')] |
| 88 | print('call sites (built dirs):', len(built), ' files:', len({c['file'] for c in built})) |
| 89 | print('by fn:', dict(collections.Counter(c['fn'] for c in built))) |
| 90 | print('by top dir:', dict(collections.Counter(c['file'].split('/')[0] for c in built))) |
| 91 | print('evidence snapshots (not built):', len(snap), 'in', len({c['file'] for c in snap}), 'files') |
| 92 | print('definitions/declarations skipped:', [(c['file'], c['line']) for c in calls if c['defn']]) |