Skip to content

Conversation

@sharder996
Copy link
Collaborator

@sharder996 sharder996 commented Dec 9, 2025

This PR implements some of the basic functions for manipulating the state of a virtual machine using the Apple Virtualization Framework. This includes:

  • start()
  • shutdown(ShutdownPolicy shutdown_policy)
  • suspend()

All Objective-C++ code, other helper functions needed to perform these actions, and unit tests for these functions are also included.

On top of being able to manually manage memory, Objective-C uses Automatic Reference Counting (ARC) where the system reference counting to automatically insert appropriate memory management method calls for you at compile-time. However, in order for objects to persist across the Objective-C/C++ boundary, we must use some types from the CoreFoundation library instead of the Foundation library, i.e., CFErrorRef over NSError, also known as Toll-Free Bridged Types. This allows us to control who owns the object and avoids having ARC and the Objective-C memory management system release memory before we are done with it. There are several macros that are used to tell the compiler about the ownership semantics of an object, but we mainly use:

  • __bridge; for objects such as the VMHandle which were declared on the C++ side and where we don't want ownership to transfer when casting the std::shared_ptr<void> to virtualization framework virtual machine type; VZVirtualMachine.

  • __bridge_retained: for objects that were created on the Objective-C side and for which we want to persist even after the block in which they were created finishes. We are responsible for manually freeing this type of object, so care must be taken in order when using this macro to prevent memory leaks. The use of this macro here is limited to returning errors from the Objective-C side to the C++ side through the use of the custom C++ type; CFError, which automatically calls CFRelease when destructed.

The other unique memory management mechanism that is used is the autoreleasepool block. This is used in special circumstances when a large number of temporary autoreleased object. The autoreleasepool sends an autorelease message to all objects in that block which are immediately released instead of at the end of the current event-loop iteration. This prevents temporary objects from unnecessarily accumulating and causing excessive overhead.

Additional reading:


MULTI-2258
MULTI-2261

@sharder996 sharder996 force-pushed the feature/apple-vz-boilerplate branch from d87a67b to 30ad1db Compare December 9, 2025 15:28
@sharder996 sharder996 force-pushed the feature/apple/basic-vm-functions branch from f7a30a3 to 7078e60 Compare December 9, 2025 15:30
@codecov
Copy link

codecov bot commented Dec 9, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.20%. Comparing base (8fd2e2f) to head (0c4ddca).

Additional details and impacted files
@@                       Coverage Diff                        @@
##           feature/apple-vz-boilerplate    #4561      +/-   ##
================================================================
+ Coverage                         87.19%   87.20%   +0.01%     
================================================================
  Files                               246      246              
  Lines                             14134    14134              
================================================================
+ Hits                              12323    12324       +1     
+ Misses                             1811     1810       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

sharder996 and others added 27 commits December 10, 2025 14:46
We wrap all calls into the Virtualization Framework in a mockable
singleton for testing purposes
Co-authored-by: Mustafa Kemal Gılor <mustafa.gilor@canonical.com>
Signed-off-by: ScottH <59572507+sharder996@users.noreply.github.com>
Many of these operations can be performed asynchronously, but Multipass
is not so we block until the operation has finished.
Keep ObjCpp code in the bridge and return custom Cpp types for objects
that need to be accessed outside the bridge. This helps maintain memory
safety and reference counting.
We prefer to contain logic in the VM class so it can be properly tested.
@sharder996 sharder996 force-pushed the feature/apple-vz-boilerplate branch from 6fda222 to cd9efab Compare December 10, 2025 20:46
@sharder996 sharder996 closed this Dec 11, 2025
@sharder996 sharder996 force-pushed the feature/apple/basic-vm-functions branch from 7078e60 to cd9efab Compare December 11, 2025 15:56
@sharder996 sharder996 reopened this Dec 11, 2025
@sharder996 sharder996 force-pushed the feature/apple/basic-vm-functions branch from cd9efab to 1773097 Compare December 11, 2025 18:34
@sharder996 sharder996 changed the title Feature/apple/basic vm functions [applevz] Basic VM functions Dec 12, 2025
@sharder996 sharder996 force-pushed the feature/apple/basic-vm-functions branch from 76f3d57 to febbbeb Compare December 12, 2025 06:17
@sharder996 sharder996 mentioned this pull request Dec 14, 2025
@sharder996 sharder996 force-pushed the feature/apple-vz-boilerplate branch 4 times, most recently from d655f8e to 0d4f2d5 Compare January 16, 2026 03:30
@sharder996 sharder996 force-pushed the feature/apple-vz-boilerplate branch 3 times, most recently from f895466 to 7519cea Compare January 22, 2026 01:08
Base automatically changed from feature/apple-vz-boilerplate to main January 22, 2026 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants