wswg/scripts/check-builds.py

30 lines
2 KiB
Python

#!/usr/bin/env python3
"""Check architecture and absence of shared-library imports in Linux payloads."""
from pathlib import Path
import os
import shutil
import struct
import subprocess
root=Path(__file__).resolve().parent.parent
for folder in sorted((root/'dist').glob('wswg-*')):
if not folder.is_dir(): continue
platform,arch=folder.name.removeprefix('wswg-').split('-',1)
expected={'amd64':'Advanced Micro Devices X86-64','386':'Intel 80386','arm':'ARM','armv7':'ARM','arm64':'AArch64'}[arch]
if platform=='windows':
for p in [folder/'wswg.exe',folder/'bin/wg.exe',folder/'bin/wireguard.exe',folder/'bin/wstunnel.exe']:
b=p.read_bytes();offset=struct.unpack_from('<I',b,0x3c)[0]
machine=struct.unpack_from('<H',b,offset+4)[0]
if b[:2]!=b'MZ' or b[offset:offset+4]!=b'PE\0\0' or machine!={'amd64':0x8664,'386':0x14c,'arm64':0xaa64}[arch]:raise RuntimeError(f'Wrong Windows architecture: {p}')
assert list((folder/'redist').glob('*.exe')),folder
print(folder.name+': PE executables + redist OK');continue
runner={'amd64':[],'386':[],'arm':['qemu-arm'],'armv7':['qemu-arm'],'arm64':['qemu-aarch64']}[arch]
for name in ['wswg','bin/wg','bin/wstunnel']:
p=folder/name
hdr=subprocess.check_output(['readelf','-h',str(p)],text=True)
if expected not in hdr:raise RuntimeError(f'Wrong architecture: {p}')
for args,marker in [(['readelf','-l',str(p)],'INTERP'),(['readelf','-d',str(p)],'(NEEDED)')]:
if marker in subprocess.check_output(args,text=True):raise RuntimeError(f'Shared library dependency: {p}')
if not runner or shutil.which(runner[0]):
proc=subprocess.run(runner+[str(p),'--version'],capture_output=True,text=True,timeout=30,env=dict(os.environ,NO_COLOR='true'))
if proc.returncode!=0:raise RuntimeError(f'Cannot run {p}: {proc.stderr}')
print(folder.name+': ELF architecture, static linkage, available runtime smoke tests OK')