← Back to Pull requests

Use generated purego bindings for the native bridge #241

Open
unknown opened this pull request with 451 lines diff across 5 files
View on GitHub
unknown unknown commented on Oct 01, 2026 03:37 UTC
Author

Replace handwritten cgo API wrappers with Go bindings generated from macOS SDK declarations and Objective-C runtime metadata. The public Go API remains unchanged.

Most API calls use purego. A small Objective-C bridge handles callbacks, dispatch queues, file descriptors, and window UI. A normal go build compiles these helpers from source and dynamically links Apple's system frameworks. Consumers need no Swift compiler, prebuilt bridge archive, or generation step. The same sources work with go mod vendor. Builds still require CGO_ENABLED=1 and Xcode or Command Line Tools.

Native object lifetime continues to follow Go GC, as it did before. The implementation replaces runtime.SetFinalizer with runtime.AddCleanup on a shared owner. Go pointer value copies share that owner and keep the underlying native reference alive.

flowchart TD
    A["All Go references, including value copies, become unreachable"]
    B["GC finds the shared owner unreachable"]
    C["runtime.AddCleanup callback runs"]
    D["Objective-C release runs on the native queue"]

    A --> B
    B -->|"asynchronous cleanup"| C
    C -->|"queued release"| D

GC and native memory release are not simultaneous. Cleanup runs asynchronously, and the native release is queued. An Objective-C release drops one owned reference; other native owners can still keep the object alive. Generated wrappers use runtime.KeepAlive to keep Go arguments alive through native calls.

Private runtime bindings check receivers and method encodings before dispatch. They remain internal and unsafe because runtime metadata does not describe ownership or variadic arguments.

For review, start with the generators, ownership handling, native helpers, and public wrappers. Commit 4e530d1 contains only the seven generated files, including the SDK contract tests. Review the code that produces them rather than each generated method. To reproduce the output, run:

go generate ./internal/vzbridge

Some intermediate commits depend on later migration changes. Build and test from the final branch tip.

Compared with main (c03d09a), this branch (923e91a) builds faster but takes longer to execute the measured host-side API calls.

On an Apple M2 running macOS 27.0.1, Go 1.27.1, and Xcode 27.0, building example/gui-linux with an empty Go build cache took 5.60 seconds instead of 16.13 seconds, a 65.3% reduction. These are medians of three runs per version, alternating execution order, with dependencies already downloaded. Unchanged builds with a populated cache took about 0.08 seconds for both versions.

Runtime benchmarks showed the following regressions. Each result is the median of ten alternating runs per version, using identical benchmark code with GOMAXPROCS=4.

| API | Main | This branch | Execution time |
|---|---:|---:|---:|
| VirtualMachineConfigurationMaximumAllowedCPUCount | 22.74 ns | 1.31 µs | 57.6× longer |
| MACAddress.String | 411.4 ns | 18.75 µs | 45.6× longer |
| VirtioNetworkDeviceConfiguration.SetMACAddress | 46.83 ns | 1.15 µs | 24.5× longer |
| NewNATNetworkDeviceAttachment | 416.6 ns | 2.34 µs | 5.6× longer |

Go heap allocations also increased in all four benchmarks. These measurements cover host-side API operations, not guest execution throughput, VM boot time, or overall application runtime.

Related to #239 and #240.

Fixes #239

Diff: 451 lines (+338 / -113) across 5 files Reviewer: AkihiroSuda Review duration: 4261m
All Vouch review confidence checks passed
Residual risk 0.26 = Change Risk (0.45) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 451 lines diff across 5 files
0.45
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
42%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.366
Shallow Review Depth shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #241"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 43.0% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 45.4% (MEDIUM) · Higher = larger blast radius
45.4%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 43.0% · Lower = rushed or shallow review
43.0%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 25.9% · Safe to merge
25.9%
Repository