From dd5095ca4a3ff3da9f5857ed3d04fb7382cc6e1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jagoba=20Gasc=C3=B3n?= Date: Thu, 28 Dec 2023 12:12:32 +0100 Subject: [PATCH] codegen/delegates: reuse callbacks to avoid reaching go limits Only a limited number of callbacks may be created in a single Go process, and any memory allocated for these callbacks is never released. Between NewCallback and NewCallbackCDecl, at least 1024 callbacks can always be created, but this may not be enough for certain applications. This commit avoids that probly by changing delegates so that all of them share the same four callbacks. fixes #82 --- internal/codegen/templates/delegate.tmpl | 57 +++---- internal/delegate/delegate.go | 139 ++++++++++++++++++ .../asyncoperationcompletedhandler.go | 49 ++---- windows/foundation/typedeventhandler.go | 49 ++---- 4 files changed, 187 insertions(+), 107 deletions(-) create mode 100644 internal/delegate/delegate.go diff --git a/internal/codegen/templates/delegate.tmpl b/internal/codegen/templates/delegate.tmpl index edc92ae..d66bc31 100644 --- a/internal/codegen/templates/delegate.tmpl +++ b/internal/codegen/templates/delegate.tmpl @@ -32,14 +32,17 @@ func New{{.Name}}(iid *ole.GUID, callback {{.Name}}Callback) *{{.Name}} { size := unsafe.Sizeof(*(*{{.Name}})(nil)) instPtr := kernel32.Malloc(size) inst := (*{{.Name}})(instPtr) + + callbacks := delegate.RegisterCallbacks(instPtr, inst) + // Initialize all properties: the malloc may contain garbage inst.RawVTable = (*interface{})(unsafe.Pointer(&{{.Name}}Vtbl{ IUnknownVtbl: ole.IUnknownVtbl{ - QueryInterface: syscall.NewCallback(inst.QueryInterface), - AddRef: syscall.NewCallback(inst.AddRef), - Release: syscall.NewCallback(inst.Release), + QueryInterface: callbacks.QueryInterface, + AddRef: callbacks.AddRef, + Release: callbacks.Release, }, - Invoke: syscall.NewCallback(inst.Invoke), + Invoke: callbacks.Invoke, })) inst.IID = *iid // copy contents inst.Mutex = sync.Mutex{} @@ -54,6 +57,10 @@ func New{{.Name}}(iid *ole.GUID, callback {{.Name}}Callback) *{{.Name}} { return inst } +func (r *{{.Name}}) GetIID() *ole.GUID { + return &r.IID +} + // addRef increments the reference counter by one func (r *{{.Name}}) addRef() uint64 { r.Lock() @@ -74,43 +81,15 @@ func (r *{{.Name}}) removeRef() uint64 { return r.refs } -func (instance *{{.Name}}) QueryInterface(_, iidPtr unsafe.Pointer, ppvObject *unsafe.Pointer) uintptr { - // Checkout these sources for more information about the QueryInterface method. - // - https://docs.microsoft.com/en-us/cpp/atl/queryinterface - // - https://docs.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void) - - if ppvObject == nil { - // If ppvObject (the address) is nullptr, then this method returns E_POINTER. - return ole.E_POINTER - } - - // This function must adhere to the QueryInterface defined here: - // https://docs.microsoft.com/en-us/windows/win32/api/unknwn/nn-unknwn-iunknown - iid := (*ole.GUID)(iidPtr) - if ole.IsEqualGUID(iid, &instance.IID) || ole.IsEqualGUID(iid, ole.IID_IUnknown) || ole.IsEqualGUID(iid, ole.IID_IInspectable){ - *ppvObject = unsafe.Pointer(instance) - } else { - *ppvObject = nil - // Return E_NOINTERFACE if the interface is not supported - return ole.E_NOINTERFACE - } - - // If the COM object implements the interface, then it returns - // a pointer to that interface after calling IUnknown::AddRef on it. - (*ole.IUnknown)(*ppvObject).AddRef() - - // Return S_OK if the interface is supported - return ole.S_OK -} - -func (instance *{{.Name}}) Invoke(instancePtr unsafe.Pointer {{range .InParams -}} - , - {{- if .Type.IsEnum -}} - {{.GoVarName}}Raw {{.Type.UnderlyingEnumType}} +func (instance *{{.Name}}) Invoke(instancePtr, rawArgs0, rawArgs1, rawArgs2, rawArgs3, rawArgs4, rawArgs5, rawArgs6, rawArgs7, rawArgs8 unsafe.Pointer) uintptr { + {{range $i, $arg := .InParams -}} + {{- if $arg.Type.IsEnum -}} + {{$arg.GoVarName}}Raw := ({{$arg.Type.UnderlyingEnumType}})(uintptr(rawArgs{{$i}})) {{- else -}} - {{.GoVarName}}Ptr unsafe.Pointer + {{$arg.GoVarName}}Ptr := rawArgs{{$i}} {{- end}} - {{- end -}}) uintptr { + {{end}} + // See the quote above. {{range .InParams -}} {{if .Type.IsEnum -}} diff --git a/internal/delegate/delegate.go b/internal/delegate/delegate.go new file mode 100644 index 0000000..9d7a68c --- /dev/null +++ b/internal/delegate/delegate.go @@ -0,0 +1,139 @@ +//go:build windows + +package delegate + +import ( + "sync" + "syscall" + "unsafe" + + "github.com/go-ole/go-ole" +) + +// Only a limited number of callbacks may be created in a single Go process, +// and any memory allocated for these callbacks is never released. +// Between NewCallback and NewCallbackCDecl, at least 1024 callbacks can always be created. +var ( + queryInterfaceCallback = syscall.NewCallback(queryInterface) + addRefCallback = syscall.NewCallback(addRef) + releaseCallback = syscall.NewCallback(release) + invokeCallback = syscall.NewCallback(invoke) +) + +// Delegate represents a WinRT delegate class. +type Delegate interface { + GetIID() *ole.GUID + Invoke(instancePtr, rawArgs0, rawArgs1, rawArgs2, rawArgs3, rawArgs4, rawArgs5, rawArgs6, rawArgs7, rawArgs8 unsafe.Pointer) uintptr + AddRef() uint64 + Release() uint64 +} + +// Callbacks contains the syscalls registered on Windows. +type Callbacks struct { + QueryInterface uintptr + AddRef uintptr + Release uintptr + Invoke uintptr +} + +var mutex = sync.RWMutex{} +var instances = make(map[uintptr]Delegate) + +// RegisterCallbacks adds the given pointer and the Delegate it points to to our instances. +// This is required to redirect received callbacks to the correct object instance. +// The function returns the callbacks to use when creating a new delegate instance. +func RegisterCallbacks(ptr unsafe.Pointer, inst Delegate) *Callbacks { + mutex.Lock() + defer mutex.Unlock() + instances[uintptr(ptr)] = inst + + return &Callbacks{ + QueryInterface: queryInterfaceCallback, + AddRef: addRefCallback, + Release: releaseCallback, + Invoke: invokeCallback, + } +} + +func getInstance(ptr unsafe.Pointer) (Delegate, bool) { + mutex.RLock() // locks writing, allows concurrent read + defer mutex.RUnlock() + + i, ok := instances[uintptr(ptr)] + return i, ok +} + +func removeInstance(ptr unsafe.Pointer) { + mutex.Lock() + defer mutex.Unlock() + delete(instances, uintptr(ptr)) +} + +func queryInterface(instancePtr unsafe.Pointer, iidPtr unsafe.Pointer, ppvObject *unsafe.Pointer) uintptr { + instance, ok := getInstance(instancePtr) + if !ok { + // instance not found + return ole.E_POINTER + } + + // Checkout these sources for more information about the QueryInterface method. + // - https://docs.microsoft.com/en-us/cpp/atl/queryinterface + // - https://docs.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void) + + if ppvObject == nil { + // If ppvObject (the address) is nullptr, then this method returns E_POINTER. + return ole.E_POINTER + } + + // This function must adhere to the QueryInterface defined here: + // https://docs.microsoft.com/en-us/windows/win32/api/unknwn/nn-unknwn-iunknown + if iid := (*ole.GUID)(iidPtr); ole.IsEqualGUID(iid, instance.GetIID()) || ole.IsEqualGUID(iid, ole.IID_IUnknown) || ole.IsEqualGUID(iid, ole.IID_IInspectable) { + *ppvObject = instancePtr + } else { + *ppvObject = nil + // Return E_NOINTERFACE if the interface is not supported + return ole.E_NOINTERFACE + } + + // If the COM object implements the interface, then it returns + // a pointer to that interface after calling IUnknown::AddRef on it. + (*ole.IUnknown)(*ppvObject).AddRef() + + // Return S_OK if the interface is supported + return ole.S_OK +} + +func invoke(instancePtr, rawArgs0, rawArgs1, rawArgs2, rawArgs3, rawArgs4, rawArgs5, rawArgs6, rawArgs7, rawArgs8 unsafe.Pointer) uintptr { + instance, ok := getInstance(instancePtr) + if !ok { + // instance not found + return ole.E_FAIL + } + + return instance.Invoke(instancePtr, rawArgs0, rawArgs1, rawArgs2, rawArgs3, rawArgs4, rawArgs5, rawArgs6, rawArgs7, rawArgs8) +} + +func addRef(instancePtr unsafe.Pointer) uint64 { + instance, ok := getInstance(instancePtr) + if !ok { + // instance not found + return ole.E_FAIL + } + + return instance.AddRef() +} + +func release(instancePtr unsafe.Pointer) uint64 { + instance, ok := getInstance(instancePtr) + if !ok { + // instance not found + return ole.E_FAIL + } + + rem := instance.Release() + if rem == 0 { + // remove this delegate + removeInstance(instancePtr) + } + return rem +} diff --git a/windows/foundation/asyncoperationcompletedhandler.go b/windows/foundation/asyncoperationcompletedhandler.go index 92940eb..1624add 100644 --- a/windows/foundation/asyncoperationcompletedhandler.go +++ b/windows/foundation/asyncoperationcompletedhandler.go @@ -7,11 +7,11 @@ package foundation import ( "sync" - "syscall" "time" "unsafe" "github.com/go-ole/go-ole" + "github.com/saltosystems/winrt-go/internal/delegate" "github.com/saltosystems/winrt-go/internal/kernel32" ) @@ -46,14 +46,17 @@ func NewAsyncOperationCompletedHandler(iid *ole.GUID, callback AsyncOperationCom size := unsafe.Sizeof(*(*AsyncOperationCompletedHandler)(nil)) instPtr := kernel32.Malloc(size) inst := (*AsyncOperationCompletedHandler)(instPtr) + + callbacks := delegate.RegisterCallbacks(instPtr, inst) + // Initialize all properties: the malloc may contain garbage inst.RawVTable = (*interface{})(unsafe.Pointer(&AsyncOperationCompletedHandlerVtbl{ IUnknownVtbl: ole.IUnknownVtbl{ - QueryInterface: syscall.NewCallback(inst.QueryInterface), - AddRef: syscall.NewCallback(inst.AddRef), - Release: syscall.NewCallback(inst.Release), + QueryInterface: callbacks.QueryInterface, + AddRef: callbacks.AddRef, + Release: callbacks.Release, }, - Invoke: syscall.NewCallback(inst.Invoke), + Invoke: callbacks.Invoke, })) inst.IID = *iid // copy contents inst.Mutex = sync.Mutex{} @@ -68,6 +71,10 @@ func NewAsyncOperationCompletedHandler(iid *ole.GUID, callback AsyncOperationCom return inst } +func (r *AsyncOperationCompletedHandler) GetIID() *ole.GUID { + return &r.IID +} + // addRef increments the reference counter by one func (r *AsyncOperationCompletedHandler) addRef() uint64 { r.Lock() @@ -88,36 +95,10 @@ func (r *AsyncOperationCompletedHandler) removeRef() uint64 { return r.refs } -func (instance *AsyncOperationCompletedHandler) QueryInterface(_, iidPtr unsafe.Pointer, ppvObject *unsafe.Pointer) uintptr { - // Checkout these sources for more information about the QueryInterface method. - // - https://docs.microsoft.com/en-us/cpp/atl/queryinterface - // - https://docs.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void) - - if ppvObject == nil { - // If ppvObject (the address) is nullptr, then this method returns E_POINTER. - return ole.E_POINTER - } - - // This function must adhere to the QueryInterface defined here: - // https://docs.microsoft.com/en-us/windows/win32/api/unknwn/nn-unknwn-iunknown - iid := (*ole.GUID)(iidPtr) - if ole.IsEqualGUID(iid, &instance.IID) || ole.IsEqualGUID(iid, ole.IID_IUnknown) || ole.IsEqualGUID(iid, ole.IID_IInspectable) { - *ppvObject = unsafe.Pointer(instance) - } else { - *ppvObject = nil - // Return E_NOINTERFACE if the interface is not supported - return ole.E_NOINTERFACE - } - - // If the COM object implements the interface, then it returns - // a pointer to that interface after calling IUnknown::AddRef on it. - (*ole.IUnknown)(*ppvObject).AddRef() - - // Return S_OK if the interface is supported - return ole.S_OK -} +func (instance *AsyncOperationCompletedHandler) Invoke(instancePtr, rawArgs0, rawArgs1, rawArgs2, rawArgs3, rawArgs4, rawArgs5, rawArgs6, rawArgs7, rawArgs8 unsafe.Pointer) uintptr { + asyncInfoPtr := rawArgs0 + asyncStatusRaw := (int32)(uintptr(rawArgs1)) -func (instance *AsyncOperationCompletedHandler) Invoke(instancePtr unsafe.Pointer, asyncInfoPtr unsafe.Pointer, asyncStatusRaw int32) uintptr { // See the quote above. asyncInfo := (*IAsyncOperation)(asyncInfoPtr) asyncStatus := (AsyncStatus)(asyncStatusRaw) diff --git a/windows/foundation/typedeventhandler.go b/windows/foundation/typedeventhandler.go index 1977b75..6def608 100644 --- a/windows/foundation/typedeventhandler.go +++ b/windows/foundation/typedeventhandler.go @@ -7,11 +7,11 @@ package foundation import ( "sync" - "syscall" "time" "unsafe" "github.com/go-ole/go-ole" + "github.com/saltosystems/winrt-go/internal/delegate" "github.com/saltosystems/winrt-go/internal/kernel32" ) @@ -46,14 +46,17 @@ func NewTypedEventHandler(iid *ole.GUID, callback TypedEventHandlerCallback) *Ty size := unsafe.Sizeof(*(*TypedEventHandler)(nil)) instPtr := kernel32.Malloc(size) inst := (*TypedEventHandler)(instPtr) + + callbacks := delegate.RegisterCallbacks(instPtr, inst) + // Initialize all properties: the malloc may contain garbage inst.RawVTable = (*interface{})(unsafe.Pointer(&TypedEventHandlerVtbl{ IUnknownVtbl: ole.IUnknownVtbl{ - QueryInterface: syscall.NewCallback(inst.QueryInterface), - AddRef: syscall.NewCallback(inst.AddRef), - Release: syscall.NewCallback(inst.Release), + QueryInterface: callbacks.QueryInterface, + AddRef: callbacks.AddRef, + Release: callbacks.Release, }, - Invoke: syscall.NewCallback(inst.Invoke), + Invoke: callbacks.Invoke, })) inst.IID = *iid // copy contents inst.Mutex = sync.Mutex{} @@ -68,6 +71,10 @@ func NewTypedEventHandler(iid *ole.GUID, callback TypedEventHandlerCallback) *Ty return inst } +func (r *TypedEventHandler) GetIID() *ole.GUID { + return &r.IID +} + // addRef increments the reference counter by one func (r *TypedEventHandler) addRef() uint64 { r.Lock() @@ -88,36 +95,10 @@ func (r *TypedEventHandler) removeRef() uint64 { return r.refs } -func (instance *TypedEventHandler) QueryInterface(_, iidPtr unsafe.Pointer, ppvObject *unsafe.Pointer) uintptr { - // Checkout these sources for more information about the QueryInterface method. - // - https://docs.microsoft.com/en-us/cpp/atl/queryinterface - // - https://docs.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void) - - if ppvObject == nil { - // If ppvObject (the address) is nullptr, then this method returns E_POINTER. - return ole.E_POINTER - } - - // This function must adhere to the QueryInterface defined here: - // https://docs.microsoft.com/en-us/windows/win32/api/unknwn/nn-unknwn-iunknown - iid := (*ole.GUID)(iidPtr) - if ole.IsEqualGUID(iid, &instance.IID) || ole.IsEqualGUID(iid, ole.IID_IUnknown) || ole.IsEqualGUID(iid, ole.IID_IInspectable) { - *ppvObject = unsafe.Pointer(instance) - } else { - *ppvObject = nil - // Return E_NOINTERFACE if the interface is not supported - return ole.E_NOINTERFACE - } - - // If the COM object implements the interface, then it returns - // a pointer to that interface after calling IUnknown::AddRef on it. - (*ole.IUnknown)(*ppvObject).AddRef() - - // Return S_OK if the interface is supported - return ole.S_OK -} +func (instance *TypedEventHandler) Invoke(instancePtr, rawArgs0, rawArgs1, rawArgs2, rawArgs3, rawArgs4, rawArgs5, rawArgs6, rawArgs7, rawArgs8 unsafe.Pointer) uintptr { + senderPtr := rawArgs0 + argsPtr := rawArgs1 -func (instance *TypedEventHandler) Invoke(instancePtr unsafe.Pointer, senderPtr unsafe.Pointer, argsPtr unsafe.Pointer) uintptr { // See the quote above. sender := (unsafe.Pointer)(senderPtr) args := (unsafe.Pointer)(argsPtr)