264 lines
23 KiB
Python
264 lines
23 KiB
Python
#!/usr/bin/env python3
|
|
"""Exact Microsoft MIDL codegen only; no installer, service, RPC or RMM job.
|
|
|
|
Root owns all target calls. Lost replies use readback of the same intent only.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import importlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import uuid
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
ACCESS = Path('/home/kevin/Schreibtisch/Coding/rust-discovery')
|
|
STATE = ROOT / 'devstate/windows-clock-midl/native-cases'
|
|
TOOLS = ROOT / 'devstate/windows-clock-midl/6b7fb9b1-0bb7-4880-850b-e6812d3df1b1/tools'
|
|
IDL = ROOT / 'crates/agent-windows/native/w32time.idl'
|
|
PINS = {'midl.exe': ('7a2a717be64f755390ad8b04cb034549b2606acec646ec7cbe44c67516ccba83', 154048),
|
|
'midlc.exe': ('34293d7bd256f2d43311d197f13bbb8a1ae26c92cc1605d9f35dd3ddfe264b65', 1366464),
|
|
'w32time.idl': ('37da4acb622daf9acf1aa6f1218ff9f52e5a2def14e58d8d9dd8da65078b3c6c', 2004)}
|
|
|
|
# A fixed Codegen job. JOB_LIST is installed before CreateProcessW; no
|
|
# suspended-process/late-Assign fallback. Child descendants join this same job.
|
|
CS = r'''
|
|
using System;using System.IO;using System.Text;using System.Runtime.InteropServices;
|
|
public static class RmmMidlJob {
|
|
[StructLayout(LayoutKind.Sequential)]struct SA{public uint cb;public IntPtr sd;[MarshalAs(UnmanagedType.Bool)]public bool inherit;}
|
|
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]struct SI{public uint cb;public string reserved,desktop,title;public uint x,y,xs,ys,xc,yc,fill,flags;public ushort show,reserved2;public IntPtr reservedp,input,output,error;}
|
|
[StructLayout(LayoutKind.Sequential)]struct SIX{public SI si;public IntPtr attrs;}
|
|
[StructLayout(LayoutKind.Sequential)]struct PI{public IntPtr process,thread;public uint pid,tid;}
|
|
[StructLayout(LayoutKind.Sequential)]struct BASIC{public long processTime,jobTime;public uint flags;public UIntPtr minimum,maximum;public uint active;public UIntPtr affinity;public uint priority,scheduling;}
|
|
[StructLayout(LayoutKind.Sequential)]struct IO{public ulong a,b,c,d,e,f;}
|
|
[StructLayout(LayoutKind.Sequential)]struct LIMIT{public BASIC basic;public IO io;public UIntPtr processMemory,jobMemory,peakProcess,peakJob;}
|
|
[StructLayout(LayoutKind.Sequential)]struct ACCOUNT{public long a,b,c,d;public uint faults,total,active,terminated;}
|
|
[DllImport("kernel32.dll",SetLastError=true)]static extern IntPtr CreateJobObjectW(IntPtr sa,string name);
|
|
[DllImport("kernel32.dll",SetLastError=true)]static extern bool SetInformationJobObject(IntPtr job,int kind,ref LIMIT value,uint len);
|
|
[DllImport("kernel32.dll",SetLastError=true)]static extern bool QueryInformationJobObject(IntPtr job,int kind,out ACCOUNT value,uint len,IntPtr needed);
|
|
[DllImport("kernel32.dll",SetLastError=true)]static extern bool TerminateJobObject(IntPtr job,uint code);
|
|
[DllImport("kernel32.dll",SetLastError=true)]static extern bool InitializeProcThreadAttributeList(IntPtr list,int count,uint flags,ref UIntPtr size);
|
|
[DllImport("kernel32.dll",SetLastError=true)]static extern bool UpdateProcThreadAttribute(IntPtr list,uint flags,UIntPtr attribute,IntPtr value,UIntPtr len,IntPtr old,IntPtr needed);
|
|
[DllImport("kernel32.dll")]static extern void DeleteProcThreadAttributeList(IntPtr list);
|
|
[DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)]static extern IntPtr CreateFileW(string path,uint access,uint share,ref SA sa,uint disposition,uint flags,IntPtr template);
|
|
[DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)]static extern bool CreateProcessW(string app,StringBuilder command,IntPtr pa,IntPtr ta,bool inherit,uint flags,IntPtr environment,string cwd,ref SIX start,out PI pi);
|
|
[DllImport("kernel32.dll",SetLastError=true)]static extern bool IsProcessInJob(IntPtr process,IntPtr job,out bool member);
|
|
[DllImport("kernel32.dll",SetLastError=true)]static extern uint ResumeThread(IntPtr thread);
|
|
[DllImport("kernel32.dll",SetLastError=true)]static extern uint WaitForSingleObject(IntPtr handle,uint ms);
|
|
[DllImport("kernel32.dll",SetLastError=true)]static extern bool GetExitCodeProcess(IntPtr handle,out uint code);
|
|
[DllImport("kernel32.dll")]static extern bool CloseHandle(IntPtr handle);
|
|
static void Need(bool ok,string stage){if(!ok)throw new InvalidOperationException(stage+"_"+Marshal.GetLastWin32Error());}
|
|
static IntPtr Output(string path,ref SA sa){var h=CreateFileW(path,0x40000000,1,ref sa,1,0,IntPtr.Zero);Need(h!=new IntPtr(-1),"OUTPUT_CREATE");return h;}
|
|
public sealed class Result {public uint pid,exit_code,active_processes;public bool timed_out,output_exceeded,atomic_job_assignment,all_own_children_exited;}
|
|
public static Result Run(string folder){
|
|
Need(IntPtr.Size==8&&Marshal.SizeOf(typeof(SI))==104&&Marshal.SizeOf(typeof(SIX))==112&&Marshal.SizeOf(typeof(LIMIT))==144,"X64_ABI");
|
|
IntPtr job=IntPtr.Zero,attrs=IntPtr.Zero,jobValue=IntPtr.Zero,handleValue=IntPtr.Zero,input=IntPtr.Zero,output=IntPtr.Zero,error=IntPtr.Zero;bool initialized=false;PI pi=new PI();
|
|
try{
|
|
job=CreateJobObjectW(IntPtr.Zero,null);Need(job!=IntPtr.Zero,"JOB_CREATE");
|
|
LIMIT limit=new LIMIT();limit.basic.flags=0x2000|8;limit.basic.active=2;Need(SetInformationJobObject(job,9,ref limit,(uint)Marshal.SizeOf(typeof(LIMIT))),"JOB_LIMITS");
|
|
SA sa=new SA();sa.cb=(uint)Marshal.SizeOf(typeof(SA));sa.inherit=true;
|
|
input=CreateFileW("NUL",0x80000000,3,ref sa,3,0,IntPtr.Zero);Need(input!=new IntPtr(-1),"INPUT_CREATE");
|
|
output=Output(Path.Combine(folder,"stdout.txt"),ref sa);error=Output(Path.Combine(folder,"stderr.txt"),ref sa);
|
|
UIntPtr bytes=UIntPtr.Zero;InitializeProcThreadAttributeList(IntPtr.Zero,2,0,ref bytes);Need(bytes.ToUInt64()>0&&bytes.ToUInt64()<65536,"ATTRIBUTE_SIZE");
|
|
attrs=Marshal.AllocHGlobal((int)bytes.ToUInt64());Need(InitializeProcThreadAttributeList(attrs,2,0,ref bytes),"ATTRIBUTE_INIT");initialized=true;
|
|
jobValue=Marshal.AllocHGlobal(IntPtr.Size);Marshal.WriteIntPtr(jobValue,job);Need(UpdateProcThreadAttribute(attrs,0,new UIntPtr(0x2000d),jobValue,new UIntPtr((uint)IntPtr.Size),IntPtr.Zero,IntPtr.Zero),"ATOMIC_JOB_LIST");
|
|
handleValue=Marshal.AllocHGlobal(3*IntPtr.Size);Marshal.WriteIntPtr(handleValue,0,input);Marshal.WriteIntPtr(handleValue,IntPtr.Size,output);Marshal.WriteIntPtr(handleValue,2*IntPtr.Size,error);
|
|
Need(UpdateProcThreadAttribute(attrs,0,new UIntPtr(0x20002),handleValue,new UIntPtr((uint)(3*IntPtr.Size)),IntPtr.Zero,IntPtr.Zero),"HANDLE_LIST");
|
|
SIX si=new SIX();si.si.cb=(uint)Marshal.SizeOf(typeof(SIX));si.attrs=attrs;si.si.flags=0x100;si.si.input=input;si.si.output=output;si.si.error=error;
|
|
string exe=Path.Combine(folder,"input","midl.exe");
|
|
var cmd=new StringBuilder("\""+exe+"\" /nologo /no_cpp /env amd64 /Oicf /robust /Zp8 /protocol dce /client stub /server none /h w32time.h /cstub w32time_c.c \""+Path.Combine(folder,"input","w32time.idl")+"\"");
|
|
Need(CreateProcessW(exe,cmd,IntPtr.Zero,IntPtr.Zero,true,0x80000|0x8000000|4,IntPtr.Zero,Path.Combine(folder,"output"),ref si,out pi),"CODEGEN_CREATE");
|
|
bool member;Need(IsProcessInJob(pi.process,job,out member)&&member,"ATOMIC_JOB_MEMBERSHIP");Need(ResumeThread(pi.thread)!=0xffffffff,"CODEGEN_RESUME");
|
|
Result result=new Result();result.pid=pi.pid;result.atomic_job_assignment=true;
|
|
var began=System.Diagnostics.Stopwatch.StartNew();
|
|
while(true){
|
|
uint waited=WaitForSingleObject(pi.process,25);Need(waited==0||waited==258,"CODEGEN_WAIT");
|
|
foreach(string path in new[]{Path.Combine(folder,"stdout.txt"),Path.Combine(folder,"stderr.txt"),Path.Combine(folder,"output","w32time.h"),Path.Combine(folder,"output","w32time_c.c")})if(File.Exists(path)&&new FileInfo(path).Length>262144)result.output_exceeded=true;
|
|
if(waited==0)break;
|
|
if(began.ElapsedMilliseconds>=10000||result.output_exceeded){result.timed_out=began.ElapsedMilliseconds>=10000;Need(TerminateJobObject(job,0xdead),"OWN_JOB_TERMINATE");break;}
|
|
}
|
|
// One total two-second observation budget, not one timeout per child.
|
|
var closing=System.Diagnostics.Stopwatch.StartNew();ACCOUNT account;
|
|
do{Need(QueryInformationJobObject(job,1,out account,(uint)Marshal.SizeOf(typeof(ACCOUNT)),IntPtr.Zero),"JOB_ACCOUNTING");if(account.active==0)break;System.Threading.Thread.Sleep(25);}while(closing.ElapsedMilliseconds<2000);
|
|
result.active_processes=account.active;result.all_own_children_exited=account.active==0;
|
|
Need(GetExitCodeProcess(pi.process,out result.exit_code),"CODEGEN_EXIT_CODE");return result;
|
|
}finally{
|
|
// Closing only this unnamed job kills any remaining own compiler child.
|
|
if(job!=IntPtr.Zero)CloseHandle(job);
|
|
if(pi.thread!=IntPtr.Zero)CloseHandle(pi.thread);if(pi.process!=IntPtr.Zero)CloseHandle(pi.process);
|
|
if(initialized)DeleteProcThreadAttributeList(attrs);if(attrs!=IntPtr.Zero)Marshal.FreeHGlobal(attrs);if(jobValue!=IntPtr.Zero)Marshal.FreeHGlobal(jobValue);if(handleValue!=IntPtr.Zero)Marshal.FreeHGlobal(handleValue);
|
|
foreach(var h in new[]{input,output,error})if(h!=IntPtr.Zero&&h!=new IntPtr(-1))CloseHandle(h);
|
|
}
|
|
}
|
|
}
|
|
'''
|
|
|
|
SCRIPT = r'''$ErrorActionPreference='Stop'
|
|
$p=([Console]::In.ReadLine()|ConvertFrom-Json)
|
|
if([guid]::ParseExact($p.id,'D').ToString()-cne$p.id-or$p.action-notin@('prepare','admit','start','readback')){throw 'Fixed codegen case'}
|
|
foreach($n in @('runner_sha256','script_sha256')){if($p.$n-notmatch'^[a-f0-9]{64}$'){throw 'Source binding'}}
|
|
$root='C:\ProgramData\RustDiscoveryBootstrap';$folder=Join-Path $root ('midl-codegen-'+$p.id)
|
|
$inputsDir=Join-Path $folder 'input';$output=Join-Path $folder 'output'
|
|
$pins=@(@('midl.exe','7a2a717be64f755390ad8b04cb034549b2606acec646ec7cbe44c67516ccba83',154048),@('midlc.exe','34293d7bd256f2d43311d197f13bbb8a1ae26c92cc1605d9f35dd3ddfe264b65',1366464),@('w32time.idl','37da4acb622daf9acf1aa6f1218ff9f52e5a2def14e58d8d9dd8da65078b3c6c',2004))
|
|
function Check($path,[bool]$directory){
|
|
$item=Get-Item -LiteralPath $path -Force;if($item.PSIsContainer-ne$directory-or($item.Attributes-band[IO.FileAttributes]::ReparsePoint)){throw 'Codegen regular private path'}
|
|
$parent=$item.Parent;if(!$directory){$parent=$item.Directory};while($null-ne$parent){if($parent.Attributes-band[IO.FileAttributes]::ReparsePoint){throw 'Codegen ancestor reparse'};$parent=$parent.Parent}
|
|
$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')-or!$a.Access.Count){throw 'Codegen private owner'}
|
|
foreach($r in $a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier])){if($r.AccessControlType-ne'Allow'-or$r.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')-or[int64]$r.FileSystemRights-ne2032127){throw 'Codegen exact controller ACL'}}
|
|
}
|
|
function Protect($path){$a=New-Object Security.AccessControl.DirectorySecurity;$a.SetAccessRuleProtection($true,$false);$a.SetOwner((New-Object Security.Principal.SecurityIdentifier('S-1-5-32-544')));foreach($sid in @('S-1-5-18','S-1-5-32-544')){$a.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule((New-Object Security.Principal.SecurityIdentifier($sid)),'FullControl','ContainerInherit,ObjectInherit','None','Allow')))};Set-Acl -LiteralPath $path -AclObject $a;Check $path $true}
|
|
function NewRecord($name,$value){$path=Join-Path $folder $name;$bytes=[Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 16 -Compress));$f=[IO.File]::Open($path,'CreateNew','Write','Read');try{$f.Write($bytes,0,$bytes.Length);$f.Flush($true)}finally{$f.Dispose()};Check $path $false}
|
|
function Hash($path){return(Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash.ToLowerInvariant()}
|
|
function Intent{
|
|
Check $folder $true;Check $inputsDir $true;Check $output $true;Check (Join-Path $folder 'intent.json') $false
|
|
$i=Get-Content -LiteralPath (Join-Path $folder 'intent.json') -Raw|ConvertFrom-Json
|
|
if($i.id-cne$p.id-or$i.runner_sha256-cne$p.runner_sha256-or$i.script_sha256-cne$p.script_sha256){throw 'Immutable codegen intent'}
|
|
}
|
|
function VerifyInputs{
|
|
$info=@();foreach($pin in $pins){$path=Join-Path $inputsDir $pin[0];Check $path $false;if((Get-Item -LiteralPath $path).Length-ne$pin[2]-or(Hash $path)-cne$pin[1]){throw 'Fixed codegen input bytes'}
|
|
if($pin[0]-like'*.exe'){
|
|
$sig=Get-AuthenticodeSignature -LiteralPath $path;if($sig.Status-ne'Valid'-or!$sig.SignerCertificate-or$sig.SignerCertificate.Subject-notmatch'(^|,\s*)O=Microsoft Corporation(,|$)'){throw 'Valid Microsoft Authenticode required'}
|
|
$chain=New-Object Security.Cryptography.X509Certificates.X509Chain;try{$chain.ChainPolicy.RevocationMode='Online';$chain.ChainPolicy.RevocationFlag='ExcludeRoot';$chain.ChainPolicy.VerificationFlags='NoFlag';$chain.ChainPolicy.UrlRetrievalTimeout=[TimeSpan]::FromSeconds(2);if(!$chain.Build($sig.SignerCertificate)){throw 'Microsoft chain validation required'}}finally{$chain.Dispose()}
|
|
$v=(Get-Item -LiteralPath $path).VersionInfo;if($v.FileMajorPart-ne8-or$v.FileMinorPart-ne1-or$v.FileBuildPart-ne628-or$v.FilePrivatePart-ne1){throw 'Exact pinned compiler version'}
|
|
$info+=,[ordered]@{name=$pin[0];sha256=$pin[1];authenticode_status='Valid';signer_thumbprint=$sig.SignerCertificate.Thumbprint;chain_valid=$true;file_version='8.1.628.1'}
|
|
}
|
|
};return ,$info
|
|
}
|
|
Check $root $true
|
|
if($p.action-ceq'prepare'){
|
|
if(Test-Path -LiteralPath $folder){throw 'Existing case: readback only'}
|
|
New-Item -ItemType Directory -Path $folder|Out-Null;Protect $folder
|
|
foreach($path in @($inputsDir,$output)){New-Item -ItemType Directory -Path $path|Out-Null;Protect $path}
|
|
NewRecord 'intent.json' @{id=$p.id;runner_sha256=$p.runner_sha256;script_sha256=$p.script_sha256;inputs=$pins;no_jobs=$true;no_rpc=$true;no_installation=$true}
|
|
@{prepared=$true;id=$p.id;tool_executed=$false}|ConvertTo-Json -Compress;return
|
|
}
|
|
Intent
|
|
if($p.action-ceq'admit'){$tools=VerifyInputs;NewRecord 'admitted.json' @{id=$p.id;tools=$tools;source_sha256=$pins[2][1];tool_executed=$false};@{admitted=$true;id=$p.id;tools=$tools;tool_executed=$false}|ConvertTo-Json -Depth 8 -Compress;return}
|
|
if($p.action-ceq'readback'){
|
|
$report=$null;$reportPath=Join-Path $folder 'result.json';if(Test-Path -LiteralPath $reportPath){Check $reportPath $false;$report=Get-Content -LiteralPath $reportPath -Raw|ConvertFrom-Json}
|
|
$files=@();foreach($pair in @(@('stdout.txt',$folder),@('stderr.txt',$folder),@('w32time.h',$output),@('w32time_c.c',$output))){$path=Join-Path $pair[1] $pair[0];if(Test-Path -LiteralPath $path){Check $path $false;$size=(Get-Item -LiteralPath $path).Length;if($size-gt262144){throw 'Codegen output bound'};$files+=,@{name=$pair[0];bytes=$size;sha256=(Hash $path);base64=[Convert]::ToBase64String([IO.File]::ReadAllBytes($path))}}}
|
|
@{id=$p.id;readback_only=$true;started=(Test-Path -LiteralPath (Join-Path $folder 'started.json'));result=$report;files=$files}|ConvertTo-Json -Depth 16 -Compress;return
|
|
}
|
|
if(Test-Path -LiteralPath (Join-Path $folder 'started.json')){throw 'No compiler restart: readback only'}
|
|
Check (Join-Path $folder 'admitted.json') $false
|
|
$tools=VerifyInputs
|
|
$held=@();try{
|
|
foreach($pin in $pins){$held+=,[IO.File]::Open((Join-Path $inputsDir $pin[0]),'Open','Read','Read')}
|
|
$null=VerifyInputs
|
|
Add-Type -TypeDefinition @'
|
|
__CS__
|
|
'@
|
|
NewRecord 'started.json' @{id=$p.id;single_start_reserved=$true;atomic_job_list_required=$true;no_rpc=$true;no_jobs=$true}
|
|
$run=$null
|
|
try{$run=[RmmMidlJob]::Run($folder);$ok=($run.exit_code-eq0-and!$run.timed_out-and!$run.output_exceeded-and$run.all_own_children_exited)
|
|
foreach($name in @('w32time.h','w32time_c.c')){$path=Join-Path $output $name;Check $path $false;if((Get-Item -LiteralPath $path).Length-gt262144){$ok=$false}}
|
|
$other=@(Get-ChildItem -LiteralPath $output -Force|Where-Object{$_.Name-notin@('w32time.h','w32time_c.c')});if($other.Count){$ok=$false}
|
|
NewRecord 'result.json' @{id=$p.id;complete=$ok;run=$run;tools=$tools;no_jobs=$true;no_rpc=$true;no_installation=$true;clock_qualified=$false}
|
|
}catch{NewRecord 'result.json' @{id=$p.id;complete=$false;run=$run;error_class=$_.Exception.GetType().Name;error_line=$_.InvocationInfo.ScriptLineNumber;no_retry=$true;clock_qualified=$false};throw}
|
|
@{id=$p.id;complete=$ok;tool_executed=$true;clock_qualified=$false}|ConvertTo-Json -Compress
|
|
}finally{foreach($f in $held){$f.Dispose()}}
|
|
'''.replace('__CS__', CS)
|
|
|
|
|
|
STAGE = r'''$ErrorActionPreference='Stop'
|
|
$p=([Console]::In.ReadLine()|ConvertFrom-Json)
|
|
if($p.sha256-notmatch'^[a-f0-9]{64}$'-or$p.bytes-le0-or$p.bytes-gt32768){throw 'Fixed codegen source'}
|
|
$root='C:\ProgramData\RustDiscoveryBootstrap'
|
|
function Check($path,[bool]$directory){$i=Get-Item -LiteralPath $path -Force;if($i.PSIsContainer-ne$directory-or($i.Attributes-band[IO.FileAttributes]::ReparsePoint)){throw 'Regular controller source'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')-or!$a.Access.Count){throw 'Source owner'};foreach($r in $a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier])){if($r.AccessControlType-ne'Allow'-or$r.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')-or[int64]$r.FileSystemRights-ne2032127){throw 'Source controller ACL'}}}
|
|
Check $root $true
|
|
$dir=Join-Path $root $p.sha256;if(!(Test-Path -LiteralPath $dir)){New-Item -ItemType Directory -Path $dir|Out-Null};Check $dir $true
|
|
$path=Join-Path $dir 'MidlCodegen.ps1';$exists=Test-Path -LiteralPath $path
|
|
if($exists){Check $path $false;if((Get-Item -LiteralPath $path).Length-ne$p.bytes-or(Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash.ToLowerInvariant()-cne$p.sha256){throw 'Immutable codegen source'}}
|
|
@{already_uploaded=$exists}|ConvertTo-Json -Compress
|
|
'''
|
|
LAUNCH = r'''$ErrorActionPreference='Stop'
|
|
$p=([Console]::In.ReadLine()|ConvertFrom-Json)
|
|
if($p.script_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed codegen source hash'}
|
|
$file='C:\ProgramData\RustDiscoveryBootstrap\'+$p.script_sha256+'\MidlCodegen.ps1'
|
|
$i=Get-Item -LiteralPath $file -Force;if($i.PSIsContainer-or($i.Attributes-band[IO.FileAttributes]::ReparsePoint)-or(Get-FileHash -LiteralPath $file -Algorithm SHA256).Hash.ToLowerInvariant()-cne$p.script_sha256){throw 'Immutable codegen source file'}
|
|
$json=$p|ConvertTo-Json -Depth 8 -Compress
|
|
$raw=$json|& "$env:WINDIR\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy RemoteSigned -File $file
|
|
if($LASTEXITCODE-ne0){throw 'Exact codegen phase failed; retain case and readback only'}
|
|
$raw
|
|
'''
|
|
|
|
|
|
def upload(cb, local, remote, folder, label):
|
|
prefix=cb.contract.protected_json(cb.ROOT/'credentials/windows-dev-access.json')['ssh_prefix']
|
|
batch='put '+json.dumps(str(local.resolve()))+' '+json.dumps(remote)+'\n'
|
|
result=subprocess.run(['sftp']+[v for v in prefix[1:-1] if v!='-T']+['-b','-',prefix[-1]],input=batch.encode(),capture_output=True,timeout=40)
|
|
save(folder,label+'.transfer.json',{'returncode':result.returncode,'stdout_sha256':hashlib.sha256(result.stdout).hexdigest(),'stderr_sha256':hashlib.sha256(result.stderr).hexdigest()})
|
|
if result.returncode:raise RuntimeError('EXACT_CODEGEN_UPLOAD_FAILED_READBACK_ONLY')
|
|
|
|
|
|
def stage_source(cb, folder, source, digest):
|
|
meta={'sha256':digest,'bytes':source.stat().st_size}
|
|
state=cb.powershell(STAGE,meta,timeout=40,remote_signed=True)
|
|
if not state['already_uploaded']:
|
|
upload(cb,source,'/C:/ProgramData/RustDiscoveryBootstrap/'+digest+'/MidlCodegen.ps1',folder,'source')
|
|
if not cb.powershell(STAGE,meta,timeout=40,remote_signed=True)['already_uploaded']:raise RuntimeError('EXACT_SOURCE_READBACK_FAILED')
|
|
|
|
|
|
def sha(path):
|
|
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
|
|
|
|
|
def save(folder, name, value):
|
|
fd = os.open(folder/name, os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW, 0o600)
|
|
with os.fdopen(fd, 'w') as out:
|
|
json.dump(value, out, indent=2);out.flush();os.fsync(out.fileno())
|
|
|
|
|
|
def transport(access_root):
|
|
if access_root.resolve() != ACCESS:
|
|
raise RuntimeError('EXACT_EXISTING_ACCESS_ROOT_REQUIRED')
|
|
sys.path.insert(0, str(ACCESS/'deploy/windows'))
|
|
return importlib.import_module('client_bootstrap')
|
|
|
|
|
|
def main():
|
|
p=argparse.ArgumentParser(description=__doc__);p.add_argument('action',choices=('prepare','start','readback'));p.add_argument('--id');p.add_argument('--access-root',type=Path,default=ACCESS);a=p.parse_args()
|
|
os.umask(0o077);cb=transport(a.access_root)
|
|
script_sha=hashlib.sha256(SCRIPT.encode()).hexdigest();runner_sha=sha(__file__)
|
|
if a.action=='prepare':
|
|
if a.id:raise RuntimeError('FRESH_CREATE_ONLY_CASE_REQUIRED')
|
|
local={'midl.exe':TOOLS/'midl.exe','midlc.exe':TOOLS/'midlc.exe','w32time.idl':IDL}
|
|
for name,path in local.items():
|
|
if path.is_symlink() or not path.is_file() or (sha(path),path.stat().st_size)!=PINS[name]:raise RuntimeError('EXACT_FIXED_INPUTS_REQUIRED')
|
|
STATE.mkdir(parents=True,exist_ok=True,mode=0o700);folder=STATE/str(uuid.uuid4());folder.mkdir(mode=0o700)
|
|
payload={'id':folder.name,'runner_sha256':runner_sha,'script_sha256':script_sha}
|
|
save(folder,'intent.json',{'payload':payload,'inputs':PINS,'transport_sha256':sha(cb.__file__),'no_rpc':True,'no_jobs':True})
|
|
save(folder,'prepare-attempt.json',{'once':True})
|
|
source=folder/'MidlCodegen.ps1'
|
|
with source.open('xb') as out:out.write(SCRIPT.encode());out.flush();os.fsync(out.fileno())
|
|
stage_source(cb,folder,source,script_sha)
|
|
result=cb.powershell(LAUNCH,dict(payload,action='prepare'),timeout=40,remote_signed=True);save(folder,'remote-prepared.json',result)
|
|
for name,path in local.items():
|
|
upload(cb,path,'/C:/ProgramData/RustDiscoveryBootstrap/midl-codegen-'+folder.name+'/input/'+name,folder,name)
|
|
admitted=cb.powershell(LAUNCH,dict(payload,action='admit'),timeout=60,remote_signed=True);save(folder,'admitted.json',admitted)
|
|
print(json.dumps({'case_id':folder.name,'prepared':True,'tool_executed':False}));return
|
|
if not a.id or str(uuid.UUID(a.id))!=a.id:raise RuntimeError('EXACT_EXISTING_CASE_REQUIRED')
|
|
folder=STATE/a.id;intent=cb.contract.protected_json(folder/'intent.json');payload=intent['payload']
|
|
if payload['id']!=a.id or payload['runner_sha256']!=runner_sha or payload['script_sha256']!=script_sha or intent['transport_sha256']!=sha(cb.__file__):raise RuntimeError('IMMUTABLE_RUNNER_AND_TRANSPORT_REQUIRED')
|
|
if a.action=='start':
|
|
cb.contract.protected_json(folder/'admitted.json');save(folder,'start-attempt.json',{'once':True,'lost_reply_readback_only':True})
|
|
result=cb.powershell(LAUNCH,dict(payload,action=a.action),timeout=60,remote_signed=True)
|
|
save(folder,('start-result.json' if a.action=='start' else 'readback-'+str(uuid.uuid4())+'.json'),result)
|
|
if a.action=='readback':
|
|
for item in result['files']:
|
|
import base64
|
|
if item['name'] not in ('stdout.txt','stderr.txt','w32time.h','w32time_c.c'):raise RuntimeError('FIXED_OUTPUT_NAME_REQUIRED')
|
|
raw=base64.b64decode(item['base64'],validate=True)
|
|
if len(raw)>262144 or len(raw)!=item['bytes'] or hashlib.sha256(raw).hexdigest()!=item['sha256']:raise RuntimeError('EXACT_OUTPUT_BYTES_REQUIRED')
|
|
dest=folder/('native-'+item['name'])
|
|
if dest.exists():
|
|
if sha(dest)!=item['sha256']:raise RuntimeError('RETAINED_OUTPUT_CHANGED')
|
|
else:
|
|
with dest.open('xb') as out:out.write(raw);out.flush();os.fsync(out.fileno())
|
|
report=result.get('result') or result
|
|
print(json.dumps({'case_id':a.id,'complete':report.get('complete',False),'readback_only':a.action=='readback','clock_qualified':False}))
|
|
|
|
if __name__=='__main__':main()
|