2026-11-19 21:03:27 -08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""verify-image-arch.py - prove an image platform really contains binaries of that architecture.
|
|
|
|
|
|
|
|
|
|
An image whose config says linux/arm64 can still carry x86-64 binaries (the "x86 in arm64"
|
|
|
|
|
exec-format-error loop seen with haproxy-manager-base). The registry manifest cannot tell; the ELF
|
|
|
|
|
header can. This pulls REF for one platform, copies the named key binaries out of a created (never
|
|
|
|
|
started, never executed) container and checks e_machine of each: aarch64 = 0xB7, x86-64 = 0x3E.
|
|
|
|
|
Same idea as haproxy-manager-base scripts/verify-image-arch.py (PR #22), adapted to docker cp so
|
|
|
|
|
no QEMU execution is needed.
|
|
|
|
|
|
|
|
|
|
verify-image-arch.py --image REF --platform linux/arm64|linux/amd64 --bin /usr/bin/php [--bin ...]
|
|
|
|
|
|
|
|
|
|
Exit 0 = every --bin is an ELF64 little-endian binary of the platform's machine type.
|
|
|
|
|
Exit 1 = a binary is missing, not an ELF, or of the wrong architecture (or docker failed).
|
2026-11-19 21:10:08 -08:00
|
|
|
Exit 2 = usage. Exit 4 = could not pull the image (3 attempts): no architecture verdict.
|
2026-11-19 21:03:27 -08:00
|
|
|
Env: DOCKER overrides the docker command (tests use a stub). Nothing in REF is ever run.
|
|
|
|
|
"""
|
|
|
|
|
import argparse
|
|
|
|
|
import os
|
|
|
|
|
import shlex
|
|
|
|
|
import subprocess
|
|
|
|
|
import sys
|
|
|
|
|
import tarfile
|
2026-11-19 21:10:08 -08:00
|
|
|
import threading
|
|
|
|
|
import time
|
2026-11-19 21:03:27 -08:00
|
|
|
|
|
|
|
|
MACHINES = {'linux/arm64': (0xB7, 'AArch64'), 'linux/amd64': (0x3E, 'x86-64')}
|
|
|
|
|
NAMES = {0x3E: 'x86-64', 0xB7: 'AArch64', 0x03: 'x86', 0x28: 'ARM', 0xF3: 'RISC-V', 0x15: 'PowerPC64', 0x16: 'S390'}
|
|
|
|
|
MAX_STREAM = 1 << 31 # refuse to read more than 2 GiB of one tar stream
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def elf_machine(head):
|
|
|
|
|
"""Return e_machine of an ELF64 little-endian header, or raise ValueError."""
|
|
|
|
|
if len(head) < 20 or head[:4] != b'\x7fELF':
|
|
|
|
|
raise ValueError('not an ELF file')
|
|
|
|
|
if head[4] != 2:
|
|
|
|
|
raise ValueError('not ELF64 (EI_CLASS=%d)' % head[4])
|
|
|
|
|
if head[5] != 1:
|
|
|
|
|
raise ValueError('not little-endian (EI_DATA=%d)' % head[5])
|
|
|
|
|
return int.from_bytes(head[18:20], 'little')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def docker_cmd():
|
|
|
|
|
return shlex.split(os.environ.get('DOCKER') or 'docker')
|
|
|
|
|
|
|
|
|
|
|
2026-11-19 21:10:08 -08:00
|
|
|
def run(args, timeout=300):
|
|
|
|
|
try:
|
|
|
|
|
return subprocess.run(docker_cmd() + args, capture_output=True, timeout=timeout)
|
|
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
|
return subprocess.CompletedProcess(args, 124, b'', b'timed out after %ds' % timeout)
|
2026-11-19 21:03:27 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def first_bytes_of(cid, path):
|
|
|
|
|
"""First 64 bytes of the regular file at path (symlinks followed) inside container cid."""
|
|
|
|
|
p = subprocess.Popen(docker_cmd() + ['cp', '-L', '%s:%s' % (cid, path), '-'],
|
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2026-11-19 21:10:08 -08:00
|
|
|
timer = threading.Timer(120, p.kill) # a stalled docker cp must not hang the job
|
|
|
|
|
timer.start()
|
2026-11-19 21:03:27 -08:00
|
|
|
try:
|
|
|
|
|
tf = tarfile.open(fileobj=p.stdout, mode='r|')
|
|
|
|
|
m = tf.next()
|
|
|
|
|
if m is None or not m.isreg():
|
|
|
|
|
raise ValueError('%s is not a regular file' % path)
|
|
|
|
|
return tf.extractfile(m).read(64)
|
|
|
|
|
except tarfile.TarError as e:
|
|
|
|
|
err = p.stderr.read(300).decode('utf-8', 'replace').strip()
|
|
|
|
|
raise ValueError('cannot read %s: %s %s' % (path, e, err))
|
|
|
|
|
finally:
|
2026-11-19 21:10:08 -08:00
|
|
|
timer.cancel()
|
2026-11-19 21:03:27 -08:00
|
|
|
p.kill()
|
|
|
|
|
p.wait()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
|
ap = argparse.ArgumentParser(description=__doc__.split('\n')[0])
|
|
|
|
|
ap.add_argument('--image', required=True)
|
|
|
|
|
ap.add_argument('--platform', required=True, choices=sorted(MACHINES))
|
|
|
|
|
ap.add_argument('--bin', dest='bins', action='append', required=True)
|
|
|
|
|
ap.add_argument('--no-pull', action='store_true', help='image is already in the local store')
|
|
|
|
|
a = ap.parse_args(argv)
|
|
|
|
|
want, label = MACHINES[a.platform]
|
|
|
|
|
|
2026-11-19 21:10:08 -08:00
|
|
|
pulled = False
|
2026-11-19 21:03:27 -08:00
|
|
|
if not a.no_pull:
|
2026-11-19 21:10:08 -08:00
|
|
|
r = None
|
|
|
|
|
for attempt in range(3):
|
|
|
|
|
r = run(['pull', '--quiet', '--platform', a.platform, a.image], timeout=900)
|
|
|
|
|
if r.returncode == 0:
|
|
|
|
|
break
|
|
|
|
|
time.sleep(float(os.environ.get('VERIFY_PULL_SLEEP', '15')))
|
2026-11-19 21:03:27 -08:00
|
|
|
if r.returncode != 0:
|
2026-11-19 21:10:08 -08:00
|
|
|
print('verify-image-arch: could not pull %s after 3 attempts (not an architecture finding): %s' % (
|
|
|
|
|
a.image, r.stderr.decode('utf-8', 'replace')[:300].strip()), file=sys.stderr)
|
|
|
|
|
return 4
|
|
|
|
|
pulled = True
|
2026-11-19 21:03:27 -08:00
|
|
|
r = run(['create', '--platform', a.platform, '--entrypoint', '/nonexistent', a.image])
|
|
|
|
|
cid = r.stdout.decode().strip()
|
|
|
|
|
if r.returncode != 0 or not cid:
|
|
|
|
|
print('verify-image-arch: docker create failed: %s' % r.stderr.decode('utf-8', 'replace')[:300].strip(), file=sys.stderr)
|
|
|
|
|
return 1
|
|
|
|
|
rc = 0
|
|
|
|
|
try:
|
|
|
|
|
for path in a.bins:
|
|
|
|
|
try:
|
|
|
|
|
m = elf_machine(first_bytes_of(cid, path))
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
print('FAIL %s %s %s: %s' % (a.image, a.platform, path, e))
|
|
|
|
|
rc = 1
|
|
|
|
|
continue
|
|
|
|
|
if m != want:
|
|
|
|
|
print('FAIL %s %s %s: e_machine=0x%X (%s), want 0x%X (%s)' % (
|
|
|
|
|
a.image, a.platform, path, m, NAMES.get(m, '?'), want, label))
|
|
|
|
|
rc = 1
|
|
|
|
|
else:
|
|
|
|
|
print('OK %s %s %s: e_machine=0x%X (%s)' % (a.image, a.platform, path, m, label))
|
|
|
|
|
finally:
|
2026-11-19 21:10:08 -08:00
|
|
|
run(['rm', '-f', cid], timeout=120)
|
|
|
|
|
if pulled:
|
|
|
|
|
run(['image', 'rm', a.image], timeout=120) # keep the runner disk from filling
|
2026-11-19 21:03:27 -08:00
|
|
|
return rc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
sys.exit(main(sys.argv[1:]))
|