Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions .github/scripts/rows/fetch-symbols.ps1

This file was deleted.

281 changes: 86 additions & 195 deletions .github/workflows/browser-rows.yml

Large diffs are not rendered by default.

761 changes: 119 additions & 642 deletions rivet/platform/windows/src/bin/chrome-builds.rs

Large diffs are not rendered by default.

57 changes: 3 additions & 54 deletions rivet/platform/windows/src/chrome_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
//! new ones.
//!
//! Each entry records where one build keeps the frame view's client-view bounds function. The
//! runtime only patches a loaded module that matches an entry exactly. The `chrome-builds` tool
//! fills entries from Google's public symbols, so a new browser release needs a data change, not
//! a code change.
//! runtime only patches a loaded module that matches an entry exactly, so a new browser release
//! needs a data change, not a code change.

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -61,13 +60,12 @@ pub fn hex(value: &str) -> Option<Vec<u8>> {
.collect()
}

/// A PE image read from a file, with enough structure to map RVAs and find its debug record.
/// A PE image read from a file, with enough structure to map RVAs and find its functions.
pub struct PeFile<'a> {
bytes: &'a [u8],
image_base: u64,
sections: Vec<(u32, u32, u32)>,
code_sections: Vec<(u32, u32)>,
debug_directory: (u32, u32),
exception_directory: (u32, u32),
}

Expand Down Expand Up @@ -103,11 +101,6 @@ impl<'a> PeFile<'a> {
return None;
}
let image_base = u64_at(bytes, optional + 24)?;
// Data directory 6 is the debug directory.
let debug_directory = (
u32_at(bytes, optional + 112 + 48)?,
u32_at(bytes, optional + 116 + 48)?,
);
// Data directory 3 is the exception table, which lists where functions start.
let exception_directory = (
u32_at(bytes, optional + 112 + 24)?,
Expand Down Expand Up @@ -142,7 +135,6 @@ impl<'a> PeFile<'a> {
image_base,
sections,
code_sections,
debug_directory,
exception_directory,
})
}
Expand Down Expand Up @@ -191,32 +183,6 @@ impl<'a> PeFile<'a> {
u32::try_from(value.checked_sub(self.image_base)?).ok()
}

/// The symbol-server key of the image's PDB: GUID and age from its CodeView record.
pub fn pdb_key(&self) -> Option<String> {
let (directory, size) = self.debug_directory;
for index in 0..size / 28 {
let entry = self.offset(directory + index * 28)?;
// Type 2 is CodeView.
if u32_at(self.bytes, entry + 12)? != 2 {
continue;
}
let record = u32_at(self.bytes, entry + 24)? as usize;
if self.bytes.get(record..record + 4)? != b"RSDS" {
return None;
}
let guid = self.bytes.get(record + 4..record + 20)?;
let age = u32_at(self.bytes, record + 20)?;
let tail: String = guid[8..].iter().map(|byte| format!("{byte:02X}")).collect();
return Some(format!(
"{:08X}{:04X}{:04X}{tail}{age:X}",
u32_at(guid, 0)?,
u16_at(guid, 4)?,
u16_at(guid, 6)?,
));
}
None
}

