diff --git a/rivet/platform/windows/src/row_features.rs b/rivet/platform/windows/src/row_features.rs index ede4daa..bfdbac9 100644 --- a/rivet/platform/windows/src/row_features.rs +++ b/rivet/platform/windows/src/row_features.rs @@ -135,6 +135,44 @@ pub const ROW_FEATURES: &[RowFeature] = &[ }, ]; +/// 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 { @@ -303,21 +341,33 @@ mod tests { 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", - "layout.frame_browser_window_offset", - "side_panel.page_area", - "side_panel.host_view", - "side_panel.contents_container", - "side_panel.toolbar", - "toolbar.get_widget_slot", - ] { + 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!({ diff --git a/rivet/platform/windows/src/row_port.rs b/rivet/platform/windows/src/row_port.rs index c458378..b619d29 100644 --- a/rivet/platform/windows/src/row_port.rs +++ b/rivet/platform/windows/src/row_port.rs @@ -76,6 +76,9 @@ pub struct PortReport { 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, /// Whether every carried value must be settled: the milestone changed, or values were read. #[serde(skip)] must_check: bool, @@ -1005,8 +1008,9 @@ pub fn port_row( (row, report) } -/// Settles each value no recipe or instruction read from the new build. 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 +/// 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. @@ -1021,7 +1025,13 @@ fn settle_carried(row: &mut Value, report: &mut PortReport) { 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) { - if let Some(why) = row_features::unused(&row["build"], &path) { + 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); @@ -2325,6 +2335,31 @@ mod tests { 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_SYMBOLS, &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(); 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 74fcc3c..b51e221 100644 --- a/rivet/runtime/windows/src/builds/chrome.rs +++ b/rivet/runtime/windows/src/builds/chrome.rs @@ -56,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: &[ @@ -320,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, @@ -377,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], @@ -460,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: &[ @@ -724,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, @@ -781,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], @@ -880,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: &[ @@ -1144,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, @@ -1201,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], @@ -1328,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: &[ @@ -1592,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, @@ -1649,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], @@ -1720,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 a96bcb5..c012b9d 100644 --- a/rivet/runtime/windows/src/builds/mod.rs +++ b/rivet/runtime/windows/src/builds/mod.rs @@ -186,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 @@ -506,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. @@ -602,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 @@ -651,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, @@ -819,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, @@ -874,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 e84ce43..1f70e64 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) ); } @@ -1071,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), @@ -1206,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, 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/mod.rs b/rivet/runtime/windows/src/chromium/mod.rs index 713c6d1..8cef86b 100644 --- a/rivet/runtime/windows/src/chromium/mod.rs +++ b/rivet/runtime/windows/src/chromium/mod.rs @@ -3,3 +3,4 @@ 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/window_views.rs b/rivet/runtime/windows/src/chromium/window_views.rs new file mode 100644 index 0000000..94e412d --- /dev/null +++ b/rivet/runtime/windows/src/chromium/window_views.rs @@ -0,0 +1,674 @@ +//! 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}")), + } + }); +} + +/// 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 da9220d..3682695 100644 --- a/rivet/runtime/windows/src/features/app_window.rs +++ b/rivet/runtime/windows/src/features/app_window.rs @@ -22,12 +22,27 @@ pub(crate) fn recorded(name: &str) -> Option { }) } -/// Where a view of `build` keeps its parent and its children. They follow from where it keeps its -/// bounds by the member order of `views::View`, so they are not values of the build's row. +/// 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 parent = View::parent_offset(build.layout.view_bounds_offset)?; - Some((parent, View::children_offset(parent))) + 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`. @@ -715,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/frame_layout.rs b/rivet/runtime/windows/src/features/frame_layout.rs index 6cd592e..56e4b19 100644 --- a/rivet/runtime/windows/src/features/frame_layout.rs +++ b/rivet/runtime/windows/src/features/frame_layout.rs @@ -888,12 +888,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::(); 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/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..8be16e6 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); } 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]