From 53c2e8ad91fd5f64989a4e6a923926990130b5cd Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 23 Jul 2024 14:13:28 +0200 Subject: [PATCH] vmimpl: refactor VM type registration Pass Type struct directly during registration. This allows to add additional optional parameters to VM types without changing all VM implementations. We we will need to add SupportsSnapshots flag and one flag to resolve #5028. With this change it will be possible to add "SupportsSnapshots: true" to just one VM type implemenetation. --- vm/adb/adb.go | 4 +++- vm/bhyve/bhyve.go | 5 ++++- vm/cuttlefish/cuttlefish.go | 5 ++++- vm/gce/gce.go | 5 ++++- vm/gvisor/gvisor.go | 5 ++++- vm/isolated/isolated.go | 4 +++- vm/proxyapp/init.go | 7 +++---- vm/qemu/qemu.go | 5 ++++- vm/starnix/starnix.go | 5 ++++- vm/vm_test.go | 4 +++- vm/vmimpl/vmimpl.go | 11 +++++------ vm/vmm/vmm.go | 5 ++++- vm/vmware/vmware.go | 4 +++- 13 files changed, 48 insertions(+), 21 deletions(-) diff --git a/vm/adb/adb.go b/vm/adb/adb.go index 2fd401084ba7..67336f0081d5 100644 --- a/vm/adb/adb.go +++ b/vm/adb/adb.go @@ -27,7 +27,9 @@ import ( ) func init() { - vmimpl.Register("adb", ctor, false) + vmimpl.Register("adb", vmimpl.Type{ + Ctor: ctor, + }) } type Device struct { diff --git a/vm/bhyve/bhyve.go b/vm/bhyve/bhyve.go index 57515ba22171..d00055a32d18 100644 --- a/vm/bhyve/bhyve.go +++ b/vm/bhyve/bhyve.go @@ -21,7 +21,10 @@ import ( ) func init() { - vmimpl.Register("bhyve", ctor, true) + vmimpl.Register("bhyve", vmimpl.Type{ + Ctor: ctor, + Overcommit: true, + }) } type Config struct { diff --git a/vm/cuttlefish/cuttlefish.go b/vm/cuttlefish/cuttlefish.go index 73014cf4e285..dcc825fbf2a2 100644 --- a/vm/cuttlefish/cuttlefish.go +++ b/vm/cuttlefish/cuttlefish.go @@ -28,7 +28,10 @@ const ( ) func init() { - vmimpl.Register("cuttlefish", ctor, true) + vmimpl.Register("cuttlefish", vmimpl.Type{ + Ctor: ctor, + Overcommit: true, + }) } type Pool struct { diff --git a/vm/gce/gce.go b/vm/gce/gce.go index 8de0b56db3c8..e65ee836b81d 100644 --- a/vm/gce/gce.go +++ b/vm/gce/gce.go @@ -35,7 +35,10 @@ import ( ) func init() { - vmimpl.Register("gce", ctor, true) + vmimpl.Register("gce", vmimpl.Type{ + Ctor: ctor, + Overcommit: true, + }) } type Config struct { diff --git a/vm/gvisor/gvisor.go b/vm/gvisor/gvisor.go index ca3a8472ca99..4d0a8230002b 100644 --- a/vm/gvisor/gvisor.go +++ b/vm/gvisor/gvisor.go @@ -26,7 +26,10 @@ import ( ) func init() { - vmimpl.Register(targets.GVisor, ctor, true) + vmimpl.Register(targets.GVisor, vmimpl.Type{ + Ctor: ctor, + Overcommit: true, + }) } type Config struct { diff --git a/vm/isolated/isolated.go b/vm/isolated/isolated.go index 99e7ad78a4d4..3b8b8720d6c8 100755 --- a/vm/isolated/isolated.go +++ b/vm/isolated/isolated.go @@ -24,7 +24,9 @@ import ( const pstoreConsoleFile = "/sys/fs/pstore/console-ramoops-0" func init() { - vmimpl.Register("isolated", ctor, false) + vmimpl.Register("isolated", vmimpl.Type{ + Ctor: ctor, + }) } type Config struct { diff --git a/vm/proxyapp/init.go b/vm/proxyapp/init.go index 467187f4689b..0500298e67a8 100644 --- a/vm/proxyapp/init.go +++ b/vm/proxyapp/init.go @@ -27,12 +27,11 @@ func makeDefaultParams() *proxyAppParams { } func init() { - vmimpl.Register( - "proxyapp", - func(env *vmimpl.Env) (vmimpl.Pool, error) { + vmimpl.Register("proxyapp", vmimpl.Type{ + Ctor: func(env *vmimpl.Env) (vmimpl.Pool, error) { return ctor(makeDefaultParams(), env) }, - false) + }) } // Package configuration VARs are mostly needed for tests. diff --git a/vm/qemu/qemu.go b/vm/qemu/qemu.go index 4d9859c0c973..a8faf54b9c6b 100644 --- a/vm/qemu/qemu.go +++ b/vm/qemu/qemu.go @@ -27,7 +27,10 @@ import ( func init() { var _ vmimpl.Infoer = (*instance)(nil) - vmimpl.Register("qemu", ctor, true) + vmimpl.Register("qemu", vmimpl.Type{ + Ctor: ctor, + Overcommit: true, + }) } type Config struct { diff --git a/vm/starnix/starnix.go b/vm/starnix/starnix.go index 65c921ee3b0b..f722badcf5bf 100644 --- a/vm/starnix/starnix.go +++ b/vm/starnix/starnix.go @@ -25,7 +25,10 @@ import ( func init() { var _ vmimpl.Infoer = (*instance)(nil) - vmimpl.Register(targets.Starnix, ctor, true) + vmimpl.Register(targets.Starnix, vmimpl.Type{ + Ctor: ctor, + Overcommit: true, + }) } type Config struct { diff --git a/vm/vm_test.go b/vm/vm_test.go index 278a4b63d5ba..eb642830fb84 100644 --- a/vm/vm_test.go +++ b/vm/vm_test.go @@ -81,7 +81,9 @@ func init() { ctor := func(env *vmimpl.Env) (vmimpl.Pool, error) { return &testPool{}, nil } - vmimpl.Register("test", ctor, false) + vmimpl.Register("test", vmimpl.Type{ + Ctor: ctor, + }) } type Test struct { diff --git a/vm/vmimpl/vmimpl.go b/vm/vmimpl/vmimpl.go index ce2a0dfb6ab2..ac38a6634dfa 100644 --- a/vm/vmimpl/vmimpl.go +++ b/vm/vmimpl/vmimpl.go @@ -124,15 +124,14 @@ func (err InfraError) InfraError() (string, []byte) { } // Register registers a new VM type within the package. -func Register(typ string, ctor ctorFunc, allowsOvercommit bool) { - Types[typ] = Type{ - Ctor: ctor, - Overcommit: allowsOvercommit, - } +func Register(typ string, desc Type) { + Types[typ] = desc } type Type struct { - Ctor ctorFunc + Ctor ctorFunc + // It's possible to create out-of-thin-air instances of this type. + // Out-of-thin-air instances are used by syz-ci for image testing, patch testing, bisection, etc. Overcommit bool } diff --git a/vm/vmm/vmm.go b/vm/vmm/vmm.go index 6a6242882b4d..246a87fb1ead 100644 --- a/vm/vmm/vmm.go +++ b/vm/vmm/vmm.go @@ -25,7 +25,10 @@ import ( var vmctlStatusRegex = regexp.MustCompile(`^\s+([0-9]+)\b.*\brunning`) func init() { - vmimpl.Register("vmm", ctor, true) + vmimpl.Register("vmm", vmimpl.Type{ + Ctor: ctor, + Overcommit: true, + }) } type Config struct { diff --git a/vm/vmware/vmware.go b/vm/vmware/vmware.go index 210a4957a4d1..56a97e0160f2 100644 --- a/vm/vmware/vmware.go +++ b/vm/vmware/vmware.go @@ -23,7 +23,9 @@ import ( ) func init() { - vmimpl.Register("vmware", ctor, false) + vmimpl.Register("vmware", vmimpl.Type{ + Ctor: ctor, + }) } type Config struct {