Repository navigation
Fix kext build failure caused by ghost root hubs with zero VID/PID - #80
Open
chenofchina wants to merge 3 commits into
Open
chenofchina wants to merge 3 commits into
chenofchina wants to merge 3 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
现象
在部分 Windows 机器上,
usbdump.exe会报告一个 VID 和 PID 均为 0 的幽灵 root hub(例如ROOT\USB\0000,设备描述为USB Composite Device)。这个控制器是 Windows USB 协议栈伪造出来的,没有 PCI ID、没有 ACPI path、也没有 BDF。BaseUSBMap.build_kext()会遍历controllers_historical中的全部控制器来生成IOKitPersonalities,该幽灵 root hub 因此被一并送入choose_matching_key()。该函数依次尝试bus_number/acpi_path/bdf/path/pci_id五种标识符,幽灵 hub 一个都不满足,最终在base.py:508抛出RuntimeError,整个 kext 构建过程中断。由于
build_kext()的遍历范围与端口选择无关,这个问题无法通过取消勾选端口绕过——只要执行K. Build UTBMap.kext,构建必定失败。复现步骤
Windows.exe;D. Discover Ports完成端口发现;S. Select Ports and Build Kext;K(Build UTBMap.kext)——崩溃。完整 traceback
发现列表中的原始输出
usbdump.exe报告的控制器如下(字段取自原始 JSON):ROOT\USB\0000PCI\VEN_8086&DEV_A0ED&SUBSYS_201F1043&REV_20\3&11583659&0&A00x8086)0xa0ed)['8086','a0ed','201f','1043']对应地,程序内的打印结果:
第 1 条即幽灵 root hub,是本 issue 的成因。
修复说明
提交 1 —
Skip ghost root hubs with zero vendor/product id during discovery在
Scripts/usbdump.py::get_controllers()中,于 serialization 之前跳过 VID/PID 均为0(或0xFFFF)的 root hub。理由:
if all(controller[i] not in [0, 0xFFFF] ...)判据本身就认为该值非法(否则不会跳过pci_id赋值),此处提前拦截与既有语义一致;0xFFFF一并纳入,因为它是 PCI 的 vendor 通配值,同样不代表真实设备。提交 2 —
Avoid RuntimeError when no unique controller identifier is available保留
choose_matching_key()的原有优先级链不变,仅把末尾的raise RuntimeError改为回退到基于 instance ID 的IOPropertyMatch。理由:
No matching key available对用户没有可操作性,无法据此自行解决。提交 3 —
Avoid KeyError when controller has bdf but no pci_idScripts/usbdump.py中SubSysID分支对pci_id使用+=,但pci_id只在上一个if成立时才被创建。若某控制器拥有有效的 BDF 却 VID/PID 非法,pci_id不存在,+=将抛出KeyError。改用setdefault("pci_id", []).extend(...)。该提交与提交 1 独立:即使不提提交 1,这个
KeyError依然可达。前后对比
修复前(
master,实测):修复后(本分支,实测):
IOKitPersonalities正常生成,端口映射按预期写入。测试说明
测试环境: Windows,Python 3.11.0,仓库 HEAD
0c6823a+ 本 PR 三个提交。测试方式: 绕过交互式 TUI(
utils.py的msvcrt.kbhit()轮询在重定向 stdin 下无法驱动),直接调用Scripts.usbdump.get_controllers(),并对返回的每个控制器调用BaseUSBMap.choose_matching_key(),验证不再抛出异常且能产出有效的匹配键。重要说明: 该测试环境无法访问 PyPI(
pip出站 HTTPS 被拦截,镜像同样不可达),因此termcolor2无法安装。为能导入base.py,测试脚本中注入了一个仅供本地使用的termcolor2占位模块。from termcolor2 import c as color这一行的导入(使用处为print()时对字符串做 ANSI 着色),不参与任何控制器发现或匹配键生成逻辑;未覆盖: 未在真实 macOS 上加载生成的 kext 验证端口生效情况;未在 macOS 侧运行
macOS.py。这两项超出本机(Windows)条件。变更范围
Scripts/usbdump.py:+7 / -1base.py:+3 / -1未包含任何格式化、重命名或与上述 bug 无关的改动。