76 lines
4.7 KiB
Python
76 lines
4.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Host-only, fixed-image EXE authoring. Never uploads or executes an installer."""
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import uuid
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
IMAGE = 'sha256:1b2fd985d2c1fcc3545ace602405dc68fa899201bd8a73fa319ff96377203fa9'
|
|
NAMES = ('contract.h', 'contract_test.c', 'fixture.c', 'build.py', 'README.md')
|
|
|
|
|
|
def sha(path):
|
|
return hashlib.sha256(path.read_bytes()).hexdigest()
|
|
|
|
|
|
def snapshot():
|
|
paths = [ROOT/'tools/windows-exe-fixture'/n for n in NAMES]
|
|
paths += [ROOT/n for n in ('AGENTS.md', 'Rust_RMM_Implementierungsspezifikation.md', 'docs/project-decisions.md', 'docs/development.md')]
|
|
return {str(p.relative_to(ROOT)): sha(p) for p in paths}
|
|
|
|
|
|
def write(path, value):
|
|
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600)
|
|
with os.fdopen(fd, 'w') as f:
|
|
json.dump(value, f, indent=2, sort_keys=True);f.flush();os.fsync(f.fileno())
|
|
|
|
|
|
def main():
|
|
os.umask(0o077)
|
|
base = ROOT/'devstate/windows-exe-fixture';base.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
case = base/('build-'+str(uuid.uuid4()));case.mkdir(mode=0o700)
|
|
before = snapshot();write(case/'intent.json', {'image': IMAGE, 'source_files': before, 'build_only': True, 'native_installer_execution': False, 'network': False})
|
|
script = '''set -eu
|
|
cd /source
|
|
cc --version > /out/linux-compiler.txt
|
|
x86_64-w64-mingw32-gcc --version > /out/windows-compiler.txt
|
|
cc -std=c11 -O2 -Wall -Wextra -Werror contract_test.c -o /out/linux-precondition-guard
|
|
/out/linux-precondition-guard
|
|
x86_64-w64-mingw32-gcc -std=c11 -O2 -Wall -Wextra -Werror contract_test.c -Wl,--no-insert-timestamp -o /out/windows-precondition-guard.original.exe
|
|
x86_64-w64-mingw32-strip --strip-all /out/windows-precondition-guard.original.exe -o /out/windows-precondition-guard.exe
|
|
for version in 10 11; do
|
|
x86_64-w64-mingw32-gcc -std=c11 -O2 -Wall -Wextra -Werror -municode -DFIXTURE_VERSION=$version fixture.c -Wl,--no-insert-timestamp,--nxcompat,--dynamicbase,--high-entropy-va -ladvapi32 -lbcrypt -o /out/RustDiscoveryExeFixture-$version.original.exe
|
|
x86_64-w64-mingw32-strip --strip-all /out/RustDiscoveryExeFixture-$version.original.exe -o /out/RustDiscoveryExeFixture-$version.exe
|
|
x86_64-w64-mingw32-objdump -p /out/RustDiscoveryExeFixture-$version.exe > /out/imports-$version.log
|
|
done
|
|
readelf -h /out/linux-precondition-guard > /out/linux-elf-header.log
|
|
readelf -d /out/linux-precondition-guard > /out/linux-elf-needed.log
|
|
readelf --version-info /out/linux-precondition-guard > /out/linux-elf-versions.log
|
|
chmod 600 /out/*
|
|
'''
|
|
args = ['docker', 'run', '--rm', '--user', str(os.getuid())+':'+str(os.getgid()), '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,exec,size=64m', '-v', str(ROOT/'tools/windows-exe-fixture')+':/source:ro', '-v', str(case)+':/out', '--entrypoint', 'sh', IMAGE, '-c', script]
|
|
result = subprocess.run(args, capture_output=True, timeout=120)
|
|
write(case/'compiler-output.json', {'exit_code': result.returncode, 'stdout': result.stdout.decode(errors='replace'), 'stderr': result.stderr.decode(errors='replace')})
|
|
after = snapshot();write(case/'source-after.json', after)
|
|
if result.returncode != 0 or before != after:
|
|
raise RuntimeError('FIXTURE_BUILD_FAILED_RETAINED_'+case.name)
|
|
artifacts = {}
|
|
for path in sorted(case.glob('*.exe')):
|
|
b = path.read_bytes();pe = int.from_bytes(b[60:64], 'little')
|
|
if b[:2] != b'MZ' or b[pe:pe+4] != b'PE\0\0' or int.from_bytes(b[pe+4:pe+6], 'little') != 0x8664:
|
|
raise RuntimeError('FIXTURE_PE_AMD64_REQUIRED')
|
|
if not '.original.' in path.name and len(b) > 262144:
|
|
raise RuntimeError('FIXTURE_ARTIFACT_BUDGET')
|
|
artifacts[path.name] = {'sha256': sha(path), 'bytes': len(b), 'architecture': 'AMD64', 'authenticode': 'not_signed_fixture_only'}
|
|
artifacts['linux-precondition-guard'] = {'sha256': sha(case/'linux-precondition-guard'), 'bytes': (case/'linux-precondition-guard').stat().st_size}
|
|
imports = {str(v): [l.strip().split(':', 1)[1].strip() for l in (case/f'imports-{v}.log').read_text().splitlines() if 'DLL Name:' in l] for v in (10, 11)}
|
|
write(case/'qualification.json', {'image': IMAGE, 'source_files': before, 'source_before_after_equal': True, 'linux_pure_precondition_guard': 'passed_container_only', 'windows_pure_guard': 'linked_not_executed', 'windows_installer': 'linked_not_executed', 'cloud_linux_native_guard': 'not_executed', 'windows_native': 'not_executed', 'artifacts': artifacts, 'imports': imports, 'no_installer_run': True, 'no_upload': True})
|
|
print(json.dumps({'case': str(case.relative_to(ROOT)), 'qualification_sha256': sha(case/'qualification.json'), 'artifacts': artifacts}))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|