[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$ApkPath, [string]$OutputPath = '' ) $ErrorActionPreference = 'Stop' $root = Split-Path -Parent $PSScriptRoot $sdkRoot = if ($env:ANDROID_HOME) { $env:ANDROID_HOME } else { 'C:\Users\shenw_o246oq4\AppData\Local\Android\Sdk' } $ndkVersion = '28.2.13676358' $buildToolsVersion = '36.1.0' $zipalign = Join-Path $sdkRoot "build-tools\$buildToolsVersion\zipalign.exe" $readelf = Join-Path $sdkRoot "ndk\$ndkVersion\toolchains\llvm\prebuilt\windows-x86_64\bin\llvm-readelf.exe" $resolvedApk = (Resolve-Path $ApkPath).Path if (-not (Test-Path $zipalign)) { throw "zipalign not found: $zipalign" } if (-not (Test-Path $readelf)) { throw "llvm-readelf not found: $readelf" } $auditRoot = [IO.Path]::GetFullPath((Join-Path $root 'build\native-audit')) $expectedAuditRoot = [IO.Path]::GetFullPath((Join-Path $root 'build')) + [IO.Path]::DirectorySeparatorChar if (-not $auditRoot.StartsWith($expectedAuditRoot, [StringComparison]::OrdinalIgnoreCase)) { throw 'Native audit output must remain inside the repository build directory.' } if (Test-Path $auditRoot) { Remove-Item -LiteralPath $auditRoot -Recurse -Force } $null = New-Item -ItemType Directory -Path $auditRoot Add-Type -AssemblyName System.IO.Compression.FileSystem [System.IO.Compression.ZipFile]::ExtractToDirectory($resolvedApk, $auditRoot) $nativeFiles = Get-ChildItem (Join-Path $auditRoot 'lib') -Filter '*.so' -File -Recurse -ErrorAction SilentlyContinue if (-not $nativeFiles) { throw 'No native libraries were found in the candidate APK.' } $unexpectedAbi = $nativeFiles | Where-Object { $_.FullName -notmatch '[\\/]lib[\\/]arm64-v8a[\\/]' } if ($unexpectedAbi) { throw "Unexpected non-arm64 native libraries: $($unexpectedAbi.FullName -join ', ')" } $zipalignOutput = & $zipalign -c -P 16 -v 4 $resolvedApk 2>&1 if ($LASTEXITCODE -ne 0) { throw "APK 16 KB zip alignment failed:`n$($zipalignOutput -join [Environment]::NewLine)" } $libraries = foreach ($library in $nativeFiles) { $segments = & $readelf -lW $library.FullName 2>&1 if ($LASTEXITCODE -ne 0) { throw "ELF inspection failed for $($library.Name)." } $loadLines = @($segments | Where-Object { $_ -match '^\s*LOAD\s+' }) if (-not $loadLines) { throw "No LOAD segments found in $($library.Name)." } $alignments = foreach ($line in $loadLines) { if ($line -notmatch '(0x[0-9a-fA-F]+)\s*$') { throw "Cannot parse LOAD alignment: $line" } [Convert]::ToInt64($Matches[1].Substring(2), 16) } if (($alignments | Measure-Object -Minimum).Minimum -lt 16384) { throw "$($library.Name) contains a LOAD segment aligned below 16 KB." } $dynamic = & $readelf -dW $library.FullName 2>&1 if ($LASTEXITCODE -ne 0) { throw "Dynamic dependency inspection failed for $($library.Name)." } $needed = @($dynamic | ForEach-Object { if ($_ -match '\(NEEDED\).*\[(.+?)\]') { $Matches[1] } }) [ordered]@{ apkPath = $library.FullName.Substring($auditRoot.Length + 1).Replace('\', '/') bytes = $library.Length sha256 = (Get-FileHash $library.FullName -Algorithm SHA256).Hash minimumLoadAlignmentBytes = ($alignments | Measure-Object -Minimum).Minimum needed = $needed } } $readelfVersion = ((& $readelf --version 2>&1 | Select-Object -First 2) -join ' ').Trim() $result = [ordered]@{ formatVersion = 1 apk = [ordered]@{ path = $resolvedApk bytes = (Get-Item $resolvedApk).Length sha256 = (Get-FileHash $resolvedApk -Algorithm SHA256).Hash zipAlignmentBytes = 16384 zipAlignmentPassed = $true } packaging = [ordered]@{ abi = 'arm64-v8a' fceumm = 'statically linked into libneslite_jni.so' oboe = 'statically linked into libneslite_jni.so' runtimeLibraries = @($libraries | ForEach-Object { $_.apkPath }) } libraries = @($libraries) tools = [ordered]@{ androidBuildTools = $buildToolsVersion ndk = $ndkVersion llvmReadelf = $readelfVersion } } if (-not $OutputPath) { $OutputPath = Join-Path $root 'build\reports\native-audit.json' } $outputDirectory = Split-Path -Parent $OutputPath if ($outputDirectory) { $null = New-Item -ItemType Directory -Path $outputDirectory -Force } $result | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath $OutputPath -Encoding utf8NoBOM Write-Output "RC native static audit passed: $OutputPath" $result | ConvertTo-Json -Depth 8