Files
reims-vgpu/scripts/browser-probe/guest_default_device.py
T
Anees IqbalandClaude a26f96bb11 🐛 The nil default Metal device was the probe's, not the guest's
`scripts/browser-probe/` (measurement only). Refutes a recorded root cause.

`guest_metal_caps.py` reported `MTLCreateSystemDefaultDevice(): nil` against a
guest whose `MTLCopyAllDevices()` returned one working device. That reading was
carried forward as the root cause of Chrome and Firefox having no GPU here:
ANGLE-Metal asks for the system default at display init, so a nil default
device explained "Initialization of all (1) EGL display types failed", and it
was recorded as a settled dead end not worth retrying.

It is a defect in the probe. Metal resolves the system default through
`CGSCreateDefaultMetalDevice`, which it reaches by `dlopen`ing CoreGraphics and
`dlsym`ing that symbol, and it returns nil with **no fallback** when the lookup
fails. The work sits behind a `dispatch_once`, so the result is latched for the
life of the process. A bare `ctypes` process has no CoreGraphics loaded; a
normal application links it and never sees this.

Measured in the running guest, one process each:

  bare                       -> nil, and still nil after a later dlopen
  dlopen(CoreGraphics) first -> Apple Paravirtual device
  MTLCopyAllDevices first    -> nil

The "still nil after a later dlopen" arm is what identifies the `dispatch_once`
latch rather than a transient. With CoreGraphics loaded before the first Metal
call, the same guest, in the same boot, answers `Apple Paravirtual device`.

So the system default device resolves, and whatever stops Chrome reaching the
GPU is still unknown. Adds `guest_default_device.py`, which walks Metal's four
nil-producing exits in one process and prints each input before the answer, so
a future nil names its own cause instead of needing a second run: it reports
the device array, the `deviceNameMatch` preference, the CoreGraphics dlopen and
dlsym, the direct `CGSCreateDefaultMetalDevice` result, and only then the
question itself. Live output ends `VERDICT: the system default device resolves.
Any browser that reports no GPU is failing for some other reason.`

Verification: both probes run against the live x86 guest (macOS 13.7.8,
`reims-vgpu-pci`); `guest_metal_caps.py` now reads `ok` where it read `nil`
minutes earlier in the same boot, which is the before/after for this change.

Not verified: why Chrome's GPU process actually fails. This commit removes a
wrong answer, it does not supply the right one, and the flag combinations
previously abandoned on the strength of that wrong answer are now untested
rather than known-bad.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-01 23:36:50 +03:00

147 lines
6.4 KiB
Python