/// The start of every function the exception table lists. A function that needs no unwind
/// data, such as a leaf that touches no stack, is not listed.
pub fn function_starts(&self) -> std::collections::BTreeSet<u32> {
Expand Down Expand Up @@ -288,23 +254,6 @@ mod tests {
bytes
}

#[test]
fn nfr3_reads_the_symbol_server_key_of_an_image() {
let mut section = vec![0u8; 0x100];
section[12..16].copy_from_slice(&2u32.to_le_bytes());
section[24..28].copy_from_slice(&0x240u32.to_le_bytes());
section[0x40..0x44].copy_from_slice(b"RSDS");
let guid = [
0x51, 0x19, 0x13, 0x7b, 0xa2, 0x33, 0x0f, 0x9e, 0x4c, 0x4c, 0x44, 0x20, 0x50, 0x44,
0x42, 0x2e,
];
section[0x44..0x54].copy_from_slice(&guid);
section[0x54..0x58].copy_from_slice(&1u32.to_le_bytes());
let bytes = image(&section);
let pe = PeFile::parse(&bytes).unwrap();
assert_eq!(pe.pdb_key().unwrap(), "7B13195133A29E0F4C4C44205044422E1");
}

#[test]
fn nfr3_finds_a_function_in_a_vtable() {
let mut section = vec![0u8; 0x100];
Expand Down
203 changes: 189 additions & 14 deletions rivet/platform/windows/src/row_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,170 @@ pub fn class_tables(pe: &PeFile, name: &str) -> BTreeSet<u32> {
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)
/// How many levels of callees below the code that fills the metadata are read for its name.
const CALLEE_DEPTH: usize = 2;
/// How many names may be tried for one table before it counts as too unclear to name.
const NAMES_MOST: usize = 16;
/// How long a class name may be.
const NAME_MOST: usize = 120;

/// The zero-terminated text at `rva` when it reads as a C++ class name without its namespace.
fn identifier_at(pe: &PeFile, rva: u32) -> Option<String> {
let bytes = [NAME_MOST + 1, 64, 32, 16, 8]
.into_iter()
.find_map(|length| pe.read(rva, length))?;
let end = bytes.iter().position(|byte| *byte == 0)?;
let name = &bytes[..end];
let first = *name.first()?;
(name.len() >= 2
&& (first.is_ascii_alphabetic() || first == b'_')
&& name
.iter()
.all(|byte| byte.is_ascii_alphanumeric() || *byte == b'_'))
.then(|| String::from_utf8_lossy(name).into_owned())
}

/// What a function's body loads with a RIP-relative `lea` outside the code, and the functions it
/// calls or jumps to directly.
fn body_of(pe: &PeFile, ranges: &BTreeMap<u32, u32>, start: u32) -> (Vec<u32>, Vec<u32>) {
let end = ranges.get(&start).copied().unwrap_or(start);
let length = end.saturating_sub(start).min(BODY_MOST) as usize;
let Some(bytes) = pe.read(start, length) else {
return (vec![], vec![]);
};
let (mut loads, mut callees) = (Vec::new(), Vec::new());
let mut at = 0;
while at < bytes.len() {
let Some(instruction) = x64::decode_body(&bytes[at..]) else {
break;
};
if instruction.length == 0 || at + instruction.length > bytes.len() {
break;
}
let raw = &bytes[at..at + instruction.length];
let rva = start + at as u32;
if let Some(found) = (raw.len() == 7).then(|| rip_use(raw, rva)).flatten() {
if !found.word && !pe.is_code(found.target) {
loads.push(found.target);
}
}
if raw.len() == 5 && matches!(raw[0], 0xE8 | 0xE9) {
let rel = i32::from_le_bytes(raw[1..5].try_into().expect("five bytes"));
if let Ok(target) = u32::try_from(i64::from(rva) + 5 + i64::from(rel)) {
if ranges.contains_key(&target) {
callees.push(target);
}
}
}
at += instruction.length;
}
(loads, callees)
}

/// The metadata statics a class table's objects reach: each constructor that writes the table at
/// the start of an object also writes other tables into it, and the second slot of the
/// `ui::metadata::MetaDataProvider` table among them returns the class's static, as `getters` reads.
fn statics_of_table(pe: &PeFile, ranges: &BTreeMap<u32, u32>, table: u32) -> BTreeSet<(u32, bool)> {
let writers: BTreeSet<u32> = rip_uses(pe, &BTreeSet::from([table]))
.iter()
.filter(|each| !each.word)
.filter_map(|each| enclosing(ranges, each.at))
.collect();
let mut found = BTreeSet::new();
for writer in writers {
let stores = stores_in(pe, writer, ranges[&writer]);
for first in stores
.iter()
.filter(|store| store.table == table && store.offset == 0)
{
for other in stores.iter().filter(|store| {
store.base == first.base && store.offset != 0 && store.table != table
}) {
let Some(getter) = pe.pointer_rva(other.table + 8).filter(|at| pe.is_code(*at))
else {
continue;
};
if let Some(each) = load_target(pe, getter).filter(|(at, _)| !pe.is_code(*at)) {
found.insert(each);
}
}
}
}
found
}

/// The names the code that fills a metadata static loads, nearest first: the texts that the
/// functions whose result is the static, and their callees, load. Only those that read as a class
/// name are kept.
fn names_of_static(
pe: &PeFile,
ranges: &BTreeMap<u32, u32>,
found: (u32, bool),
) -> Vec<Vec<String>> {
let mut layer: BTreeSet<u32> = rip_uses(pe, &BTreeSet::from([found.0]))
.iter()
.filter_map(|each| enclosing(ranges, each.at))
.filter(|function| load_target(pe, *function) == Some(found))
.collect();
let mut seen = layer.clone();
let mut by_depth = Vec::new();
for _ in 0..=CALLEE_DEPTH {
if layer.is_empty() || seen.len() > CALLERS_MOST {
break;
}
let mut names = Vec::new();
let mut next = BTreeSet::new();
for function in &layer {
let (loads, callees) = body_of(pe, ranges, *function);
names.extend(loads.into_iter().filter_map(|at| identifier_at(pe, at)));
next.extend(callees.into_iter().filter(|callee| !seen.contains(callee)));
}
by_depth.push(names);
seen.extend(&next);
layer = next;
}
by_depth
}

/// The views class a class table belongs to, by the class name its `ui::metadata` holds, read back
/// from the build: the metadata static the table's objects reach, and the names the code that fills
/// it loads. A name counts only where `class_tables` gives exactly this table for it, so a name
/// that is not the class's own gives none. The nearest code that yields exactly one such name
/// names the table; none, or two, gives no name.
pub fn class_name_of_table(pe: &PeFile, table: u32) -> Option<String> {
let ranges = pe.function_ranges();
let mut by_depth: Vec<Vec<String>> = Vec::new();
for each in statics_of_table(pe, &ranges, table) {
for (depth, names) in names_of_static(pe, &ranges, each).into_iter().enumerate() {
if by_depth.len() <= depth {
by_depth.resize(depth + 1, Vec::new());
}
by_depth[depth].extend(names);
}
}
let mut tried = BTreeSet::new();
for names in by_depth {
let mut confirmed = BTreeSet::new();
for name in names {
if tried.len() >= NAMES_MOST {
return None;
}
if tried.insert(name.clone()) && class_tables(pe, &name) == BTreeSet::from([table]) {
confirmed.insert(name);
}
}
match confirmed.len() {
0 => continue,
1 => return confirmed.into_iter().next(),
_ => return None,
}
}
None
}

/// Whether `name` is the class name of `table` in `pe`: `class_tables` gives this table alone.
pub fn names_table(pe: &PeFile, name: &str, table: u32) -> bool {
class_tables(pe, name) == BTreeSet::from([table])
}

#[cfg(test)]
Expand Down Expand Up @@ -518,15 +675,33 @@ mod tests {
}

#[test]
fn nfr6_a_table_symbol_names_its_class() {
fn nfr6_a_class_name_is_read_from_the_prior_build_without_symbols() {
let bytes = class_module("EdgeBrowserFrameViewWin");
let pe = PeFile::parse(&bytes).unwrap();
// The table the constructor writes at the object's start is named by the text its
// metadata code loads, and the name gives that table back.
assert_eq!(
class_of_table_symbol("??_7EdgeBrowserFrameViewWin@@6BLayerDelegate@ui@@@"),
class_name_of_table(&pe, 0x2400).as_deref(),
Some("EdgeBrowserFrameViewWin")
);
assert_eq!(
class_of_table_symbol("??_7VerticalTabStripStateControllerImpl@tabs@@6B@"),
Some("VerticalTabStripStateControllerImpl")
);
assert_eq!(class_of_table_symbol("?Layout@BrowserView@@UEAAXXZ"), None);
assert!(names_table(&pe, "EdgeBrowserFrameViewWin", 0x2400));
// The provider table is written further into the object, so it names no class.
assert_eq!(class_name_of_table(&pe, 0x2200), None);
assert!(!names_table(&pe, "EdgeBrowserFrameViewWin", 0x2200));
// A name the metadata code does not load does not name the table.
assert!(!names_table(&pe, "BrowserFrameViewWin", 0x2400));
}

#[test]
fn nfr6_a_class_name_is_a_plain_identifier() {
let mut code = class_module("BrowserView");
let at = 0x400 + 0x1000 + 0x100;
assert_eq!(&code[at..at + 12], b"BrowserView\0");
let pe = PeFile::parse(&code).unwrap();
assert_eq!(identifier_at(&pe, 0x2100).as_deref(), Some("BrowserView"));
// A path or a qualified name is not a class name.
code[at + 7] = b':';
let pe = PeFile::parse(&code).unwrap();
assert_eq!(identifier_at(&pe, 0x2100), None);
}
}
Loading
Loading