XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
[CmdletBinding()]
param(
    [string]$ToolRoot,
    [string]$WindowsSdkLib
)
$ErrorActionPreference='Stop'
$taskRoot=[IO.Path]::GetFullPath((Join-Path $PSScriptRoot '../..'))
if(!$ToolRoot){$ToolRoot=Join-Path $taskRoot '.tools'}
if(!$WindowsSdkLib){
    $sdk=Get-ChildItem -LiteralPath 'C:/Program Files (x86)/Windows Kits/10/Lib' -Directory |
        Where-Object {Test-Path -LiteralPath (Join-Path $_.FullName 'um/x64/kernel32.lib')} |
        Sort-Object Name -Descending | Select-Object -First 1
    if(!$sdk){throw 'A Windows SDK kernel32 import library is required for this host-only contract test.'}
    $WindowsSdkLib=Join-Path $sdk.FullName 'um/x64/kernel32.lib'
}
$output=Join-Path $taskRoot ('build/IndependentKernel/ExtrasContract-'+[DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds())
[void][IO.Directory]::CreateDirectory($output)
$llvm=Join-Path $ToolRoot 'llvm/bin'
$extras=Join-Path $taskRoot 'Source/XFESystem.NativeRuntime/Native/SystemNativeExtras.cpp'
$stdio=Join-Path $taskRoot 'Source/XFESystem.NativeRuntime/Native/Stdio.c'
$test=Join-Path $PSScriptRoot 'SystemNativeExtrasContract.cpp'
$common=@('--target=x86_64-pc-windows-msvc','-ffreestanding','-fno-builtin','-fno-stack-protector','-mno-stack-arg-probe','-O2','-c')
$cpp=@('-fno-exceptions','-fno-rtti','-std=c++17','-Wall','-Wextra','-Werror')
& (Join-Path $llvm 'clang++.exe') @common @cpp $extras '-o' (Join-Path $output 'Extras.obj')
if($LASTEXITCODE -ne 0){throw 'Production SystemNativeExtras host compilation failed.'}
& (Join-Path $llvm 'clang.exe') @common $stdio '-o' (Join-Path $output 'Stdio.obj')
if($LASTEXITCODE -ne 0){throw 'Production Stdio host compilation failed.'}
& (Join-Path $llvm 'clang++.exe') @common @cpp $test '-o' (Join-Path $output 'Contract.obj')
if($LASTEXITCODE -ne 0){throw 'Native contract test compilation failed.'}
$executable=Join-Path $output 'Contract.exe'
& (Join-Path $llvm 'lld-link.exe') '/entry:main' '/subsystem:console' '/nodefaultlib' ('/out:'+$executable) (Join-Path $output 'Extras.obj') (Join-Path $output 'Stdio.obj') (Join-Path $output 'Contract.obj') $WindowsSdkLib
if($LASTEXITCODE -ne 0){throw 'Native contract test linking failed.'}
$result=& $executable
$exitCode=$LASTEXITCODE
$result | Set-Content -LiteralPath (Join-Path $output 'Result.log') -Encoding UTF8
$unavailable=& $executable '--rtc-unavailable'
$unavailableExit=$LASTEXITCODE
$overflow=& $executable '--rtc-overflow'
$overflowExit=$LASTEXITCODE
$rtcPassed=$unavailableExit -eq 254 -and $unavailable -ceq 'RTC_UNAVAILABLE' -and $overflowExit -eq 254 -and $overflow -ceq 'RTC_OUT_OF_RANGE'
$counts=[regex]::Match(($result -join "`n"),'checks=(\d+) failures=(\d+)')
if(!$counts.Success){throw 'The native executable did not report actual check counters.'}
$passedCount=[int]$counts.Groups[1].Value-[int]$counts.Groups[2].Value+$(if($rtcPassed){2}else{0})
$failedCount=[int]$counts.Groups[2].Value+$(if($rtcPassed){0}else{2})
$logPath=Join-Path $output 'Result.log'
[ordered]@{Kind='XfeSystemNativeExtrasHostContract';Passed=($exitCode -eq 0 -and $rtcPassed);GuestAcceptancePassed=$false;
    HostOnly=$true;GuestExecution=$false;PassedCount=$passedCount;Failed=$failedCount;
    Log=$logPath;LogSha256=(Get-FileHash -LiteralPath $logPath -Algorithm SHA256).Hash.ToLowerInvariant();
    ExitCode=$exitCode;Result=@($result);RtcFailurePassed=$rtcPassed;RtcUnavailableExit=$unavailableExit;RtcUnavailableResult=@($unavailable);
    RtcOverflowExit=$overflowExit;RtcOverflowResult=@($overflow);ExtrasSha256=(Get-FileHash -LiteralPath $extras -Algorithm SHA256).Hash.ToLowerInvariant();
    StdioSha256=(Get-FileHash -LiteralPath $stdio -Algorithm SHA256).Hash.ToLowerInvariant();
    TestSha256=(Get-FileHash -LiteralPath $test -Algorithm SHA256).Hash.ToLowerInvariant()} |
    ConvertTo-Json -Depth 4 | Set-Content -LiteralPath (Join-Path $output 'Receipt.json') -Encoding UTF8
$result
if($exitCode -ne 0 -or !$rtcPassed){throw 'Native contract checks failed. Receipt: '+$output}
Write-Output ('Contract receipt: '+$output)