From aefca5230e86aaf03fc3b1daa1c72f90f32fb693 Mon Sep 17 00:00:00 2001 From: Markus Pettersson Date: Fri, 21 Feb 2025 14:52:47 +0100 Subject: [PATCH] Remove explicit `unsafe` from `windows-installer` --- Cargo.lock | 1 + windows-installer/Cargo.toml | 5 +++-- windows-installer/src/windows.rs | 32 ++++++++++---------------------- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4205eb9adc..645c02fac4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5886,6 +5886,7 @@ version = "0.0.0" dependencies = [ "anyhow", "mullvad-version", + "talpid-platform-metadata", "tempfile", "windows-sys 0.52.0", "winres", diff --git a/windows-installer/Cargo.toml b/windows-installer/Cargo.toml index 518fe2d82d..f50f032626 100644 --- a/windows-installer/Cargo.toml +++ b/windows-installer/Cargo.toml @@ -11,9 +11,10 @@ rust-version.workspace = true workspace = true [target.'cfg(all(target_os = "windows", target_arch = "x86_64"))'.dependencies] -windows-sys = { version = "0.52.0", features = ["Win32_System", "Win32_System_LibraryLoader", "Win32_System_SystemInformation", "Win32_System_Threading"] } -tempfile = "3.10" anyhow.workspace = true +talpid-platform-metadata = { path = "../talpid-platform-metadata" } +tempfile = "3.10" +windows-sys = { version = "0.52.0", features = ["Win32_System", "Win32_System_LibraryLoader", "Win32_System_SystemInformation", "Win32_System_Threading"] } [build-dependencies] winres = "0.1" diff --git a/windows-installer/src/windows.rs b/windows-installer/src/windows.rs index 1b74b074b3..07e4587728 100644 --- a/windows-installer/src/windows.rs +++ b/windows-installer/src/windows.rs @@ -7,7 +7,7 @@ //! * `WIN_ARM64_INSTALLER` - a path to the ARM64 Windows installer use anyhow::{bail, Context}; use std::{ - ffi::{c_ushort, OsStr}, + ffi::OsStr, io::{self, Write}, num::NonZero, process::{Command, ExitStatus}, @@ -16,11 +16,7 @@ use std::{ use tempfile::TempPath; use windows_sys::{ w, - Win32::System::{ - LibraryLoader::{FindResourceW, LoadResource, LockResource, SizeofResource}, - SystemInformation::{IMAGE_FILE_MACHINE_AMD64, IMAGE_FILE_MACHINE_ARM64}, - Threading::IsWow64Process2, - }, + Win32::System::LibraryLoader::{FindResourceW, LoadResource, LockResource, SizeofResource}, }; /// Import resource constants from `resource.rs`. This is automatically generated by the build @@ -124,22 +120,14 @@ enum Architecture { /// Return native architecture (ignoring WOW64) fn get_native_arch() -> anyhow::Result { - let mut running_arch: c_ushort = 0; - let mut native_arch: c_ushort = 0; + let Some(arch) = + talpid_platform_metadata::get_native_arch().context("Failed to get native architecture")? + else { + bail!("Unable to detect native architecture (most likely unsupported)"); + }; - // SAFETY: Trivially safe, since we provide the required arguments. `hprocess == 0` is - // undocumented but refers to the current process. - let result = unsafe { IsWow64Process2(0, &mut running_arch, &mut native_arch) }; - if result == 0 { - bail!( - "Failed to get native architecture: {}", - io::Error::last_os_error() - ); - } - - match native_arch { - IMAGE_FILE_MACHINE_AMD64 => Ok(Architecture::X64), - IMAGE_FILE_MACHINE_ARM64 => Ok(Architecture::Arm64), - other => bail!("unsupported architecture: {other}"), + match arch { + talpid_platform_metadata::Architecture::X86 => Ok(Architecture::X64), + talpid_platform_metadata::Architecture::Arm64 => Ok(Architecture::Arm64), } }