#!/usr/bin/env python3
"""Ask the guest why `MTLCreateSystemDefaultDevice()` answers the way it does.
Run inside the guest: python3 guest_default_device.py
WHY THIS EXISTS
A nil system default device is the whole reason Chrome has no GPU here:
ANGLE-Metal asks for the system default at display init and gives up when it is
nil, so the GPU process never boots. `guest_metal_caps.py` reports *that* it is
nil. It cannot report *why*, and the why decides whether this is something the
host can influence at all.
Reading Metal.framework's own selection path gives four exits, and they have
completely different consequences:
1. the CoreGraphics dlsym fails -> returns nil with NO fallback
2. a `deviceNameMatch` preference is set and matches nothing -> nil
3. the device array is empty -> nil, and the device is really absent
4. otherwise CGSCreateDefaultMetalDevice, and if that is nil, the first
entry of the same array `MTLCopyAllDevices()` returns
Exit 4 is the important one: it means a nil default device alongside a non-empty
`MTLCopyAllDevices()` should be **impossible**. If this probe reports exactly
that, then the selection path is not what is being executed and the premise
needs re-deriving; if it reports a match on 1 or 2, the cause is guest process
environment and no host change can address it.
Each step is printed unconditionally and in order, so the output distinguishes
the exits by itself rather than needing a second run to narrow down.
"""
import ctypes
import os
import sys
libc = ctypes.CDLL(None)
libc.dlopen.restype = ctypes.c_void_p
libc.dlopen.argtypes = [ctypes.c_char_p, ctypes.c_int]
libc.dlsym.restype = ctypes.c_void_p
libc.dlsym.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
objc = ctypes.CDLL("/usr/lib/libobjc.A.dylib")
objc.sel_registerName.restype = ctypes.c_void_p
objc.sel_registerName.argtypes = [ctypes.c_char_p]
CF = ctypes.CDLL("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")
CF.CFStringCreateWithCString.restype = ctypes.c_void_p
CF.CFStringCreateWithCString.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_uint32]
CF.CFPreferencesCopyAppValue.restype = ctypes.c_void_p
CF.CFPreferencesCopyAppValue.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
CF.CFStringGetCString.restype = ctypes.c_bool
CF.CFStringGetCString.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_long, ctypes.c_uint32]
CF.CFGetTypeID.restype = ctypes.c_ulong
CF.CFGetTypeID.argtypes = [ctypes.c_void_p]
CF.CFStringGetTypeID.restype = ctypes.c_ulong
kCFStringEncodingUTF8 = 0x08000100
RTLD_LAZY = 0x1
CORE_GRAPHICS = b"/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics"
def cfstr(text):
return CF.CFStringCreateWithCString(None, text.encode(), kCFStringEncodingUTF8)
def from_cfstr(ref):
"""Render a CFStringRef, or describe a non-string so a wrong type is visible."""
if not ref:
return None
if CF.CFGetTypeID(ref) != CF.CFStringGetTypeID():
return "<non-string CFTypeRef>"
buf = ctypes.create_string_buffer(512)
if CF.CFStringGetCString(ref, buf, len(buf), kCFStringEncodingUTF8):
return buf.value.decode(errors="replace")
return "<undecodable>"
def msg(restype, obj, selector, *args, argtypes=None):
"""One `objc_msgSend` call, with restype/argtypes bound explicitly.
ctypes caches attributes on the shared function object, so a type left over
from a previous call would silently apply to this one; rebinding per call is
the price of not writing a compiled helper. argtypes is never derived from
the values, because a derived type cannot express an out-parameter.
"""
fn = objc.objc_msgSend
fn.restype = restype
fn.argtypes = [ctypes.c_void_p, ctypes.c_void_p] + list(argtypes or [])
return fn(obj, objc.sel_registerName(selector), *args)
def main():
metal = ctypes.CDLL("/System/Library/Frameworks/Metal.framework/Metal")
metal.MTLCreateSystemDefaultDevice.restype = ctypes.c_void_p
metal.MTLCopyAllDevices.restype = ctypes.c_void_p
# Step 3 first: the device array is what every other exit is judged against.
arr = metal.MTLCopyAllDevices()
count = msg(ctypes.c_ulonglong, arr, b"count") if arr else 0
print(f"MTLCopyAllDevices count : {count}")
for i in range(count):
dev = msg(ctypes.c_void_p, arr, b"objectAtIndex:", ctypes.c_ulonglong(i),
argtypes=[ctypes.c_ulonglong])
name = msg(ctypes.c_void_p, dev, b"name")
print(f" [{i}] name : {from_cfstr(name)}")
# Step 2: a name filter that matches nothing returns nil with a live device.
pref = CF.CFPreferencesCopyAppValue(cfstr("deviceNameMatch"), cfstr("com.apple.Metal"))
print(f"deviceNameMatch pref : {from_cfstr(pref)!r}")
# Anything that restricts registration by registry id, read at Metal init.
for var in ("ALLOWED_GPU_IDS", "MTL_DEVICE_WRAPPER_TYPE", "MTL_HUD_ENABLED",
"MTL_DEBUG_LAYER", "CGL_DEVICE_MASK"):
if var in os.environ:
print(f"env {var:24}: {os.environ[var]!r}")
# Step 1: the one exit that returns nil with no fallback at all.
handle = libc.dlopen(CORE_GRAPHICS, RTLD_LAZY)
print(f"dlopen(CoreGraphics) : {'ok' if handle else 'FAILED'}")
sym = libc.dlsym(handle, b"CGSCreateDefaultMetalDevice") if handle else None
print(f"dlsym(CGSCreateDefaultMetal): {'ok' if sym else 'NULL -> nil with no fallback'}")
if sym:
cgs = ctypes.CFUNCTYPE(ctypes.c_void_p)(sym)
got = cgs()
print(f"CGSCreateDefaultMetalDevice : {'nil' if not got else from_cfstr(msg(ctypes.c_void_p, got, b'name'))}")
# And the question itself, asked last so every input above is already shown.
default = metal.MTLCreateSystemDefaultDevice()
print(f"MTLCreateSystemDefaultDevice: "
f"{'nil' if not default else from_cfstr(msg(ctypes.c_void_p, default, b'name'))}")
if not default and count:
print("VERDICT: nil default device with a NON-EMPTY device array — the "
"documented fallback did not run; re-derive the selection path.")
elif not default:
print("VERDICT: the device array is empty; the device is genuinely absent "
"to Metal, so this is not a default-device selection problem.")
else:
print("VERDICT: the system default device resolves. Any browser that "
"reports no GPU is failing for some other reason.")
return 0
if __name__ == "__main__":
sys.exit(main())