Skip to content

armgit / Handy

Public Imported from GitHub
0

A free, open source, and extensible speech-to-text application that works completely offline.

guard vulkan so only available to true x86 host, not emulated (#1731)

CJ Pais46d6a2aec855
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index d93cfccd..37275dbb 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -803,10 +803,17 @@ jobs:
}
}
- & $handy.FullName --list-devices | Out-Host
+ $deviceOutput = (& $handy.FullName --list-devices | Out-String)
+ $deviceOutput | Write-Host
if ($LASTEXITCODE -ne 0) {
throw "$Label handy.exe --list-devices failed"
}
+ if ("${{ inputs.target }}" -like "aarch64*" -and $deviceOutput -match "kind=(vulkan|cuda|metal|gpu)") {
+ throw "$Label Windows ARM64 package unexpectedly exposed a GPU compute device"
+ }
+ if ("${{ inputs.target }}" -like "aarch64*" -and $deviceOutput -notmatch "kind=cpu") {
+ throw "$Label Windows ARM64 package did not expose its required CPU compute device"
+ }
}
$nsisPackages = Get-ChildItem (Join-Path $bundleBase "nsis") -Filter "*.exe" -ErrorAction SilentlyContinue
diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml
index d63e985c..819d6676 100644
--- a/src-tauri/Cargo.toml
+++ b/src-tauri/Cargo.toml
@@ -106,6 +106,8 @@ tauri-plugin-updater = "2.10.0"
windows = { version = "0.61.3", features = [
"Win32_Media_Audio_Endpoints",
"Win32_System_Com_StructuredStorage",
+ "Win32_System_LibraryLoader",
+ "Win32_System_Threading",
"Win32_System_Variant",
"Win32_Foundation",
"Win32_UI_WindowsAndMessaging",
diff --git a/src-tauri/src/managers/transcription.rs b/src-tauri/src/managers/transcription.rs
index 8bf7bd58..466c950d 100644
--- a/src-tauri/src/managers/transcription.rs
+++ b/src-tauri/src/managers/transcription.rs
@@ -1700,7 +1700,13 @@ pub fn init_transcribe_backend() {
transcribe_cpp::init_logging();
match transcribe_cpp::init_backends_default() {
Ok(()) => {
- let devices = transcribe_cpp::devices();
+ if transcribe_gpu_disabled_for_host() {
+ warn!(
+ "Windows x64 build is running under emulation on an ARM64 host; \
+ disabling transcribe.cpp GPU acceleration and using CPU"
+ );
+ }
+ let devices = transcribe_compute_devices();
info!(
"transcribe-cpp initialized with {} compute device(s): [{}]",
devices.len(),
@@ -1720,7 +1726,7 @@ pub fn init_transcribe_backend() {
/// value to pass to `--device-index`. Backends must be initialized first
/// (see [`init_transcribe_backend`]).
pub fn describe_compute_devices() -> Vec<String> {
- transcribe_cpp::devices()
+ transcribe_compute_devices()
.into_iter()
.map(|d| {
let idx = d
@@ -1746,7 +1752,7 @@ pub fn describe_compute_devices() -> Vec<String> {
/// backend is set explicitly from the device's kind, so there's no "index 0 =
/// auto" ambiguity. Errors if the index isn't a registered, loadable device.
fn resolve_device_index(index: usize) -> Result<(Backend, i32)> {
- let device = transcribe_cpp::devices()
+ let device = transcribe_compute_devices()
.into_iter()
.find(|d| d.index == Some(index))
.ok_or_else(|| {
@@ -1777,9 +1783,10 @@ fn resolve_device_index(index: usize) -> Result<(Backend, i32)> {
/// `Auto` lets the library pick the best device (with CPU fallback). `Cpu` forces
/// strict CPU. `Gpu` requests the platform GPU backend, but only if a device for
/// it is actually registered — otherwise it falls back to `Auto` so the load
-/// never fails outright on a machine without that GPU backend.
+/// never fails outright on a machine without that GPU backend. An emulated x64
+/// process on Windows ARM64 forces strict CPU for every setting.
fn select_transcribe_backend(setting: TranscribeAcceleratorSetting) -> Backend {
- match setting {
+ match effective_transcribe_accelerator(setting, transcribe_gpu_disabled_for_host()) {
TranscribeAcceleratorSetting::Cpu => Backend::Cpu,
TranscribeAcceleratorSetting::Auto => Backend::Auto,
TranscribeAcceleratorSetting::Gpu => {
@@ -1812,12 +1819,15 @@ fn select_transcribe_backend(setting: TranscribeAcceleratorSetting) -> Backend {
/// resolves to a registered GPU device — otherwise fall back to `0` so a stale
/// selection can never fail the load.
fn resolve_gpu_device(setting: TranscribeAcceleratorSetting, gpu_device: i32) -> i32 {
- if setting != TranscribeAcceleratorSetting::Gpu || gpu_device <= 0 {
+ if transcribe_gpu_disabled_for_host()
+ || setting != TranscribeAcceleratorSetting::Gpu
+ || gpu_device <= 0
+ {
return 0;
}
- let still_valid = transcribe_cpp::devices()
+ let still_valid = transcribe_compute_devices()
.iter()
- .any(|d| d.index == Some(gpu_device as usize) && d.kind != "cpu" && d.kind != "accel");
+ .any(|d| d.index == Some(gpu_device as usize) && is_transcribe_gpu_device(d));
if still_valid {
gpu_device
} else {
@@ -1865,6 +1875,50 @@ pub struct GpuDeviceOption {
static GPU_DEVICES: OnceLock<Vec<GpuDeviceOption>> = OnceLock::new();
+fn transcribe_gpu_disabled_for_host() -> bool {
+ crate::utils::is_windows_x64_emulated_on_arm64()
+}
+
+fn effective_transcribe_accelerator(
+ setting: TranscribeAcceleratorSetting,
+ gpu_disabled: bool,
+) -> TranscribeAcceleratorSetting {
+ if gpu_disabled {
+ TranscribeAcceleratorSetting::Cpu
+ } else {
+ setting
+ }
+}
+
+fn is_transcribe_gpu_device(device: &transcribe_cpp::Device) -> bool {
+ device.kind != "cpu" && device.kind != "accel"
+}
+
+fn transcribe_device_allowed(kind: &str, gpu_disabled: bool) -> bool {
+ !gpu_disabled || matches!(kind, "cpu" | "accel")
+}
+
+fn transcribe_compute_devices() -> Vec<transcribe_cpp::Device> {
+ let devices = transcribe_cpp::devices();
+ let gpu_disabled = transcribe_gpu_disabled_for_host();
+ if !gpu_disabled {
+ return devices;
+ }
+
+ devices
+ .into_iter()
+ .filter(|device| transcribe_device_allowed(&device.kind, gpu_disabled))
+ .collect()
+}
+
+fn available_transcribe_accelerators(gpu_disabled: bool) -> Vec<String> {
+ if gpu_disabled {
+ vec!["cpu".to_string()]
+ } else {
+ vec!["auto".to_string(), "cpu".to_string(), "gpu".to_string()]
+ }
+}
+
fn cached_gpu_devices() -> &'static [GpuDeviceOption] {
// GPU compute devices transcribe-cpp registered at startup. `id` is the
// device's registry index (`Device::index`, not a re-counted position) so it
@@ -1872,9 +1926,9 @@ fn cached_gpu_devices() -> &'static [GpuDeviceOption] {
// `total_vram_mb` is the backend-reported capacity, 0 when unreported (some
// Metal/Vulkan drivers).
GPU_DEVICES.get_or_init(|| {
- transcribe_cpp::devices()
+ transcribe_compute_devices()
.into_iter()
- .filter(|d| d.kind != "cpu" && d.kind != "accel")
+ .filter(is_transcribe_gpu_device)
.map(|d| GpuDeviceOption {
id: d.index.unwrap_or(0) as i32,
name: if d.description.is_empty() {
@@ -1895,7 +1949,7 @@ pub struct AvailableAccelerators {
pub gpu_devices: Vec<GpuDeviceOption>,
}
-/// Return which accelerators are compiled into this build.
+/// Return the accelerators available to this process on its current host.
pub fn get_available_accelerators() -> AvailableAccelerators {
use transcribe_rs::accel::OrtAccelerator;
@@ -1904,7 +1958,7 @@ pub fn get_available_accelerators() -> AvailableAccelerators {
.map(|a| a.to_string())
.collect();
- let transcribe_options = vec!["auto".to_string(), "cpu".to_string(), "gpu".to_string()];
+ let transcribe_options = available_transcribe_accelerators(transcribe_gpu_disabled_for_host());
AvailableAccelerators {
transcribe: transcribe_options,
@@ -1921,6 +1975,44 @@ mod tests {
codes.iter().map(|code| (*code).to_string()).collect()
}
+ #[test]
+ fn normal_hosts_preserve_every_transcribe_accelerator_setting() {
+ for setting in [
+ TranscribeAcceleratorSetting::Auto,
+ TranscribeAcceleratorSetting::Cpu,
+ TranscribeAcceleratorSetting::Gpu,
+ ] {
+ assert_eq!(effective_transcribe_accelerator(setting, false), setting);
+ }
+ assert_eq!(
+ available_transcribe_accelerators(false),
+ ["auto", "cpu", "gpu"]
+ );
+ for kind in ["cpu", "accel", "metal", "cuda", "vulkan", "gpu"] {
+ assert!(transcribe_device_allowed(kind, false));
+ }
+ }
+
+ #[test]
+ fn emulated_x64_on_arm64_forces_every_transcribe_setting_to_cpu() {
+ for setting in [
+ TranscribeAcceleratorSetting::Auto,
+ TranscribeAcceleratorSetting::Cpu,
+ TranscribeAcceleratorSetting::Gpu,
+ ] {
+ assert_eq!(
+ effective_transcribe_accelerator(setting, true),
+ TranscribeAcceleratorSetting::Cpu
+ );
+ }
+ assert_eq!(available_transcribe_accelerators(true), ["cpu"]);
+ assert!(transcribe_device_allowed("cpu", true));
+ assert!(transcribe_device_allowed("accel", true));
+ for kind in ["metal", "cuda", "vulkan", "gpu", "unknown"] {
+ assert!(!transcribe_device_allowed(kind, true));
+ }
+ }
+
#[test]
fn optional_text_transform_falls_back_to_raw_text_after_panic() {
let raw = "原始轉錄。".to_string();
diff --git a/src-tauri/src/utils.rs b/src-tauri/src/utils.rs
index 79005ec0..30e8e0fd 100644
--- a/src-tauri/src/utils.rs
+++ b/src-tauri/src/utils.rs
@@ -12,6 +12,65 @@ pub use crate::clipboard::*;
pub use crate::overlay::*;
pub use crate::tray::*;
+#[cfg(any(test, all(target_os = "windows", target_arch = "x86_64")))]
+const IMAGE_FILE_MACHINE_ARM64: u16 = 0xaa64;
+
+#[cfg(any(test, all(target_os = "windows", target_arch = "x86_64")))]
+fn native_machine_is_arm64(native_machine: Option<u16>) -> bool {
+ native_machine == Some(IMAGE_FILE_MACHINE_ARM64)
+}
+
+/// Whether this is the x64 Windows build running under emulation on Windows ARM64.
+///
+/// Only that exact process/host pairing disables the transcribe.cpp GPU path.
+/// Detection is deliberately fail-open: a native x64 host, an older Windows
+/// version without `IsWow64Process2`, or any API error leaves existing behavior
+/// unchanged.
+pub fn is_windows_x64_emulated_on_arm64() -> bool {
+ #[cfg(all(target_os = "windows", target_arch = "x86_64"))]
+ {
+ use std::sync::OnceLock;
+
+ static DETECTED: OnceLock<bool> = OnceLock::new();
+ *DETECTED.get_or_init(|| native_machine_is_arm64(native_windows_machine()))
+ }
+
+ #[cfg(not(all(target_os = "windows", target_arch = "x86_64")))]
+ {
+ false
+ }
+}
+
+#[cfg(all(target_os = "windows", target_arch = "x86_64"))]
+fn native_windows_machine() -> Option<u16> {
+ use windows::core::{s, w, BOOL};
+ use windows::Win32::Foundation::HANDLE;
+ use windows::Win32::System::LibraryLoader::{GetModuleHandleW, GetProcAddress};
+ use windows::Win32::System::Threading::GetCurrentProcess;
+
+ type IsWow64Process2 = unsafe extern "system" fn(HANDLE, *mut u16, *mut u16) -> BOOL;
+
+ // Resolve IsWow64Process2 dynamically so merely starting Handy never raises
+ // the minimum Windows version. Windows-on-ARM versions provide this API,
+ // while a missing symbol or failed query safely preserves the x64 behavior.
+ unsafe {
+ let kernel32 = GetModuleHandleW(w!("kernel32.dll")).ok()?;
+ let address = GetProcAddress(kernel32, s!("IsWow64Process2"))?;
+ // SAFETY: GetProcAddress returned the documented IsWow64Process2 symbol;
+ // function pointers have the same representation on supported Windows.
+ let is_wow64_process2: IsWow64Process2 = std::mem::transmute(address);
+ let mut process_machine = 0u16;
+ let mut native_machine = 0u16;
+ is_wow64_process2(
+ GetCurrentProcess(),
+ &mut process_machine,
+ &mut native_machine,
+ )
+ .as_bool()
+ .then_some(native_machine)
+ }
+}
+
/// Centralized cancellation function that can be called from anywhere in the app.
/// Handles cancelling both recording and transcription operations and updates UI state.
pub fn cancel_current_operation(app: &AppHandle) {
@@ -67,3 +126,16 @@ pub fn is_kde_plasma() -> bool {
pub fn is_kde_wayland() -> bool {
is_wayland() && is_kde_plasma()
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn arm64_native_machine_is_the_only_match() {
+ assert!(native_machine_is_arm64(Some(IMAGE_FILE_MACHINE_ARM64)));
+ assert!(!native_machine_is_arm64(Some(0x8664))); // AMD64
+ assert!(!native_machine_is_arm64(Some(0x014c))); // I386
+ assert!(!native_machine_is_arm64(None)); // API unavailable or failed
+ }
+}
diff --git a/src/components/settings/AccelerationSelector.tsx b/src/components/settings/AccelerationSelector.tsx
index 505b6df0..7a936ab9 100644
--- a/src/components/settings/AccelerationSelector.tsx
+++ b/src/components/settings/AccelerationSelector.tsx
@@ -65,25 +65,30 @@ export const AccelerationSelector: FC<AccelerationSelectorProps> = ({
useEffect(() => {
commands.getAvailableAccelerators().then((available) => {
// Build combined transcribe.cpp options: Auto, [GPU devices...], CPU
- const opts: DropdownOption[] = [
- {
+ const opts: DropdownOption[] = [];
+ if (available.transcribe.includes("auto")) {
+ opts.push({
value: "auto",
label: t("settings.advanced.acceleration.gpuDevice.auto"),
- },
- ];
-
- for (const dev of available.gpu_devices) {
- const vramLabel =
- dev.total_vram_mb >= 1024
- ? `${(dev.total_vram_mb / 1024).toFixed(1)} GB`
- : `${dev.total_vram_mb} MB`;
- opts.push({
- value: `gpu:${dev.id}`,
- label: `${dev.name} (${vramLabel})`,
});
}
- opts.push({ value: "cpu", label: "CPU" });
+ if (available.transcribe.includes("gpu")) {
+ for (const dev of available.gpu_devices) {
+ const vramLabel =
+ dev.total_vram_mb >= 1024
+ ? `${(dev.total_vram_mb / 1024).toFixed(1)} GB`
+ : `${dev.total_vram_mb} MB`;
+ opts.push({
+ value: `gpu:${dev.id}`,
+ label: `${dev.name} (${vramLabel})`,
+ });
+ }
+ }
+
+ if (available.transcribe.includes("cpu")) {
+ opts.push({ value: "cpu", label: "CPU" });
+ }
setTranscribeOptions(opts);
// ORT options (unchanged)
@@ -105,6 +110,11 @@ export const AccelerationSelector: FC<AccelerationSelectorProps> = ({
currentAccelerator as TranscribeAcceleratorSetting,
currentGpuDevice as number,
);
+ const displayedTranscribe = transcribeOptions.some(
+ (option) => option.value === currentTranscribe,
+ )
+ ? currentTranscribe
+ : (transcribeOptions[0]?.value ?? null);
const currentOrt = getSetting("ort_accelerator") ?? "auto";
const handleTranscribeChange = async (value: string) => {
@@ -124,7 +134,7 @@ export const AccelerationSelector: FC<AccelerationSelectorProps> = ({
>
<Dropdown
options={transcribeOptions}
- selectedValue={currentTranscribe}
+ selectedValue={displayedTranscribe}
onSelect={handleTranscribeChange}
disabled={
isUpdating("transcribe_accelerator") ||