diff --git a/rivet/platform/windows/src/bin/chrome-builds.rs b/rivet/platform/windows/src/bin/chrome-builds.rs index e37dac5..af0f457 100644 --- a/rivet/platform/windows/src/bin/chrome-builds.rs +++ b/rivet/platform/windows/src/bin/chrome-builds.rs @@ -325,8 +325,10 @@ fn find( drop(old); let bytes = read(module)?; let pe = PeFile::parse(&bytes).ok_or("not a 64-bit PE image")?; + // A class table of a views class is also found again by the class name its symbol gives. + let classes = row_port::class_names(&named); let (mut row, mut report) = - row_port::find_candidate(&prior, &prints, prior_pe.as_ref(), version, &pe); + row_port::find_candidate(&prior, &prints, prior_pe.as_ref(), &classes, version, &pe); // No symbols name `screen_origin` here: a prior row without it gives a row without it. row_port::seed_screen_origin(&mut row, &[], &pe, &mut report); report.review.extend(row_port::hand_only_review(&prior)); diff --git a/rivet/platform/windows/src/command_ids.rs b/rivet/platform/windows/src/command_ids.rs index fe805cb..f6543d5 100644 --- a/rivet/platform/windows/src/command_ids.rs +++ b/rivet/platform/windows/src/command_ids.rs @@ -23,6 +23,31 @@ pub const DEV_TOOLS: &[(u16, u16, u32, u32)] = &[ (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"]; @@ -52,4 +77,24 @@ mod tests { .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 81c550f..f364973 100644 --- a/rivet/platform/windows/src/lib.rs +++ b/rivet/platform/windows/src/lib.rs @@ -10,6 +10,7 @@ pub mod command_ids; pub mod control; pub mod focus; pub mod hold; +pub mod row_class; pub mod row_features; pub mod row_port; pub mod row_recipe; diff --git a/rivet/platform/windows/src/row_class.rs b/rivet/platform/windows/src/row_class.rs new file mode 100644 index 0000000..6a0112a --- /dev/null +++ b/rivet/platform/windows/src/row_class.rs @@ -0,0 +1,532 @@ +//! 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` or `mov` in the code whose target is one of `targets`. +fn rip_uses(pe: &PeFile, targets: &BTreeSet) -> 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(7) { + if let Some(found_use) = rip_use(&bytes[at..at + 7], base + at as u32) { + if targets.contains(&found_use.target) { + found.push(found_use); + } + } + } + } + found +} + +/// Every direct `call` or `jmp` into one of `targets`, as (site, whether it is a jump). +fn branches_into(pe: &PeFile, targets: &BTreeSet) -> Vec<(u32, bool)> { + 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(5) { + if !matches!(bytes[at], 0xE8 | 0xE9) { + continue; + } + let rel = i32::from_le_bytes(bytes[at + 1..at + 5].try_into().expect("in range")); + let site = base + at as u32; + let target = i64::from(site) + 5 + i64::from(rel); + if u32::try_from(target).is_ok_and(|target| targets.contains(&target)) { + found.push((site, bytes[at] == 0xE9)); + } + } + } + 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 +} + +/// The class a public table symbol names, as `??_7EdgeBrowserFrameViewWin@@6BLayerDelegate@ui@@@` +/// names `EdgeBrowserFrameViewWin`. The class name its metadata holds has no namespace. +pub fn class_of_table_symbol(symbol: &str) -> Option<&str> { + let rest = symbol.strip_prefix("??_7")?; + let name = rest.split('@').next()?; + (!name.is_empty() && name.bytes().all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')) + .then_some(name) +} + +#[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_table_symbol_names_its_class() { + assert_eq!( + class_of_table_symbol("??_7EdgeBrowserFrameViewWin@@6BLayerDelegate@ui@@@"), + Some("EdgeBrowserFrameViewWin") + ); + assert_eq!( + class_of_table_symbol("??_7VerticalTabStripStateControllerImpl@tabs@@6B@"), + Some("VerticalTabStripStateControllerImpl") + ); + assert_eq!(class_of_table_symbol("?Layout@BrowserView@@UEAAXXZ"), None); + } +} diff --git a/rivet/platform/windows/src/row_features.rs b/rivet/platform/windows/src/row_features.rs index bfdbac9..266fa47 100644 --- a/rivet/platform/windows/src/row_features.rs +++ b/rivet/platform/windows/src/row_features.rs @@ -78,6 +78,7 @@ pub const ROW_FEATURES: &[RowFeature] = &[ "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, @@ -104,6 +105,48 @@ pub const ROW_FEATURES: &[RowFeature] = &[ 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."], @@ -126,6 +169,14 @@ pub const ROW_FEATURES: &[RowFeature] = &[ 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"], @@ -177,18 +228,26 @@ pub fn leave_out(build: &mut Value, path: &str) { /// values of the build at all, and a row holds zero for them. pub struct UnusedWhen { pub paths: &'static [&'static str], - /// The address that decides it. - pub address: &'static str, - /// Unused while that address is recorded, or while it is absent. - pub while_recorded: bool, + /// 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"], - address: "tabs.tab_at", - while_recorded: true, + when: When::Recorded("tabs.tab_at"), why: "the build has its own GetWebContentsAt", }, UnusedWhen { @@ -198,10 +257,14 @@ pub const UNUSED_WHEN: &[UnusedWhen] = &[ "vertical.toolbar_in_view", "vertical.toolbar_leading", ], - address: "vertical.window_feature", - while_recorded: false, + 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`. @@ -244,8 +307,13 @@ pub fn unused(build: &Value, path: &str) -> Option<&'static str> { .iter() .find(|rule| { rule.paths.iter().any(|each| covers(each, path)) - && at(build, rule.address) - .is_some_and(|value| is_recorded(value) == rule.while_recorded) + && 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) } @@ -289,6 +357,27 @@ pub fn switch_off(build: &mut Value, feature: &RowFeature) { } } +/// 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. diff --git a/rivet/platform/windows/src/row_port.rs b/rivet/platform/windows/src/row_port.rs index b619d29..1f73777 100644 --- a/rivet/platform/windows/src/row_port.rs +++ b/rivet/platform/windows/src/row_port.rs @@ -79,6 +79,9 @@ pub struct PortReport { /// 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, @@ -88,6 +91,10 @@ pub struct PortReport { /// 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 { @@ -1019,12 +1026,25 @@ fn settle_carried(row: &mut Value, report: &mut PortReport) { report .derived .extend(crate::row_recipe::follow(&mut row["build"])); - if !report.must_check { - return; + 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); + } } + 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(); - let mut off: BTreeMap<&'static str, (&'static RowFeature, Vec)> = BTreeMap::new(); - for path in std::mem::take(&mut report.carried) { + for path in carried { if row_features::is_reflected(&path) { row_features::leave_out(&mut row["build"], &path); report.unread.remove(&path); @@ -1046,8 +1066,8 @@ fn settle_carried(row: &mut Value, report: &mut PortReport) { } } let mut entries = Vec::new(); - for (name, (feature, paths)) in off { - row_features::switch_off(&mut row["build"], feature); + 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) { @@ -1063,6 +1083,9 @@ fn settle_carried(row: &mut Value, report: &mut PortReport) { 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) { @@ -1282,12 +1305,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) } @@ -1450,7 +1481,7 @@ pub fn find_row(prior: &Value, version: &str, pe: &PeFile) -> Result Result, prior_pe: Option<&PeFile>, + classes: &BTreeMap, version: &str, pe: &PeFile, ) -> (Value, PortReport) { - let (row, mut report) = begin_row(prior, version, prior_pe.is_none()); + let (mut row, mut report) = begin_row(prior, version, prior_pe.is_none()); report.made_without_pdb = true; let prior_starts = prior_pe.map(PeFile::function_starts).unwrap_or_default(); let targets = fingerprint_targets(prior); @@ -1486,7 +1519,15 @@ pub fn find_candidate( // 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 want = |kind, candidates: Option>| row_resolve::Wanted { kind, candidates }; + 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(), + } + }; for target in &targets { match stored.get(&format!("{target:#x}")) { Some(Value::String(text)) => match hex(text).filter(|shape| shape.len() >= ANCHOR) { @@ -1520,7 +1561,7 @@ pub fn find_candidate( } else { row_resolve::Kind::Table }; - wanted.insert(*target, (line, want(kind, None))); + wanted.insert(*target, (line, want(*target, kind, None))); } None => report.review.push(line), } @@ -1565,15 +1606,15 @@ pub fn find_candidate( "{target:#x} matches no function start, of {} places", all.len() ); - wanted.insert(*target, (line, want(function, None))); + wanted.insert(*target, (line, want(*target, function, None))); } [] => { let line = format!("{target:#x} matches no place"); - wanted.insert(*target, (line, want(function, None))); + wanted.insert(*target, (line, want(*target, function, None))); } many => { let line = format!("{target:#x} matches {} places", many.len()); - wanted.insert(*target, (line, want(function, Some(many.to_vec())))); + wanted.insert(*target, (line, want(*target, function, Some(many.to_vec())))); } } } @@ -1581,7 +1622,7 @@ pub fn find_candidate( let table = row_resolve::Kind::Table; let Some(function) = moved.get(holds).copied() else { let line = format!("{target:#x}: the function in slot {slot} was not found"); - wanted.insert(*target, (line, want(table, None))); + wanted.insert(*target, (line, want(*target, table, None))); continue; }; match find_table(pe, *slot, function).as_slice() { @@ -1597,11 +1638,14 @@ pub fn find_candidate( many.len() ); let candidates = (!many.is_empty()).then(|| many.to_vec()); - wanted.insert(*target, (line, want(table, candidates))); + 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 @@ -1629,17 +1673,16 @@ pub fn find_candidate( .reasons .get(target) .map_or("no method was run", String::as_str); - report.review.push(format!("{line}; not settled: {reason}")); + open.insert(*target, format!("{line}; not settled: {reason}")); } } } report.settled.extend(resolution.settled); followed = resolution.followed; } - _ => report - .review - .extend(wanted.into_values().map(|(line, _)| line)), + _ => 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 @@ -1658,6 +1701,73 @@ pub fn find_candidate( (row, report) } +/// 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; + } + 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()); + } + }; + 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; + } + 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(", "))); + } +} + /// 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; @@ -1854,6 +1964,26 @@ pub fn check_against_pdb( Ok(differences) } +/// The views class each class table of the prior row is named after by the prior build's public +/// symbols, for the tables found again by their class name. A table whose symbols name two classes +/// is left out. +pub fn class_names(named: &BTreeMap)>) -> BTreeMap { + let mut out = BTreeMap::new(); + for (target, (distance, names)) in named { + if *distance != 0 { + continue; + } + let classes: BTreeSet<&str> = names + .iter() + .filter_map(|name| crate::row_class::class_of_table_symbol(stable_name(name))) + .collect(); + if let [only] = classes.into_iter().collect::>()[..] { + out.insert(*target, only.to_owned()); + } + } + out +} + /// Adds to each review line that starts with a prior address the public name that address lies /// in, from the prior build's symbols, so a person sees which function was not found. pub fn name_review_lines(review: &mut [String], named: &BTreeMap)>) { @@ -2645,6 +2775,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(); @@ -2745,7 +2879,7 @@ mod tests { ) -> (Value, PortReport) { let bytes = module(&code_refs(code)); let pe = PeFile::parse(&bytes).unwrap(); - find_candidate(&verified_row(), prints, None, version, &pe) + find_candidate(&verified_row(), prints, None, &BTreeMap::new(), version, &pe) } /// `module` with an exception table at 0x10f000 that lists `starts`. @@ -2802,7 +2936,8 @@ mod tests { new[3].1 = paint_reading(new_loads); let bytes = with_ranges(&new, &ranges(0x2000, new[3].1.len())); let pe = PeFile::parse(&bytes).unwrap(); - find_candidate(&verified_row(), &prints, Some(&prior_pe), "1.0.0.3", &pe) + let classes = BTreeMap::new(); + find_candidate(&verified_row(), &prints, Some(&prior_pe), &classes, "1.0.0.3", &pe) } #[test] @@ -2874,7 +3009,8 @@ mod tests { 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), "1.0.0.3", &pe); + 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. @@ -2885,13 +3021,15 @@ mod tests { )); 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), "1.0.0.3", &pe); + 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), "1.0.0.3", &pe); + let (_, report) = + find_candidate(&verified, &prints, Some(&prior_pe), &no_classes(), "1.0.0.3", &pe); assert!( report .review @@ -2955,6 +3093,62 @@ mod tests { ); } + #[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 nfr6_a_row_made_without_its_pdb_is_checked_against_the_pdb_row() { let (mut row, _) = candidate(&prints_of_prior(), &shifted_code(0x2000, 0x44), "1.0.0.3"); diff --git a/rivet/platform/windows/src/row_resolve.rs b/rivet/platform/windows/src/row_resolve.rs index f6d0bf3..f65f0f9 100644 --- a/rivet/platform/windows/src/row_resolve.rs +++ b/rivet/platform/windows/src/row_resolve.rs @@ -14,7 +14,13 @@ //! - 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). +//! 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; +//! - its fingerprint: a place a class table gives, by its class name or by a slot, 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. @@ -680,6 +686,8 @@ pub enum Method { Text, References, Slots, + ClassName, + Fingerprint, } impl fmt::Display for Method { @@ -691,6 +699,8 @@ impl fmt::Display for Method { Method::Text => "a text it uses", Method::References => "references to it", Method::Slots => "its slots", + Method::ClassName => "its class name", + Method::Fingerprint => "its fingerprint", }) } } @@ -752,6 +762,47 @@ pub enum Kind { pub struct Wanted { pub kind: Kind, pub candidates: Option>, + /// For a class table, the views class the prior build's public symbol names it after. + pub class: Option, +} + +/// 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. @@ -818,6 +869,23 @@ pub fn resolve( } let callers = calls_into(&new, &called); + // 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); + } + } + } + let mut pending: BTreeSet = wanted.keys().copied().collect(); for _ in 0..ROUNDS { let followed = follow(&mut old, &mut new, &known); @@ -849,7 +917,9 @@ pub fn resolve( for place in by_its_calls(&mut new, calls, &candidates) { answers.push((Method::ItsCalls, place)); } - for place in by_table_slot(&old, &new, &known, &table_pairs, target) { + 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)); } if let Some(place) = by_text(&texts_of[&target], &users) { @@ -860,11 +930,29 @@ pub fn resolve( if let Some(place) = by_reference(&followed, target) { answers.push((Method::References, place)); } - let candidates = match &want.candidates { + let mut candidates = match &want.candidates { Some(list) => list.clone(), None => table_candidates(&old, &new, &known, target), }; - for place in by_slots(&old, &new, &known, target, &candidates) { + // 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)); } } @@ -882,6 +970,10 @@ pub fn resolve( found_any = true; } Err(reason) => { + let reason = match class_reasons.get(&target) { + Some(class) => format!("{reason}; {class}"), + None => reason, + }; resolution.reasons.insert(target, reason); } } @@ -1231,6 +1323,7 @@ pub(crate) mod tests { Wanted { kind: Kind::Function, candidates: Some(NEW_TARGETS.to_vec()), + class: None, }, )]); resolve(&old, &new, &known, &wanted, &BTreeSet::from([OLD_TABLE])) @@ -1242,11 +1335,34 @@ pub(crate) mod tests { 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"] + ["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); diff --git a/rivet/runtime/windows/src/browsers/chromium/commands.rs b/rivet/runtime/windows/src/browsers/chromium/commands.rs index 4ada104..99f2283 100644 --- a/rivet/runtime/windows/src/browsers/chromium/commands.rs +++ b/rivet/runtime/windows/src/browsers/chromium/commands.rs @@ -45,6 +45,12 @@ pub(crate) fn dev_tools(browser: &str, version: [u16; 4]) -> (u32, u32) { .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 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)) + ); + } +}