diff --git a/.github/scripts/rows/fetch-browser.ps1 b/.github/scripts/rows/fetch-browser.ps1 index 926c4f9..ca6767d 100644 --- a/.github/scripts/rows/fetch-browser.ps1 +++ b/.github/scripts/rows/fetch-browser.ps1 @@ -60,6 +60,14 @@ function Resolve-Edge { $url = switch ($Browser) { 'Google Chrome' { Resolve-Chrome } 'Microsoft Edge' { Resolve-Edge } + 'Brave' { + # Brave's feed lists only new builds, but every release keeps its installer on GitHub under + # the tag of the version without its Chromium milestone, so an earlier build is found too. + if ($Download) { $Download } else { + $tag = 'v' + (($Version -split '\.', 2)[1]) + "https://github.com/brave/brave-browser/releases/download/$tag/BraveBrowserStandaloneSilentSetup.exe" + } + } default { if (-not $Download) { throw "The feed names no installer for $Browser $Version yet." } $Download @@ -78,11 +86,46 @@ if ($Browser -eq 'Brave') { # Brave publishes only update-client installers. Their payload is split into several branch # streams that 7-Zip reads as one, so the files come out damaged. A runner is thrown away after # the job and has no Brave of its own, so the silent installer installs this exact build. - $installed = Join-Path $env:ProgramFiles 'BraveSoftware\Brave-Browser\Application' - if (Test-Path $installed) { throw "This runner already has Brave at $installed, so the build would not be the one asked for." } + # The installer picks a machine or a user install by itself, and its update client may still be + # copying files after the setup process has exited, so every place is checked until the build's + # own version folder appears. + $places = @( + (Join-Path $env:ProgramFiles 'BraveSoftware\Brave-Browser\Application'), + (Join-Path ${env:ProgramFiles(x86)} 'BraveSoftware\Brave-Browser\Application'), + (Join-Path $env:LOCALAPPDATA 'BraveSoftware\Brave-Browser\Application') + ) + foreach ($place in $places) { + if (Test-Path $place) { throw "This runner already has Brave at $place, so the build would not be the one asked for." } + } $setup = Start-Process $package -Wait -PassThru if ($setup.ExitCode -ne 0) { throw "Brave's installer exited with $($setup.ExitCode)" } - $unpacked = $installed + $installed = $null + $until = (Get-Date).AddMinutes(10) + while (-not $installed -and (Get-Date) -lt $until) { + $installed = $places | Where-Object { + (Test-Path (Join-Path $_ 'brave.exe')) -and (Test-Path (Join-Path $_ "$Version\chrome.dll")) + } | Select-Object -First 1 + if (-not $installed) { Start-Sleep -Seconds 5 } + } + if (-not $installed) { + $seen = foreach ($root in $env:ProgramFiles, ${env:ProgramFiles(x86)}, $env:LOCALAPPDATA) { + Get-ChildItem (Join-Path $root 'BraveSoftware') -Recurse -Depth 3 -ErrorAction SilentlyContinue | + ForEach-Object FullName + } + throw "Brave's installer exited with 0, but no place holds brave.exe and $Version\chrome.dll after 10 minutes. Under BraveSoftware: $(@($seen) -join '; ')" + } + # The files are copied out and Brave is removed again, so a second build, such as the one + # before it, can be installed on the same runner. + $unpacked = Join-Path $Out 'files' + New-Item -ItemType Directory -Force $unpacked | Out-Null + Copy-Item $installed $unpacked -Recurse -Force + $remover = Join-Path $installed "$Version\Installer\setup.exe" + $level = if ($installed -like "$env:LOCALAPPDATA*") { @() } else { @('--system-level') } + if (Test-Path $remover) { + Start-Process $remover -ArgumentList (@('--uninstall', '--force-uninstall') + $level) -Wait | Out-Null + } + Remove-Item (Split-Path $installed -Parent) -Recurse -Force -ErrorAction SilentlyContinue + if (Test-Path $installed) { throw "Brave $Version could not be removed from $installed after its files were copied" } } else { 7z x $package "-o$unpacked" -y | Out-Null if ($LASTEXITCODE -ne 0) { throw "7-Zip could not unpack $url" } diff --git a/.github/scripts/rows/fetch-symbols.ps1 b/.github/scripts/rows/fetch-symbols.ps1 deleted file mode 100644 index fdb4d14..0000000 --- a/.github/scripts/rows/fetch-symbols.ps1 +++ /dev/null @@ -1,29 +0,0 @@ -param( - [Parameter(Mandatory = $true)][string]$Browser, - [Parameter(Mandatory = $true)][string]$Key, - [Parameter(Mandatory = $true)][string]$Out, - # Prints the address without fetching it. - [switch]$ResolveOnly -) - -$ErrorActionPreference = 'Stop' -# Each maker publishes its symbols in its own place. Brave keeps them gzipped. These are only for -# making rows on the build machine: a Rivet on a person's computer never downloads a PDB. -$url, $packed = switch ($Browser) { - 'Google Chrome' { "https://chromium-browser-symsrv.commondatastorage.googleapis.com/chrome.dll.pdb/$Key/chrome.dll.pdb", $false } - 'Microsoft Edge' { "https://msdl.microsoft.com/download/symbols/msedge.dll.pdb/$Key/msedge.dll.pdb", $false } - 'Brave' { "https://brave-symbols.s3.brave.com/chrome.dll.pdb/$Key/chrome.dll.pdb", $true } - default { throw "No public symbols are known for $Browser" } -} -if ($ResolveOnly) { $url; return } -$target = if ($packed) { "$Out.gz" } else { $Out } -curl.exe -fsSL --retry 3 -o $target $url -if ($LASTEXITCODE -ne 0) { throw "The symbol server has no PDB at $url yet" } -if ($packed) { - $in = [IO.File]::OpenRead($target) - $gzip = [IO.Compression.GZipStream]::new($in, [IO.Compression.CompressionMode]::Decompress) - $to = [IO.File]::Create($Out) - $gzip.CopyTo($to) - $to.Dispose(); $gzip.Dispose(); $in.Dispose() - Remove-Item $target -} diff --git a/.github/workflows/browser-rows.yml b/.github/workflows/browser-rows.yml index 505e7a7..5b0cbf5 100644 --- a/.github/workflows/browser-rows.yml +++ b/.github/workflows/browser-rows.yml @@ -1,9 +1,9 @@ # Keeps Rivet's supported-build table ahead of Chrome, Edge and Brave. # # A browser updates itself whenever its maker says, and Rivet does not hold it back. So this job -# watches what the makers publish, builds a whole row for each new build from public symbols, -# proves the row on the real browser, and publishes a signed table that reaches people without -# reinstalling Rivet. A row the generator cannot vouch for, such as the first of a new milestone, +# watches what the makers publish, builds a whole row for each new build from the verified row of +# the build before it, proves the row on the real browser, and publishes a signed table that reaches +# people without reinstalling Rivet. A row the generator cannot vouch for, such as the first of a new milestone, # becomes a pull request for a person and is not published. # # Needs on the default branch: this file. Needs in the repository's secrets: @@ -17,6 +17,17 @@ # A failure is never silent: a failed build opens or updates the issue named after it, with the step # that failed, its reason and the capture of the failing window. Each step records its reason in # `failure.txt` before it stops. +# +# No browser's debug symbols are downloaded or read. A maker can ship a build long before its +# symbols appear, or never publish them. A row is made from the verified prior row, the fingerprints +# of its functions, the prior build's module and the new build's module: each function must match +# one place, or two independent methods must agree on it, and every offset, slot and size is read +# again from the new module. A feature whose values are not found is off in the row, and a part the +# window cannot do without stops the row for a person. +# +# A run started by hand with `score` makes, the same way, the rows of builds that already have a +# verified row, and compares them value by value. It is a measurement: it publishes, proposes and +# reports nothing, and fails only when a value it found differs from the verified one. name: Browser rows on: @@ -29,6 +40,19 @@ on: description: "Rows to take out of the published table, as browser@version separated by semicolons. Leave empty for a normal run." required: false default: "" + trial: + description: "Make rows with the code of the branch this run is started on, and keep them only in the run's files: nothing is proposed, published or reported." + type: boolean + required: false + default: false + only: + description: "With trial: the builds to make, as browser@version separated by semicolons, instead of those the feeds list." + required: false + default: "" + score: + description: "Score the generator on builds that have a verified row, as browser@prior>new separated by semicolons, or 'default' for scripts/tests/row-score.ps1's list. Nothing is published." + required: false + default: "" permissions: contents: write @@ -44,7 +68,7 @@ concurrency: env: RELEASE_TAG: builds-table - # Symbols are several gigabytes, and a run does no more than this many builds. + # Each build is a download of hundreds of megabytes, and a run does no more than this many builds. MOST_PER_RUN: "3" jobs: @@ -97,7 +121,7 @@ jobs: watch: name: Find builds with no row needs: gate - if: needs.gate.outputs.enabled == 'true' && github.event.inputs.withdraw == '' + if: needs.gate.outputs.enabled == 'true' && github.event.inputs.withdraw == '' && github.event.inputs.score == '' runs-on: windows-latest defaults: run: @@ -107,7 +131,8 @@ jobs: steps: - uses: actions/checkout@v4 with: - ref: develop + # A trial makes rows with the code it is started on; every other run uses develop. + ref: ${{ inputs.trial && github.ref || 'develop' }} - name: Build the row tools working-directory: rivet @@ -126,6 +151,8 @@ jobs: env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} RIVET_TABLE_PUBLIC_KEY: ${{ vars.RIVET_TABLE_PUBLIC_KEY }} + TRIAL: ${{ inputs.trial }} + ONLY: ${{ inputs.only }} run: | $feeds = Join-Path $env:RUNNER_TEMP "feeds" New-Item -ItemType Directory -Force $feeds | Out-Null @@ -148,6 +175,20 @@ jobs: & "$tools/chrome-builds.exe" pending "$feeds/chrome-stable.json" "$feeds/chrome-beta.json" "$feeds/edge.json" "$feeds/brave.json" "$feeds/known.json" | Set-Content "$feeds/pending.json" if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } Get-Content "$feeds/pending.json" + $only = @($env:ONLY -split ';' | ForEach-Object { $_.Trim() } | Where-Object { $_ }) + if ($only.Count) { + if ($env:TRIAL -ne 'true') { throw "Builds are named only for a trial run." } + # A named build need not be in the feeds any more: Edge's update service still serves it. + $listed = @(Get-Content -Raw "$feeds/pending.json" | ConvertFrom-Json) + $builds = @(foreach ($name in $only) { + $browser, $version = $name -split '@', 2 + $known = $listed | Where-Object { $_.browser -eq $browser -and $_.version -eq $version } | Select-Object -First 1 + if ($known) { $known } else { [pscustomobject]@{ browser = $browser; version = $version; channel = 'stable'; download = '' } } + }) + $json = $builds | ConvertTo-Json -Compress -AsArray + "builds=$json" >> $env:GITHUB_OUTPUT + exit 0 + } $builds = foreach ($build in @(Get-Content -Raw "$feeds/pending.json" | ConvertFrom-Json)) { # A proposal waits for its person, and a run must not spend its turn on it again. $branch = "feat/row-$($build.browser -replace ' ','-')-$($build.version)".ToLower() @@ -162,14 +203,14 @@ jobs: } } $global:LASTEXITCODE = 0 - $builds = @($builds | Where-Object { $_ -isnot [string] } | Select-Object -First ([int]$env:MOST_PER_RUN)) + $builds = @(@($builds) | Where-Object { $_ -and $_ -isnot [string] } | Select-Object -First ([int]$env:MOST_PER_RUN)) # Through the pipeline: -InputObject would wrap the whole list as one element of another list. $json = if ($builds.Count) { $builds | ConvertTo-Json -Compress -AsArray } else { "[]" } "builds=$json" >> $env:GITHUB_OUTPUT exit 0 - name: Tell a person that the watch failed - if: failure() + if: failure() && !inputs.trial env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | @@ -191,14 +232,14 @@ jobs: steps: - uses: actions/checkout@v4 with: - ref: develop + ref: ${{ inputs.trial && github.ref || 'develop' }} - uses: actions/download-artifact@v4 with: name: row-tools path: tools - - name: Get the build and its symbols + - name: Get the build id: build run: | trap { Add-Content (Join-Path $env:RUNNER_TEMP failure.txt) "Get the build: $_"; break } @@ -207,36 +248,51 @@ jobs: -Channel "${{ matrix.build.channel }}" -Download "${{ matrix.build.download }}" ` -Out (Join-Path $env:RUNNER_TEMP "browser") -Output $env:GITHUB_OUTPUT - - name: Download the symbols of this build and of the row before it + - name: Get the build of the row before it + id: prior run: | - trap { Add-Content (Join-Path $env:RUNNER_TEMP failure.txt) "Download the symbols: $_"; break } + trap { Add-Content (Join-Path $env:RUNNER_TEMP failure.txt) "Get the prior build: $_"; break } $browser = "${{ matrix.build.browser }}" $version = "${{ matrix.build.version }}" - $prior = Join-Path $env:RUNNER_TEMP "prior.json" - $tables = @("--table", "rivet/runtime/windows/rows/table.json") - ./tools/rivet-rows.exe prior $browser $version @tables | Set-Content $prior + $temp = $env:RUNNER_TEMP + $prior = Join-Path $temp "prior.json" + ./tools/rivet-rows.exe prior $browser $version --table rivet/runtime/windows/rows/table.json | Set-Content $prior if ($LASTEXITCODE -ne 0) { throw "rivet-rows found no prior row for $browser $version" } $priorVersion = (Get-Content -Raw $prior | ConvertFrom-Json).frame.version - $symbols = (Get-Content -Raw rivet/runtime/windows/rows/table.json | ConvertFrom-Json).symbols - $priorKey = $symbols."$browser $priorVersion" - if (-not $priorKey) { throw "The table holds no symbol key for the prior row $browser $priorVersion. Add it to rows/table.json with chrome-builds pdb-key." } - $newKey = ./tools/chrome-builds.exe pdb-key "${{ steps.build.outputs.module }}" - if ($LASTEXITCODE -ne 0) { throw "chrome-builds could not read the symbol key of the new module" } - ./.github/scripts/rows/fetch-symbols.ps1 -Browser $browser -Key $priorKey -Out (Join-Path $env:RUNNER_TEMP "prior.pdb") - ./.github/scripts/rows/fetch-symbols.ps1 -Browser $browser -Key ($newKey -split '/')[1] -Out (Join-Path $env:RUNNER_TEMP "new.pdb") + # The prior build's own module gives what its row was made from: its function starts, + # its class names and the instructions the methods compare with the new build. + $found = Join-Path $temp "prior-build.txt" + $fetch = @{ Browser = $browser; Version = $priorVersion; Out = (Join-Path $temp "prior-browser"); Output = $found } + try { + ./.github/scripts/rows/fetch-browser.ps1 @fetch -Channel "${{ matrix.build.channel }}" + } catch { + if ("${{ matrix.build.channel }}" -eq 'stable') { throw "The prior build $browser $priorVersion could not be fetched: $_" } + ./.github/scripts/rows/fetch-browser.ps1 @fetch -Channel stable + } + $priorModule = (Get-Content $found | Where-Object { $_ -like 'module=*' } | Select-Object -First 1) -replace '^module=', '' + "module=$priorModule" >> $env:GITHUB_OUTPUT - name: Make the row id: made + env: + PRIOR_MODULE: ${{ steps.prior.outputs.module }} run: | trap { Add-Content (Join-Path $env:RUNNER_TEMP failure.txt) "Make the row: $_"; break } $temp = $env:RUNNER_TEMP - ./tools/chrome-builds.exe port "$temp/prior.json" "$temp/prior.pdb" "$temp/new.pdb" "${{ steps.build.outputs.module }}" "${{ matrix.build.version }}" "$temp/row.json" "$temp/report.json" + # Every function is looked for by its fingerprint and the methods that read the prior + # build next to the new one. Offsets, slots and sizes are read from the new module. + ./tools/chrome-builds.exe find "$temp/prior.json" $env:PRIOR_MODULE "${{ steps.build.outputs.module }}" "${{ matrix.build.version }}" "$temp/row.json" "$temp/report.json" $code = $LASTEXITCODE - if ($code -ne 0 -and $code -ne 3) { throw "chrome-builds port exited with $code; report.json says why" } - # Fingerprints let a Rivet find these functions in a later build that has no row yet. A row - # whose addresses cannot all be fingerprinted is still a good row, just without them. - ./tools/chrome-builds.exe fingerprint "$temp/row.json" "${{ steps.build.outputs.module }}" "$temp/row.json" - if ($LASTEXITCODE -ne 0) { "The row has no fingerprints." } + if ($code -ne 0 -and $code -ne 3) { throw "chrome-builds find exited with $code; report.json says why" } + # Fingerprints, read from this build's own module at the places the row holds, let the + # next build's row start from this one. A row that needs review may still hold a prior + # address, which would fingerprint the wrong code, so it gets none. + if ($code -eq 0) { + ./tools/chrome-builds.exe fingerprint "$temp/row.json" "${{ steps.build.outputs.module }}" "$temp/row.json" + if ($LASTEXITCODE -ne 0) { "The row has no fingerprints." } + } else { + "A row that needs review gets no fingerprints." + } "for_review=$($code -eq 3)".ToLower() >> $env:GITHUB_OUTPUT Get-Content "$temp/report.json" exit 0 @@ -254,8 +310,8 @@ jobs: Set-Content (Join-Path $temp "test-key.txt") $env:RIVET_TABLE_PUBLIC_KEY $env:LOCALAPPDATA = Join-Path $temp "local" New-Item -ItemType Directory -Force $env:LOCALAPPDATA | Out-Null - Set-Content "$temp/part.json" '{"schema":1,"sequence":0,"symbols":{},"rows":[]}' - ./tools/chrome-builds.exe table-add "$temp/part.json" "$temp/row.json" "${{ steps.build.outputs.module }}" + Set-Content "$temp/part.json" '{"schema":1,"sequence":0,"rows":[]}' + ./tools/chrome-builds.exe table-add "$temp/part.json" "$temp/row.json" ./tools/chrome-builds.exe table-sign "$temp/part.json" "$temp/test.bin" Push-Location rivet cargo build -p rivet -p rivet-runtime-windows @@ -287,7 +343,9 @@ jobs: # The harness builds Rivet itself, with the test flags that keep sync and sign-in off. ./scripts/tests/windows-features.ps1 -Browsers $name ` -Executable "${{ steps.build.outputs.executable }}" -OutDir $features - ./tools/chrome-builds.exe gate (Join-Path $features "results.json") $name + # A check of a feature the row turns off for this build is not required; the row ships with + # that feature off, and the gate prints a note for it. + ./tools/chrome-builds.exe gate (Join-Path $features "results.json") $name "$temp/row.json" if ($LASTEXITCODE -ne 0) { throw "a feature check failed or could not run; results.json lists which" } Set-Content (Join-Path $temp "session/features-passed") "${{ matrix.build.version }}" @@ -310,7 +368,7 @@ jobs: retention-days: 30 - name: Tell a person that this build failed - if: failure() + if: failure() && !inputs.trial env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | @@ -321,7 +379,7 @@ jobs: -Files "${{ steps.files.outputs.artifact-url }}" - name: Ask a person about a row the generator cannot vouch for - if: steps.made.outputs.for_review == 'true' + if: steps.made.outputs.for_review == 'true' && !inputs.trial env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | @@ -332,12 +390,17 @@ jobs: git config user.name "rivet-browser-rows" git config user.email "rivet-browser-rows@users.noreply.github.com" git switch -c $branch - ./tools/chrome-builds.exe table-add rivet/runtime/windows/rows/table.json "$temp/row.json" "${{ steps.build.outputs.module }}" + ./tools/chrome-builds.exe table-add rivet/runtime/windows/rows/table.json "$temp/row.json" git add rivet/runtime/windows/rows/table.json git commit -m "Feat: Propose a row for ${{ matrix.build.browser }} ${{ matrix.build.version }}" git push -u origin $branch $report = Get-Content -Raw "$temp/report.json" | ConvertFrom-Json - $body = "The generator could not vouch for this row, so it was not published.`n`n" + (($report.review | ForEach-Object { "- $_" }) -join "`n") + $body = "The generator could not vouch for this row, so it was not published. It was made from the $($report.prior_version) row: $($report.fingerprinted.Count) of $($report.addresses) addresses matched exactly one place by fingerprint, and $($report.settled.Count) were settled by two methods.`n`n" + $body += (($report.review | ForEach-Object { "- $_" }) -join "`n") + if ($report.switched_off.Count -gt 0) { + $body += "`n`nThese features are off in this row, because a value they need could not be read from this build:`n`n" + $body += (($report.switched_off | ForEach-Object { "- $_" }) -join "`n") + } gh pr create --draft --base develop --head $branch --title "Feat: Propose a row for ${{ matrix.build.browser }} ${{ matrix.build.version }}" --body $body gh workflow run test.yml --ref $branch @@ -345,7 +408,7 @@ jobs: name: Publish the signed table needs: row # A row that failed its proof holds up nothing else: the others have passed theirs. - if: ${{ !cancelled() && needs.row.result != 'skipped' }} + if: ${{ !cancelled() && needs.row.result != 'skipped' && !inputs.trial }} runs-on: windows-latest defaults: run: @@ -415,3 +478,37 @@ jobs: git push -u origin $branch gh pr create --base develop --head $branch --title "Feat: Record rows published by the table job" --body "These rows are already published in the signed table and passed the row proof in run $env:GITHUB_RUN_ID. Merging adds them to the table compiled into the next Rivet." gh workflow run test.yml --ref $branch + + score: + name: Score the generator against verified rows + # A measurement: it needs no signing key, and it publishes, proposes and reports nothing. + if: github.event.inputs.score != '' + runs-on: windows-latest + permissions: + contents: read + defaults: + run: + shell: pwsh + steps: + - uses: actions/checkout@v4 + + - name: Build the row tools + working-directory: rivet + run: cargo build --release -p rivet-platform-windows -p rivet-runtime-windows --bins + + - name: Make each row without symbols and compare it with its verified row + env: + SCORE: ${{ github.event.inputs.score }} + run: | + $pairs = if ($env:SCORE -eq 'default') { @() } else { @($env:SCORE -split ';' | ForEach-Object { $_.Trim() } | Where-Object { $_ }) } + ./scripts/tests/row-score.ps1 ` + -Rows rivet/target/release/rivet-rows.exe -Generator rivet/target/release/chrome-builds.exe ` + -Table rivet/runtime/windows/rows/table.json -Pairs $pairs -Fetch ` + -Work (Join-Path $env:RUNNER_TEMP "score") + + - uses: actions/upload-artifact@v4 + if: always() + with: + name: row-score + path: ${{ runner.temp }}/score/*.json + retention-days: 30 diff --git a/rivet/platform/windows/src/bin/chrome-builds.rs b/rivet/platform/windows/src/bin/chrome-builds.rs index c278196..d70f005 100644 --- a/rivet/platform/windows/src/bin/chrome-builds.rs +++ b/rivet/platform/windows/src/bin/chrome-builds.rs @@ -1,18 +1,17 @@ -//! Adds a Chromium build to Rivet's supported-build table from the build's public symbols. +//! Makes and publishes rows of Rivet's supported-build table. No browser's debug symbols are read: +//! a new row comes from the verified prior row, its fingerprints, the prior module and the new one. //! -//! `chrome-builds pdb-key ` prints the symbol-server path of the module's PDB. -//! `chrome-builds extract [frame-class]` -//! resolves the layout hook's addresses with DbgHelp, checks them against the module file and -//! updates the table. The frame class is `BrowserFrameViewWin` unless the browser builds its -//! windows from a subclass of it. -//! `chrome-builds port ` -//! makes a whole row for a new build from the row of a prior build, and exits 3 when the row needs -//! a person's review. +//! `chrome-builds find ` +//! makes a candidate row for a new build and exits 3 when the row needs a person's review. +//! `chrome-builds score ` +//! makes the row of a build that already has a verified row the same way, compares the two value +//! by value and writes the score of each feature. It exits 5 when a value it found is wrong. +//! `chrome-builds fingerprint ` adds the fingerprints of a row's +//! functions, read from its own module, so the next build's row can start from them. //! `chrome-builds pending ` //! prints the builds that have no row yet, most urgent first. //! `chrome-builds table-key` prints a new signing key pair. -//! `chrome-builds table-add ` puts a row into a table, with the -//! symbol key the next build's row will need. +//! `chrome-builds table-add ` puts a row into a table. //! `chrome-builds table-read ` writes out a table that verifies under //! `RIVET_TABLE_PUBLIC_KEY`. //! `chrome-builds table-merge ...` adds the rows of other tables to a table. @@ -23,35 +22,17 @@ use std::path::Path; use std::process::ExitCode; -use rivet_platform_windows::chrome_build::{self, ChromeBuild, PeFile}; -use rivet_platform_windows::row_port::{self, NewBuild}; +use rivet_platform_windows::chrome_build::{self, PeFile}; +use rivet_platform_windows::row_port::{self, PortReport}; use rivet_platform_windows::{row_watch, signed_table}; -/// How many bytes of `views::HWNDForView` the runtime compares before patching. -const PROLOGUE: usize = 15; - fn main() -> ExitCode { let arguments: Vec = std::env::args().skip(1).collect(); let result = match arguments.iter().map(String::as_str).collect::>().as_slice() { - ["pdb-key", module] => pdb_key(Path::new(module)), - ["names", pdb, needles @ ..] => names(Path::new(pdb), needles), - ["near", pdb, addresses @ ..] => near(Path::new(pdb), addresses), - ["extract", module, pdbs, browser, version, table] => extract( - Path::new(module), - Path::new(pdbs), - browser, - version, - Path::new(table), - "BrowserFrameViewWin", - ), - ["extract", module, pdbs, browser, version, table, class] => { - extract(Path::new(module), Path::new(pdbs), browser, version, Path::new(table), class) - } - ["port", prior, old_pdb, new_pdb, module, version, row, report] => { - return match port( + ["find", prior, prior_module, module, version, row, report] => { + return match find( Path::new(prior), - Path::new(old_pdb), - Path::new(new_pdb), + Path::new(prior_module), Path::new(module), version, Path::new(row), @@ -65,10 +46,27 @@ fn main() -> ExitCode { } } } + ["score", prior, prior_module, module, verified, out] => { + return match score( + Path::new(prior), + Path::new(prior_module), + Path::new(module), + Path::new(verified), + Path::new(out), + ) { + Ok(true) => ExitCode::SUCCESS, + Ok(false) => ExitCode::from(5), + Err(error) => { + eprintln!("chrome-builds: {error}"); + ExitCode::FAILURE + } + } + } ["pending", stable, beta, edge, brave, known] => { pending(Path::new(stable), Path::new(beta), Path::new(edge), Path::new(brave), Path::new(known)) } - ["gate", results, browser] => gate(Path::new(results), browser), + ["gate", results, browser] => gate(Path::new(results), browser, None), + ["gate", results, browser, row] => gate(Path::new(results), browser, Some(Path::new(row))), ["fingerprint", row, module, out] => fingerprint(Path::new(row), Path::new(module), Path::new(out)), ["release-sign", version, signed, files @ ..] => release_sign(version, Path::new(signed), files), ["release-check", signed, file] => release_check(Path::new(signed), Path::new(file)), @@ -76,7 +74,7 @@ fn main() -> ExitCode { println!("private {private}"); println!("public {public}"); }), - ["table-add", table, row, module] => table_add(Path::new(table), Path::new(row), Path::new(module)), + ["table-add", table, row] => table_add(Path::new(table), Path::new(row)), ["table-read", signed, out] => table_read(Path::new(signed), Path::new(out)), ["table-merge", table, parts @ ..] => table_merge(Path::new(table), parts), ["table-sign", table, signed] => table_sign(Path::new(table), Path::new(signed), None), @@ -86,7 +84,7 @@ fn main() -> ExitCode { ["table-withdraw", published, signed, builds @ ..] if !builds.is_empty() => { table_withdraw(Path::new(published), Path::new(signed), builds) } - _ => Err("usage: chrome-builds pdb-key | extract
[frame-class] | port | pending | gate | fingerprint | release-sign ... | release-check | table-key | table-add | table-read | table-merge ... | table-sign [published-file] | table-withdraw ...".into()), + _ => Err("usage: chrome-builds find | score | pending | gate [row.json] | fingerprint | release-sign ... | release-check | table-key | table-add | table-read | table-merge ... | table-sign [published-file] | table-withdraw ...".into()), }; match result { Ok(()) => ExitCode::SUCCESS, @@ -97,118 +95,141 @@ fn main() -> ExitCode { } } -/// Prints the address and decorated name of every public symbol whose name holds one of the needles. -fn names(pdb: &Path, needles: &[&str]) -> Result<(), String> { - symbols::publics(pdb, &mut |address, name| { - if needles.iter().any(|needle| name.contains(needle)) { - println!("{address:08X} {name}"); - } - }) +fn read(path: &Path) -> Result, String> { + std::fs::read(path).map_err(|error| format!("could not read {}: {error}", path.display())) } -/// Prints, for each address in hex, the public symbol that starts at or just before it. -fn near(pdb: &Path, addresses: &[&str]) -> Result<(), String> { - let wanted: Vec = addresses - .iter() - .filter_map(|text| u32::from_str_radix(text.trim_start_matches("0x"), 16).ok()) - .collect(); - let mut best: Vec> = vec![None; wanted.len()]; - symbols::publics(pdb, &mut |address, name| { - for (slot, target) in wanted.iter().enumerate() { - if address <= *target && best[slot].as_ref().is_none_or(|(at, _)| address > *at) { - best[slot] = Some((address, name.to_owned())); +/// Makes the row of `version` from the prior row, the prior row's own module and the new module. +fn make( + prior: &serde_json::Value, + prior_module: &Path, + module: &Path, + version: &str, +) -> Result<(serde_json::Value, PortReport), String> { + let prior_bytes = read(prior_module)?; + let prior_pe = + PeFile::parse(&prior_bytes).ok_or("the prior module is not a 64-bit PE image")?; + // A module of another build would give fingerprints and class names of the wrong functions. + let start = prior["frame"]["hwnd_for_view"] + .as_u64() + .and_then(|address| u32::try_from(address).ok()); + let prologue = prior["frame"]["hwnd_for_view_prologue"] + .as_str() + .and_then(chrome_build::hex); + match (start, prologue) { + (Some(start), Some(prologue)) + if prior_pe.read(start, prologue.len()) == Some(prologue.as_slice()) => {} + _ => return Err("the prior module is not the build of the prior row".into()), + } + // The fingerprints the prior row stored were read from this module when the row was made. + // A row stored without them gets them from the module now. + let prints = match prior["fingerprints"].as_object() { + Some(stored) => stored.clone(), + None => { + let (prints, missing) = row_port::fingerprints_of(prior, &prior_pe); + if !missing.is_empty() { + println!( + "the prior build gives no fingerprint for {}", + missing.join(", ") + ); } + prints } - })?; - for (target, found) in wanted.iter().zip(best) { - match found { - Some((at, name)) => println!("{target:08X} is {:#x} past {at:08X} {name}", target - at), - None => println!("{target:08X} has no symbol before it"), - } - } - Ok(()) -} - -fn read(path: &Path) -> Result, String> { - std::fs::read(path).map_err(|error| format!("could not read {}: {error}", path.display())) + }; + // Each class table of the prior row is found again by its views class's name too. + let classes = row_port::prior_classes(prior, &prior_pe); + let bytes = read(module)?; + let pe = PeFile::parse(&bytes).ok_or("the new module is not a 64-bit PE image")?; + let (mut row, mut report) = + row_port::find_candidate(prior, &prints, Some(&prior_pe), &classes, version, &pe); + row_port::leave_out_screen_origin(&mut row, &mut report); + report.review.extend(row_port::hand_only_review(prior)); + println!( + "exception tables: {} function starts in the prior build, {} in the new one; {} class tables named", + prior_pe.function_starts().len(), + pe.function_starts().len(), + classes.len() + ); + Ok((row, report)) } -fn port( - prior: &Path, - old_pdb: &Path, - new_pdb: &Path, +/// Makes a candidate row for a new build. True when nothing needs a person's review. +fn find( + prior_path: &Path, + prior_module: &Path, module: &Path, version: &str, row_path: &Path, report_path: &Path, ) -> Result { - let prior: serde_json::Value = serde_json::from_slice(&read(prior)?) - .map_err(|error| format!("the prior row is not JSON: {error}"))?; - let bytes = read(module)?; - let pe = PeFile::parse(&bytes).ok_or("not a 64-bit PE image")?; - let mut old = Vec::new(); - symbols::publics(old_pdb, &mut |address, name| { - old.push((address, name.to_owned())) - })?; - old.sort(); - // The frame entry keeps plain numbers. - let mut targets = row_port::addresses_of(&prior["build"]); - for key in ["frame_view_vtable", "client_view_bounds", "hwnd_for_view"] { - targets.extend(prior["frame"][key].as_u64().map(|value| value as u32)); - } - let named = row_port::name_addresses(&targets, &old); - let wanted = row_port::wanted_names(&named); - drop(old); - let mut new = std::collections::HashMap::>::new(); - // `views::View::ConvertPointToScreen` is found by its own name, for a prior row without it. - let mut screen_origin = Vec::new(); - symbols::publics(new_pdb, &mut |address, name| { - if row_port::is_screen_origin_name(name) { - screen_origin.push(address); - } - let name = row_port::stable_name(name); - if wanted.contains(name) { - new.entry(name.to_owned()).or_default().push(address); - } - })?; - let unnamed = row_port::unnamed_code(&prior, &named, &pe); - let names = symbols::private_names(old_pdb, &unnamed)?; - let places = symbols::private_places(new_pdb, &names.values().cloned().collect::>())?; - let private = names - .iter() - .filter_map( - |(address, name)| match places.get(name).map(Vec::as_slice) { - Some([place]) => Some((*address, *place)), - _ => None, - }, - ) - .collect(); - let (mut row, mut report) = row_port::port_row( - &prior, - &named, - &NewBuild { - version, - pe: &pe, - symbols: &new, - private: &private, - }, - ); - row_port::seed_screen_origin(&mut row, &screen_origin, &pe, &mut report); - report.review.extend(row_port::hand_only_review(&prior)); + let prior = json_file(prior_path)?; + let (row, report) = make(&prior, prior_module, module, version)?; let text = |value: &dyn erased::Json| value.pretty(); std::fs::write(row_path, text(&row)).map_err(|error| error.to_string())?; std::fs::write(report_path, text(&report)).map_err(|error| error.to_string())?; println!( - "{} {}: {} of {} addresses, {} for review", + "{} {} from {}: {} of {} addresses found by fingerprint, {} settled by two methods, {} values read, {} carried, {} features off, {} for review", report.browser, report.version, - report.mapped, + report.prior_version, + report.fingerprinted.len(), report.addresses, + report.settled.len(), + report.derived.len(), + report.carried.len(), + report.switched_off.len(), report.review.len() ); Ok(report.review.is_empty()) } +/// Makes the row of a build that already has a verified row, and scores it against that row. +/// True when no value it found is wrong. Nothing is written but the score. +fn score( + prior_path: &Path, + prior_module: &Path, + module: &Path, + verified_path: &Path, + out: &Path, +) -> Result { + let prior = json_file(prior_path)?; + let verified = json_file(verified_path)?; + let version = verified["frame"]["version"] + .as_str() + .ok_or("the verified row has no frame version")? + .to_owned(); + let (row, report) = make(&prior, prior_module, module, &version)?; + let score = row_port::score_row(&row, &report, &verified); + std::fs::write( + out, + serde_json::to_string_pretty(&score).map_err(|error| error.to_string())? + "\n", + ) + .map_err(|error| error.to_string())?; + println!( + "{} {} from {}:", + score.browser, score.version, score.prior_version + ); + for (feature, each) in &score.features { + let state = if each.off { "off" } else { "on" }; + println!( + " {feature}: {state}, {} equal, {} different, {} not held", + each.equal, + each.different.len(), + each.not_held.len() + ); + for line in &each.different { + println!(" wrong: {line}"); + } + } + if !score.review.is_empty() { + println!( + " the row would stop for review: {}", + score.review.join("; ") + ); + } + Ok(score.wrong() == 0) +} + mod erased { pub trait Json { fn pretty(&self) -> String; @@ -249,18 +270,9 @@ fn pending( Ok(()) } -fn table_add(table_path: &Path, row: &Path, module: &Path) -> Result<(), String> { +fn table_add(table_path: &Path, row: &Path) -> Result<(), String> { let row = json_file(row)?; let mut table = json_file(table_path)?; - let bytes = read(module)?; - let pe = PeFile::parse(&bytes).ok_or("not a 64-bit PE image")?; - let key = pe.pdb_key().ok_or("the image has no CodeView record")?; - let name = format!( - "{} {}", - row["frame"]["browser"].as_str().unwrap_or_default(), - row["frame"]["version"].as_str().unwrap_or_default() - ); - table["symbols"][name] = key.into(); signed_table::upsert_row(&mut table, row)?; let text = serde_json::to_string_pretty(&table).map_err(|error| error.to_string())? + "\n"; std::fs::write(table_path, text).map_err(|error| error.to_string()) @@ -345,381 +357,19 @@ fn table_withdraw(published: &Path, signed: &Path, builds: &[&str]) -> Result<() Ok(()) } -fn pdb_key(module: &Path) -> Result<(), String> { - let bytes = read(module)?; - let pe = PeFile::parse(&bytes).ok_or("not a 64-bit PE image")?; - let key = pe.pdb_key().ok_or("the image has no CodeView record")?; - let pdb = format!( - "{}.pdb", - module - .file_name() - .and_then(|name| name.to_str()) - .ok_or("bad module name")? - ); - println!("{pdb}/{key}/{pdb}"); - Ok(()) -} - -fn extract( - module: &Path, - pdbs: &Path, - browser: &str, - version: &str, - table_path: &Path, - frame_class: &str, -) -> Result<(), String> { - let bytes = read(module)?; - let pe = PeFile::parse(&bytes).ok_or("not a 64-bit PE image")?; - let symbols = symbols::Session::open(module, pdbs)?; - // The function is declared on `BrowserFrameViewWin` whichever subclass builds the windows. - let client_view_bounds = symbols.address("BrowserFrameViewWin::GetBoundsForClientView")?; - let hwnd_for_view = symbols.address("views::HWNDForView")?; - // The class has several vtables because of multiple inheritance. Exactly one of them holds - // the client-view bounds function. - let (frame_view_vtable, client_view_slot) = symbols - .vtables(frame_class)? - .into_iter() - .find_map(|vtable| Some((vtable, pe.vtable_slot(vtable, client_view_bounds)?))) - .ok_or("no vtable of the frame class holds GetBoundsForClientView")?; - let prologue = pe - .read(hwnd_for_view, PROLOGUE) - .ok_or("views::HWNDForView is outside the image")?; - let build = ChromeBuild { - browser: browser.into(), - version: version.into(), - module: module - .file_name() - .and_then(|name| name.to_str()) - .ok_or("bad module name")? - .into(), - frame_view_vtable, - client_view_slot, - client_view_bounds, - hwnd_for_view, - hwnd_for_view_prologue: prologue.iter().map(|byte| format!("{byte:02x}")).collect(), - }; - let text = std::fs::read_to_string(table_path).unwrap_or_else(|_| "[]".into()); - let mut table = chrome_build::builds(&text); - chrome_build::upsert(&mut table, build.clone()); - let text = serde_json::to_string_pretty(&table).map_err(|error| error.to_string())? + "\n"; - std::fs::write(table_path, text) - .map_err(|error| format!("could not write {}: {error}", table_path.display()))?; - println!( - "{}", - serde_json::to_string(&build).map_err(|error| error.to_string())? - ); - Ok(()) -} - -#[cfg(windows)] -mod symbols { - use std::collections::{BTreeMap, HashMap}; - use std::os::windows::ffi::OsStrExt; - use std::path::Path; - - use windows_sys::Win32::System::Diagnostics::Debug::{ - SymCleanup, SymEnumSymbolsW, SymFromAddrW, SymFromNameW, SymInitializeW, SymLoadModuleExW, - SymSetOptions, SYMBOL_INFOW, SYMOPT_UNDNAME, - }; - use windows_sys::Win32::System::Threading::GetCurrentProcess; - - /// The base the module is loaded at for symbol lookups. Results are reported as RVAs. - const BASE: u64 = 0x1_8000_0000; - /// Accept a PDB whose age differs from the image, which Google's symbol server serves. - const SYMOPT_LOAD_ANYTHING: u32 = 0x40; - - fn wide(path: &Path) -> Vec { - path.as_os_str().encode_wide().chain(Some(0)).collect() - } - - /// Where a PDB opened on its own is placed: wide enough for every browser's module. - const IMAGE_SIZE: u32 = 0x2000_0000; - /// Reads only the linker's public names, which are decorated and stable between builds. - const SYMOPT_PUBLICS_ONLY: u32 = 0x4000; - - /// Visits every public symbol of a PDB, with its address in the module and its decorated name. - pub fn publics(pdb: &Path, visit: &mut dyn FnMut(u32, &str)) -> Result<(), String> { - unsafe extern "system" fn each( - info: *const SYMBOL_INFOW, - _size: u32, - context: *const core::ffi::c_void, - ) -> i32 { - let info = &*info; - let visit = &mut *(context as *mut &mut dyn FnMut(u32, &str)); - let name = std::slice::from_raw_parts(info.Name.as_ptr(), info.NameLen as usize); - if let Ok(address) = u32::try_from(info.Address - BASE) { - visit(address, &String::from_utf16_lossy(name)); - } - 1 - } - unsafe { - SymSetOptions(SYMOPT_LOAD_ANYTHING | SYMOPT_PUBLICS_ONLY); - if SymInitializeW(GetCurrentProcess(), std::ptr::null(), 0) == 0 { - return Err("DbgHelp could not start".into()); - } - let base = SymLoadModuleExW( - GetCurrentProcess(), - std::ptr::null_mut(), - wide(pdb).as_ptr(), - std::ptr::null(), - BASE, - IMAGE_SIZE, - std::ptr::null(), - 0, - ); - if base == 0 { - SymCleanup(GetCurrentProcess()); - return Err(format!("DbgHelp could not load {}", pdb.display())); - } - let mask: Vec = "*".encode_utf16().chain(Some(0)).collect(); - let mut visit = visit; - let done = SymEnumSymbolsW( - GetCurrentProcess(), - BASE, - mask.as_ptr(), - Some(each), - (&raw mut visit).cast(), - ); - SymCleanup(GetCurrentProcess()); - if done == 0 { - return Err(format!("DbgHelp could not list {}", pdb.display())); - } - } - Ok(()) - } - - fn load_all(pdb: &Path) -> Result<(), String> { - unsafe { - SymSetOptions(SYMOPT_LOAD_ANYTHING | SYMOPT_UNDNAME); - if SymInitializeW(GetCurrentProcess(), std::ptr::null(), 0) == 0 { - return Err("DbgHelp could not start".into()); - } - let base = SymLoadModuleExW( - GetCurrentProcess(), - std::ptr::null_mut(), - wide(pdb).as_ptr(), - std::ptr::null(), - BASE, - IMAGE_SIZE, - std::ptr::null(), - 0, - ); - if base == 0 { - SymCleanup(GetCurrentProcess()); - return Err(format!("DbgHelp could not load {}", pdb.display())); - } - } - Ok(()) - } - - /// The function that starts at each address, from the PDB's private symbols. - pub fn private_names(pdb: &Path, addresses: &[u32]) -> Result, String> { - load_all(pdb)?; - let mut names = BTreeMap::new(); - for address in addresses { - unsafe { - // SYMBOL_INFOW ends in the name, whose room the caller provides. - let mut storage = vec![0u64; (std::mem::size_of::() + 2048) / 8]; - let info = storage.as_mut_ptr().cast::(); - (*info).SizeOfStruct = std::mem::size_of::() as u32; - (*info).MaxNameLen = 1000; - let mut distance = 0u64; - if SymFromAddrW( - GetCurrentProcess(), - BASE + u64::from(*address), - &mut distance, - info, - ) != 0 - && distance == 0 - { - let name = - std::slice::from_raw_parts((*info).Name.as_ptr(), (*info).NameLen as usize); - names.insert( - *address, - String::from_utf16_lossy(name) - .trim_end_matches(['\0', ' ']) - .to_owned(), - ); - } - } - } - unsafe { SymCleanup(GetCurrentProcess()) }; - Ok(names) - } - - /// Where each function name lies, from the PDB's private symbols. - pub fn private_places( - pdb: &Path, - names: &[String], - ) -> Result>, String> { - unsafe extern "system" fn each( - info: *const SYMBOL_INFOW, - _size: u32, - context: *const core::ffi::c_void, - ) -> i32 { - let info = &*info; - let found = &mut *(context as *mut Vec<(String, u32)>); - let name = std::slice::from_raw_parts(info.Name.as_ptr(), info.NameLen as usize); - if let Ok(address) = u32::try_from(info.Address - BASE) { - found.push((String::from_utf16_lossy(name), address)); - } - 1 - } - load_all(pdb)?; - let mut places: HashMap> = HashMap::new(); - for name in names { - let mask: Vec = name.encode_utf16().chain(Some(0)).collect(); - let mut found: Vec<(String, u32)> = Vec::new(); - unsafe { - SymEnumSymbolsW( - GetCurrentProcess(), - BASE, - mask.as_ptr(), - Some(each), - (&raw mut found).cast(), - ); - } - let mut exact: Vec = found - .into_iter() - .filter(|(other, _)| other.trim_end_matches(['\0', ' ']) == name) - .map(|(_, place)| place) - .collect(); - exact.sort_unstable(); - exact.dedup(); - places.insert(name.clone(), exact); - } - unsafe { SymCleanup(GetCurrentProcess()) }; - Ok(places) - } - - pub struct Session; - - impl Session { - pub fn open(module: &Path, pdbs: &Path) -> Result { - unsafe { - SymSetOptions(SYMOPT_UNDNAME | SYMOPT_LOAD_ANYTHING); - if SymInitializeW(GetCurrentProcess(), wide(pdbs).as_ptr(), 0) == 0 { - return Err("DbgHelp could not start".into()); - } - let base = SymLoadModuleExW( - GetCurrentProcess(), - std::ptr::null_mut(), - wide(module).as_ptr(), - std::ptr::null(), - BASE, - 0, - std::ptr::null(), - 0, - ); - if base == 0 { - return Err("DbgHelp could not load the module's symbols".into()); - } - } - Ok(Self) - } - - pub fn address(&self, name: &str) -> Result { - let name_wide: Vec = name.encode_utf16().chain(Some(0)).collect(); - unsafe { - let mut info: SYMBOL_INFOW = std::mem::zeroed(); - info.SizeOfStruct = std::mem::size_of::() as u32; - if SymFromNameW(GetCurrentProcess(), name_wide.as_ptr(), &mut info) == 0 { - return Err(format!("the symbols have no {name}")); - } - u32::try_from(info.Address - BASE).map_err(|_| format!("{name} is out of range")) - } - } - - pub fn vtables(&self, class: &str) -> Result, String> { - unsafe extern "system" fn visit( - info: *const SYMBOL_INFOW, - _size: u32, - context: *const core::ffi::c_void, - ) -> i32 { - let info = &*info; - let found = &mut *(context as *mut Vec<(String, u64)>); - let name = std::slice::from_raw_parts(info.Name.as_ptr(), info.NameLen as usize); - found.push((String::from_utf16_lossy(name), info.Address)); - 1 - } - let mask: Vec = format!("{class}::*") - .encode_utf16() - .chain(Some(0)) - .collect(); - let mut found: Vec<(String, u64)> = Vec::new(); - unsafe { - SymEnumSymbolsW( - GetCurrentProcess(), - BASE, - mask.as_ptr(), - Some(visit), - &mut found as *mut _ as *const _, - ); - } - let vtables: Vec = found - .into_iter() - .filter(|(name, _)| { - name.trim_end_matches(['\0', ' ']) == format!("{class}::`vftable'") - }) - .filter_map(|(_, address)| u32::try_from(address - BASE).ok()) - .collect(); - if vtables.is_empty() { - return Err(format!("the symbols have no {class} vtable")); - } - Ok(vtables) - } - } - - impl Drop for Session { - fn drop(&mut self) { - unsafe { SymCleanup(GetCurrentProcess()) }; - } - } -} - -#[cfg(not(windows))] -mod symbols { - use std::path::Path; - - pub fn publics(_pdb: &Path, _visit: &mut dyn FnMut(u32, &str)) -> Result<(), String> { - Err("symbol extraction needs Windows DbgHelp".into()) - } - pub fn private_names( - _pdb: &Path, - _addresses: &[u32], - ) -> Result, String> { - Err("symbol extraction needs Windows DbgHelp".into()) - } - pub fn private_places( - _pdb: &Path, - _names: &[String], - ) -> Result>, String> { - Err("symbol extraction needs Windows DbgHelp".into()) - } - - pub struct Session; - - impl Session { - pub fn open(_module: &Path, _pdbs: &Path) -> Result { - Err("symbol extraction needs Windows DbgHelp".into()) - } - pub fn address(&self, _name: &str) -> Result { - unreachable!() - } - pub fn vtables(&self, _class: &str) -> Result, String> { - unreachable!() - } - } -} - /// Refuses a browser that did not pass every feature check, so it is never listed as supported. -fn gate(results: &Path, browser: &str) -> Result<(), String> { +/// The row is the one being published: a check of a feature it turns off is not required. +fn gate(results: &Path, browser: &str, row: Option<&Path>) -> Result<(), String> { + let row = match row { + Some(path) => json_file(path)?, + None => serde_json::Value::Null, + }; let text = String::from_utf8(read(results)?) .map_err(|error| format!("the feature results are not text: {error}"))?; // PowerShell writes UTF-8 with a byte order mark. let results: serde_json::Value = serde_json::from_str(text.trim_start_matches('\u{feff}')) .map_err(|error| format!("the feature results are not JSON: {error}"))?; - let notes = row_watch::publish_gate(&results, browser) + let notes = row_watch::publish_gate_for_row(&results, browser, &row) .map_err(|reasons| format!("{browser} is not supported: {}", reasons.join("; ")))?; for note in notes { println!("note: {note}"); diff --git a/rivet/platform/windows/src/chrome_build.rs b/rivet/platform/windows/src/chrome_build.rs index 04db929..158eb82 100644 --- a/rivet/platform/windows/src/chrome_build.rs +++ b/rivet/platform/windows/src/chrome_build.rs @@ -2,9 +2,8 @@ //! new ones. //! //! Each entry records where one build keeps the frame view's client-view bounds function. The -//! runtime only patches a loaded module that matches an entry exactly. The `chrome-builds` tool -//! fills entries from Google's public symbols, so a new browser release needs a data change, not -//! a code change. +//! runtime only patches a loaded module that matches an entry exactly, so a new browser release +//! needs a data change, not a code change. use serde::{Deserialize, Serialize}; @@ -61,13 +60,15 @@ pub fn hex(value: &str) -> Option> { .collect() } -/// A PE image read from a file, with enough structure to map RVAs and find its debug record. +/// A PE image read from a file, with enough structure to map RVAs and find its functions. pub struct PeFile<'a> { bytes: &'a [u8], image_base: u64, sections: Vec<(u32, u32, u32)>, code_sections: Vec<(u32, u32)>, - debug_directory: (u32, u32), + exception_directory: (u32, u32), + /// What `row_class` reads from the whole code once, kept for its later questions. + pub(crate) code_index: std::cell::OnceCell, } fn u16_at(bytes: &[u8], offset: usize) -> Option { @@ -102,10 +103,10 @@ impl<'a> PeFile<'a> { return None; } let image_base = u64_at(bytes, optional + 24)?; - // Data directory 6 is the debug directory. - let debug_directory = ( - u32_at(bytes, optional + 112 + 48)?, - u32_at(bytes, optional + 116 + 48)?, + // Data directory 3 is the exception table, which lists where functions start. + let exception_directory = ( + u32_at(bytes, optional + 112 + 24)?, + u32_at(bytes, optional + 116 + 24)?, ); let table = optional + optional_size; // IMAGE_SCN_MEM_EXECUTE. @@ -136,7 +137,8 @@ impl<'a> PeFile<'a> { image_base, sections, code_sections, - debug_directory, + exception_directory, + code_index: std::cell::OnceCell::new(), }) } @@ -184,30 +186,43 @@ impl<'a> PeFile<'a> { u32::try_from(value.checked_sub(self.image_base)?).ok() } - /// The symbol-server key of the image's PDB: GUID and age from its CodeView record. - pub fn pdb_key(&self) -> Option { - let (directory, size) = self.debug_directory; - for index in 0..size / 28 { - let entry = self.offset(directory + index * 28)?; - // Type 2 is CodeView. - if u32_at(self.bytes, entry + 12)? != 2 { - continue; - } - let record = u32_at(self.bytes, entry + 24)? as usize; - if self.bytes.get(record..record + 4)? != b"RSDS" { - return None; + /// The start of every function the exception table lists. A function that needs no unwind + /// data, such as a leaf that touches no stack, is not listed. + pub fn function_starts(&self) -> std::collections::BTreeSet { + let (directory, size) = self.exception_directory; + let mut starts = std::collections::BTreeSet::new(); + let Some(table) = self.offset(directory).filter(|_| directory != 0) else { + return starts; + }; + // Each entry is the function's start, its end and its unwind data. + for index in 0..(size / 12) as usize { + match u32_at(self.bytes, table + index * 12) { + Some(start) => starts.insert(start), + None => break, + }; + } + starts + } + + /// Every part of a function the exception table lists, as start and end. The linker moves cold + /// parts out of a function, and each such part has its own entry. + pub fn function_ranges(&self) -> std::collections::BTreeMap { + let (directory, size) = self.exception_directory; + let mut ranges = std::collections::BTreeMap::new(); + let Some(table) = self.offset(directory).filter(|_| directory != 0) else { + return ranges; + }; + for index in 0..(size / 12) as usize { + let entry = table + index * 12; + match (u32_at(self.bytes, entry), u32_at(self.bytes, entry + 4)) { + (Some(start), Some(end)) if end > start => { + ranges.insert(start, end); + } + (Some(_), Some(_)) => {} + _ => break, } - let guid = self.bytes.get(record + 4..record + 20)?; - let age = u32_at(self.bytes, record + 20)?; - let tail: String = guid[8..].iter().map(|byte| format!("{byte:02X}")).collect(); - return Some(format!( - "{:08X}{:04X}{:04X}{tail}{age:X}", - u32_at(guid, 0)?, - u16_at(guid, 4)?, - u16_at(guid, 6)?, - )); } - None + ranges } /// Finds the slot of `function` in the vtable at `vtable`, scanning a bounded number of slots. @@ -242,23 +257,6 @@ mod tests { bytes } - #[test] - fn nfr3_reads_the_symbol_server_key_of_an_image() { - let mut section = vec![0u8; 0x100]; - section[12..16].copy_from_slice(&2u32.to_le_bytes()); - section[24..28].copy_from_slice(&0x240u32.to_le_bytes()); - section[0x40..0x44].copy_from_slice(b"RSDS"); - let guid = [ - 0x51, 0x19, 0x13, 0x7b, 0xa2, 0x33, 0x0f, 0x9e, 0x4c, 0x4c, 0x44, 0x20, 0x50, 0x44, - 0x42, 0x2e, - ]; - section[0x44..0x54].copy_from_slice(&guid); - section[0x54..0x58].copy_from_slice(&1u32.to_le_bytes()); - let bytes = image(§ion); - let pe = PeFile::parse(&bytes).unwrap(); - assert_eq!(pe.pdb_key().unwrap(), "7B13195133A29E0F4C4C44205044422E1"); - } - #[test] fn nfr3_finds_a_function_in_a_vtable() { let mut section = vec![0u8; 0x100]; diff --git a/rivet/platform/windows/src/command_ids.rs b/rivet/platform/windows/src/command_ids.rs new file mode 100644 index 0000000..f6543d5 --- /dev/null +++ b/rivet/platform/windows/src/command_ids.rs @@ -0,0 +1,100 @@ +//! Chromium's browser command ids that Rivet runs, per Chromium milestone. +//! +//! A browser window runs these ids from its own `WM_COMMAND`, as its own menu and keys do, so they +//! need no address or member of the build. They are defined in `chrome/app/chrome_command_ids.h`, +//! and each value below was read from that header at the milestone's branch on +//! chromium.googlesource.com (`refs/branch-heads//chrome/app/chrome_command_ids.h`). They +//! are public and stable, so they are not values of a build row: the row generator and the +//! runtime both take them from here. A milestone without a read value has no id, and what needs it +//! stays off, because a wrong id runs another command. + +/// `IDC_DEV_TOOLS_INSPECT` and `IDC_DEV_TOOLS_CONSOLE` per Chromium milestone, with the branch the +/// header was read at. They are the commands of the Ctrl+Shift+C and Ctrl+Shift+J keys. +pub const DEV_TOOLS: &[(u16, u16, u32, u32)] = &[ + (150, 7871, 40023, 40005), + (151, 7922, 40023, 40005), + (152, 7977, 40023, 40005), + (153, 8010, 40023, 40005), + (154, 8037, 40023, 40005), + (155, 8059, 40023, 40005), + (156, 8078, 40023, 40005), + (157, 8103, 40023, 40005), + (158, 8127, 40023, 40005), + (159, 8154, 40023, 40005), +]; + +/// `IDC_NEW_TAB` per Chromium milestone, with the branch the header was read at. It is the command +/// of Ctrl+T and of the browser's own new tab button. Milestone 143 is the base of Samsung +/// Internet 30. +pub const NEW_TAB: &[(u16, u16, usize)] = &[ + (143, 7499, 34014), + (150, 7871, 34014), + (151, 7922, 34014), + (152, 7977, 34014), + (153, 8010, 34014), + (154, 8037, 34014), + (155, 8059, 34014), + (156, 8078, 34014), + (157, 8103, 34014), + (158, 8127, 34014), + (159, 8154, 34014), +]; + +/// The new tab command id of a Chromium milestone. +pub fn new_tab(milestone: u16) -> Option { + NEW_TAB + .iter() + .find(|(known, _, _)| *known == milestone) + .map(|(_, _, id)| *id) +} + +/// The paths of a row's values that are these command ids. A row generator never carries them: +/// it writes the value this table gives, or zero for a milestone it does not know. +pub const ROW_PATHS: &[&str] = &["toolbar.dev.inspect", "toolbar.dev.console"]; + +/// The inspect and console command ids of a Chromium milestone. +pub fn dev_tools(milestone: u16) -> Option<(u32, u32)> { + DEV_TOOLS + .iter() + .find(|(known, _, _, _)| *known == milestone) + .map(|(_, _, inspect, console)| (*inspect, *console)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn fr11_developer_commands_come_from_chromium_command_ids() { + for milestone in 150..=159u16 { + assert_eq!(dev_tools(milestone), Some((40023, 40005)), "{milestone}"); + } + // An unread milestone never borrows a neighbour's value. + assert_eq!(dev_tools(149), None); + assert_eq!(dev_tools(160), None); + // Each milestone appears once, in branch order. + assert!(DEV_TOOLS + .windows(2) + .all(|pair| pair[0].0 + 1 == pair[1].0 && pair[0].1 < pair[1].1)); + } + + #[test] + fn nfr6_the_new_tab_command_comes_from_chromium_command_ids() { + for milestone in (150..=159u16).chain([143]) { + assert_eq!(new_tab(milestone), Some(34014), "{milestone}"); + } + // An unread milestone never borrows a neighbour's value. + for milestone in [142, 144, 149, 160] { + assert_eq!(new_tab(milestone), None, "{milestone}"); + } + assert!(NEW_TAB + .windows(2) + .all(|pair| pair[0].0 < pair[1].0 && pair[0].1 < pair[1].1)); + // The same branches as the developer commands, which were read from the same headers. + for (milestone, branch, _) in NEW_TAB { + if let Some(dev) = DEV_TOOLS.iter().find(|dev| dev.0 == *milestone) { + assert_eq!(dev.1, *branch, "{milestone}"); + } + } + } +} diff --git a/rivet/platform/windows/src/lib.rs b/rivet/platform/windows/src/lib.rs index 27f8313..2734201 100644 --- a/rivet/platform/windows/src/lib.rs +++ b/rivet/platform/windows/src/lib.rs @@ -6,11 +6,16 @@ pub mod background; pub mod chrome_build; +pub mod command_ids; pub mod control; pub mod focus; pub mod hold; +pub mod row_class; +pub mod row_command; +pub mod row_features; pub mod row_port; pub mod row_recipe; +pub mod row_resolve; pub mod row_watch; pub mod signed_table; pub mod start; diff --git a/rivet/platform/windows/src/row_class.rs b/rivet/platform/windows/src/row_class.rs new file mode 100644 index 0000000..93b215b --- /dev/null +++ b/rivet/platform/windows/src/row_class.rs @@ -0,0 +1,742 @@ +//! Finds the class table of a views class in a build by the class name its `ui::metadata` holds. +//! +//! Chromium's views classes declare their metadata with `METADATA_HEADER` and `BEGIN_METADATA` +//! (`ui/base/metadata/metadata_header_macros.h` and `metadata_impl_macros.h`). `MetaData()` keeps +//! the class's `ClassMetaData` in a static pointer that code holding the class name fills on first +//! use, and `GetClassMetaData`, the second slot of the class's `ui::metadata::MetaDataProvider` +//! table, returns it, directly or through a jump to `MetaData()`. A constructor of the class writes +//! that table into the object next to the class's own table, which sits at the object's start +//! because `views::View` names `ui::LayerDelegate` as its first base (`ui/views/view.h`). +//! +//! So, from the name: the code that uses the text and its callers, the static pointer the first of +//! them to load one loads, every function that loads the same pointer as its result, the tables +//! whose second slot holds one of them, and the table the constructors that write such a table +//! write at offset zero of the same object. The load is read the way the runtime's +//! `chromium/reflect.rs` reads it, so the generator and the runtime agree on the static a table +//! names. Nothing is guessed: a step that finds nothing, or more than one table, gives no table. + +use std::collections::{BTreeMap, BTreeSet, HashSet}; + +use crate::chrome_build::PeFile; +use crate::row_port::x64; + +/// How many levels of callers above the code that uses the name are read. +const CALLER_DEPTH: usize = 3; +/// How many functions the callers may grow to before the name counts as too common to follow. +const CALLERS_MOST: usize = 64; +/// How many jumps a `GetClassMetaData` thunk may take to the function that loads the static. +const THUNK_DEPTH: usize = 3; +/// How far a constructor is read. +const BODY_MOST: u32 = 0x4000; + +/// Where the code at `at` gets its result: the target of its first `lea rax, [rip+d]`, or the word +/// its first `mov rax, [rip+d]` reads, after up to three jumps that may first move `this` with +/// `sub rcx, n` or `add rcx, n`. The same reading as the runtime's `reflect::load_target`. +pub fn load_target(pe: &PeFile, at: u32) -> Option<(u32, bool)> { + let mut code_at = at; + for _ in 0..3 { + let head = pe.read(code_at, 12)?; + let skip = match head[..3] { + [0x48, 0x83, 0xE9] | [0x48, 0x83, 0xC1] => 4, + [0x48, 0x81, 0xE9] | [0x48, 0x81, 0xC1] => 7, + _ => 0, + }; + if head[skip] != 0xE9 { + break; + } + let rel = i32::from_le_bytes(head[skip + 1..skip + 5].try_into().ok()?); + code_at = u32::try_from(i64::from(code_at) + skip as i64 + 5 + i64::from(rel)).ok()?; + } + let code = pe.read(code_at, 128)?; + let at = (0..code.len() - 7).find(|&i| { + code[i] == 0x48 && matches!(code[i + 1], 0x8D | 0x8B) && code[i + 2] == 0x05 + })?; + let rel = i32::from_le_bytes(code[at + 3..at + 7].try_into().ok()?); + let target = u32::try_from(i64::from(code_at) + at as i64 + 7 + i64::from(rel)).ok()?; + Some((target, code[at + 1] == 0x8B)) +} + +/// Every place outside the code that holds `name` as a zero-terminated text. +fn texts(pe: &PeFile, name: &str) -> BTreeSet { + let mut needle = name.as_bytes().to_vec(); + needle.push(0); + let mut found = BTreeSet::new(); + for (base, bytes, is_code) in pe.sections() { + if is_code || bytes.len() < needle.len() { + continue; + } + for (at, window) in bytes.windows(needle.len()).enumerate() { + if window[0] == needle[0] && window == needle.as_slice() { + found.insert(base + at as u32); + } + } + } + found +} + +/// A `lea` or `mov` of a 64-bit register with a RIP-relative operand. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct RipUse { + at: u32, + target: u32, + register: u8, + /// A `mov`, which reads the word at the target, rather than a `lea`. + word: bool, +} + +fn rip_use(bytes: &[u8], rva: u32) -> Option { + let [rex, op, modrm, ..] = *bytes else { + return None; + }; + if rex & 0xF8 != 0x48 || !matches!(op, 0x8D | 0x8B) || modrm & 0xC7 != 0x05 { + return None; + } + let rel = i32::from_le_bytes(bytes.get(3..7)?.try_into().ok()?); + Some(RipUse { + at: rva, + target: u32::try_from(i64::from(rva) + 7 + i64::from(rel)).ok()?, + register: (((rex >> 2) & 1) << 3) | ((modrm >> 3) & 7), + word: op == 0x8B, + }) +} + +/// Every RIP-relative `lea` and `mov` and every direct `call` and `jmp` of a module's code, sorted +/// by what they point at. The code of a browser is hundreds of megabytes and each class name asks +/// about it many times, so it is read once and each question is a search in the sorted lists. +#[derive(Debug, Default)] +pub struct CodeIndex { + uses: Vec, + /// (target, site, whether it is a jump). + branches: Vec<(u32, u32, bool)>, +} + +impl CodeIndex { + fn of<'p>(pe: &'p PeFile<'_>) -> &'p CodeIndex { + pe.code_index.get_or_init(|| { + let mut index = CodeIndex::default(); + for (base, bytes, is_code) in pe.sections() { + if !is_code { + continue; + } + for at in 0..bytes.len() { + let rva = base + at as u32; + if let Some(found) = bytes.get(at..at + 7).and_then(|raw| rip_use(raw, rva)) { + index.uses.push(found); + } + if matches!(bytes[at], 0xE8 | 0xE9) { + if let Some(raw) = bytes.get(at + 1..at + 5) { + let rel = i32::from_le_bytes(raw.try_into().expect("four bytes")); + let target = i64::from(rva) + 5 + i64::from(rel); + // Only a branch into code can be one into a function. + if let Some(target) = + u32::try_from(target).ok().filter(|target| pe.is_code(*target)) + { + index.branches.push((target, rva, bytes[at] == 0xE9)); + } + } + } + } + } + index.uses.sort_unstable_by_key(|each| (each.target, each.at)); + index.branches.sort_unstable(); + index + }) + } +} + +/// Every RIP-relative `lea` or `mov` in the code whose target is one of `targets`, in the order +/// of where they are. +fn rip_uses(pe: &PeFile, targets: &BTreeSet) -> Vec { + let index = CodeIndex::of(pe); + let mut found = Vec::new(); + for target in targets { + let from = index.uses.partition_point(|each| each.target < *target); + found.extend(index.uses[from..].iter().take_while(|each| each.target == *target).copied()); + } + found.sort_unstable_by_key(|each: &RipUse| each.at); + found +} + +/// Every direct `call` or `jmp` into one of `targets`, as (site, whether it is a jump), in the +/// order of where they are. +fn branches_into(pe: &PeFile, targets: &BTreeSet) -> Vec<(u32, bool)> { + let index = CodeIndex::of(pe); + let mut found = Vec::new(); + for target in targets { + let from = index.branches.partition_point(|each| each.0 < *target); + found.extend( + index.branches[from..] + .iter() + .take_while(|each| each.0 == *target) + .map(|each| (each.1, each.2)), + ); + } + found.sort_unstable(); + found +} + +/// The function part the exception table lists around an address. +fn enclosing(ranges: &BTreeMap, rva: u32) -> Option { + let (start, end) = ranges.range(..=rva).next_back()?; + (rva < *end).then_some(*start) +} + +/// The static pointers of the class metadata the code that uses the class name fills. +fn statics_of(pe: &PeFile, ranges: &BTreeMap, name: &str) -> BTreeSet<(u32, bool)> { + let texts = texts(pe, name); + if texts.is_empty() { + return BTreeSet::new(); + } + let mut layer: BTreeSet = rip_uses(pe, &texts) + .iter() + .filter(|found| !found.word) + .filter_map(|found| enclosing(ranges, found.at)) + .collect(); + let mut all = layer.clone(); + for _ in 0..CALLER_DEPTH { + if layer.is_empty() || all.len() > CALLERS_MOST { + break; + } + layer = branches_into(pe, &layer) + .into_iter() + .filter_map(|(site, _)| enclosing(ranges, site)) + .filter(|function| !all.contains(function)) + .collect(); + all.extend(&layer); + } + if all.len() > CALLERS_MOST { + return BTreeSet::new(); + } + all.iter() + .filter_map(|function| load_target(pe, *function)) + .filter(|(target, _)| !pe.is_code(*target)) + .collect() +} + +/// The functions whose result is `found`: those that load it, and the thunks that jump to them. +fn getters(pe: &PeFile, ranges: &BTreeMap, found: (u32, bool)) -> BTreeSet { + let loads = rip_uses(pe, &BTreeSet::from([found.0])); + let mut getters: BTreeSet = loads + .iter() + .filter(|each| each.register == 0 && each.word == found.1) + .filter_map(|each| enclosing(ranges, each.at)) + .filter(|function| load_target(pe, *function) == Some(found)) + .collect(); + let mut layer = getters.clone(); + for _ in 0..THUNK_DEPTH { + let mut next = BTreeSet::new(); + for (site, jump) in branches_into(pe, &layer) { + if !jump { + continue; + } + // `jmp`, `sub rcx, n; jmp` or `add rcx, n; jmp`, as a thunk of a base that is not the + // object's first moves `this` back before it jumps. + let starts = [ + Some(site), + site.checked_sub(4).filter(|start| { + pe.read(*start, 3) + .is_some_and(|head| matches!(head, [0x48, 0x83, 0xE9 | 0xC1])) + }), + site.checked_sub(7).filter(|start| { + pe.read(*start, 3) + .is_some_and(|head| matches!(head, [0x48, 0x81, 0xE9 | 0xC1])) + }), + ]; + for start in starts.into_iter().flatten() { + if !getters.contains(&start) && load_target(pe, start) == Some(found) { + next.insert(start); + } + } + } + if next.is_empty() { + break; + } + getters.extend(&next); + layer = next; + } + getters +} + +/// The tables outside the code whose second slot holds one of `functions`. +fn tables_holding(pe: &PeFile, functions: &BTreeSet) -> BTreeSet { + let wanted: HashSet = functions + .iter() + .map(|function| pe.image_base() + u64::from(*function)) + .collect(); + let mut found = BTreeSet::new(); + for (base, bytes, is_code) in pe.sections() { + if is_code { + continue; + } + for at in (8..bytes.len().saturating_sub(7)).step_by(8) { + let word = u64::from_le_bytes(bytes[at..at + 8].try_into().expect("in range")); + if wanted.contains(&word) { + found.insert(base + at as u32 - 8); + } + } + } + found +} + +/// A table a constructor writes into an object: the table, the register that holds the object +/// and how far into the object it goes. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct Store { + index: usize, + table: u32, + base: u8, + offset: i64, +} + +/// `mov [base + offset], register` with a 64-bit register, as (base, offset). +fn store_of(raw: &[u8], register: u8) -> Option<(u8, i64)> { + let rex = raw.first().copied().filter(|byte| byte & 0xF0 == 0x40)?; + let [_, op, modrm, ..] = *raw else { + return None; + }; + if rex & 8 == 0 || op != 0x89 { + return None; + } + let mode = modrm >> 6; + let rm = modrm & 7; + // Register to register, a SIB byte or a RIP-relative operand is not a store into an object. + if mode == 3 || rm == 4 || (mode == 0 && rm == 5) { + return None; + } + if ((((rex >> 2) & 1) << 3) | ((modrm >> 3) & 7)) != register { + return None; + } + let offset = match mode { + 0 => 0, + 1 => i64::from(*raw.get(3)? as i8), + _ => i64::from(i32::from_le_bytes(raw.get(3..7)?.try_into().ok()?)), + }; + Some((((rex & 1) << 3) | rm, offset)) +} + +/// The tables a function writes into objects: a `lea` of a table outside the code, then a store +/// of that register. +fn stores_in(pe: &PeFile, start: u32, end: u32) -> Vec { + let length = end.saturating_sub(start).min(BODY_MOST) as usize; + let Some(bytes) = pe.read(start, length) else { + return vec![]; + }; + let mut steps = Vec::new(); + let mut at = 0; + while at < bytes.len() { + let Some(instruction) = x64::decode_body(&bytes[at..]) else { + break; + }; + if instruction.length == 0 || at + instruction.length > bytes.len() { + break; + } + steps.push(&bytes[at..at + instruction.length]); + at += instruction.length; + } + let mut stores = Vec::new(); + let mut offset = 0u32; + for (index, raw) in steps.iter().enumerate() { + let rva = start + offset; + offset += raw.len() as u32; + let Some(lea) = (raw.len() == 7).then(|| rip_use(raw, rva)).flatten() else { + continue; + }; + if lea.word || pe.is_code(lea.target) { + continue; + } + if let Some((base, at)) = steps + .get(index + 1) + .and_then(|next| store_of(next, lea.register)) + { + stores.push(Store { + index, + table: lea.target, + base, + offset: at, + }); + } + } + stores +} + +/// The table each constructor that writes `provider` writes at the start of the same object. +fn first_tables(pe: &PeFile, ranges: &BTreeMap, provider: u32) -> BTreeSet { + let writers: BTreeSet = rip_uses(pe, &BTreeSet::from([provider])) + .iter() + .filter(|each| !each.word) + .filter_map(|each| enclosing(ranges, each.at)) + .collect(); + let mut found = BTreeSet::new(); + for writer in writers { + let stores = stores_in(pe, writer, ranges[&writer]); + for written in stores.iter().filter(|store| store.table == provider) { + let first = stores + .iter() + .filter(|store| { + store.base == written.base && store.offset == 0 && store.table != provider + }) + .min_by_key(|store| store.index.abs_diff(written.index)); + if let Some(first) = first { + found.insert(first.table); + } + } + } + found +} + +/// The class tables of the views class `name` in `pe`, each the table at the start of its +/// objects. More than one means the name does not tell one class table, and the caller takes none. +pub fn class_tables(pe: &PeFile, name: &str) -> BTreeSet { + let ranges = pe.function_ranges(); + let mut found = BTreeSet::new(); + for each in statics_of(pe, &ranges, name) { + let getters = getters(pe, &ranges, each); + if getters.is_empty() { + continue; + } + for provider in tables_holding(pe, &getters) { + found.extend(first_tables(pe, &ranges, provider)); + } + } + found +} + +/// How many levels of callees below the code that fills the metadata are read for its name. +const CALLEE_DEPTH: usize = 2; +/// How many names may be tried for one table before it counts as too unclear to name. +const NAMES_MOST: usize = 16; +/// How long a class name may be. +const NAME_MOST: usize = 120; + +/// The zero-terminated text at `rva` when it reads as a C++ class name without its namespace. +fn identifier_at(pe: &PeFile, rva: u32) -> Option { + let bytes = [NAME_MOST + 1, 64, 32, 16, 8] + .into_iter() + .find_map(|length| pe.read(rva, length))?; + let end = bytes.iter().position(|byte| *byte == 0)?; + let name = &bytes[..end]; + let first = *name.first()?; + (name.len() >= 2 + && (first.is_ascii_alphabetic() || first == b'_') + && name + .iter() + .all(|byte| byte.is_ascii_alphanumeric() || *byte == b'_')) + .then(|| String::from_utf8_lossy(name).into_owned()) +} + +/// What a function's body loads with a RIP-relative `lea` outside the code, and the functions it +/// calls or jumps to directly. +fn body_of(pe: &PeFile, ranges: &BTreeMap, start: u32) -> (Vec, Vec) { + let end = ranges.get(&start).copied().unwrap_or(start); + let length = end.saturating_sub(start).min(BODY_MOST) as usize; + let Some(bytes) = pe.read(start, length) else { + return (vec![], vec![]); + }; + let (mut loads, mut callees) = (Vec::new(), Vec::new()); + let mut at = 0; + while at < bytes.len() { + let Some(instruction) = x64::decode_body(&bytes[at..]) else { + break; + }; + if instruction.length == 0 || at + instruction.length > bytes.len() { + break; + } + let raw = &bytes[at..at + instruction.length]; + let rva = start + at as u32; + if let Some(found) = (raw.len() == 7).then(|| rip_use(raw, rva)).flatten() { + if !found.word && !pe.is_code(found.target) { + loads.push(found.target); + } + } + if raw.len() == 5 && matches!(raw[0], 0xE8 | 0xE9) { + let rel = i32::from_le_bytes(raw[1..5].try_into().expect("five bytes")); + if let Ok(target) = u32::try_from(i64::from(rva) + 5 + i64::from(rel)) { + if ranges.contains_key(&target) { + callees.push(target); + } + } + } + at += instruction.length; + } + (loads, callees) +} + +/// The metadata statics a class table's objects reach: each constructor that writes the table at +/// the start of an object also writes other tables into it, and the second slot of the +/// `ui::metadata::MetaDataProvider` table among them returns the class's static, as `getters` reads. +fn statics_of_table(pe: &PeFile, ranges: &BTreeMap, table: u32) -> BTreeSet<(u32, bool)> { + let writers: BTreeSet = rip_uses(pe, &BTreeSet::from([table])) + .iter() + .filter(|each| !each.word) + .filter_map(|each| enclosing(ranges, each.at)) + .collect(); + let mut found = BTreeSet::new(); + for writer in writers { + let stores = stores_in(pe, writer, ranges[&writer]); + for first in stores + .iter() + .filter(|store| store.table == table && store.offset == 0) + { + for other in stores.iter().filter(|store| { + store.base == first.base && store.offset != 0 && store.table != table + }) { + let Some(getter) = pe.pointer_rva(other.table + 8).filter(|at| pe.is_code(*at)) + else { + continue; + }; + if let Some(each) = load_target(pe, getter).filter(|(at, _)| !pe.is_code(*at)) { + found.insert(each); + } + } + } + } + found +} + +/// The names the code that fills a metadata static loads, nearest first: the texts that the +/// functions whose result is the static, and their callees, load. Only those that read as a class +/// name are kept. +fn names_of_static( + pe: &PeFile, + ranges: &BTreeMap, + found: (u32, bool), +) -> Vec> { + let mut layer: BTreeSet = rip_uses(pe, &BTreeSet::from([found.0])) + .iter() + .filter_map(|each| enclosing(ranges, each.at)) + .filter(|function| load_target(pe, *function) == Some(found)) + .collect(); + let mut seen = layer.clone(); + let mut by_depth = Vec::new(); + for _ in 0..=CALLEE_DEPTH { + if layer.is_empty() || seen.len() > CALLERS_MOST { + break; + } + let mut names = Vec::new(); + let mut next = BTreeSet::new(); + for function in &layer { + let (loads, callees) = body_of(pe, ranges, *function); + names.extend(loads.into_iter().filter_map(|at| identifier_at(pe, at))); + next.extend(callees.into_iter().filter(|callee| !seen.contains(callee))); + } + by_depth.push(names); + seen.extend(&next); + layer = next; + } + by_depth +} + +/// The views class a class table belongs to, by the class name its `ui::metadata` holds, read back +/// from the build: the metadata static the table's objects reach, and the names the code that fills +/// it loads. A name counts only where `class_tables` gives exactly this table for it, so a name +/// that is not the class's own gives none. The nearest code that yields exactly one such name +/// names the table; none, or two, gives no name. +pub fn class_name_of_table(pe: &PeFile, table: u32) -> Option { + let ranges = pe.function_ranges(); + let mut by_depth: Vec> = Vec::new(); + for each in statics_of_table(pe, &ranges, table) { + for (depth, names) in names_of_static(pe, &ranges, each).into_iter().enumerate() { + if by_depth.len() <= depth { + by_depth.resize(depth + 1, Vec::new()); + } + by_depth[depth].extend(names); + } + } + let mut tried = BTreeSet::new(); + for names in by_depth { + let mut confirmed = BTreeSet::new(); + for name in names { + if tried.len() >= NAMES_MOST { + return None; + } + if tried.insert(name.clone()) && class_tables(pe, &name) == BTreeSet::from([table]) { + confirmed.insert(name); + } + } + match confirmed.len() { + 0 => continue, + 1 => return confirmed.into_iter().next(), + _ => return None, + } + } + None +} + +/// Whether `name` is the class name of `table` in `pe`: `class_tables` gives this table alone. +pub fn names_table(pe: &PeFile, name: &str, table: u32) -> bool { + class_tables(pe, name) == BTreeSet::from([table]) +} + +#[cfg(test)] +mod tests { + use super::*; + + const IMAGE_BASE: u64 = 0x1_8000_0000; + + /// A module with code at 0x1000, read-only data at 0x2000 and data at 0x3000, each 0x1000 + /// long, and an exception table at 0x2f00 that lists `ranges`. + fn module(code: &[(u32, Vec)], ranges: &[(u32, u32)]) -> Vec { + let mut bytes = vec![0u8; 0x400]; + bytes[0x3c..0x40].copy_from_slice(&0x80u32.to_le_bytes()); + bytes[0x80..0x84].copy_from_slice(b"PE\0\0"); + bytes[0x86..0x88].copy_from_slice(&3u16.to_le_bytes()); + bytes[0x94..0x96].copy_from_slice(&240u16.to_le_bytes()); + let optional = 0x98; + bytes[optional..optional + 2].copy_from_slice(&0x20bu16.to_le_bytes()); + bytes[optional + 24..optional + 32].copy_from_slice(&IMAGE_BASE.to_le_bytes()); + let directory = optional + 112 + 24; + bytes[directory..directory + 4].copy_from_slice(&0x2f00u32.to_le_bytes()); + bytes[directory + 4..directory + 8] + .copy_from_slice(&(12 * ranges.len() as u32).to_le_bytes()); + for (index, flags) in [0x6000_0020u32, 0x4000_0040, 0xC000_0040].iter().enumerate() { + let header = optional + 240 + 40 * index; + let rva = 0x1000 * (index as u32 + 1); + bytes[header + 8..header + 12].copy_from_slice(&0x1000u32.to_le_bytes()); + bytes[header + 12..header + 16].copy_from_slice(&rva.to_le_bytes()); + bytes[header + 16..header + 20].copy_from_slice(&0x1000u32.to_le_bytes()); + let raw = 0x400 + 0x1000 * index as u32; + bytes[header + 20..header + 24].copy_from_slice(&raw.to_le_bytes()); + bytes[header + 36..header + 40].copy_from_slice(&flags.to_le_bytes()); + } + bytes.resize(0x400 + 0x3000, 0); + // Code between functions is padding. + bytes[0x400..0x1400].fill(0xCC); + let mut table = Vec::new(); + for (start, end) in ranges { + table.extend_from_slice(&start.to_le_bytes()); + table.extend_from_slice(&end.to_le_bytes()); + table.extend_from_slice(&0u32.to_le_bytes()); + } + let mut code = code.to_vec(); + code.push((0x2f00, table)); + for (rva, data) in code { + let at = 0x400 + (rva - 0x1000) as usize; + bytes[at..at + data.len()].copy_from_slice(&data); + } + bytes + } + + fn rel(from: u32, length: u32, to: u32) -> [u8; 4] { + (to as i32 - (from + length) as i32).to_le_bytes() + } + + fn pointer(rva: u32) -> Vec { + (IMAGE_BASE + u64::from(rva)).to_le_bytes().to_vec() + } + + /// `name`'s metadata at 0x3000, filled by its `MetaData()` at 0x1100 from the text at 0x2100. + /// `GetClassMetaData` at 0x1200 is a thunk to it in slot 1 of the `MetaDataProvider` table at + /// 0x2200, and the constructor at 0x1400 writes the class table at 0x2400 at the object's + /// start and the provider's table 0x58 bytes in. + fn class_module(name: &str) -> Vec { + let mut meta = vec![0x48, 0x8B, 0x05]; + meta.extend(rel(0x1100, 7, 0x3000)); + // test rax, rax; jne +7 + meta.extend([0x48, 0x85, 0xC0, 0x75, 0x07]); + meta.extend([0x48, 0x8D, 0x0D]); + meta.extend(rel(0x110c, 7, 0x2100)); + meta.push(0xC3); + let mut thunk = vec![0x48, 0x83, 0xE9, 0x58, 0xE9]; + thunk.extend(rel(0x1204, 5, 0x1100)); + let mut constructor = vec![0x48, 0x8D, 0x05]; + constructor.extend(rel(0x1400, 7, 0x2400)); + // mov [rsi], rax + constructor.extend([0x48, 0x89, 0x06]); + constructor.extend([0x48, 0x8D, 0x0D]); + constructor.extend(rel(0x140a, 7, 0x2200)); + // mov [rsi + 0x58], rcx + constructor.extend([0x48, 0x89, 0x4E, 0x58]); + constructor.push(0xC3); + let mut text = name.as_bytes().to_vec(); + text.push(0); + let mut provider = pointer(0x1300); + provider.extend(pointer(0x1200)); + let mut class = pointer(0x1300); + class.extend(pointer(0x1310)); + module( + &[ + (0x1100, meta), + (0x1200, thunk), + (0x1300, vec![0xC3]), + (0x1310, vec![0xC3]), + (0x1400, constructor), + (0x2100, text), + (0x2200, provider), + (0x2400, class), + ], + &[(0x1100, 0x1120), (0x1300, 0x1301), (0x1310, 0x1311), (0x1400, 0x1420)], + ) + } + + #[test] + fn nfr6_a_frame_vtable_is_found_from_its_class_name() { + let bytes = class_module("EdgeBrowserFrameViewWin"); + let pe = PeFile::parse(&bytes).unwrap(); + // The getter reads the static the way the runtime reads it. + assert_eq!(load_target(&pe, 0x1200), Some((0x3000, true))); + assert_eq!(class_tables(&pe, "EdgeBrowserFrameViewWin"), BTreeSet::from([0x2400])); + // A name the build does not hold, or holds only as part of a longer one, gives nothing. + assert!(class_tables(&pe, "BrowserFrameViewWin").is_empty()); + assert!(class_tables(&pe, "WindowsCaptionButton").is_empty()); + } + + #[test] + fn nfr6_a_class_whose_constructor_writes_no_first_table_gives_none() { + let mut bytes = class_module("BrowserView"); + // The constructor stores another register than the one it loaded the class table into, so + // no table is known at the object's start. + let at = 0x400 + 0x407; + assert_eq!(bytes[at..at + 3], [0x48, 0x89, 0x06]); + bytes[at + 2] = 0x0E; + let pe = PeFile::parse(&bytes).unwrap(); + assert_eq!(store_of(&[0x48, 0x89, 0x0E], 1), Some((6, 0))); + assert_eq!(store_of(&[0x48, 0x89, 0x4E, 0x58], 1), Some((6, 0x58))); + assert!(class_tables(&pe, "BrowserView").is_empty()); + } + + #[test] + fn nfr6_a_class_name_counts_only_where_it_gives_the_prior_table_in_the_prior_build() { + let prior = class_module("WindowsCaptionButton"); + let prior = PeFile::parse(&prior).unwrap(); + let new = class_module("WindowsCaptionButton"); + let new = PeFile::parse(&new).unwrap(); + let place = crate::row_resolve::class_place(&prior, &new, 0x2400, "WindowsCaptionButton"); + assert_eq!(place, Ok(0x2400)); + // The prior row's table is another one: the name does not tell this table. + let other = crate::row_resolve::class_place(&prior, &new, 0x2200, "WindowsCaptionButton"); + assert!(other.unwrap_err().contains("in the prior build")); + let missing = crate::row_resolve::class_place(&prior, &new, 0x2400, "CustomFloatingCorner"); + assert!(missing.is_err()); + } + + #[test] + fn nfr6_a_class_name_is_read_from_the_prior_build_without_symbols() { + let bytes = class_module("EdgeBrowserFrameViewWin"); + let pe = PeFile::parse(&bytes).unwrap(); + // The table the constructor writes at the object's start is named by the text its + // metadata code loads, and the name gives that table back. + assert_eq!( + class_name_of_table(&pe, 0x2400).as_deref(), + Some("EdgeBrowserFrameViewWin") + ); + assert!(names_table(&pe, "EdgeBrowserFrameViewWin", 0x2400)); + // The provider table is written further into the object, so it names no class. + assert_eq!(class_name_of_table(&pe, 0x2200), None); + assert!(!names_table(&pe, "EdgeBrowserFrameViewWin", 0x2200)); + // A name the metadata code does not load does not name the table. + assert!(!names_table(&pe, "BrowserFrameViewWin", 0x2400)); + } + + #[test] + fn nfr6_a_class_name_is_a_plain_identifier() { + let mut code = class_module("BrowserView"); + let at = 0x400 + 0x1000 + 0x100; + assert_eq!(&code[at..at + 12], b"BrowserView\0"); + let pe = PeFile::parse(&code).unwrap(); + assert_eq!(identifier_at(&pe, 0x2100).as_deref(), Some("BrowserView")); + // A path or a qualified name is not a class name. + code[at + 7] = b':'; + let pe = PeFile::parse(&code).unwrap(); + assert_eq!(identifier_at(&pe, 0x2100), None); + } +} diff --git a/rivet/platform/windows/src/row_command.rs b/rivet/platform/windows/src/row_command.rs new file mode 100644 index 0000000..5a46bb9 --- /dev/null +++ b/rivet/platform/windows/src/row_command.rs @@ -0,0 +1,505 @@ +//! Finds a function a browser command runs from its public command id. +//! +//! `chrome::BrowserCommandController::HandleCommandWithDisposition` runs every browser command +//! through one `switch (id)` (`chrome/browser/ui/browser_command_controller.cc`), and each case +//! calls the command's function first: `case IDC_NEW_TAB` calls `chrome::NewTab(browser_, ...)`. +//! The ids are public (`chrome/app/chrome_command_ids.h`, kept in `command_ids`). A switch that +//! dense compiles to a jump table: the id less the lowest case is checked against the number of +//! cases, then the table gives the case's code. So the case of an id is read from the table, and +//! the function is the first one its code calls. +//! +//! A build holds other switches over command ids, so the reading is checked on the prior build +//! first: the switches whose case for the id calls the prior row's function there tell the lowest +//! case of the dispatch switch, and in the new build only switches with that same lowest case are +//! read. Nothing is taken unless they all name one function. + +use std::collections::BTreeSet; + +use crate::chrome_build::PeFile; +use crate::row_port::x64; + +/// How far a case's code is read for its first call. +const CASE_REACH: usize = 0x80; + +/// A jump table switch: the lowest case it subtracts, the highest index it takes, its table and +/// where an index past it goes. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Switch { + pub at: u32, + pub lowest: i64, + pub bound: u32, + pub table: u32, + pub default: u32, +} + +fn i32_at(bytes: &[u8], at: usize) -> Option { + Some(i32::from_le_bytes(bytes.get(at..at + 4)?.try_into().ok()?)) +} + +fn rip_target(rva: u32, length: usize, displacement: i32) -> Option { + u32::try_from(i64::from(rva) + length as i64 + i64::from(displacement)).ok() +} + +/// `lea r64, [rip + d]; movsxd r64, dword [r + index * 4]; add r64, r64; jmp r64` at `at`, as the +/// table it loads. +fn table_jump(bytes: &[u8], at: usize, rva: u32) -> Option { + let code = bytes.get(at..at + 17)?; + let [rex, 0x8D, modrm, ..] = *code else { + return None; + }; + if rex & 0xFB != 0x48 || modrm & 0xC7 != 0x05 { + return None; + } + let register = (((rex >> 2) & 1) << 3) | ((modrm >> 3) & 7); + let table = rip_target(rva, 7, i32_at(code, 3)?)?; + // movsxd with a SIB byte that scales the index by four and takes the table as its base. + let [load_rex, 0x63, load_modrm, sib] = code[7..11] else { + return None; + }; + if load_rex & 0xF8 != 0x48 || load_modrm & 0xC7 != 0x04 || sib >> 6 != 2 { + return None; + } + if ((load_rex & 1) << 3) | (sib & 7) != register { + return None; + } + // add r64, r64 + let [add_rex, add, add_modrm] = code[11..14] else { + return None; + }; + if add_rex & 0xF8 != 0x48 || !matches!(add, 0x01 | 0x03) || add_modrm >> 6 != 3 { + return None; + } + // jmp r64, with a REX prefix for r8 to r15. + let jump = if code[14] == 0x41 { + &code[15..17] + } else { + &code[14..16] + }; + (jump[0] == 0xFF && jump[1] & 0xF8 == 0xE0).then_some(table) +} + +/// The `ja` that ends at `end`, as its target, with where it starts. +fn above_before(bytes: &[u8], end: usize, base: u32) -> Option<(usize, u32)> { + if let Some(start) = end.checked_sub(6) { + if bytes.get(start..start + 2) == Some(&[0x0F, 0x87][..]) { + let target = rip_target(base + start as u32, 6, i32_at(bytes, start + 2)?)?; + return Some((start, target)); + } + } + let start = end.checked_sub(2)?; + let [0x77, rel] = *bytes.get(start..end)? else { + return None; + }; + Some(( + start, + rip_target(base + start as u32, 2, i32::from(rel as i8))?, + )) +} + +/// The `cmp r32, imm` that ends at `end`, as its immediate, with where it starts. +fn compare_before(bytes: &[u8], end: usize) -> Option<(usize, u32)> { + let at = |length: usize| { + let start = end.checked_sub(length)?; + Some((start, bytes.get(start..end)?)) + }; + // cmp eax, imm32 + if let Some((start, [0x3D, ..])) = at(5) { + return Some((start, i32_at(bytes, start + 1)? as u32)); + } + for rex in [false, true] { + let skip = usize::from(rex); + if let Some((start, code)) = at(6 + skip) { + if (!rex || code[0] == 0x41) && code[skip] == 0x81 && code[skip + 1] & 0xF8 == 0xF8 { + return Some((start, i32_at(bytes, start + skip + 2)? as u32)); + } + } + if let Some((start, code)) = at(3 + skip) { + if (!rex || code[0] == 0x41) && code[skip] == 0x83 && code[skip + 1] & 0xF8 == 0xF8 { + return Some((start, code[skip + 2] as i8 as i32 as u32)); + } + } + } + None +} + +/// The lowest case a switch subtracts before `end`: `lea r32, [r - n]`, `add r32, -n` or +/// `sub r32, n`, nearest first. Zero when the switch takes the id as it is. +fn lowest_before(bytes: &[u8], end: usize) -> i64 { + for stop in (end.saturating_sub(16)..=end).rev() { + for start in stop.saturating_sub(7)..stop { + let Some(code) = bytes.get(start..stop) else { + continue; + }; + let skip = usize::from(matches!(code.first(), Some(0x40..=0x4F)) && code.len() > 1); + let rest = &code[skip..]; + let value = match *rest { + // lea r32, [r + disp32] or [r + disp8], with no SIB byte. + [0x8D, modrm, ..] if modrm >> 6 == 2 && modrm & 7 != 4 && rest.len() == 6 => { + i32_at(rest, 2).map(|value| -i64::from(value)) + } + [0x8D, modrm, disp] if modrm >> 6 == 1 && modrm & 7 != 4 => { + Some(-i64::from(disp as i8)) + } + [0x05, ..] if rest.len() == 5 => i32_at(rest, 1).map(|value| -i64::from(value)), + [0x2D, ..] if rest.len() == 5 => i32_at(rest, 1).map(i64::from), + [0x81, modrm, ..] if rest.len() == 6 && modrm & 0xF8 == 0xC0 => { + i32_at(rest, 2).map(|value| -i64::from(value)) + } + [0x81, modrm, ..] if rest.len() == 6 && modrm & 0xF8 == 0xE8 => { + i32_at(rest, 2).map(i64::from) + } + _ => None, + }; + if let Some(value) = value { + return value; + } + } + } + 0 +} + +/// Every jump table switch in the build's code. +pub fn switches(pe: &PeFile) -> Vec { + let mut found = Vec::new(); + for (base, bytes, is_code) in pe.sections() { + if !is_code { + continue; + } + for at in 0..bytes.len().saturating_sub(17) { + if bytes[at + 1] != 0x8D { + continue; + } + let rva = base + at as u32; + let Some(table) = table_jump(bytes, at, rva) else { + continue; + }; + if pe.read(table, 4).is_none() { + continue; + } + // The bound check ends right before the table load, or before a `mov r32, r32` that + // clears the index's upper half. + let Some((above, default)) = [at, at.saturating_sub(2), at.saturating_sub(3)] + .into_iter() + .find_map(|end| above_before(bytes, end, base)) + else { + continue; + }; + let Some((compare, bound)) = compare_before(bytes, above) else { + continue; + }; + found.push(Switch { + at: rva, + lowest: lowest_before(bytes, compare), + bound, + table, + default, + }); + } + } + found +} + +/// Where a switch sends `id`, unless it sends it where it sends an id it has no case for. +pub fn case_of(pe: &PeFile, switch: &Switch, id: i64) -> Option { + let index = u32::try_from(id - switch.lowest).ok()?; + if index > switch.bound { + return None; + } + let entry = i32::from_le_bytes(pe.read(switch.table + index * 4, 4)?.try_into().ok()?); + let case = rip_target(switch.table, 0, entry)?; + (case != switch.default && pe.is_code(case)).then_some(case) +} + +/// The first function the code at `at` calls before it leaves, after one jump it may start with. +pub fn first_call(pe: &PeFile, at: u32) -> Option { + let mut at = at; + let mut followed = false; + let bytes = pe.read(at, CASE_REACH)?; + let mut bytes = bytes.to_vec(); + let mut offset = 0; + while offset < bytes.len() { + let rva = at + offset as u32; + match bytes[offset] { + 0xE8 => return rip_target(rva, 5, i32_at(&bytes, offset + 1)?), + 0xE9 if offset == 0 && !followed => { + at = rip_target(rva, 5, i32_at(&bytes, 1)?)?; + bytes = pe.read(at, CASE_REACH)?.to_vec(); + followed = true; + continue; + } + 0xC3 | 0xE9 | 0xEB | 0xCC => return None, + _ => {} + } + let instruction = x64::decode_body(&bytes[offset..])?; + if instruction.length == 0 { + return None; + } + offset += instruction.length; + } + None +} + +/// The function each switch's case for `id` calls first, with the switch. +pub fn command_calls(pe: &PeFile, id: i64) -> Vec<(Switch, u32)> { + calls_through(pe, &switches(pe), id) +} + +/// The function each of `switches`' case for `id` calls first, with the switch. +fn calls_through(pe: &PeFile, switches: &[Switch], id: i64) -> Vec<(Switch, u32)> { + switches + .iter() + .filter_map(|switch| { + let case = case_of(pe, switch, id)?; + Some((*switch, first_call(pe, case)?)) + }) + .collect() +} + +/// The function the command `id` runs in the new build, read the same way that gives `target` in +/// the prior build: only switches whose lowest case is that of a prior switch that sends the id to +/// `target` count, all of them must call one function, and in the prior build every such switch +/// calls `target`. +pub fn command_place( + old_pe: &PeFile, + new_pe: &PeFile, + target: u32, + id: i64, +) -> Result { + let old = (old_pe, switches(old_pe)); + let new = (new_pe, switches(new_pe)); + command_place_in((old.0, &old.1), (new.0, &new.1), target, id) +} + +/// `command_place` with the switches of both builds already read. +pub fn command_place_in( + old: (&PeFile, &[Switch]), + new: (&PeFile, &[Switch]), + target: u32, + id: i64, +) -> Result { + let prior = calls_through(old.0, old.1, id); + let lowest: BTreeSet = prior + .iter() + .filter(|(_, called)| *called == target) + .map(|(switch, _)| switch.lowest) + .collect(); + if lowest.is_empty() { + return Err(format!( + "no switch of the prior build sends command {id} to it" + )); + } + let others = prior + .iter() + .filter(|(switch, called)| lowest.contains(&switch.lowest) && *called != target) + .count(); + if others > 0 { + return Err(format!( + "{others} switches of the prior build like its dispatch send command {id} elsewhere" + )); + } + let called: BTreeSet = calls_through(new.0, new.1, id) + .into_iter() + .filter(|(switch, _)| lowest.contains(&switch.lowest)) + .map(|(_, called)| called) + .collect(); + match called.into_iter().collect::>().as_slice() { + [only] => Ok(*only), + many => Err(format!( + "command {id} reaches {} functions through the dispatch in the new build", + many.len() + )), + } +} + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + use crate::row_resolve::tests::{call, module}; + + pub const DISPATCH: u32 = 0x101000; + const OTHER_SWITCH: u32 = 0x101800; + const TABLE: u32 = 0x201000; + const OTHER_TABLE: u32 = 0x203000; + const DEFAULT: u32 = 0x101f00; + const BACK: u32 = 0x102000; + const ELSEWHERE: u32 = 0x104000; + const LOWEST: i32 = 33000; + const NEW_TAB_ID: i64 = 34014; + + fn rel(from: u32, length: u32, to: u32) -> [u8; 4] { + ((i64::from(to) - i64::from(from) - i64::from(length)) as i32).to_le_bytes() + } + + /// A switch at `at` over ids from `lowest` with `bound + 1` cases in the table at `table`: + /// `lea eax, [rdx - lowest]; cmp eax, bound; ja default; lea rcx, [rip + table]; + /// movsxd rax, [rcx + rax * 4]; add rax, rcx; jmp rax`. + fn switch_code(at: u32, lowest: i32, bound: u32, table: u32) -> Vec { + let mut code = vec![0x8D, 0x82]; + code.extend((-lowest).to_le_bytes()); + code.push(0x3D); + code.extend(bound.to_le_bytes()); + code.extend([0x0F, 0x87]); + code.extend(rel(at + code.len() as u32 - 2, 6, DEFAULT)); + code.extend([0x48, 0x8D, 0x0D]); + code.extend(rel(at + code.len() as u32 - 3, 7, table)); + code.extend([0x48, 0x63, 0x04, 0x81, 0x48, 0x01, 0xC8, 0xFF, 0xE0]); + code + } + + /// A case that loads the browser and a constant and calls `to`, then leaves the switch. + fn case_code(at: u32, to: u32) -> Vec { + let mut code = vec![0x48, 0x8B, 0x4E, 0x10, 0xBA, 0x01, 0, 0, 0]; + code.extend(call(at + code.len() as u32, to)); + code.extend([0xEB, 0x00]); + code + } + + /// A table of `bound + 1` entries from `table`, each the distance to `default` except `cases`. + fn table_of(table: u32, bound: u32, cases: &[(u32, u32)]) -> Vec { + let mut bytes = Vec::new(); + for index in 0..=bound { + let to = cases + .iter() + .find(|(case, _)| *case == index) + .map_or(DEFAULT, |(_, to)| *to); + bytes.extend(((i64::from(to) - i64::from(table)) as i32).to_le_bytes()); + } + bytes + } + + /// A build whose dispatch over ids from 33000 sends IDC_BACK to `BACK` and IDC_NEW_TAB to + /// `new_tab`, and that holds a second switch, over ids from 34000, whose case for 34014 calls + /// another function. `new_tab` gets the bytes in `body` once, or at each of `copies`. + pub fn dispatch_module(new_tab: Option, copies: &[u32]) -> Vec { + let bound = 1100; + let back_case = 0x101100; + let new_tab_case = 0x101140; + let other_case = 0x101900; + let mut parts = vec![ + (DISPATCH, switch_code(DISPATCH, LOWEST, bound, TABLE)), + (back_case, case_code(back_case, BACK)), + ( + OTHER_SWITCH, + switch_code(OTHER_SWITCH, 34000, 40, OTHER_TABLE), + ), + (other_case, case_code(other_case, ELSEWHERE)), + (DEFAULT, vec![0xC3]), + (BACK, vec![0xC3]), + (ELSEWHERE, vec![0xC3]), + (OTHER_TABLE, table_of(OTHER_TABLE, 40, &[(14, other_case)])), + ]; + let mut cases = vec![(0, back_case)]; + if let Some(new_tab) = new_tab { + parts.push((new_tab_case, case_code(new_tab_case, new_tab))); + cases.push(((NEW_TAB_ID - i64::from(LOWEST)) as u32, new_tab_case)); + } + parts.push((TABLE, table_of(TABLE, bound, &cases))); + let body = [ + 0x56, 0x48, 0x83, 0xEC, 0x20, 0x48, 0x89, 0xCE, 0x48, 0x83, 0xC4, 0x20, 0x5E, 0xC3, + ]; + let mut ranges = vec![(DISPATCH, DISPATCH + 0x40)]; + for at in copies { + parts.push((*at, body.to_vec())); + ranges.push((*at, *at + body.len() as u32)); + } + module(&parts, &ranges) + } + + pub const OLD_NEW_TAB: u32 = 0x103000; + pub const NEW_NEW_TAB: u32 = 0x103400; + + #[test] + fn nfr6_a_switch_case_is_read_from_its_jump_table() { + let bytes = dispatch_module(Some(OLD_NEW_TAB), &[OLD_NEW_TAB]); + let pe = PeFile::parse(&bytes).unwrap(); + let found = switches(&pe); + // The table load follows `lea`, `cmp` and `ja`: 6, 5 and 6 bytes. + let dispatch = found + .iter() + .find(|switch| switch.at == DISPATCH + 17) + .unwrap(); + assert_eq!( + ( + dispatch.lowest, + dispatch.bound, + dispatch.table, + dispatch.default + ), + (i64::from(LOWEST), 1100, TABLE, DEFAULT) + ); + let back = case_of(&pe, dispatch, 33000).and_then(|case| first_call(&pe, case)); + assert_eq!(back, Some(BACK)); + // An id with no case goes where the switch sends any id it does not know. + assert_eq!(case_of(&pe, dispatch, 33001), None); + assert_eq!(case_of(&pe, dispatch, 40000), None); + let mut called: Vec = command_calls(&pe, NEW_TAB_ID) + .into_iter() + .map(|(_, to)| to) + .collect(); + called.sort(); + assert_eq!(called, [OLD_NEW_TAB, ELSEWHERE]); + } + + /// What the resolver makes of `chrome::NewTab` between the two builds, with the places its + /// fingerprint matched off the exception table's function starts. + fn resolve_new_tab(loose: Vec) -> crate::row_resolve::Resolution { + use crate::row_resolve::{resolve, Kind, Wanted}; + use std::collections::BTreeMap; + let old = dispatch_module(Some(OLD_NEW_TAB), &[OLD_NEW_TAB]); + let new = dispatch_module(Some(NEW_NEW_TAB), &[NEW_NEW_TAB]); + let (old, new) = (PeFile::parse(&old).unwrap(), PeFile::parse(&new).unwrap()); + let wanted = BTreeMap::from([( + OLD_NEW_TAB, + Wanted { + kind: Kind::Function, + candidates: None, + class: None, + command: Some(NEW_TAB_ID), + loose, + }, + )]); + let none = BTreeSet::new(); + resolve(&old, &new, &BTreeMap::new(), &wanted, &none, &none) + } + + #[test] + fn nfr6_new_tab_is_taken_where_its_command_and_fingerprint_agree() { + let resolution = resolve_new_tab(vec![NEW_NEW_TAB]); + assert_eq!(resolution.places.get(&OLD_NEW_TAB), Some(&NEW_NEW_TAB)); + assert_eq!( + resolution.settled, + ["0x103000 -> 0x103400 by its fingerprint and its command id"] + ); + // The command id alone is one method. + let alone = resolve_new_tab(Vec::new()); + assert!(alone.places.is_empty()); + let reason = &alone.reasons[&OLD_NEW_TAB]; + assert!( + reason.starts_with("only its command id found it"), + "{reason}" + ); + } + + #[test] + fn nfr6_new_tab_is_found_from_the_new_tab_command() { + let old = dispatch_module(Some(OLD_NEW_TAB), &[OLD_NEW_TAB]); + let new = dispatch_module(Some(NEW_NEW_TAB), &[NEW_NEW_TAB]); + let (old, new) = (PeFile::parse(&old).unwrap(), PeFile::parse(&new).unwrap()); + // Only the dispatch's own switch counts; the other switch's case calls another function. + assert_eq!( + command_place(&old, &new, OLD_NEW_TAB, NEW_TAB_ID), + Ok(NEW_NEW_TAB) + ); + // The prior build must send the command to the prior row's function. + let refused = command_place(&old, &new, 0x103800, NEW_TAB_ID).unwrap_err(); + assert!( + refused.starts_with("no switch of the prior build"), + "{refused}" + ); + // A new build whose dispatch has no case for the command gives nothing. + let gone = dispatch_module(None, &[]); + let gone = PeFile::parse(&gone).unwrap(); + let none = command_place(&old, &gone, OLD_NEW_TAB, NEW_TAB_ID).unwrap_err(); + assert!(none.contains("reaches 0 functions"), "{none}"); + } +} diff --git a/rivet/platform/windows/src/row_features.rs b/rivet/platform/windows/src/row_features.rs new file mode 100644 index 0000000..266fa47 --- /dev/null +++ b/rivet/platform/windows/src/row_features.rs @@ -0,0 +1,487 @@ +//! Which feature of a row each value belongs to, and how a row turns that feature off. +//! +//! A row value that could not be read from its own build must never reach a browser. When it +//! belongs to a feature the runtime can do without, the row turns that feature off and clears the +//! value, and the rest of the row can still ship once its checks pass. A value of a part the +//! window cannot do without (the frame and the page area layout) has no switch here, so it still +//! stops the row and the browser opens with Rivet's held build. +//! +//! The switches are the ones the runtime already obeys: a section's `supported` flag, an optional +//! part left `null`, an empty list of colour ids, and the frame colour's `provider_verified`. Each +//! one was checked against every reader of the values it covers, so a feature that is off reads +//! none of them. The same switches work in older runtimes, which read the same row fields. + +use serde_json::{json, Value}; + +/// The address a build records for what it does not have. +const ABSENT: &str = "0x7fff0000"; + +/// What turns a feature off in a row. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Off { + /// A `supported` or `provider_verified` flag set to false. + Flag(&'static str), + /// An optional part set to `null`. + Part(&'static str), +} + +/// What a value of a feature that is off becomes, so the row holds nothing of another build. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Clear { + /// Every number in it becomes zero, in the form it was written. + Zero, + /// The address the build does not have, which a reader takes as nothing there. + Absent, + /// An empty list. + Empty, +} + +/// A feature of a row and the values only it reads. +pub struct RowFeature { + /// The feature's name, as the runtime and the report give it. + pub name: &'static str, + /// Row paths of its values. A path that ends with `.` covers the whole section. + pub paths: &'static [&'static str], + pub off: &'static [Off], + pub clear: Clear, + /// The checks of `scripts/tests/windows-features.ps1` that need the feature. A row that has the + /// feature off is not expected to pass them. A prefix ending with `-` covers its family. + pub checks: &'static [&'static str], +} + +pub const ROW_FEATURES: &[RowFeature] = &[ + RowFeature { + name: "split view", + paths: &["split."], + off: &[Off::Flag("split.supported")], + clear: Clear::Zero, + checks: &["split", "split-"], + }, + RowFeature { + name: "vertical tabs", + paths: &[ + "vertical.update_collapse_action", + "vertical.browser_offset", + "vertical.mode_offset", + "vertical.width_offset", + ], + off: &[Off::Flag("vertical.supported")], + clear: Clear::Zero, + checks: &[], + }, + RowFeature { + name: "sidebar", + paths: &[ + "side_panel.color_source", + "side_panel.source_in_widget", + "side_panel.entry_count", + "side_panel.extension_entry", + "side_panel.features_coordinator", + "side_panel.features_offset", + "side_panel.browser_offset", + ], + off: &[Off::Flag("side_panel.supported")], + clear: Clear::Zero, + checks: &["sidebar", "sidebar-", "fullscreen-sidebar"], + }, + RowFeature { + name: "sidebar", + paths: &["side_panel.hidden_entries"], + off: &[Off::Flag("side_panel.supported")], + clear: Clear::Empty, + checks: &["sidebar", "sidebar-", "fullscreen-sidebar"], + }, + RowFeature { + // Fields of the browser's layout object that only the Sidebar's part of the layout reads. + // The layout reads an absent field as no view. + name: "sidebar", + paths: &[ + "side_panel.side_panel", + "side_panel.side_panel_animation", + "side_panel.contents_outline", + "side_panel.vertical_tabs", + ], + off: &[Off::Flag("side_panel.supported")], + clear: Clear::Absent, + checks: &["sidebar", "sidebar-", "fullscreen-sidebar"], + }, + RowFeature { + // The Sidebar's own functions and class tables. Only `features/side_panel.rs` reads them, + // and only once the Sidebar's targets are all recorded. + name: "sidebar", + paths: &[ + "side_panel.side_panel_vtable", + "side_panel.coordinator_vtable", + "side_panel.browser_view_view_vtable", + "side_panel.show", + "side_panel.entry_id_to_string", + "side_panel.toggle", + "side_panel.entry_showing", + "side_panel.registry_from", + "side_panel.get_color_provider", + "side_panel.generate_colors_css", + "side_panel.event_handler_for_point", + ], + off: &[Off::Flag("side_panel.supported")], + clear: Clear::Absent, + checks: &["sidebar", "sidebar-", "fullscreen-sidebar"], + }, + RowFeature { + // The vertical tab controller's functions and class tables. Only + // `features/vertical_tabs.rs` reads them, and only once its targets are all recorded. + name: "vertical tabs", + paths: &[ + "vertical.slot", + "vertical.should_display", + "vertical.destructor", + "vertical.browser_view_for_browser", + "vertical.hwnd_for_view", + "vertical.notify_mode_changed", + "vertical.browser_view_view_vtable", + "vertical.region_preferred_size", + "vertical.region_preferred_size_slot", + "vertical.region_minimum_size", + "vertical.invalidate_layout", + ], + off: &[Off::Flag("vertical.supported")], + clear: Clear::Absent, + checks: &[], + }, + RowFeature { + name: "app window", + paths: &["app_window."], + off: &[Off::Part("app_window")], + clear: Clear::Zero, + checks: &["mini-", "tab-moves"], + }, + RowFeature { + name: "address suggestions", + paths: &["omnibox."], + off: &[Off::Part("omnibox")], + clear: Clear::Zero, + checks: &["address-suggest", "security-chip"], + }, + RowFeature { + // An empty list is a build without developer colours, whose page keeps the browser's own. + name: "developer colours", + paths: &["toolbar.dev.colours"], + off: &[], + clear: Clear::Empty, + checks: &["dev-row"], + }, + RowFeature { + // Font functions recorded beside the developer colours. No runtime calls them. + name: "developer colours", + paths: &["toolbar.dev.font_list_new", "toolbar.dev.font_new"], + off: &[], + clear: Clear::Absent, + checks: &["dev-row"], + }, + RowFeature { + name: "frame colour", + paths: &["layout.frame_ids", "layout.caption_ink_ids"], + off: &[Off::Flag("layout.provider_verified")], + clear: Clear::Empty, + checks: &[], + }, +]; + +/// Values the runtime finds in the browser's own memory by Chromium's class names and the shape of +/// the view tree, so a row needs none of them. Where a row has one, the runtime only compares it +/// with what it found. A value no instruction of the new build gave is left out of the row rather +/// than carried from the prior build, and does not stop the row: the browser view, the area below +/// the top row and the view of the pages are the layout's views named `BrowserView`, +/// `MainBackgroundRegionView` and `MultiContentsView`; the frame view's field is the one that holds +/// that same browser view; and a view's widget is the one the root of its view tree names. A +/// runtime from before this cannot read a row without them, so it refuses the row and opens its +/// held build instead of guessing. +pub const REFLECTED: &[&str] = &[ + "layout.frame_browser_window_offset", + "side_panel.contents_container", + "side_panel.host_view", + "side_panel.page_area", + "toolbar.get_widget_slot", +]; + +/// Whether the runtime finds a value by reflection, so a row may leave it out. +pub fn is_reflected(path: &str) -> bool { + REFLECTED.contains(&field_of(path)) +} + +/// Leaves the field a value path names out of a row's build part. +pub fn leave_out(build: &mut Value, path: &str) { + let field = field_of(path); + let (parent, key) = match field.rsplit_once('.') { + Some((parent, key)) => (Some(parent), key), + None => (None, field), + }; + let holder = match parent { + Some(parent) => at_mut(build, parent), + None => Some(build), + }; + if let Some(object) = holder.and_then(Value::as_object_mut) { + object.remove(key); + } +} + +/// Values the runtime does not read while another value of the row says so: then they are not +/// values of the build at all, and a row holds zero for them. +pub struct UnusedWhen { + pub paths: &'static [&'static str], + /// The value of the row that decides it. + pub when: When, + pub why: &'static str, +} + +/// What a deciding value of a row must be for other values to be unused. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum When { + /// An address the build records. + Recorded(&'static str), + /// An address the build does not record. + Absent(&'static str), + /// A flag that is false. + False(&'static str), +} + +pub const UNUSED_WHEN: &[UnusedWhen] = &[ + UnusedWhen { + paths: &["tabs.tab_collection", "tabs.tab_contents"], + when: When::Recorded("tabs.tab_at"), + why: "the build has its own GetWebContentsAt", + }, + UnusedWhen { + paths: &[ + "vertical.window_feature_tab_strip", + "vertical.window_feature_callers", + "vertical.toolbar_in_view", + "vertical.toolbar_leading", + ], + when: When::Absent("vertical.window_feature"), + why: "the tab strip is left out through ShouldDrawTabStrip, not the window feature", + }, + UnusedWhen { + paths: &["vertical.should_draw_tab_strip"], + when: When::False("vertical.detour_strip"), + why: "the build does not leave its tab strip out by a detour", + }, +]; + +/// The field a value path names: `layout.frame_ids[2]` names `layout.frame_ids`. +pub fn field_of(path: &str) -> &str { + path.split('[').next().unwrap_or(path) +} + +fn covers(rule: &str, path: &str) -> bool { + let field = field_of(path); + if rule.ends_with('.') { + field.starts_with(rule) + } else { + field == rule + } +} + +/// The feature a value belongs to, when the row can turn it off. +pub fn feature_of(path: &str) -> Option<&'static RowFeature> { + ROW_FEATURES + .iter() + .find(|feature| feature.paths.iter().any(|rule| covers(rule, path))) +} + +fn at<'a>(build: &'a Value, path: &str) -> Option<&'a Value> { + path.split('.').try_fold(build, |value, key| value.get(key)) +} + +fn at_mut<'a>(build: &'a mut Value, path: &str) -> Option<&'a mut Value> { + path.split('.').try_fold(build, |value, key| value.get_mut(key)) +} + +fn is_recorded(value: &Value) -> bool { + crate::row_recipe::number(value) + .is_some_and(|value| (0x10_0000..0x7FFF_0000).contains(&value)) +} + +/// Why the runtime does not read a value in this row, or `None` when it does. +pub fn unused(build: &Value, path: &str) -> Option<&'static str> { + UNUSED_WHEN + .iter() + .find(|rule| { + rule.paths.iter().any(|each| covers(each, path)) + && match rule.when { + When::Recorded(address) => at(build, address).is_some_and(is_recorded), + When::Absent(address) => { + at(build, address).is_some_and(|value| !is_recorded(value)) + } + When::False(flag) => at(build, flag).and_then(Value::as_bool) == Some(false), + } + }) + .map(|rule| rule.why) +} + +fn zero(value: &mut Value) { + if let Value::Array(items) = value { + items.iter_mut().for_each(zero); + return; + } + let cleared = match &*value { + Value::String(text) if text.starts_with("0x") => json!("0x0"), + Value::Number(_) => json!(0), + _ => return, + }; + *value = cleared; +} + +/// Clears the field a value path names, as `clear` says. +pub fn clear(build: &mut Value, path: &str, how: Clear) { + let Some(value) = at_mut(build, field_of(path)) else { + return; + }; + match how { + Clear::Zero => zero(value), + Clear::Absent => *value = json!(ABSENT), + Clear::Empty => *value = json!([]), + } +} + +/// Turns a feature off in a row's build part. +pub fn switch_off(build: &mut Value, feature: &RowFeature) { + for off in feature.off { + match *off { + Off::Flag(path) => { + if let Some(flag) = at_mut(build, path) { + *flag = json!(false); + } + } + Off::Part(name) => build[name] = Value::Null, + } + } +} + +/// Turns off the feature of that name, through every part of the row it has, and clears each of +/// its values that a path names, so a feature that is off holds nothing of another build. +pub fn turn_off(build: &mut Value, name: &str) { + for feature in ROW_FEATURES.iter().filter(|feature| feature.name == name) { + switch_off(build, feature); + for path in feature.paths.iter().filter(|path| !path.ends_with('.')) { + clear(build, path, feature.clear); + } + } +} + +/// How a value of a feature that is off is cleared where it sits: an address inside a list of +/// signatures becomes zero with the rest of that list, since a list holds no single address. +pub fn clear_value(build: &mut Value, path: &str, feature: &RowFeature) { + let how = match feature.clear { + Clear::Absent if path.contains('[') => Clear::Zero, + how => how, + }; + clear(build, path, how); +} + +/// The key of a row that names the features it has off and the values that turned them off. The +/// runtime reads only `frame` and `build`, so it changes nothing in a browser; the feature gate +/// reads it. +pub const SWITCHED_OFF: &str = "switched_off"; + +/// The feature checks a row's switched-off features excuse, by the names the row gives. +pub fn excused_checks(row: &Value) -> Vec<(&'static str, &'static str)> { + let names: Vec<&str> = row[SWITCHED_OFF] + .as_array() + .map_or(&[][..], Vec::as_slice) + .iter() + .filter_map(|entry| entry["feature"].as_str()) + .collect(); + let mut out = Vec::new(); + for feature in ROW_FEATURES + .iter() + .filter(|feature| names.contains(&feature.name)) + { + for check in feature.checks { + if !out.contains(&(feature.name, *check)) { + out.push((feature.name, *check)); + } + } + } + out +} + +/// The switched-off feature that excuses a feature check, if one does. +pub fn excuses(excused: &[(&'static str, &'static str)], check: &str) -> Option<&'static str> { + excused + .iter() + .find(|(_, rule)| { + if rule.ends_with('-') { + check.starts_with(rule) + } else { + check == *rule + } + }) + .map(|(name, _)| *name) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn nfr6_every_row_value_of_a_feature_has_one_switch() { + assert_eq!(feature_of("split.view_size").unwrap().name, "split view"); + assert_eq!(feature_of("toolbar.dev.colours[3][0]").unwrap().name, "developer colours"); + assert_eq!(feature_of("layout.frame_ids[1]").unwrap().name, "frame colour"); + assert_eq!(feature_of("omnibox.match_size").unwrap().name, "address suggestions"); + // The frame and the page area layout have no switch: the window cannot do without them. + for core in ["layout.view_bounds_offset", "side_panel.toolbar"] { + assert!(feature_of(core).is_none(), "{core}"); + assert!(!is_reflected(core), "{core}"); + } + // The window's views and widget have no switch either; the runtime finds them itself. + for reflected in REFLECTED { + assert!(feature_of(reflected).is_none(), "{reflected}"); + assert!(is_reflected(reflected), "{reflected}"); + } + // A value that only looks like a section's name is not in it. + assert!(feature_of("splitter.width").is_none()); + } + + #[test] + fn nfr6_a_reflected_value_is_left_out_of_the_row() { + let mut build = json!({ + "layout": {"frame_browser_window_offset": "0x340", "view_bounds_offset": "0x1c8"}, + "toolbar": {"get_widget_slot": "0xb"}, + }); + leave_out(&mut build, "layout.frame_browser_window_offset"); + leave_out(&mut build, "toolbar.get_widget_slot"); + assert_eq!( + build, + json!({"layout": {"view_bounds_offset": "0x1c8"}, "toolbar": {}}) + ); + } + + #[test] + fn nfr6_a_value_the_runtime_does_not_read_is_not_a_value_of_the_build() { + let build = json!({ + "tabs": {"tab_at": "0x326a00", "tab_collection": "0x30"}, + "vertical": {"window_feature": "0x7fff0000", "window_feature_callers": ["0x0", "0x0"]}, + }); + assert!(unused(&build, "tabs.tab_collection").is_some()); + assert!(unused(&build, "vertical.window_feature_callers[1]").is_some()); + let build = json!({ + "tabs": {"tab_at": "0x7fff0000"}, + "vertical": {"window_feature": "0x1234560"}, + }); + assert!(unused(&build, "tabs.tab_collection").is_none()); + assert!(unused(&build, "vertical.toolbar_in_view").is_none()); + } + + #[test] + fn nfr6_a_switched_off_feature_excuses_only_its_own_checks() { + let row = json!({"switched_off": [{"feature": "split view"}, {"feature": "developer colours"}]}); + let excused = excused_checks(&row); + assert_eq!(excuses(&excused, "split-grid-4"), Some("split view")); + assert_eq!(excuses(&excused, "split"), Some("split view")); + assert_eq!(excuses(&excused, "dev-row"), Some("developer colours")); + assert_eq!(excuses(&excused, "sidebar"), None); + assert_eq!(excuses(&excused, "bar-layout"), None); + assert!(excused_checks(&json!({})).is_empty()); + } +} diff --git a/rivet/platform/windows/src/row_port.rs b/rivet/platform/windows/src/row_port.rs index 33b0dbd..77306fa 100644 --- a/rivet/platform/windows/src/row_port.rs +++ b/rivet/platform/windows/src/row_port.rs @@ -1,45 +1,31 @@ //! Makes the supported-build row of a new browser build from the row of a prior build. //! //! Chromium's source changes little between two builds of a milestone, but every address moves. -//! So each address of the prior row is named through the prior build's public symbols, the same -//! decorated names are looked up in the new build's symbols, and the instruction bytes that guard -//! each hook are read again from the new module. Values that are not addresses (member offsets, +//! Each address of the prior row is found again in the new module by its fingerprint, the first +//! instructions of the function without their displacements, or by a slot of a class table that +//! holds such a function. What a fingerprint does not pin down is settled by `row_resolve`, which +//! takes a place only where two independent methods agree. The instruction bytes that guard each +//! hook are then read again from the new module. Values that are not addresses (member offsets, //! table slots, object sizes) are read again from the new module where `row_recipe` has a recipe -//! for them, and a recipe that fails stops the row. The rest are carried over. They have no public -//! symbols, so a row that crosses a milestone lists each of them and is never published without a -//! person's review, and every row must still pass on the real browser. +//! for them, or from the instructions that use them. No browser's debug symbols are read: the +//! verified prior row, its fingerprints and the two modules are all the generator has. //! //! A row is plain data here, in the form the runtime writes: a `usize` is a `0x` string, and bytes -//! are bare hex. A value this code cannot explain stops the row: it goes to review rather than to -//! people's browsers, and the runtime's own prologue checks stand behind that. +//! are bare hex. A value this code cannot explain stops the row or turns its feature off: it never +//! reaches people's browsers, and the runtime's own prologue checks stand behind that. -use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; +use std::collections::{BTreeMap, BTreeSet, HashMap}; use serde::Serialize; use serde_json::{json, Value}; use crate::chrome_build::{hex, PeFile}; +use crate::row_resolve; /// The lowest value taken for an address. Offsets and slots lie below it. const LOWEST_ADDRESS: u64 = 0x10_0000; /// The address a build records for what it does not have. const ABSENT: u64 = 0x7FFF_0000; -/// How far below an address its naming symbol may lie: a table slot or an instruction in a function. -const NEAREST_SYMBOL: u32 = 0x2000; - -/// One symbol: its address in the module and its decorated name. -pub type Symbol = (u32, String); - -/// Removes what the linker adds to a decorated name, which differs from build to build. -pub fn stable_name(name: &str) -> &str { - let name = name.trim_end_matches('\0'); - let name = match name.strip_prefix(".weak.") { - Some(rest) => rest.split(".default.").next().unwrap_or(rest), - None => name, - }; - name.split(".llvm.").next().unwrap_or(name) -} - /// What the generator found, for the person or job that decides what to do with the row. #[derive(Debug, Default, Serialize)] pub struct PortReport { @@ -52,10 +38,6 @@ pub struct PortReport { pub review: Vec, /// Addresses with several possible new places, settled by an independent check. pub settled: Vec, - /// Addresses inside a symbol rather than at its start, moved by the same distance. - pub interior: Vec, - /// Addresses found through private symbols, since no public one covers them. - pub private: Vec, pub bytes_changed: Vec, pub slots_moved: Vec, /// Values that are not addresses, read again from the new module by their recipes. @@ -64,6 +46,34 @@ pub struct PortReport { pub carried: Vec, /// Optional parts the new build could not supply. The runtime does without them. pub left_out: Vec, + /// Addresses a fingerprint found in exactly one place of the new build. + pub fingerprinted: Vec, + /// Values that are Rivet's settings rather than facts about the build, kept from the prior row. + pub settings: Vec, + /// Features this row turns off, each with the values that could not be read for it. The row + /// clears those values, so none of the prior build reaches a browser. + pub switched_off: Vec, + /// Values the runtime does not read in this row, written as zero, with the reason. + pub unused: Vec, + /// Values no instruction of the new build gave that the runtime finds by reflection, so the + /// row leaves them out instead of carrying them. + pub reflected: Vec, + /// Addresses no method placed in the new build that only features this row turns off hold, + /// with the values that held them. The row clears those values like the feature's others. + pub unplaced: Vec, + /// Whether every carried value must be settled: the milestone changed, or values were read. + #[serde(skip)] + must_check: bool, + /// The milestone change, when there is one and nothing read the carried values. + #[serde(skip)] + milestone: Option, + /// Why each value that was tried could not be read. + #[serde(skip)] + unread: BTreeMap, + /// Values of features the row turns off whatever the milestone, with the reason: an address + /// that has no place in the new build, or a recipe that failed. + #[serde(skip)] + off_values: BTreeMap, } fn is_address(value: u64) -> bool { @@ -109,64 +119,48 @@ pub fn addresses_of(row: &Value) -> BTreeSet { found } -/// Names each address: all the names of the nearest symbol at or below it, which are many where -/// the linker folded identical functions, and how far above that symbol it lies. -pub fn name_addresses( - targets: &BTreeSet, - sorted_symbols: &[Symbol], -) -> BTreeMap)> { - let mut named = BTreeMap::new(); - for target in targets { - let end = sorted_symbols.partition_point(|(address, _)| address <= target); - let Some((nearest, _)) = end.checked_sub(1).map(|at| &sorted_symbols[at]) else { - continue; - }; - if target - nearest > NEAREST_SYMBOL { - continue; - } - let start = sorted_symbols.partition_point(|(address, _)| address < nearest); - let names = sorted_symbols[start..end] - .iter() - .map(|(_, name)| stable_name(name).to_owned()) - .collect(); - named.insert(*target, (target - nearest, names)); - } - named -} - -/// The names to keep from the new build's symbols. -pub fn wanted_names(named: &BTreeMap)>) -> HashSet { - named - .values() - .flat_map(|(_, names)| names.iter().cloned()) - .collect() -} - -/// Each place an address may have moved to, with how many of its names say so, most votes first. -fn candidates( - names: &[String], - distance: u32, - new: &HashMap>, -) -> Vec<(u32, usize)> { - let mut votes: BTreeMap = BTreeMap::new(); - for name in names { - for address in new.get(name).map_or(&[][..], Vec::as_slice) { - *votes.entry(address + distance).or_default() += 1; - } - } - let mut votes: Vec<_> = votes.into_iter().collect(); - votes.sort_by_key(|(address, count)| (std::cmp::Reverse(*count), *address)); - votes -} - -/// A function's first instructions, read as far as this needs them. +/// A function's instructions, read as far as this needs them. pub mod x64 { - /// One decoded instruction: its length and where its RIP-relative displacement or relative - /// branch target is, if it has one. + /// One decoded instruction: its length, where its RIP-relative displacement or relative branch + /// target is, where a displacement from a register is, as in `[rcx + 0x558]`, and where its + /// immediate value is. Each place is `(offset, size)` within the instruction. #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub struct Instruction { pub length: usize, pub relative: Option<(usize, usize)>, + pub displacement: Option<(usize, usize)>, + pub immediate: Option<(usize, usize)>, + } + + impl Instruction { + fn plain(length: usize) -> Self { + Self { + length, + relative: None, + displacement: None, + immediate: None, + } + } + + /// An instruction whose last `size` bytes, from `at`, are an immediate value. + fn with_immediate(at: usize, size: usize) -> Self { + Self { + length: at + size, + relative: None, + displacement: None, + immediate: Some((at, size)), + } + } + + /// A branch whose last `size` bytes, from `at`, are the distance to its target. + fn branch(at: usize, size: usize) -> Self { + Self { + length: at + size, + relative: Some((at, size)), + displacement: None, + immediate: None, + } + } } fn modrm(bytes: &[u8], at: usize, immediate: usize) -> Option { @@ -174,10 +168,12 @@ pub mod x64 { let (mode, rm) = (byte >> 6, byte & 7); let mut length = at + 1; let mut relative = None; + let mut displacement = None; if mode != 3 && rm == 4 { let sib = *bytes.get(length)?; length += 1; if mode == 0 && sib & 7 == 5 { + displacement = Some((length, 4)); length += 4; } } @@ -186,19 +182,39 @@ pub mod x64 { relative = Some((length, 4)); length += 4; } - 1 => length += 1, - 2 => length += 4, + 1 => { + displacement = Some((length, 1)); + length += 1; + } + 2 => { + displacement = Some((length, 4)); + length += 4; + } _ => {} } Some(Instruction { length: length + immediate, relative, + displacement, + immediate: (immediate > 0).then_some((length, immediate)), }) } /// Decodes the instruction at the start of `bytes`, or `None` for one this does not know. The /// list covers what function starts are made of, and anything else goes to a person. pub fn decode(bytes: &[u8]) -> Option { + decode_with(bytes, false) + } + + /// Decodes as `decode` does, and also the instructions met inside a function body: lock and + /// segment prefixes, string and x87 instructions, VEX encoded vector instructions and more of + /// the two and three byte opcodes. It reads bodies to follow calls and operands, and never + /// judges a guard, so what a hook may copy stays as `decode` says. + pub fn decode_body(bytes: &[u8]) -> Option { + decode_with(bytes, true) + } + + fn decode_with(bytes: &[u8], body: bool) -> Option { let mut at = 0; let mut wide_operand = false; let mut rex_w = false; @@ -206,6 +222,7 @@ pub mod x64 { match *bytes.get(at)? { 0x66 => wide_operand = true, 0xF2 | 0xF3 => {} + 0xF0 | 0x26 | 0x2E | 0x36 | 0x3E | 0x64 | 0x65 if body => {} _ => break, } at += 1; @@ -217,15 +234,9 @@ pub mod x64 { let opcode = *bytes.get(at)?; at += 1; let word = if wide_operand { 2 } else { 4 }; - let plain = |length| { - Some(Instruction { - length, - relative: None, - }) - }; match opcode { - 0x50..=0x5F | 0x90 | 0xC3 | 0xCC | 0x99 => plain(at), - 0x98 => plain(at), + 0x50..=0x5F | 0x90 | 0xC3 | 0xCC | 0x99 => Some(Instruction::plain(at)), + 0x98 => Some(Instruction::plain(at)), 0x00..=0x03 | 0x08..=0x0B | 0x10..=0x13 @@ -242,10 +253,15 @@ pub mod x64 { | 0xFE | 0xFF => modrm(bytes, at, 0), 0x04 | 0x0C | 0x14 | 0x1C | 0x24 | 0x2C | 0x34 | 0x3C | 0x6A | 0xA8 | 0xB0..=0xB7 => { - plain(at + 1) + Some(Instruction::with_immediate(at, 1)) + } + 0x05 | 0x0D | 0x15 | 0x1D | 0x25 | 0x2D | 0x35 | 0x3D | 0x68 | 0xA9 => { + Some(Instruction::with_immediate(at, word)) } - 0x05 | 0x0D | 0x15 | 0x1D | 0x25 | 0x2D | 0x35 | 0x3D | 0x68 | 0xA9 => plain(at + word), - 0xB8..=0xBF => plain(at + if rex_w { 8 } else { word }), + 0xB8..=0xBF => Some(Instruction::with_immediate( + at, + if rex_w { 8 } else { word }, + )), 0x80 | 0x83 | 0xC0 | 0xC1 | 0xC6 | 0x6B => modrm(bytes, at, 1), 0x81 | 0xC7 | 0x69 => modrm(bytes, at, word), 0xF6 => { @@ -260,22 +276,13 @@ pub mod x64 { }; modrm(bytes, at, immediate) } - 0x70..=0x7F | 0xEB => Some(Instruction { - length: at + 1, - relative: Some((at, 1)), - }), - 0xE8 | 0xE9 => Some(Instruction { - length: at + 4, - relative: Some((at, 4)), - }), + 0x70..=0x7F | 0xEB => Some(Instruction::branch(at, 1)), + 0xE8 | 0xE9 => Some(Instruction::branch(at, 4)), 0x0F => { let second = *bytes.get(at)?; at += 1; match second { - 0x80..=0x8F => Some(Instruction { - length: at + 4, - relative: Some((at, 4)), - }), + 0x80..=0x8F => Some(Instruction::branch(at, 4)), 0x10..=0x17 | 0x1E | 0x1F @@ -291,9 +298,81 @@ pub mod x64 { | 0xBF | 0xD6 | 0xEF => modrm(bytes, at, 0), + _ if body => body_two_byte(bytes, at, second), _ => None, } } + _ if body => body_one_byte(bytes, at, opcode), + _ => None, + } + } + + /// One byte opcodes that only a function body holds. `at` is just past the opcode. + fn body_one_byte(bytes: &[u8], at: usize, opcode: u8) -> Option { + match opcode { + 0x91..=0x97 | 0x9C..=0x9F | 0xA4..=0xA7 | 0xAA..=0xAF | 0xC9 | 0xF4 | 0xF5 => { + Some(Instruction::plain(at)) + } + 0xF8..=0xFD => Some(Instruction::plain(at)), + 0xC2 => Some(Instruction::with_immediate(at, 2)), + 0xCD => Some(Instruction::with_immediate(at, 1)), + 0xD8..=0xDF => modrm(bytes, at, 0), + 0xE0..=0xE3 => Some(Instruction::branch(at, 1)), + // Two byte VEX: one payload byte, then an opcode of the 0F map. + 0xC5 => { + let opcode = *bytes.get(at + 1)?; + modrm(bytes, at + 2, vex_immediate(1, opcode)) + } + // Three byte VEX: the map in the first payload byte, then one more payload byte. + 0xC4 => { + let map = bytes.get(at)? & 0x1F; + let opcode = *bytes.get(at + 2)?; + modrm(bytes, at + 3, vex_immediate(map, opcode)) + } + _ => None, + } + } + + /// Whether a VEX instruction of `map` ends in an immediate byte. + fn vex_immediate(map: u8, opcode: u8) -> usize { + match map { + 3 => 1, + 1 => usize::from(matches!(opcode, 0x70..=0x73 | 0xC2 | 0xC4..=0xC6)), + _ => 0, + } + } + + /// Two byte opcodes that only a function body holds. `at` is just past the second byte. + fn body_two_byte(bytes: &[u8], at: usize, second: u8) -> Option { + match second { + 0x0B | 0x31 | 0x77 | 0xA2 | 0xC8..=0xCF => Some(Instruction::plain(at)), + 0x0D + | 0x18..=0x1D + | 0x74..=0x76 + | 0x7C + | 0x7D + | 0xA3 + | 0xA5 + | 0xAB + | 0xAD + | 0xAE + | 0xB0 + | 0xB1 + | 0xB3 + | 0xB8 + | 0xBB + | 0xBC + | 0xBD + | 0xC0 + | 0xC1 + | 0xC3 + | 0xC7 + | 0xD0..=0xD5 + | 0xD7..=0xEE + | 0xF0..=0xFE => modrm(bytes, at, 0), + 0x70..=0x73 | 0xA4 | 0xAC | 0xBA | 0xC2 | 0xC4..=0xC6 => modrm(bytes, at, 1), + 0x38 => modrm(bytes, at + 1, 0), + 0x3A => modrm(bytes, at + 1, 1), _ => None, } } @@ -350,6 +429,23 @@ pub mod x64 { } Some(false) } + + /// An instruction's bytes with its relative value, displacement and immediate cleared: what + /// stays the same when code moves and members, slots and constants change. + pub fn skeleton(bytes: &[u8], instruction: &Instruction) -> Vec { + let mut out = bytes[..instruction.length.min(bytes.len())].to_vec(); + let fields = [ + instruction.relative, + instruction.displacement, + instruction.immediate, + ]; + for (offset, size) in fields.into_iter().flatten() { + for index in offset..(offset + size).min(out.len()) { + out[index] = 0; + } + } + out + } } /// A byte field with no address field of the same name, and where it was read from. @@ -455,51 +551,8 @@ fn usize_at(row: &Value, path: &str) -> Option { read_address(at_path(row, path)) } -/// Everything the new build supplies. -pub struct NewBuild<'a> { - pub version: &'a str, - pub pe: &'a PeFile<'a>, - pub symbols: &'a HashMap>, - /// Where each address with no public name went, found through private symbols. - pub private: &'a BTreeMap, -} - -/// Settles an address the names disagree on. Folded functions share names across builds, so the -/// names alone can point at a function the new build no longer folds with the wanted one. The -/// instructions that row checks for it say which of the places is meant, and a table slot that -/// must hold it says the same. -fn settle(target: u32, choices: &[(u32, usize)], build: &Value, pe: &PeFile) -> Option { - let mut remaining: Vec = choices.iter().map(|(address, _)| *address).collect(); - let mut guards = vec![]; - collect_guards(build, &mut guards); - for (address, bytes) in guards { - if address != target { - continue; - } - let wanted = x64::shape(&bytes)?; - remaining.retain(|place| { - pe.read(*place, bytes.len()) - .and_then(x64::shape) - .is_some_and(|shape| shape == wanted) - }); - } - if remaining.len() == 1 { - return Some(remaining[0]); - } - // The linker folds functions with the same code under one name, and a new build can fold them - // into two groups. Short functions read to their end show that any of the places does the same. - let code: Vec<_> = remaining - .iter() - .map(|place| whole_function(pe, *place)) - .collect(); - let first = code.first()?.as_ref()?; - code.iter() - .all(|other| other.as_ref() == Some(first)) - .then(|| remaining[0]) -} - /// The bytes of a function short enough to read to its end: its first `ret` with no branch before. -fn whole_function(pe: &PeFile, address: u32) -> Option> { +pub(crate) fn whole_function(pe: &PeFile, address: u32) -> Option> { let bytes = pe.read(address, 48)?; let mut at = 0; while at < 32 { @@ -515,43 +568,6 @@ fn whole_function(pe: &PeFile, address: u32) -> Option> { None } -/// Every `(address, bytes)` guard of a row. -fn collect_guards(value: &Value, guards: &mut Vec<(u32, Vec)>) { - match value { - Value::Object(object) => { - for (key, item) in object { - if let Some(bytes) = item.as_str().filter(|_| is_byte_field(key)).and_then(hex) { - let base = byte_field_address(key) - .map(|(base, _)| base.to_owned()) - .unwrap_or_else(|| { - key.trim_end_matches("_prologue") - .trim_end_matches("_bytes") - .to_owned() - }); - if let Some(address) = object - .get(&base) - .and_then(read_address) - .filter(|value| is_address(*value)) - { - guards.push((address as u32, bytes)); - } - } - collect_guards(item, guards); - } - } - Value::Array(items) => { - if let [address, Value::String(bytes)] = items.as_slice() { - if let (Some(address), Some(bytes)) = (read_address(address), hex(bytes)) { - guards.push((address as u32, bytes)); - return; - } - } - items.iter().for_each(|item| collect_guards(item, guards)); - } - _ => {} - } -} - fn is_byte_field(key: &str) -> bool { key.ends_with("_prologue") || key.ends_with("_bytes") || byte_field_address(key).is_some() } @@ -801,53 +817,108 @@ fn read_rounded_corners(build: &mut Value, pe: &PeFile, report: &mut PortReport) build["split"]["set_rounded_corners_parity"] = write_address(start + 13); } -/// Addresses read from instructions above, which no symbol names. +/// Addresses read from instructions above, which no fingerprint finds. const READ_FROM_CODE: &[&str] = &[ "split.set_rounded_corners_changed", "split.set_rounded_corners_parity", ]; -fn read_from_code_targets(build: &Value) -> Vec { - READ_FROM_CODE - .iter() - .filter_map(|path| usize_at(build, path)) - .map(|value| value as u32) - .collect() -} - -/// The code addresses a row holds that no public symbol starts at, which need private symbols. -pub fn unnamed_code( - prior: &Value, - named: &BTreeMap)>, - pe_of_new: &PeFile, -) -> Vec { - let read = read_from_code_targets(&prior["build"]); - let mut out = vec![]; - for (target, (distance, _)) in named { - // The prior module is gone by now. Code and data keep their sections from build to build. - if *distance != 0 && !read.contains(target) && pe_of_new.is_code(*target) { - out.push(*target); +/// Settles each value no recipe or instruction read from the new build. A value the runtime finds +/// by reflection is left out of the row. A value the runtime does not read in this row is written +/// as zero. A value of a feature the runtime can do without turns +/// that feature off for this build and is cleared, so the rest of the row is not held back by it. +/// Any other value stops the row with its reason, since no feature may run on a value of another +/// build. +fn settle_carried(row: &mut Value, report: &mut PortReport) { + use crate::row_features::{self, Clear, RowFeature}; + report + .derived + .extend(crate::row_recipe::follow(&mut row["build"])); + let mut off: BTreeMap<&'static str, (&'static RowFeature, Vec)> = BTreeMap::new(); + for (path, reason) in std::mem::take(&mut report.off_values) { + if let Some(feature) = row_features::feature_of(&path) { + row_features::clear_value(&mut row["build"], &path, feature); + report.unread.insert(path.clone(), reason); + off.entry(feature.name) + .or_insert((feature, Vec::new())) + .1 + .push(path); } } - out -} - -/// Makes the new row. `prior` is `{frame, build}`, as the runtime writes it. -pub fn port_row( - prior: &Value, - named: &BTreeMap)>, - new: &NewBuild, -) -> (Value, PortReport) { - let (row, mut report) = begin_row(prior, new.version); - let mut moved: BTreeMap = BTreeMap::new(); - let targets = addresses_of(&row); - report.addresses = targets.len(); - port_by_name(prior, &row, named, new, &targets, &mut moved, &mut report); - finish_row(row, &moved, new.pe, report) + let must_check = report.must_check; + let carried = if must_check { + std::mem::take(&mut report.carried) + } else { + Vec::new() + }; + let mut left = Vec::new(); + for path in carried { + if row_features::is_reflected(&path) { + row_features::leave_out(&mut row["build"], &path); + report.unread.remove(&path); + report + .reflected + .push(format!("{path}: left out, the runtime finds it by reflection")); + } else if let Some(why) = row_features::unused(&row["build"], &path) { + row_features::clear(&mut row["build"], &path, Clear::Zero); + report.unused.push(format!("{path}: {why}")); + report.unread.remove(&path); + } else if let Some(feature) = row_features::feature_of(&path) { + row_features::clear(&mut row["build"], &path, feature.clear); + off.entry(feature.name) + .or_insert((feature, Vec::new())) + .1 + .push(path); + } else { + left.push(path); + } + } + let mut entries = Vec::new(); + for (name, (_, paths)) in off { + row_features::turn_off(&mut row["build"], name); + let reasons: Vec = paths + .iter() + .map(|path| match report.unread.remove(path) { + Some(reason) => format!("{path} {reason}"), + None => format!("{path} has no recipe"), + }) + .collect(); + report + .switched_off + .push(format!("{name} is off: {}", reasons.join("; "))); + entries.push(json!({"feature": name, "values": paths})); + } + if !entries.is_empty() { + row[row_features::SWITCHED_OFF] = json!(entries); + } + if !must_check { + return; + } + let mut unchecked = Vec::new(); + for path in &left { + match report.unread.remove(path) { + Some(reason) => report.review.push(format!("{path} {reason}")), + None => unchecked.push(path.clone()), + } + } + if !unchecked.is_empty() { + let head = report + .milestone + .clone() + .unwrap_or_else(|| "values were not read".to_owned()); + report.review.push(format!( + "{head}: {} values have no recipe and were carried over unchecked: {}", + unchecked.len(), + unchecked.join(", ") + )); + } + report.carried = left; } -/// A copy of the prior row with the new version, and frame addresses in the `0x` form. -fn begin_row(prior: &Value, version: &str) -> (Value, PortReport) { +/// A copy of the prior row with the new version, and frame addresses in the `0x` form. `carries` +/// says the values with no recipe stay as the prior row has them, so a new milestone stops the +/// row; without it they are read from the new build later, each with its own review line. +fn begin_row(prior: &Value, version: &str, carries: bool) -> (Value, PortReport) { let mut row = prior.clone(); let prior_version = prior["frame"]["version"] .as_str() @@ -871,12 +942,14 @@ fn begin_row(prior: &Value, version: &str) -> (Value, PortReport) { .review .push(format!("{} is not a four part version", version)); } + write_command_ids(&mut row, version, &mut report); report.carried = crate::row_recipe::without_recipe(&prior["build"]); - if prior_version.split('.').next() != version.split('.').next() { - report.review.push(format!( - "the milestone changed from {prior_version} to {version}: {} values have no recipe and were carried over unchecked: {}", - report.carried.len(), - report.carried.join(", ") + report.settings = crate::row_recipe::settings(&prior["build"]); + let milestone_changed = prior_version.split('.').next() != version.split('.').next(); + report.must_check = !carries || milestone_changed; + if carries && milestone_changed { + report.milestone = Some(format!( + "the milestone changed from {prior_version} to {version}" )); } row["build"]["version"] = json!(parts); @@ -887,70 +960,65 @@ fn begin_row(prior: &Value, version: &str) -> (Value, PortReport) { (row, report) } -/// Finds each address of a row through the public names of both builds. -fn port_by_name( - prior: &Value, - row: &Value, - named: &BTreeMap)>, - new: &NewBuild, - targets: &BTreeSet, - moved: &mut BTreeMap, - report: &mut PortReport, -) { - for target in targets { - let Some((distance, names)) = named.get(target) else { - continue; - }; - // No public name covers this function: the distance from the nearest one means nothing, - // since another build lays those functions out differently. - if *distance != 0 - && new.pe.is_code(*target) - && !read_from_code_targets(&prior["build"]).contains(target) - { - match new.private.get(target) { - Some(place) => { - moved.insert(*target, *place); - report.private.push(format!("{target:#x} -> {place:#x}")); - } - None => report.review.push(format!( - "{target:#x} lies in a function with no name in both builds" - )), - } +/// Writes Chromium's public command ids into a row from the table read from Chromium's source, +/// for runtimes that still read them from the row. They are never carried from the prior row: a +/// milestone the table does not know gets zero, which offers no command. A prior row with zero +/// never had the developer bar checked, so it keeps zero. +fn write_command_ids(row: &mut Value, version: &str, report: &mut PortReport) { + let browser = row["build"]["browser"].as_str().unwrap_or_default(); + // Chrome, Edge and Brave carry their Chromium milestone as the first part of their version. + let milestone = matches!(browser, "Google Chrome" | "Microsoft Edge" | "Brave") + .then(|| version.split('.').next()?.parse::().ok()) + .flatten(); + let ids = milestone.and_then(crate::command_ids::dev_tools); + let Some(dev) = row["build"]["toolbar"]["dev"].as_object_mut() else { + return; + }; + for (key, id) in [ + ("inspect", ids.map(|ids| ids.0)), + ("console", ids.map(|ids| ids.1)), + ] { + let Some(prior) = dev.get(key).and_then(Value::as_u64) else { continue; - } - let choices = candidates(names, *distance, new.symbols); - let place = match choices.as_slice() { - [] => None, - [(only, _)] => Some(*only), - many => { - let settled = settle(*target, many, &row["build"], new.pe); - let place = settled - .or_else(|| by_slot(*target, many, &row["build"], new.pe, named, new.symbols)); - match place { - Some(place) => report - .settled - .push(format!("{target:#x} -> {place:#x} of {many:x?}")), - None => { - // Most folded names still fold together, so the largest group is the likely - // place, and a person looks at it. - report.review.push(format!( - "{target:#x} has several places: {many:x?}, first of {:?}", - &names[..names.len().min(3)] - )); - } - } - place.or(Some(many[0].0)) - } }; - if let Some(place) = place { - if *distance != 0 { - report.interior.push(format!( - "{target:#x} = {}+{distance:#x} -> {place:#x}", - names[0] - )); - } - moved.insert(*target, place); - } + let value = if prior == 0 { 0 } else { id.unwrap_or(0) }; + dev.insert(key.to_owned(), json!(value)); + report.derived.push(match (prior == 0, id) { + (true, _) => format!("toolbar.dev.{key}: 0, the prior row offers no developer bar"), + (false, Some(id)) => format!( + "toolbar.dev.{key}: {id}, from chrome_command_ids.h at Chromium {}", + milestone.unwrap_or_default() + ), + (false, None) => format!( + "toolbar.dev.{key}: 0, no command id is read for this build's Chromium milestone" + ), + }); + } +} + +/// The prior row's functions that a browser command runs first, with the command's public id: +/// `chrome::NewTab`, the first call of the `IDC_NEW_TAB` case of the browser's command switch. The +/// id must be the same at the prior and the new build's Chromium milestone, which Chrome, Edge and +/// Brave carry as the first part of their version. +fn command_targets(prior: &Value, version: &str) -> BTreeMap { + let browser = prior["build"]["browser"].as_str().unwrap_or_default(); + if !matches!(browser, "Google Chrome" | "Microsoft Edge" | "Brave") { + return BTreeMap::new(); + } + let milestone = |version: &str| version.split('.').next()?.parse::().ok(); + let prior_version = prior["frame"]["version"].as_str().unwrap_or_default(); + let ids = ( + milestone(prior_version).and_then(crate::command_ids::new_tab), + milestone(version).and_then(crate::command_ids::new_tab), + ); + let (Some(id), Some(same)) = ids else { + return BTreeMap::new(); + }; + let address = + read_address(&prior["build"]["tabs"]["new_tab"]).filter(|value| is_address(*value)); + match address { + Some(address) if id == same => BTreeMap::from([(address as u32, id as i64)]), + _ => BTreeMap::new(), } } @@ -1000,12 +1068,20 @@ fn finish_row( // Last, since the recipes read the addresses of the new build. let derived = crate::row_recipe::derive_row(&mut row["build"], pe); report.derived.extend(derived.read); - report.review.extend( - derived - .failed - .into_iter() - .map(|reason| format!("a value could not be read from the new build: {reason}")), - ); + for failure in derived.failed { + // A value of a feature the runtime can do without turns that feature off instead. + let path = failure.split(": ").next().unwrap_or_default(); + if crate::row_features::feature_of(path).is_some() { + report.off_values.insert( + path.to_owned(), + format!("could not be read from the new build: {failure}"), + ); + } else { + report + .review + .push(format!("a value could not be read from the new build: {failure}")); + } + } (row, report) } @@ -1037,7 +1113,7 @@ fn fingerprint_targets(row: &Value) -> BTreeSet { } /// The start of a function with its displacements cleared, cut at an instruction end. -fn code_print(pe: &PeFile, address: u32) -> Option> { +pub(crate) fn code_print(pe: &PeFile, address: u32) -> Option> { if !pe.is_code(address) { return None; } @@ -1067,6 +1143,17 @@ fn code_print(pe: &PeFile, address: u32) -> Option> { /// known by a slot that holds one of those functions. An address that is neither cannot be found, /// so then the row gets no fingerprints at all. pub fn add_fingerprints(row: &mut Value, pe: &PeFile) -> Result<(), String> { + let (prints, missing) = fingerprints_of(row, pe); + if !missing.is_empty() { + return Err(format!("no fingerprint for {}", missing.join(", "))); + } + row["fingerprints"] = Value::Object(prints); + Ok(()) +} + +/// The fingerprint of each address of a row that has one, read from the row's own module, and the +/// addresses that have none. A row made from them leaves those addresses unvouched. +pub fn fingerprints_of(row: &Value, pe: &PeFile) -> (serde_json::Map, Vec) { let targets = fingerprint_targets(row); let mut prints = serde_json::Map::new(); let mut missing = Vec::new(); @@ -1093,15 +1180,11 @@ pub fn add_fingerprints(row: &mut Value, pe: &PeFile) -> Result<(), String> { None => missing.push(key), } } - if !missing.is_empty() { - return Err(format!("no fingerprint for {}", missing.join(", "))); - } - row["fingerprints"] = Value::Object(prints); - Ok(()) + (prints, missing) } /// Every place each function fingerprint matches, in one pass over the code. -fn find_prints(pe: &PeFile, prints: &BTreeMap>) -> BTreeMap> { +pub(crate) fn find_prints(pe: &PeFile, prints: &BTreeMap>) -> BTreeMap> { let mut by_anchor: HashMap<[u8; ANCHOR], Vec> = HashMap::new(); let mut first = [false; 256]; for (target, shape) in prints { @@ -1137,7 +1220,7 @@ fn find_prints(pe: &PeFile, prints: &BTreeMap>) -> BTreeMap Vec { +pub(crate) fn find_table(pe: &PeFile, slot: u32, function: u32) -> Vec { let value = (pe.image_base() + u64::from(function)).to_le_bytes(); let mut found = Vec::new(); for (base, bytes, _) in pe.sections() { @@ -1153,7 +1236,7 @@ fn find_table(pe: &PeFile, slot: u32, function: u32) -> Vec { } /// Makes the row of a build that has no row from the fingerprints of a verified row of the same -/// milestone, without symbols. Each address must match exactly one place, no two addresses may +/// milestone, from the new module alone. Each address must match exactly one place, no two addresses may /// share one, and every function start and table slot is then checked again in the new module the /// way a ported row is. Any doubt is an error with its reasons, and then the runtime changes /// nothing in the browser. @@ -1161,11 +1244,60 @@ pub fn find_row(prior: &Value, version: &str, pe: &PeFile) -> Result, + prior_pe: Option<&PeFile>, + classes: &BTreeMap, + version: &str, + pe: &PeFile, +) -> (Value, PortReport) { + let (mut row, mut report) = begin_row(prior, version, prior_pe.is_none()); + let prior_starts = prior_pe.map(PeFile::function_starts).unwrap_or_default(); let targets = fingerprint_targets(prior); report.addresses = targets.len(); let mut functions = BTreeMap::new(); let mut tables = BTreeMap::new(); + // Each address its fingerprint did not pin down, with its review line, waits for the methods + // that read the prior build next to the new one. + let mut wanted: BTreeMap = BTreeMap::new(); + let commands = command_targets(prior, version); + let want = |target: u32, kind: row_resolve::Kind, candidates: Option>| { + row_resolve::Wanted { + kind, + candidates, + class: (kind == row_resolve::Kind::Table) + .then(|| classes.get(&target).cloned()) + .flatten(), + command: (kind == row_resolve::Kind::Function) + .then(|| commands.get(&target).copied()) + .flatten(), + loose: Vec::new(), + } + }; for target in &targets { match stored.get(&format!("{target:#x}")) { Some(Value::String(text)) => match hex(text).filter(|shape| shape.len() >= ANCHOR) { @@ -1190,120 +1322,439 @@ pub fn find_row(prior: &Value, version: &str, pe: &PeFile) -> Result report.review.push(format!("{target:#x}: no fingerprint")), + _ => { + let line = format!("{target:#x}: no fingerprint"); + // Without the prior module no method can place it, but it is still settled with + // the other open addresses, so a feature that is off does not stop the row. + let kind = match prior_pe { + Some(prior_pe) if !prior_pe.is_code(*target) => row_resolve::Kind::Table, + _ => row_resolve::Kind::Function, + }; + wanted.insert(*target, (line, want(*target, kind, None))); + } } } let found = find_prints(pe, &functions); + let new_starts = if prior_starts.is_empty() { + BTreeSet::new() + } else { + pe.function_starts() + }; let mut moved = BTreeMap::new(); for target in functions.keys() { - match found.get(target).map(Vec::as_slice) { - Some([only]) => { + let all = found.get(target).map_or(&[][..], Vec::as_slice); + let places: Vec = all + .iter() + .copied() + .filter(|place| { + // The linker starts whole functions on 16 bytes, and the exception table also + // lists the parts it moves out of a function, which start anywhere. + !prior_starts.contains(target) + || (new_starts.contains(place) && (target % 16 != 0 || *place % 16 == 0)) + }) + .collect(); + let function = row_resolve::Kind::Function; + match places.as_slice() { + [only] => { moved.insert(*target, *only); + // Says which matches the exception tables checked, for the person reading the report. + let checked = if prior_starts.contains(target) { + " at a function start" + } else { + "" + }; + report + .fingerprinted + .push(format!("{target:#x} -> {only:#x}{checked}")); + } + [] if !all.is_empty() => { + let line = format!( + "{target:#x} matches no function start, of {} places", + all.len() + ); + let mut asked = want(*target, function, None); + asked.loose = all.to_vec(); + wanted.insert(*target, (line, asked)); + } + [] => { + let line = format!("{target:#x} matches no place"); + wanted.insert(*target, (line, want(*target, function, None))); + } + many => { + let line = format!("{target:#x} matches {} places", many.len()); + wanted.insert(*target, (line, want(*target, function, Some(many.to_vec())))); } - Some(many) => report - .review - .push(format!("{target:#x} matches {} places", many.len())), - None => report.review.push(format!("{target:#x} matches no place")), } } for (target, (slot, holds)) in &tables { + let table = row_resolve::Kind::Table; let Some(function) = moved.get(holds).copied() else { - report.review.push(format!( - "{target:#x}: the function in slot {slot} was not found" - )); + let line = format!("{target:#x}: the function in slot {slot} was not found"); + wanted.insert(*target, (line, want(*target, table, None))); continue; }; match find_table(pe, *slot, function).as_slice() { [only] => { moved.insert(*target, *only); + report + .fingerprinted + .push(format!("{target:#x} -> {only:#x}")); } - many => report.review.push(format!( - "{target:#x}: {} tables hold {function:#x} in slot {slot}", - many.len() - )), + many => { + let line = format!( + "{target:#x}: {} tables hold {function:#x} in slot {slot}", + many.len() + ); + let candidates = (!many.is_empty()).then(|| many.to_vec()); + wanted.insert(*target, (line, want(*target, table, candidates))); + } + } + } + let mut followed = BTreeMap::new(); + // Each address no method placed, with its review line. Those that only features the runtime + // can do without hold do not stop the row. + let mut open: BTreeMap = BTreeMap::new(); + match prior_pe { + Some(prior_pe) if !wanted.is_empty() => { + let table_targets: BTreeSet = tables + .keys() + .copied() + .chain( + wanted + .iter() + .filter(|(_, (_, want))| want.kind == row_resolve::Kind::Table) + .map(|(target, _)| *target), + ) + .collect(); + let asked: BTreeMap = wanted + .iter() + .map(|(target, (_, want))| (*target, want.clone())) + .collect(); + // The tables a class name was confirmed for in the prior build are the tables of views + // classes the slot shift compares. + let views: BTreeSet = classes.keys().copied().collect(); + let resolution = + row_resolve::resolve(prior_pe, pe, &moved, &asked, &table_targets, &views); + for (target, (line, _)) in &wanted { + match resolution.places.get(target) { + Some(place) => { + moved.insert(*target, *place); + } + None => { + let reason = resolution + .reasons + .get(target) + .map_or("no method was run", String::as_str); + open.insert(*target, format!("{line}; not settled: {reason}")); + } + } + } + report.settled.extend(resolution.settled); + followed = resolution.followed; } + _ => open.extend(wanted.into_iter().map(|(target, (line, _))| (target, line))), } + release_unplaced(prior, &mut row, open, &mut report); let places: BTreeSet = moved.values().copied().collect(); if places.len() != moved.len() { report .review .push("two addresses found the same place".to_owned()); } - let (mut row, report) = finish_row(row, &moved, pe, report); - if !report.review.is_empty() { - return Err(report.review); - } + let (mut row, mut report) = finish_row(row, &moved, pe, report); if let Some(object) = row.as_object_mut() { // They describe the prior build, so they would mislead in this row. object.remove("fingerprints"); + object.remove(CLASSES); } - Ok(row) + record_classes(prior, &mut row, classes, &moved); + if let Some(prior_pe) = prior_pe { + read_carried(prior, &mut row, &moved, &followed, prior_pe, pe, &mut report); + } + settle_carried(&mut row, &mut report); + (row, report) } -/// A folded function that fills a known table slot is the one in that slot of the new table. -fn by_slot( - target: u32, - choices: &[(u32, usize)], - build: &Value, - pe: &PeFile, - named: &BTreeMap)>, - symbols: &HashMap>, -) -> Option { - for (vtable_path, slot_path, function_path) in VTABLE_SLOTS { - if usize_at(build, function_path) != Some(u64::from(target)) { - continue; +/// Settles each address no method placed in the new build. Where every value that holds it belongs +/// to a feature the runtime can do without, or is not read in this row, those values are cleared +/// and the features go off, and the address is listed in the report. An address that a part the +/// window cannot do without holds, the frame or the page area layout, stops the row with its line. +fn release_unplaced( + prior: &Value, + row: &mut Value, + open: BTreeMap, + report: &mut PortReport, +) { + use crate::row_features::{self, Clear}; + if open.is_empty() { + return; + } + let mut holders: BTreeMap> = BTreeMap::new(); + let mut note = |path: &str, value: &mut Value| { + if READ_FROM_CODE.iter().any(|field| path.ends_with(field)) { + return; } - let vtable = usize_at(build, vtable_path)? as u32; - let slot = usize_at(build, slot_path)? as u32; - let (distance, names) = named.get(&vtable)?; - let [(new_vtable, _)] = candidates(names, *distance, symbols)[..] else { - continue; - }; - let held = pe.pointer_rva(new_vtable + slot * 8)?; - if choices.iter().any(|(place, _)| *place == held) { - return Some(held); + if let Some(address) = read_address(value).filter(|value| is_address(*value)) { + holders + .entry(address as u32) + .or_default() + .push(path.trim_start_matches('.').to_owned()); } - } - // The classes of hidden buttons keep the function of one known slot. - let slot = usize_at(build, "toolbar.theme_slot")? as u32; - for pair in build["toolbar"]["hidden"].as_array()? { - if read_address(&pair[1]) != Some(u64::from(target)) { + }; + let mut build = prior["build"].clone(); + walk_addresses("", &mut build, &mut note); + // The frame part has no feature to turn off, so its paths always stop the row. + let mut frame = frame_addresses(&prior["frame"]); + walk_addresses("frame", &mut frame, &mut note); + for (target, line) in open { + let paths = holders.remove(&target).unwrap_or_default(); + let needed = paths.is_empty() + || paths.iter().any(|path| { + path.starts_with("frame.") + || (row_features::feature_of(path).is_none() + && row_features::unused(&row["build"], path).is_none()) + }); + if needed { + report.review.push(line); continue; } - let (distance, names) = named.get(&(read_address(&pair[0])? as u32))?; - let [(new_vtable, _)] = candidates(names, *distance, symbols)[..] else { - continue; - }; - let held = pe.pointer_rva(new_vtable + slot * 8)?; - if choices.iter().any(|(place, _)| *place == held) { - return Some(held); + for path in &paths { + match row_features::feature_of(path) { + Some(feature) => { + row_features::clear_value(&mut row["build"], path, feature); + report.off_values.insert( + path.clone(), + format!("has no place in the new build: {line}"), + ); + } + None => { + let why = row_features::unused(&row["build"], path).unwrap_or_default(); + // A list holds no single address, so a value in one becomes zero. + let how = if path.contains('[') { Clear::Zero } else { Clear::Absent }; + row_features::clear(&mut row["build"], path, how); + report.unused.push(format!("{path}: {why}")); + } + } } + report + .unplaced + .push(format!("{line}; cleared from {}", paths.join(", "))); } - None } -/// The frame entry keeps plain numbers. The walk over a row reads `0x` strings. -fn frame_addresses(frame: &Value) -> Value { - let field = |key: &str| frame[key].as_u64().map_or(Value::Null, write_address); - json!({ - "frame_view_vtable": field("frame_view_vtable"), - "client_view_bounds": field("client_view_bounds"), - "hwnd_for_view": field("hwnd_for_view"), - }) +/// The value at a path such as `layout.frame_ids[2]` or `toolbar.dev.colours[3][0]`. +fn value_at<'a>(value: &'a Value, path: &str) -> Option<&'a Value> { + let mut current = value; + for part in path.split('.') { + let (key, indexes) = part.split_at(part.find('[').unwrap_or(part.len())); + current = current.get(key)?; + for index in indexes.split(']').filter(|index| !index.is_empty()) { + current = current.get(index.trim_start_matches('[').parse::().ok()?)?; + } + } + Some(current) } -fn apply_frame_addresses(frame: &mut Value, addresses: &Value) { - for key in ["frame_view_vtable", "client_view_bounds", "hwnd_for_view"] { - if let Some(value) = read_address(&addresses[key]) { - frame[key] = json!(value); +fn value_at_mut<'a>(value: &'a mut Value, path: &str) -> Option<&'a mut Value> { + let mut current = value; + for part in path.split('.') { + let (key, indexes) = part.split_at(part.find('[').unwrap_or(part.len())); + current = current.get_mut(key)?; + for index in indexes.split(']').filter(|index| !index.is_empty()) { + current = current.get_mut(index.trim_start_matches('[').parse::().ok()?)?; } } + Some(current) } -fn refresh_frame_guard(frame: &mut Value, pe: &PeFile, report: &mut PortReport) { - let (Some(address), Some(old)) = ( - frame["hwnd_for_view"].as_u64(), - frame["hwnd_for_view_prologue"].as_str().and_then(hex), +/// Reads each value the recipes leave, a member offset, slot, size or id, from the instructions +/// that use it in the prior build's functions and the same instructions in the new build. A value +/// that cannot be read keeps its reason and stays listed as carried, and `settle_carried` decides +/// what it does to the row, so the row is never published with a value of the prior build. +fn read_carried( + prior: &Value, + row: &mut Value, + moved: &BTreeMap, + followed: &BTreeMap, + prior_pe: &PeFile, + pe: &PeFile, + report: &mut PortReport, +) { + let mut values = Vec::new(); + let mut unreadable = Vec::new(); + for path in std::mem::take(&mut report.carried) { + let items: Vec<(String, &Value)> = match value_at(&prior["build"], &path) { + Some(Value::Array(items)) => items + .iter() + .enumerate() + .map(|(index, item)| (format!("{path}[{index}]"), item)) + .collect(), + Some(item) => vec![(path.clone(), item)], + None => vec![], + }; + if items.is_empty() { + unreadable.push(path); + } + for (path, item) in items { + match crate::row_recipe::number(item) { + Some(value) => values.push((path, value)), + None => unreadable.push(path), + } + } + } + // Each part of the row reads its values from its own functions. + let mut sections: BTreeMap> = BTreeMap::new(); + for (section, part) in prior["build"].as_object().into_iter().flatten() { + let mut part = part.clone(); + let mut pairs = Vec::new(); + walk_addresses("", &mut part, &mut |_, value| { + if let Some(address) = read_address(value).filter(|value| is_address(*value)) { + let address = address as u32; + if let Some(place) = moved.get(&address).filter(|_| prior_pe.is_code(address)) { + pairs.push((address, *place)); + } + } + }); + pairs.sort_unstable(); + pairs.dedup(); + if !pairs.is_empty() { + sections.insert(section.clone(), pairs); + } + } + for reading in row_resolve::read_values(prior_pe, pe, §ions, followed, &values) { + match reading.value { + Ok((value, how)) => { + if let Some(place) = value_at_mut(&mut row["build"], &reading.path) { + *place = if place.is_string() { + write_address(value) + } else { + json!(value) + }; + } + report.derived.push(if value == reading.prior { + format!("{}: {value:#x}, {how}", reading.path) + } else { + format!("{}: {:#x} -> {value:#x}, {how}", reading.path, reading.prior) + }); + } + Err(reason) => { + report.unread.insert( + reading.path.clone(), + format!("was not read from the new build: {reason}"), + ); + report.carried.push(reading.path); + } + } + } + for path in unreadable { + report.unread.insert( + path.clone(), + "is not a number this generator can read".to_owned(), + ); + report.carried.push(path); + } +} + +/// The key of a row that names the views class of each of its class tables, by the path of the +/// value that holds the table, as `{"build.layout.frame_vtable": "BrowserFrameViewWin"}`. The +/// runtime reads only `frame` and `build`, so the names change nothing in a browser. +pub const CLASSES: &str = "classes"; + +/// The path of every address a row holds, the frame entry's included, as `build.layout.frame_vtable` +/// or `frame.frame_view_vtable`. +fn address_paths(row: &Value) -> Vec<(String, u32)> { + let mut out = Vec::new(); + let mut note = |path: &str, value: &mut Value| { + if READ_FROM_CODE.iter().any(|field| path.ends_with(field)) { + return; + } + if let Some(address) = read_address(value).filter(|value| is_address(*value)) { + out.push((path.to_owned(), address as u32)); + } + }; + let mut build = row["build"].clone(); + walk_addresses("build", &mut build, &mut note); + let mut frame = frame_addresses(&row["frame"]); + walk_addresses("frame", &mut frame, &mut note); + out +} + +/// The views class of each class table of the prior row, from the build alone. A name the prior +/// row records for the table is taken when it gives exactly that table in the prior module, and +/// a table with no such name is named by the class name its metadata holds, read back from the +/// prior module by `row_class`. Either way the name is the one the class's `BEGIN_METADATA` gives +/// in Chromium's source, and a table no name gives alone has none. +pub fn prior_classes(prior: &Value, prior_pe: &PeFile) -> BTreeMap { + let recorded = prior[CLASSES].as_object(); + let mut out = BTreeMap::new(); + let mut tried = BTreeSet::new(); + for (path, address) in address_paths(prior) { + if prior_pe.is_code(address) || out.contains_key(&address) { + continue; + } + let name = recorded + .and_then(|names| names.get(&path)) + .and_then(Value::as_str) + .filter(|name| crate::row_class::names_table(prior_pe, name, address)) + .map(str::to_owned); + let name = match name { + Some(name) => Some(name), + None if tried.insert(address) => { + crate::row_class::class_name_of_table(prior_pe, address) + } + None => None, + }; + if let Some(name) = name { + out.insert(address, name); + } + } + out +} + +/// Writes into the new row the class name of each class table it placed, by the path of each value +/// that holds the table. +fn record_classes( + prior: &Value, + row: &mut Value, + classes: &BTreeMap, + moved: &BTreeMap, +) { + let mut names = serde_json::Map::new(); + for (path, address) in address_paths(prior) { + if let Some(name) = classes + .get(&address) + .filter(|_| moved.contains_key(&address)) + { + names.insert(path, Value::String(name.clone())); + } + } + if !names.is_empty() { + row[CLASSES] = Value::Object(names); + } +} + +/// The frame entry keeps plain numbers. The walk over a row reads `0x` strings. +fn frame_addresses(frame: &Value) -> Value { + let field = |key: &str| frame[key].as_u64().map_or(Value::Null, write_address); + json!({ + "frame_view_vtable": field("frame_view_vtable"), + "client_view_bounds": field("client_view_bounds"), + "hwnd_for_view": field("hwnd_for_view"), + }) +} + +fn apply_frame_addresses(frame: &mut Value, addresses: &Value) { + for key in ["frame_view_vtable", "client_view_bounds", "hwnd_for_view"] { + if let Some(value) = read_address(&addresses[key]) { + frame[key] = json!(value); + } + } +} + +fn refresh_frame_guard(frame: &mut Value, pe: &PeFile, report: &mut PortReport) { + let (Some(address), Some(old)) = ( + frame["hwnd_for_view"].as_u64(), + frame["hwnd_for_view_prologue"].as_str().and_then(hex), ) else { report .review @@ -1350,59 +1801,136 @@ fn check_frame_slot(frame: &mut Value, pe: &PeFile, report: &mut PortReport) { } } -/// The start of the public name of `views::View::ConvertPointToScreen(const View*, gfx::Point*)`: -/// a static function returning nothing. The other overload returns the point and is named `SA?AV`. -pub const SCREEN_ORIGIN_NAME: &str = "?ConvertPointToScreen@View@views@@SAX"; +/// Leaves `screen_origin`, `[address, first bytes]` of `views::View::ConvertPointToScreen`, out of a +/// row whose prior row had none. A prior row that had it was found again with every other address. +/// The runtime then keeps placing bubbles from the window's monitor, so the row is not stopped. +pub fn leave_out_screen_origin(row: &mut Value, report: &mut PortReport) { + if row["build"]["screen_origin"].is_array() { + return; + } + row["build"]["screen_origin"] = Value::Null; + report + .left_out + .push("build.screen_origin: the prior row has none".to_owned()); +} -/// How many bytes of that function's start the runtime compares before it ever calls it. -const SCREEN_ORIGIN_BYTES: usize = 12; +/// The group a value of a row is scored in: its feature, or the window, whose parts cannot be off. +const WINDOW: &str = "window"; + +/// How one feature of a row made without debug symbols compares with the verified row of the same +/// build, value by value. +#[derive(Debug, Default, Serialize, PartialEq, Eq)] +pub struct FeatureScore { + /// Values the made row holds exactly as the verified row does. + pub equal: usize, + /// Values the made row holds differently, with both. Any is a wrong value. + pub different: Vec, + /// The made row turns the feature off, so none of its values is used. + pub off: bool, + /// Values the made row does not hold or does not use: the runtime finds them by reflection, + /// does not read them in this build, or the prior row did not have them. + pub not_held: Vec, +} -/// Whether a public name is the one `screen_origin` is found by. -pub fn is_screen_origin_name(name: &str) -> bool { - stable_name(name).starts_with(SCREEN_ORIGIN_NAME) +/// The score of a made row against its answer key: each feature's comparison, and the review lines +/// that stop the row. +#[derive(Debug, Default, Serialize)] +pub struct RowScore { + pub browser: String, + pub version: String, + pub prior_version: String, + pub features: BTreeMap, + pub review: Vec, } -/// The first whole instructions of a function, at least `SCREEN_ORIGIN_BYTES` long. -fn function_start(pe: &PeFile, address: u32) -> Option> { - if !pe.is_code(address) { - return None; +impl RowScore { + /// How many values were found but differ from the verified row. Must be zero. + pub fn wrong(&self) -> usize { + self.features + .values() + .map(|score| score.different.len()) + .sum() } - let window = pe.read(address, 32)?; - let (ends, _) = x64::boundaries(window, SCREEN_ORIGIN_BYTES); - let end = *ends.iter().find(|end| **end >= SCREEN_ORIGIN_BYTES)?; - Some(window[..end].to_vec()) } -/// Adds `screen_origin`, `[address, first bytes]` of `views::View::ConvertPointToScreen`, to a row -/// whose prior row had none, by the function's public name in the new build. A prior row that had -/// it was ported with every other address. A build with no single place for the name goes without -/// it, and the runtime keeps placing bubbles from the window's monitor, so the row is not stopped. -pub fn seed_screen_origin(row: &mut Value, places: &[u32], pe: &PeFile, report: &mut PortReport) { - if row["build"]["screen_origin"].is_array() { - return; +/// Every value a row part holds that is not a list or object, by its path. +fn leaves(path: &str, value: &Value, out: &mut BTreeMap) { + match value { + Value::Object(object) => { + for (key, item) in object { + let next = if path.is_empty() { + key.clone() + } else { + format!("{path}.{key}") + }; + leaves(&next, item, out); + } + } + Value::Array(items) if !items.is_empty() => { + for (index, item) in items.iter().enumerate() { + leaves(&format!("{path}[{index}]"), item, out); + } + } + _ => { + out.insert(path.to_owned(), value.clone()); + } } - let mut places = places.to_vec(); - places.sort_unstable(); - places.dedup(); - let found = match places.as_slice() { - [only] => function_start(pe, *only).map(|bytes| (*only, bytes)), - _ => None, +} + +/// Compares a row made from a prior row with the row people verified for the same build. A value of +/// a feature the made row turns off is not compared, since the runtime does not use it. A value the +/// made row leaves out, or writes as zero because this build does not read it, is listed as not +/// held. Every other value must be equal, and one that differs is listed with both values. +pub fn score_row(made: &Value, report: &PortReport, verified: &Value) -> RowScore { + let off: BTreeSet<&str> = made[crate::row_features::SWITCHED_OFF] + .as_array() + .map_or(&[][..], Vec::as_slice) + .iter() + .filter_map(|entry| entry["feature"].as_str()) + .collect(); + let unused: BTreeSet<&str> = report + .unused + .iter() + .chain(&report.reflected) + .filter_map(|line| line.split(": ").next()) + .collect(); + let mut score = RowScore { + browser: report.browser.clone(), + version: report.version.clone(), + prior_version: report.prior_version.clone(), + review: report.review.clone(), + ..RowScore::default() }; - match found { - Some((address, bytes)) => { - row["build"]["screen_origin"] = - json!([write_address(u64::from(address)), hex_text(&bytes)]); - report.derived.push(format!( - "build.screen_origin: {SCREEN_ORIGIN_NAME} at {address:#x}" - )); - } - None => { - row["build"]["screen_origin"] = Value::Null; - report.left_out.push(format!( - "build.screen_origin: no single readable function named {SCREEN_ORIGIN_NAME}: {places:x?}" - )); + for part in ["frame", "build"] { + let (mut want, mut have) = (BTreeMap::new(), BTreeMap::new()); + leaves("", &verified[part], &mut want); + leaves("", &made[part], &mut have); + for (path, value) in want { + let feature = match part { + "build" => { + crate::row_features::feature_of(&path).map_or(WINDOW, |feature| feature.name) + } + _ => WINDOW, + }; + let entry = score.features.entry(feature.to_owned()).or_default(); + if off.contains(feature) { + entry.off = true; + continue; + } + let shown = format!("{part}.{path}"); + match have.get(&path) { + Some(found) if *found == value => entry.equal += 1, + None | Some(Value::Null) => entry.not_held.push(shown), + Some(_) if unused.iter().any(|unused| path.starts_with(unused)) => { + entry.not_held.push(shown) + } + Some(found) => entry + .different + .push(format!("{shown}: made {found}, verified {value}")), + } } } + score } /// The review items for the parts of the prior row that only a person writes today. A row made @@ -1478,58 +2006,33 @@ mod tests { }) } - fn symbols_old() -> Vec { - [ - (0x200000, "vt"), - (0x200100, "bounds"), - (0x200200, "hwnd"), - (0x200300, "paint"), - ] - .map(|(address, name)| (address, name.to_owned())) - .to_vec() - } - - fn places(rows: &[(&str, &[u32])]) -> HashMap> { - rows.iter() - .map(|(name, list)| ((*name).to_owned(), list.to_vec())) - .collect() - } - fn pointer(rva: u32) -> [u8; 8] { (IMAGE_BASE + u64::from(rva)).to_le_bytes() } + /// Places the prior row's addresses where `moved` says, as the fingerprints and `row_resolve` + /// would, and settles the rest of the row the way every new row is settled. fn port( prior: &Value, - new: &[(&str, &[u32])], + moved: &[(u32, u32)], code: &[(u32, &[u8])], version: &str, ) -> (Value, PortReport) { - let mut targets = addresses_of(&prior["build"]); - for key in ["frame_view_vtable", "client_view_bounds", "hwnd_for_view"] { - targets.insert(prior["frame"][key].as_u64().unwrap() as u32); - } - let named = name_addresses(&targets, &symbols_old()); let bytes = module(code); let pe = PeFile::parse(&bytes).unwrap(); - let symbols = places(new); - port_row( - prior, - &named, - &NewBuild { - version, - pe: &pe, - symbols: &symbols, - private: &BTreeMap::new(), - }, - ) - } - - const NEW_SYMBOLS: &[(&str, &[u32])] = &[ - ("vt", &[0x101000]), - ("bounds", &[0x101100]), - ("hwnd", &[0x101200]), - ("paint", &[0x101300]), + let (row, mut report) = begin_row(prior, version, true); + report.addresses = addresses_of(&row["build"]).len(); + let moved: BTreeMap = moved.iter().copied().collect(); + let (mut row, mut report) = finish_row(row, &moved, &pe, report); + settle_carried(&mut row, &mut report); + (row, report) + } + + const NEW_PLACES: &[(u32, u32)] = &[ + (0x200000, 0x101000), + (0x200100, 0x101100), + (0x200200, 0x101200), + (0x200300, 0x101300), ]; fn new_code() -> Vec<(u32, Vec)> { @@ -1547,47 +2050,9 @@ mod tests { } #[test] - fn nfr3_screen_origin_is_found_by_its_public_name_in_the_new_build() { - // push rsi; push rdi; sub rsp, 0x28; mov rsi, rdx; mov rdi, rcx; call. - let start = hex("56574883ec284889d64889cfe800000000").unwrap(); - let bytes = module(&[(0x101400, start.as_slice())]); - let pe = PeFile::parse(&bytes).unwrap(); - let mut row = prior(); - let mut report = PortReport::default(); - seed_screen_origin(&mut row, &[0x101400, 0x101400], &pe, &mut report); - assert_eq!( - row["build"]["screen_origin"], - json!(["0x101400", "56574883ec284889d64889cf"]) - ); - assert!(report.review.is_empty() && report.left_out.is_empty()); - // A row that already has it keeps the place the port gave it. - seed_screen_origin(&mut row, &[0x101500], &pe, &mut report); - assert_eq!(row["build"]["screen_origin"][0], "0x101400"); - // No name, or two places for it, leaves the part out without stopping the row. - for places in [&[][..], &[0x101400, 0x101500][..]] { - let mut row = prior(); - let mut report = PortReport::default(); - seed_screen_origin(&mut row, places, &pe, &mut report); - assert!(row["build"]["screen_origin"].is_null()); - assert_eq!(report.left_out.len(), 1); - assert!(report.review.is_empty()); - } - // Only the in-place overload, which returns nothing, is the one. - assert!(is_screen_origin_name( - "?ConvertPointToScreen@View@views@@SAXPEBV12@PEAVPoint@gfx@@@Z" - )); - assert!(!is_screen_origin_name( - "?ConvertPointToScreen@View@views@@SA?AVPoint@gfx@@PEBV12@AEBV34@@Z" - )); - assert!(!is_screen_origin_name( - "?ConvertRectToScreen@View@views@@SAXPEBV12@PEAVRect@gfx@@@Z" - )); - } - - #[test] - fn nfr3_a_new_row_takes_addresses_by_name_and_bytes_from_the_new_module() { + fn nfr3_a_new_row_takes_the_found_places_and_bytes_from_the_new_module() { let code = new_code(); - let (row, report) = port(&prior(), NEW_SYMBOLS, &code_refs(&code), "1.0.0.2"); + let (row, report) = port(&prior(), NEW_PLACES, &code_refs(&code), "1.0.0.2"); assert_eq!(report.review, Vec::::new()); assert_eq!(row["build"]["version"], json!([1, 0, 0, 2])); assert_eq!(row["frame"]["version"], "1.0.0.2"); @@ -1604,14 +2069,14 @@ mod tests { } #[test] - fn nfr3_a_name_the_new_build_lacks_stops_the_row() { + fn nfr3_an_address_with_no_new_place_stops_the_row() { let code = new_code(); - let symbols: Vec<_> = NEW_SYMBOLS + let places: Vec<_> = NEW_PLACES .iter() - .filter(|(name, _)| *name != "paint") + .filter(|(prior, _)| *prior != 0x200300) .copied() .collect(); - let (_, report) = port(&prior(), &symbols, &code_refs(&code), "1.0.0.2"); + let (_, report) = port(&prior(), &places, &code_refs(&code), "1.0.0.2"); assert!( report .review @@ -1623,77 +2088,158 @@ mod tests { } #[test] - fn nfr3_a_row_that_crosses_a_milestone_stops_for_review() { + fn nfr6_a_command_id_is_not_a_row_value() { + let mut prior = prior(); + prior["build"]["browser"] = json!("Google Chrome"); + prior["frame"]["version"] = json!("157.0.8103.39"); + // A wrong prior value shows that the new row does not copy it. + prior["build"]["toolbar"]["dev"] = json!({"inspect": 1, "console": 2, "colours": []}); let code = new_code(); - let (_, report) = port(&prior(), NEW_SYMBOLS, &code_refs(&code), "2.0.0.1"); + let (row, report) = port(&prior, NEW_PLACES, &code_refs(&code), "158.0.8127.36"); + assert_eq!(row["build"]["toolbar"]["dev"]["inspect"], 40023); + assert_eq!(row["build"]["toolbar"]["dev"]["console"], 40005); assert!( - report.review.iter().any(|line| line.contains("milestone")), + !report.carried.iter().any(|path| path.starts_with("toolbar.dev.inspect") + || path.starts_with("toolbar.dev.console")), "{:?}", - report.review + report.carried + ); + let milestone = report + .review + .iter() + .find(|line| line.contains("milestone")) + .expect("the other values still cross the milestone"); + assert!(!milestone.contains("toolbar.dev.console"), "{milestone}"); + // A milestone whose header was not read offers no command rather than the prior one. + let (row, _) = port(&prior, NEW_PLACES, &code_refs(&code), "199.0.1.1"); + assert_eq!(row["build"]["toolbar"]["dev"]["inspect"], 0); + // A row that never offered the developer bar keeps offering none. + prior["build"]["toolbar"]["dev"] = json!({"inspect": 0, "console": 0, "colours": []}); + let (row, _) = port(&prior, NEW_PLACES, &code_refs(&code), "158.0.8127.36"); + assert_eq!(row["build"]["toolbar"]["dev"]["console"], 0); + } + + /// A prior row whose values that are not addresses all belong to features the runtime can do + /// without, or are not read in this row. + fn prior_with_features() -> Value { + let mut prior = prior(); + let toolbar = prior["build"]["toolbar"].as_object_mut().unwrap(); + toolbar.remove("view_layer"); + toolbar.remove("colours"); + toolbar.insert( + "dev".to_owned(), + json!({"colours": [[321, 4280953905u32], [1597, 0]]}), ); + prior["build"]["split"] = json!({"supported": true, "outline_member": "0x40"}); + prior["build"]["omnibox"] = json!({"match_size": "0x820"}); + prior["build"]["tabs"]["tab_at"] = json!("0x200300"); + prior["build"]["tabs"]["tab_collection"] = json!("0x30"); + prior } #[test] - fn nfr3_a_frame_slot_that_no_longer_holds_its_function_stops_the_row() { - let code: Vec<_> = new_code() - .into_iter() - .filter(|(rva, _)| *rva != 0x101008) + fn nfr6_a_feature_with_an_unread_value_is_off_for_that_build() { + let code = new_code(); + let (row, report) = port(&prior_with_features(), NEW_PLACES, &code_refs(&code), "2.0.0.1"); + // Nothing holds the rest of the row back. + assert_eq!(report.review, Vec::::new()); + assert!(report.carried.is_empty(), "{:?}", report.carried); + let build = &row["build"]; + // Each feature with a value of the prior build is off, and the value is gone. + assert_eq!(build["split"]["supported"], false); + assert_eq!(build["split"]["outline_member"], "0x0"); + assert!(build["omnibox"].is_null()); + assert_eq!(build["toolbar"]["dev"]["colours"], json!([])); + let off: Vec<&str> = row[crate::row_features::SWITCHED_OFF] + .as_array() + .unwrap() + .iter() + .map(|entry| entry["feature"].as_str().unwrap()) .collect(); - let (_, report) = port(&prior(), NEW_SYMBOLS, &code_refs(&code), "1.0.0.2"); + assert_eq!(off, ["address suggestions", "developer colours", "split view"]); + assert_eq!(report.switched_off.len(), 3, "{:?}", report.switched_off); + // A value this build does not use is written as zero, and no feature goes off for it. + assert_eq!(build["tabs"]["tab_collection"], "0x0"); + assert_eq!(report.unused.len(), 1, "{:?}", report.unused); + // The addresses of the row still move to the new build. + assert_eq!(build["tabs"]["tab_at"], "0x101300"); + } + + #[test] + fn nfr6_a_row_without_window_offsets_is_not_blocked() { + // Edge 157's row stopped on exactly these values. The runtime finds them by class name and + // the view tree, so the new row leaves them out and nothing holds it back. + let mut prior = prior_with_features(); + prior["build"]["layout"] = json!({"frame_browser_window_offset": "0x3b0"}); + prior["build"]["side_panel"] = + json!({"host_view": "0x38", "page_area": "0x0", "contents_container": "0xd8"}); + prior["build"]["toolbar"]["get_widget_slot"] = json!("0xa"); + let code = new_code(); + let (row, report) = port(&prior, NEW_PLACES, &code_refs(&code), "2.0.0.1"); + assert_eq!(report.review, Vec::::new()); + assert!(report.carried.is_empty(), "{:?}", report.carried); + assert_eq!(report.reflected.len(), 5, "{:?}", report.reflected); + let build = &row["build"]; + assert!(build["layout"].get("frame_browser_window_offset").is_none()); + assert_eq!(build["side_panel"], json!({})); + assert!(build["toolbar"].get("get_widget_slot").is_none()); + // No feature went off for them. + assert!(!report + .switched_off + .iter() + .any(|line| line.contains("side_panel.") || line.contains("get_widget_slot"))); + } + + #[test] + fn nfr6_a_value_the_window_cannot_do_without_still_stops_the_row() { + let mut prior = prior_with_features(); + prior["build"]["layout"] = json!({"view_bounds_offset": "0x1c8"}); + let code = new_code(); + let (row, report) = port(&prior, NEW_PLACES, &code_refs(&code), "2.0.0.1"); + assert_eq!(report.carried, ["layout.view_bounds_offset"]); assert!( - report - .review - .iter() - .any(|line| line.contains("does not hold")), + report.review.iter().any(|line| line.contains("milestone") + && line.ends_with("carried over unchecked: layout.view_bounds_offset")), "{:?}", report.review ); + // The features it could do without are still off. + assert_eq!(row["build"]["split"]["supported"], false); } #[test] - fn nfr3_folded_names_that_stay_apart_are_settled_by_the_guard_or_stop_the_row() { - let mut code = new_code(); - code.push((0x101400, hex("565755534883ec584889ce48").unwrap())); - let two: &[u32] = &[0x101300, 0x101400]; - let symbols: Vec<_> = NEW_SYMBOLS - .iter() - .map(|(name, list)| { - if *name == "paint" { - (*name, two) - } else { - (*name, *list) - } - }) - .collect(); - let (row, report) = port(&prior(), &symbols, &code_refs(&code), "1.0.0.2"); - assert_eq!(row["build"]["toolbar"]["paint"], "0x101300"); - assert_eq!(report.settled.len(), 1); - assert!(report.review.is_empty(), "{:?}", report.review); - // Two places with the same start cannot be told apart. - let mut same = new_code(); - same.push((0x101400, hex("415741564155415456575553").unwrap())); - let (_, report) = port(&prior(), &symbols, &code_refs(&same), "1.0.0.2"); + fn nfr6_a_row_within_its_milestone_turns_nothing_off() { + let code = new_code(); + let (row, report) = port(&prior_with_features(), NEW_PLACES, &code_refs(&code), "1.0.0.2"); + assert_eq!(report.review, Vec::::new()); + assert!(report.switched_off.is_empty()); + assert_eq!(row["build"]["split"]["supported"], true); + assert!(row.get(crate::row_features::SWITCHED_OFF).is_none()); + } + + #[test] + fn nfr3_a_row_that_crosses_a_milestone_stops_for_review() { + let code = new_code(); + let (_, report) = port(&prior(), NEW_PLACES, &code_refs(&code), "2.0.0.1"); assert!( - report - .review - .iter() - .any(|line| line.contains("several places")), + report.review.iter().any(|line| line.contains("milestone")), "{:?}", report.review ); } #[test] - fn nfr3_a_function_with_no_public_name_is_not_moved_by_distance() { - let mut prior = prior(); - prior["build"]["toolbar"]["inside"] = json!("0x200340"); - let code = new_code(); - let (_, report) = port(&prior, NEW_SYMBOLS, &code_refs(&code), "1.0.0.2"); + fn nfr3_a_frame_slot_that_no_longer_holds_its_function_stops_the_row() { + let code: Vec<_> = new_code() + .into_iter() + .filter(|(rva, _)| *rva != 0x101008) + .collect(); + let (_, report) = port(&prior(), NEW_PLACES, &code_refs(&code), "1.0.0.2"); assert!( report .review .iter() - .any(|line| line.contains("no name in both")), + .any(|line| line.contains("does not hold")), "{:?}", report.review ); @@ -1706,7 +2252,7 @@ mod tests { prior["build"]["toolbar"]["paint_prologue"] = json!("41574156565755534881ecb8000000"); let mut code = new_code(); code[2].1 = hex("4156565755534881eca00000004889ce0000").unwrap(); - let (row, report) = port(&prior, NEW_SYMBOLS, &code_refs(&code), "1.0.0.2"); + let (row, report) = port(&prior, NEW_PLACES, &code_refs(&code), "1.0.0.2"); assert_eq!( row["build"]["toolbar"]["paint_prologue"], "4156565755534881eca0000000" @@ -1714,39 +2260,12 @@ mod tests { assert!(report.review.is_empty(), "{:?}", report.review); } - #[test] - fn nfr3_stable_name_drops_what_the_linker_adds() { - assert_eq!(stable_name("??1GURL@@QEAA@XZ\0"), "??1GURL@@QEAA@XZ"); - assert_eq!(stable_name("?F@@YAXXZ.llvm.123456"), "?F@@YAXXZ"); - assert_eq!( - stable_name(".weak.??_7BrowserView@@6B@.default.?RejectPromise@@"), - "??_7BrowserView@@6B@" - ); - } - #[test] fn nfr3_addresses_are_told_from_offsets_and_the_absent_marker() { let row = json!({"a": "0x10", "b": "0xfffff", "c": "0x100000", "d": ["0x7fff0000", "0x4fb01f0"], "e": 1234567, "f": "0xabcd_not"}); assert_eq!(addresses_of(&row), BTreeSet::from([0x100000, 0x4fb01f0])); } - #[test] - fn nfr3_an_address_inside_a_table_is_named_by_the_table_below_it() { - let symbols: Vec = vec![ - (0x1000, "other".into()), - (0x2000, "??_7Table@@6B@".into()), - (0x2000, "alias".into()), - (0x9000, "far".into()), - ]; - let named = name_addresses(&BTreeSet::from([0x2000, 0x2250, 0x8000]), &symbols); - assert_eq!( - named[&0x2000], - (0, vec!["??_7Table@@6B@".to_owned(), "alias".to_owned()]) - ); - assert_eq!(named[&0x2250].0, 0x250); - assert!(!named.contains_key(&0x8000)); - } - #[test] fn nfr3_x64_lengths_of_real_function_starts() { // Chrome 156.0.8078.74 chip text: the frame setup ends at byte 13. @@ -1802,6 +2321,33 @@ mod tests { assert_eq!(x64::shape(&[0x56, 0xCF]), None); } + #[test] + fn nfr6_the_operands_of_a_body_instruction_are_located() { + // mov rax, [rcx + 0x558]: a displacement of four bytes from the third byte. + let load = x64::decode(&[0x48, 0x8B, 0x81, 0x58, 0x05, 0x00, 0x00]).unwrap(); + assert_eq!(load.displacement, Some((3, 4))); + assert_eq!(load.immediate, None); + // call [rax + 0x2b8]: a table slot read through its displacement. + let call = x64::decode(&[0xFF, 0x90, 0xB8, 0x02, 0x00, 0x00]).unwrap(); + assert_eq!(call.displacement, Some((2, 4))); + // mov edx, 0x9c45, and cmp byte [rcx + 0x30], 1. + let id = x64::decode(&[0xBA, 0x45, 0x9C, 0x00, 0x00]).unwrap(); + assert_eq!(id.immediate, Some((1, 4))); + let compare = x64::decode(&[0x80, 0x79, 0x30, 0x01]).unwrap(); + assert_eq!(compare.displacement, Some((2, 1))); + assert_eq!(compare.immediate, Some((3, 1))); + // lock cmpxchg and a VEX move decode only as body instructions. + let lock = [0xF0, 0x48, 0x0F, 0xB1, 0x51, 0x08]; + assert_eq!(x64::decode(&lock), None); + assert_eq!(x64::decode_body(&lock).unwrap().displacement, Some((5, 1))); + let vex = [0xC5, 0xF8, 0x10, 0x41, 0x10]; + assert_eq!(x64::decode_body(&vex).unwrap().length, 5); + assert_eq!( + x64::skeleton(&[0x48, 0x8B, 0x81, 0x58, 0x05, 0x00, 0x00], &load), + [0x48, 0x8B, 0x81, 0, 0, 0, 0] + ); + } + /// A verified row whose functions sit at 0x101000 in its module. fn verified_row() -> Value { json!({ @@ -1853,6 +2399,10 @@ mod tests { row } + fn no_classes() -> BTreeMap { + BTreeMap::new() + } + fn find(row: &Value, code: &[(u32, Vec)], version: &str) -> Result> { let bytes = module(&code_refs(code)); let pe = PeFile::parse(&bytes).unwrap(); @@ -1936,6 +2486,292 @@ mod tests { assert_eq!(reasons, ["the row has no fingerprints"]); } + /// The fingerprints of the verified row, read from its own module as the row job reads them + /// when the prior row stored none. + fn prints_of_prior() -> serde_json::Map { + let bytes = module(&code_refs(&shifted_code(0, 0x10))); + let pe = PeFile::parse(&bytes).unwrap(); + let (prints, missing) = fingerprints_of(&verified_row(), &pe); + assert!(missing.is_empty(), "{missing:?}"); + prints + } + + fn candidate( + prints: &serde_json::Map, + code: &[(u32, Vec)], + version: &str, + ) -> (Value, PortReport) { + let bytes = module(&code_refs(code)); + let pe = PeFile::parse(&bytes).unwrap(); + find_candidate(&verified_row(), prints, None, &BTreeMap::new(), version, &pe) + } + + /// `module` with an exception table at 0x10f000 that lists `starts`. + fn with_starts(code: &[(u32, Vec)], starts: &[u32]) -> Vec { + let ranges: Vec<(u32, u32)> = starts.iter().map(|start| (*start, start + 0x20)).collect(); + with_ranges(code, &ranges) + } + + /// `module` with an exception table at 0x10f000 that lists each `(start, end)`. + fn with_ranges(code: &[(u32, Vec)], ranges: &[(u32, u32)]) -> Vec { + let mut table = vec![]; + for (start, end) in ranges { + table.extend_from_slice(&start.to_le_bytes()); + table.extend_from_slice(&end.to_le_bytes()); + table.extend_from_slice(&0u32.to_le_bytes()); + } + let mut code = code.to_vec(); + code.push((0x10f000, table)); + let mut bytes = module(&code_refs(&code)); + let directory = 0x98 + 112 + 24; + bytes[directory..directory + 4].copy_from_slice(&0x10f000u32.to_le_bytes()); + bytes[directory + 4..directory + 8] + .copy_from_slice(&(12 * ranges.len() as u32).to_le_bytes()); + bytes + } + + /// The verified row's paint function, which reads `view_layer` with `loads` past the bytes its + /// fingerprint holds. + fn paint_reading(loads: &str) -> Vec { + let mut bytes = hex("4157415641554154565755534881eca0000000").unwrap(); + bytes.resize(32, 0x90); + bytes.extend(hex(loads).unwrap()); + bytes.push(0xC3); + bytes + } + + /// A candidate row for a build whose paint function reads `view_layer` with `new_loads`, made + /// from the verified row and its build, whose paint function reads it with `prior_loads`. + fn read_view_layer(prior_loads: &str, new_loads: &str) -> (Value, PortReport) { + let ranges = |shift: u32, paint: usize| { + vec![ + (0x101100 + shift, 0x101120 + shift), + (0x101200 + shift, 0x101220 + shift), + (0x101300 + shift, 0x101300 + shift + paint as u32), + ] + }; + let mut old = shifted_code(0, 0x10); + old[3].1 = paint_reading(prior_loads); + let prior_bytes = with_ranges(&old, &ranges(0, old[3].1.len())); + let prior_pe = PeFile::parse(&prior_bytes).unwrap(); + let (prints, missing) = fingerprints_of(&verified_row(), &prior_pe); + assert!(missing.is_empty(), "{missing:?}"); + let mut new = shifted_code(0x2000, 0x44); + new[3].1 = paint_reading(new_loads); + let bytes = with_ranges(&new, &ranges(0x2000, new[3].1.len())); + let pe = PeFile::parse(&bytes).unwrap(); + let classes = BTreeMap::new(); + find_candidate(&verified_row(), &prints, Some(&prior_pe), &classes, "1.0.0.3", &pe) + } + + #[test] + fn nfr6_a_carried_value_is_read_from_the_instructions_that_use_it() { + // mov rax, [rcx + 0x20]; mov rdx, [rcx + 0x20], and the new build moved the member to 0x28. + let (row, report) = + read_view_layer("488b8120000000488b9120000000", "488b8128000000488b9128000000"); + assert!(report.review.is_empty(), "{:?}", report.review); + assert_eq!(row["build"]["toolbar"]["view_layer"], "0x28"); + assert!(report.carried.is_empty(), "{:?}", report.carried); + assert_eq!( + report.derived, + ["toolbar.view_layer: 0x20 -> 0x28, read at 2 uses in 1 of its functions"] + ); + } + + #[test] + fn nfr6_a_value_no_instruction_gives_stops_the_row() { + // The new build reads the member with a short displacement: not the same instruction. + let (_, report) = read_view_layer("488b8120000000488b9120000000", "488b4128488b5128"); + assert_eq!(report.carried, ["toolbar.view_layer"]); + assert!(report.derived.is_empty(), "{:?}", report.derived); + assert!( + report.review.iter().any(|line| line + .starts_with("toolbar.view_layer was not read from the new build: its 2 uses")), + "{:?}", + report.review + ); + // One use alone is not read either, and neither are uses that disagree. + let (_, report) = read_view_layer("488b8120000000", "488b8128000000"); + assert!( + report + .review + .iter() + .any(|line| line.contains("a second reading is needed")), + "{:?}", + report.review + ); + let (_, report) = + read_view_layer("488b8120000000488b9120000000", "488b8128000000488b9130000000"); + assert!( + report + .review + .iter() + .any(|line| line.contains("its uses disagree")), + "{:?}", + report.review + ); + } + + #[test] + fn nfr6_a_function_start_is_found_only_at_a_function_start() { + // The paint function's first bytes also open a block inside another function at 0x105300. + let mut code = shifted_code(0x2000, 0x44); + code.push(( + 0x105300, + hex("4157415641554154565755534881eca0000000").unwrap(), + )); + let prior_bytes = with_starts(&shifted_code(0, 0x10), &[0x101100, 0x101200, 0x101300]); + let prior_pe = PeFile::parse(&prior_bytes).unwrap(); + let prior_starts = prior_pe.function_starts(); + assert_eq!(prior_starts.len(), 3); + // The row holds no value without a recipe, so only its addresses are looked for. + let mut verified = verified_row(); + verified["build"]["toolbar"] + .as_object_mut() + .unwrap() + .remove("view_layer"); + let (prints, _) = fingerprints_of(&verified, &prior_pe); + let bytes = with_starts(&code, &[0x103100, 0x103200, 0x103300]); + let pe = PeFile::parse(&bytes).unwrap(); + let (row, report) = + find_candidate(&verified, &prints, Some(&prior_pe), &no_classes(), "1.0.0.3", &pe); + assert!(report.review.is_empty(), "{:?}", report.review); + assert_eq!(row["build"]["toolbar"]["paint"], "0x103300"); + // A part moved out of another function starts where it may, and a whole function does not. + let mut split = shifted_code(0x2000, 0x44); + split.push(( + 0x105308, + hex("4157415641554154565755534881eca0000000").unwrap(), + )); + let bytes = with_starts(&split, &[0x103100, 0x103200, 0x103300, 0x105308]); + let pe = PeFile::parse(&bytes).unwrap(); + let (row, report) = + find_candidate(&verified, &prints, Some(&prior_pe), &no_classes(), "1.0.0.3", &pe); + assert!(report.review.is_empty(), "{:?}", report.review); + assert_eq!(row["build"]["toolbar"]["paint"], "0x103300"); + // Bytes that match only inside another function are no place at all. + let bytes = with_starts(&code, &[0x103100, 0x103200]); + let pe = PeFile::parse(&bytes).unwrap(); + let (_, report) = + find_candidate(&verified, &prints, Some(&prior_pe), &no_classes(), "1.0.0.3", &pe); + assert!( + report + .review + .iter() + .any(|line| line + .starts_with("0x101300 matches no function start, of 2 places; not settled:")), + "{:?}", + report.review + ); + } + + #[test] + fn nfr6_a_new_build_gets_a_row_from_the_prior_rows_fingerprints_alone() { + let (row, report) = candidate(&prints_of_prior(), &shifted_code(0x2000, 0x44), "1.0.0.3"); + assert!(report.review.is_empty(), "{:?}", report.review); + assert_eq!(report.fingerprinted.len(), 4, "{:?}", report.fingerprinted); + assert_eq!(row["build"]["toolbar"]["paint"], "0x103300"); + assert_eq!(row["frame"]["frame_view_vtable"], 0x103000); + assert_eq!(row["frame"]["hwnd_for_view"], 0x103200); + // Values that are not addresses are left to the recipes, and nothing of the prior build stays. + assert_eq!(row["build"]["toolbar"]["view_layer"], "0x20"); + assert!(row.get("fingerprints").is_none()); + } + + #[test] + fn nfr6_an_address_its_fingerprint_does_not_pin_down_is_not_vouched_for() { + // Two places match the paint function: that part keeps its prior value and stops the row, + // and the rest of the row is still found. + let mut code = shifted_code(0x2000, 0x44); + code.push(( + 0x105300, + hex("4157415641554154565755534881eca0000000").unwrap(), + )); + let (row, report) = candidate(&prints_of_prior(), &code, "1.0.0.3"); + assert!( + report + .review + .iter() + .any(|line| line.contains("0x101300 matches 2 places")), + "{:?}", + report.review + ); + assert!(report + .review + .iter() + .any(|line| line.contains("no new place"))); + assert_eq!(row["build"]["toolbar"]["paint"], "0x101300"); + assert_eq!(row["frame"]["hwnd_for_view"], 0x103200); + // An address the prior build gave no fingerprint is not looked for at all. + let mut prints = prints_of_prior(); + prints.remove("0x101200"); + let (_, report) = candidate(&prints, &shifted_code(0x2000, 0x44), "1.0.0.3"); + assert!( + report + .review + .iter() + .any(|line| line == "0x101200: no fingerprint"), + "{:?}", + report.review + ); + } + + #[test] + fn nfr6_unplaced_functions_of_a_feature_that_is_off_do_not_block_the_row() { + // Edge 157's row stopped on functions no method placed, most of which only features the + // runtime can do without hold. The prior build gave these two no fingerprint. + let mut prior = verified_row(); + prior["build"]["split"] = json!({ + "supported": true, + "remove_split": "0x101400", + "remove_split_prologue": "4883ec28488b0d10000000", + "signatures": [["0x101400", "4883ec28"]], + }); + prior["build"]["vertical"] = json!({ + "supported": false, + "detour_strip": false, + "should_draw_tab_strip": "0x101500", + "should_draw_tab_strip_prologue": "4883ec28", + }); + let bytes = module(&code_refs(&shifted_code(0x2000, 0x44))); + let pe = PeFile::parse(&bytes).unwrap(); + let prints = prints_of_prior(); + let (row, report) = find_candidate(&prior, &prints, None, &no_classes(), "1.0.0.3", &pe); + assert_eq!(report.review, Vec::::new()); + assert_eq!(report.unplaced.len(), 2, "{:?}", report.unplaced); + assert!(report.unplaced[0].starts_with("0x101400: no fingerprint")); + assert!(report.unplaced[0].ends_with("split.remove_split, split.signatures[0][0]")); + let build = &row["build"]; + // The feature is off and holds nothing of the prior build. + assert_eq!(build["split"]["supported"], false); + assert_eq!(build["split"]["remove_split"], "0x0"); + assert_eq!(build["split"]["signatures"][0][0], "0x0"); + assert_eq!(row[crate::row_features::SWITCHED_OFF][0]["feature"], "split view"); + assert!( + report.switched_off[0] + .starts_with("split view is off: split.remove_split has no place"), + "{:?}", + report.switched_off + ); + // A function this build does not read is absent, and turns nothing off. + assert_eq!(build["vertical"]["should_draw_tab_strip"], "0x7fff0000"); + assert_eq!(report.switched_off.len(), 1, "{:?}", report.switched_off); + // The rest of the row is found. + assert_eq!(build["toolbar"]["paint"], "0x103300"); + + // The same address held by a part the window cannot do without still stops the row. + prior["build"]["layout"] = json!({"frame_vtable": "0x101400"}); + let prints = prints_of_prior(); + let (row, report) = find_candidate(&prior, &prints, None, &no_classes(), "1.0.0.3", &pe); + assert!( + report.review.iter().any(|line| line == "0x101400: no fingerprint"), + "{:?}", + report.review + ); + assert!(report.unplaced.iter().all(|line| !line.contains("0x101400"))); + assert_eq!(row["build"]["split"]["supported"], true); + } + #[test] fn nfr3_an_address_that_is_neither_code_nor_a_table_gets_no_fingerprints() { let bytes = module(&code_refs(&shifted_code(0, 0x10))); @@ -1947,4 +2783,91 @@ mod tests { assert!(error.contains("0x1f0000"), "{error}"); assert!(row.get("fingerprints").is_none()); } + + #[test] + fn nfr3_a_prior_row_without_screen_origin_gives_a_row_without_it() { + let mut row = prior(); + let mut report = PortReport::default(); + leave_out_screen_origin(&mut row, &mut report); + assert!(row["build"]["screen_origin"].is_null()); + assert_eq!(report.left_out.len(), 1); + assert!(report.review.is_empty()); + // A row that has it keeps the place it was found at. + row["build"]["screen_origin"] = json!(["0x101400", "5657"]); + leave_out_screen_origin(&mut row, &mut report); + assert_eq!(row["build"]["screen_origin"][0], "0x101400"); + } + + #[test] + fn nfr6_a_new_row_records_the_class_names_of_the_tables_it_placed() { + let mut prior = verified_row(); + // A name the prior row recorded is not carried: only names confirmed now are written. + prior[CLASSES] = json!({"build.toolbar.stale": "Stale"}); + let classes = BTreeMap::from([(0x101000, "BrowserFrameViewWin".to_owned())]); + let bytes = module(&code_refs(&shifted_code(0x2000, 0x44))); + let pe = PeFile::parse(&bytes).unwrap(); + let (row, report) = + find_candidate(&prior, &prints_of_prior(), None, &classes, "1.0.0.3", &pe); + assert!(report.review.is_empty(), "{:?}", report.review); + assert_eq!( + row[CLASSES], + json!({"frame.frame_view_vtable": "BrowserFrameViewWin"}) + ); + // With no confirmed name the row records none. + let (row, _) = find_candidate( + &prior, + &prints_of_prior(), + None, + &BTreeMap::new(), + "1.0.0.3", + &pe, + ); + assert!(row.get(CLASSES).is_none()); + } + + #[test] + fn nfr6_every_address_of_a_row_is_known_by_its_path() { + let paths = address_paths(&verified_row()); + assert!(paths.contains(&("frame.frame_view_vtable".to_owned(), 0x101000))); + assert!(paths.contains(&("build.toolbar.paint".to_owned(), 0x101300))); + assert!(paths.contains(&("build.tabs.signatures[0][0]".to_owned(), 0x101300))); + // Offsets and the absent marker are not addresses. + assert!(!paths.iter().any(|(path, _)| path.ends_with("view_layer"))); + assert!(!paths.iter().any(|(path, _)| path.ends_with("missing"))); + } + + #[test] + fn nfr6_a_made_row_is_scored_value_by_value_against_the_verified_row() { + let (made, report) = candidate(&prints_of_prior(), &shifted_code(0x2000, 0x44), "1.0.0.3"); + let mut verified = made.clone(); + let score = score_row(&made, &report, &verified); + assert_eq!(score.wrong(), 0); + assert!(score.features[WINDOW].equal > 0); + // A value found at another place than the verified one is wrong. + verified["build"]["toolbar"]["paint"] = json!("0x103310"); + let score = score_row(&made, &report, &verified); + assert_eq!(score.wrong(), 1, "{:?}", score.features); + let wrong: Vec<&String> = score.features.values().flat_map(|f| &f.different).collect(); + assert!( + wrong[0].starts_with("build.toolbar.paint: made"), + "{wrong:?}" + ); + // A value the made row does not hold is not wrong. + let mut made_less = made.clone(); + made_less["build"]["toolbar"] + .as_object_mut() + .unwrap() + .remove("paint"); + let score = score_row(&made_less, &report, &verified); + assert_eq!(score.wrong(), 0); + // A feature the made row turns off is not compared at all. + let mut off = made.clone(); + off[crate::row_features::SWITCHED_OFF] = json!([{"feature": "split view", "values": []}]); + let mut split = verified.clone(); + split["build"]["split"] = json!({"supported": true, "outline_member": "0x40"}); + off["build"]["split"] = json!({"supported": false, "outline_member": "0x0"}); + let score = score_row(&off, &report, &split); + assert!(score.features["split view"].off); + assert!(score.features["split view"].different.is_empty()); + } } diff --git a/rivet/platform/windows/src/row_recipe.rs b/rivet/platform/windows/src/row_recipe.rs index 327383c..ee91f92 100644 --- a/rivet/platform/windows/src/row_recipe.rs +++ b/rivet/platform/windows/src/row_recipe.rs @@ -1,7 +1,7 @@ //! Reads the values of a row that are not addresses from the new build itself. //! -//! A row holds member offsets, table slots and object sizes as well as addresses. They have no -//! public symbols, and a new milestone moves them without notice: Chromium 157 moved many view +//! A row holds member offsets, table slots and object sizes as well as addresses. No name in the +//! module tells them, and a new milestone moves them without notice: Chromium 157 moved many view //! members by 8 bytes and every frame view slot above one removed virtual by one. A port that //! copies them gives a row that looks complete and is wrong. So each such value has recipes that //! read it from the new build's own instructions and tables. A value is taken only when every @@ -415,11 +415,86 @@ pub fn derive_row(build: &mut Value, pe: &PeFile) -> Derived { derived } +/// Values that follow from another value of the row by the member order of a public Chromium +/// class, as (path, the value it follows, the distance). `ui/views/view.h` declares `parent_`, +/// then `children_`, then the members up to `bounds_`, 0x30 bytes after `parent_`. The runtime +/// takes them the same way, so they are written from the value they follow and never carried. +pub const FOLLOWS: &[(&str, &str, i64)] = &[ + ("app_window.view_parent", "layout.view_bounds_offset", -0x30), + ("app_window.view_children", "layout.view_bounds_offset", -0x28), +]; + +/// Writes each value of `FOLLOWS` the row has from the value it follows. +pub fn follow(build: &mut Value) -> Vec { + let mut read = Vec::new(); + for (path, from, by) in FOLLOWS { + let Some(base) = number(at_path(build, from)).filter(|base| *base >= 0x30) else { + continue; + }; + let (parent, key) = path.split_once('.').expect("a path has a parent"); + let Some(old) = number(&build[parent][key]) else { + continue; + }; + let value = (base as i64 + by) as u64; + let is_string = build[parent][key].is_string(); + build[parent][key] = if is_string { + Value::String(format!("{value:#x}")) + } else { + json!(value) + }; + read.push(if old == value { + format!("{path}: {value:#x}, from {from}") + } else { + format!("{path}: {old:#x} -> {value:#x}, from {from}") + }); + } + read +} + +/// Values Rivet chooses rather than reads from a build, such as how long it waits before it opens +/// a new tab, how far its toolbar starts after the browser view's edge to leave room for Rivet's +/// toggle button, and the DIPs it adds to the top inset it reports. The row carries them for the +/// runtime, so they are kept from the prior row. +pub const SETTINGS: &[&str] = &[ + "tabs.new_tab_delay_ms", + "vertical.toolbar_leading", + "layout.top_inset_adjust", +]; + +/// Lists of `[id, colour]` pairs: the id is a value of the build, and the colour is Rivet's choice. +pub const ID_COLOUR_PAIRS: &[&str] = &["toolbar.dev.colours"]; + +/// The values of a row that are Rivet's settings rather than facts about its build. +pub fn settings(build: &Value) -> Vec { + let mut out: Vec = SETTINGS + .iter() + .filter(|path| number(at_path(build, path)).is_some()) + .map(|path| (*path).to_owned()) + .collect(); + for list in ID_COLOUR_PAIRS { + if let Some(pairs) = at_path(build, list).as_array() { + out.extend((0..pairs.len()).map(|index| format!("{list}[{index}][1]"))); + } + } + out +} + /// Every number of a row below the address range that no recipe reads: member offsets, slots, -/// sizes, ids and constants that a port can only carry over. +/// sizes, ids and constants that a port can only carry over. Rivet's own settings and Chromium's +/// public command ids are not listed. pub fn without_recipe(build: &Value) -> Vec { fn walk(path: &str, value: &Value, out: &mut Vec) { match value { + Value::Array(items) + if items.len() == 2 + && path + .rsplit_once('[') + .is_some_and(|(list, _)| ID_COLOUR_PAIRS.contains(&list)) => + { + if number(&items[0]).is_some_and(|id| id < LOWEST_ADDRESS) { + out.push(format!("{path}[0]")); + } + } Value::Object(object) => { for (key, item) in object { let next = if path.is_empty() { @@ -459,6 +534,9 @@ pub fn without_recipe(build: &Value) -> Vec { path != "version" && !path.starts_with("version[") && !FIELDS.iter().any(|field| field.path == path) + && !SETTINGS.contains(&path.as_str()) + && !crate::command_ids::ROW_PATHS.contains(&path.as_str()) + && !FOLLOWS.iter().any(|(follows, _, _)| follows == path) }); out } @@ -637,4 +715,54 @@ mod tests { vec!["layout.caption_container_offset", "layout.frame_ids"] ); } + + #[test] + fn nfr6_a_views_parent_and_children_follow_its_bounds() { + let mut build = json!({ + "layout": {"view_bounds_offset": "0x1c0"}, + "app_window": {"view_parent": "0x198", "view_children": "0x1a0"}, + }); + assert_eq!(without_recipe(&build), vec!["layout.view_bounds_offset"]); + let read = follow(&mut build); + assert_eq!(build["app_window"]["view_parent"], "0x190"); + assert_eq!(build["app_window"]["view_children"], "0x198"); + assert_eq!(read.len(), 2, "{read:?}"); + // A row without the app window part gets none. + let mut bare = json!({"layout": {"view_bounds_offset": "0x1c8"}, "app_window": null}); + assert!(follow(&mut bare).is_empty()); + assert!(bare["app_window"].is_null()); + } + + #[test] + fn nfr6_rivet_settings_are_not_values_of_the_build() { + let build = json!({ + "tabs": {"new_tab_delay_ms": 0, "tab_contents": "0x0"}, + "toolbar": {"dev": {"colours": [[316, 4280953905u32], [2082, 0]], "console": 40005}}, + }); + assert_eq!( + without_recipe(&build), + vec![ + "tabs.tab_contents", + "toolbar.dev.colours[0][0]", + "toolbar.dev.colours[1][0]" + ] + ); + let chosen = json!({ + "vertical": {"toolbar_leading": 26, "toolbar_in_view": "0x60"}, + "layout": {"top_inset_adjust": 0}, + }); + assert_eq!(without_recipe(&chosen), vec!["vertical.toolbar_in_view"]); + assert_eq!( + settings(&chosen), + vec!["vertical.toolbar_leading", "layout.top_inset_adjust"] + ); + assert_eq!( + settings(&build), + vec![ + "tabs.new_tab_delay_ms", + "toolbar.dev.colours[0][1]", + "toolbar.dev.colours[1][1]" + ] + ); + } } diff --git a/rivet/platform/windows/src/row_resolve.rs b/rivet/platform/windows/src/row_resolve.rs new file mode 100644 index 0000000..dd9d19a --- /dev/null +++ b/rivet/platform/windows/src/row_resolve.rs @@ -0,0 +1,1713 @@ +//! Finds, in a new build, the functions and class tables that the prior row's fingerprints do not +//! pin down to one place. +//! +//! Each such address is looked for by several independent methods, and a place is taken only when +//! two different methods name the same place and no method names another. The methods read the +//! prior build, whose row a person verified, next to the new build: +//! +//! - calls into it: a function known in both builds calls or loads it from the same spot of its +//! body in both builds; +//! - its calls: in the prior build it calls functions that their own fingerprints find in the new +//! build, and exactly one candidate makes the same calls from the same spots; +//! - its table slot: a class table known in both builds holds it, at the slot that lines up once +//! the slots around it are compared, since a milestone can add or remove a virtual function; +//! - its slot shift: for a virtual function of a views class, the table of a second views class +//! lines the same slot up by the same shift. Every views class derives from `views::View`, whose +//! virtual functions keep one slot in all their tables (`ui/views/view.h`), so a second table +//! confirms the slot number apart from the first; +//! - a text it uses: it refers to a text the new build holds once, and exactly one function there +//! refers to that text; +//! - for a class table, references to it (a function known in both builds loads it from the same +//! spot) and its slots (exactly one candidate holds the same functions in the same slots); +//! - for the class table of a views class, its class name: `row_class` reads the table from the +//! name the class's `ui::metadata` holds, and the same reading must give the prior table in the +//! prior build; +//! - a known caller: a function known in both builds calls it in the prior build, and of the places +//! its fingerprint matched, the caller's new body calls exactly one. Public source tells which +//! function calls which (a frame view's `OnPaint` calls `gfx::Canvas::DrawColor`), and the call +//! is a fact of the caller apart from the fingerprint's bytes, so with the fingerprint it is two +//! methods. Each place a virtual function's table slot settles is a known caller in the next +//! round, so drawing helpers follow the functions they are called from; +//! - its command id: for a function a browser command runs first, the case of the command's public +//! id in the browser's command switch, read by `row_command` the same way in both builds; +//! - its fingerprint: a place a class table or a command id gives that is one of the places the +//! fingerprint matched. Those methods do not search among the fingerprint's places, so the two +//! agree independently. +//! +//! Anything else stays unresolved with its reason, and its row goes to a person. + +use std::collections::{BTreeMap, BTreeSet, HashMap}; +use std::fmt; +use std::rc::Rc; + +use crate::chrome_build::PeFile; +use crate::row_port::{code_print, find_prints, find_table, whole_function, x64}; + +/// How many bytes of a function body are read at most. +const BODY_LIMIT: u32 = 0x4000; +/// How far a function the exception table does not list, a leaf, is read at most. +const LEAF_LIMIT: u32 = 0x200; +/// How many instructions before a site name it within its body. +const CONTEXT: usize = 3; +/// How many levels of calls are followed from the functions known in both builds. +const FOLLOW_DEPTH: usize = 3; +/// How many functions are followed at most. +const FOLLOW_LIMIT: usize = 4000; +/// How many slots of a class table are read at most. +const TABLE_SLOTS: u32 = 512; +/// How many slots of a class table are compared to tell one table from another. +const TABLE_SEEN: u32 = 0x80; +/// How many slots on each side of a slot are compared to line two tables up. +const ALIGN_REACH: u32 = 6; +/// How far a milestone may move a slot. +const ALIGN_SHIFT: i64 = 2; +/// How many slots must agree for two tables, or two stretches of them, to be the same. +const ALIGN_LEAST: usize = 3; +/// The shortest text taken as a sign of the function that uses it. +const TEXT_LEAST: usize = 8; +/// The longest text read. +const TEXT_MOST: usize = 256; +/// How many candidates a function with no fingerprint match may have. +const CANDIDATES_MOST: usize = 256; +/// How many times the methods run again with what the last round found. +const ROUNDS: usize = 4; + +/// One instruction of a function body, as far as the methods need it. +#[derive(Debug, Clone)] +pub struct Step { + pub rva: u32, + /// The bytes with the relative value, displacement and immediate cleared. + pub skeleton: Vec, + /// Where a direct call or jump goes. + pub call: Option, + /// What a RIP-relative operand points at: data, a table or a function passed as a callback. + pub reference: Option, + /// A displacement from a register, as `[rcx + 0x558]` holds. + pub displacement: Option, + pub immediate: Option, + /// A call or jump through memory, as `call [rax + 0x2b8]` calls a table slot. + pub indirect: bool, + /// Its operand is the stack: a local variable, or the frame's size. Neither is a value of a row. + pub stack: bool, +} + +/// Whether a memory operand's base is the stack or frame pointer, which hold locals, not members. +fn stack_based(raw: &[u8], displacement: Option<(usize, usize)>) -> bool { + let Some((offset, size)) = displacement else { + return false; + }; + let opcode = opcode_at(raw); + // REX.B names r12 or r13 where the base field says rsp or rbp. + let extended = opcode > 0 && raw[opcode - 1] & 0xF0 == 0x40 && raw[opcode - 1] & 1 != 0; + let mode = if size == 1 { 1 } else { 2 }; + let before = offset.checked_sub(1).and_then(|at| raw.get(at)).copied(); + let base = match before { + // The ModRM byte itself, with no SIB byte after it. + Some(modrm) if modrm >> 6 == mode && modrm & 7 != 4 => modrm & 7, + // A SIB byte, whose low bits name the base. + Some(sib) => sib & 7, + None => return false, + }; + !extended && matches!(base, 4 | 5) +} + +fn signed(bytes: &[u8], field: (usize, usize)) -> Option { + let (offset, size) = field; + let part = bytes.get(offset..offset + size)?; + Some(match size { + 1 => i64::from(part[0] as i8), + 2 => i64::from(i16::from_le_bytes([part[0], part[1]])), + 4 => i64::from(i32::from_le_bytes([part[0], part[1], part[2], part[3]])), + 8 => i64::from_le_bytes(part.try_into().ok()?), + _ => return None, + }) +} + +/// Where the opcode of an instruction is, past its prefixes. +fn opcode_at(raw: &[u8]) -> usize { + let mut at = 0; + while raw.get(at).is_some_and(|byte| { + matches!( + byte, + 0x66 | 0xF2 | 0xF3 | 0xF0 | 0x26 | 0x2E | 0x36 | 0x3E | 0x64 | 0x65 + ) + }) { + at += 1; + } + if raw.get(at).is_some_and(|byte| byte & 0xF0 == 0x40) { + at += 1; + } + at +} + +fn step(raw: &[u8], instruction: &x64::Instruction, rva: u32) -> Step { + let next = i64::from(rva) + instruction.length as i64; + let opcode = opcode_at(raw); + let first = raw.get(opcode).copied(); + let second = raw.get(opcode + 1).copied(); + let branch = matches!(first, Some(0x70..=0x7F | 0xEB | 0xE0..=0xE3 | 0xE8 | 0xE9)) + || (first == Some(0x0F) && matches!(second, Some(0x80..=0x8F))); + let mut call = None; + let mut reference = None; + if let Some(field) = instruction.relative { + let target = signed(raw, field) + .map(|distance| next + distance) + .and_then(|target| u32::try_from(target).ok()); + if matches!(first, Some(0xE8 | 0xE9)) { + call = target; + } else if !branch { + reference = target; + } + } + Step { + rva, + skeleton: x64::skeleton(raw, instruction), + call, + reference, + displacement: instruction + .displacement + .and_then(|field| signed(raw, field)), + immediate: instruction.immediate.and_then(|field| signed(raw, field)), + indirect: first == Some(0xFF) + && second.is_some_and(|modrm| matches!((modrm >> 3) & 7, 2 | 4)), + // `add rsp, n` and `sub rsp, n` size the frame. + stack: stack_based(raw, instruction.displacement) + || (matches!(first, Some(0x81 | 0x83)) && matches!(second, Some(0xC4 | 0xEC))), + } +} + +/// Reads a function body from its start, as far as its exception table entry or, for a leaf, its +/// padding, and stops at the first instruction it cannot read. +fn read_body(pe: &PeFile, ranges: &BTreeMap, start: u32) -> Vec { + if !pe.is_code(start) { + return vec![]; + } + let listed = ranges.get(&start).copied(); + let length = match listed { + Some(end) => end.saturating_sub(start).min(BODY_LIMIT), + None => LEAF_LIMIT, + } as usize; + let Some(bytes) = pe.read(start, length).or_else(|| pe.read(start, 16)) else { + return vec![]; + }; + let mut steps = Vec::new(); + let mut at = 0; + while at < bytes.len() { + let rest = &bytes[at..]; + if listed.is_none() && rest.starts_with(&[0xCC, 0xCC]) { + break; + } + let Some(instruction) = x64::decode_body(rest) else { + break; + }; + if instruction.length == 0 || instruction.length > rest.len() { + break; + } + steps.push(step(&rest[..instruction.length], &instruction, start + at as u32)); + at += instruction.length; + } + steps +} + +/// One build: its module, its function ranges and the bodies read so far. +pub struct Side<'a> { + pub pe: &'a PeFile<'a>, + ranges: BTreeMap, + bodies: HashMap>>, +} + +impl<'a> Side<'a> { + pub fn new(pe: &'a PeFile<'a>) -> Self { + Self { + pe, + ranges: pe.function_ranges(), + bodies: HashMap::new(), + } + } + + pub fn body(&mut self, start: u32) -> Rc> { + if let Some(body) = self.bodies.get(&start) { + return Rc::clone(body); + } + let body = Rc::new(read_body(self.pe, &self.ranges, start)); + self.bodies.insert(start, Rc::clone(&body)); + body + } + + /// The start of the function part that holds an address. + fn enclosing(&self, rva: u32) -> Option { + let (start, end) = self.ranges.range(..=rva).next_back()?; + (rva < *end).then_some(*start) + } + + fn is_start(&self, rva: u32) -> bool { + self.ranges.contains_key(&rva) + } +} + +/// Names the instruction at `index` by the instructions around it, as they stay when code moves. +pub fn context(steps: &[Step], index: usize, before: usize, after: usize) -> Vec { + let first = index.saturating_sub(before); + let last = (index + after).min(steps.len().saturating_sub(1)); + let mut key = vec![(index - first) as u8]; + for step in &steps[first..=last] { + key.push(step.skeleton.len() as u8); + key.extend_from_slice(&step.skeleton); + } + key +} + +/// Where each call or reference of a body points, named by the instructions before it, in order. +fn named_sites(steps: &[Step]) -> HashMap, Vec> { + let mut sites: HashMap, Vec> = HashMap::new(); + for (index, step) in steps.iter().enumerate() { + if let Some(target) = step.call.or(step.reference) { + sites + .entry(context(steps, index, CONTEXT, 0)) + .or_default() + .push(target); + } + } + sites +} + +/// Pairs what the two bodies of one function point at: sites named the same way, which each body +/// has the same number of, in order. +fn pair_sites(old: &HashMap, Vec>, new: &HashMap, Vec>) -> Vec<(u32, u32)> { + let mut pairs = Vec::new(); + for (key, targets) in old { + if let Some(others) = new.get(key).filter(|others| others.len() == targets.len()) { + pairs.extend(targets.iter().copied().zip(others.iter().copied())); + } + } + pairs.sort_unstable(); + pairs +} + +/// Addresses of the prior build with their places in the new one, found by following the calls +/// and references of functions known in both. An address reached two ways that disagree has none. +#[derive(Debug, Default)] +pub struct Followed { + pub places: BTreeMap, + conflicts: BTreeSet, +} + +impl Followed { + /// Records a pair, and says whether it is new. + fn add(&mut self, old: u32, new: u32) -> bool { + if self.conflicts.contains(&old) { + return false; + } + match self.places.get(&old) { + Some(place) if *place == new => false, + Some(_) => { + self.places.remove(&old); + self.conflicts.insert(old); + false + } + None => { + self.places.insert(old, new); + true + } + } + } + + fn get(&self, old: u32) -> Option { + self.places.get(&old).copied() + } +} + +/// Follows calls and references from the functions known in both builds, a few levels deep. +pub fn follow(old: &mut Side, new: &mut Side, known: &BTreeMap) -> Followed { + let mut followed = Followed::default(); + let mut frontier: Vec<(u32, u32)> = known + .iter() + .map(|(from, to)| (*from, *to)) + .filter(|(from, to)| old.pe.is_code(*from) && new.pe.is_code(*to)) + .collect(); + let mut visited = BTreeSet::new(); + for _ in 0..FOLLOW_DEPTH { + let mut next = Vec::new(); + for (from_old, from_new) in frontier { + if visited.len() >= FOLLOW_LIMIT || !visited.insert(from_old) { + continue; + } + let old_sites = named_sites(&old.body(from_old)); + let new_sites = named_sites(&new.body(from_new)); + for (to_old, to_new) in pair_sites(&old_sites, &new_sites) { + if known.contains_key(&to_old) { + continue; + } + if followed.add(to_old, to_new) && old.pe.is_code(to_old) && new.pe.is_code(to_new) + { + next.push((to_old, to_new)); + } + } + } + frontier = next; + } + followed +} + +/// The calls a function makes, as its prior body names them, to functions whose place in the new +/// build is known: `(site, rank among sites named the same, how many such sites, new place)`. +fn expected_calls( + old: &mut Side, + target: u32, + anchors: &BTreeMap, +) -> Vec<(Vec, usize, usize, u32)> { + let body = old.body(target); + let mut expected = Vec::new(); + for (key, callees) in named_sites(&body) { + for (rank, callee) in callees.iter().enumerate() { + if let Some(place) = anchors.get(callee) { + expected.push((key.clone(), rank, callees.len(), *place)); + } + } + } + expected.sort(); + expected +} + +/// The one candidate that makes the expected calls from the same spots, and contradicts none. +fn by_its_calls( + new: &mut Side, + expected: &[(Vec, usize, usize, u32)], + candidates: &[u32], +) -> Vec { + if expected.is_empty() { + return vec![]; + } + let least = expected.len().min(2); + let mut passing = Vec::new(); + for candidate in candidates { + let theirs = named_sites(&new.body(*candidate)); + let (mut hits, mut misses) = (0, 0); + for (key, rank, count, place) in expected { + match theirs.get(key) { + Some(found) if found.len() == *count => { + if found[*rank] == *place { + hits += 1; + } else { + misses += 1; + } + } + _ => {} + } + } + if misses == 0 && hits >= least { + passing.push(*candidate); + } + } + // Two candidates that both pass cannot be told apart: neither is an answer. + if passing.len() == 1 { + passing + } else { + vec![] + } +} + +/// The functions of a build that call any of `wanted` directly, by the function part each call is in. +fn calls_into(side: &Side, wanted: &BTreeSet) -> BTreeMap> { + let mut found: BTreeMap> = BTreeMap::new(); + if wanted.is_empty() { + return found; + } + for (base, bytes, is_code) in side.pe.sections() { + if !is_code { + continue; + } + for at in 0..bytes.len().saturating_sub(4) { + if !matches!(bytes[at], 0xE8 | 0xE9) { + continue; + } + let distance = i32::from_le_bytes([bytes[at + 1], bytes[at + 2], bytes[at + 3], bytes[at + 4]]); + let site = base + at as u32; + let Ok(target) = u32::try_from(i64::from(site) + 5 + i64::from(distance)) else { + continue; + }; + if wanted.contains(&target) { + if let Some(start) = side.enclosing(site) { + found.entry(target).or_default().insert(start); + } + } + } + } + found +} + +/// The functions the prior bodies of `targets` call that their own fingerprints find in exactly one +/// place of the new build. They do not depend on the calls followed from known functions. +fn callee_anchors(old: &mut Side, new: &Side, targets: &[u32]) -> BTreeMap { + let mut prints = BTreeMap::new(); + for target in targets { + let body = old.body(*target); + for step in body.iter() { + let callee = step + .call + .or(step.reference.filter(|place| old.pe.is_code(*place))); + if let Some(callee) = callee { + if let Some(print) = code_print(old.pe, callee) { + prints.insert(callee, print); + } + } + } + } + let mut anchors = BTreeMap::new(); + for (callee, places) in find_prints(new.pe, &prints) { + // A print taken at a function start counts only function starts, as for the row itself. + let places: Vec = places + .into_iter() + .filter(|place| !old.is_start(callee) || new.is_start(*place)) + .collect(); + if let [only] = places.as_slice() { + anchors.insert(callee, *only); + } + } + anchors +} + +/// A function as the slots of a class table show it: its fingerprint, or the whole of a short one. +fn entry_shape(pe: &PeFile, function: u32) -> Option> { + code_print(pe, function).or_else(|| whole_function(pe, function)) +} + +fn same_function(old: &Side, new: &Side, known: &BTreeMap, from: u32, to: u32) -> bool { + if let Some(place) = known.get(&from) { + return *place == to; + } + match (entry_shape(old.pe, from), entry_shape(new.pe, to)) { + (Some(left), Some(right)) => left == right, + _ => false, + } +} + +/// The function in a slot of a class table, or `None` past the table's end. +fn entry(pe: &PeFile, table: u32, slot: i64) -> Option { + let slot = u32::try_from(slot).ok()?; + let at = table.checked_add(slot.checked_mul(8)?)?; + pe.pointer_rva(at).filter(|function| pe.is_code(*function)) +} + +/// How far slot `slot` of the old table moved in the new one, judged by the slots around it. +fn line_up( + old: &Side, + new: &Side, + known: &BTreeMap, + tables: (u32, u32), + slot: u32, +) -> Option { + let mut scores = Vec::new(); + for shift in -ALIGN_SHIFT..=ALIGN_SHIFT { + let mut score = 0; + for near in slot.saturating_sub(ALIGN_REACH)..=slot + ALIGN_REACH { + if near == slot { + continue; + } + let from = entry(old.pe, tables.0, i64::from(near)); + let to = entry(new.pe, tables.1, i64::from(near) + shift); + if let (Some(from), Some(to)) = (from, to) { + if same_function(old, new, known, from, to) { + score += 1; + } + } + } + scores.push((score, shift)); + } + scores.sort_by(|left, right| right.0.cmp(&left.0)); + match scores.as_slice() { + [(top, shift), (second, _), ..] if *top >= ALIGN_LEAST && top > second => Some(*shift), + _ => None, + } +} + +/// The places the class tables known in both builds give a function by its slot. +fn by_table_slot( + old: &Side, + new: &Side, + known: &BTreeMap, + tables: &[(u32, u32)], + target: u32, +) -> BTreeSet { + let mut found = BTreeSet::new(); + for pair in tables { + for slot in 0..TABLE_SLOTS { + let Some(function) = entry(old.pe, pair.0, i64::from(slot)) else { + break; + }; + if function != target { + continue; + } + if let Some(shift) = line_up(old, new, known, *pair, slot) { + if let Some(place) = entry(new.pe, pair.1, i64::from(slot) + shift) { + found.insert(place); + } + } + } + } + found +} + +/// The places a views class table gives a function by its slot, where the table of a second views +/// class, known in both builds, lines the same slot up by the same shift. `views` names the prior +/// build's tables of views classes. A slot of `views::View` is the same slot in every views class +/// table, and one of a derived class is the same slot in that class's subclasses, so a slot that two +/// tables line up alike is a slot number found twice, by tables found apart. +fn by_slot_shift( + old: &Side, + new: &Side, + known: &BTreeMap, + tables: &[(u32, u32)], + views: &BTreeSet, + target: u32, +) -> BTreeSet { + let mut found = BTreeSet::new(); + for pair in tables.iter().filter(|pair| views.contains(&pair.0)) { + for slot in 0..TABLE_SLOTS { + let Some(function) = entry(old.pe, pair.0, i64::from(slot)) else { + break; + }; + if function != target { + continue; + } + let Some(shift) = line_up(old, new, known, *pair, slot) else { + continue; + }; + let confirmed = tables + .iter() + .filter(|other| other.0 != pair.0 && views.contains(&other.0)) + .any(|other| line_up(old, new, known, *other, slot) == Some(shift)); + if confirmed { + if let Some(place) = entry(new.pe, pair.1, i64::from(slot) + shift) { + found.insert(place); + } + } + } + } + found +} + +/// The places the known callers of `target` give it among `candidates`: for each function that +/// calls it in the prior build and whose new place is known, the one candidate its new body calls, +/// when it calls exactly one. `callers` holds the prior build's callers of each wanted function. +fn by_known_callers( + new: &mut Side, + known: &BTreeMap, + callers: &BTreeSet, + candidates: &[u32], +) -> BTreeSet { + let mut found = BTreeSet::new(); + if candidates.is_empty() { + return found; + } + for caller in callers { + let Some(place) = known.get(caller).filter(|place| new.pe.is_code(**place)) else { + continue; + }; + let called: BTreeSet = new + .body(*place) + .iter() + .filter_map(|step| step.call) + .filter(|callee| candidates.contains(callee)) + .collect(); + if let [only] = called.into_iter().collect::>()[..] { + found.insert(only); + } + } + found +} + +/// The candidate tables whose slots hold the same functions as the old table, and how many agree. +fn by_slots( + old: &Side, + new: &Side, + known: &BTreeMap, + target: u32, + candidates: &[u32], +) -> Vec { + let mut passing = Vec::new(); + for candidate in candidates { + let (mut seen, mut agreed) = (0, 0); + for slot in 0..TABLE_SEEN { + let Some(function) = entry(old.pe, target, i64::from(slot)) else { + break; + }; + seen += 1; + let agrees = (-1..=1).any(|shift| { + entry(new.pe, *candidate, i64::from(slot) + shift) + .is_some_and(|place| same_function(old, new, known, function, place)) + }); + if agrees { + agreed += 1; + } + } + if agreed >= ALIGN_LEAST && agreed * 2 >= seen { + passing.push(*candidate); + } + } + passing +} + +/// Tables of the new build that hold, near the same slot, a function the old table holds and +/// whose place is known. +fn table_candidates(old: &Side, new: &Side, known: &BTreeMap, target: u32) -> Vec { + let mut found = BTreeSet::new(); + let mut tried = 0; + for slot in 0..TABLE_SEEN { + let Some(function) = entry(old.pe, target, i64::from(slot)) else { + break; + }; + let Some(place) = known.get(&function).filter(|place| new.pe.is_code(**place)) else { + continue; + }; + for shifted in slot.saturating_sub(1)..=slot + 1 { + found.extend(find_table(new.pe, shifted, *place)); + } + tried += 1; + if tried == 2 { + break; + } + } + found.into_iter().take(CANDIDATES_MOST).collect() +} + +/// A table a function known in both builds loads from the same spot, or a place a few slots into +/// one, which keeps its distance from the table's start. +fn by_reference(followed: &Followed, target: u32) -> Option { + let (from, to) = followed + .places + .range(target.saturating_sub(0x40)..=target) + .next_back()?; + to.checked_add(target - from) +} + +/// The text at an address of a data section, with its terminating zero, when it is plain text. +fn text_at(pe: &PeFile, rva: u32) -> Option> { + if pe.is_code(rva) { + return None; + } + let window = pe + .read(rva, TEXT_MOST + 1) + .or_else(|| pe.read(rva, TEXT_LEAST + 1))?; + let end = window.iter().position(|byte| *byte == 0)?; + let text = &window[..end]; + if text.len() < TEXT_LEAST || !text.iter().all(|byte| (0x20..0x7F).contains(byte)) { + return None; + } + Some(window[..=end].to_vec()) +} + +/// For each text, the functions of a build that load it, when the build holds it exactly once. +fn functions_using(side: &Side, texts: &BTreeSet>) -> BTreeMap, BTreeSet> { + let mut by_head: HashMap<[u8; TEXT_LEAST], Vec<&Vec>> = HashMap::new(); + let mut first = [false; 256]; + for text in texts { + let head: [u8; TEXT_LEAST] = text[..TEXT_LEAST].try_into().expect("checked length"); + first[head[0] as usize] = true; + by_head.entry(head).or_default().push(text); + } + let mut places: BTreeMap<&Vec, Vec> = BTreeMap::new(); + for (base, bytes, is_code) in side.pe.sections() { + if is_code { + continue; + } + for at in 0..bytes.len().saturating_sub(TEXT_LEAST) { + if !first[bytes[at] as usize] { + continue; + } + let head: [u8; TEXT_LEAST] = bytes[at..at + TEXT_LEAST].try_into().expect("in range"); + for text in by_head.get(&head).map_or(&[][..], Vec::as_slice) { + if bytes[at..].starts_with(text) { + places.entry(*text).or_default().push(base + at as u32); + } + } + } + } + let at_address: HashMap> = places + .iter() + .filter_map(|(text, found)| match found.as_slice() { + [only] => Some((*only, *text)), + _ => None, + }) + .collect(); + let mut users: BTreeMap, BTreeSet> = BTreeMap::new(); + for (base, bytes, is_code) in side.pe.sections() { + if !is_code { + continue; + } + // `lea` or `mov` of a register from [rip + disp32]: REX, 8D or 8B, a ModRM of 00 reg 101. + for at in 3..bytes.len().saturating_sub(4) { + if bytes[at - 1] & 0xC7 != 0x05 + || !matches!(bytes[at - 2], 0x8D | 0x8B) + || bytes[at - 3] & 0xF0 != 0x40 + { + continue; + } + let distance = i32::from_le_bytes([bytes[at], bytes[at + 1], bytes[at + 2], bytes[at + 3]]); + let site = base + at as u32; + let Ok(target) = u32::try_from(i64::from(site) + 4 + i64::from(distance)) else { + continue; + }; + if let Some(text) = at_address.get(&target) { + if let Some(start) = side.enclosing(site) { + users.entry((*text).clone()).or_default().insert(start); + } + } + } + } + users +} + +/// Which method gave a place. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub enum Method { + CallsInto, + ItsCalls, + TableSlot, + Text, + References, + Slots, + ClassName, + Fingerprint, + SlotShift, + Command, + KnownCaller, +} + +impl fmt::Display for Method { + fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result { + out.write_str(match self { + Method::CallsInto => "calls into it", + Method::ItsCalls => "its calls", + Method::TableSlot => "its table slot", + Method::Text => "a text it uses", + Method::References => "references to it", + Method::Slots => "its slots", + Method::ClassName => "its class name", + Method::Fingerprint => "its fingerprint", + Method::SlotShift => "its slot shift", + Method::Command => "its command id", + Method::KnownCaller => "a known caller", + }) + } +} + +fn names(methods: &BTreeSet) -> String { + methods + .iter() + .map(Method::to_string) + .collect::>() + .join(" and ") +} + +/// Takes a place only when two different methods name it, no method names another, and it is one +/// of the fingerprint's places where there are any. +pub fn decide( + answers: &[(Method, u32)], + candidates: Option<&[u32]>, +) -> Result<(u32, BTreeSet), String> { + let places: BTreeSet = answers.iter().map(|(_, place)| *place).collect(); + let methods: BTreeSet = answers.iter().map(|(method, _)| *method).collect(); + match places.iter().copied().collect::>().as_slice() { + [] => Err("no method found it".to_owned()), + [place] => { + if candidates.is_some_and(|candidates| !candidates.contains(place)) { + Err(format!( + "{} found {place:#x}, which is not one of its fingerprint's places", + names(&methods) + )) + } else if methods.len() < 2 { + Err(format!( + "only {} found it, at {place:#x}, and a second method is needed", + names(&methods) + )) + } else { + Ok((*place, methods)) + } + } + _ => { + let mut said: Vec = answers + .iter() + .map(|(method, place)| format!("{method} {place:#x}")) + .collect(); + said.sort(); + said.dedup(); + Err(format!("the methods disagree: {}", said.join(", "))) + } + } +} + +/// What an address of the prior row is. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Kind { + Function, + Table, +} + +/// An address the fingerprints did not pin down, with the places they matched, if any. +#[derive(Debug, Clone)] +pub struct Wanted { + pub kind: Kind, + pub candidates: Option>, + /// For a class table, the views class whose name its metadata holds in the prior build. + pub class: Option, + /// For a function a browser command runs first, the command's public id. + pub command: Option, + /// Places the fingerprint matched that the exception table does not list as function starts. + /// They name no place alone, but a place another method finds among them is one the + /// fingerprint agrees with. + pub loose: Vec, +} + +/// The class table a views class name gives in the new build, once the same reading gives the +/// prior table in the prior build. +pub fn class_place( + old_pe: &PeFile, + new_pe: &PeFile, + target: u32, + class: &str, +) -> Result { + let old = crate::row_class::class_tables(old_pe, class); + if old != BTreeSet::from([target]) { + return Err(format!( + "its class name {class} gives {} tables in the prior build, not the prior table alone", + old.len() + )); + } + let new = crate::row_class::class_tables(new_pe, class); + match new.iter().copied().collect::>().as_slice() { + [only] => Ok(*only), + many => Err(format!( + "its class name {class} gives {} tables in the new build", + many.len() + )), + } +} + +/// What the fingerprint adds to the places a class table gives: agreement when the table gives +/// exactly one place and that place is one of the fingerprint's. +pub fn fingerprint_agrees( + given: &BTreeSet, + candidates: Option<&[u32]>, +) -> Option<(Method, u32)> { + let [only] = given.iter().copied().collect::>()[..] else { + return None; + }; + candidates + .is_some_and(|candidates| candidates.contains(&only)) + .then_some((Method::Fingerprint, only)) +} + +/// What the methods found. +#[derive(Debug, Default)] +pub struct Resolution { + /// The new place of each address two methods agree on. + pub places: BTreeMap, + /// For the report: each place and the methods that found it. + pub settled: Vec, + /// Why each other address has no place. + pub reasons: BTreeMap, + /// Functions of the prior build with their places in the new one, as the calls of known + /// functions pair them, for reading values from the same instructions in both builds. + pub followed: BTreeMap, +} + +/// Looks for each wanted address with every method, in rounds, since a place found in one round +/// is known in the next. `known` holds the places the fingerprints found, `tables` names the +/// prior row's addresses that are class tables, and `views` those of them that are tables of views +/// classes. +pub fn resolve( + old_pe: &PeFile, + new_pe: &PeFile, + known: &BTreeMap, + wanted: &BTreeMap, + tables: &BTreeSet, + views: &BTreeSet, +) -> Resolution { + let mut old = Side::new(old_pe); + let mut new = Side::new(new_pe); + let mut known = known.clone(); + let mut resolution = Resolution::default(); + let functions: Vec = wanted + .iter() + .filter(|(_, want)| want.kind == Kind::Function) + .map(|(target, _)| *target) + .collect(); + + // What does not depend on the known places is read once: the callees found by their own + // fingerprints, the functions that call them, and the texts each function uses. + let anchors = callee_anchors(&mut old, &new, &functions); + let mut texts_of: BTreeMap>> = BTreeMap::new(); + for target in &functions { + let body = old.body(*target); + let texts: BTreeSet> = body + .iter() + .filter_map(|step| step.reference) + .filter_map(|place| text_at(old.pe, place)) + .collect(); + texts_of.insert(*target, texts); + } + let all_texts: BTreeSet> = texts_of.values().flatten().cloned().collect(); + let users = functions_using(&new, &all_texts); + let mut seen_anchors = anchors.clone(); + for (from, to) in &known { + if old.pe.is_code(*from) { + seen_anchors.insert(*from, *to); + } + } + let mut expected = BTreeMap::new(); + let mut called = BTreeSet::new(); + for target in &functions { + let calls = expected_calls(&mut old, *target, &seen_anchors); + called.extend(calls.iter().map(|call| call.3)); + expected.insert(*target, calls); + } + let callers = calls_into(&new, &called); + // The prior build's callers of each wanted function with fingerprint places, for the method of + // a known caller. + let with_candidates: BTreeSet = wanted + .iter() + .filter(|(_, want)| want.kind == Kind::Function && want.candidates.is_some()) + .map(|(target, _)| *target) + .collect(); + let prior_callers = calls_into(&old, &with_candidates); + + // A class name gives a table from the build alone, so it is read once. + let mut by_class = BTreeMap::new(); + let mut class_reasons = BTreeMap::new(); + for (target, want) in wanted { + let Some(class) = want.class.as_deref().filter(|_| want.kind == Kind::Table) else { + continue; + }; + match class_place(old_pe, new_pe, *target, class) { + Ok(place) => { + by_class.insert(*target, place); + } + Err(reason) => { + class_reasons.insert(*target, reason); + } + } + } + + // A command id gives a function from both builds alone, so it is read once. + let mut by_command = BTreeMap::new(); + let commands: Vec<(u32, i64)> = wanted + .iter() + .filter(|(_, want)| want.kind == Kind::Function) + .filter_map(|(target, want)| Some((*target, want.command?))) + .collect(); + if !commands.is_empty() { + let old_switches = crate::row_command::switches(old_pe); + let new_switches = crate::row_command::switches(new_pe); + for (target, id) in commands { + let old = (old_pe, old_switches.as_slice()); + let new = (new_pe, new_switches.as_slice()); + match crate::row_command::command_place_in(old, new, target, id) { + Ok(place) => { + by_command.insert(target, place); + } + Err(reason) => { + class_reasons.insert(target, reason); + } + } + } + } + + let mut pending: BTreeSet = wanted.keys().copied().collect(); + for _ in 0..ROUNDS { + let followed = follow(&mut old, &mut new, &known); + let table_pairs: Vec<(u32, u32)> = known + .iter() + .filter(|(from, _)| tables.contains(from)) + .map(|(from, to)| (*from, *to)) + .collect(); + let mut found_any = false; + for target in pending.clone() { + let want = &wanted[&target]; + let mut answers = Vec::new(); + match want.kind { + Kind::Function => { + if let Some(place) = followed.get(target) { + answers.push((Method::CallsInto, place)); + } + let calls = &expected[&target]; + let candidates: Vec = match &want.candidates { + Some(list) => list.clone(), + None => calls + .iter() + .flat_map(|call| callers.get(&call.3).into_iter().flatten().copied()) + .collect::>() + .into_iter() + .take(CANDIDATES_MOST) + .collect(), + }; + for place in by_its_calls(&mut new, calls, &candidates) { + answers.push((Method::ItsCalls, place)); + } + let slotted = by_table_slot(&old, &new, &known, &table_pairs, target); + answers.extend(fingerprint_agrees(&slotted, want.candidates.as_deref())); + for place in slotted { + answers.push((Method::TableSlot, place)); + } + for place in by_slot_shift(&old, &new, &known, &table_pairs, views, target) { + answers.push((Method::SlotShift, place)); + } + if let Some(place) = by_text(&texts_of[&target], &users) { + answers.push((Method::Text, place)); + } + let from = prior_callers.get(&target); + if let (Some(list), Some(from)) = (&want.candidates, from) { + // A known caller is a call from a function known in both builds, the same + // kind of fact as the calls into it, so it is never counted beside them and + // the fingerprint's places only limit where it may point. + let given = by_known_callers(&mut new, &known, from, list); + for place in given { + let seen = answers + .iter() + .any(|(method, at)| *method == Method::CallsInto && *at == place); + if !seen { + answers.push((Method::KnownCaller, place)); + } + } + } + if let Some(place) = by_command.get(&target).copied() { + answers.push((Method::Command, place)); + let matched = want + .candidates + .as_ref() + .is_some_and(|candidates| candidates.contains(&place)) + || want.loose.contains(&place); + if matched { + answers.push((Method::Fingerprint, place)); + } + } + } + Kind::Table => { + if let Some(place) = by_reference(&followed, target) { + answers.push((Method::References, place)); + } + let mut candidates = match &want.candidates { + Some(list) => list.clone(), + None => table_candidates(&old, &new, &known, target), + }; + // The table a class name gives is also compared slot by slot, which checks + // its structure apart from the name. + if let Some(place) = by_class.get(&target) { + if !candidates.contains(place) { + candidates.push(*place); + } + } + let mut slots = by_slots(&old, &new, &known, target, &candidates); + if let Some(place) = by_class.get(&target).copied() { + answers.push((Method::ClassName, place)); + let given = BTreeSet::from([place]); + answers.extend(fingerprint_agrees(&given, want.candidates.as_deref())); + // Several tables holding the same slots is no answer of their own; the + // class name tells which of them is the class's. + if slots.len() > 1 && slots.contains(&place) { + slots = vec![place]; + } + } + for place in slots { + answers.push((Method::Slots, place)); + } + } + } + match decide(&answers, want.candidates.as_deref()) { + Ok((place, methods)) => { + known.insert(target, place); + resolution.places.insert(target, place); + resolution.settled.push(format!( + "{target:#x} -> {place:#x} by {}", + names(&methods) + )); + resolution.reasons.remove(&target); + pending.remove(&target); + found_any = true; + } + Err(reason) => { + let reason = match class_reasons.get(&target) { + Some(class) => format!("{reason}; {class}"), + None => reason, + }; + resolution.reasons.insert(target, reason); + } + } + } + resolution.followed = followed.places; + if !found_any || pending.is_empty() { + break; + } + } + resolution +} + +/// The one function that uses every text of a target the new build holds once. +fn by_text(texts: &BTreeSet>, users: &BTreeMap, BTreeSet>) -> Option { + let mut common: Option> = None; + for text in texts { + let Some(found) = users.get(text) else { + continue; + }; + common = Some(match common { + Some(common) => common.intersection(found).copied().collect(), + None => found.clone(), + }); + } + let common = common?; + match common.iter().copied().collect::>().as_slice() { + [only] => Some(*only), + _ => None, + } +} + +/// How many instructions on each side of a use name it within its body. +const VALUE_CONTEXT: usize = 2; + +/// Which operand of an instruction holds a value. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Operand { + Displacement, + Immediate, +} + +/// A value of the prior row read again from the new build, or why it could not be. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Reading { + pub path: String, + pub prior: u64, + /// The new value and how it was read. + pub value: Result<(u64, String), String>, +} + +fn keys(steps: &[Step]) -> Vec> { + (0..steps.len()) + .map(|index| context(steps, index, VALUE_CONTEXT, VALUE_CONTEXT)) + .collect() +} + +/// Whether an instruction uses `wanted`: as a member displacement or an immediate for a plain +/// value, or as the displacement of a call through a table for a slot, which is eight bytes each. +fn use_of(step: &Step, wanted: i64, slot: bool) -> Option { + if step.stack { + return None; + } + if slot { + return (step.indirect && step.displacement == Some(wanted)).then_some(Operand::Displacement); + } + if step.displacement == Some(wanted) { + Some(Operand::Displacement) + } else if step.immediate == Some(wanted) { + Some(Operand::Immediate) + } else { + None + } +} + +/// Reads one value from the instructions that use it. Each use of `prior` in a prior function body +/// is named by the instructions around it, with their own operands cleared, and the same +/// instruction is looked up in the function's new body. A value is taken only when at least two +/// uses are read and every use read gives the same value. +fn read_value( + old: &mut Side, + new: &mut Side, + pairs: &[(u32, u32)], + prior: u64, + slot: bool, +) -> Result<(u64, String), String> { + if prior == 0 { + return Err("zero is used too widely in code to be told apart".to_owned()); + } + let wanted = if slot { prior.checked_mul(8) } else { Some(prior) } + .and_then(|value| i64::try_from(value).ok()) + .ok_or_else(|| format!("{prior:#x} is too large to be an operand"))?; + let mut found: Vec<(u64, u32)> = Vec::new(); + let mut uses = 0; + for (from, to) in pairs { + let old_body = old.body(*from); + let new_body = new.body(*to); + let old_keys = keys(&old_body); + let new_keys = keys(&new_body); + for (index, step) in old_body.iter().enumerate() { + let Some(operand) = use_of(step, wanted, slot) else { + continue; + }; + uses += 1; + let key = &old_keys[index]; + let rank = old_keys[..index].iter().filter(|other| *other == key).count(); + let count = old_keys.iter().filter(|other| *other == key).count(); + let theirs: Vec = new_keys + .iter() + .enumerate() + .filter(|(_, other)| *other == key) + .map(|(at, _)| at) + .collect(); + if theirs.len() != count { + continue; + } + let other = &new_body[theirs[rank]]; + let read = match operand { + Operand::Displacement => other.displacement, + Operand::Immediate => other.immediate, + }; + let Some(read) = read.and_then(|read| u64::try_from(read).ok()) else { + continue; + }; + if slot && read % 8 != 0 { + continue; + } + found.push((if slot { read / 8 } else { read }, *from)); + } + } + let values: BTreeSet = found.iter().map(|(value, _)| *value).collect(); + let functions: BTreeSet = found.iter().map(|(_, function)| *function).collect(); + match values.iter().copied().collect::>().as_slice() { + [] if uses == 0 => Err(format!( + "no instruction of the {} functions known in both builds uses {prior:#x}", + pairs.len() + )), + [] => Err(format!( + "its {uses} uses in the prior build were not found in the new one" + )), + [value] if found.len() >= 2 => Ok(( + *value, + format!( + "read at {} uses in {} of its functions", + found.len(), + functions.len() + ), + )), + [value] => Err(format!( + "read at only one use, as {value:#x}, and a second reading is needed" + )), + _ => Err(format!("its uses disagree: {values:#x?}")), + } +} + +/// Reads each value of the prior row that has no recipe from the instructions that use it in the +/// functions of its part of the row, and in the functions they call, in both builds. `sections` +/// pairs the prior and new places of each part's functions, and `followed` pairs the functions +/// the calls of known functions reach. A value's part is the first name of its path, and a value +/// whose name ends in `_slot` is a slot of a class table. +pub fn read_values( + old_pe: &PeFile, + new_pe: &PeFile, + sections: &BTreeMap>, + followed: &BTreeMap, + values: &[(String, u64)], +) -> Vec { + let mut old = Side::new(old_pe); + let mut new = Side::new(new_pe); + let mut pairs_of: BTreeMap<&str, Vec<(u32, u32)>> = BTreeMap::new(); + for (section, pairs) in sections { + let mut all: BTreeSet<(u32, u32)> = pairs.iter().copied().collect(); + for (from, _) in pairs { + let body = old.body(*from); + for step in body.iter() { + if let Some(place) = step.call.and_then(|callee| followed.get(&callee)) { + all.insert((step.call.expect("checked"), *place)); + } + } + } + pairs_of.insert(section.as_str(), all.into_iter().collect()); + } + let mut readings = Vec::new(); + for (path, prior) in values { + let section = path.split('.').next().unwrap_or_default(); + let name = path.rsplit('.').next().unwrap_or_default(); + let slot = name.split('[').next().unwrap_or_default().ends_with("_slot"); + let pairs = pairs_of.get(section).map_or(&[][..], Vec::as_slice); + readings.push(Reading { + path: path.clone(), + prior: *prior, + value: read_value(&mut old, &mut new, pairs, *prior, slot), + }); + } + readings +} + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + + pub const IMAGE_BASE: u64 = 0x1_8000_0000; + /// Where a fixture keeps its exception table, in its data section. + const EXCEPTIONS: u32 = 0x20f000; + + fn place(rva: u32) -> usize { + if rva >= 0x200000 { + 0x10400 + (rva - 0x200000) as usize + } else { + 0x400 + (rva - 0x100000) as usize + } + } + + /// A 64-bit module with a code section at 0x100000 and a data section at 0x200000, holding + /// the given bytes and an exception table that lists `ranges`. + pub fn module(parts: &[(u32, Vec)], ranges: &[(u32, u32)]) -> Vec { + let mut bytes = vec![0u8; 0x400]; + bytes[0x3c..0x40].copy_from_slice(&0x80u32.to_le_bytes()); + bytes[0x80..0x84].copy_from_slice(b"PE\0\0"); + bytes[0x86..0x88].copy_from_slice(&2u16.to_le_bytes()); + bytes[0x94..0x96].copy_from_slice(&240u16.to_le_bytes()); + bytes[0x98..0x9a].copy_from_slice(&0x20bu16.to_le_bytes()); + bytes[0x98 + 24..0x98 + 32].copy_from_slice(&IMAGE_BASE.to_le_bytes()); + let sections = [ + (0x100000u32, 0x400u32, 0x2000_0000u32), + (0x200000, 0x10400, 0x4000_0000), + ]; + for (index, (rva, raw, flags)) in sections.iter().enumerate() { + let header = 0x98 + 240 + 40 * index; + bytes[header + 8..header + 12].copy_from_slice(&0x10000u32.to_le_bytes()); + bytes[header + 12..header + 16].copy_from_slice(&rva.to_le_bytes()); + bytes[header + 16..header + 20].copy_from_slice(&0x10000u32.to_le_bytes()); + bytes[header + 20..header + 24].copy_from_slice(&raw.to_le_bytes()); + bytes[header + 36..header + 40].copy_from_slice(&flags.to_le_bytes()); + } + bytes.resize(0x400 + 0x20000, 0); + bytes[0x400..0x10400].fill(0xCC); + let mut table = vec![]; + for (start, end) in ranges { + table.extend_from_slice(&start.to_le_bytes()); + table.extend_from_slice(&end.to_le_bytes()); + table.extend_from_slice(&0u32.to_le_bytes()); + } + let directory = 0x98 + 112 + 24; + bytes[directory..directory + 4].copy_from_slice(&EXCEPTIONS.to_le_bytes()); + bytes[directory + 4..directory + 8].copy_from_slice(&(table.len() as u32).to_le_bytes()); + let at = place(EXCEPTIONS); + bytes[at..at + table.len()].copy_from_slice(&table); + for (rva, data) in parts { + let at = place(*rva); + bytes[at..at + data.len()].copy_from_slice(data); + } + bytes + } + + pub fn call(from: u32, to: u32) -> Vec { + let mut bytes = vec![0xE8]; + bytes.extend_from_slice(&((i64::from(to) - i64::from(from) - 5) as i32).to_le_bytes()); + bytes + } + + pub fn table(functions: &[u32]) -> Vec { + functions + .iter() + .flat_map(|function| (IMAGE_BASE + u64::from(*function)).to_le_bytes()) + .collect() + } + + /// sub rsp, 28h; call `to`; add rsp, 28h; ret. + fn caller(at: u32, to: u32) -> Vec { + let mut bytes = vec![0x48, 0x83, 0xEC, 0x28]; + bytes.extend(call(at + 4, to)); + bytes.extend([0x48, 0x83, 0xC4, 0x28, 0xC3]); + bytes + } + + /// push rsi; sub rsp, 20h; mov rsi, rcx; add rsp, 20h; pop rsi; ret. + const TARGET: [u8; 14] = [ + 0x56, 0x48, 0x83, 0xEC, 0x20, 0x48, 0x89, 0xCE, 0x48, 0x83, 0xC4, 0x20, 0x5E, 0xC3, + ]; + + /// `mov eax, n; ret`: a different small function for each slot of a table. + fn small(n: u8) -> Vec { + vec![0xB8, n, 0, 0, 0, 0xC3] + } + + const OLD_CALLER: u32 = 0x101000; + const OLD_TARGET: u32 = 0x101100; + const OLD_TABLE: u32 = 0x200100; + const NEW_CALLER: u32 = 0x103000; + const NEW_TARGETS: [u32; 2] = [0x103100, 0x103200]; + const NEW_TABLE: u32 = 0x200800; + + /// The prior build: a caller of the target, and a table that holds it in slot 3. + fn old_module() -> Vec { + let mut parts = vec![ + (OLD_CALLER, caller(OLD_CALLER, OLD_TARGET)), + (OLD_TARGET, TARGET.to_vec()), + ]; + let mut slots = vec![]; + for n in 0..6u8 { + let at = 0x101400 + 0x10 * u32::from(n); + parts.push((at, small(n))); + slots.push(at); + } + slots.insert(3, OLD_TARGET); + parts.push((OLD_TABLE, table(&slots))); + module( + &parts, + &[(OLD_CALLER, OLD_CALLER + 14), (OLD_TARGET, OLD_TARGET + 14)], + ) + } + + /// The new build: the target's bytes in two places, a caller of the first, and a table that + /// holds `in_table` in slot 3. + fn new_module(in_table: u32) -> Vec { + let mut parts = vec![ + (NEW_CALLER, caller(NEW_CALLER, NEW_TARGETS[0])), + (NEW_TARGETS[0], TARGET.to_vec()), + (NEW_TARGETS[1], TARGET.to_vec()), + ]; + let mut slots = vec![]; + for n in 0..6u8 { + let at = 0x103400 + 0x10 * u32::from(n); + parts.push((at, small(n))); + slots.push(at); + } + slots.insert(3, in_table); + parts.push((NEW_TABLE, table(&slots))); + let ranges = [ + (NEW_CALLER, NEW_CALLER + 14), + (NEW_TARGETS[0], NEW_TARGETS[0] + 14), + (NEW_TARGETS[1], NEW_TARGETS[1] + 14), + ]; + module(&parts, &ranges) + } + + fn run(new_bytes: &[u8], known_table: bool) -> Resolution { + let old_bytes = old_module(); + let old = PeFile::parse(&old_bytes).unwrap(); + let new = PeFile::parse(new_bytes).unwrap(); + let mut known = BTreeMap::from([(OLD_CALLER, NEW_CALLER)]); + if known_table { + known.insert(OLD_TABLE, NEW_TABLE); + } + let wanted = BTreeMap::from([( + OLD_TARGET, + Wanted { + kind: Kind::Function, + candidates: Some(NEW_TARGETS.to_vec()), + class: None, + command: None, + loose: Vec::new(), + }, + )]); + resolve( + &old, + &new, + &known, + &wanted, + &BTreeSet::from([OLD_TABLE]), + &BTreeSet::new(), + ) + } + + #[test] + fn nfr6_a_function_with_several_places_takes_the_one_two_methods_agree_on() { + let resolution = run(&new_module(NEW_TARGETS[0]), true); + assert_eq!(resolution.places.get(&OLD_TARGET), Some(&NEW_TARGETS[0])); + assert_eq!( + resolution.settled, + ["0x101100 -> 0x103100 by calls into it and its table slot and its fingerprint"] + ); + assert!(resolution.reasons.is_empty(), "{:?}", resolution.reasons); + } + + #[test] + fn nfr6_a_class_table_is_taken_where_its_class_name_and_fingerprint_agree() { + // Edge 157's frame table: two tables hold the frame's layout function in slot 24, and its + // class name tells which of them is the frame view's. + let candidates: &[u32] = &[0x11b5_19a8, 0x11b5_2528]; + let by_name = BTreeSet::from([candidates[1]]); + let mut answers = vec![(Method::ClassName, candidates[1])]; + answers.extend(fingerprint_agrees(&by_name, Some(candidates))); + assert_eq!(answers[1], (Method::Fingerprint, candidates[1])); + let (place, methods) = decide(&answers, Some(candidates)).unwrap(); + assert_eq!(place, candidates[1]); + assert_eq!(methods.len(), 2); + // A class name alone is one method. + let alone = decide(&answers[..1], None).unwrap_err(); + assert!(alone.starts_with("only its class name found it"), "{alone}"); + // A place that is not one of the fingerprint's places gets no agreement from it. + assert_eq!(fingerprint_agrees(&BTreeSet::from([0x11b5_3000]), Some(candidates)), None); + // A table slot that gives two places gives the fingerprint nothing to agree with. + let both: BTreeSet = candidates.iter().copied().collect(); + assert_eq!(fingerprint_agrees(&both, Some(candidates)), None); + assert_eq!(fingerprint_agrees(&by_name, None), None); + } + + #[test] + fn nfr6_methods_that_disagree_leave_the_function_unresolved() { + let resolution = run(&new_module(NEW_TARGETS[1]), true); + assert!(resolution.places.is_empty()); + let reason = &resolution.reasons[&OLD_TARGET]; + assert!(reason.starts_with("the methods disagree: "), "{reason}"); + assert!(reason.contains("calls into it 0x103100"), "{reason}"); + assert!(reason.contains("its table slot 0x103200"), "{reason}"); + } + + #[test] + fn nfr6_one_method_alone_does_not_place_a_function() { + let resolution = run(&new_module(NEW_TARGETS[0]), false); + assert!(resolution.places.is_empty()); + let reason = &resolution.reasons[&OLD_TARGET]; + assert!(reason.starts_with("only calls into it found it"), "{reason}"); + } + + const VIEW_TARGET: u32 = 0x105000; + const VIEW_TABLE: u32 = 0x201000; + const OTHER_VIEW_TABLE: u32 = 0x201400; + + /// A build with two tables of views classes. The first holds, among small functions, the + /// target in slot 6; the second holds small functions only. In a new build each table gets one + /// more slot at slot 2 when `inserted` says so, as when a milestone adds a virtual function to + /// `views::View`, and the target's code changed, so its fingerprint finds nothing. + fn views_module(new: bool, inserted: [bool; 2]) -> Vec { + let mut parts = vec![]; + let mut ranges = vec![]; + let mut slots = |base: u32, first: u8, holds_target: bool, insert: bool| { + let mut list = vec![]; + for n in 0..12u8 { + let at = base + 0x10 * u32::from(n); + parts.push((at, small(first + n))); + list.push(at); + } + if holds_target { + list.insert(6, VIEW_TARGET); + } + if insert { + let at = base + 0x100; + parts.push((at, small(first + 0x40))); + list.insert(2, at); + } + list + }; + let first = slots(0x105400, 0, true, new && inserted[0]); + let second = slots(0x105800, 0x80, false, new && inserted[1]); + parts.push((VIEW_TABLE, table(&first))); + parts.push((OTHER_VIEW_TABLE, table(&second))); + let mut code = TARGET.to_vec(); + if new { + // mov rsi, rdx instead of mov rsi, rcx. + code[7] = 0xD6; + } + parts.push((VIEW_TARGET, code)); + ranges.push((VIEW_TARGET, VIEW_TARGET + 14)); + module(&parts, &ranges) + } + + fn run_views(inserted: [bool; 2], views: &[u32]) -> Resolution { + let old_bytes = views_module(false, [false; 2]); + let new_bytes = views_module(true, inserted); + let old = PeFile::parse(&old_bytes).unwrap(); + let new = PeFile::parse(&new_bytes).unwrap(); + let known = BTreeMap::from([ + (VIEW_TABLE, VIEW_TABLE), + (OTHER_VIEW_TABLE, OTHER_VIEW_TABLE), + ]); + let wanted = BTreeMap::from([( + VIEW_TARGET, + Wanted { + kind: Kind::Function, + candidates: None, + class: None, + command: None, + loose: Vec::new(), + }, + )]); + let tables = BTreeSet::from([VIEW_TABLE, OTHER_VIEW_TABLE]); + let views: BTreeSet = views.iter().copied().collect(); + resolve(&old, &new, &known, &wanted, &tables, &views) + } + + #[test] + fn nfr6_a_virtual_function_is_read_from_its_views_class_table_by_slot() { + // Both tables moved the slot by one: the slot number is found twice, and the function is + // the one the first table holds there. + let resolution = run_views([true, true], &[VIEW_TABLE, OTHER_VIEW_TABLE]); + assert_eq!(resolution.places.get(&VIEW_TARGET), Some(&VIEW_TARGET)); + assert_eq!( + resolution.settled, + ["0x105000 -> 0x105000 by its table slot and its slot shift"] + ); + } + + #[test] + fn nfr6_a_virtual_function_needs_a_second_views_table_to_agree_on_its_slot() { + // Without a second table of a views class, the slot is found once. + let alone = run_views([true, true], &[VIEW_TABLE]); + assert!(alone.places.is_empty()); + let reason = &alone.reasons[&VIEW_TARGET]; + assert!(reason.starts_with("only its table slot found it"), "{reason}"); + // A second table that lines the slot up another way confirms nothing. + let apart = run_views([true, false], &[VIEW_TABLE, OTHER_VIEW_TABLE]); + assert!(apart.places.is_empty()); + let reason = &apart.reasons[&VIEW_TARGET]; + assert!(reason.starts_with("only its table slot found it"), "{reason}"); + } + + /// push rbx; mov ebx, 5; xor eax, eax; call `first`; [call `second`;] nop; pop rbx; ret: a + /// caller whose body around the call changed, so no call site of it pairs with the prior one. + fn changed_caller(at: u32, first: u32, second: Option) -> Vec { + let mut bytes = vec![0x53, 0xBB, 0x05, 0, 0, 0, 0x31, 0xC0]; + bytes.extend(call(at + bytes.len() as u32, first)); + if let Some(second) = second { + bytes.extend(call(at + bytes.len() as u32, second)); + } + bytes.extend([0x90, 0x5B, 0xC3]); + bytes + } + + fn run_known_caller(second: Option) -> Resolution { + let old_bytes = old_module(); + let caller = changed_caller(NEW_CALLER, NEW_TARGETS[0], second); + let new_bytes = module( + &[ + (NEW_CALLER, caller), + (NEW_TARGETS[0], TARGET.to_vec()), + (NEW_TARGETS[1], TARGET.to_vec()), + ], + &[ + (NEW_CALLER, NEW_CALLER + 24), + (NEW_TARGETS[0], NEW_TARGETS[0] + 14), + (NEW_TARGETS[1], NEW_TARGETS[1] + 14), + ], + ); + let old = PeFile::parse(&old_bytes).unwrap(); + let new = PeFile::parse(&new_bytes).unwrap(); + let known = BTreeMap::from([(OLD_CALLER, NEW_CALLER)]); + let wanted = BTreeMap::from([( + OLD_TARGET, + Wanted { + kind: Kind::Function, + candidates: Some(NEW_TARGETS.to_vec()), + class: None, + command: None, + loose: Vec::new(), + }, + )]); + let none = BTreeSet::new(); + resolve(&old, &new, &known, &wanted, &none, &none) + } + + #[test] + fn nfr6_a_drawing_helper_is_not_placed_by_its_caller_alone() { + // The caller, as a frame view's paint calls `DrawColor`, is known in both builds, and of + // the two places the helper's fingerprint matched, its new body calls one. That call is the + // same kind of fact as the calls into it, and the fingerprint matched both places, so it is + // one method and the helper waits for a second one. + let resolution = run_known_caller(None); + assert!(resolution.places.is_empty(), "{:?}", resolution.places); + assert!(resolution.settled.is_empty(), "{:?}", resolution.settled); + } + + #[test] + fn nfr6_a_caller_that_calls_every_fingerprint_place_names_no_helper() { + let resolution = run_known_caller(Some(NEW_TARGETS[1])); + assert!(resolution.places.is_empty()); + assert_eq!(resolution.reasons[&OLD_TARGET], "no method found it"); + } + + #[test] + fn nfr6_a_place_outside_the_fingerprint_matches_is_refused() { + let answers = [(Method::CallsInto, 0x103300), (Method::TableSlot, 0x103300)]; + let refused = decide(&answers, Some(&NEW_TARGETS[..])).unwrap_err(); + assert!(refused.contains("not one of its fingerprint's places"), "{refused}"); + let (place, methods) = decide(&answers, None).unwrap(); + assert_eq!(place, 0x103300); + assert_eq!(methods.len(), 2); + assert_eq!(decide(&[], None).unwrap_err(), "no method found it"); + } +} diff --git a/rivet/platform/windows/src/row_watch.rs b/rivet/platform/windows/src/row_watch.rs index c960533..86e7c19 100644 --- a/rivet/platform/windows/src/row_watch.rs +++ b/rivet/platform/windows/src/row_watch.rs @@ -161,7 +161,7 @@ pub fn brave_candidates(releases: &Value) -> Vec { /// What still needs a row, most urgent first: what people have now, then what stable is about to /// serve, then beta, which brings the next milestone weeks ahead and gives a person time to review -/// what symbols cannot show. A version older than every row and not served is history, not news. +/// what the generator cannot read. A version older than every row and not served is history, not news. pub fn pending(candidates: Vec, known: &BTreeSet<(String, String)>) -> Vec { let mut newest_known: BTreeMap<&str, Vec> = BTreeMap::new(); for (browser, version) in known { @@ -299,6 +299,18 @@ pub fn test_desktop_limits(browser: &str) -> Vec<(String, String)> { /// notes name each feature left undriven by the test desktop, and a listed feature that now /// passes, so the list is looked at again. pub fn publish_gate(results: &Value, browser: &str) -> Result, Vec> { + publish_gate_for_row(results, browser, &Value::Null) +} + +/// `publish_gate` for the row being published. A check of a feature the row turns off for its +/// build is not expected to pass: the row ships with that feature off, and a note names it. Every +/// other check must still pass. +pub fn publish_gate_for_row( + results: &Value, + browser: &str, + row: &Value, +) -> Result, Vec> { + let excused = crate::row_features::excused_checks(row); let rows: Vec<&Value> = match results { Value::Array(rows) => rows.iter().collect(), // ConvertTo-Json writes a single row as an object. @@ -326,6 +338,10 @@ pub fn publish_gate(results: &Value, browser: &str) -> Result, Vec reasons.push(format!("{feature}: not run")), @@ -531,6 +547,26 @@ mod tests { .collect() } + #[test] + fn nfr6_a_feature_off_in_the_row_does_not_hold_back_the_rest() { + let mut rows = all_pass("chrome"); + for row in rows.iter_mut().filter(|row| row.1.starts_with("split")) { + row.2 = "FAIL"; + } + // Without the row, the failed split checks keep the browser off the list. + assert!(publish_gate(&results(&rows), "chrome").is_err()); + let row = json!({"switched_off": [{"feature": "split view", "values": ["split.outline_member"]}]}); + let notes = publish_gate_for_row(&results(&rows), "chrome", &row).unwrap(); + assert!(notes + .iter() + .any(|note| note == "split-grid-4: split view is off in this build's row")); + // Another feature that fails still keeps it off. + let sidebar = rows.iter().position(|row| row.1 == "sidebar").unwrap(); + rows[sidebar].2 = "FAIL"; + let reasons = publish_gate_for_row(&results(&rows), "chrome", &row).unwrap_err(); + assert_eq!(reasons, ["sidebar: FAIL"]); + } + #[test] fn nfr3_a_browser_that_passed_every_feature_is_published() { assert!(publish_gate(&results(&all_pass("chrome")), "Chrome").is_ok()); diff --git a/rivet/runtime/windows/rows/table.json b/rivet/runtime/windows/rows/table.json index 41d56c3..90ba91d 100644 --- a/rivet/runtime/windows/rows/table.json +++ b/rivet/runtime/windows/rows/table.json @@ -2604,17 +2604,5 @@ } ], "schema": 1, - "sequence": 0, - "symbols": { - "Brave 156.1.98.63": "5723B770F8DCB2794C4C44205044422E1", - "Brave 157.1.99.55": "22DC58117C04ED6E4C4C44205044422E1", - "Google Chrome 156.0.8078.74": "713E6CA96EBDD5814C4C44205044422E1", - "Google Chrome 156.0.8078.80": "B2C7C3B72F30D3ED4C4C44205044422E1", - "Google Chrome 156.0.8078.81": "4A4A604319CF7D794C4C44205044422E1", - "Google Chrome 156.0.8078.82": "A28166C00E1AB5384C4C44205044422E1", - "Google Chrome 157.0.8103.38": "B445A620A73B6EA94C4C44205044422E1", - "Google Chrome 157.0.8103.39": "037147ED9738B0F84C4C44205044422E1", - "Microsoft Edge 156.0.4314.50": "F44CB0836ADF26834C4C44205044422E1", - "Microsoft Edge 156.0.4314.58": "03775A411C2857584C4C44205044422E1" - } + "sequence": 0 } diff --git a/rivet/runtime/windows/src/browsers/chromium/commands.rs b/rivet/runtime/windows/src/browsers/chromium/commands.rs index 05041b7..99f2283 100644 --- a/rivet/runtime/windows/src/browsers/chromium/commands.rs +++ b/rivet/runtime/windows/src/browsers/chromium/commands.rs @@ -36,6 +36,21 @@ pub(crate) fn chromium_milestone(browser: &str, version: [u16; 4]) -> Option (u32, u32) { + chromium_milestone(browser, version) + .and_then(rivet_platform_windows::command_ids::dev_tools) + .unwrap_or((0, 0)) +} + +/// The new tab command id of a build, from Chromium's public header for its milestone, or `None` +/// when that milestone's header was not read. +pub(crate) fn new_tab(browser: &str, version: [u16; 4]) -> Option { + chromium_milestone(browser, version).and_then(rivet_platform_windows::command_ids::new_tab) +} + /// Whether a browser keeps Chromium's command ids without public source to confirm it. Brave /// publishes its changes and adds its own ids only from 56000, so it keeps Chromium's. Edge, Whale /// and Samsung Internet take the value of their Chromium milestone, which needs a regression check @@ -150,6 +165,16 @@ mod tests { assert!(move_tab_to_new_window("Opera", [130, 0, 1, 1]).is_err()); } + #[test] + fn nfr6_a_command_id_is_not_a_row_value() { + // The developer commands follow the build's Chromium milestone, whatever its row holds. + assert_eq!(dev_tools("Google Chrome", [159, 0, 8154, 6]), (40023, 40005)); + assert_eq!(dev_tools("Microsoft Edge", [157, 0, 4341, 46]), (40023, 40005)); + // A milestone whose header was not read offers no command. + assert_eq!(dev_tools("Google Chrome", [149, 0, 1, 1]), (0, 0)); + assert_eq!(dev_tools("Samsung Internet", [30, 0, 4, 29]), (0, 0)); + } + #[test] fn fr9_browsers_without_public_command_ids_need_a_regression_check() { assert!(!needs_regression_check("Google Chrome")); diff --git a/rivet/runtime/windows/src/browsers/chromium/layout.rs b/rivet/runtime/windows/src/browsers/chromium/layout.rs index 184f4c2..052bf49 100644 --- a/rivet/runtime/windows/src/browsers/chromium/layout.rs +++ b/rivet/runtime/windows/src/browsers/chromium/layout.rs @@ -87,9 +87,12 @@ pub(crate) unsafe extern "system" fn proposed_layout( ) -> *mut c_void { let original: ProposedLayout = std::mem::transmute(page_area::original()); let result = original(this, out, params); - let host = *(this - .cast::() - .add(crate::builds::get().side_panel.host_view) as *const usize); + // The layout keeps the view it lays out in `BrowserViewLayoutViews::browser_view`, found by + // the class name of the view it holds. + let Some(host_field) = crate::chromium::window_views::host_field(this as usize) else { + return result; + }; + let host = *(this.cast::().add(host_field) as *const usize); crate::trace::trace_changed( "layout hook", &format!( @@ -115,9 +118,12 @@ unsafe extern "system" fn app_proposed_layout( let original: ProposedLayout = std::mem::transmute(APP_ORIGINAL.load(std::sync::atomic::Ordering::Acquire)); let result = original(this, out, params); - let host = *(this - .cast::() - .add(crate::builds::get().side_panel.host_view) as *const usize); + // The layout keeps the view it lays out in `BrowserViewLayoutViews::browser_view`, found by + // the class name of the view it holds. + let Some(host_field) = crate::chromium::window_views::host_field(this as usize) else { + return result; + }; + let host = *(this.cast::().add(host_field) as *const usize); crate::trace::trace_changed( "layout hook", &format!( diff --git a/rivet/runtime/windows/src/browsers/chromium/split.rs b/rivet/runtime/windows/src/browsers/chromium/split.rs index 1585390..7efa102 100644 --- a/rivet/runtime/windows/src/browsers/chromium/split.rs +++ b/rivet/runtime/windows/src/browsers/chromium/split.rs @@ -83,10 +83,10 @@ pub(crate) unsafe fn on_page_area(area: &mut crate::hooks::page_area::PageArea) let row = &crate::builds::get().side_panel; if let (Some(toolbar), Some(contents)) = ( area.bounds(row.toolbar), - area.bounds(row.contents_container), + area.contents(), ) { if area.rail == 0 && active_is_split(area.window) { - if let Some(page) = area.bounds(row.page_area) { + if let Some(page) = area.page() { fill_area(&mut *contents, &*page); } } diff --git a/rivet/runtime/windows/src/builds/brave.rs b/rivet/runtime/windows/src/builds/brave.rs index eeb8057..52d74ff 100644 --- a/rivet/runtime/windows/src/builds/brave.rs +++ b/rivet/runtime/windows/src/builds/brave.rs @@ -49,7 +49,7 @@ pub static BRAVE: Build = Build { module: "chrome.dll", version: [156, 1, 98, 62], toolbar: Toolbar { - get_widget_slot: 11, + get_widget_slot: Some(11), dev: DEV_BRAVE_156, get_color: 0x06A9A890, // ?GetColor@ColorProvider@ui@@QEBAIH@Z get_color_bytes: &[ @@ -236,9 +236,9 @@ pub static BRAVE: Build = Build { coordinator_browser: None, entry_key_size: None, extensions_by_toggle: None, - host_view: 0x38, - page_area: 0x50, - contents_container: 0xb8, + host_view: Some(0x38), + page_area: Some(0x50), + contents_container: Some(0xb8), vertical_tabs: 0x80, toolbar: 0x60, contents_outline: 0x58, @@ -295,7 +295,7 @@ pub static BRAVE: Build = Build { stack_at_top_prologue: &[0x41, 0x57, 0x41, 0x56, 0x56, 0x57], set_opaque: 0x02322E10, // ?SetFillsBoundsOpaquely@Layer@ui@@QEAAX_N@Z set_opaque_prologue: &[0x56, 0x48, 0x83, 0xEC, 0x20], - frame_browser_window_offset: 0x340, + frame_browser_window_offset: Some(0x340), browser_window_in_view: 0x1F0, caption_ink_ids: &[0x67D, 0x67E, 0x67F], top_inset_adjust: -12, @@ -380,7 +380,7 @@ pub static BRAVE_63: Build = Build { module: "chrome.dll", version: [156, 1, 98, 63], toolbar: Toolbar { - get_widget_slot: 11, + get_widget_slot: Some(11), dev: DEV_BRAVE_156, get_color: 0x06A5CB00, // ?GetColor@ColorProvider@ui@@QEBAIH@Z get_color_bytes: &[ @@ -604,9 +604,9 @@ pub static BRAVE_63: Build = Build { coordinator_browser: None, entry_key_size: None, extensions_by_toggle: None, - host_view: 0x38, - page_area: 0x50, - contents_container: 0xb8, + host_view: Some(0x38), + page_area: Some(0x50), + contents_container: Some(0xb8), vertical_tabs: 0x80, toolbar: 0x60, contents_outline: 0x58, @@ -663,7 +663,7 @@ pub static BRAVE_63: Build = Build { stack_at_top_prologue: &[0x41, 0x57, 0x41, 0x56, 0x56, 0x57], set_opaque: 0x0244FE30, // ?SetFillsBoundsOpaquely@Layer@ui@@QEAAX_N@Z set_opaque_prologue: &[0x56, 0x48, 0x83, 0xEC, 0x20], - frame_browser_window_offset: 0x340, + frame_browser_window_offset: Some(0x340), browser_window_in_view: 0x1F0, caption_ink_ids: &[0x67D, 0x67E, 0x67F], top_inset_adjust: 0, // Chromium's own GetTopInset, as on Chrome @@ -782,7 +782,7 @@ pub static BRAVE_157_55: Build = Build { module: "chrome.dll", version: [157, 1, 99, 55], toolbar: Toolbar { - get_widget_slot: 11, + get_widget_slot: Some(11), dev: DEV_BRAVE_157, get_color: 0x05B6CB30, // ?GetColor@ColorProvider@ui@@QEBAIH@Z get_color_bytes: &[ @@ -1006,9 +1006,9 @@ pub static BRAVE_157_55: Build = Build { coordinator_browser: None, entry_key_size: None, extensions_by_toggle: None, - host_view: 0x38, - page_area: 0x50, - contents_container: 0xb8, + host_view: Some(0x38), + page_area: Some(0x50), + contents_container: Some(0xb8), vertical_tabs: 0x80, toolbar: 0x60, contents_outline: 0x58, @@ -1065,7 +1065,7 @@ pub static BRAVE_157_55: Build = Build { stack_at_top_prologue: &[0x41, 0x57, 0x41, 0x56, 0x56, 0x57], set_opaque: 0x0080EA30, // ?SetFillsBoundsOpaquely@Layer@ui@@UEAAX_N@Z set_opaque_prologue: &[0x56, 0x48, 0x83, 0xEC, 0x20], - frame_browser_window_offset: 0x338, + frame_browser_window_offset: Some(0x338), browser_window_in_view: 0x1F0, caption_ink_ids: &[0x684, 0x685, 0x686], top_inset_adjust: 0, // Chromium's own GetTopInset, as on Chrome diff --git a/rivet/runtime/windows/src/builds/chrome.rs b/rivet/runtime/windows/src/builds/chrome.rs index 81e0e5d..b51e221 100644 --- a/rivet/runtime/windows/src/builds/chrome.rs +++ b/rivet/runtime/windows/src/builds/chrome.rs @@ -3,8 +3,6 @@ use super::*; pub(super) const DEV_CHROME_156: DevMode = DevMode { - inspect: 40023, - console: 40005, colours: &[ (0x13C, DEV_GROUND), (1590, DEV_GROUND), @@ -58,7 +56,7 @@ pub static CHROME: Build = Build { module: "chrome.dll", version: [156, 0, 8078, 48], toolbar: Toolbar { - get_widget_slot: 11, + get_widget_slot: Some(11), dev: DEV_CHROME_156, get_color: 0x04AA24F0, get_color_bytes: &[ @@ -322,9 +320,9 @@ pub static CHROME: Build = Build { coordinator_browser: None, entry_key_size: None, extensions_by_toggle: None, - host_view: 0x38, - page_area: 0x50, - contents_container: 0xb8, + host_view: Some(0x38), + page_area: Some(0x50), + contents_container: Some(0xb8), vertical_tabs: 0x80, toolbar: 0x60, contents_outline: 0x58, @@ -379,7 +377,7 @@ pub static CHROME: Build = Build { corner_paint_prologue: &[0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54], layout_prologue: &[0x41, 0x56, 0x56, 0x57, 0x55, 0x53], set_bounds_rect_prologue: &[0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54], - frame_browser_window_offset: 0x340, + frame_browser_window_offset: Some(0x340), browser_window_in_view: 0x1f0, caption_paint_prologue: &[0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54], caption_ink_ids: &[0x66C, 0x66D, 0x66E], @@ -462,7 +460,7 @@ pub static CHROME_156_74: Build = Build { module: "chrome.dll", version: [156, 0, 8078, 74], toolbar: Toolbar { - get_widget_slot: 11, + get_widget_slot: Some(11), dev: DEV_CHROME_156_74, get_color: 0x0535_9A80, get_color_bytes: &[ @@ -726,9 +724,9 @@ pub static CHROME_156_74: Build = Build { coordinator_browser: None, entry_key_size: None, extensions_by_toggle: None, - host_view: 0x38, - page_area: 0x50, - contents_container: 0xb8, + host_view: Some(0x38), + page_area: Some(0x50), + contents_container: Some(0xb8), vertical_tabs: 0x80, toolbar: 0x60, contents_outline: 0x58, @@ -783,7 +781,7 @@ pub static CHROME_156_74: Build = Build { corner_paint_prologue: &[0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54], layout_prologue: &[0x41, 0x56, 0x56, 0x57, 0x55, 0x53], set_bounds_rect_prologue: &[0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54], - frame_browser_window_offset: 0x340, + frame_browser_window_offset: Some(0x340), browser_window_in_view: 0x1f0, caption_paint_prologue: &[0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54], caption_ink_ids: &[0x66C, 0x66D, 0x66E], @@ -882,7 +880,7 @@ pub static CHROME_156_80: Build = Build { module: "chrome.dll", version: [156, 0, 8078, 80], toolbar: Toolbar { - get_widget_slot: 11, + get_widget_slot: Some(11), dev: DEV_CHROME_156_80, get_color: 0x052B_06D0, get_color_bytes: &[ @@ -1146,9 +1144,9 @@ pub static CHROME_156_80: Build = Build { coordinator_browser: None, entry_key_size: None, extensions_by_toggle: None, - host_view: 0x38, - page_area: 0x50, - contents_container: 0xb8, + host_view: Some(0x38), + page_area: Some(0x50), + contents_container: Some(0xb8), vertical_tabs: 0x80, toolbar: 0x60, contents_outline: 0x58, @@ -1203,7 +1201,7 @@ pub static CHROME_156_80: Build = Build { corner_paint_prologue: &[0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54], layout_prologue: &[0x41, 0x56, 0x56, 0x57, 0x55, 0x53], set_bounds_rect_prologue: &[0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54], - frame_browser_window_offset: 0x340, + frame_browser_window_offset: Some(0x340), browser_window_in_view: 0x1f0, caption_paint_prologue: &[0xE9, 0x0B, 0x00, 0x00, 0x00, 0xCC, 0xCC, 0xCC], caption_ink_ids: &[0x66C, 0x66D, 0x66E], @@ -1330,7 +1328,7 @@ pub static CHROME_157_38: Build = Build { module: "chrome.dll", version: [157, 0, 8103, 38], toolbar: Toolbar { - get_widget_slot: 11, + get_widget_slot: Some(11), dev: DEV_CHROME_157_38, get_color: 0x0454_F050, get_color_bytes: &[ @@ -1594,9 +1592,9 @@ pub static CHROME_157_38: Build = Build { coordinator_browser: None, entry_key_size: None, extensions_by_toggle: None, - host_view: 0x38, - page_area: 0x50, - contents_container: 0xb8, + host_view: Some(0x38), + page_area: Some(0x50), + contents_container: Some(0xb8), vertical_tabs: 0x80, toolbar: 0x60, contents_outline: 0x58, @@ -1651,7 +1649,7 @@ pub static CHROME_157_38: Build = Build { corner_paint_prologue: &[0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54], layout_prologue: &[0x41, 0x56, 0x56, 0x57, 0x55, 0x53], set_bounds_rect_prologue: &[0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54], - frame_browser_window_offset: 0x338, + frame_browser_window_offset: Some(0x338), browser_window_in_view: 0x1f0, caption_paint_prologue: &[0xE9, 0x0B, 0x00, 0x00, 0x00, 0xCC, 0xCC, 0xCC], caption_ink_ids: &[0x673, 0x674, 0x675], @@ -1722,7 +1720,7 @@ pub static CHROME_150: Build = Build { corner_paint_prologue: &[0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54], layout_prologue: &[0x41, 0x57, 0x41, 0x56, 0x56, 0x57], set_bounds_rect_prologue: &[0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54], - frame_browser_window_offset: 0x338, + frame_browser_window_offset: Some(0x338), browser_window_in_view: 0x1f0, caption_paint_prologue: &[0xE9, 0x0B, 0x00, 0x00, 0x00, 0xCC, 0xCC, 0xCC], caption_ink_ids: &[1593, 1594, 1595], diff --git a/rivet/runtime/windows/src/builds/edge.rs b/rivet/runtime/windows/src/builds/edge.rs index 7af0022..29ab5c9 100644 --- a/rivet/runtime/windows/src/builds/edge.rs +++ b/rivet/runtime/windows/src/builds/edge.rs @@ -57,7 +57,7 @@ pub static EDGE: Build = Build { module: "msedge.dll", version: [156, 0, 4314, 50], toolbar: Toolbar { - get_widget_slot: 10, + get_widget_slot: Some(10), dev: DEV_EDGE_156, get_color: 0x0100309E, get_color_bytes: &[ @@ -323,9 +323,9 @@ pub static EDGE: Build = Build { coordinator_browser: None, entry_key_size: None, extensions_by_toggle: None, - host_view: 0x38, - page_area: 0, - contents_container: 0xd8, + host_view: Some(0x38), + page_area: Some(0), + contents_container: Some(0xd8), vertical_tabs: ABSENT, toolbar: ABSENT, // the pages of a split fit their toolbar in Chrome's layout only contents_outline: ABSENT, @@ -381,7 +381,7 @@ pub static EDGE: Build = Build { corner_paint_prologue: &[0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54], layout_prologue: &[0x56, 0x48, 0x83, 0xEC, 0x40, 0x48], set_bounds_rect_prologue: &[0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54], - frame_browser_window_offset: 0x3B0, // frame view member that holds the BrowserWindow subobject, read by GetBrowserView + frame_browser_window_offset: Some(0x3B0), // frame view member that holds the BrowserWindow subobject, read by GetBrowserView browser_window_in_view: 0x1F8, // BrowserWindow subobject distance from the BrowserView, read by GetBrowserView caption_paint_prologue: &[0xE9, 0x01, 0x00, 0x00, 0x00, 0xCC, 0x41, 0x57], caption_ink_ids: &[0x66C, 0x66D, 0x66E], // Chrome value, not checked on Edge diff --git a/rivet/runtime/windows/src/builds/mod.rs b/rivet/runtime/windows/src/builds/mod.rs index b5335aa..c012b9d 100644 --- a/rivet/runtime/windows/src/builds/mod.rs +++ b/rivet/runtime/windows/src/builds/mod.rs @@ -139,7 +139,9 @@ pub struct AppWindow { pub detach_prologue: &'static [u8], pub insert: Option, pub insert_prologue: &'static [u8], - /// A `views::View` keeps its parent at this offset and its children vector right after it. + /// A `views::View` keeps its parent at this offset and its children vector right after it. Kept + /// for runtimes that read them from the row; this runtime takes both from the layout's bounds + /// offset, by the member order of `views::View`. pub view_parent: usize, pub view_children: usize, /// `operator new(size)`, `views::WebView::WebView(this, context)` and the object's size. @@ -184,8 +186,10 @@ pub struct AppWindow { row_struct! { /// Values for `chrome_toolbar.rs`. pub struct Toolbar { - /// `views::View::GetWidget` is the twelfth entry of every view's vtable. - pub get_widget_slot: usize, + /// `views::View::GetWidget`'s entry in every view's vtable, only to confirm the widget the root + /// of a view's tree names. `None` when the row does not have it: the widget is then found + /// through the view tree alone. + pub get_widget_slot: Option, /// What this build needs to show a page in developer mode. pub dev: DevMode, /// `ui::ColorProvider::GetColor(this, id)`. A provider makes a colour the first time it is asked @@ -504,11 +508,13 @@ pub struct SidePanel { /// toggle and its key. pub extensions_by_toggle: Option, /// The layout object keeps the host view at +0x38, the contents container at +0xb8, the side - /// panel at +0xc0 and the content shown while the side panel animates at +0xc8. - pub host_view: usize, + /// panel at +0xc0 and the content shown while the side panel animates at +0xc8. The host view, + /// the page area and the contents container are also found by the class names of the views + /// they hold, and a row value only confirms them; `None` when the row does not have one. + pub host_view: Option, /// The view behind the whole area below the top row, which the rail shares the height of. - pub page_area: usize, - pub contents_container: usize, + pub page_area: Option, + pub contents_container: Option, /// The browser's vertical tab list, which lies left of the page. pub vertical_tabs: usize, /// The browser's top row: toolbar, address bar and the buttons around it. @@ -600,8 +606,9 @@ pub struct Layout { pub layout_prologue: &'static [u8], pub set_bounds_rect_prologue: &'static [u8], /// `BrowserFrameViewWin` holds the `BrowserView`'s `BrowserWindow` subobject here, and the -/// subobject lies this many bytes after the start of the `BrowserView`. - pub frame_browser_window_offset: usize, + /// subobject lies this many bytes after the start of the `BrowserView`. The field is also found + /// as the one that holds the view the layout lays out, and a row value only confirms it. + pub frame_browser_window_offset: Option, pub browser_window_in_view: usize, pub caption_paint_prologue: &'static [u8], /// Colour ids `WindowsCaptionButton::PaintButtonContents` reads for its glyphs: active, inactive @@ -618,13 +625,10 @@ pub struct Layout { row_struct! { /// What a build needs to show a page in developer mode. pub struct DevMode { - /// The browser's own commands the developer bar runs, as `WM_COMMAND` ids, or zero when the build - /// has none. They are the commands of the Ctrl+Shift+C and Ctrl+Shift+J keys, read from the - /// browser's accelerator table. - pub inspect: u32, - pub console: u32, /// Colour ids of this build and the colours the row takes in developer mode, as `0xAARRGGBB`. /// An id that `colours` does not hold is asked of the provider first, like the ones that it does. + /// A build with no colours has no developer bar. The bar's browser commands are Chromium's + /// public command ids, which come from `browsers::chromium::commands`, not from the row. pub colours: &'static [(i32, u32)], /// `gfx::Font::Font(this, &name, size)` and `gfx::FontList::FontList(this, &font)`, which build /// the monospace font of the address. A build without them keeps the address in its own font. @@ -636,8 +640,6 @@ pub struct DevMode { } const UNCHECKED_DEV: DevMode = DevMode { - inspect: 0, - console: 0, colours: &[], font_new: ABSENT, font_new_prologue: &[0], @@ -654,7 +656,7 @@ const DEV_HOVER: u32 = 0x17FF_FFFF; const DEV_RIPPLE: u32 = 0x24FF_FFFF; const UNCHECKED_TOOLBAR: Toolbar = Toolbar { - get_widget_slot: 0, + get_widget_slot: None, dev: UNCHECKED_DEV, content_window_for_hwnd: ABSENT, widget_for_window: ABSENT, @@ -822,9 +824,9 @@ const UNCHECKED_SIDEPANEL: SidePanel = SidePanel { coordinator_browser: None, entry_key_size: None, extensions_by_toggle: None, - host_view: 0, - page_area: 0, - contents_container: 0, + host_view: None, + page_area: None, + contents_container: None, vertical_tabs: 0, toolbar: 0, contents_outline: 0, @@ -877,7 +879,7 @@ const UNCHECKED_LAYOUT: Layout = Layout { corner_paint_prologue: &[0], layout_prologue: &[0], set_bounds_rect_prologue: &[0], - frame_browser_window_offset: 0, + frame_browser_window_offset: None, browser_window_in_view: 0, caption_paint_prologue: &[0], caption_ink_ids: &[], diff --git a/rivet/runtime/windows/src/builds/samsung.rs b/rivet/runtime/windows/src/builds/samsung.rs index cf5ba5b..befdbd5 100644 --- a/rivet/runtime/windows/src/builds/samsung.rs +++ b/rivet/runtime/windows/src/builds/samsung.rs @@ -363,9 +363,9 @@ pub static SAMSUNG: Build = Build { coordinator_browser: Some(0x28), entry_key_size: Some(0x48), extensions_by_toggle: Some(true), - host_view: 0x30, - page_area: ABSENT, - contents_container: 0x80, + host_view: Some(0x30), + page_area: Some(ABSENT), + contents_container: Some(0x80), vertical_tabs: ABSENT, toolbar: 0x70, contents_outline: ABSENT, diff --git a/rivet/runtime/windows/src/builds/tests.rs b/rivet/runtime/windows/src/builds/tests.rs index d1d3757..3b4e8cd 100644 --- a/rivet/runtime/windows/src/builds/tests.rs +++ b/rivet/runtime/windows/src/builds/tests.rs @@ -217,7 +217,7 @@ fn nfr3_whale_maps_only_the_frame_hooks() { WHALE.layout.frame_browser_window_offset, WHALE.layout.browser_window_in_view ), - (0x338, 0x210) + (Some(0x338), 0x210) ); } @@ -633,11 +633,16 @@ fn fr3_a_build_with_tabs_checks_only_functions_it_has() { #[test] fn fr11_developer_commands_are_those_of_the_devtools_keys() { for build in [&CHROME, &EDGE, &BRAVE, &BRAVE_63] { - let dev = &build.toolbar.dev; - assert_eq!((dev.console, dev.inspect), (40005, 40023)); + assert!(!build.toolbar.dev.colours.is_empty()); + assert_eq!( + crate::features::dev_page::dev_commands(build), + (40023, 40005) + ); } // A build that was never checked offers no command and no bar. - assert_eq!(UNCHECKED_TOOLBAR.dev.inspect, 0); + for build in [&WHALE, &SAMSUNG, &CHROME_150] { + assert_eq!(crate::features::dev_page::dev_commands(build), (0, 0)); + } } #[test] @@ -911,8 +916,6 @@ fn fr13_every_build_that_shows_a_second_view_records_the_same_chromium_functions "size", "add_child", "set_contents", - "vparent", - "vchildren", "dtor_slot", ] { assert!( @@ -1068,7 +1071,7 @@ fn nfr3_chromium_157_rows_record_the_members_that_moved() { assert_eq!(build.tabs.browser_in_view, 0x558, "{name}"); assert_eq!(build.side_panel.browser_offset, 0x558, "{name}"); assert_eq!(build.layout.view_bounds_offset, 0x1C8, "{name}"); - assert_eq!(build.layout.frame_browser_window_offset, 0x338, "{name}"); + assert_eq!(build.layout.frame_browser_window_offset, Some(0x338), "{name}"); assert_eq!(build.layout.caption_container_offset, 0x3C0, "{name}"); assert_eq!( (build.layout.top_inset_slot, build.layout.caption_paint_slot), @@ -1203,7 +1206,7 @@ fn fr9_whale_split_functions_match_the_installed_build() { fn check_brave_toolbar_hooks(brave: &Build) { assert_eq!(brave.toolbar.skip, ""); - assert_eq!(brave.toolbar.get_widget_slot, 11); + assert_eq!(brave.toolbar.get_widget_slot, Some(11)); // The Brave row's caption colour ids sit 17 above Chrome's. assert_eq!( brave.layout.caption_ink_ids, @@ -1498,36 +1501,6 @@ fn fr12_edge_58_and_whale_57_record_the_page_view_that_moves_below_the_bar() { } } -#[cfg(windows)] -#[test] -fn nfr6_newest_row_of_each_ported_browser_has_its_symbol_key() { - // The row job ports a new build from the newest row of its browser, read through that row's - // public symbols. A newest row without its symbol key stops every later row of the browser. - let table: serde_json::Value = - serde_json::from_str(CHECKED_IN).expect("the checked-in table is JSON"); - let mut rows = crate::compiled_rows(); - rows.extend(table["rows"].as_array().cloned().unwrap_or_default()); - let version_of = |row: &serde_json::Value| -> Vec { - row["build"]["version"] - .as_array() - .map(|parts| parts.iter().filter_map(serde_json::Value::as_u64).collect()) - .unwrap_or_default() - }; - for browser in ["Google Chrome", "Microsoft Edge", "Brave"] { - let newest = rows - .iter() - .filter(|row| row["build"]["browser"] == browser && !row["frame"].is_null()) - .max_by_key(|row| version_of(row)) - .unwrap_or_else(|| panic!("{browser} has a row")); - let version: Vec = version_of(newest).iter().map(u64::to_string).collect(); - let key = format!("{browser} {}", version.join(".")); - assert!( - table["symbols"][key.as_str()].is_string(), - "{key} has no symbol key in rows/table.json" - ); - } -} - #[cfg(windows)] #[test] fn nfr6_recipes_read_every_row_value_they_cover_again_from_its_own_module() { @@ -1632,3 +1605,32 @@ fn nfr6_a_table_row_carries_the_page_tools_and_bubble_anchor_of_its_prior_row() )) ); } + +/// The parent and children of a view follow from its bounds by the member order of `views::View`. +/// Every row that recorded them by hand agrees, so the rule replaces them. +#[test] +fn nfr6_a_views_parent_and_children_are_not_row_values() { + let mut seen = 0; + for build in compiled() { + let Some(row) = build.app_window.as_ref() else { + continue; + }; + assert_eq!( + crate::features::app_window::view_fields(build), + Some((row.view_parent, row.view_children)), + "{} {:?}", + build.browser, + build.version + ); + seen += 1; + } + assert!(seen >= 5, "{seen}"); + // A row the bounds offset of which moves gives the parent and children of that build. + let mut moved = crate::row_data::RowValue::to_row(&CHROME_157_38); + moved["layout"]["view_bounds_offset"] = serde_json::json!("0x1d0"); + let moved = ::from_row(&moved).unwrap(); + assert_eq!( + crate::features::app_window::view_fields(&moved), + Some((0x1A0, 0x1A8)) + ); +} diff --git a/rivet/runtime/windows/src/builds/whale.rs b/rivet/runtime/windows/src/builds/whale.rs index 770434a..45931c8 100644 --- a/rivet/runtime/windows/src/builds/whale.rs +++ b/rivet/runtime/windows/src/builds/whale.rs @@ -358,9 +358,9 @@ pub static WHALE: Build = Build { coordinator_browser: None, entry_key_size: None, extensions_by_toggle: None, - host_view: 0x20, - page_area: 0x28, - contents_container: 0x90, + host_view: Some(0x20), + page_area: Some(0x28), + contents_container: Some(0x90), vertical_tabs: 0x60, toolbar: 0x40, contents_outline: 0x38, @@ -396,7 +396,7 @@ pub static WHALE: Build = Build { caption_vtable: 0xfb15d60, caption_size_slot: 74, caption_size: ABSENT, - frame_browser_window_offset: 0x338, + frame_browser_window_offset: Some(0x338), browser_window_in_view: 0x210, view_bounds_offset: 0x1C0, ..UNCHECKED_LAYOUT diff --git a/rivet/runtime/windows/src/chromium/live_tables.rs b/rivet/runtime/windows/src/chromium/live_tables.rs new file mode 100644 index 0000000..e81be26 --- /dev/null +++ b/rivet/runtime/windows/src/chromium/live_tables.rs @@ -0,0 +1,333 @@ +//! Reads the class tables of the views every browser window has from live objects, so a row needs +//! no class table address for them. +//! +//! A C++ object's first word is the table of its class. Every views class derives from +//! `views::View`, whose first base is `ui::LayerDelegate` (`ui/views/view.h`), so a view's first +//! word is its class's own table, the one whose slots hold the class's virtual functions. The views +//! are found by the class names their `ui::metadata` holds, through the view tree public Chromium +//! source describes: +//! +//! - `views::NonClientView` holds the window's frame view and its client view as children +//! (`ui/views/window/non_client_view.h`), and the client view of a browser window is the +//! `BrowserView` (`chrome/browser/ui/views/frame/browser_view.h`). So the frame view is the child of +//! the browser view's parent whose class name names a frame view. +//! - The frame view's caption buttons are `WindowsCaptionButton` views below it +//! (`chrome/browser/ui/views/frame/windows_caption_button.h`). +//! +//! A table is taken only when every live view of its class has the same one, it lies in read-only +//! data, its first slots lie in code, and, where the row also has the table, the two agree. + +use crate::chromium::window_views::Memory; + +/// The browser view's class name. +pub(crate) const BROWSER_VIEW: &str = "BrowserView"; +/// The caption buttons' class name. +pub(crate) const CAPTION_BUTTON: &str = "WindowsCaptionButton"; +/// The corner wedges' class name, where a browser has them. +pub(crate) const CORNER: &str = "CustomFloatingCorner"; + +/// How many views below the root are read at most. +const MOST_VIEWS: usize = 4000; +/// How many children one view may list. +const MOST_CHILDREN: usize = 256; + +/// The views a view lists at `children`, a `std::vector`. +fn children(memory: &M, view: usize, children: usize) -> Vec { + let (Some(begin), Some(end)) = ( + memory.word(view + children), + memory.word(view + children + 8), + ) else { + return Vec::new(); + }; + if begin == 0 || end < begin || (end - begin) % 8 != 0 || (end - begin) / 8 > MOST_CHILDREN { + return Vec::new(); + } + (begin..end) + .step_by(8) + .filter_map(|at| memory.word(at)) + .filter(|child| *child != 0) + .collect() +} + +/// Whether a class name names a browser frame view, as `BrowserFrameViewWin`, or a browser's own +/// subclass of one, as `EdgeBrowserFrameViewWin`, does. +pub(crate) fn is_frame_view(name: &str) -> bool { + name.contains("FrameView") +} + +/// Every view below `root`, the root included, whose class name `wanted` accepts, in tree order. +/// Only a child that names its parent at `parent` is followed, so a list read wrongly is not walked. +pub(crate) fn views_below( + memory: &M, + root: usize, + parent: usize, + wanted: &dyn Fn(&str) -> bool, +) -> Vec { + let mut found = Vec::new(); + let mut stack = vec![root]; + let mut seen = 0; + while let Some(view) = stack.pop() { + seen += 1; + if seen > MOST_VIEWS { + break; + } + if memory.class_name(view).is_some_and(|name| wanted(&name)) { + found.push(view); + } + let listed = children(memory, view, parent + 8); + for child in listed.into_iter().rev() { + if memory.word(child + parent) == Some(view) { + stack.push(child); + } + } + } + found +} + +/// The frame view of the window whose browser view is `browser_view`: the child of the browser +/// view's parent named as a frame view. `None` when there is not exactly one. +pub(crate) fn frame_view( + memory: &M, + browser_view: usize, + parent: usize, +) -> Option { + let holder = memory.word(browser_view + parent).filter(|holder| *holder != 0)?; + let frames: Vec = children(memory, holder, parent + 8) + .into_iter() + .filter(|child| *child != browser_view) + .filter(|child| memory.word(child + parent) == Some(holder)) + .filter(|child| { + memory + .class_name(*child) + .is_some_and(|name| is_frame_view(&name)) + }) + .collect(); + match frames.as_slice() { + [only] => Some(*only), + _ => None, + } +} + +/// The class table every one of `views` begins with, when they all begin with the same one and +/// `is_table` accepts it. `None` for no views or views of different tables. +pub(crate) fn shared_table( + memory: &M, + views: &[usize], + is_table: &dyn Fn(usize) -> bool, +) -> Option { + let mut tables = views.iter().map(|view| memory.word(*view)); + let first = tables.next()??; + (tables.all(|table| table == Some(first)) && is_table(first)).then_some(first) +} + +/// One class table from what the live views gave and what the row has, both as offsets in the +/// module. Both must agree when both are known; a row without one takes the live table, and a build +/// where no live view gave one keeps the row's. +pub(crate) fn settle_table(live: Option, row: Option) -> Option { + match (live, row) { + (Some(live), Some(row)) => (live == row).then_some(row), + (Some(live), None) => Some(live), + (None, row) => row, + } +} + +/// The class tables of the window's views, as addresses. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub(crate) struct Tables { + pub browser_view: Option, + pub frame: Option, + pub caption: Option, + pub corner: Option, +} + +/// Reads the tables from the window whose browser view is `browser_view`. +pub(crate) fn tables_of( + memory: &M, + browser_view: usize, + parent: usize, + is_table: &dyn Fn(usize) -> bool, +) -> Tables { + let named = |name: &str| memory.class_name(browser_view).as_deref() == Some(name); + if !named(BROWSER_VIEW) { + return Tables::default(); + } + let frame = frame_view(memory, browser_view, parent); + let below = |class: &'static str| { + frame + .map(|frame| views_below(memory, frame, parent, &|name| name == class)) + .unwrap_or_default() + }; + Tables { + browser_view: shared_table(memory, &[browser_view], is_table), + frame: frame.and_then(|frame| shared_table(memory, &[frame], is_table)), + caption: shared_table(memory, &below(CAPTION_BUTTON), is_table), + corner: shared_table(memory, &below(CORNER), is_table), + } +} + +/// The tables the live views of this process gave, as offsets in the module, once read. +static FOUND: std::sync::Mutex> = std::sync::Mutex::new(None); + +/// Reads the window's class tables once per process from the browser view the view walk found, +/// as offsets in the module at `base`. +pub(crate) fn note( + memory: &M, + browser_view: usize, + parent: usize, + base: usize, + is_table: &dyn Fn(usize) -> bool, +) -> Tables { + let mut found = FOUND.lock().unwrap_or_else(|error| error.into_inner()); + if let Some(tables) = *found { + return tables; + } + let live = tables_of(memory, browser_view, parent, is_table); + let offset = |table: Option| table.map(|table| table - base); + let tables = Tables { + browser_view: offset(live.browser_view), + frame: offset(live.frame), + caption: offset(live.caption), + corner: offset(live.corner), + }; + if tables != Tables::default() { + *found = Some(tables); + } + tables +} + +/// The class table a feature uses, as an offset in the module: the row's and the live one settled +/// together, or the address the build does not have when they disagree or neither is known. +pub(crate) fn table(row: usize, live: Option) -> usize { + let row = (row != crate::builds::ABSENT).then_some(row); + settle_table(live, row).unwrap_or(crate::builds::ABSENT) +} + +/// The tables read so far, as offsets in the module. +pub(crate) fn found() -> Tables { + FOUND + .lock() + .unwrap_or_else(|error| error.into_inner()) + .unwrap_or_default() +} + +#[cfg(test)] +mod tests { + use super::*; + use std::collections::HashMap; + + /// A view tree in plain memory: objects whose first word is a table, with parent and child + /// list fields where `views::View` keeps them, and class names by object. + #[derive(Default)] + struct Tree { + words: HashMap, + names: HashMap, + next_list: usize, + } + + const PARENT: usize = 0x198; + + impl Tree { + fn view(&mut self, at: usize, table: usize, name: &str) { + self.words.insert(at, table); + self.names.insert(at, name.to_owned()); + } + + fn adopt(&mut self, parent: usize, children: &[usize]) { + self.next_list += 0x1000; + let list = 0x90_0000 + self.next_list; + for (index, child) in children.iter().enumerate() { + self.words.insert(list + index * 8, *child); + self.words.insert(child + PARENT, parent); + } + self.words.insert(parent + PARENT + 8, list); + self.words.insert(parent + PARENT + 16, list + children.len() * 8); + } + } + + impl Memory for Tree { + fn word(&self, address: usize) -> Option { + Some(self.words.get(&address).copied().unwrap_or(0)) + } + fn class_name(&self, object: usize) -> Option { + self.names.get(&object).cloned() + } + } + + const BASE: usize = 0x1_8000_0000; + const NON_CLIENT: usize = 0x10_0000; + const BROWSER: usize = 0x20_0000; + const FRAME: usize = 0x30_0000; + const CONTAINER: usize = 0x40_0000; + const BUTTONS: [usize; 3] = [0x50_0000, 0x51_0000, 0x52_0000]; + + fn is_table(table: usize) -> bool { + table >= BASE + 0x100_0000 && table < BASE + 0x200_0000 + } + + /// A browser window's tree: a non-client view holding the frame view and the browser view, + /// and three caption buttons in a container of the frame view. + fn window(frame_name: &str, button_tables: [usize; 3]) -> Tree { + let mut tree = Tree::default(); + tree.view(NON_CLIENT, BASE + 0x100_1000, "NonClientView"); + tree.view(BROWSER, BASE + 0x100_2000, BROWSER_VIEW); + tree.view(FRAME, BASE + 0x100_3000, frame_name); + tree.view(CONTAINER, BASE + 0x100_4000, "WindowsCaptionButtonContainer"); + for (button, table) in BUTTONS.iter().zip(button_tables) { + tree.view(*button, table, CAPTION_BUTTON); + } + tree.adopt(NON_CLIENT, &[FRAME, BROWSER]); + tree.adopt(FRAME, &[CONTAINER]); + tree.adopt(CONTAINER, &BUTTONS); + tree + } + + #[test] + fn nfr6_a_views_class_table_is_read_from_its_live_views() { + let caption = BASE + 0x100_5000; + let tree = window("EdgeBrowserFrameViewWin", [caption; 3]); + let tables = tables_of(&tree, BROWSER, PARENT, &is_table); + assert_eq!(tables.browser_view, Some(BASE + 0x100_2000)); + assert_eq!(tables.frame, Some(BASE + 0x100_3000)); + assert_eq!(tables.caption, Some(caption)); + // The window has no corner wedges, so their table stays the row's. + assert_eq!(tables.corner, None); + let buttons = views_below(&tree, FRAME, PARENT, &|name| name == CAPTION_BUTTON); + assert_eq!(buttons, BUTTONS); + } + + #[test] + fn nfr6_live_views_of_one_class_must_share_their_table() { + let (one, other) = (BASE + 0x100_5000, BASE + 0x100_6000); + let tree = window("BrowserFrameViewWin", [one, one, other]); + assert_eq!(tables_of(&tree, BROWSER, PARENT, &is_table).caption, None); + // A first word outside the module's tables is no class table. + let tree = window("BrowserFrameViewWin", [0x1234; 3]); + assert_eq!(tables_of(&tree, BROWSER, PARENT, &is_table).caption, None); + // Only a view named as a frame view is the frame view. + let tree = window("OpaqueTitleBar", [BASE + 0x100_5000; 3]); + let tables = tables_of(&tree, BROWSER, PARENT, &is_table); + assert_eq!((tables.frame, tables.caption), (None, None)); + // The walk starts only from a view named as the browser view. + let tree = window("BrowserFrameViewWin", [BASE + 0x100_5000; 3]); + assert_eq!(tables_of(&tree, FRAME, PARENT, &is_table), Tables::default()); + } + + #[test] + fn nfr6_a_row_without_a_class_table_takes_the_live_one() { + let absent = crate::builds::ABSENT; + assert_eq!(table(absent, Some(0x1185_ff40)), 0x1185_ff40); + assert_eq!(table(0x1185_ff40, Some(0x1185_ff40)), 0x1185_ff40); + assert_eq!(table(0x1185_ff40, Some(0x11b5_2528)), absent); + assert_eq!(table(0x1185_ff40, None), 0x1185_ff40); + assert_eq!(table(absent, None), absent); + } + + #[test] + fn nfr6_a_live_table_and_the_rows_must_agree() { + assert_eq!(settle_table(Some(0x10), Some(0x10)), Some(0x10)); + assert_eq!(settle_table(Some(0x10), Some(0x18)), None); + assert_eq!(settle_table(Some(0x10), None), Some(0x10)); + assert_eq!(settle_table(None, Some(0x18)), Some(0x18)); + assert_eq!(settle_table(None, None), None); + } +} diff --git a/rivet/runtime/windows/src/chromium/mod.rs b/rivet/runtime/windows/src/chromium/mod.rs index 713c6d1..d604aca 100644 --- a/rivet/runtime/windows/src/chromium/mod.rs +++ b/rivet/runtime/windows/src/chromium/mod.rs @@ -1,5 +1,7 @@ pub(crate) mod image; +pub(crate) mod live_tables; pub(crate) mod memory; pub(crate) mod reflect; pub(crate) mod screen; pub(crate) mod view; +pub(crate) mod window_views; diff --git a/rivet/runtime/windows/src/chromium/reflect.rs b/rivet/runtime/windows/src/chromium/reflect.rs index 0e2843e..8f3e7be 100644 --- a/rivet/runtime/windows/src/chromium/reflect.rs +++ b/rivet/runtime/windows/src/chromium/reflect.rs @@ -149,6 +149,12 @@ pub(crate) fn class_name(ranges: &Ranges, view: usize) -> Option { class_name_within(ranges, view, 0x100) } +/// `class_name` for an object whose `MetaDataProvider` part may come late, after bases with +/// members of their own, as in `views::Widget` and in a `views::View` reached at its start. +pub(crate) fn class_name_far(ranges: &Ranges, object: usize) -> Option { + class_name_within(ranges, object, 0x800) +} + /// `class_name` for a pointer to the start of a `views::View`, whose `MetaDataProvider` part comes /// after seven other bases and their members, so it is looked for within `reach` bytes. fn class_name_within(ranges: &Ranges, view: usize, reach: usize) -> Option { @@ -437,6 +443,16 @@ pub(crate) fn find_cached( Ok((found, false)) } +/// The parent and view list fields an earlier walk found for the build with this fingerprint. +pub(crate) fn walked_fields(build: (u32, u32)) -> Option<(usize, usize)> { + CACHE.lock().ok().and_then(|cache| { + cache + .iter() + .find(|(key, _)| *key == build) + .map(|(_, offsets)| (offsets.parent, offsets.children)) + }) +} + /// The views a `std::vector` at `view + field` holds. fn children_of(view: usize, field: usize) -> Vec { let (Some(begin), Some(end)) = (word(view + field), word(view + field + 8)) else { diff --git a/rivet/runtime/windows/src/chromium/view.rs b/rivet/runtime/windows/src/chromium/view.rs index 3e3faa7..49621fd 100644 --- a/rivet/runtime/windows/src/chromium/view.rs +++ b/rivet/runtime/windows/src/chromium/view.rs @@ -24,6 +24,13 @@ impl View { (bounds_offset >= 0x30).then(|| bounds_offset - 0x30) } + /// Where a view keeps its children, from where it keeps its parent: `children_`, a + /// `std::vector` of views, comes right after `parent_` in `ui/views/view.h`. The view walk of + /// `reflect` finds the same pair at run time, each child naming the view 8 bytes before its list. + pub(crate) const fn children_offset(parent_offset: usize) -> usize { + parent_offset + 8 + } + pub(crate) const fn at(address: usize) -> Self { Self(address) } diff --git a/rivet/runtime/windows/src/chromium/window_views.rs b/rivet/runtime/windows/src/chromium/window_views.rs new file mode 100644 index 0000000..d9b511d --- /dev/null +++ b/rivet/runtime/windows/src/chromium/window_views.rs @@ -0,0 +1,718 @@ +//! Finds the views and the widget of a browser window by Chromium's own class names and the shape +//! of its view tree, so the window needs no member offset or vtable slot of its build for them. +//! +//! Each path below follows public Chromium source: +//! +//! - `BrowserViewLayout` keeps the views it lays out in `BrowserViewLayoutViews views_` +//! (`chrome/browser/ui/views/frame/layout/browser_view_layout.h`). Its `browser_view` is the +//! `BrowserView` as a `views::View`, `main_background_region` is a `MainBackgroundRegionView` and +//! `multi_contents_view` is a `MultiContentsView`. `BrowserView::BrowserView` adds both of the +//! latter with `AddChildView` (`browser_view.cc`), so each names the browser view as its parent. +//! The layout's fields are found by the class names of the views they point at. +//! - `BrowserFrameView` keeps the browser view in `views::ViewTracker browser_view_` +//! (`browser_frame_view.h`), which holds the `BrowserView`'s `views::View`: the view the layout +//! lays out. The frame view's field is the one that holds the same view. +//! - A view's widget is what `View::GetWidget` returns: the root view's `widget_`, a +//! `raw_ptr` of `views::internal::RootView` (`ui/views/widget/root_view.h`). The root +//! view is reached through `View::parent_` until it is null. `Widget` owns the root view in +//! `std::unique_ptr root_view_` (`ui/views/widget/widget.h`), so the root's +//! widget field is the one whose object points back at the root, and `Widget` carries its own +//! class metadata (`METADATA_HEADER_BASE(Widget)`). +//! +//! The logic reads memory through `Memory`, so it is tested on plain data. Where a build's row also +//! has the value, the two must agree or the value is not used. + +/// Reads of the browser's memory that the paths need. +pub(crate) trait Memory { + /// The word at `address`, or `None` when it cannot be read. + fn word(&self, address: usize) -> Option; + /// The `ui::metadata` class name of the object at `object`, when it has one. + fn class_name(&self, object: usize) -> Option; +} + +pub(crate) const BROWSER_VIEW: &str = "BrowserView"; +pub(crate) const MAIN_BACKGROUND_REGION: &str = "MainBackgroundRegionView"; +pub(crate) const MULTI_CONTENTS: &str = "MultiContentsView"; + +/// How far into a layout object its view fields are looked for. +const LAYOUT_REACH: usize = 0x400; +/// How far into a frame view, a root view or a widget a field is looked for. +const OBJECT_REACH: usize = 0x1000; +/// How many parents a view may have before the walk to its root gives up. +const MAX_DEPTH: usize = 64; + +/// The fields of `object` within `reach` that hold the one object named `class`, with that object. +/// Several fields may hold the same object. Two different objects of the class leave it unknown. +pub(crate) fn fields_naming( + memory: &M, + object: usize, + reach: usize, + class: &str, + accept: &dyn Fn(usize) -> bool, +) -> Option<(Vec, usize)> { + let mut found: Option<(Vec, usize)> = None; + for field in (0..reach).step_by(8) { + let Some(value) = memory.word(object + field) else { + break; + }; + if value == 0 || value == object || value % 8 != 0 { + continue; + } + if let Some((fields, known)) = found.as_mut() { + if *known == value { + fields.push(field); + continue; + } + } + if memory.class_name(value).as_deref() != Some(class) || !accept(value) { + continue; + } + if found.is_some() { + return None; + } + found = Some((vec![field], value)); + } + found +} + +/// The fields of a browser layout object, as offsets into it. +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub(crate) struct LayoutFields { + /// The fields that hold the `BrowserView`, the view the layout lays out. + pub host: Vec, + /// The fields that hold the `MainBackgroundRegionView`, the area below the top row. + pub page_area: Vec, + /// The fields that hold the `MultiContentsView`, which holds the pages. + pub contents: Vec, +} + +/// The view fields of the layout object at `layout`. With the parent field known, a page area or +/// contents view counts only when it names the browser view as its parent, as `AddChildView` left +/// it. `None` when no field holds a browser view. +pub(crate) fn layout_fields( + memory: &M, + layout: usize, + parent: Option, +) -> Option { + let anything = |_: usize| true; + let (host, view) = fields_naming(memory, layout, LAYOUT_REACH, BROWSER_VIEW, &anything)?; + let child_of_host = |child: usize| match parent { + Some(parent) => memory.word(child + parent) == Some(view), + None => true, + }; + let child = |class: &str| { + fields_naming(memory, layout, LAYOUT_REACH, class, &child_of_host) + .map_or_else(Vec::new, |(fields, _)| fields) + }; + Some(LayoutFields { + host, + page_area: child(MAIN_BACKGROUND_REGION), + contents: child(MULTI_CONTENTS), + }) +} + +/// The fields of the frame view at `frame` that hold its browser view. When the layout already +/// gave the browser view, the field must hold that one; otherwise every view in the browser view's +/// own list of children must name it as their parent, which tells a view's own address from a +/// pointer into another part of the object. +pub(crate) fn frame_fields( + memory: &M, + frame: usize, + host: Option, + parent: Option, +) -> Option> { + let is_host = |view: usize| match (host, parent) { + (Some(host), _) => view == host, + (None, Some(parent)) => is_listing_parent(memory, view, parent), + (None, None) => false, + }; + fields_naming(memory, frame, OBJECT_REACH, BROWSER_VIEW, &is_host).map(|(fields, _)| fields) +} + +/// Whether the view at `view` lists children that all name it at `parent`. `View::children_` is +/// the `std::vector` right after `parent_` in `ui/views/view.h`. +fn is_listing_parent(memory: &M, view: usize, parent: usize) -> bool { + let children = children(memory, view, parent + 8); + !children.is_empty() + && children + .iter() + .all(|child| memory.word(child + parent) == Some(view)) +} + +fn children(memory: &M, view: usize, field: usize) -> Vec { + let (Some(begin), Some(end)) = (memory.word(view + field), memory.word(view + field + 8)) else { + return Vec::new(); + }; + if begin == 0 || end < begin || (end - begin) % 8 != 0 || (end - begin) / 8 > 256 { + return Vec::new(); + } + (begin..end).step_by(8).filter_map(|at| memory.word(at)).collect() +} + +/// The root of the view tree `view` is in: the view whose parent is null. +pub(crate) fn root_of(memory: &M, view: usize, parent: usize) -> Option { + let mut at = view; + for _ in 0..MAX_DEPTH { + match memory.word(at + parent)? { + 0 => return Some(at), + next => at = next, + } + } + None +} + +/// Where a root view keeps its widget and where the widget keeps the root view, as offsets. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct WidgetFields { + pub widget_in_root: usize, + pub root_in_widget: usize, +} + +/// Finds the widget fields from a root view at `root`: a field of the root whose object has class +/// metadata and holds the root in turn, which is not a child view naming the root as its parent. +/// The root must be named as a `RootView` is. `None` when no field or two different ones qualify. +pub(crate) fn widget_fields( + memory: &M, + root: usize, + parent: usize, +) -> Option { + if !memory + .class_name(root) + .is_some_and(|name| name.ends_with("RootView")) + { + return None; + } + let listed = children(memory, root, parent + 8); + let mut found: Option<(usize, WidgetFields)> = None; + for field in (0..OBJECT_REACH).step_by(8) { + let Some(widget) = memory.word(root + field) else { + break; + }; + if widget == 0 || widget == root || widget % 8 != 0 || listed.contains(&widget) { + continue; + } + if found.is_some_and(|(known, _)| known == widget) { + continue; + } + // Reading stops where the object's memory ends. + let Some(back) = (0..OBJECT_REACH) + .step_by(8) + .map_while(|offset| memory.word(widget + offset).map(|word| (offset, word))) + .find(|(offset, word)| *offset != parent && *word == root) + .map(|(offset, _)| offset) + else { + continue; + }; + if memory.class_name(widget).is_none() { + continue; + } + let fields = WidgetFields { + widget_in_root: field, + root_in_widget: back, + }; + if found.is_some() { + return None; + } + found = Some((widget, fields)); + } + found.map(|(_, fields)| fields) +} + +/// The widget that holds `view` through fields already found: the root's widget, which must hold +/// the root back. Zero for a view in no widget, as `View::GetWidget` gives. +pub(crate) fn widget_of( + memory: &M, + view: usize, + parent: usize, + fields: WidgetFields, +) -> usize { + let Some(root) = root_of(memory, view, parent) else { + return 0; + }; + let Some(widget) = memory.word(root + fields.widget_in_root) else { + return 0; + }; + if widget == 0 || memory.word(widget + fields.root_in_widget) != Some(root) { + return 0; + } + widget +} + +/// One value from the fields reflection found and the value of the row. Both must agree when both +/// are known: the row's field must be one of the fields that hold the object. A row with no value +/// takes the reflected one, and a build reflection finds nothing in keeps its row's. +pub(crate) fn settle(reflected: Option<&[usize]>, row: Option) -> Option { + match (reflected.filter(|fields| !fields.is_empty()), row) { + (Some(fields), Some(row)) => fields.contains(&row).then_some(row), + (Some(fields), None) => fields.first().copied(), + (None, row) => row, + } +} + +/// The browser's own memory, read only where it can be read, with class names read through each +/// object's `ui::metadata` as `reflect` reads them. +pub(crate) struct Live { + ranges: crate::chromium::reflect::Ranges, +} + +impl Memory for Live { + fn word(&self, address: usize) -> Option { + crate::chromium::memory::readable(address, 8) + .then(|| unsafe { (address as *const usize).read() }) + } + fn class_name(&self, object: usize) -> Option { + crate::chromium::reflect::class_name_far(&self.ranges, object) + } +} + +/// The browser module's memory, once its sections are known. +fn live() -> Option<&'static Live> { + static LIVE: std::sync::OnceLock> = std::sync::OnceLock::new(); + LIVE.get_or_init(|| { + let image = crate::chromium::image::recorded()?; + let headers = + unsafe { std::slice::from_raw_parts(image.base as *const u8, 0x1000.min(image.size)) }; + let list = crate::chromium::reflect::sections(headers); + Some(Live { + ranges: crate::chromium::reflect::ranges_of(image.base, &list), + }) + }) + .as_ref() +} + +/// Where a view of this build keeps its parent. +fn parent_field() -> Option { + crate::builds::current() + .and_then(crate::features::app_window::view_fields) + .map(|(parent, _)| parent) +} + +/// What one value became: not looked for yet, or settled, with `None` for a value that neither +/// reflection nor the row gave, or where the two disagree. +#[derive(Clone, Copy, PartialEq, Eq)] +enum Settled { + Unknown, + Known(Option), +} + +/// How many times a value is looked for before the row alone decides it, so a build whose views +/// carry other names does not scan on every layout. +const TRIES: u32 = 8; + +struct Slot { + value: std::sync::Mutex<(Settled, u32)>, +} + +impl Slot { + const fn new() -> Self { + Self { + value: std::sync::Mutex::new((Settled::Unknown, 0)), + } + } + + /// The settled value, or what `find` and the row settle it to. A miss counts as a try, and + /// after the last try the row's value stands alone. + fn get(&self, row: Option, find: impl FnOnce() -> Option>) -> Option { + let mut value = self.value.lock().unwrap_or_else(|error| error.into_inner()); + if let Settled::Known(known) = value.0 { + return known; + } + match find() { + Some(fields) => { + let settled = settle(Some(fields.as_slice()), row); + if settled.is_none() { + crate::trace::trace(&format!( + "window views: reflection found {fields:x?} where the row has {row:x?}, so the value is not used" + )); + } + value.0 = Settled::Known(settled); + settled + } + None => { + value.1 += 1; + if value.1 >= TRIES { + value.0 = Settled::Known(row); + } + row + } + } + } + + fn known(&self, row: Option) -> Option { + match self.value.lock().unwrap_or_else(|error| error.into_inner()).0 { + Settled::Known(known) => known, + Settled::Unknown => row, + } + } +} + +static HOST: Slot = Slot::new(); +static PAGE_AREA: Slot = Slot::new(); +static CONTENTS: Slot = Slot::new(); +static FRAME: Slot = Slot::new(); + +/// A row value that is recorded: an offset the build has, not the absent marker. +fn recorded(value: Option) -> Option { + value.filter(|value| *value != crate::builds::ABSENT) +} + +fn side_panel_row() -> &'static crate::builds::SidePanel { + &crate::builds::get().side_panel +} + +/// Settles the view fields of the browser layout object at `layout`, which the layout hook hands +/// over, and returns the field that holds the view it lays out. Reads memory only. +pub(crate) unsafe fn host_field(layout: usize) -> Option { + let row = side_panel_row(); + let mut found: Option = None; + let mut fields = || { + if found.is_none() { + found = live().and_then(|live| layout_fields(live, layout, parent_field())); + } + found.clone() + }; + let host = HOST.get(recorded(row.host_view), || fields().map(|found| found.host)); + PAGE_AREA.get(recorded(row.page_area), || { + fields().map(|found| found.page_area).filter(|fields| !fields.is_empty()) + }); + CONTENTS.get(recorded(row.contents_container), || { + fields().map(|found| found.contents).filter(|fields| !fields.is_empty()) + }); + host +} + +/// The layout object's field that holds the area below the top row, or `ABSENT`. +pub(crate) fn page_area_field() -> usize { + PAGE_AREA + .known(recorded(side_panel_row().page_area)) + .unwrap_or(crate::builds::ABSENT) +} + +/// The layout object's field that holds the view of the pages, or `ABSENT`. +pub(crate) fn contents_field() -> usize { + CONTENTS + .known(recorded(side_panel_row().contents_container)) + .unwrap_or(crate::builds::ABSENT) +} + +/// The browser view the frame view at `frame` holds, the same view the browser's layout lays out. +/// `host` is that view when the layout already ran for the window. +pub(crate) unsafe fn frame_browser_view(frame: usize, host: Option) -> Option { + let row = recorded(crate::builds::get().layout.frame_browser_window_offset); + let field = FRAME.get(row, || { + live().and_then(|live| frame_fields(live, frame, host, parent_field())) + })?; + live()? + .word(frame + field) + .filter(|view| *view != 0) +} + +/// Starts the view walk of `reflect` from `window`'s own `HWNDMessageHandler`, once per process, on +/// a worker thread: it only reads memory, and its result is kept by build fingerprint for +/// `walked_view_fields`. +pub(crate) unsafe fn walk_views_once(window: windows_sys::Win32::Foundation::HWND) { + use windows_sys::Win32::UI::WindowsAndMessaging::{GetWindowLongPtrW, GWLP_USERDATA}; + static STARTED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + let (Some(live), Some(build)) = (live(), fingerprint()) else { + return; + }; + if STARTED.swap(true, std::sync::atomic::Ordering::AcqRel) { + return; + } + let ranges = live.ranges; + let handler = GetWindowLongPtrW(window, GWLP_USERDATA) as usize; + std::thread::spawn(move || { + let found = crate::chromium::reflect::find_cached( + &ranges, + handler, + std::time::Duration::from_millis(500), + build, + ); + match found { + Ok((found, _)) => crate::trace::trace(&format!( + "window views: the view walk found the parent at {:#x} and the view list at {:#x}", + found.parent, found.children + )), + Err(error) => crate::trace::trace(&format!("window views: {error}")), + } + }); +} + +/// Reads the class tables of `window`'s views from the live views once per process: the browser +/// view the view walk found, its frame view and the frame view's caption buttons and corners. The +/// walk's result is reused when the worker thread already has it. Reads memory only. +pub(crate) unsafe fn read_tables( + window: windows_sys::Win32::Foundation::HWND, +) -> crate::chromium::live_tables::Tables { + use crate::chromium::live_tables; + use windows_sys::Win32::UI::WindowsAndMessaging::{GetWindowLongPtrW, GWLP_USERDATA}; + let known = live_tables::found(); + if known != live_tables::Tables::default() { + return known; + } + let (Some(live), Some(build), Some(image)) = + (live(), fingerprint(), crate::chromium::image::recorded()) + else { + return known; + }; + let handler = GetWindowLongPtrW(window, GWLP_USERDATA) as usize; + let found = crate::chromium::reflect::find_cached( + &live.ranges, + handler, + std::time::Duration::from_millis(300), + build, + ); + let Ok((found, _)) = found else { + return known; + }; + let Some(browser_view) = found.browser_view else { + return known; + }; + let ranges = live.ranges; + // A class table lies in read-only data and its first slot holds code. + let is_table = |table: usize| { + table >= ranges.rdata.0 + && table < ranges.rdata.1 + && live + .word(table) + .is_some_and(|slot| slot >= ranges.text.0 && slot < ranges.text.1) + }; + let tables = live_tables::note(live, browser_view, found.parent, image.base, &is_table); + crate::trace::trace(&format!("window views: class tables from live views {tables:x?}")); + tables +} + +/// The browser module's fingerprint, which keys what the view walk found. +fn fingerprint() -> Option<(u32, u32)> { + let image = crate::chromium::image::recorded()?; + let headers = + unsafe { std::slice::from_raw_parts(image.base as *const u8, 0x1000.min(image.size)) }; + crate::chromium::reflect::fingerprint(headers) +} + +/// The parent and view list fields the view walk found in this process's build, once it ran. +pub(crate) fn walked_view_fields() -> Option<(usize, usize)> { + crate::chromium::reflect::walked_fields(fingerprint()?) +} + +/// What the widget path became: the fields found, or that it gave nothing. +static WIDGET: std::sync::Mutex<(Option>, u32)> = + std::sync::Mutex::new((None, 0)); + +/// The widget that holds `view`, found through the root of its view tree. When the build's row +/// has `View::GetWidget`'s vtable slot, `by_slot` calls it, once, to confirm the path: if the two +/// disagree neither is used, and a view is then in no widget. Without a path the slot decides. +pub(crate) unsafe fn widget_holding( + view: usize, + by_slot: Option<&dyn Fn(usize) -> usize>, +) -> usize { + let Some((live, parent)) = live().zip(parent_field()) else { + return by_slot.map_or(0, |slot| slot(view)); + }; + let mut state = WIDGET.lock().unwrap_or_else(|error| error.into_inner()); + if state.0.is_none() { + let fields = root_of(live, view, parent).and_then(|root| widget_fields(live, root, parent)); + match fields { + Some(fields) => { + let found = widget_of(live, view, parent, fields); + let agrees = by_slot.is_none_or(|slot| slot(view) == found); + if !agrees { + crate::trace::trace( + "window views: the widget path and the widget slot disagree, so neither is used", + ); + } + state.0 = Some(agrees.then_some(fields)); + } + None => { + state.1 += 1; + if state.1 >= TRIES { + state.0 = Some(None); + } + drop(state); + return by_slot.map_or(0, |slot| slot(view)); + } + } + } + let (settled, tries) = (state.0, state.1); + drop(state); + match settled { + Some(Some(fields)) => widget_of(live, view, parent, fields), + // The slot stands alone only where no path was found; a disagreement uses neither. + Some(None) if tries >= TRIES => by_slot.map_or(0, |slot| slot(view)), + _ => 0, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::collections::HashMap; + + /// Browser memory as plain words and the class names some objects carry. + #[derive(Default)] + struct Words { + words: HashMap, + names: HashMap, + } + + impl Words { + fn set(&mut self, address: usize, value: usize) { + self.words.insert(address, value); + } + fn name(&mut self, object: usize, name: &'static str) { + self.names.insert(object, name); + } + /// A list of views at `list` held by the `std::vector` at `view + field`. + fn list(&mut self, view: usize, field: usize, list: usize, items: &[usize]) { + self.set(view + field, list); + self.set(view + field + 8, list + items.len() * 8); + for (index, item) in items.iter().enumerate() { + self.set(list + index * 8, *item); + } + } + } + + impl Memory for Words { + fn word(&self, address: usize) -> Option { + // Objects are small blocks: every address inside one reads, zero where nothing is set. + let block = address & !0xFFFF; + self.words + .keys() + .any(|known| (known & !0xFFFF) == block) + .then(|| self.words.get(&address).copied().unwrap_or(0)) + } + fn class_name(&self, object: usize) -> Option { + self.names.get(&object).map(|name| (*name).to_owned()) + } + } + + const PARENT: usize = 0x198; + const LAYOUT: usize = 0x10_0000; + const BROWSER: usize = 0x20_0000; + const PAGE_AREA: usize = 0x30_0000; + const CONTENTS: usize = 0x40_0000; + const PANEL: usize = 0x50_0000; + const FRAME: usize = 0x60_0000; + const ROOT: usize = 0x70_0000; + const WIDGET: usize = 0x80_0000; + const LISTS: usize = 0x90_0000; + const OTHER: usize = 0xA0_0000; + + /// A browser window laid out as Chromium 157's layout keeps it, with its views at the fields + /// `BrowserViewLayoutViews` gives them and the tree `BrowserView` builds. + fn window(host: usize, page_area: usize, contents: usize) -> Words { + let mut memory = Words::default(); + memory.set(LAYOUT, 0x1234); // the layout's vtable + memory.set(LAYOUT + host, BROWSER); + memory.set(LAYOUT + page_area, PAGE_AREA); + memory.set(LAYOUT + contents, CONTENTS); + memory.set(LAYOUT + 0xC0, PANEL); + memory.name(BROWSER, "BrowserView"); + memory.name(PAGE_AREA, "MainBackgroundRegionView"); + memory.name(CONTENTS, "MultiContentsView"); + memory.name(PANEL, "SidePanel"); + for child in [PAGE_AREA, CONTENTS, PANEL] { + memory.set(child + PARENT, BROWSER); + } + memory.list(BROWSER, PARENT + 8, LISTS, &[PAGE_AREA, CONTENTS, PANEL]); + // The browser view sits in the root view, which belongs to the widget. + memory.set(BROWSER + PARENT, ROOT); + memory.set(ROOT + PARENT, 0); + memory.name(ROOT, "BrowserRootView"); + memory.list(ROOT, PARENT + 8, LISTS + 0x100, &[BROWSER]); + memory.set(ROOT + 0x3A0, WIDGET); + memory.set(WIDGET + 0x1D8, ROOT); + memory.name(WIDGET, "BrowserWidget"); + // The frame view's tracker of the browser view, and the tracker's observation of it. + memory.set(FRAME + 0x338, 0x5678); + memory.set(FRAME + 0x340, BROWSER); + memory.set(FRAME + 0x348, BROWSER); + memory + } + + #[test] + fn nfr6_the_side_panel_views_are_found_by_class_name() { + // Chrome 157 keeps them at 0x38, 0x50 and 0xb8; Edge has its own fields between them. + for (host, page_area, contents) in [(0x38, 0x50, 0xB8), (0x38, 0x58, 0xD8)] { + let memory = window(host, page_area, contents); + let fields = layout_fields(&memory, LAYOUT, Some(PARENT)).expect("a browser view"); + assert_eq!(fields.host, [host]); + assert_eq!(fields.page_area, [page_area]); + assert_eq!(fields.contents, [contents]); + } + } + + #[test] + fn nfr6_a_layout_view_that_is_not_the_browser_views_child_is_not_taken() { + let mut memory = window(0x38, 0x50, 0xB8); + memory.set(CONTENTS + PARENT, OTHER); + let fields = layout_fields(&memory, LAYOUT, Some(PARENT)).unwrap(); + assert!(fields.contents.is_empty()); + assert_eq!(fields.page_area, [0x50]); + // A layout with no browser view has no fields at all. + let mut bare = Words::default(); + bare.set(LAYOUT + 0x38, PAGE_AREA); + bare.name(PAGE_AREA, "MainBackgroundRegionView"); + assert_eq!(layout_fields(&bare, LAYOUT, Some(PARENT)), None); + } + + #[test] + fn nfr6_two_different_views_of_one_class_leave_the_field_unknown() { + let mut memory = window(0x38, 0x50, 0xB8); + memory.set(LAYOUT + 0x100, OTHER); + memory.name(OTHER, "MultiContentsView"); + memory.set(OTHER + PARENT, BROWSER); + let fields = layout_fields(&memory, LAYOUT, Some(PARENT)).unwrap(); + assert!(fields.contents.is_empty()); + } + + #[test] + fn nfr6_the_frame_view_field_is_the_one_holding_the_browser_view() { + let memory = window(0x38, 0x50, 0xB8); + // With the laid out view known, and without it through the browser view's own children. + assert_eq!( + frame_fields(&memory, FRAME, Some(BROWSER), Some(PARENT)), + Some(vec![0x340, 0x348]) + ); + assert_eq!( + frame_fields(&memory, FRAME, None, Some(PARENT)), + Some(vec![0x340, 0x348]) + ); + assert_eq!(frame_fields(&memory, FRAME, Some(OTHER), Some(PARENT)), None); + assert_eq!(frame_fields(&memory, FRAME, None, None), None); + } + + #[test] + fn nfr6_a_views_widget_is_found_without_a_vtable_slot() { + let memory = window(0x38, 0x50, 0xB8); + assert_eq!(root_of(&memory, CONTENTS, PARENT), Some(ROOT)); + let fields = widget_fields(&memory, ROOT, PARENT).expect("the widget fields"); + assert_eq!( + fields, + WidgetFields { + widget_in_root: 0x3A0, + root_in_widget: 0x1D8 + } + ); + assert_eq!(widget_of(&memory, CONTENTS, PARENT, fields), WIDGET); + assert_eq!(widget_of(&memory, BROWSER, PARENT, fields), WIDGET); + // A view whose tree has no widget, or whose root is not the widget's, has none. + let mut loose = window(0x38, 0x50, 0xB8); + loose.set(WIDGET + 0x1D8, OTHER); + assert_eq!(widget_of(&loose, CONTENTS, PARENT, fields), 0); + // Only a root view gives the fields. + assert_eq!(widget_fields(&memory, BROWSER, PARENT), None); + } + + #[test] + fn nfr6_a_reflected_value_must_agree_with_the_row() { + let both: &[usize] = &[0x340, 0x348]; + assert_eq!(settle(Some(both), Some(0x340)), Some(0x340)); + assert_eq!(settle(Some(both), Some(0x348)), Some(0x348)); + assert_eq!(settle(Some(&both[..1]), Some(0x3B0)), None); + assert_eq!(settle(Some(&[0x3B0][..]), None), Some(0x3B0)); + assert_eq!(settle(None, Some(0x38)), Some(0x38)); + assert_eq!(settle(Some(&[][..]), Some(0x38)), Some(0x38)); + assert_eq!(settle(None, None), None); + } +} diff --git a/rivet/runtime/windows/src/features/address_bar.rs b/rivet/runtime/windows/src/features/address_bar.rs index 6bec37f..857437d 100644 --- a/rivet/runtime/windows/src/features/address_bar.rs +++ b/rivet/runtime/windows/src/features/address_bar.rs @@ -421,7 +421,7 @@ pub(crate) unsafe fn on_page_area(area: &mut crate::hooks::page_area::PageArea) return; } let row = &crate::builds::get().side_panel; - let Some(contents) = area.bounds(row.contents_container) else { + let Some(contents) = area.contents() else { crate::trace::trace_changed( "bar layout", "address bar: the layout has no page container", diff --git a/rivet/runtime/windows/src/features/app_window.rs b/rivet/runtime/windows/src/features/app_window.rs index 87dd96e..3682695 100644 --- a/rivet/runtime/windows/src/features/app_window.rs +++ b/rivet/runtime/windows/src/features/app_window.rs @@ -12,7 +12,37 @@ use crate::builds::AppWindow; /// An address or value of the app window, as an RVA of the browser module where it is a function. pub(crate) fn recorded(name: &str) -> Option { - switched(name).or_else(|| checked_row().and_then(|row| row_value(row, name))) + switched(name).or_else(|| match name { + "vparent" | "vchildren" => { + checked_row()?; + let (parent, children) = view_fields(crate::builds::current()?)?; + Some(if name == "vparent" { parent } else { children }) + } + _ => checked_row().and_then(|row| row_value(row, name)), + }) +} + +/// Where a view of `build` keeps its parent and its children. The view walk of `reflect` finds them +/// at run time from the window's own views: each child of the browser view names it as its parent +/// right before its list of children. They also follow from where the view keeps its bounds by the +/// member order of `views::View`, which confirms the walk. So they are not values of the build's +/// row. +pub(crate) fn view_fields(build: &crate::builds::Build) -> Option<(usize, usize)> { + use crate::chromium::view::View; + let by_bounds = View::parent_offset(build.layout.view_bounds_offset) + .map(|parent| (parent, View::children_offset(parent))); + agreed_view_fields(crate::chromium::window_views::walked_view_fields(), by_bounds) +} + +/// The view fields from the walk and from the bounds offset. Both must agree when both are known. +pub(crate) fn agreed_view_fields( + walked: Option<(usize, usize)>, + by_bounds: Option<(usize, usize)>, +) -> Option<(usize, usize)> { + match (walked, by_bounds) { + (Some(walked), Some(by_bounds)) => (walked == by_bounds).then_some(walked), + (walked, by_bounds) => walked.or(by_bounds), + } } /// A value named in the test profile's `rivet-spike.txt`. @@ -121,8 +151,6 @@ pub(crate) fn row_value(row: &AppWindow, name: &str) -> Option { "kind" => Some(row.browser_type), "detach" => row.detach, "insert" => row.insert, - "vparent" => Some(row.view_parent), - "vchildren" => Some(row.view_children), "new" => Some(row.operator_new), "ctor" => Some(row.web_view_ctor), "size" => Some(row.web_view_size), @@ -702,6 +730,21 @@ pub(crate) fn install_features() { mod tests { use super::*; + #[test] + fn nfr6_the_view_walk_gives_the_view_fields_and_the_bounds_offset_confirms_them() { + // The walk and the bounds offset agree on Chromium 156 and later. + assert_eq!( + agreed_view_fields(Some((0x198, 0x1A0)), Some((0x198, 0x1A0))), + Some((0x198, 0x1A0)) + ); + // A walk that disagrees with the bounds offset leaves the fields unknown. + assert_eq!(agreed_view_fields(Some((0x190, 0x198)), Some((0x198, 0x1A0))), None); + // Either alone stands until the other is known. + assert_eq!(agreed_view_fields(Some((0x190, 0x198)), None), Some((0x190, 0x198))); + assert_eq!(agreed_view_fields(None, Some((0x198, 0x1A0))), Some((0x198, 0x1A0))); + assert_eq!(agreed_view_fields(None, None), None); + } + #[test] fn fr9_the_mini_window_is_the_window_the_browser_created_for_the_tab() { let tooltip = 0x10 as HWND; diff --git a/rivet/runtime/windows/src/features/dev_page.rs b/rivet/runtime/windows/src/features/dev_page.rs index 54c12c1..084df23 100644 --- a/rivet/runtime/windows/src/features/dev_page.rs +++ b/rivet/runtime/windows/src/features/dev_page.rs @@ -47,18 +47,28 @@ pub(crate) unsafe fn toggle_dev_mode(window: HWND) -> bool { /// The buttons the developer bar of this build offers, with the command each runs. pub(crate) fn dev_actions() -> Vec<(Action, u32)> { - let dev = &crate::builds::get().toolbar.dev; + let (inspect, console) = dev_commands(crate::builds::get()); // Chromium has no browser command that opens DevTools on its Network or Performance panel. // Rivet opens them itself through Chromium's DevTools functions where the build records them. let panels = panel_buttons(crate::features::dev_tools::available()); crate::dev_mode::bar_buttons(&[ - (Action::Inspect, dev.inspect), - (Action::Console, dev.console), + (Action::Inspect, inspect), + (Action::Console, console), (Action::Network, panels), (Action::Performance, panels), ]) } +/// The inspect and console commands the developer bar of `build` runs, or zero for each. They are +/// Chromium's public command ids for the build's milestone. Only a build whose developer colours +/// were checked has the bar at all. +pub(crate) fn dev_commands(build: &crate::builds::Build) -> (u32, u32) { + if build.toolbar.dev.colours.is_empty() { + return (0, 0); + } + crate::browsers::chromium::commands::dev_tools(build.browser, build.version) +} + /// The marker the Network and Performance buttons carry when Rivet can open their panels. Rivet /// runs them itself, so it is never sent to the browser as a command. pub(crate) const PANEL_BUTTON: u32 = u32::MAX; diff --git a/rivet/runtime/windows/src/features/frame_layout.rs b/rivet/runtime/windows/src/features/frame_layout.rs index 6cd592e..310d70a 100644 --- a/rivet/runtime/windows/src/features/frame_layout.rs +++ b/rivet/runtime/windows/src/features/frame_layout.rs @@ -129,6 +129,30 @@ pub(crate) fn needs_top_inset(build: &Build) -> Needs { Needs::targets(vec![Target::new("top inset", build.layout.top_inset, &[])]) } +/// The frame view's class table: the row's, read again from the window's live frame view. +fn frame_table() -> usize { + use crate::chromium::live_tables; + live_tables::table(bv().frame_vtable, live_tables::found().frame) +} + +/// The caption buttons' class table, settled with the live buttons' as `frame_table` is. +fn caption_table() -> usize { + use crate::chromium::live_tables; + live_tables::table(bv().caption_vtable, live_tables::found().caption) +} + +/// The corner wedges' class table, settled with the live wedges' as `frame_table` is. +fn corner_table() -> usize { + use crate::chromium::live_tables; + live_tables::table(bv().corner_vtable, live_tables::found().corner) +} + +/// The browser view's class table, settled with the live browser view's as `frame_table` is. +fn browser_view_table() -> usize { + use crate::chromium::live_tables; + live_tables::table(bv().browser_view_view_vtable, live_tables::found().browser_view) +} + fn recorded_image() -> Result { image::recorded().ok_or_else(|| "this browser build is not supported yet".to_owned()) } @@ -394,7 +418,7 @@ unsafe extern "system" fn corner_paint(view: *const c_void, canvas: *const c_voi } unsafe fn install_corner_paint(image: &Image) -> Result<(), String> { - let slot_rva = bv().corner_vtable + bv().on_paint_slot * 8; + let slot_rva = corner_table() + bv().on_paint_slot * 8; if !image.has(bv().corner_paint, bv().corner_paint_prologue) { return Err("this browser build has no recorded corner paint".into()); } @@ -420,7 +444,7 @@ pub unsafe fn install_frame_paint(paint: Paint) -> Result<(), String> { return Err("this browser build has no verified colour provider layout".into()); } let image = recorded_image()?; - let slot_rva = bv().frame_vtable + bv().on_paint_slot * 8; + let slot_rva = frame_table() + bv().on_paint_slot * 8; if !image.points_to(slot_rva, bv().on_paint) { return Err("this browser build has no recorded frame paint".into()); } @@ -765,7 +789,7 @@ pub unsafe fn install_caption_paint() -> Result<(), String> { return Err("this browser build has no verified colour provider layout".into()); } let image = recorded_image()?; - let slot_rva = bv().caption_vtable + bv().caption_paint_slot * 8; + let slot_rva = caption_table() + bv().caption_paint_slot * 8; if !image.points_to(slot_rva, bv().caption_paint) || !image.has(bv().caption_paint, bv().caption_paint_prologue) || !image.has(bv().get_color_provider, bv().get_color_provider_prologue) @@ -805,7 +829,7 @@ pub unsafe fn install_caption_height(height: CaptionHeight) -> Result<(), String return Ok(()); } let image = recorded_image()?; - let slot_rva = bv().caption_vtable + bv().caption_size_slot * 8; + let slot_rva = caption_table() + bv().caption_size_slot * 8; if !image.points_to(slot_rva, bv().caption_size) { return Err("this browser build has no recorded caption button size".into()); } @@ -851,7 +875,7 @@ pub unsafe fn install_top_inset(extra: ExtraTop) -> Result<(), String> { return Ok(()); } let image = recorded_image()?; - let slot_rva = bv().frame_vtable + bv().top_inset_slot * 8; + let slot_rva = frame_table() + bv().top_inset_slot * 8; if !image.points_to(slot_rva, bv().top_inset) { return Err("this browser build has no recorded top inset".into()); } @@ -888,12 +912,13 @@ unsafe fn clip_panel(frame: *mut c_void) { let Some((set_layer, set_radius, set_masks)) = PANEL_LAYER.get() else { return; }; - // BrowserFrameViewWin holds BrowserView's BrowserWindow subobject at +0x340. - let browser_window = - *(frame.cast::().add(bv().frame_browser_window_offset) as *const *mut u8); - if browser_window.is_null() { + // The frame view keeps BrowserView's BrowserWindow subobject in its tracker of the browser view. + let Some(browser_window) = + crate::chromium::window_views::frame_browser_view(frame as usize, None) + .map(|view| view as *mut u8) + else { return; - } + }; let browser = browser_window .sub(bv().browser_window_in_view) .cast::(); @@ -902,7 +927,7 @@ unsafe fn clip_panel(frame: *mut c_void) { }; let base = image.base; let Some(index) = - (0..0x100).find(|index| *browser.add(*index) == base + bv().browser_view_view_vtable) + (0..0x100).find(|index| *browser.add(*index) == base + browser_view_table()) else { return; }; @@ -1051,7 +1076,7 @@ pub unsafe fn install_caption_layout(height: CaptionHeight) -> Result<(), String return Ok(()); } let image = recorded_image()?; - let slot_rva = bv().frame_vtable + bv().layout_slot * 8; + let slot_rva = frame_table() + bv().layout_slot * 8; if !image.points_to(slot_rva, bv().layout) { return Err("this browser build has no recorded frame layout".into()); } diff --git a/rivet/runtime/windows/src/features/pane_infobar.rs b/rivet/runtime/windows/src/features/pane_infobar.rs index 0897948..313323f 100644 --- a/rivet/runtime/windows/src/features/pane_infobar.rs +++ b/rivet/runtime/windows/src/features/pane_infobar.rs @@ -56,8 +56,7 @@ pub(crate) fn narrow(bar: [i32; 4], contents: [i32; 4], columns: (i32, i32)) -> /// The page area step: the bars above the page take the main pane's columns while a split is /// open, and their height is kept for the second view's placement. pub(crate) unsafe fn on_page_area(area: &mut crate::hooks::page_area::PageArea) { - let row = &crate::builds::get().side_panel; - let Some(contents) = area.bounds(row.contents_container) else { + let Some(contents) = area.contents() else { return; }; let page = *contents; diff --git a/rivet/runtime/windows/src/features/side_panel.rs b/rivet/runtime/windows/src/features/side_panel.rs index 8df8f8c..0e6e622 100644 --- a/rivet/runtime/windows/src/features/side_panel.rs +++ b/rivet/runtime/windows/src/features/side_panel.rs @@ -133,12 +133,7 @@ pub unsafe fn objects(window: HWND) -> Option { if base == 0 || !checked() { return None; } - let browser_window = match frame_layout::frame_view(window) { - Some(frame) => { - *((frame + crate::builds::get().layout.frame_browser_window_offset) as *const usize) - } - None => page_area::laid_out_host(window)?, - }; + let browser_window = browser_window_of(window)?; let cached = FOUND.with(|found| found.borrow().get(&(window as isize)).copied()); let vtables = [ bv().coordinator_vtable, @@ -162,15 +157,19 @@ pub unsafe fn objects(window: HWND) -> Option { Some(found) } +/// The `BrowserWindow` subobject of a window's `BrowserView`, which the frame view keeps in its +/// tracker of the browser view. A window whose frame view is not known has it from the layout, +/// which lays that same view out. +unsafe fn browser_window_of(window: HWND) -> Option { + let host = page_area::laid_out_host(window); + match frame_layout::frame_view(window) { + Some(frame) => crate::chromium::window_views::frame_browser_view(frame, host), + None => host, + } +} + unsafe fn find_objects(window: HWND, base: usize) -> Option { - // The frame view holds the `BrowserWindow` subobject of `BrowserView` at +0x340. A build whose - // frame view is not recorded has it from the layout, which lays that same view out. - let browser_window = match frame_layout::frame_view(window) { - Some(frame) => { - *((frame + crate::builds::get().layout.frame_browser_window_offset) as *const usize) - } - None => page_area::laid_out_host(window)?, - }; + let browser_window = browser_window_of(window)?; if !readable(browser_window, 8) { return None; } @@ -905,13 +904,13 @@ pub(crate) unsafe fn on_page_area(area: &mut PageArea) { let rail = rail_of(area.window); area.rail = rail; if rail > 0 { - if let Some(contents) = area.bounds(bv().contents_container) { + if let Some(contents) = area.contents() { let side_panel = area.bounds(bv().side_panel); let animation = area.bounds(bv().side_panel_animation); let outline = area.bounds(bv().contents_outline); let panel_view = area.field(bv().side_panel); let page = area - .bounds(bv().page_area) + .page() .map(|bounds| *bounds) .unwrap_or([0; 4]); // The page starts where the browser's vertical tab list ends. @@ -1228,7 +1227,7 @@ mod tests { // The traced app window layout keeps the page area, the top row and the page container here. assert_eq!( (whale.page_area, whale.toolbar, whale.contents_container), - (0x28, 0x40, 0x90) + (Some(0x28), 0x40, Some(0x90)) ); let samsung = row("Samsung Internet"); assert!(samsung.supported); @@ -1236,7 +1235,7 @@ mod tests { assert_eq!(samsung.old_layout, Some(true)); assert_eq!( (samsung.contents_container, samsung.side_panel), - (0x80, 0x98) + (Some(0x80), 0x98) ); } diff --git a/rivet/runtime/windows/src/features/sidebar_page.rs b/rivet/runtime/windows/src/features/sidebar_page.rs index f3854b6..733935a 100644 --- a/rivet/runtime/windows/src/features/sidebar_page.rs +++ b/rivet/runtime/windows/src/features/sidebar_page.rs @@ -174,7 +174,7 @@ pub(crate) unsafe fn on_page_area(area: &mut crate::hooks::page_area::PageArea) return; } let row = &crate::builds::get().side_panel; - let page = area.bounds(row.page_area).map(|bounds| *bounds); + let page = area.page().map(|bounds| *bounds); let Some(page_right) = page.map(|page| page[0] + page[2]) else { return; }; @@ -182,7 +182,7 @@ pub(crate) unsafe fn on_page_area(area: &mut crate::hooks::page_area::PageArea) let Some(side_panel) = area.bounds(offset) else { continue; }; - if let Some(contents) = area.bounds(row.contents_container) { + if let Some(contents) = area.contents() { float_over(&mut *contents, &mut *side_panel, page_right); } if let Some(outline) = area.bounds(row.contents_outline) { diff --git a/rivet/runtime/windows/src/features/tabs.rs b/rivet/runtime/windows/src/features/tabs.rs index 66ca561..ebcacdb 100644 --- a/rivet/runtime/windows/src/features/tabs.rs +++ b/rivet/runtime/windows/src/features/tabs.rs @@ -628,25 +628,58 @@ pub unsafe fn new_tab(window: HWND) -> bool { opened } +/// How a Rivet window opens a tab in a build. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum NewTabBy { + /// The browser's own new tab command, sent as its menu sends it. It ends in `chrome::NewTab` + /// like the function below, on the same thread, so the new tab hook sees Rivet's own tab. + Command(usize), + /// `chrome::NewTab` itself, for a build whose milestone has no command id that was read. + Function(usize), +} + +/// How a Rivet window of `build` opens a tab: the public command when the build's milestone has +/// one, else the recorded function, else not at all. +pub(crate) fn new_tab_by(build: &Build) -> Option { + let command = crate::browsers::chromium::commands::new_tab(build.browser, build.version); + choose_new_tab(command, build.tabs.new_tab, build.tabs.new_tab_address) +} + +fn choose_new_tab( + command: Option, + new_tab: usize, + new_tab_address: Option, +) -> Option { + if let Some(id) = command { + return Some(NewTabBy::Command(id)); + } + // A build that passes its window interface takes another first argument than the browser. + (new_tab != crate::builds::ABSENT && new_tab_address.is_none()) + .then_some(NewTabBy::Function(new_tab)) +} + unsafe fn open_new_tab(window: HWND) -> bool { - let Some(base) = base() else { return false }; - if bv().new_tab == crate::builds::ABSENT || bv().new_tab_address.is_some() { - // The browser's own new-tab command, as its menu sends it. - const IDC_NEW_TAB: usize = 34014; - windows_sys::Win32::UI::WindowsAndMessaging::SendMessageW( - window, - windows_sys::Win32::UI::WindowsAndMessaging::WM_COMMAND, - IDC_NEW_TAB, - 0, - ); - return true; + match new_tab_by(crate::builds::get()) { + Some(NewTabBy::Command(id)) => { + windows_sys::Win32::UI::WindowsAndMessaging::SendMessageW( + window, + windows_sys::Win32::UI::WindowsAndMessaging::WM_COMMAND, + id, + 0, + ); + true + } + Some(NewTabBy::Function(rva)) => { + let Some(base) = base() else { return false }; + let Some(browser) = browser(base, window) else { + return false; + }; + // Chrome accepts the new-tab command source value 5. + function::(base, rva)(browser, 5); + true + } + None => false, } - let Some(browser) = browser(base, window) else { - return false; - }; - // Chrome accepts the new-tab command source value 5. - function::(base, bv().new_tab)(browser, 5); - true } /// One-shot diagnostic for the isolated test profile. @@ -914,3 +947,42 @@ mod first_page_tests { assert_eq!(first_page(true, false, false, start()), None); } } + +#[cfg(test)] +mod new_tab_tests { + use super::{choose_new_tab, new_tab_by, NewTabBy}; + use crate::builds; + + #[test] + fn nfr6_a_new_tab_uses_the_public_command() { + // Every build Rivet knows opens a tab with the browser's own command, also those that + // record `chrome::NewTab`, so the function is only the new tab hook's. + for build in builds::compiled() { + assert_eq!( + new_tab_by(build), + Some(NewTabBy::Command(34014)), + "{} {:?}", + build.browser, + build.version + ); + } + // A milestone whose header was not read has no command, and only a recorded function that + // takes the browser itself opens a tab there. + assert_eq!( + crate::browsers::chromium::commands::new_tab("Google Chrome", [160, 0, 1, 1]), + None + ); + let function = 0x0123_4560; + assert_eq!( + choose_new_tab(None, function, None), + Some(NewTabBy::Function(function)) + ); + assert_eq!(choose_new_tab(None, function, Some(0x68)), None); + assert_eq!(choose_new_tab(None, builds::ABSENT, None), None); + // With a command the function is never called, recorded or not. + assert_eq!( + choose_new_tab(Some(34014), function, None), + Some(NewTabBy::Command(34014)) + ); + } +} diff --git a/rivet/runtime/windows/src/features/widgets.rs b/rivet/runtime/windows/src/features/widgets.rs index 71459fa..54a6d97 100644 --- a/rivet/runtime/windows/src/features/widgets.rs +++ b/rivet/runtime/windows/src/features/widgets.rs @@ -67,13 +67,22 @@ pub(crate) fn rivet_widgets() -> Vec { all().iter().map(|(widget, _)| *widget).collect() } -/// The widget that holds `view`, which must point at a `views::View`: `View::GetWidget` through -/// the view's own vtable. +/// The widget that holds `view`, which must point at a `views::View`: the widget the root of its +/// view tree names, as `View::GetWidget` returns it. Where the row has `GetWidget`'s vtable slot, +/// the slot confirms the path once. pub(crate) unsafe fn widget_holding(view: *mut c_void) -> usize { - let table = *(view as *const usize); - let get_widget: Pointer = - std::mem::transmute(*(table as *const usize).add(row().get_widget_slot)); - get_widget(view) as usize + let by_slot = |view: usize| { + let Some(slot) = row().get_widget_slot else { + return 0; + }; + let table = *(view as *const usize); + let get_widget: Pointer = std::mem::transmute(*(table as *const usize).add(slot)); + get_widget(view as *const c_void) as usize + }; + let by_slot: Option<&dyn Fn(usize) -> usize> = row() + .get_widget_slot + .map(|_| &by_slot as &dyn Fn(usize) -> usize); + crate::chromium::window_views::widget_holding(view as usize, by_slot) } #[cfg(test)] diff --git a/rivet/runtime/windows/src/frame.rs b/rivet/runtime/windows/src/frame.rs index c625721..f031b6f 100644 --- a/rivet/runtime/windows/src/frame.rs +++ b/rivet/runtime/windows/src/frame.rs @@ -2810,6 +2810,8 @@ pub unsafe fn attach(window: HWND) -> bool { layout_dpi(window) )); crate::features::control::serve(window); + // The window's views are found by their class names from here on, on a worker thread. + crate::chromium::window_views::walk_views_once(window); } else { crate::features::look::first_look(window, crate::features::look::FirstLook::AttachFailed); } @@ -2865,6 +2867,12 @@ fn attach_steps() -> Vec> { false, Some("rivet: not attaching to this browser"), ), + // The class tables of the frame view, its caption buttons and corners and the browser view + // are read from the window's live views before the features that replace their slots. + Step::Setup(|attach| unsafe { + crate::chromium::window_views::read_tables(attach.window); + true + }), feature( Feature::FrameColour, frame_layout::needs_colour, diff --git a/rivet/runtime/windows/src/hooks/page_area.rs b/rivet/runtime/windows/src/hooks/page_area.rs index aac6672..d34479c 100644 --- a/rivet/runtime/windows/src/hooks/page_area.rs +++ b/rivet/runtime/windows/src/hooks/page_area.rs @@ -117,6 +117,16 @@ impl PageArea<'_> { self.bounds.of(view) } + /// The bounds the layout gave the view of the pages, found by its class name or the row. + pub(crate) unsafe fn contents(&mut self) -> Option<*mut [i32; 4]> { + self.bounds(crate::chromium::window_views::contents_field()) + } + + /// The bounds the layout gave the area below the top row, found by its class name or the row. + pub(crate) unsafe fn page(&mut self) -> Option<*mut [i32; 4]> { + self.bounds(crate::chromium::window_views::page_area_field()) + } + /// The bounds the layout gave every view it placed, where the source can list them. pub(crate) unsafe fn all_bounds(&mut self) -> Vec<(usize, *mut [i32; 4])> { let views = self.bounds.views(); @@ -166,9 +176,10 @@ pub(crate) unsafe fn run( HOST_VIEWS.with(|views| views.borrow_mut().insert(window as isize, host)); // A layout without the recorded page container is traced once, which shows where it keeps it. static DUMPED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); - let unknown = row().contents_container != crate::builds::ABSENT + let contents = crate::chromium::window_views::contents_field(); + let unknown = contents != crate::builds::ABSENT && bounds - .of(*(this.cast::().add(row().contents_container) as *const usize)) + .of(*(this.cast::().add(contents) as *const usize)) .is_none() && crate::trace::tracing() && !DUMPED.swap(true, std::sync::atomic::Ordering::Relaxed); @@ -182,7 +193,7 @@ pub(crate) unsafe fn run( window, rail: 0, }; - if let Some(page) = area.bounds(row().page_area) { + if let Some(page) = area.page() { let page = *page; AREAS.with(|areas| { areas @@ -191,8 +202,12 @@ pub(crate) unsafe fn run( }); } let traced = |area: &mut PageArea| { - [row().page_area, row().contents_container, row().toolbar] - .map(|offset| area.bounds(offset).map(|bounds| *bounds)) + [ + crate::chromium::window_views::page_area_field(), + crate::chromium::window_views::contents_field(), + row().toolbar, + ] + .map(|offset| area.bounds(offset).map(|bounds| *bounds)) }; let before = traced(&mut area); for step in STEPS { @@ -252,10 +267,7 @@ pub(crate) unsafe fn install() -> Result<(), String> { /// the browser's layout works on. unsafe fn host_of(window: HWND) -> Option { let frame = frame_layout::frame_view(window)?; - let at = frame + crate::builds::get().layout.frame_browser_window_offset; - readable(at, 8) - .then(|| *(at as *const usize)) - .filter(|view| *view != 0) + crate::chromium::window_views::frame_browser_view(frame, None) } /// Lays the browser view out again, which runs the patched layout. A window the layout has not run diff --git a/rivet/runtime/windows/src/row_data.rs b/rivet/runtime/windows/src/row_data.rs index b1381d1..c71f0b3 100644 --- a/rivet/runtime/windows/src/row_data.rs +++ b/rivet/runtime/windows/src/row_data.rs @@ -399,15 +399,34 @@ mod tests { #[test] fn nfr6_a_row_without_a_field_is_refused_and_named() { let mut row = builds::CHROME.to_row(); - row["toolbar"] - .as_object_mut() - .unwrap() - .remove("get_widget_slot"); + row["toolbar"].as_object_mut().unwrap().remove("get_color"); let error = match builds::Build::from_row(&row) { Err(error) => error, - Ok(_) => panic!("a row with no widget slot was accepted"), + Ok(_) => panic!("a row with no colour getter was accepted"), }; - assert_eq!(error, "toolbar: get_widget_slot is missing"); + assert_eq!(error, "toolbar: get_color is missing"); + } + + #[test] + fn nfr6_a_row_without_window_offsets_is_not_blocked() { + // The window's views and widget are found by class name and the view tree, so a row made + // without these values still reads, and has none of them. + let mut row = builds::CHROME.to_row(); + for (part, key) in [ + ("layout", "frame_browser_window_offset"), + ("side_panel", "contents_container"), + ("side_panel", "host_view"), + ("side_panel", "page_area"), + ("toolbar", "get_widget_slot"), + ] { + row[part].as_object_mut().unwrap().remove(key); + } + let build = builds::Build::from_row(&row).expect("a row without window offsets reads"); + assert_eq!(build.layout.frame_browser_window_offset, None); + assert_eq!(build.side_panel.contents_container, None); + assert_eq!(build.side_panel.host_view, None); + assert_eq!(build.side_panel.page_area, None); + assert_eq!(build.toolbar.get_widget_slot, None); } #[test] diff --git a/scripts/check.sh b/scripts/check.sh index 34c08c3..0f97327 100755 --- a/scripts/check.sh +++ b/scripts/check.sh @@ -15,6 +15,7 @@ tests=( scripts/tests/installer.test.sh scripts/tests/windows-features.test.sh scripts/tests/paths.test.sh + scripts/tests/no-pdb.test.sh scripts/tests/layers.test.sh scripts/tests/test-desktop.test.sh scripts/tests/test-window-placement.test.sh diff --git a/scripts/tests/browser-rows.test.sh b/scripts/tests/browser-rows.test.sh index 6834e3d..111771d 100755 --- a/scripts/tests/browser-rows.test.sh +++ b/scripts/tests/browser-rows.test.sh @@ -25,12 +25,18 @@ expect "proves a row on the real browser" "windows-row-proof.ps1" expect "does not publish a row that needs review" "steps.made.outputs.for_review == 'false'" expect "publishes a signed table" "table-sign" expect "signs with a secret" "secrets.RIVET_TABLE_SIGNING_KEY" +expect "makes each row from the prior row and the prior build's module" 'chrome-builds.exe find "$temp/prior.json" $env:PRIOR_MODULE' +expect "fingerprints a new row from its own module" "chrome-builds.exe fingerprint" +expect "scores the generator against verified rows" "scripts/tests/row-score.ps1" +expect "a score run makes no row" "github.event.inputs.score == ''" +expect "a trial run publishes nothing" "needs.row.result != 'skipped' && !inputs.trial" +expect "a trial run proposes nothing" "steps.made.outputs.for_review == 'true' && !inputs.trial" if [[ -e "$repo_root/.github/workflows/chrome-support.yml" ]]; then echo "chrome-support.yml covers Chrome only and is replaced by browser-rows.yml" >&2 failed=1 fi -for script in fetch-browser fetch-symbols; do +for script in fetch-browser; do if [[ ! -f "$repo_root/.github/scripts/rows/$script.ps1" ]]; then echo "the job needs .github/scripts/rows/$script.ps1" >&2 failed=1 diff --git a/scripts/tests/no-pdb.test.sh b/scripts/tests/no-pdb.test.sh new file mode 100755 index 0000000..090b618 --- /dev/null +++ b/scripts/tests/no-pdb.test.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# Fails if code, scripts or workflows download or read a browser's debug symbols. Rivet and its row +# generator make every row from the verified prior row, its fingerprints and the two builds' +# modules: a maker can ship a build long before its symbols appear, or never publish them, so +# nothing may wait on them. The rows already verified with symbols stay as the starting point. +set -uo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +cd "$repo_root" + +# Searches the tracked code, scripts and workflows. A search that cannot run fails, so a broken +# search never passes as a clean one. +search() { + local found status + found="$(git grep -inIE -e "$1" -- rivet scripts .github ':!scripts/tests/no-pdb.test.sh')" + status=$? + if (( status > 1 )); then + echo "error: git grep could not search for $1" >&2 + return 2 + fi + # The script that runs this test names it, and the name is not a reader. + printf '%s' "$found" | grep -v 'no-pdb\.test\.sh' + return 0 +} + +# A symbol server, the variable that names one, or the path a PDB is asked for by. +servers='symsrv|msdl\.microsoft\.com|brave-symbols|symbols\.mozilla|/download/symbols|_NT_SYMBOL_PATH|srv\*|\.pdb/|\.dll\.pdb' +found="$(search "$servers")" || exit 2 +if [[ -n "$found" ]]; then + echo "error: no code, script or workflow may name a symbol server:" >&2 + echo "$found" >&2 + exit 1 +fi +echo "ok no code, script or workflow names a symbol server" + +# DbgHelp and DIA read PDBs, a CodeView record names the PDB of a module, and the `pdb` crate parses +# one. Any of them, or a file named after a PDB, is code that reads symbols. +readers='dbghelp|SymInitialize|SymLoadModule|SymEnumSymbols|SymFromName|SymFromAddr|SymSetOptions|msdia|IDiaDataSource|RSDS|CodeView|pdb_key|pdb-key|(^|[^a-z0-9_])pdb([^a-z0-9_]|$)|fetch-symbols' +found="$(search "$readers")" || exit 2 +if [[ -n "$found" ]]; then + echo "error: no code, script or workflow may read a PDB:" >&2 + echo "$found" >&2 + exit 1 +fi +echo "ok no code, script or workflow reads a PDB" diff --git a/scripts/tests/row-port.ps1 b/scripts/tests/row-port.ps1 deleted file mode 100644 index d074f1c..0000000 --- a/scripts/tests/row-port.ps1 +++ /dev/null @@ -1,62 +0,0 @@ -param( - [Parameter(Mandatory = $true)][string]$Rows, - [Parameter(Mandatory = $true)][string]$Generator, - [Parameter(Mandatory = $true)][string]$Symbols -) - -$ErrorActionPreference = 'Stop' -# Proves the row generator on builds people already verified: a row made from the prior build must -# hold the same addresses as the row that was checked by hand. Fields the prior row did not have -# yet are named, since a generator cannot add a feature. Needs the PDBs and an installed browser, so -# a machine without them skips the pair it lacks. -$work = Join-Path ([IO.Path]::GetTempPath()) "rivet-row-port-$PID" -New-Item -ItemType Directory -Force $work | Out-Null - -function Flatten($value, $path, $out) { - if ($value -is [System.Management.Automation.PSCustomObject]) { - foreach ($item in $value.PSObject.Properties) { Flatten $item.Value "$path.$($item.Name)" $out } - } elseif ($value -is [System.Collections.IEnumerable] -and $value -isnot [string]) { - $index = 0 - foreach ($item in $value) { Flatten $item "$path[$index]" $out; $index++ } - } else { - $out[$path] = $value - } -} - -$failed = $false -foreach ($pair in @( - @{ Browser = 'Google Chrome'; Prior = '156.0.8078.48'; New = '156.0.8078.74'; Pdbs = 'chrome-symbols'; Module = 'C:\Program Files\Google\Chrome\Application\156.0.8078.74\chrome.dll' }, - @{ Browser = 'Brave'; Prior = '156.1.98.62'; New = '156.1.98.63'; Pdbs = 'brave-symbols'; Module = 'C:\Program Files\BraveSoftware\Brave-Browser\Application\156.1.98.63\chrome.dll' } -)) { - $old = Join-Path $Symbols "$($pair.Pdbs)\$($pair.Prior)\chrome.dll.pdb" - $new = Join-Path $Symbols "$($pair.Pdbs)\$($pair.New)\chrome.dll.pdb" - if (-not ((Test-Path $old) -and (Test-Path $new) -and (Test-Path $pair.Module))) { - "skip $($pair.Browser) $($pair.New): no symbols or module here" - continue - } - $priorRow = Join-Path $work "prior.json" - $made = Join-Path $work "made.json" - $report = Join-Path $work "report.json" - & $Rows row $pair.Browser $pair.Prior | Set-Content -Encoding utf8 $priorRow - $truth = & $Rows row $pair.Browser $pair.New | ConvertFrom-Json - & $Generator port $priorRow $old $new $pair.Module $pair.New $made $report - $review = (Get-Content -Raw $report | ConvertFrom-Json).review - if ($review.Count -gt 0) { "$($pair.Browser): for review: $review"; $failed = $true } - $a = @{}; $b = @{} - Flatten (Get-Content -Raw $made | ConvertFrom-Json) '' $a - Flatten $truth '' $b - $count = 0 - foreach ($path in $b.Keys) { - if ("$($b[$path])" -notmatch '^0x') { continue } - $count++ - if ($a[$path] -ne $b[$path]) { - if ($a[$path] -eq '0x7fff0000' -or $null -eq $a[$path]) { "$($pair.Browser): $path was added by hand after the prior row"; continue } - "$($pair.Browser): $path made $($a[$path]), verified $($b[$path])" - $failed = $true - } - } - "$($pair.Browser) $($pair.Prior) -> $($pair.New): compared $count address values" -} -Remove-Item -Recurse -Force $work -if ($failed) { throw 'NFR-3: A generated row differs from the row people verified.' } -'NFR-3: Generated rows match the verified rows.' diff --git a/scripts/tests/row-score.ps1 b/scripts/tests/row-score.ps1 new file mode 100644 index 0000000..1e1cd81 --- /dev/null +++ b/scripts/tests/row-score.ps1 @@ -0,0 +1,98 @@ +param( + # rivet-rows.exe, which prints the rows Rivet knows. + [Parameter(Mandatory = $true)][string]$Rows, + # chrome-builds.exe, the row generator. + [Parameter(Mandatory = $true)][string]$Generator, + # The checked-in table, whose rows count as verified like those built into rivet-rows. + [Parameter(Mandatory = $true)][string]$Table, + # Where the modules, rows and scores go. Removed and made again. + [Parameter(Mandatory = $true)][string]$Work, + # `browser@prior>new` pairs. Empty means the list below. + [string[]]$Pairs = @(), + # Fetches each build with .github/scripts/rows/fetch-browser.ps1. Without it, each module is + # taken from `$Modules\\\`. + [switch]$Fetch, + [string]$Modules = '' +) + +$ErrorActionPreference = 'Stop' +# Scores the row generator against answer keys: builds whose rows people verified, made from +# public symbols before Rivet stopped reading them. Each new row is made without symbols from the +# verified row of an older build, the way the row job makes rows, and compared value by value with +# the verified row of the new build. Per feature it reports values found and equal, values found +# but different, and features the made row turns off because their values were not found. +# +# A value found but different is a wrong value, which would reach people's browsers, so any fails +# this script. A feature turned off is not a failure: the runtime keeps it off. A pair whose builds +# cannot be fetched is reported and skipped, and a run that scores no pair fails, since it measured +# nothing. Nothing is published or proposed. +# +# A build newer than every verified row, such as Chrome 158 while the newest verified row is 157, +# has no answer key and cannot be scored here. +if (-not $Pairs -or $Pairs.Count -eq 0) { + $Pairs = @( + 'Google Chrome@156.0.8078.82>157.0.8103.38', + 'Google Chrome@156.0.8078.82>157.0.8103.39', + 'Google Chrome@156.0.8078.74>156.0.8078.80', + 'Microsoft Edge@156.0.4314.50>156.0.4314.58', + 'Brave@156.1.98.63>157.1.99.55' + ) +} + +if (Test-Path $Work) { Remove-Item -Recurse -Force $Work } +New-Item -ItemType Directory -Force $Work | Out-Null + +function Find-BuildModule($browser, $version) { + $dll = if ($browser -eq 'Microsoft Edge') { 'msedge.dll' } else { 'chrome.dll' } + if (-not $Fetch) { + $path = Join-Path $Modules "$browser\$version\$dll" + if (-not (Test-Path $path)) { throw "no module at $path" } + return $path + } + $out = Join-Path $Work ("browser-" + ($browser -replace ' ', '-') + "-$version") + $found = Join-Path $Work ("browser-" + ($browser -replace ' ', '-') + "-$version.txt") + if (-not (Test-Path $found)) { + & "$PSScriptRoot/../../.github/scripts/rows/fetch-browser.ps1" -Browser $browser -Version $version -Out $out -Output $found | Out-Host + } + (Get-Content $found | Where-Object { $_ -like 'module=*' } | Select-Object -First 1) -replace '^module=', '' +} + +function Get-VerifiedRow($browser, $version, $file) { + & $Rows prior $browser $version --table $Table | Set-Content -Encoding utf8 $file + if ($LASTEXITCODE -ne 0) { throw "rivet-rows has no row for $browser at or below $version" } + $row = Get-Content -Raw $file | ConvertFrom-Json + if ($row.frame.version -ne $version) { throw "$browser $version has no verified row; the nearest is $($row.frame.version)" } +} + +$scored = 0 +$wrong = 0 +$failed = @() +foreach ($pair in $Pairs) { + $browser, $versions = $pair -split '@', 2 + $prior, $new = $versions -split '>', 2 + $slug = ($browser -replace ' ', '-') + "-$prior-to-$new" + $priorRow = Join-Path $Work "$slug-prior.json" + $verified = Join-Path $Work "$slug-verified.json" + $score = Join-Path $Work "$slug-score.json" + try { + Get-VerifiedRow $browser $prior $priorRow + Get-VerifiedRow $browser $new $verified + $priorModule = Find-BuildModule $browser $prior + $module = Find-BuildModule $browser $new + } catch { + "skip ${browser} $prior -> ${new}: $_" + continue + } + & $Generator score $priorRow $priorModule $module $verified $score + switch ($LASTEXITCODE) { + 0 { $scored++ } + 5 { $scored++; $wrong++ } + default { $failed += "$browser $prior -> $new (chrome-builds exited with $LASTEXITCODE)" } + } +} + +"scored $scored of $($Pairs.Count) pairs; $wrong with a wrong value" +if ($failed.Count -gt 0) { throw "NFR-6: the generator failed on: $($failed -join '; ')" } +if ($wrong -gt 0) { throw 'NFR-6: a row made without symbols holds a value that differs from its verified row.' } +if ($scored -eq 0) { throw 'NFR-6: no pair could be scored, so nothing was measured.' } +'NFR-6: every value the generator found equals the verified row.'