Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(desktop): check the update's architecture without developer tools (#2256)

Call White7ac579f584a4
diff --git a/desktop/src-tauri/src/update_install.rs b/desktop/src-tauri/src/update_install.rs
index 379a7d990..98d82e484 100644
--- a/desktop/src-tauri/src/update_install.rs
+++ b/desktop/src-tauri/src/update_install.rs
@@ -35,18 +35,7 @@ pub fn install(_app: &AppHandle, bytes: &[u8], version: &str) -> Result<(), Stri
return Err("replacement application identity mismatch".into());
}
}
- let binary = replacement.join("Contents/MacOS/avibe-desktop");
- let arch = if cfg!(target_arch = "aarch64") {
- "arm64"
- } else {
- "x86_64"
- };
- let output = Command::new("/usr/bin/lipo")
- .arg("-archs")
- .arg(&binary)
- .output()
- .map_err(|e| e.to_string())?;
- if !output.status.success() || String::from_utf8_lossy(&output.stdout).trim() != arch {
+ if !runs_on_this_mac(&replacement.join("Contents/MacOS/avibe-desktop")).map_err(|e| e.to_string())? {
return Err("replacement architecture mismatch".into());
}
if !Command::new("/usr/bin/codesign")
@@ -72,6 +61,53 @@ pub fn install(_app: &AppHandle, bytes: &[u8], version: &str) -> Result<(), Stri
}
}
+/// `<mach/machine.h>` CPU types: the base type with the 64-bit ABI flag.
+#[cfg(any(target_os = "macos", test))]
+const CPU_TYPE_ARM64: u32 = 0x0100_000c;
+#[cfg(any(target_os = "macos", test))]
+const CPU_TYPE_X86_64: u32 = 0x0100_0007;
+
+/// Read in-process, not with `/usr/bin/lipo`: without the Command Line Tools
+/// that is only the xcrun shim, which fails and prompts to install them (#2254).
+#[cfg(target_os = "macos")]
+fn runs_on_this_mac(binary: &std::path::Path) -> std::io::Result<bool> {
+ use std::io::Read;
+ let cputype = if cfg!(target_arch = "aarch64") {
+ CPU_TYPE_ARM64
+ } else {
+ CPU_TYPE_X86_64
+ };
+ // The Mach-O header and any fat arch table fit in the first page.
+ let mut header = Vec::new();
+ std::fs::File::open(binary)?.take(4096).read_to_end(&mut header)?;
+ Ok(macho_runs_as(&header, cputype))
+}
+
+/// Whether a Mach-O image, given by its leading bytes, runs as `cputype`: a
+/// thin 64-bit image of that type, or a fat image whose complete arch table
+/// lists it. Anything else, including a truncated table, is rejected.
+#[cfg(any(target_os = "macos", test))]
+fn macho_runs_as(header: &[u8], cputype: u32) -> bool {
+ let word = |at: usize| header.get(at..at + 4).and_then(|b| <[u8; 4]>::try_from(b).ok());
+ // Fat headers are big-endian on disk.
+ let be = |at: usize| word(at).map(u32::from_be_bytes);
+ let fat = |entry: usize| {
+ let count = be(4).unwrap_or(0) as usize;
+ count
+ .checked_mul(entry)
+ .and_then(|table| table.checked_add(8))
+ .is_some_and(|end| end <= header.len())
+ && (0..count).any(|i| be(8 + i * entry) == Some(cputype))
+ };
+ match be(0) {
+ // MH_MAGIC_64 stored little-endian, the byte order of both shipped arches.
+ Some(0xcffa_edfe) => word(4).map(u32::from_le_bytes) == Some(cputype),
+ Some(0xcafe_babe) => fat(20), // FAT_MAGIC, 20-byte `fat_arch`
+ Some(0xcafe_babf) => fat(32), // FAT_MAGIC_64, 32-byte `fat_arch_64`
+ _ => false,
+ }
+}
+
#[cfg(any(target_os = "macos", test))]
fn replace_with_rollback(
current: &std::path::Path,
@@ -196,4 +232,78 @@ mod tests {
assert!(result.is_err());
assert_eq!(std::fs::read(&backup).unwrap(), b"old");
}
+
+ const CPU_TYPE_I386: u32 = 7;
+ const CPU_TYPE_ARM: u32 = 12;
+
+ /// A 32-byte `mach_header_64` with `magic` written little-endian.
+ fn thin(magic: u32, cputype: u32) -> Vec<u8> {
+ [magic, cputype, 0, 2, 0, 0, 0, 0]
+ .iter()
+ .flat_map(|w| w.to_le_bytes())
+ .collect()
+ }
+
+ /// A big-endian fat header and arch table; slice offsets are not read.
+ fn fat(magic: u32, cputypes: &[u32]) -> Vec<u8> {
+ let words = if magic == 0xcafe_babf { 8 } else { 5 };
+ let mut table = vec![magic, cputypes.len() as u32];
+ for &cputype in cputypes {
+ table.extend([cputype, 0]);
+ table.extend(std::iter::repeat_n(0, words - 2));
+ }
+ table.iter().flat_map(|w| w.to_be_bytes()).collect()
+ }
+
+ #[test]
+ fn only_an_image_that_runs_as_the_arch_passes_the_architecture_check() {
+ let (arm64, x86_64) = (CPU_TYPE_ARM64, CPU_TYPE_X86_64);
+ let both = fat(0xcafe_babe, &[x86_64, arm64]);
+ let mut truncated_table = both.clone();
+ truncated_table.pop();
+ let mut overlong_count = fat(0xcafe_babe, &[arm64]);
+ overlong_count[4..8].copy_from_slice(&u32::MAX.to_be_bytes());
+ let cases: [(&str, Vec<u8>, &[u32]); 13] = [
+ ("thin arm64", thin(0xfeed_facf, arm64), &[arm64]),
+ ("thin x86_64", thin(0xfeed_facf, x86_64), &[x86_64]),
+ ("fat with both", both.clone(), &[arm64, x86_64]),
+ ("fat with x86_64 only", fat(0xcafe_babe, &[x86_64]), &[x86_64]),
+ ("fat_64 with both", fat(0xcafe_babf, &[arm64, x86_64]), &[arm64, x86_64]),
+ ("fat_64 with arm64 only", fat(0xcafe_babf, &[arm64]), &[arm64]),
+ ("fat with no arches", fat(0xcafe_babe, &[]), &[]),
+ (
+ "fat with 32-bit arches only",
+ fat(0xcafe_babe, &[CPU_TYPE_I386, CPU_TYPE_ARM]),
+ &[],
+ ),
+ ("thin 32-bit", thin(0xfeed_face, arm64), &[]),
+ (
+ "thin 64-bit in big-endian order",
+ thin(0xcffa_edfe, arm64.swap_bytes()),
+ &[],
+ ),
+ ("truncated thin header", thin(0xfeed_facf, arm64)[..6].to_vec(), &[]),
+ ("truncated arch table", truncated_table, &[]),
+ ("arch count beyond the table", overlong_count, &[]),
+ ];
+ for (name, bytes, runs_as) in cases {
+ for cputype in [arm64, x86_64] {
+ assert_eq!(
+ macho_runs_as(&bytes, cputype),
+ runs_as.contains(&cputype),
+ "{name} as {cputype:#x}"
+ );
+ }
+ }
+ for garbage in [&b""[..], b"\xca\xfe", b"#!/bin/sh\nexit 0\n", &[0xff; 64]] {
+ assert!(!macho_runs_as(garbage, arm64) && !macho_runs_as(garbage, x86_64));
+ }
+ }
+
+ /// The test binary is real toolchain output for the running arch.
+ #[cfg(target_os = "macos")]
+ #[test]
+ fn this_toolchains_own_binary_passes_the_architecture_check() {
+ assert!(runs_on_this_mac(&std::env::current_exe().unwrap()).unwrap());
+ }
}