Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
---
BasedOnStyle: WebKit
Language: ObjC
TabWidth: 4
PointerAlignment: Right
---
BasedOnStyle: WebKit
Language: Cpp
TabWidth: 4
PointerAlignment: Right
2 changes: 1 addition & 1 deletion .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
test:
needs: formatting-check
runs-on: ${{ matrix.os }}
timeout-minutes: 3
timeout-minutes: 5
strategy:
fail-fast: false
# Can't expand the matrix due to the flakiness of the CI infra
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fmt:

.PHONY: test
test:
go test -p 1 -exec "go run $(PWD)/cmd/codesign" ./... -timeout 2m -v
go test -p 1 -exec "go run $(PWD)/cmd/codesign" ./... -timeout 4m -v

.PHONY: test/run
test/run:
Expand Down
2 changes: 1 addition & 1 deletion cgoutil.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package vz

/*
#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c
#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c -fno-objc-arc
#cgo darwin LDFLAGS: -lobjc -framework Foundation
#import <Foundation/Foundation.h>

Expand Down
1 change: 1 addition & 0 deletions example/macOS/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ require github.com/Code-Hex/vz/v3 v3.0.0-00010101000000-000000000000
require (
github.com/Code-Hex/go-infinity-channel v1.0.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sys v0.39.0 // indirect
)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ require (
golang.org/x/mod v0.22.0
)

require golang.org/x/sys v0.39.0 // indirect
require golang.org/x/sys v0.39.0
40 changes: 40 additions & 0 deletions internal/cgohandler/cgohandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cgohandler

import (
"runtime"
"runtime/cgo"
)

// Handler holds a cgo.Handle for an Object.
// It provides methods to hold and release the handle.
// handle will released when Handler is cleaned up.
type Handler struct {
handle cgo.Handle
}

// releaseOnCleanup registers a cleanup function to delete the cgo.Handle when cleaned up.
func (h *Handler) releaseOnCleanup() {
runtime.AddCleanup(h, func(h cgo.Handle) {
h.Delete()
}, h.handle)
}

// New creates a new [Handler] and holds the given value.
func New(v any) (*Handler, uintptr) {
if v == nil {
return nil, 0
}
h := &Handler{cgo.NewHandle(v)}
h.releaseOnCleanup()
return h, uintptr(h.handle)
}

// Unwrap unwraps the cgo.Handle from the given uintptr and returns the associated value.
// It does NOT delete the handle; it expects the handle to be managed by [Handler] or caller.
func Unwrap[T any](handle uintptr) T {
if handle == 0 {
var zero T
return zero
}
return cgo.Handle(handle).Value().(T)
}
3 changes: 3 additions & 0 deletions internal/objc/objc.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ func Retain(o NSObject) {

// Ptr returns unsafe.Pointer of the NSObject
func Ptr(o NSObject) unsafe.Pointer {
if o == nil {
return nil
}
return o.ptr()
}

Expand Down
38 changes: 19 additions & 19 deletions osversion.go → internal/osversion/osversion.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package vz
package osversion

/*
#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c -fno-objc-arc
Expand Down Expand Up @@ -27,24 +27,24 @@ var (
ErrBuildTargetOSVersion = errors.New("unsupported build target macOS version")
)

func macOSAvailable(version float64) error {
func MacOSAvailable(version float64) error {
if macOSMajorMinorVersion() < version {
return ErrUnsupportedOSVersion
}
return macOSBuildTargetAvailable(version)
return MacOSBuildTargetAvailable(version)
}

var (
majorMinorVersion float64
majorMinorVersionOnce interface{ Do(func()) } = &sync.Once{}
MajorMinorVersion float64
MajorMinorVersionOnce interface{ Do(func()) } = &sync.Once{}

// This can be replaced in the test code to enable mock.
// It will not be changed in production.
sysctl = syscall.Sysctl
Sysctl = syscall.Sysctl
)

func fetchMajorMinorVersion() (float64, error) {
osver, err := sysctl("kern.osproductversion")
func FetchMajorMinorVersion() (float64, error) {
osver, err := Sysctl("kern.osproductversion")
if err != nil {
return 0, err
}
Expand All @@ -58,34 +58,34 @@ func fetchMajorMinorVersion() (float64, error) {
}

func macOSMajorMinorVersion() float64 {
majorMinorVersionOnce.Do(func() {
version, err := fetchMajorMinorVersion()
MajorMinorVersionOnce.Do(func() {
version, err := FetchMajorMinorVersion()
if err != nil {
panic(err)
}
majorMinorVersion = version
MajorMinorVersion = version
})
return majorMinorVersion
return MajorMinorVersion
}

var (
maxAllowedVersion int
maxAllowedVersionOnce interface{ Do(func()) } = &sync.Once{}
MaxAllowedVersion int
MaxAllowedVersionOnce interface{ Do(func()) } = &sync.Once{}

getMaxAllowedVersion = func() int {
return int(C.mac_os_x_version_max_allowed())
}
)

func fetchMaxAllowedVersion() int {
maxAllowedVersionOnce.Do(func() {
maxAllowedVersion = getMaxAllowedVersion()
MaxAllowedVersionOnce.Do(func() {
MaxAllowedVersion = getMaxAllowedVersion()
})
return maxAllowedVersion
return MaxAllowedVersion
}

// macOSBuildTargetAvailable checks whether the API available in a given version has been compiled.
func macOSBuildTargetAvailable(version float64) error {
// MacOSBuildTargetAvailable checks whether the API available in a given version has been compiled.
func MacOSBuildTargetAvailable(version float64) error {
allowedVersion := fetchMaxAllowedVersion()
if allowedVersion == 0 {
return fmt.Errorf("undefined __MAC_OS_X_VERSION_MAX_ALLOWED: %w", ErrBuildTargetOSVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ NSDictionary *dumpProcessinfo();
#pragma message("macOS 15 API has been disabled")
#endif

// for macOS 15.4 API
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 150400
#define INCLUDE_TARGET_OSX_15_4 1
#else
#pragma message("macOS 15.4 API has been disabled")
#endif

// for macOS 26 API
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 260000
#define INCLUDE_TARGET_OSX_26 1
#else
#pragma message("macOS 26 API has been disabled")
#endif

static inline int mac_os_x_version_max_allowed()
{
#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
Expand Down
File renamed without changes.
49 changes: 48 additions & 1 deletion network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package vz

/*
#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c -fno-objc-arc
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization -framework vmnet
# include "virtualization_11.h"
# include "virtualization_13.h"
# include "virtualization_26.h"
*/
import "C"
import (
Expand All @@ -14,6 +15,7 @@ import (
"syscall"

"github.com/Code-Hex/vz/v3/internal/objc"
"github.com/Code-Hex/vz/v3/vmnet"
)

// BridgedNetwork defines a network interface that bridges a physical interface with a virtual machine.
Expand Down Expand Up @@ -260,6 +262,51 @@ func (f *FileHandleNetworkDeviceAttachment) MaximumTransmissionUnit() int {
return f.mtu
}

// VmnetNetworkDeviceAttachment represents a vmnet network device attachment.
//
// This attachment is used to connect a virtual machine to a vmnet network.
// The attachment is created with a VmnetNetwork and can be used with a VirtioNetworkDeviceConfiguration.
// see: https://developer.apple.com/documentation/virtualization/vzvmnetnetworkdeviceattachment?language=objc
//
// This is only supported on macOS 26 and newer, error will
// be returned on older versions.
type VmnetNetworkDeviceAttachment struct {
*pointer

*baseNetworkDeviceAttachment
}

func (*VmnetNetworkDeviceAttachment) String() string {
return "VmnetNetworkDeviceAttachment"
}

func (v *VmnetNetworkDeviceAttachment) Network() *vmnet.Network {
ptr := C.VZVmnetNetworkDeviceAttachment_network(objc.Ptr(v))
return vmnet.NewNetworkFromPointer(objc.NewPointer(ptr))
}

var _ NetworkDeviceAttachment = (*VmnetNetworkDeviceAttachment)(nil)

// NewVmnetNetworkDeviceAttachment creates a new VmnetNetworkDeviceAttachment with network.
//
// This is only supported on macOS 26 and newer, error will
// be returned on older versions.
func NewVmnetNetworkDeviceAttachment(network *vmnet.Network) (*VmnetNetworkDeviceAttachment, error) {
if err := macOSAvailable(26); err != nil {
return nil, err
}

attachment := &VmnetNetworkDeviceAttachment{
pointer: objc.NewPointer(
C.newVZVmnetNetworkDeviceAttachment(objc.Ptr(network)),
),
}
objc.SetFinalizer(attachment, func(self *VmnetNetworkDeviceAttachment) {
objc.Release(self)
})
return attachment, nil
}

// NetworkDeviceAttachment for a network device attachment.
// see: https://developer.apple.com/documentation/virtualization/vznetworkdeviceattachment?language=objc
type NetworkDeviceAttachment interface {
Expand Down
28 changes: 28 additions & 0 deletions osversion_alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package vz

import "github.com/Code-Hex/vz/v3/internal/osversion"

var (
// ErrUnsupportedOSVersion is returned when calling a method which is only
// available in newer macOS versions.
ErrUnsupportedOSVersion = osversion.ErrUnsupportedOSVersion

// ErrBuildTargetOSVersion indicates that the API is available but the
// running program has disabled it.
ErrBuildTargetOSVersion = osversion.ErrBuildTargetOSVersion

macOSAvailable = osversion.MacOSAvailable

// MacOSBuildTargetAvailable checks whether the API available in a given version has been compiled.
macOSBuildTargetAvailable = osversion.MacOSBuildTargetAvailable
)

// for Testing
var (
fetchMajorMinorVersion = osversion.FetchMajorMinorVersion
majorMinorVersion = &osversion.MajorMinorVersion
majorMinorVersionOnce = &osversion.MajorMinorVersionOnce
maxAllowedVersion = &osversion.MaxAllowedVersion
maxAllowedVersionOnce = &osversion.MaxAllowedVersionOnce
sysctl = &osversion.Sysctl
)
12 changes: 6 additions & 6 deletions osversion_arm64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
)

func TestAvailableVersionArm64(t *testing.T) {
majorMinorVersionOnce = &nopDoer{}
*majorMinorVersionOnce = &nopDoer{}
defer func() {
majorMinorVersion = 0
majorMinorVersionOnce = &sync.Once{}
*majorMinorVersion = 0
*majorMinorVersionOnce = &sync.Once{}
}()
t.Run("macOS 12", func(t *testing.T) {
majorMinorVersion = 11
*majorMinorVersion = 11
cases := map[string]func() error{
"NewMacOSBootLoader": func() error {
_, err := NewMacOSBootLoader()
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestAvailableVersionArm64(t *testing.T) {
t.Skip("disabled build target for macOS 13")
}

majorMinorVersion = 12.3
*majorMinorVersion = 12.3
cases := map[string]func() error{
"WithStartUpFromMacOSRecovery": func() error {
return (*VirtualMachine)(nil).Start(WithStartUpFromMacOSRecovery(true))
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestAvailableVersionArm64(t *testing.T) {
}
defer f.Close()

majorMinorVersion = 13
*majorMinorVersion = 13
cases := map[string]func() error{
"NewLinuxRosettaUnixSocketCachingOptions": func() error {
_, err := NewLinuxRosettaUnixSocketCachingOptions(filename)
Expand Down
Loading